function JJMMAAAA(strdate, b_alert){
	var jour, mois, annee;
	var chaine;
	var i, len;
	len	= strdate.length;
	if (len==0) { 
		return ""; 
	}
	i = 0;
	while (((strdate.charAt(i)<'0')||(strdate.charAt(i)>'9'))&&(++i<len)) {}
	if (i==len) { 
		if ((b_alert) && (strdate != ''))
			alert('Format de date incorrect.');
		return ""; 
	}
	chaine = "";
	jour	= 0;
	while ((strdate.charAt(i)>='0')&&(strdate.charAt(i)<='9'))
	{
		chaine += strdate.charAt(i);
		jour = jour*10 + eval(strdate.charAt(i),10);
		i++;
		if (i>=len) break;
	}
					
	if ((jour>31)||(jour==0))
	{
		if ((chaine.length==6)||(chaine.length==8))
		{
			jour  = eval(chaine.charAt(0),10)*10+eval(chaine.charAt(1),10);
			mois  = eval(chaine.charAt(2),10)*10+eval(chaine.charAt(3),10);
			annee = eval(chaine.charAt(4),10)*10+eval(chaine.charAt(5),10);
			if (chaine.length==8)
			{
				annee = annee*100 + eval(chaine.charAt(6),10)*10+eval(chaine.charAt(7),10);
			}
			if ((jour>31)||(jour==0)||(mois>12)||(mois==0)) { 
				if ((b_alert) && (strdate != ''))
					alert('Format de date incorrect.');
				return ""; 
			}	
		}
		else { 
			if ((b_alert) && (strdate != ''))
				alert('Format de date incorrect.');
			return ""; 
		}
	}
	else
	{
		while (((strdate.charAt(i)<'1')||(strdate.charAt(i)>'9'))&&(++i<len)) {}
		if (i==len) { 
			if ((b_alert) && (strdate != ''))
				alert('Format de date incorrect.');
			return ""; 
		}
		mois = 0;
		while ((strdate.charAt(i)>='0')&&(strdate.charAt(i)<='9'))
		{
			mois = mois*10 + eval(strdate.charAt(i),10);
			i++;
			if (i>=len) break;
		}
		if ((mois>12)||(mois==0)) { 
			if ((b_alert) && (strdate != ''))
				alert('Format de date incorrect.');
			return "";  
		}
		while (((strdate.charAt(i)<'0')||(strdate.charAt(i)>'9'))&&(++i<len)) {}
		if (i==len) { 
			if ((b_alert) && (strdate != ''))
				alert('Format de date incorrect.');
			return ""; 
		}
		annee	= 0;
		while ((strdate.charAt(i)>='0')&&(strdate.charAt(i)<='9'))
		{
			annee = annee*10 + eval(strdate.charAt(i),10);
			i++;
			if (i>=len) break;
		}
	}
	if (annee<15)
	{
		annee += 2000;
	}
	if (annee<1900)
	{
		annee = 1900;
	}		
	if ((annee>=100) && (annee.toString().length == 3))
	{
		alert('Format de date incorrect.');
		return "";
	}			
	var jourbis, moisbis;
	jourbis = "" + eval(jour);
	moisbis = "" + eval(mois);
	if ( jourbis.length<2 )
	{
		jourbis = "0" + eval(jour);
	}
	if ( moisbis.length<2 )
	{
		moisbis = "0" + eval(mois);
	}
	return	"" + jourbis + "/" + moisbis + "/" + annee;
}

