var whitespace = " \t\n\r";

function DisableForm(frm, Disabled){
	objElems = frm.elements;
	for(i=0;i<objElems.length;i++){
		objElems[i].disabled = Disabled;
	}
}


function KeyDownHandler(btn)
{
	// process only the Enter key
	if (event.keyCode == 13)
	{
		// cancel the default submit
		event.returnValue=false;
		event.cancel = true;
		// submit the form by programmatically clicking the specified button
		btn.click();
	}
}

function checkEmail(theField, s)
{   
    var pos = theField.value.indexOf("@");
    var pos1 = theField.value.lastIndexOf(".");
    var len = theField.value.length - 1;

    if (!isEmpty(theField.value)){
		if ( (pos == -1) || (pos == 0) || (pos == len) ||
		     ( pos1 < pos ) || (pos1 == len))
		{
			theField.focus();
			alert(s);
			return false;
		}
	}
    return true;
}


function checkURLString(theField, s)
{   
    var pos = theField.value.indexOf(":");
    var len = theField.value.length - 1;

    if (isWhitespace(theField.value) || (pos == (len-2))) 
       return warnEmpty (theField, s);
    else return true;
}

function warnEmpty(theField, s)
{   
   	if (s != null && s != '') {
   		theField.focus();
		alert(s);
	}
    return false;
    
}

function isEmpty(s)
{   
    return ((s == null) || (s.length == 0))
}

function isWhitespace(s)
{   
var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isBlank(theField)
{
    if (isWhitespace(theField.value)) 
       return true;
    else return false;
}

function checkString(theField, s)
{
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
    else return true;
}

function checkSelector(theField, focusField, s)
{
    if (theField.value == '0' || isWhitespace(theField.value)) 
       return warnEmpty (focusField, s);
    else return true;
}

function checkSelect(theField, s)
{
    if (!isSelect(theField)) 
    {
       warnEmpty (theField, s);	
       return false;
    }
    else 
    {
       return true;
    }
}

function isSelect(theField) {
	if (theField.options.selectedIndex == 0) {return false;}
	
	return true;
}

function getSelect(theField) {
	return theField.options[theField.options.selectedIndex].text;
}

function setSelect(theField, strValue) {
	for (i = 0; i < theField.options.length; i++) {
		//alert(theField.options[i].value + " - " + strValue);
		if (theField.options[i].value == strValue) {
			theField.options.selectedIndex = i;
			return;
		}
	}
}

function checkChecked(theField, s) {
    if (!isChecked(theField)){
		if(!theField.length > 0){
			return warnEmpty (theField, s);	
		}else{
			return warnEmpty (theField[0], s);	
		}
    }else{
		return true;
    }
}

function isChecked(theField) {
	var i;
	if(!theField.length > 0){
		if (theField.checked) 
			return true;
	}else{
		for (i = 0; i < theField.length; i++) {
			if (theField[i].checked) 
				return true;
		}
	}
	return false;
}

function checkCheckedIndex(theField, Index, s) {
    if (!isCheckedIndex(theField, Index)){
		if(!theField.length > 0){
			return warnEmpty (theField, s);	
		}else{
			return warnEmpty (theField[Index], s);	
		}
    }else{
		return true;
    }
}

function isCheckedIndex(theField, Index) {
	var i;
	if(!theField.length > 0){
		if (Index == 0 && theField.checked) 
			return true;
	}else if(Index <= theField.length) {
		if (theField[Index].checked) 
			return true;
	}
	return false;
}


function checkMaxChecked(theField, MaxCount, s) {
	var i;
	var count = 0;
	
	for (i = 0; i < theField.length; i++) {
		if (theField[i].checked) count++;
	}
	if (count <= MaxCount) return true;
	
	if(!theField.length > 0){
		return warnEmpty (theField, s);	
	}else{
		return warnEmpty (theField[0], s);	
	}

}

function getCheckedValue(theField) {
	var i;
	if(!theField.length > 0){
		if (theField.checked) 
			return theField.value;
	}else{
		for (i = 0; i < theField.length; i++) {
			if (theField[i].checked) 
				return theField[i].value;
		}
	}
	return "";
}

function getCheckedIndex(theField, Value) {
	var Index, i;
	Index = -1;
	if(theField.length > 0){
		for (i = 0; i < theField.length; i++) {
			if (theField[i].value == Value) 
				Index = i;
		}
	}
	return Index;
}

function getCheckedFieldByValue(theField, Value) {
	var i;
	i = getCheckedIndex(theField, Value);
	if(i >= 0){
		return theField[i];
	}else{
		return theField;
	}
}

function toggleCheckBoxes(theControlField, theField) {
	if(isChecked(theControlField)){
		setCheckBoxes(theField, true);
	}else{
		setCheckBoxes(theField, false);
	}
}

function setCheckBoxes(theField, Checked) {
	var i;
	if(!theField.length > 0){
		theField.checked = Checked;
	}else{
		for (i = 0; i < theField.length; i++) {
			theField[i].checked = Checked;
		}
	}
}


function enforceWordCount(theField, maxlimit) {
	var x, newString, newStringCharCount;
	var char_count = theField.value.length;
	var fullStr = theField.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;

	if (word_count > maxlimit){
		newStringCharCount = 0;
		for (x = 0; x < maxlimit; x++){
			newStringCharCount = newStringCharCount + splitString[x].length + 1;
		}
		newStringCharCount = newStringCharCount + (theField.value.length - cleanedStr.length);
		theField.value = theField.value.substring(0, newStringCharCount) + " ";
	}
}

// validate Date field which may have formats like
// MM/DD/YY   MM/DD/YYYY   DD-MM-YY   DD-MM-YYYY	
function checkDate(theField, s) {
	var dateStr = theField.value;	
	if (isWhitespace(""+dateStr)) return true;
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		return warnEmpty(theField, s);
	}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
		
	if (day < 1 || day > 31) {
		return warnEmpty(theField, s);
	}
	if (month < 1 || month > 12) { // check month range
		return warnEmpty(theField, s);
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		return warnEmpty(theField, s);
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			return warnEmpty(theField, s);
		}
	}
	return true;
}

