/*
PRODUCT: 	General JavaScript Utilities for Cookies, Popup Windows, and Form Validation
VERSION:	1.01, April 2004
CONTACT:	Future Shock Ltd, www.Future-Shock.net, post@future-shock.net

This file may be used freely as long as none of the comments are not removed.

NOTES:
Optional arguments for functions can be skipped by sending null when you call the function,
for example: FSfncSetCookie('MyCookieName','MyCookieValue',null,null,'www.mydomain.com',true).
This is not required if you are skipping all optional arguments,
for example: FSfncSetCookie('MyCookieName','MyCookieValue')
Both routines use the TITLE attribute of the form element in validity warning messages.
*/

// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function Trim(TRIM_VALUE)
{
	if(TRIM_VALUE.length < 1)
	{
		return "";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	
	if(TRIM_VALUE == "")
	{
		return "";
	}
	else
	{
		return TRIM_VALUE;
	}
} //End Function

// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0)
	{
		return"";
	}
	var iTemp = v_length - 1;

	while(iTemp > -1)
	{
		if(VALUE.charAt(iTemp) == w_space){
		}
		else
		{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp - 1;

	} //End While
	return strTemp;

} //End Function

// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function LTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	if(v_length < 1)
	{
		return "";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length)	
	{
		if(VALUE.charAt(iTemp) == w_space){
		}
		else
		{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function


// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function FSfncCheckComboBox(FormField,AllowBlank) 
{
	var v_field = FormField.options[FormField.selectedIndex].value;
	
	if ((AllowBlank==false) && ((v_field=="") || (v_field=="0"))) 
	{
		if (v_language == "fr" || v_language == "")
		{
			alert("Erreur! Veuillez saisir une valeur pour ' " + FormField.title + " '");
		}
		else
		{
			alert("Error! Please enter a value for ' " + FormField.title + " '"); 
		}

		FormField.focus(); 
		return false;
	}
	return true;
}


// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function FSfncCheckRadioButton(FormField) 
{

	// -----------------------------------------------------
	// Messages
	// -----------------------------------------------------
	if (v_language == "fr" || v_language == "")
	{
		v_msg = "Erreur! Veuillez sélectionner une valeur pour ";
	}
	else
	{
		v_msg = "Error! Please select a value for ";
	}
	
	// Loop from zero to the one minus the number of radio button selections
	var v_trouver = false;
	for (v_counter = 0; v_counter < FormField.length; v_counter++)
	{
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (FormField[v_counter].checked)
		{
			v_trouver = true; 
		}
	}
	
	if (v_trouver)
	{
		return (true);
	}
	else
	{
		alert(v_msg + "' " + FormField[0].title + " '");
		return (false);
	}
}

// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function FSfncCheckString(FormField, AllowBlank, MaxLength) 
{
	// MaxLength is optional, when not provided the string is only checked for being blank.
	// Implement by adding onSubmit handler to FORM or onBlur handler to INPUT or TEXTAREA 
	// element, eg. onBlur="FSfncCheckString(this,false,10)".
	
	if ((AllowBlank==false) && (Trim(FormField.value)=="")) 
	{
		if (v_language == "fr" || v_language == "")
		{
			alert("Erreur! Veuillez saisir une valeur pour ' " + FormField.title + " '"); 
		}
		else
		{
			alert("Error! Please enter a value for ' " + FormField.title + " '"); 
		}

		FormField.value="";
		FormField.focus(); 
		return false;
	}
	
	if ((MaxLength!="") && (FormField.value.length>MaxLength)) 
	{
		if (v_language == "fr" || v_language == "")
		{
			alert("Erreur! Veuillez saisir un maximum de ' " + MaxLength + "' caractères pour ' " + FormField.title + " '");
		}
		else
		{
			alert("Error! Please enter a maximum of ' " + MaxLength + "' caracters for ' " + FormField.title + " '");
		}

		FormField.focus(); 
		return false;
	}
	return true;
}


// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function FSfncCheckNumber(FormField,AllowBlank,PositiveOnly,IntegerOnly) 
{
	// AllowBlank, PositiveOnly, and IntegerOnly are optional
	// Implement by adding onSubmit handler to FORM or onBlur handler to INPUT element, eg. onBlur="FSfncCheckNumber(this,false,true,true)".
	if (isNaN(FormField.value)) 
	{	
		if (v_language == "fr" || v_language == "")
		{
			alert("Erreur! Veuillez saisir une valeur entière pour ' " + FormField.title + " '");
		}
		else
		{
			alert("Error! Please enter a number value for ' " + FormField.title + " '"); 
		}
		FormField.value="";
		FormField.focus();
		return false;
	}
	
	if ((AllowBlank==false) && (Trim(FormField.value)=="")) 
	{
		if (v_language == "fr" || v_language == "")
		{
			alert("Erreur! ' " + FormField.title + "' ne peut pas être une valeur nulle"); 
		}
		else
		{
			alert("Error! ' " + FormField.title + "' cannot be blank"); 
		}

		FormField.value="";
		FormField.focus(); 
		return false;
	}
	
	if ((PositiveOnly) && (FormField.value<0)) 
	{	
		if (v_language == "fr" || v_language == "")
		{
			alert("Erreur! ' " + FormField.title + "' ne peut pas être une valeur négative"); 
		}
		else
		{
			alert("Error! ' " + FormField.title + "' cannot be negative value"); 
		}

		FormField.value="";
		FormField.focus(); 
		return false
	}
	
	if ((IntegerOnly) && (FormField.value.indexOf(".")>-1)) 
	{
		if (v_language == "fr" || v_language == "")
		{
			alert("Erreur! ' " + FormField.title + "' ne peut pas être une valeur décimale"); 
		}
		else
		{
			alert("Error! ' " + FormField.title + "' cannot contain a decimal point");
		}

		FormField.value="";
		FormField.focus(); 
		return false
	}
	
	return true;
}


// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function FSfncCheckTime(FormField) {
	// Check time is supplied in valid 24 hour clock (hh:mm) format.
	// Implement by adding onSubmit handler to FORM or onBlur handler to INPUT element, eg. onBlur="FSfncCheckTime(this)".
	if (FormField.value.indexOf(":")==-1) {alert(FormField.title + " is not a valid time"); FormField.focus(); return false}
	var ArrayTime = FormField.value.split(":");
	if ((ArrayTime.length!=2) || (isNaN(ArrayTime[0])) || (ArrayTime[0]=="") || (isNaN(ArrayTime[1])) || (ArrayTime[1]=="")) {alert(FormField.title + " is not a valid time"); FormField.focus(); return false}
	if ((parseInt(ArrayTime[0],10)<0) || (parseInt(ArrayTime[0],10)>23) || (parseInt(ArrayTime[1],10)<0) || (parseInt(ArrayTime[1],10)>59)) {alert(FormField.title + " is not a valid time"); FormField.focus(); return false}
	return true;
	}


// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function FSfncCheckDateFormat(FormField,FormatMode) 
{
	// Check date supplied is valid. FormatMode is optional, when not supplied it 
	// defaults to 1 (1=dd/mm/yyyy, 0=mm/dd/yyyy).
	// Implement by adding onSubmit handler to FORM or onBlur handler to INPUT element, eg. onBlur="FSfncCheckDateFormat(this,0)".

	var v_msg;
 
 //	alert (FormField.value);
	
 
	if (v_language == "fr" || v_language == "")
	{
		v_msg = "Erreur! ' " + FormField.title + " ' n'a pas un format valide.\nVeuillez saisir une date dont le format est 'dd/mm/yyyy'";
	}
	else
	{
		v_msg = "Erreur! ' " + FormField.title + " ' is not valid date. \nPlease enter a date with a format 'dd/mm/yyyy'";
	}
  
  
	if (FormField.value !='')
	{
		if (FormatMode!=0) 
		{
			FormatMode=1;
		}
	
		if (FormField.value.indexOf("/")==-1) 
		{
			alert(v_msg); 
			FormField.value=''; 
			FormField.focus(); 
			return false;
		}
	
		var ArrayDate = FormField.value.split("/");

		if ((ArrayDate.length!=3) || (isNaN(ArrayDate[0])) || (ArrayDate[0]=="") || (isNaN(ArrayDate[1])) || (ArrayDate[1]=="") || (isNaN(ArrayDate[2])) || (ArrayDate[2]=="")) 
		{
			alert(FormField.title + v_msg); 
			FormField.value=''; 
			FormField.focus(); 
			return false;
		}

		var daysInMonth = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
		if ((parseInt(ArrayDate[1 - FormatMode],10)<1) || (parseInt(ArrayDate[1 - FormatMode],10)>daysInMonth[parseInt(ArrayDate[0 + FormatMode],10)])) 
		{
			alert(v_msg); 
			FormField.value=''; 
			FormField.focus(); 
			return false;
		}
	
		if ((parseInt(ArrayDate[0 + FormatMode],10)==2) && (parseInt(ArrayDate[1 - FormatMode],10)>FSfncDaysInFebruary(parseInt(ArrayDate[2],10)))) 
		{	
			alert(v_msg); 
			FormField.value=''; 
			FormField.focus(); 
			return false;
		}
	
		if ((parseInt(ArrayDate[0 + FormatMode],10)<1) || (parseInt(ArrayDate[0 + FormatMode],10)>12)) 
		{
			alert(v_msg); 
			FormField.value=''; 
			FormField.focus(); 
			return false;
		}
	
		return true;
	}
/*
	else
	{
		alert('kk');
		return false;
	}
*/
}


// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function FSfncCompare2Values(FormField1,FormField2,TypeField,FormatMode) 
{

	// -----------------------------------------------------
	// FormatMode :	0 ... Egalité
	//				1 ... >
	//				2 ... >=
	//				3 ... <
	//				4 ... >=
	// -----------------------------------------------------
	if (FormatMode == 0) {v_format_mode_fr = ' = à ';  v_format_mode_eng = ' = to '; }
	if (FormatMode == 1) {v_format_mode_fr = ' > à ';  v_format_mode_eng = ' > to '; }
	if (FormatMode == 2) {v_format_mode_fr = ' >= à '; v_format_mode_eng = ' >= to '; }
	if (FormatMode == 3) {v_format_mode_fr = ' < à ';  v_format_mode_eng = ' < to '; }
	if (FormatMode == 4) {v_format_mode_fr = ' <= à '; v_format_mode_eng = ' <= to '; }

	// -----------------------------------------------------
	// Messages
	// -----------------------------------------------------
	if (v_language == "fr" || v_language == "")
	{
		v_msg = "Erreur! ' " + FormField1.title + "' doit être" + v_format_mode_fr + "' " + FormField2.title + " '";
	}
	else
	{
		v_msg = "Erreur! ' " + FormField1.title + "' must be" + v_format_mode_fr + "' " + FormField2.title + " '";
	}
	
	// -----------------------------------------------------
	// TypeField :	0...Date
	//				1...Number
	//				2...String
	// -----------------------------------------------------
	if (TypeField == 0) {v_field1 = Date.parse(FormField1.value); v_field2 = Date.parse(FormField2.value);}

	// -----------------------------------------------------
	// Compare 2 fields
	// -----------------------------------------------------
	// if (!(Date.parse(p_form.txtDateDebut.value) < Date.parse(p_form.txtDateFin.value)) )
	
	if (FormatMode = 3)
	{
		if ( !(v_field1 < v_field2) )
		{
			alert(v_msg);
			FormField1.focus();
			return(false);
		}

		return true;
	}

}

// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function FSfncDaysInFebruary(year) {return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 )}


// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function FSfncPopUp(NextPage,width,height,resizable,scrollbars,status) {
	// resizable,scrollbars, and status are optional, when not provided these default to no.
	// Implement by adding onClick handler to any element, eg. onClick="FSfncPopUp('PopupContent.htm',200,100,'yes','yes','yes')".
	if (resizable=="")	{resizable="no"}
	if (scrollbars=="") {scrollbars="no"}
	if (status=="")		{status="no"}
	x=self.screenLeft + 10;
	y=self.screenTop + 10;
	if (navigator.appVersion.indexOf("AOL")>0) {winName="A" + (Math.round(Math.random() * 1000))} else {winName="FSpopUpWindow"}
	FSpopUp=window.open(NextPage,winName,'toolbar=no,width=' + width + ',height=' + height + ',left=' + x + ',screenX=' + x + ',top=' + y + ',screenY=' + y + ',status=' + status + ',scrollbars=' + scrollbars + ',resizable=' + resizable + ',menubar=no,directories=no');
	}


// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function FSfncReadCookie(key) {
	// Implement by calling the function with the name of the cookie required, eg. FSfncReadCookie('MyCookieName'), the cookie value will be returned.
	var cookie_string='' + document.cookie;
	var cookie_array=cookie_string.split('; ');
	for (var i=0; i<cookie_array.length; i++) {
		var single_cookie=cookie_array[i].split('=');
		if (single_cookie.length!=2) {continue}
		if (key==unescape(single_cookie[0])) {return unescape(single_cookie[1])}
		}
	return 'None';
	}


// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function FSfncSetCookie(name,value,expires,path,domain,secure) {
	// expires,path,domain,secure are optional, if supplied the expires must be a JavaScript date object, path and domain are strings, and secure is true/false.
	// Implement by calling the function, eg. FSfncSetCookie('MyCookieName','MyCookieValue').
	document.cookie=name + '=' + escape(value) + ((expires == null) ? '' : ('; expires=' + expires.toGMTString())) + ((path == null) ? '' : ('; path=' + path)) + ((domain == null) ? '' : ('; domain=' + domain)) + ((secure == true) ? '; secure' : '');
	}


// --------------------------------------------------------------------
//
// --------------------------------------------------------------------
function FSfncCheckEmail(FormField, AllowBlank, MaxLength) 
{
	if ((AllowBlank==false) && (Trim(FormField.value)=="")) 
	{
		if (v_language == "fr" || v_language == "")
		{
			alert("Erreur! Veuillez saisir une valeur pour ' " + FormField.title + " '"); 
		}
		else
		{
			alert("Error! Please enter a value for ' " + FormField.title + " '"); 
		}

		FormField.value="";
		FormField.focus(); 
		return false;
	}

	if ((MaxLength!="") && (FormField.value.length>MaxLength)) 
	{
		if (v_language == "fr" || v_language == "")
		{
			alert("Erreur! Veuillez saisir un maximum de ' " + MaxLength + "' caractères pour ' " + FormField.title + " '"); 
		}
		else
		{
			alert("Error! Please enter a maximum of ' " + MaxLength + "' caracters for ' " + FormField.title + " '"); 
		}

		FormField.focus(); 
		return false;
	}
	
	v_verifier = (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(FormField.value));
	
	if ( (v_verifier == false) && (FormField.value.length > 0) )
	{
		if (v_language == "fr" || v_language == "")
		{
			alert("Erreur! Veuillez saisir une adresse valide pour ' " + FormField.title + " '");
		}
		else
		{
			alert("Error! Please enter a valid email address for ' " + FormField.title + " '"); 
		}
		FormField.value="";
		FormField.focus();
		return (false);
	}
	return(true);
}





