////////////////////////////////////////////////////////////////////
/////////VALIDAITION FUNCTIONS//////////////////////////////////////
////////////////////////////////////////////////////////////////////
if(window.includeOnce) { 
	includeOnce("/javascript/textformat.js");
	includeOnce("/javascript/ajax.js");
}

function isAge(text) {
	//if(console) console.info('isAge(' + text + ')  Called');
  	if (isInteger(text)) return (parseInt(text) >= 0 && parseInt(text) < 140);
  	return false;
}
function isBirthday(text) {
	//if(console) console.info('isBirthday(' + text + ')  Called');
	var birthdate, arrDate, now;
	now = new Date();
	if(text==null || text=="") return false;
	birthdate = FormatDate(text,"Y-m-d");
	//if(console) console.info('  Formatted Date: ' + birthdate);
	if(birthdate=="") return false;
	arrDate = birthdate.split("-");
	birthdate = new Date(parseInt(arrDate[0]),parseInt(arrDate[1])-1,parseInt(arrDate[2]));
	//if(console) console.info('  Parsed Date: ',arrDate);
	///if(console) console.info('  Parsed Date: ' + birthdate);
	//if(console) console.info('  Now Date: ' + now);
	if(birthdate=="Invalid Date") return false;
	if(birthdate > now) return false;
	if((parseInt(arrDate[0])+141)<(now.getFullYear())) return false; 
	return true;	  
}
function isBYUID(text) { // has exactly 9 numeric characters
	var stripped = text.replace(/[\(\)\.\-\ \/A-Za-z]/g, '');
	if(stripped.length==9) return true;
	return false;
}
function isCreditCard(text) {       
	//code.dreamincode.net/snippet154.htm
	text = new String(text);
	if(text==null || text.length==0) return false;
	if (text.length < 9) return false;
	if (text.length > 19) return false;
	//if(console) console.info(text.length);
	var sum, mul, l, digit;
    sum = 0; mul = 1; l = text.length;
    for (i = 0; i < l; i++) {
    	digit = text.substring(l-i-1,l-i);
        tproduct = parseInt(digit ,10)*mul;
        if (tproduct >= 10) { sum += (tproduct % 10) + 1; } else { sum += tproduct; }
        if (mul == 1) { mul++; }  else { mul--; }
    }
    return ((sum % 10) == 0);
}
function isDate(text) {
	if(text==null || text=="") return false;
	var d,m,y,da,ds,td;
	ds = FormatDate(text);	
	if(ds=="") 	return false;
	da = ds.split('-');
	d = da[2]; m = da[1]-1; y = da[0];
	if(y > (new Date()).getYear() + 2000) return false;
	if(m >= 12 || m < 0) return false;
	td=new Date(y,m,d);
	if(td.getMonth()==m) return true;
	return false;
}
function isEmail(text) {
	if(text==null) return false;
	if(text.indexOf(".") <= 0) return false;
	if(text.indexOf("@") <= 0) return false;
	if(text.indexOf("@",text.indexOf("@")+1) > 0) return false;
	if(((text.indexOf(".") > 0) && (text.indexOf("@") > 0))) return true;
	return false;
}
function isFunction(text) {	
	try {
		return eval("if(window." + text + ") { functionexists=true;} else {functionexists=false;}"); 
	} catch(e) { return false; }
}
function isGPA(text) {  // is a real positive number between 
	if(isNaN(text) || text=='') return false;
	number = parseFloat(text);
	if(number<0  || number>5) return false;
	return true;
}
function isInteger(text) { // only validates whole, positive, real, integers
	if(text==null || text.length==0) return false;
	var i,c;
    for(i = 0; i < text.length; i++)    {   
        c = text.charAt(i);
        if ((c < "0") || (c > "9")) return false;
    }
    return true;
}
function isSignedInteger(text) {  // whole, positive or negative, real integers
	if(text==null || text.length==0) return false;
	var sp = 0;
	if ( (text.charAt(0) == "-") || (text.charAt(0) == "+") ) sp = 1;
	return (isInteger(text.substring(sp, text.length)));
}
function isIPAddress(text) {  // is a valid IP4 address
	var IPExp = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
	return IPExp.test(text);
}
function isNumeric(text) { // any real number
	if(text==null || text.length==0) return false;
	return (!isNaN(text)); 
}
function isPhone(text) { // has at least 10 numeric characters  - requires area code and allows international
	var stripped = text.replace(/[\(\)\.\-\ A-Za-z]/g, '');
	if(stripped.length>=10) return true;
	return false;
}
function isRegex(text,regex) {  // allows testing by regular expression
	if(regex==null) return false;
	var regObj = eval("regObj = " + regex);
	return regObj.test(text);
}
function isSSN(text) {  // has exactly 9 numeric characters
	var stripped = text.replace(/[\(\)\.\-\ \/A-Za-z]/g, '');
	if(stripped.length==9) return true;
	return false;
}
function isStateAbv(text) {  // is one of the valid two state abreviatiosn
	var stateExp = /^(AK|AL|AR|AS|AZ|CA|CO|CT|DC|DE|FL|FM|GA|GU|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MH|MI|MN|MO|MP|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|PR|PW|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;
	return stateExp.test(text);
}
function isString(text) {
	if(text==null || text.length==0) return false;
	var invalidCharactersRegExp = /[^a-z\d ,?.]/i;
  	return !(invalidCharactersRegExp.test(text) );
}
function isZipCode(text) {  // is 5 digit or 10 digit zip code
	var zipCodeExp = /\d{5}(-\d{4})?/;
	if(text.length!=5 && text.length!=10) return false;
	return zipCodeExp.test(text);
}
function optionIsSelected(formField) {
	var i;
	for(i = 0; i < formField.length; i++) 	{
		if(formField[i].checked == true) return true;
	}
	return false;
}
function wordCount(text) {
	var regExp = /(\s\s\s*)/g; //find occurrences of multiple spaces
	var wordCount = 1;
	var currPos = 0;
	
	text = trim(text);
	
	text = text.replace(regExp, " "); //put them in
	
	while (text.indexOf(" ", currPos) != -1) {
		wordCount++;
		currPos = text.indexOf(" ", currPos) + 1;
	 }
	
	 // Handle a few special cases with the word count
	 if (text.charAt(text.length - 1) == " " || text.length == 0)  wordCount--;
	 return wordCount;
}

function changeSelect(selectObjId,value) {
	var selectObj = document.getElementById(selectObjId,value);
	var len = selectObj.options.length;
	for(var i=0; i<len; i++) {
		selectObj.options[i].selected = 0;
		if(selectObj.options[i].value==value) {
			selectObj.options[i].selected = 1;
		}
	}
	 
 }

// Flexible Function Names
isage=isAge;
isbirthday=isBirthday;
isbyuid=isByuId=isBYUID;
iscreditcard=isCreditCard;
isdate=isDate;
isemail=isEmail;
isfunction=isFunction;
isgpa=isGpa=isGPA;
isinteger=isdigits=isDigits=isInteger;
issignedinteger=isSignedInteger;
isip=isIP=isIp=isIPAddress;
isFloat=isfloat=isnumeric=isnumber=isNumber=isNumeric;
isphone=istelephone=isTelephone=isPhone;
isregex=isRegex;
isssn=isSsn=isSSN;
isstateabv=isStateAbv;
isstring=isString;
iszip=isZip=iszipcode=isZipCode;