//VARIABLES
var nobug = true;
var ie4   = (document.all) ? true : false;
var ns6   = (document.layers) ? true : false;
var ff2   = (document.getElementById && !document.all) ? true : false;
var defaultColor = "666666";

//VALIDATION FORM FUNCTION
function valid_contact()
{	
	nobug=true;
	check('login','text','0', 'True');
	check('password','text','0', 'True');
	check('prenom','text','0', 'True');
	check('nom','text','0', 'True');
	check('pays','text','0', 'True');
	check('province','text','0', 'True');
	check('email','email','0', 'True');
	check('code_postal','postalcode_CAN', '0', 'False');
	check('tel','phone', '0', 'False');
	
	if(document.getElementById('annee_naissance').value != "" || document.getElementById('mois_naissance').value != "" || document.getElementById('jour_naissance').value != "")
	{
		/*$Jour_naissance = document.getElementById('jour_naissance').value;
		$Mois_naissance = document.getElementById('mois_naissance').value;
		*/
		
		check('jour_naissance','birth', '0', 'False');		
	}
	
	//check('txt_comments','text','0', 'True');

	if(nobug==true){
		checkError('wrong_fields', 'wrong_fields', 'none', 'cc3333');
		return true;
	} else {
		checkError('wrong_fields', 'wrong_fields', 'block', 'cc3333');
		return false;
	}
}

//VALIDATION FORM CONCOURS

function valid_concours(color)
{	
	defaultColor =color;
	nobug=true;
	check('prenom','text','0', 'True');
	check('nom','text','0', 'True');
	//check('tel','phone', '0', 'True');
	check('email','email','0', 'True');
	check('province','text','0', 'True');
	check('ville','text','0', 'True');
	//check('code_postal','postalcode_CAN', '0', 'False');
	//check('adresse','text','0', 'True');


	if(nobug==true){
		checkError('wrong_fields', 'wrong_fields', 'none', 'cc3333');
		return true;
	} else {
		checkError('wrong_fields', 'wrong_fields', 'block', 'cc3333');
		return false;
	}
}

//VALIDATION FORM INSCRIPTION FUNCTION
function valid_contact_inscription()
{	
	nobug=true;
	check('login','text','0', 'True');
	check('email','email','0', 'True');
	
	//check('txt_comments','text','0', 'True');

	if(nobug==true){
		checkError('wrong_fields', 'wrong_fields', 'none', 'cc3333');
		return true;
	} else {
		checkError('wrong_fields', 'wrong_fields', 'block', 'cc3333');
		return false;
	}
}
//VALIDATION FORM QUESTION (CONTACT)
function valid_question()
{
	nobug=true;
	
	check('date1','date_num','0', 'False');
	check('date2','date_num','0', 'False');
	check('date3','date_num','0', 'False');
	
	if(nobug==true){
		checkError('wrong_fields', 'wrong_fields', 'none', 'cc3333');
		return true;
	} else {
		checkError('wrong_fields', 'wrong_fields', 'block', 'cc3333');
		return false;
	}
	
}

function valid_getdemo()
{	
	nobug=true;
	
	check('txt_firstname','text','0', 'True');
	check('txt_lastname','text','0', 'True');
	check('txt_company','text','0', 'True');
	check('txt_email','email','0', 'True');
	check('txt_phone','phone','0', 'True');
	check('txt_fax','phone','0', 'False');
	check('txt_address','text','0', 'True');
	check('txt_city','text','0', 'True');
	check('opt_province','province','0', 'True');
	check('txt_postalcode','postalcode_CAN','0', 'True');
	
	if(nobug==true){
		checkError('wrong_fields', 'wrong_fields', 'none', 'cc3333');
		return true;
	} else {
		checkError('wrong_fields', 'wrong_fields', 'block', 'cc3333');
		return false;
	}
}


//CHECK WITH NO SPACE
function trim(val)
{
	if (!val) return val;

	 while(val.charAt(0) == ' ') val = val.substring(1,val.length);
	 while(val.charAt(length)==' ')  val = val.substring(0,val.length-1);

	return val;
}

//CONVERT TO UPPERCASE AND SUPPRESS SPACE
function toUC(oObject){
		eval("var oCurrentObject = document.getElementById('"+oObject+"').value");
		oCurrentObject = oCurrentObject.split(" ").join("");
		eval("document.getElementById('"+oObject+"').value = oCurrentObject.toUpperCase()");
}

//CONVERT TO LOWERCASE AND SUPPRESS SPACE
function toLC(oObject){
		eval("var oCurrentObject = document.getElementById('"+oObject+"').value");
		oCurrentObject = oCurrentObject.split(" ").join("");
		eval("document.getElementById('"+oObject+"').value = oCurrentObject.toLowerCase()");
}

//DELETE SPACE
function noSpace(oObject){
		eval("var oCurrentObject = document.getElementById('"+oObject+"').value");
		oCurrentObject = oCurrentObject.split(" ").join("");
		eval("document.getElementById('"+oObject+"').value = oCurrentObject");
}

