// Functions available :
// ----------------------------------------------------------------------
// -----> validateString(myfield,name)
// -----> validateRadio_Check(myfield,name)
// -----> validateDate(myfield,name)
// -----> compareDates(date1,date2) // equal=0 date1<date2=-1 date1>date2=1
// -----> validateEmail(myfield,name)
// -----> validateNum(myfield,name)
// -----> validateDropDown(myfield,name)
// -----> validateDate3(myfield_day,myfield_month,myfield_year,name)
// ----------------------------------------------------------------------

function validateDate3(myfield_day,myfield_month,myfield_year,name)
{
	if (validateDropDown(myfield_day,"*")=="" || validateDropDown(myfield_month,"*")=="" || validateDropDown(myfield_year,"*")=="") {
		myfield = myfield_day.options[myfield_day.selectedIndex].value + "/" + myfield_month.options[myfield_month.selectedIndex].value + "/" + myfield_year.options[myfield_year.selectedIndex].value;
		if (notNull(myfield)&& notBlank(myfield)&&isDate(myfield))
			return "";
		else
			return (name+"\n");
	} else {
		return "";
	}
}

function validateString(myfield,name)
{
	if (notNull(myfield.value)&& notBlank(myfield.value))
		return "";
	else
		return (name+"\n");
}

function validateRadio_Check(myfield,name)
{
	count=myfield.length;
	allValid=false;
	if (count>0) {
		for (ic=0;ic<count;ic++)
		{
			if (myfield[ic].checked)
			{	allValid=true;
				break;
			}
		}
	} else {
		if (myfield.checked) {
			allValid=true;
		}
	}

	if (!allValid)
		return (name+"\n");
	else
		return "";
}

function validateDate(myfield,name)
{
	if (notNull(myfield.value)&& notBlank(myfield.value)&&isDate(myfield.value))
		return "";
	else
		return (name+"\n");
}

function compareDates(date1,date2){

	if ((isDate(date1.value)) && (isDate(date2.value)))
	{
		vardate1 = new Date(date1.value);
	   	vardate2 = new Date(date2.value);
	  	if(vardate1.getDateString() == vardate2.getDateString())
	    		return(0);
	 	else if(vardate1.getDateString() < vardate2.getDateString())
	  		return(-1);
	  	else
	   		return(1);
	}
	else
		return (2);
}

function validateEmail(myfield,name)
{
	if (notNull(myfield.value)&& notBlank(myfield.value) && isEmail(myfield.value))
		return "";
	else
		return (name+"\n");
}

function validateNum(myfield,name)
{
	//alert("hello");
	if (notNull(myfield.value)&& notBlank(myfield.value)&&isDigits(myfield.value))
		return "";
	else
		return (name+"\n");
}



//======================================================================
// Begin String Validation
//======================================================================
function notNull(str) {
	if (str.length == 0 )
		return false
	else
		return true
}

function notBlank(str) {
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) != " ")
			return true
	}
	return false
}
//======================================================================
// End String Validation
//======================================================================

//======================================================================
// Begin : Date Validation
//======================================================================
// isYear (STRING s [, BOOLEAN emptyOK])
//
// isYear returns true if string s is a valid
// Year number.  Must be 2 or 4 digits only.
//
// For Year 2000 compliance, you are advised
// to use 4-digit year numbers everywhere.
//
// And yes, this function is not Year 10000 compliant, but
// because I am giving you 8003 years of advance notice,
// I don't feel very guilty about this ...
//
// For B.C. compliance, write your own function. ;->
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isYear (s)
{   if (isEmpty(s))
       if (isYear.arguments.length == 1)
       		return false;
       else
       		return (isYear.arguments[1] == true);

    return ((s.length == 2) || (s.length == 4));
}

// isMonth (STRING s [, BOOLEAN emptyOK])
//
// isMonth returns true if string s is a valid
// month number between 1 and 12.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isMonth (s)
{   if (isEmpty(s))
       if (isMonth.arguments.length == 1) return false;
       else return (isMonth.arguments[1] == true);
    return isInRange (s, 1, 12);
}

