// JavaScript Document
// determine if the radio button was selected

function checkradiobutton(radio_name)
{
	var message ="";
	myradio = document.getElementsByName(radio_name);
	rad_val =0;
	for (var i=0; i < myradio.length; i++)
   		{
   			if (myradio[i].checked)
      			{
      				 rad_val = 1;
      			}
   		}//for
		
		if(rad_val == 1)
		{
		
		
		}
		else {
			
			message= "Au moins un type de civilité dois etre selecté.\n";
			myradio.className= "texterror";
			
			
		}
	return message;
}


function validateEmpty(id,nonduchamp) {
   fld= document.getElementById(id);
   
   var error = "";
 
    if (fld.value.length == 0) {
       
        error = "-Le champ "+nonduchamp+" est vide.\n"
		document.getElementById(id).className= "texterror";
    } else {
       document.getElementById(id).className= "textnor";
    }
	
    return error;  
	
}

function validatePhone(id,nonduchamp) {
	fld= document.getElementById(id);
	
	
	
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, ''); 
	
	
   if (fld.value == "") {
        error = "-Vous devez mettre votre numero de "+nonduchamp+ ".\n";
		document.getElementById(id).className= "texterror";
		
		
        
    }
	
	else if (isNaN(parseInt(stripped)) && fld.value !="") {
        error = "-Le champ "+nonduchamp+" contient des characteurs illegaux.\n";
		document.getElementById(id).className= "texterror";
       
	   } 
        
    
    
	
	else if (!(stripped.length == 10)&&fld.value !="") {
        error = "-Le numero de telephone dois comporté 10 chiffres.\n";
        document.getElementById(id).className= "texterror";
    }
	
	else 
	{document.getElementById(id).className= "textnor";
	
	}
    return error;
}

function validateEmail(id) {
	fld = document.getElementById(id);
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    //var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var emailFilter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
  
    if (fld.value == "") {
         document.getElementById(id).className= "texterror";
        error = "-Vous n'avez pas mis votre adresse mail.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
         document.getElementById(id).className= "texterror";
        error = "Veillez mettre un adresse mail valide.\n";
    } else if (fld.value.match(illegalChars)) {
         document.getElementById(id).className= "texterror";
        error = "-Votre adresse mail contient des characteurs illegaux.\n";
    } else {
       document.getElementById(id).className= "textnor";
    }
    return error;
}

function speciale_validateEmail(id) {
	
	fld = document.getElementById(id);
	
    var error="";
    var tfld = trim(fld.value);   
	if(fld.value!="" )
	{
		// value of field with whitespace trimmed off
		var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	   
		if (fld.value == "") {
			 document.getElementById(id).className= "texterror";
			error = "-Vous n'avez pas mis l'adresse mail de votre correspontant.\n";
		} else if (!emailFilter.test(tfld)) {              //test email for illegal characters
			 document.getElementById(id).className= "texterror";
			error = "-Veillez mettre un adresse mail valide pour votre correspontant.\n";
		} else if (fld.value.match(illegalChars)) {
			 document.getElementById(id).className= "texterror";
			error = "-L'adresse mail de votre correspontant contient des characteurs illegaux.\n";
		} else {
		   document.getElementById(id).className= "textnor";
		}
		return error;
	}
	return error;
}


function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function copyme(from,to)
{
	
	source= document.getElementById(from);	
	to = document.getElementById(to);	
	
	//copy
	to.value = source.value
}




function IsNumeric(strString)//check numeric
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
	  
   return blnResult;
   }
   
   function validate_min_email()
   {
	  var error ="";
	  ami_email =document.getElementById('email2').value ;
	   ami_email1 =document.getElementById('email3').value; 
	    ami_email2 =document.getElementById('email4').value ;
		 ami_email3 =document.getElementById('email5').value ;
		  ami_email4 =document.getElementById('email6').value; 
		   ami_email5 =document.getElementById('email7').value; 
		 
		  if( ami_email == "" &&ami_email1 == ""&&ami_email2 == ""&& ami_email3 == "" &&ami_email4 == "" && ami_email5== "")
		  	
		  error ="Vous devez mettre au moins un email de votre ami.\n";
		 return error; 
   }



function validateFormOnSubmit()


{
	
	var reason = "";

 
  reason += validateEmpty('civilite','Civilité');
  reason += validatePhone('tel','Téléphone');
  reason += validateEmpty('Nom','Nom');
 
  reason += validateEmpty('Prenom','Prénom');
  reason += validateEmail('E-mail');
 
  

 

      
  if (reason != "") {
    alert("Vous devez faire les corrections suivantes:\n" + reason);
    return false;
  }

  return true;
	
}

function validateFormOnSubmit2()


{
	
	var reason = "";

 
  reason += validateEmpty('CIVILITE','Civilité');
  reason += validatePhone('Telephone','Téléphone');
  reason += validateEmpty('Nom','Nom');
  reason +=validateEmpty('Prenom','Prénom')
 
  reason += validateEmail('E-mail');
  reason +=validateEmptyRelated('departement2','Departement','dept');
 
  reason +=validaterayon('rayon','Rayon','rai');
 //reason +=validateEmpty('facture','methode de facture');

 
 
 
  

 

      
  if (reason != "") {
    alert("Vous devez faire les corrections suivantes:\n" + reason);
    return false;
  }

  return true;
	
}

