// ********* ********* ********* ********* *********
// *  key press validation
// ********* ********* ********* ********* *********
//usage: onkeypress="onKeyPressValidate(event, 'nohtml');"

function onKeyPressValidate(e, strValidation)
{
	var objEvent = window.event? event : e;
	var key = objEvent.keyCode ? objEvent.keyCode : objEvent.which ? objEvent.which : objEvent.charCode;

	if ( (strValidation == "numeric")       && (key<45 || key>57) ) objEvent.returnValue = false;
	if ( (strValidation == "alphabetic")    && ((key<65 || key>122) & (key!=32)) )  objEvent.returnValue = false;
	if ( (strValidation == "alphanumeric")  && ((key>32 & key<38) || (key>39 & key<44) || key==47 || (key>57 & key<65) || (key>90 & key<95) || (key>122 & key<127)) )  objEvent.returnValue = false;
	if ( (strValidation == "hex")			&& (!((key>=45 && key<=57) || (key>=65 && key<=70) || (key>=97 && key<=102) || (key==35))) ) objEvent.returnValue = false;
	if ( (strValidation == "nohtml")        && (key==60 || key==62) )  objEvent.returnValue = false;
	if ( (strValidation == "nosinglequote") && (key==39) )  objEvent.returnValue = false;
	if ( (strValidation == "nodoublequote") && (key==34) )  objEvent.returnValue = false;
	if ( (strValidation == "noquote")       && (key==34 || key==39 ) )  objEvent.returnValue = false;
};

// ********* ********* ********* ********* *********
// *  email obscure
// ********* ********* ********* ********* *********
function gfntSendEmail(strLeft, strRight) {
	var w=window.open("mailto:" + strLeft + strRight,"newwindow");
	if (w) w.close();
};


// ********* ********* ********* ********* *********
// *  disable textbox
// ********* ********* ********* ********* *********
function gfntDisableField(obj, blnValue) {
	if (document.getElementById) {
		obj.disabled = blnValue;
		obj.style["backgroundColor"] = "#ffffff";
		if (blnValue) {
			obj.style["backgroundColor"] = "#eeeeee";
			obj.value = "";
		};
	} else {
		// can't disable NS 4.79 fields
		// permanently disabled fields have [onFocus="blur();"]
	};
};

// ********* ********* ********* ********* *********
// *  Cross browser compatability functions
// ********* ********* ********* ********* *********
function gfntDisplayProps(obj) {
// returns a string of an objects properties and methods
	var i;
	var strOutput;
	var strTerm;
	var strData;
	var strSpace;
	var lngCols;
	
	// initialize number of columns
	lngCols=4;
	if (document.layers) lngCols=3;

	// initialize an empty string
	strSpace = "                                        ";
	
	strOutput = obj.name + "\n\n";
	i=1;
	for (var prop in obj) {
		strTerm = "\t";
		if (i==lngCols) strTerm = "\n";

		i=i+1;
		if (i>lngCols) i=1;	

		strData = "." + prop + " = " + obj[prop];
		if (strData.length<30) strData += strSpace.substring(0, 30-strData.length);
		if (strData.length>30) strData = strData.substring(0, 30);

		strOutput += strData + strTerm;
	};	
	alert(strOutput);
	return strOutput;
};

function gfntGetForm(formName) {
// returns a reference to a Form

	// Netscape 6.2.2 
	// Internet Explorer 5
	if (document.getElementById) {
		return document.getElementById(formName);
	} else if (document.all) {
		return document.all[formName];
	// Netscape 4.79		
	} else if (document.layers) {
		return document.forms[formName];
	} else {
		return false;
	};
};

function gfntGetFormElement(formName, objectName) {
// returns a reference to an object of a Form
// formName = object reference or string

	// if we are passed an object rather than string
	// (test the name property)
	// translate the variable into the form name
	if (formName.name!=null) formName = formName.name;

	// Netscape 6.2.2 
	// Internet Explorer 5
	if (document.getElementById) {
		return document.getElementById(objectName);
	} else if (document.all) {
		return document.all[objectName];
	// Netscape 4.79	
	} else if (document.layers) {
		return document.forms[formName].elements[objectName];
	} else {
		return false;
	};
};

function gfntGetRadio(theForm, objGroup, strID, lngValue) {
	var obj;	

	// Netscape 4.79		
	if (document.layers) {
		for (var i=0; i<objGroup.length; i++) {
			obj=objGroup[i];
			if (obj.value==lngValue) {
				return obj;
			};
		};
		//alert("gfntGetRadio : Bad call...\nstrID=" + strID + "\nlngValue=" + lngValue);
		return false;

	//everthing else
	} else {
		return gfntGetFormElement(theForm, strID);
	};	
};

function gfntGroupSelected(theForm, objGroup, strName) {
	var obj;	

	// Netscape 4.79		
	if (document.layers) {
		if (objGroup.length) {
			for (var i=0; i<objGroup.length; i++) {
				obj=objGroup[i];
				if (obj.checked) return true;
			};
		// if the "objGroup" isn't an array, just see if it's checked
		} else {
			if (objGroup.checked) return true;
		};			
		return false;

	//everthing else
	} else {
		for (i=0; i<theForm.elements.length; i++) {
			obj = theForm.elements[i];
			if (obj.type=="radio") {
				// alert(obj.id + "\n" + obj.name + "\n" + strName);
				if (obj.name==strName) {
					if (obj.checked) return true;
				};
			};
		};		
		return false;
	};	
};

