var whitespace = " \t\n\r";

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)
{   
    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 checkSelect(theField, s)
{
    if (!isSelect(theField)) 
    {
       warnEmpty (theField, s);	
       return false;
    }
    else 
    {
       return true;
    }
}

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 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 isSelect(theField) {
	if (theField.options.selectedIndex == 0) {return false;}
	
	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;
}


// 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);
}


//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);
}
	
