/*
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 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) && (FormField.value=="")) {alert(FormField.title + " cannot be blank"); FormField.focus(); return false}
	if ((MaxLength!="") && (FormField.value.length>MaxLength)) {alert(FormField.title + " must be no more than " + MaxLength + " characters long"); 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)) {alert(FormField.title + " must be a number"); FormField.focus(); return false}
	if ((AllowBlank==false) && (FormField.value=="")) {alert(FormField.title + " cannot be blank"); FormField.focus(); return false}
	if ((PositiveOnly) && (FormField.value<0)) {alert(FormField.title + " cannot be negative"); FormField.focus(); return false}
	if ((IntegerOnly) && (FormField.value.indexOf(".")>-1)) {alert(FormField.title + " cannot contain a decimal point"); 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)".
	if (FormatMode!=0) {FormatMode=1}
	if (FormField.value.indexOf("/")==-1) {alert(FormField.title + " is not a valid date"); 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 + " is not a valid date"); 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(FormField.title + " is not a valid date"); FormField.focus(); return false}
	if ((parseInt(ArrayDate[0 + FormatMode],10)==2) && (parseInt(ArrayDate[1 - FormatMode],10)>FSfncDaysInFebruary(parseInt(ArrayDate[2],10)))) {alert(FormField.title + " is not a valid date"); FormField.focus(); return false}
	if ((parseInt(ArrayDate[0 + FormatMode],10)<1) || (parseInt(ArrayDate[0 + FormatMode],10)>12)) {alert(FormField.title + " is not a valid date"); FormField.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' : '');
	}
