/*************************************************************
Ensemble de fonctions pour effectuer les checks de validation 
pour divers champs de formulaire

Copyright by Buddycat (buddycat.atrex.ch)
Dernières modifications 04.06.2004

<form action="" method="post" name="tete" onSubmit="return frmCheck(this);">

La valeur du champ emailFields indique quels champs doivent etre valide comme email
exemple:
<input name="monChampEmail_1" type="text" value="mon email 1">
<input name="monChampEmail_2" type="text" value="mon email 2">
<input name="emailFields" type="hidden" value="monChampEmail_1, monChampEmail_2">

La valeur du champ requiredFields indique quels champs doivent etre valide comme necessaire
exemple:
<input name="monChampNecessaire_1" type="text" value="mon champ necessaire 1">
<input name="monChampNecessaire_2" type="text" value="mon champ necessaire 2">
<input name="requiredFields" type="hidden" value="monChampNecessaire_1, monChampNecessaire_2">

La valeur du champ phoneFields indique quels champs doivent etre valide comme telephone
exemple:
<input name="monChampTelephone_1" type="text" value="mon telephone 1">
<input name="monChampTelephone_2" type="text" value="mon telephone 2">
<input name="phoneFields" type="hidden" value="monChampTelephone_1, monChampTelephone_2">

La valeur du champ dateFields indique quels champs doivent etre valide comme date
exemple:
<input name="monChampDate_1" type="text" value="ma date 1">
<input name="monChampDate_2" type="text" value="ma date 2">
<input name="dateFields" type="hidden" value="monChampDate_1, monChampDate_2">

Voir exemple a la fin de la page
*************************************************************/

//Couleur de fond des champs checkes
var fieldBgColor = 'red';

//Que faire avec les champes invalides
function showInvalide(invalideElement)
{
invalideElement.style.backgroundColor = fieldBgColor;
invalideElement.focus();
}

//Check les champs d'un formulaire grace aux inputs de check
function frmCheck(frm)
{
	$inputRequiredFields = false;
	$inputEmailFields = false;
	$inputPhoneFields = false;
	$inputDateFields = false;
	
	for (var k=0; k < frm.length; k++)
	{
		//Remet à jour la couleur de fond de tout les champs
		frm.elements[k].style.backgroundColor = '';
		
		//Verifie l'existance des champs de ...Fields
		$name = frm.elements[k].name;
		switch ($name)
		{
			case 'requiredFields':
				$inputRequiredFields = true;
				break;
			case 'emailFields':
				$inputEmailFields = true;
				break;
			case 'phoneFields':
				$inputPhoneFields = true;
				break;
			case 'dateFields':
				$inputDateFields = true;
				break;
		}		
	}
	
	//parcours tout les champs du formulaire passe en parametre
	for (var j=0; j < frm.length; j++)
	{	
		//Si le champ requiredField existe
		//et si le champ en cours doit etre checke
		if ($inputRequiredFields == true && 
			frm.requiredFields.value.indexOf(frm.elements[j].name,0) >= 0)
		{	
			if (!checkRequired(frm.elements[j]))
				return false;
		}
			
		//Si le champ emailField existe
		//et si le champ en cours doit etre checke
		if ($inputEmailFields == true && 
			frm.emailFields.value.indexOf(frm.elements[j].name,0) >= 0)
		{
			if (frm.elements[j].value.length != 0)
				if (!checkEmail(frm.elements[j]))
					return false;
		}
	
		//Si le champ phoneFields existe
		//et si le champ en cours doit etre checke
		if ($inputPhoneFields == true && 
			frm.phoneFields.value.indexOf(frm.elements[j].name,0) >= 0)
		{
			if (frm.elements[j].value.length != 0)
				if (!checkNatel(frm.elements[j]))
					return false;
		}
		
		//Si le champ dateFields existe
		//et si le champ en cours doit etre checke
		if ($inputDateFields == true && 
			frm.dateFields.value.indexOf(frm.elements[j].name,0) >= 0)
		{
			if (frm.elements[j].value.length != 0)
				if (!checkDate(frm.elements[j]))
					return false;
		}	
	}
	return true;
}

//Check les champs requis
function checkRequired(element)
{
	if (element.value.replace(/(^\s*)|(\s*$)/g,'') == '')
	{
		element.value = '';
		showInvalide(element);
		errorRequired();
		return false;
	}
return true;
}


//Check les emails
function checkEmail(element)
{
	
	var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
	if (reg.exec(element.value) == null)
	{
		showInvalide(element);
		errorEmail();
		return false;
	}
	return true;
}

//Check les numero de natel
function checkNatel(element)
{
	var reg = /^[0]{2}[0-9]{8,}$/;
	if (reg.exec(element.value) == null)
	{ 
		showInvalide(element);
		errorPhone();
		return false;
	}
	//Si numero en suisse
	if (element.value.substring(0, 4) == "0041")
	{ 
		//Verifier longueur
		if (element.value.length != 13)
		{
			showInvalide(element);
			errorPhoneLength();
			return false;
		}
		//Verifier indicatif natel
		if (element.value.substring(4, 5) != "7")
		{
			showInvalide(element);
			errorNatel();
			return false;
		}
	}
return true;
}


//Check les date
var dtCh= ".";
var minYear=1900;
var maxYear=2100;

function checkDate(element)
{
	var dtStr = element.value;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) 
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1)
	{
		showInvalide(element);
		errorDateFormat();
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12)
	{
		showInvalide(element);
		errorDateMonth();
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || 
		(month==2 && day>daysInFebruary(year)) || 
		day > daysInMonth[month])
	{
		showInvalide(element);
		errorDateDay();
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{
		showInvalide(element);
		errorDateYear();
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		showInvalide(element);
		errorDateFormat();
		return false;
	}
return true
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

