
// patch getElementById for non-compliant browsers
if (!document.getElementById) document.getElementById = function() { return null; }

/* add another onload event without overwriting existing events */
/* code from http://www.tek-tips.com/faqs.cfm?fid=4862 */
function addOnloadEvent(fnc) {
	if (typeof(window.addEventListener) != "undefined") {
		window.addEventListener("load", fnc, false);
	} else if (typeof(window.attachEvent) != "undefined") {
		window.attachEvent("onload", fnc);
	} else {
		if (window.onload != null) {
			var oldOnload = window.onload;
			window.onload = function(e) {
				oldOnload(e);
				window[fnc]();
			}
		} else {
			window.onload = fnc;
		}
	}
}

// the following function may not be needed in the final site
function printPage(actuator) {
	actuator.blur();
	if(alert('Most browsers can format our pages for printing without any action on your part; simply use your browser\'s Print feature to print any of our pages.\n\nIf you wish to see a preview of the printed page, use your browser\'s Print Preview feature.')) {
//		window.open(unescape(window.location.pathname+'?printpreview=1'));
	}
}

/*****************************************************
 * radioValue
 *****************************************************/
function radioValue(radioButton) {
	var returnValue = '';

	//determine radio value
	try {
		if (!radioButton.length) {
			if (radioButton.checked == true) {
				return radioButton.value;
			}
		}
		for (x = 0; x <= radioButton.length; x++) {
			if (radioButton[x].checked == true) {
				return radioButton[x].value;
				break;
			}
		}
	}
	catch(e) {
		return '';
	}
}

/*****************************************************
 * phIsBlank
 * if the string is blank return true 
 *****************************************************/
function phIsBlank(thetext) {
	if (thetext == "") { return true; } else { return false; }
}

/*****************************************************
 * phIsDecimal
 * if the string is not a decimal number return false 
 *****************************************************/
function phIsDecimal(thetext) {
	var validChars = "0123456789.";

	// text with illegal characters are invalid
	for (i = 0; i < thetext.length; i++) {
		if (validChars.indexOf(thetext.charAt(i)) == -1) { return false; }
	}
	return true;
}

/*****************************************************
 * phIsDecimalComma
 * if the string is not a decimal number return false
 * (commas are allowed)
 *****************************************************/
function phIsDecimalComma(thetext) {
	var validChars = "0123456789.,";

	// text with illegal characters are invalid
	for (i = 0; i < thetext.length; i++) {
		if (validChars.indexOf(thetext.charAt(i)) == -1) { return false; }
	}
	return true;
}

/*****************************************************
 * phIsDecimalCommaNeg
 * if the string is not a decimal number return false
 * (commas and negative values are allowed)
 *****************************************************/
function phIsDecimalCommaNeg(thetext) {
	var validChars = "0123456789.,-";

	// text with illegal characters are invalid
	for (i = 0; i < thetext.length; i++) {
		if (validChars.indexOf(thetext.charAt(i)) == -1) { return false; }
	}
	return true;
}

/*****************************************************
 * phIsHex
 * if the string is not a decimal number return false 
 *****************************************************/
function phIsHex(thetext) {
	var validChars = "0123456789ABCDEFabcdef";

	// text with illegal characters are invalid
	for (i = 0; i < thetext.length; i++) {
		if (validChars.indexOf(thetext.charAt(i)) == -1) { return false; }
	}
	return true;
}

/*****************************************************
 * phIsInteger
 * if the string is not an integrer return false 
 *****************************************************/
function phIsInteger(thetext){
	var i;

	for (i = 0; i < thetext.length; i++) {
		var c = thetext.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	return true;
}

/*****************************************************
 * phIsDate
 * Based on script from SmartWebby.com 
 * (http://www.smartwebby.com/dhtml/)
 *****************************************************/
function phIsDate(thetext) {

	// Declare valid date character, minimum year and maximum year
	var dtCh				= "/";
	var minYear			= 1900;
	var maxYear			= 2100;
	var daysInMonth	= DaysArray(12);
	var pos1				= thetext.indexOf(dtCh);
	var pos2				= thetext.indexOf(dtCh,pos1+1);
	var strMonth		= thetext.substring(0,pos1);
	var strDay			= thetext.substring(pos1+1,pos2);
	var strYear			= thetext.substring(pos2+1);

	strYr = strYear;

	if (strDay.charAt(0) == "0" && strDay.length>1) { strDay=strDay.substring(1); }
	if (strMonth.charAt(0) == "0" && strMonth.length > 1) { strMonth=strMonth.substring(1); }

	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0) == "0" && strYr.length > 1) strYr=strYr.substring(1);
	}

	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);

	if (pos1 == -1 || pos2 == -1) {
		return false;
	}

	if (strMonth.length < 1 || month < 1 || month > 12) {
		return false;
	}

	if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
		return false;
	}

	if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear ) {
		return false;
	}

	if (thetext.indexOf(dtCh,pos2+1) != -1 || phIsInteger(stripCharsInBag(thetext, dtCh)) == false) {
		return false;
	}

	return true;
}