// isDay (STRING s [, BOOLEAN emptyOK])
//
// isDay returns true if string s is a valid
// day number between 1 and 31.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isDay (s)
{   if (isEmpty(s))
       if (isDay.arguments.length == 1) return false;
       else return (isDay.arguments[1] == true);
    return isInRange (s, 1, 31);
}

function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   }
   return this
}

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

// daysInFebruary (INTEGER year)
//
// Given integer argument year,
// returns number of days in February of that year.

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 );
}

// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day
// form a valid date.
//

function isDateCheck (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false;

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function isDate(DateStr)
{
	todaysdate = new Date();
	date  = todaysdate.getDate();
	day  = todaysdate.getDay() + 1;
	month = todaysdate.getMonth() + 1;
	yy = todaysdate.getYear();

	dateVal = DateStr
	var firstSlash = dateVal.indexOf("/")
	var lastSlash = dateVal.lastIndexOf("/")
	if (firstSlash!=0 && firstSlash == lastSlash) {
		dateVal = DateStr + "/" + yy;
		lastSlash = dateVal.lastIndexOf("/")
	}
	if (firstSlash != lastSlash)
	{
		//var month = dateVal.substring(0,firstSlash);
		//var day = dateVal.substring(firstSlash+1,lastSlash);
		var day = dateVal.substring(0,firstSlash);
		var month = dateVal.substring(firstSlash+1,lastSlash);
		var year = dateVal.substring(lastSlash+1);

		if (month.substring(0,1) == '0')
			month = month.substring(1,2);
		if (day.substring(0,1) == '0')
			day = day.substring(1,2);
		if (((day=="")||(month=="")) || (year==""))
			return false;
		else
			return(isDateCheck(year,month,day))
	}
	else
		return false;
}

Date.prototype.getDateString = getDateString;
Date.prototype.getFullYear = getFullYear;


function getDateString(){
   var dateStr;
   dateStr = "" + this.getFullYear();
   if (this.getMonth() < 9)
      dateStr += "0";
   dateStr +=  (this.getMonth() + 1);
   if (this.getDate() < 10)
      dateStr += "0";
   dateStr += this.getDate();
   return dateStr;
}

function getFullYear(){
   var year = this.getYear();
   if(year < 1000){
      year += 1900;}
   return year
}

//======================================================================
// End : Date Validation
//======================================================================

//======================================================================
// Begin : Whitespace Validation
//======================================================================
var whitespace = " \t\n\r";
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 isEmpty(s)
{   return ((s == null) || (s.length == 0))
}
//======================================================================
// End : Whitespace Validation
//======================================================================

//======================================================================
// Begin Numeric Validation
//======================================================================
function isDigits(str) {
	var i
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if (mychar < "0" || mychar > "9")
			return false
	}
	return true
}

function isNumber(str) {
	numdecs = 0
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if ((mychar >= "0" && mychar <= "9") || mychar
			== ".") {
			if (mychar == ".")
				numdecs++
		}
		else
			return false
	}
	if (numdecs > 1)
		return false
return true
}

function isInRange(str, num1, num2) {
	while (str.charAt(0)=="0") str = str.substring(1); //remove all leading zeros

	var i = parseInt(str) //parseInt won't work correctly for number with leading zero(s)
	return((i >= num1) && (i <= num2))

}

function isMoney(str)
{
	var tempStr;

	tempStr = str;

	tempStr = stripChars(tempStr, '$');

	return isNumber(tempStr);
}
//======================================================================
// End Numeric Validation
//======================================================================

//======================================================================
// Begin : Email Validation
//======================================================================
// isEmail (STRING s [, BOOLEAN emptyOK])
//
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   if (isEmpty(s))
       if (isEmail.arguments.length == 1) return false;
       else return (isEmail.arguments[1] == true);

    // is s whitespace?
    if (isWhitespace(s)) return false;

    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}
//======================================================================
// End : Email Validation
//======================================================================

function validateDropDown(myfield,name)
{
	if ((myfield.options[myfield.selectedIndex].value) == '')
		return (name+"\n");
	else
		return "";
}