//CHECK FIELD
function check(oObject,oType,oError, oRequire) {
	eval("var oCurrentObject = document.getElementById('"+oObject+"').value");
	eval("var oCurrentObject2 = document.getElementById('"+oObject+"')");
	
	if(oRequire=='True' && trim(oCurrentObject)=="") {
		if(nobug == true){oCurrentObject2.focus();}
		nobug = false;
		checkError('wrong_fields', oObject, 'block', 'cc3333');
	} else {
		if(trim(oCurrentObject)!=""){
			checkError('wrong_fields', oObject, 'none', defaultColor);
			
			if(oType!='text' && oType!='date_num') {
				
				
				/*---------------*/
				/*Exception DATE*/
				/*---------------*/
				
				if(oType=='birth')
				{				
					
					var month = document.getElementById('mois_naissance').value;
					
					;
					
					if(DaysInMonth(month,oCurrentObject))
					{
						result = "Good";
					}
					else
					{
						result = null;
					}
				}
				else
				{
					pattern = checkPattern(oType);
					result= oCurrentObject.match(pattern);
				}
				/*---------------*/
				/*fin Exception DATE*/
				/*---------------*/
				
				if(result==null) { 
					if(nobug == true){oCurrentObject2.focus();}
					nobug = false;
					checkError('wrong_fields', oObject, 'block', 'cc3333');
					
				} 
				else
				{
					checkError('wrong_fields', oObject, 'none', defaultColor);
				}
			}
			/*---------------*/
				/*Exception Password*/
				/*---------------*/
				else if(oObject=='password')
				{
					oObject1 ='password1'; 
					var password1 = document.getElementById( oObject );
					var password2 = document.getElementById( oObject1 );
					if(password2.value != password1.value || password1.value.length>16 || password1.value.length<5 || password1.value ==null)
					{
						result = null;
					}
					else
					{
						result = "Good";
					}
					
					if(result==null) { 
						if(nobug == true){oCurrentObject2.focus();}
						nobug = false;
						checkError('wrong_fields', oObject, 'block', 'cc3333');
						checkError('wrong_fields', oObject1, 'block', 'cc3333');
					
					} 
					else
					{
						checkError('wrong_fields', oObject, 'none', defaultColor);
						checkError('wrong_fields', oObject1, 'block', defaultColor);
					}
				}
				
				if(oType=='date_num' && verif_num(oCurrentObject)== false)
				{
					if(nobug == true){oCurrentObject2.focus();alert(oCurrentObject2.name); }
					
					nobug = false;
					checkError('wrong_fields', oObject, 'block', 'cc3333');
					//checkError('wrong_fields', oObject1, 'block', 'cc3333');
				}
				/*---------------*/
				/*fin Exception Password*/
				/*---------------*/
		} 
		
		else {
			checkError('wrong_fields', oObject, 'none', defaultColor);
		}
	}
}

//SHOW OR HIDE ERROR
function checkError(eError, eObject, eShow, eColor){
		if(ie4) {
			eval("document.all['label_"+eError+"'].style.display = '"+eShow+"'");
			eval("document.getElementById('label_"+eObject+"').style.color = '#"+eColor+"'");
		}	
		if(ns6) {
			if(eShow=='block'){eShow='Show';}else{eShow='Hide';}
			eval("document.layers['label_"+eError+"'].display = '"+eShow+"'");
			eval("document.layers['label_"+eObject+"'].color = '#"+eColor+"'");
		}
		if(ff2) {
			eval("document.getElementById('label_"+eError+"').style.display = '"+eShow+"'");
			eval("document.getElementById('label_"+eObject+"').style.color = '#"+eColor+"'");
		}
}

//CHECK TYPE OF FIELD (PATTERN)
function checkPattern(oPattern){
	switch (oPattern)
	{
	case 'email':
		pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/; 
		break; 
	
	case 'postalcode_USA':
		pattern = /^\s*\d{5}(?:[-]\d{4})?\s*$/; 
		break; 
	
	case 'postalcode_CAN':
		pattern = /^[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]$/; 
		break; 
	
	case 'phone':
		pattern = /^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$/;
		break; 
		
	case 'birth':
		pattern = /([0-9]{4})(\/| )([0-1]{1,}[0-9]{1,})(\/| )([0-3]{1,}[0-9]{1,})/;
		/*/[0-9]{4}(\/| )[0-9]{2}(\/| )[0-9]{2}/*/
		
		break; 
		
	case 'province':
		pattern = /^(AB|BC|PE|MB|NB|NS|ON|QC|SK|TN)$/; 
		break;
	}
	
	return pattern;
}
function DaysInMonth(month,day) {
	
	if(isNaN(month) || isNaN(month))
	{
		daysInMonth = false;
	}
	else
	{
		for (var i = 1; i <= month; i++) {
			daysInMonthValue = 31
			if (i==4 || i==6 || i==9 || i==11) {daysInMonthValue = 30}
			if (i==2) {daysInMonthValue = 29}
	   } 
	   if(day > daysInMonthValue)
	   {
		 daysInMonth = false;  
	   }
	   else
	   {
		daysInMonth = true;     
	   }
	 }
	
   
   return daysInMonth;
}
function verif_date(oCurrentObject)
{
	var actualDate=new Date();
	var year = oCurrentObject.substring(0,4);
	var month = oCurrentObject.substring(5,7);
	var day = oCurrentObject.substring(8,10);
	//alert(year +"= y "+month +"= m "+day +"= d ");
	var HumanCapacity = 120;
	var maxYear = actualDate.getFullYear();
	var minYear = actualDate.getFullYear()-HumanCapacity;
	if(year >= maxYear || year <= minYear || month > 12 || month <= 0 ||  day > 31 ||  day <= 0)
	{
		return false;
	}
	else
	{
		return true;
	}
	   
}
function verif_num(oCurrentObject)
{
		var jour = oCurrentObject.substring(0,2);
		if(isNaN(jour))
		{
			return false;
		}
		else
		{
			return true;
		}
}
