// this file contains all functions that i use in my projects.


function required(field,label)
{
	if(field.value==""){
		alert('Le champ '+label+' est obligatoire.');
		field.focus();
		return false;
	}
	return true;
}
//////////////////////////////////
function isChar(field,label)
{
   var pattern = /\W/;
	   if(pattern.test(field.value))
	   {
			alert('Le champ '+label+' ne doit contenir que des caractères alphanumérique.');
			field.select();
			return false;
	   }
   return true;
} 
//////////////////////////////////////////
function isNumber(field,label)
{
	if(isNaN(field.value))
	{
		alert('Le champ '+label+' ne doit contenir que des nombres.');
		field.select();
		return false;
	}
	return true;
}
/*
there functions are used manipulate forms with Ajax. 
*/
function XHR(){
                var xhr = null; 
				if(window.XMLHttpRequest)// for firefox and other
				   xhr = new XMLHttpRequest(); 
				else if(window.ActiveXObject){ // for IE
				   try {
			                xhr = new ActiveXObject("Msxml2.XMLHTTP");
			            } catch (e) {
			                xhr = new ActiveXObject("Microsoft.XMLHTTP");
			            }
				}
				else { 
				   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
				   xhr = false; 
				} 
               return xhr
			}
/////////////////////////////////////////////
function getResponse(insertedId,xhr)
{
	if(xhr.readyState == 4 && xhr.status == 200)
	{
		document.getElementById(insertedId).innerHTML = xhr.responseText;
	}	
}
/* function to link two select using Ajax*/
function doAjaxSelect(insertedId,sourceId,urlSource){
				var xhr = XHR();
				xhr.onreadystatechange = function(){
					
					if(xhr.readyState == 4 && xhr.status == 200)
	                {
		               document.getElementById(insertedId).innerHTML = xhr.responseText;
	                 }	
				}

				xhr.open("POST",urlSource,true);
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				sel = document.getElementById(sourceId);
				id= sel.options[sel.selectedIndex].value;
				if(id!=''){
				  xhr.send("id="+id);
				}
}
/*----------------------------------------------
* function to select a group of chackbox.
*  
*
*
-----------------------------------------------*/
function checkedElts(nameElts,idParent)
 {
    var elts = document.getElementsByName(nameElts); // the set of checkbox, have the same name.
	var parent=document.getElementById(idParent);    // the checkbox used to select the set on click it.
	var count;                                       // Number of checkboxs.
	if(typeof(elts.length)=='undefined')
	{
	  count=0;
	}else{
	  count=elts.length;
	}
	for(i=0;i<count;i++)
	{
		if(parent.checked==false)
		{
			elts[i].checked=false; //incheck checkbox if  the parent is inchecked.
		}else{
		   elts[i].checked=true; //check checkbox if  the parent is checked.
		} //end if
	} // end for
 }// end function
 /*-------------------------------------------------
 * function to test if elements are checked.
 *
 *
 *
 *
 --------------------------------------------------*/
 function isChecks(nameElts)
 {
 	var elts = document.getElementsByName(nameElts); // the set of checkbox, have the same name.
	var count;                                       // Number of checkboxs.
	if(typeof(elts.length)=='undefined')
	{
	  count=0;
	}else{
	  count=elts.length;
	}
	for(i=0;i<count;i++)
	{
			if(elts[i].checked==true)
			{
				return true;
			} // end if.
	} // end for
	return false;	
 }
 /*-----------------------------------------------
 * verify if elements are checked and submit the
 * form
 *
 *
 -------------------------------------------------*/
 function deleteAll(form,nameElts)
 {
 if(isChecks(nameElts))
 {
	   if( confirm('Voulez vous vraiment supprimer toute la sélectionne?')){
		   form.submit();
	   }
	 }else{
		alert('Aucun élèment n\'a pas été sélectionné');
	 }// end if
 }//end function
 /*-----------------------------------------------
 * JK Popup Window Script (version 3.0)- By JavaScript Kit (http://www.javascriptkit.com)
 * Visit JavaScriptKit.com for free JavaScripts
 * This notice must stay intact for legal use
 ------------------------------------------------*/

function openpopup(popurl){
var winpops=window.open(popurl,"","width=660,height=430,toolbar=0,location=0,status=1,scrollbars=1,menubar=0,resizable=1");
}
/*
* function to validate email 
*
*
*/
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
        if(str==""){
          return true;
        }
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Enter un E-mail Valide comme suit: email@exemple.com.")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Enter un E-mail Valide comme suit: email@exemple.com.")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Enter un E-mail Valide comme suit: email@exemple.com.")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Enter un E-mail Valide comme suit: email@exemple.com.")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Enter un E-mail Valide comme suit: email@exemple.com.")
		    return false
		 }
 		 return true					
	}