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

//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');
	check('date_naissance','birth', '0', 'False');	
	
	//check('txt_comments','text','0', 'True');

	if(nobug==true){
		checkError('wrong_fields', 'wrong_fields', 'none', 'ff0000');
		return true;
	} else {
		checkError('wrong_fields', 'wrong_fields', 'block', 'ff0000');
		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', 'ff0000');
		return true;
	} else {
		checkError('wrong_fields', 'wrong_fields', 'block', 'ff0000');
		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', 'ff0000');
		return true;
	} else {
		checkError('wrong_fields', 'wrong_fields', 'block', 'ff0000');
		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', 'ff0000');
	} else {
		if(trim(oCurrentObject)!=""){
			checkError('wrong_fields', oObject, 'none', '');
			
			if(oType!='text') {
				pattern = checkPattern(oType);
				result= oCurrentObject.match(pattern);
				/*---------------*/
				/*Exception DATE*/
				/*---------------*/
				if(oType=='birth' && result!=null)
				{
					date_result = verif_date(oCurrentObject);
					if(date_result == false)
					{
						result = null;
					}
					else
					{
						result = "Good";
					}
				}
				/*---------------*/
				/*fin Exception DATE*/
				/*---------------*/
				
				if(result==null) { 
					if(nobug == true){oCurrentObject2.focus();}
					nobug = false;
					checkError('wrong_fields', oObject, 'block', 'ff0000');
					
				} 
				else
				{
					checkError('wrong_fields', oObject, 'none', '');
				}
			}
			/*---------------*/
				/*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', 'ff0000');
						checkError('wrong_fields', oObject1, 'block', 'ff0000');
					
					} 
					else
					{
						checkError('wrong_fields', oObject, 'none', '');
						checkError('wrong_fields', oObject1, 'block', '');
					}
				}
				/*---------------*/
				/*fin Exception Password*/
				/*---------------*/
		} 
		
		else {
			checkError('wrong_fields', oObject, 'none', '');
		}
	}
}

//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 = /^((\+\d{1,3}(\.| )?\(?\d\)?(\.| )?\d{1,3})|(\(?\d{2,3}\)?))(\.| )?(\d{3,4})(\.| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/; 
		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 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;
	}
	   
}