function gfntDropdownSelected(theForm, objDropdown) {
	var obj;	

	// Netscape 4.79		
	if (document.layers) {
		if (objDropdown.length) {
			for (var i=0; i<objDropdown.length; i++) {
				obj=objDropdown[i];
				if (obj.selected==true && obj.value!=0) return true;
			};
		};			
		return false;

	//everthing else
	} else {
		if (objDropdown.value!=0) return true;
		return false;
	};	
};


// ********* ********* ********* ********* *********
// * validation functions
// ********* ********* ********* ********* *********

function gfntValidateTextbox( obj, strFieldName, lngLength) {
	if (obj.value == "") {
		alert("Please enter a value for the \"" + strFieldName +"\" field.");
		obj.focus();
		return false;
	};
  
	if (lngLength != null) {
		if (obj.value.length > lngLength) {
			alert("Please limit the \"" + strFieldName +"\" field to " + lngLength.toString() + " characters.");
			obj.focus();
			return false;
		};
	};	

	return true;
};

function gfntValidateNumericTextbox( obj, strFieldName, lngLength) {
	if (obj.value == "") {
		alert("Please enter a value for the \"" + strFieldName +"\" field.");
		obj.focus();
		return false;
	};
  
	if (lngLength != null) {
		if (obj.value.length > lngLength) {
			alert("Please limit the \"" + strFieldName +"\" field to " + lngLength.toString() + " characters.");
			obj.focus();
			return false;
		};
	};	

	var vntValue = parseFloat(obj.value);	
	if (isNaN(vntValue)==true) {
		alert("Please enter a numeric value in the \"" + strFieldName +"\" field.");
		obj.focus();
		return false;
	}	

	return true;
};

function gfntValidateTextboxLength( obj, strFieldName, lngLength) {
	if (obj.value.length > lngLength) {
		alert("Please limit the \"" + strFieldName +"\" field to " + lngLength.toString() + " characters.");
		obj.focus();
		return false;
	};	

	return true;
};

function gfntValidateSelect( obj, strMsg ) {
	var lngCurIndex = obj.selectedIndex;
	if (obj.options[lngCurIndex].value == "") {
		alert(strMsg);
		obj.focus();
		return false;
	};

	return true;
};

function gfntValidateList( obj, strMsg, blnSetFocus ) {
	var lngCurIndex = obj.selectedIndex;
	if (lngCurIndex == 0) {
		if (strMsg != "") alert(strMsg);
		if (blnSetFocus == true) obj.focus();
		return false;
	} else {	
		if (obj.options[lngCurIndex].value == ""){
			if (strMsg != "") alert(strMsg);
			if (blnSetFocus == true) obj.focus();
			return false;
		};
	};

	return true;
};


/*
** ********* ********* ********* ********* ********* ********* ********* *********
** Function IsEmailValid(strEmail) 
** Action: checks if an email is correct. 
** Parameter: strEmail - the Email address 
** Returned value: on success it returns True, else False. 
** ********* ********* ********* ********* ********* ********* ********* *********
*/
function gfntIsEmailValid(strEmail) {
	var strArray;
	var strItem;
	var strValidChars;
	var i;
	var j;
	var c;
	var r = "\r\n";
	
	// initialize array and local variables
	strArray = new Array("1", "2");
	strValidChars = "1234567890abcdefghijklmnopqrstuvwxyz_-.";
    
	// split the email address in two parts: name@domain.ext 
	i = strEmail.indexOf("@");
	if ( i == -1 ) return false;  // email didn't include @
	strArray[0] = strEmail.substring(0, i); 
	strArray[1] = strEmail.substring(i+1, strEmail.length);

	// check each part
	// This next line is not Netscape 4.79 compliant ( the line below it is a fudge )
	// for (strItem in strArray) {
	for (j=0; j<strArray.length; j++) {
		strItem = strArray[j];

	        // no part can be void 
        	i = strItem.length;
			if ( i <= 0 ) return false;

	        // check each character of the part 
        	// only following "abcdefghijklmnopqrstuvwxyz_-." 
	        // characters and the ten digits are allowed 
        	for (i=0; i<=strItem.length; i++) {
			c = strItem.substring(i, i+1);
			c = c.toLowerCase();
			if ( strValidChars.indexOf(c) == -1 ) return false;
		}; 		
       		
		// the first and the last character in the
		// part cannot be . (dot) 
		c = strItem.substring( strItem.length-1, strItem.length );
		if ( strItem.substring(0,1) == "." || c == "." ) return false;
	};

	// the second part (domain.ext) must contain a . (dot) 
	strItem = strArray[1];
	if ( strItem.indexOf(".") == -1 ) return false;

	// check the length of the extension  
	// the length of the extension can be only 2, 3, or 4 
	// to cover the new "info" extension 
	strItem = strArray[1];
	i = strItem.length - strItem.lastIndexOf(".");
	if ( i < 2 || i > 4 ) return false;

	// after . (dot) cannot follow a . (dot) 
	i = strEmail.indexOf("..") 
	if ( i > -1 ) return false;

	// finally it's OK  
	return true;
};

