/**********************************************
* icol.js                                     *  
***********************************************
* Icol version juin 2004                      *
* Dsi St Malo				                  *
**********************************************/

function isFormSearchBasicOk(m1,y1,m2,y2,champImo){

	if (isPeriodeOk(m1,y1,m2,y2) && isNumImoOk(champImo)){
		return true;
	}else{
		return false;
	}
}


function isFormSearchAdvancedOk(m1,y1,m2,y2,champImo,minGT,maxGT,minYB,maxYB){

	if (isPeriodeOk(m1,y1,m2,y2)&&isNumImoOk(champImo)&&isGTOk(minGT)&&isGTOk(maxGT)&&isGTCompare(minGT,maxGT)&&isYBOk(minYB)&&isYBOk(maxYB)&&isYBCompare(minYB,maxYB)&&isPortInspOK()){
		return true;
	}else{
		return false;
	}
}

/*
*	Fonction isPeriodeOk()
*		4 paramètres en entrée : mois et années de 2 jours
*					  			  (m1,y1,m2,y2)
*	    retourne 			   : true si le jour identifié par m1/y1 est antérieur au jour 2 identifié par m2/y2
*	    		   				  false dans le cas contraire
*/
function isPeriodeOk(m1,y1,m2,y2){
if (m2="00"){m2="12"}
var p1 = y1+m1;
var p2 = y2+m2;
var ip1 = 0;
var ip2 = 0;
	ip1 = parseInt(p1);
	ip2 = parseInt(p2);
	if (ip1 <= ip2){
		return true;
	}else
	{
		alert("Error on field 'Period' : the beginning date should be lower than the ending date.");
		return false;
	}
}
function isNumber(champ){
var vNum=champ.value;
	if(Number(vNum) == 0){
		champ.value="";
		return true;
	}else{
		if(isNaN(Number(vNum))){
			alert("Error on field : it should be a number.");
			champ.focus();
			return false;
		}else{
			champ.value=Number(vNum);
			return true;
		}
	}
}

/*
* 	Fonction isNumImoOk()
*		1 paramètre en entrée	:	le champ du numéro imo saisi
*		retourne				:	true si numéro OK
*									false si numéro pas OK
*/
function isNumImoOk(champImo){
var som = 0;
var reste = 0;
var pImo = champImo.value;
	if(Number(pImo) == 0){
		champImo.value="";
		return true;
	}else{
		if(isNaN(Number(pImo))){
			alert("Error on field 'Imo number' : it should be a number.");
			champImo.focus();
			return false;
		}else if(parseInt(pImo)<999999 || parseInt(pImo)>9999999){
			alert("Error on field 'Imo number' : it should have 7 digits.");
			champImo.focus();
			return false;
		}else{
			som = (parseInt(pImo.substr(0,1))*7) + (parseInt(pImo.substr(1,1))*6) ;
			som = som +(parseInt(pImo.substr(2,1))*5) + (parseInt(pImo.substr(3,1))*4) ;
			som = som +(parseInt(pImo.substr(4,1))*3) + (parseInt(pImo.substr(5,1))*2) ;
			som = som +(parseInt(pImo.substr(6,1))*1);
			reste = (som - parseInt(pImo.substr(6,1))) % 10;
			if (reste != parseInt(pImo.substr(6,1)) ){
				alert("Error on field 'Imo number' : it is not valid.");
				champImo.focus();
				return false;
			}else{
				return true;
			}
		}
	}
}

/*
* Verifie si nombre
*/
function isGTOk(champGT){
var pGT = champGT.value;
	if(pGT == ""){
		return true;
	}else{
		if(isNaN(Number(pGT))||parseInt(pGT)<0 ){
			alert("Error on field 'Gross tonnage' : it should be a positive number.");
			champGT.focus();
			return false;
		}else{
			return true;
		}
	}
}

/*
*   Effectue la verif si les 2 populés = TRUE si correct
*/
function isGTCompare(pMinGT,pMaxGT){
var minGT = pMinGT.value;
var maxGT = pMaxGT.value;
	if (minGT==""||maxGT==""){
		return true;
	}else{
		if (Number(minGT)<=Number(maxGT)){
			return true;
		}else{
			alert("Error on field 'Gross Tonnage' : the beginning Gross Tonnage should be lower than the ending Gross Tonnage..");
			pMinGT.focus();
			return false;
		}
	}
}

/*
* Verifie expression YB --> 1000 à 2999
*/
function isYBOk(pYB){
var YB = pYB.value;
var OK = /[1-2][0-9]{3}/.exec(YB);
	if (OK || YB == ""){
		return true;
	}else{
		alert("Error on field 'Keel date' : it should be a number between 1000 and 2999.");
		pYB.focus();
		return false;
	}
}

/*
*   Effectue la verif si les 2 populés = TRUE si correct
*/
function isYBCompare(pMinYB,pMaxYB){
var minYB = pMinYB.value;
var maxYB = pMaxYB.value;
	if (minYB=="" || maxYB==""){
		return true;
	}else{
		if (Number(minYB)<=Number(maxYB)){
			return true;
		}else{
			alert("Error on field 'Keel date' : the beginning 'kell date' should be lower than the ending 'keel date'.");
			pMinYB.focus();
			return false;
		}
	}
}

function isPortInspOK(){
if (document.FRM.port.options.selectedIndex!= -1){
	return true;
	}else{
		alert("Please, complete the field 'Port of inspection' ");
		document.FRM.port.focus();
		return false;
	}		
}
function popPortList(pIdState){

	j=1;
	if (pIdState==''&& document.FRM.port.options.selectedIndex== -1){
		document.FRM.port.options[0]= new Option('All','')
	}
	for (var i=0; i<idState.length;i++){
		if (pIdState==idState[i]){
			if (j==1 && document.FRM.port.length>1 && document.FRM.port.options[j].text==libPort[i]){
				break;
			}
			document.FRM.port.options[j]= new Option(libPort[i],idPort[i]);
			j++;
		}
	}
	if (j>1 || pIdState==''){
		k=j;
		while (document.FRM.port.options[k]!=null){
			document.FRM.port.options[k]=null;
		}
	}
}




