// Fonction permettant de controler la clé du CIP13
// Argument code CIP sur 13 caractères
// Retourne la clé calculé 

function calculCleCIP13(cip13){  
      var cle = 0;
      var cleRangPair = 0;
      var cleRangImpair = 0;
      var calculCle = 0;
      // somme des valeurs de rang pair
      var totRangPair = parseInt(cip13.charAt(1))+parseInt(cip13.charAt(3))+parseInt(cip13.charAt(5))+parseInt(cip13.charAt(7))+parseInt(cip13.charAt(9))+parseInt(cip13.charAt(11));
      // somme des valeurs de rang impair
      var totRangImpair = parseInt(cip13.charAt(0))+parseInt(cip13.charAt(2))+parseInt(cip13.charAt(4))+parseInt(cip13.charAt(6))+parseInt(cip13.charAt(8))+parseInt(cip13.charAt(10));
      var totInter = totRangPair * 3 + totRangImpair;
      //alert(" somme : "+totInter);
      var pos = totInter.toString().length;
      if (parseInt(totInter.toString().charAt(pos-1)) > 0){
        cle = 10 - parseInt(totInter.toString().charAt(pos-1));
      }
      return cle;
}
// Fonction permettant de controler la clé du CIP7
// Argument code CIP sur 7 caractères
// Retourne la clé calculé 
function calculCleCIP7(cip7){ 
      var cle = ((parseInt(cip7.charAt(0))*2)+(parseInt(cip7.charAt(1))*3)+(parseInt(cip7.charAt(2))*4)+(parseInt(cip7.charAt(3))*5)+(parseInt(cip7.charAt(4))*6)+(parseInt(cip7.charAt(5))*7))%11;			
      return cle;
}
function formatCip13(str1) {
  var saisi = "";
  if (isNaN(str1.value)) {
  		saisi = "34009";
   } else  
  if(str1.value.substr(0,5) != "34009"){
    saisi = "34009"+str1.value;
  } else {
    saisi = str1.value;
  }
  return saisi;
}

function formatCip7(str1) {
  var saisi = "";
  
  if (isNaN(str1.value)) {
  		saisi = "";  		
  } else if(str1.value.substr(0,5) == "34009"){
    if (str1.value.length != 5) {
      saisi = str1.value.substr(5,(str1.value.length-5));
    }
  }
  return saisi;
}

function controleSaisi(str1,str2) {
  //alert (" code cip : "+str1.value+" - "+str2.value); 
	if (str1.value == "" && str2.value == ""){
		alert  ("Vous devez saisir au moins un critère");
		return false ;
	}
	if (str1.value != "" && str2.value != ""){
		alert  ("Saisir uniquement un code CIP ou une spécialité");
		return false ;
	}
	//**********************
  //Recheche sur code CIP
	//**********************
  //alert(" code CIP numerique : "+str1.value+" so "+isNaN(str1.value));
	if (str1.value != "" && str2.value == ""){
    // Test si chaine numérique
    if (isNaN(str1.value)) {
  		alert  ("Le code CIP doit être numérique");
  		return false ;
  	}
  	//c'est un cip 7
  	if(document.form1.p_table[0].checked) {
    	//pour une chaine de plus de 7 caractères controle le début
        if (str1.value.length > 7){
          alert("Code CIP incohérent");
          return false;
    
       }
        //alert(" code CIP numerique : "+str1.value+" longu : "+str1.value.length);
        // Controles CIP 7
        if (str1.value.length == 7){
          var cip7 = str1.value;
          var cle = calculCleCIP7(cip7);
      		if (cle == cip7.substr(6,1)){
      		  return true ;
          }
          else {
          	alert  ("Clé CIP7 erronée");
        		return false ;
          }
      	}
    }
  	//c'est un cip 13
  	if(document.form1.p_table[1].checked){
      	//pour une chaine de plus de 7 caractères controle le début
        if (str1.value.substr(0,5) != "34009"){
          alert("Un code CIP13 commence par \'34009\'");
          return false;
        }
      	//pour une chaine de plus de 7 caractères controle le début
          if (str1.value.length > 13){
            alert("Code CIP13 incohérent");
            return false;
         }        
        // Controles sur CIP13
         if (str1.value.length == 13){
          var cip13 = str1.value;
          var cle = calculCleCIP13(cip13);
          //alert(" clé calculé :"+cle+" clé CIP13 :"+cip13.substr(13-1,1));
          if (cip13.substr(12,1) == cle){
      			//alert  ("cle CIP 13 ok");
         		return true ;
          } else {
      			alert  ("Clé CIP13 erronée");
      			return false ;
          }
      	} 
      	
    }
	   return true;
	}
}