function checkEmail(myEmail)
{
	tmp_email = deleteEspace(myEmail);
	len = tmp_email.length ;
	tmp_email = myEmail;
	i=0;
	while (i<len)
	{
		myChar = tmp_email.charAt(i);
		if (  (('A' <= myChar) && (myChar <= 'Z')) || 
			(('a' <= myChar) && (myChar <= 'z')) || 
			(('0' <= myChar) && (myChar <= '9')) || 
			(myChar == '@') || (myChar == '.') || 
			(myChar == '_') || (myChar == '-')  )
		{}
		else
		{
			return 1;
		}
		i = i + 1;
	}
	// 2) Le caractere '@'
	// 2.1) au moin un caractere @ et pas en 1er position
	i = tmp_email.indexOf("@");
	if (i == -1) 
	{
		return 2;
	}
	if (i == 0) 
	{
		return 3;
	}
	// 2.2) Un seul et unique caratere @
	tmp_domaine = tmp_email.substr(i+1);
	j = tmp_domaine.indexOf("@");
	if (j != -1)
	{
		return 4;
	}
	// 2.3) Sequences interdites autour du @
	substr1 = tmp_email.substr(i-1,2);
	substr2 = tmp_email.substr(i,2);
	if ( (substr1 == '.@') || (substr2 == '@.') ||
		(substr1 == '-@') || (substr2 == '@-') || 
		(substr1 == '_@') || (substr2 == '@_') )
	{
		return 5;
	}
	// 3) l'extension 
	if ((tmp_email.substr(len-3,1) != '.') && (tmp_email.substr(len-4,1) != '.'))  
	{
		return 6;
	}
	if ((tmp_email.lastIndexOf(".") != len-3) && (tmp_email.lastIndexOf(".") != len-4))  	
	{
		return 7;
	}
	i=tmp_email.lastIndexOf(".");
	while (i<len)
	{
		myChar = tmp_email.charAt(i);
		if (  (('A' <= myChar) && (myChar <= 'Z')) || 
			(('a' <= myChar) && (myChar <= 'z')) || 
			(myChar == '.'))
		{
			// Tout est OK
		}
		else
		{
			return 7;
		}
		i = i + 1;
	}
	return 0 ; 
}

function  deleteEspace(chaine)
{
	retour = "";
	lenChaine = chaine.length ;
	i=0;
	while (i < lenChaine)
	{
		subSTR_i = chaine.substr(i,1);
		codeStr_i = chaine.charCodeAt(i);
		if ( subSTR_i != " " )
		{
			retour = retour.concat(subSTR_i);
		}
		i++;
	}
	return retour;
}

function checkTelNumber(num)
{
	var val; 
	val = deleteEspace(num);
	if (val == ""){	return false; }
	else
	{
		// Valeur numérique ?
		if ( !(checkInteger(val)) )
		{
			return false;
		}

		if ((val.length < 10) || (val.length > 14))
		{
			return false;
		}
	}
	return true;
}

function checkInteger(chaine) { 
	var strTemp = trim(chaine,' ');
	if (strTemp == '' ){
		return false;
	}
	var chain = new String(strTemp);
	erreur=0;
	for (var i = 0; i < chain.length ; i++) 
	{ 
		var chainez = chain.substring (i, i + 1); 
		if (chainez < "0" || "9" < chainez) 
			{
			erreur=2 ; 
			break;
			} 
	} 
	if (erreur == 2 ) 
	{    
		erreur=0;
		return false;
	}
	else {return true;}
}
function checkRangedInteger(chaine, iMin, iMax)
{
	if (checkInteger(chaine) )
	{
		var i = parseInt(chaine, 10);
		if (i < iMin || i > iMax)
		{return false;}
		else {return true;}
	}
	return false;
}

function trimStart(s,c)
{
	var iLen = s.length;
	var sOut = "";
	var chr = "";
	for (var i=0; i<iLen; i++)
	{
		chr = s.charAt(i); 
		if ((c == "0") && (iLen == 1))
			sOut = c;
		else { 
			if ((chr==c) && (sOut.length==0)){
			}
			else
			{
				sOut = sOut + chr; 
			}
		}
	}
	return sOut;
}

function trimEnd(s,c)
{
	var iLen = s.length;
	var sOut = "";
	var chr = "";
	for (var i=iLen-1; i>=0; i--)
	{
		chr = s.charAt(i); 
		if ((chr==c) && (sOut.length==0)){
		}
		else
		{
			sOut = chr + sOut; 
		}
	}
	return sOut;
}

function trim(s,c){
	return trimEnd(trimStart(s,c),c);
}
var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+)|(\d+([,]\d*)?)|((\d*[,])?\d+))$/
//add the following filter, if we want to add the "."
// (\d+(\.\d*)?)|((\d*\.)?\d+)

var defaultEmptyOK = false;