/*****************************************************
 * phIsTime12Hour
 *****************************************************/
function phIsTime12Hour(thetext) {

	// Declare valid time character, minimum and maximum hour, minimum and maximum minute
	var dtCh				= ":";
	var minHour			= 1;
	var maxHour			= 12;
	var minMin			= 0;
	var maxMin			= 59;
	var pos1				= thetext.indexOf(dtCh);
	var strHour			= thetext.substring(0,pos1);
	var strMin			= thetext.substring(pos1+1);

	var hour = parseInt(strHour);
	var min = parseInt(strMin);

	if (pos1 == -1) return false;

	if (strHour.length < 1 || hour < minHour || hour > maxHour) return false;

	if (strMin.length < 1 || min < minMin || min > maxMin) return false;

	return true;
}

/*****************************************************
 * stripCharsInBag
 * Based on script from SmartWebby.com 
 * (http://www.smartwebby.com/dhtml/)
 *****************************************************/
function stripCharsInBag(s, bag){
	var i;
	var returnString = "";

	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

/*****************************************************
 * daysInFebruary
 * Based on script from SmartWebby.com 
 * (http://www.smartwebby.com/dhtml/)
 *****************************************************/
function daysInFebruary (year) {
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

/*****************************************************
 * DaysArray
 * Based on script from SmartWebby.com 
 * (http://www.smartwebby.com/dhtml/)
 *****************************************************/
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
		if (i == 2) { this[i] = 29 }
	}
	return this;
}

/*****************************************************
 * phValidEmail
 * check if text appears to be an email address 
 * return true or false 
 *****************************************************/
function phValidEmail(email) {
	// characters not allowed in an email address
	invalidChars = " /:,;";

	// blank emails are invalid
	if (email == "") { return false }
	
	// emails with illegal characters are invalid
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) > -1) { return false }
	}

	// emails without an @ are invalid
	atPos = email.indexOf("@",1)
	if (atPos == -1) { return false }
	
	// emails with more than one @ are invalid
	if (email.indexOf("@",atPos +1) > -1) { return false }

	// emails without a . are invalid
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) { return false }

	// emails with less than three chars after the 
	// first . following the @ are invalid
	if (periodPos + 3 > email.length) { return false }
	
	return true;
}

/*****************************************************
 * phNoBrackets
 * check if text contains brackets that might include
 * inline HTML or Javascript. Return true or false 
 *****************************************************/
function phNoBrackets(theText) {
	// characters not allowed in an email address
	invalidChars = "<>";
	
	// text with illegal characters is invalid
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (theText.indexOf(badChar,0) > -1) { return false }
	}
	return true;
}

/*****************************************************
 * phValidURL
 * check if text appears to be an email address 
 * finish: 0 = do not alter the URL (just return if valid)
 *         1 = remove "http://" from the front of the address
 *         2 = add "http://" to the front of the address 
 * 
 * return the URL, or empty string if invalid 
 *****************************************************/

function phValidURL(url) {
	// characters not allowed in a url
	invalidChars = " ,;";

	// blank urls are invalid
	if (url == "") { return false }

	// urls with illegal characters are invalid
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (url.indexOf(badChar,0) > -1) { return false }
	}

	// urls without a . are invalid
	periodPos = url.indexOf(".")
	if (periodPos == -1) { return false }

	return true;
}

/*****************************************************
 * phCleanURL
 * finish: 0 = remove "http://" from the front of the address
 *         1 = add "http://" to the front of the address 
 * 
 * return the URL, or empty string if invalid 
 *****************************************************/

function phCleanURL(url,finish) {

	// if we're stripping off http://
	if ( finish == 0 ) {
		if ( url.indexOf("http://") == 0 ) {
			newURL = url.substr(7);
			return newURL;
		}
		else { return url }
	}

	// if we're adding http://
	if ( finish == 1 ) {
		if ( url.indexOf("http://") == -1 ) {
			newURL = "http://" + url;
			return newURL;
		}
		else { return url }
	}

	// finish wasn't set to a known value
	else { return url }
}

/*****************************************************
 * PH_changePerspective
 * Not really a validation script, but it fit in here
 * so nicely...
/*****************************************************/
function PH_changePerspective(selectObject) { 
	eval("parent.location='globals/php/perspectiveChange.php?id="+selectObject.options[selectObject.selectedIndex].value+"'");
}