/*
** ********* ********* ********* ********* ********* ********* ********* *********
** Function IsDate(dateStr) 
** Action: checks if an date is correct. 
** Parameter: dateStr - the date
** Returned value: on success it returns True, else False. 
** ********* ********* ********* ********* ********* ********* ********* *********
*/
function gfntIsDate(dateStr) {
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat);
	
	if (matchArray == null) return false;

	// parse date into variables
	var lngMonth = matchArray[1]; 
 	var lngDay = matchArray[3];
	var lngYear = matchArray[5];

	if (lngMonth < 1 || lngMonth > 12) return false;
	if (lngDay < 1 || lngDay > 31) return false;

	if ((lngMonth==4 || lngMonth==6 || lngMonth==9 || lngMonth==11) && lngDay==31) return false;

	if (lngMonth == 2) { // check for february 29th
		var isleap = (lngYear % 4 == 0 && (lngYear % 100 != 0 || lngYear % 400 == 0));
		if (lngDay > 29 || (lngDay==29 && !isleap)) return false;
	};

	return true; 
};

/*
** ********* ********* ********* ********* ********* ********* ********* *********
** Function URLEncode(strText) 
** Action: performs equivalent of ASP Server.URLEncode
** ********* ********* ********* ********* ********* ********* ********* *********
*/
function gfntURLEncode(strText) {
	strText = escape(strText);
	strText = gfntReplace(strText,'+','%2B');
	strText = gfntReplace(strText,'%20','+');
	strText = gfntReplace(strText,'%A1','%B0');
	strText = gfntReplace(strText,'%80','%C4');
	strText = gfntReplace(strText,'%81','%C5');
	strText = gfntReplace(strText,'%82','%C7');
	strText = gfntReplace(strText,'%83','%C9');
	strText = gfntReplace(strText,'%84','%D1');
	strText = gfntReplace(strText,'%85','%D6');
	strText = gfntReplace(strText,'%86','%DC');
	strText = gfntReplace(strText,'%87','%E1');
	strText = gfntReplace(strText,'%88','%E0');
	strText = gfntReplace(strText,'%89','%E2');
	strText = gfntReplace(strText,'%8A','%E4');
	strText = gfntReplace(strText,'%8B','%E3');
	strText = gfntReplace(strText,'%8C','%E5');
	strText = gfntReplace(strText,'%8D','%E7');
	strText = gfntReplace(strText,'%8E','%E9');
	strText = gfntReplace(strText,'%8F','%E8');
	strText = gfntReplace(strText,'%90','%EA');
	strText = gfntReplace(strText,'%91','%EB');
	strText = gfntReplace(strText,'%92','%ED');
	strText = gfntReplace(strText,'%93','%EC');
	strText = gfntReplace(strText,'%94','%EE');
	strText = gfntReplace(strText,'%95','%EF');
	strText = gfntReplace(strText,'%96','%F1');
	strText = gfntReplace(strText,'%97','%F3');
	strText = gfntReplace(strText,'%98','%F2');
	strText = gfntReplace(strText,'%99','%F4');
	strText = gfntReplace(strText,'%9A','%F6');
	strText = gfntReplace(strText,'%9B','%F5');
	strText = gfntReplace(strText,'%9C','%FA');
	strText = gfntReplace(strText,'%9D','%F9');
	strText = gfntReplace(strText,'%9E','%FB');


	strText = gfntReplace(strText,'%9F','%FC');
	
	return strText;
};

// ********* ********* ********* ********* *********
// * utility functions
// ********* ********* ********* ********* *********
function gfntSort(arr) {
	var i;
	var j;
	var s;

	for (j=0; j<=arr.length; j++) {
		for (i=1;  i<=arr.length; i++) {
			if (arr[i] < arr[i-1]) {
				s = arr[i];
				arr[i] = arr[i-1];
				arr[i-1] = s;
			};
		};
	};

	return arr;
};

function gfntReplace(strData, strSrc, strDst) {
	var i;
	var j=0;
	var strLeft;
	var strRight;
	var strTemp = String.fromCharCode(1);
	
	// replace strSrc with a temporary string ( place holder )
	i = strData.indexOf(strSrc)
	while (i>0 || i<j) {
		strLeft = strData.substring(0, i);
		strRight = strData.substring(i + strSrc.length, strData.length);
		if (strRight!=strData) strData = strLeft + strTemp + strRight;

		j = i;
		i = strData.indexOf(strSrc);
	};

	// replace temporary string with strDst
	j=0;
	i = strData.indexOf(strTemp)
	while (i>0 || i<j) {
		strLeft = strData.substring(0, i);
		strRight = strData.substring(i + strTemp.length, strData.length);
		if (strRight!=strData) strData = strLeft + strDst + strRight;

		j = i;
		i = strData.indexOf(strTemp);
	};

	return strData;
};