//validate a field if it's numeric value or not
function checkNumber(theField, s) {
	var fldvalue = theField.value;	
	if (isWhitespace(""+fldvalue)) return true;
	var validChars = "-.0123456789"
	var temp;
	for (var i=0; i<fldvalue.length; i++) {
		temp = "" + fldvalue.substring(i, i+1);
		if (validChars.indexOf(temp) == "-1") return warnEmpty(theField, s);
	}
	if (isNaN(parseFloat(fldvalue))) return warnEmpty(theField, s);
	return (true);
}

//validate a field to ensure it is not above the maximum value
function checkMaxNumber(theField, MaxValue, s) {
	var fldvalue = theField.value;	
	if (isWhitespace(""+fldvalue)) return true;
	if (fldvalue > MaxValue) return warnEmpty(theField, s);
	return (true);
}

//validate a field to ensure it is not below the minimum value
function checkMinNumber(theField, MinValue, s) {
	var fldvalue = theField.value;	
	if (isWhitespace(""+fldvalue)) return true;
	if (fldvalue < MinValue) return warnEmpty(theField, s);
	return (true);
}

//validate a field to ensure it is not below the minimum length
function checkMinLength(theField, MinLength, s) {
	var fldlength = theField.value.length;	
	
	if (fldlength < MinLength) return warnEmpty(theField, s);
	return (true);
}

//check if a Time field is in HH:MM:SS format
// The seconds are optional.
function checkTime(theField, s) {
	var timeStr = theField.value;	
	if (isWhitespace(""+timeStr)) return true;
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?$/;
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
	//	alert("Time is not in a valid format.");
		return warnEmpty(theField, s);
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];

	if (second=="") { second = null; }
	if (hour < 0  || hour > 12) {
	//	alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return warnEmpty(theField, s);
	}
	if (minute < 0 || minute > 59) {
	//	alert ("Minute must be between 0 and 59.");
		return warnEmpty(theField, s);
	}
	if (second != null && (second < 0 || second > 59)) {
	//	alert ("Second must be between 0 and 59.");
		return warnEmpty(theField, s);
	}
	return true;
}

//check if a Military Time field is in HH:MM:SS format
// The seconds are optional.
function checkMilitaryTime(theField, s) {
	var timeStr = theField.value;	
	if (isWhitespace(""+timeStr)) return true;
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?$/;
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
	//	alert("Time is not in a valid format.");
		return warnEmpty(theField, s);
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];

	if (second=="") { second = null; }
	if (hour < 0  || hour > 23) {
	//	alert("Hour must be between 0 and 23.");
		return warnEmpty(theField, s);
	}
	if (minute < 0 || minute > 59) {
	//	alert ("Minute must be between 0 and 59.");
		return warnEmpty(theField, s);
	}
	if (second != null && (second < 0 || second > 59)) {
	//	alert ("Second must be between 0 and 59.");
		return warnEmpty(theField, s);
	}
	return true;
}

//***************************** BROKEN ******************************** FIX IT!!!!!!!!1
//compare date1 to date2. If date1 is later than date2 return true,
//otherwise return false;
function IsDate1GTDate2(date1Str, date2Str) {
	date1 = new Date();
	date2 = new Date();
	if (isSpace(""+date2Str)) return (true);
	if (isValidDate(date2Str)) { // Validates second date 
		date2temp = new Date(date2Str);
		date2.setTime(date2temp.getTime());
	}
	else return (true); // otherwise exits
	if (isSpace(""+date1Str)) return (true);
	if (isValidDate(date1Str)) { // Validates first date 
		date1temp = new Date(date1Str);
		date1.setTime(date1temp.getTime());
	}
	else return (false) ; // otherwise exits
	if (date1.getTime() - date2.getTime() >= 0)
		 return (true);
	else
		return (false);
}
	

