// VERIFY STRING IS EMPTY //
function isEmpty(str) {
	if (str.length == 0) {
		return true;
	} else {
		return false;
	}
}

// VERIFY VALUE IS A VALID INTEGER //
function isInteger(str){
	var i;
	
    for (i = 0; i < str.length; i++) {   

        // CHECK THAT CURRENT CHARACTER IS NUMBER. //
        var c = str.charAt(i);
        if (((c < "0") || (c > "9"))) {
			return false;
		}

    }
	
    // IF ALL CHARACTERS ARE NUMBERS //
    return true;
}

// VERIFY VALUE IS A VALID NUMBER //
function isNumeric(str) {
	Regex = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
	if (!str.match(Regex)) {
		return false;
	} else {
		return true;
	}
}

function isPhone(str) {
	Regex = /^\d\d\d-\d\d\d-\d\d\d\d$/;
	if (!str.match(Regex)) {
		return false;
	} else {
		return true;
	}
}

// VERIFY VALUE IS A VALID ALPHANUMERIC STRING //
function isAlphaNumeric(str, spaces_allowed)
{

	if (spaces_allowed == 1) {
		var character_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
	} else {
		var character_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
	}
	
	var string_valid = true;
	for (x = 0; x < str.length; x++) {
		current_character = str.charAt(x);
		if (character_list.indexOf(current_character) == -1) {
			alert(current_character);
			string_valid = false;
		}
	}
	
	return string_valid;

}

// VERIFY VALUE IS A VALID EMAIL ADDRESS //
function isEmail(str) {
	Regex = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/;
	if(str.match(Regex)){
		return true;
	} else {
		return false;
	}
}

// VERIFY VALUE IS A VALID URL //
function isURL(str) {
	Regex = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
	if(str.match(Regex)){
		return true;
	} else {
		return false;
	}
}

// VERIFY VALUE IS A VALID DATE //
function isDate(dtStr, minYear, maxYear){

	var dtCh = "/";
	if (minYear == '' || minYear == 0) {
		var minYear = 1900;
	}

	if (maxYear == '' || maxYear == 0) {
		var maxYear = 1900;
	}

	var daysInMonth	= DaysArray(12);
	var pos1		= dtStr.indexOf(dtCh);
	var pos2		= dtStr.indexOf(dtCh, pos1+1);
	var strMonth	= dtStr.substring(0, pos1);
	var strDay		= dtStr.substring(pos1+1, pos2);
	var strYear		= dtStr.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;
	} else if (strMonth.length < 1 || month < 1 || month > 12) {
		return false;
	} else if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
		return false;
	} else if (strYear.length != 4 || year==0 || year < minYear || year > maxYear) {
		return false;
	} else if (dtStr.indexOf(dtCh,pos2+1) != -1 || isInteger(stripInvalidChars(dtStr, dtCh)) == false) {
		return false;
	} else {
		return true;
	}
}

// BEGIN isDate SUPPORTING FUNCTIONS //
function stripInvalidChars(str, invalid_chars) {

	var i;
    var returnString = '';
	
    // LOOP THROUGH STRING'S CHARACTERS ONE BY ONE //
    // IF CHARACTER IS NOT IN BAG, APPEND TO RETURNSTRING //
    for (i = 0; i < str.length; i++) {   
        var character = str.charAt(i);
        if (invalid_chars.indexOf(character) == -1) {
			returnString += character;
		}
    }
	
    return returnString;
	
}

function daysInFebruary (year) {
	// DETERMINE THE NUMBER OF DAYS IN FEBRUARY //
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

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;
}
// END isDate SUPPORTING FUNCTIONS //

// LIMIT THE LENGTH OF A TEXTAREA FIELD //
function limitLength(str, maxLength) {
	if (str.length < maxLength) {
		return true;
	} else {
		return false;
	}
}

//POPUP ADMIN HELP WINDOW //
function openHelpWindow(section) {
	window.open("/admin/help.php?section=" + section, "helpWindow","location=1,status=1,scrollbars=1,width=600,height=400");
}

// AJAX OBJECT FUNCTION //
function GetXmlHttpObject() {

	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}

	if (window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
  	}
	
	return null;
	
}

