//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('txt_firstname','text','0', 'True');
	check('txt_lastname','text','0', 'True');
	check('txt_email','email','0', 'True');
	check('txt_company','text','0', 'True');
	//check('txt_comments','text','0', 'True');

	if(nobug==true){
		checkError('0', 'error_0', 'none', 'E03438');
		document.form.submit();
	} else {
		checkError('0', 'error_0', 'block', 'E03438');
	}
}

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('0', 'error_0', 'none', 'E03438');
		document.form.submit();
	} else {
		checkError('0', 'error_0', 'block', 'E03438');
	}
}
/*

var rep_05 = String(Request.Form("05"));
var rep_06 = String(Request.Form("06"));
var rep_07 = String(Request.Form("07"));
var rep_08 = String(Request.Form("08"));
var rep_09 = String(Request.Form("09"));
var rep_10 = String(Request.Form("10"));
var rep_11 = String(Request.Form("11aspects_modifier"));
var rep_12 = String(Request.Form("12"));*/
function valid_getSondage()
{	
	nobug=true;
	
	check('compagnie','text','0', 'True');
	check('province','text','0', 'True');
	check('contact','text','0', 'True');
	check('telephone','phone','0', 'True');
	check('courriel','email','0', 'True');	
	check('fax','phone','0', 'True');
	
	isRadio('Q1');
	isRadio('Q2');
	isRadio('Q3');
	isCheckBox('Q4','5');
	isRadio('Q5');
	isRadio('Q6');
	isRadio('Q7');
	isRadio('Q8');
	isRadio('Q9');
	isRadio('Q10');
	isRadio('Q12');

	
	if(nobug==true){
		
		checkError('0', 'error_0', 'none', 'E03438');
		document.form.submit();
	} else {
		checkError('0', 'error_0', 'block', 'E03438');
	}
}

function valid_employee_shipment()
{	
	nobug=true;
	
	check('txt_name','text','0', 'True');
	check('txt_company','text','0', 'True');
	check('txt_email','email','0', 'True');
	check('txt_reference','text','0', 'True');
	//check('txt_comments','text','0', 'True');

	if(nobug==true){
		checkError('0', 'error_0', 'none', 'E03438');
		document.form.submit();
	} else {
		checkError('0', 'error_0', 'block', 'E03438');
	}
}

//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.form."+oObject+".value");
		oCurrentObject = oCurrentObject.split(" ").join("");
		eval("document.form."+oObject+".value = oCurrentObject.toUpperCase()");
}

//CONVERT TO LOWERCASE AND SUPPRESS SPACE
function toLC(oObject){
		eval("var oCurrentObject = document.form."+oObject+".value");
		oCurrentObject = oCurrentObject.split(" ").join("");
		eval("document.form."+oObject+".value = oCurrentObject.toLowerCase()");
}

//DELETE SPACE
function noSpace(oObject){
		eval("var oCurrentObject = document.form."+oObject+".value");
		oCurrentObject = oCurrentObject.split(" ").join("");
		eval("document.form."+oObject+".value = oCurrentObject");
}

//CHECK FIELD
function check(oObject,oType,oError, oRequire) {
	eval("var oCurrentObject = document.form."+oObject+".value");
	eval("var oCurrentObject2 = document.form."+oObject);
	
	if(oRequire=='True' && trim(oCurrentObject)=="") {
		if(nobug == true){oCurrentObject2.focus();}
		nobug = false;
		checkError('0', oObject, 'block', 'E03438');
	} else {
		if(trim(oCurrentObject)!=""){
			checkError('0', oObject, 'none', '3D3D3D');
			
			if(oType!='text') {
				pattern = checkPattern(oType);
				result= oCurrentObject.match(pattern); 
				if(result==null) { 
					if(nobug == true){oCurrentObject2.focus();}
					nobug = false;
					checkError('0', oObject, 'block', 'E03438');
					
				} 
				else
				{
					checkError('0', oObject, 'none', '3D3D3D');
				}
			}
		} else {
			checkError('0', oObject, 'none', '3D3D3D');
		}
	}
}

//SHOW OR HIDE ERROR
function checkError(eError, eObject, eShow, eColor){
		//alert(eError + " - " + eObject + " - " + eShow + " - " + eColor + " - " );
			
			if(ie4) {
				//eval("document.all['error_"+eError+"'].style.display = '"+eShow+"'");
				//alert(eObject + "//" + document.getElementById(eObject).id);
				eval("document.getElementById('"+eObject+"').style.color = '#"+eColor+"'");
				
			}	
			if(ns6) {
				//if(eShow=='block'){eShow='Show';}else{eShow='Hide';}
				//eval("document.layers['error_"+eError+"'].display = '"+eShow+"'");
				eval("document.layers['"+eObject+"'].color = '#"+eColor+"'");
			}
			if(ff2) {
				//eval("document.getElementById('error_"+eError+"').style.display = '"+eShow+"'");
				eval("document.getElementById('"+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 'province':
		pattern = /^(AB|BC|PE|MB|NB|NS|ON|QC|SK|TN)$/; 
		break;
	}
	
	return pattern;
}

function isRadio(le_name) {
   var collection;
   var result = false;

	var collection = document.form[le_name];
	//alert(le_name +" " + collection.length);
   for (i=0;i<collection.length;i++) 
   {
		
		if (collection[i].checked)
		{
			result = true;
		}
   }
	var changeName = le_name + "_id";

   if(result == true)
   {
	   	checkError('0', changeName, 'none', '3d748a');
	}
	else
	{
		
		if(nobug == true){window.location.hash=le_name;}
		nobug = false;
		checkError('0', changeName, 'block', 'E03438');
	}
	
 }
 
 function isCheckBox(le_name, qte)
 {
	var result = false;
	var focus_name = document.form[le_name+"0"];
	for (i=1;i<qte;i++) 
   {
	   var final_name = document.form[le_name+""+i];
		if (final_name.checked)
		{
			result = true;
		}
   }
	
	var changeName = le_name + "_id";	
   if(result == true)
   {
	   	checkError('0', changeName, 'none', '3d748a');
	}
	else
	{
		
		if(nobug == true){window.location.hash=le_name;}
		nobug = false;
		checkError('0', changeName, 'block', 'E03438');
	}
	
 }