function selectcheckbox(theElement,obj,theid) {
	



	
	if(obj.checked==true)
	{
		// change la classe 
		id = 'rub'+theid+'_list';
		//if(len >0)
		document.getElementById(id).className = "show";
		list = document.getElementsByName(theElement);
		for(i = 0; i<list.length; i++)
		{
			list[i].checked = true;
		}
	}//if
	else {
		// change la classe 
		id = 'rub'+theid+'_list';
		//if(len >0)
		document.getElementById(id).className = "hide";
		list = document.getElementsByName(theElement);
			for(i = 0; i<list.length; i++)
		{
			list[i].checked = false;
		}
	}


// now we save the result of the checkvalues in variable 

  list = document.getElementsByName(theElement);
 var myvalue ="";
			for(i = 0; i<list.length; i++)
		{	if(list[i].checked ==true)
				{	if(i ==list.length-1)
				myvalue +=list[i].value;
				else 
				myvalue +=list[i].value+",";
				}//if
		}
		//assign myvalue to a hidden input
		 inputname= 'rub'+theElement+'_activities';
		
		document.getElementById(inputname).value = myvalue;
		
 }
 
 
function recompute(id, t) {
    // we need to update the value 
    thename = t.name;
    thevalue = t.value;
    
    hiddeninput =  document.getElementById(thename);
    hiddeninput = 'rub'+thename+'_activities';
    
    myvalue = document.getElementById(hiddeninput).value;
    // we search for id in  the string "thehiddenfieldvalue"
    //split the string
    if (myvalue != "")
	oldarray = myvalue.split(",");
    else
	oldarray = new Array();
    flag = 0;
    for (i = 0; i < oldarray.length; ++i) {
	if (oldarray[i] == thevalue)
	    flag = 1;
    }
    if (flag == 0) {
	// does not exist
	oldarray.push(thevalue); 
	// we convert the array to string
	if (oldarray[0] == ",")
	    oldarray.splice(0, 1);
	if (oldarray[oldarray.length] == ",")
	    oldarray.splice(oldarray.length,1);
	
	newvalue = oldarray.toString();
	document.getElementById(hiddeninput).value= newvalue ;
    } else {
	// exists
	// we remove the string
	//get the index of   the value
	myvalue = document.getElementById(hiddeninput).value;
	// we search for id in  the string "thehiddenfieldvalue"
	//split the string
	if (myvalue != "")
	    oldarray = myvalue.split(",");
	else
	    oldarray = new Array();
	for (i = 0; i < oldarray.length; ++i) {
	    if (oldarray[i] == thevalue) {
		index = i;
	    }
	}
	//alert(index);
	oldarray.splice(index,1);
	if (oldarray[0] == ",")
	    oldarray.splice(0, 1);
	if (oldarray[oldarray.length] ==",")
	    oldarray.splice(oldarray.length, 1);
	newvalue = oldarray.toString();
	document.getElementById(hiddeninput).value = newvalue;
    }
}

function checkdiv(theid)

{
id = 'rub'+theid+'_list';	


if(document.getElementById(id).className =="hide")
document.getElementById(id).className ="show"
else 
document.getElementById(id).className ="hide"
}



   
   
   
  function validaterayon(id,name,id2)
  
  {
	var msg = "";
	if(document.getElementById(id2).checked )
	
	{
	
		//check if numeric
		if(IsNumeric(document.getElementById(id).value))
		document.getElementById(id).className= "textnor";
		else  
		{msg+="-Le champ Rayon ne contient pas un chiffre.\n";
		document.getElementById(id).className= "texterror";
		}
		//check if less than 100
		 if(document.getElementById(id).value >100 && IsNumeric(document.getElementById(id).value) )
		{
		msg+="-Le Rayon d'intervention depasse 100 KM.\n";
		document.getElementById(id).className= "texterror";
		}
		else 
		document.getElementById(id).className= "textnor";
		msg +=validateEmptyRelated('rayon','Rayon','rai');
        msg +=validateEmptyRelated('adresse2','Adresse','rai');
		msg+=requireLength('cp2','Code_postal','cp2',5);
				
		
		
	
	}
	else 
	
	{
		document.getElementById('rayon').className= "textnor";
		document.getElementById('adresse2').className= "textnor";
		document.getElementById('cp2').className= "textnor";
	}
		
	
	return msg;
	
  }
  
  
  function requireLength(id,nonduchamp,id2,min) //for codepostal
{ 
	fld = document.getElementById(id2).value;
	var message ="";
var len= fld.length;
// set minimum and/or maximum field lengths
  
  
  if (len >min || len <min)
  {
	  message ="-Le nombre de chiffres dans le  champ "+nonduchamp+" est incorrect.\n";
	  document.getElementById(id).className= "texterror";
  }
  else if (!fixInt(id2))
 	{
		message ="-La valeur dans le  champ "+nonduchamp+" est incorrect.\n";
	    document.getElementById(id).className= "texterror";
		
	}
  else
  	{
	 
  	document.getElementById(id).className= "textnor";
  	}
  	return message;
 	}
 
 
 function validateEmptyRelated(id,nonduchamp,id2)
 
 {
	 
	  var error = "";
		if(document.getElementById(id2).checked) 
			{
					fld= document.getElementById(id);
	  
	 
				 if (fld.value.length == 0)
				 
				 {
		   
				error = "-Le champ "+nonduchamp+" est vide.\n"
				document.getElementById(id).className= "texterror";
				} 		
				else
				{
				document.getElementById(id).className= "textnor";
				}
	     }
		 else 
		 document.getElementById(id).className= "textnor";
    return error;  
	
}
 function fixInt(id,sep)
{ // integer check/complainer 
	fld = document.getElementById(id).value;
	len = fld.length;
  if(!len) return true; // blank fields are the domain of requireValue 
  var val= fld;
  if(typeof(sep)!='undefined') val= val.replace(new RegExp(sep,'g'),'');
  val= parseInt(val);
  if(isNaN(val))
  { // parse error 
   
    return false;
  }
 else
  return true;
}
	 
 