//an une of Javascript's object RegEx : string.match(expression)
// isFloat (STRING s [, BOOLEAN emptyOK])
function isFloat (s)
{   sTemp = trim(s,' ');
	if (sTemp == '') 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else 
		return (isFloat.arguments[1] == true);

    return sTemp.match(reFloat);
}
//another use of Javascript's object RegEx : expression.test()
function checkDate(strDate)
{
	if ( trim(strDate, ' ') == '' ) return false;
	expression = "(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])"
	if ( expression.test(strDate) )
		return true;
	else return false;
}	

function checkZipCode(val)
{
	// Test si la valeur est numérique
	var msgCP;
	if (val == ""){	return false; }
	else
	{
		if ( !(checkInteger(val)) )
		{
			return false;
		}
	
		// Test si la valeur est un code postal
		if ( val.length != 5 ) 
		{
			//alert('Code postal incorrect : un code postal est composé de 5 chiffres');
			return false;
		}

		msgCP = "Etes-vous certain d\'avoir saisi un code postal du territoire français ?";
			
		// Test si le CP est compris entre 2 valeurs
		if ( (val < 1000) || (val > 99000) || ((val >= 96000) && (val <= 96999)))
		{
			alert(msgCP);
			return false;
		}
	}
	return true;
}// Fin de checkZipCode

function checkControlFloat(strFormID, strControlID, strErrorMessage)
{
	if ( ! isFloat(document.forms[strFormID].elements[strControlID].value) )
	{
		alert(strErrorMessage);
		document.forms[strFormID].elements[strControlID].focus();
		return false;
	}
	
	return true;
}

//The ControlID must be UniqueID in the RadioButtonList
function checkRadioButton(strFormID, strControlID, strErrorMessage)
{
	var b_checked = false;
	for (var i=0; i< document.forms[strFormID].elements[strControlID].length; i++)  {
		if (document.forms[strFormID].elements[strControlID][i].checked)  {
			b_checked = true;
		}
	}

	//focus to the first group element
	if (b_checked == false){
		alert(strErrorMessage);
		document.forms[strFormID].elements[strControlID][0].focus();
		return false;
	}
	return true;
}

function checkControlZipCode(strFormID, strControlID, strErrorMessage)
{
	if ( ! checkZipCode(document.forms[strFormID].elements[strControlID].value) )
	{
		alert(strErrorMessage);
		document.forms[strFormID].elements[strControlID].focus();
		return false;
	}
	
	return true;
}
function checkControlDate(strFormID, strControlID, strErrorMessage)
{
	var strValue = document.forms[strFormID].elements[strControlID].value;
	if ( trim(value ,' ') != '' )
	{

		var newvalue = JJMMAAAA(document.forms[strFormID].elements[strControlID].value,true);
		if (newvalue != '')
		{document.forms[strFormID].elements[strControlID].value = newvalue;}
		else {
			document.forms[strFormID].elements[strControlID].value = '';
			document.forms[strFormID].elements[strControlID].focus();
			return false;
		}
	}
	
	return true;
}

function checkControlEmail(strFormID, strControlID, strErrorMessage)
{
	if ( checkEmail( trim(document.forms[strFormID].elements[strControlID].value,' ') ) != 0){
		alert(strErrorMessage);
		document.forms[strFormID].elements[strControlID].focus();
		return false;
	}
	
	return true;
}

function checkControlTelNumber(strFormID, strControlID, strErrorMessage)
{
	if (checkTelNumber(trim(document.forms[strFormID].elements[strControlID].value,' ')) == false){
		alert(strErrorMessage);
		document.forms[strFormID].elements[strControlID].focus();
		return false;
	}
	
	
	return true;
}



