/*****************************************************

 * phValidate

 * 9/09/2002

 * 

 * useful form validation functions 

 *****************************************************/



/*****************************************************

 * 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;

}



/*****************************************************

 * 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

 * if the string is blank return true 

 * 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;

}



/*****************************************************

 * 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 }

}