function checkControlInteger(strFormID, strControlID, strErrorMessage)
{
    //if (document.forms[strFormID].elements[strControlID]  == null)
     //   alert("inexistant " + strControlID);
    
	if ( ! checkInteger(document.forms[strFormID].elements[strControlID].value) ){
		alert(strErrorMessage);
		document.forms[strFormID].elements[strControlID].focus();
		return false;
	}
	
	
	return true;
}
function checkControlRangedInteger(strFormID, strControlID, strErrorMessage, iMin, iMax)
{
	if ( ! checkRangedInteger(document.forms[strFormID].elements[strControlID].value, iMin, iMax) ){
		alert(strErrorMessage);
		document.forms[strFormID].elements[strControlID].focus();
		return false;
	}
	
	
	return true;
}
function checkQuickQuoteRangedInteger(strFormID, strControlID, strSelectID, strErrorMessage)
{
   var ctrlSelect = document.forms[strFormID].elements[strSelectID];
   // get credit type from the dropdownlist

   return checkQuickQuoteRangedInteger2(strFormID, strControlID, ctrlSelect.value, strErrorMessage);
}

function checkQuickQuoteRangedIntegerRestit(strFormID, strControlID, strCreditType, strErrorMessage)
{
    return checkQuickQuoteRangedInteger2(strFormID, strControlID, strCreditType, strErrorMessage);
}

function checkQuickQuoteRangedInteger2(strFormID, strControlID, strCreditType, strErrorMessage)
{
	var iMin;
	var iMax;
	
	switch (strCreditType)
	{
	    // auto : de 1600 à 150000 (neuf ou occaz)
	    // moto: de 1600 à 100000 (neuf ou occaz)

		case '1': // auto ou moto neuf (en fait on fait un max des limites de l'auto et moto)
		case '2': // auto ou moto occasion (en fait on fait un max des limites de l'auto et moto)
			iMin = 1600;
			iMax = 150000;
			break;
		case '3': // personal
			iMin = 1600;
			iMax = 40000; 
			break;
		case '4': // travaux
			iMin = 1600;
			iMax = 21500; 
			break;
		case '5': // renouvelable
			iMin = 500;
			iMax = 5000;
			break;
	}
	 
	var ctrlTextBox = document.forms[strFormID].elements[strControlID];
	if (ctrlTextBox != null){
		if ( ! checkRangedInteger(ctrlTextBox.value, iMin, iMax) ){
			alert('Pour ce type de projet, le montant est compris entre ' + iMin + ' et ' + iMax + ' euros');
			ctrlTextBox.focus();
			return false;
		}
	}
	return true;
}



function checkControlEmpty(strFormID, strControlID, strErrorMessage)
{
	if ( trim(document.forms[strFormID].elements[strControlID].value , ' ') == '' ){
		alert(strErrorMessage);
		document.forms[strFormID].elements[strControlID].focus();
		return false;
	}
	return true;
}
function checkEmptyOrInteger(strFormID, strControlID, strErrorMessage)
{
	//
	if ( trim(document.forms[strFormID].elements[strControlID].value , ' ') != '' ){
		return checkControlInteger(strFormID, strControlID, strErrorMessage);
	}
	//else OK
	
	return true;
}

function checkControlDropDownList(strFormID, strControlID, strErrorMessage)
{
	var ctrl = document.forms[strFormID].elements[strControlID];
	if (ctrl.disabled == false)
	{
		if (trim(ctrl.value,' ') == ''){
			alert(strErrorMessage);
			ctrl.focus();
			return false;
		}
	}
	return true;
}

//Teste la validité d'une date
function CheckDate(strJJ, strMM, strAA) {

			
		if ( strMM.charAt(0) == "0"){strMM = strMM.charAt(1)};
		if ( strJJ.charAt(0) == "0" ){strJJ = strJJ.charAt(1)};

		var j = parseInt(strJJ);
		var m =	parseInt(strMM);
		var y =	parseInt(strAA);
		
		if ( isNaN(j) || isNaN(m) || isNaN(y) ) {
	
			return false;
		}
		if ( (j<1) || (j>31) ) {
	
			return false;
		}
		if ( (m<1) || (m>12) ) {
	
			return false;
		}
		if  ( ((m == 4) || (m == 6) || (m == 9) || (m == 11)) && (j == 31) ) {
	
			return false;
		}
		if ( (m == 2) && (j == 29) && !( (y%400 == 0) || ( (y%4 == 0) && y%100 ) ) ) {
	
			return false;
		}
		if ( (m == 2) && (j > 29) ) {
	
			
			return false;
		}

		return true;
	}

