// FormChek.js
// 18 Feb 97 created Eric Krock
// (c) 1997 Netscape Communications Corporation

var whitespace = "\r\n 	";

var mPrefix = "You did not enter a value into the "
var mSuffix = " field. This is a required field. Please enter it now."

var mNumericPrefix = "The value must be numeric in the "
var mNumericSuffix = " field. This is a required field. Please enter it now."

// s is an abbreviation for "string"

var sEmail = "Email"

// i is an abbreviation for "invalid"

var iEmail = "This field must be a valid email address (like me@here.com). Please reenter it now."


// p is an abbreviation for "prompt"

var pEmail = "valid email address (like foo@bar.com)."

var defaultEmptyOK = false

// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkString (theField, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value))
       return warnEmpty (theField, s);
    else return true;
}

// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnEmpty (theField, s)
{   theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}

function warnEmptyNumeric (theField, s)
{   theField.focus()
    alert(mNumericPrefix + s + mNumericSuffix)
    return false
}



// 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 defaultEmptyOK;
       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;
}



// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or
// whitespace characters only.

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;
}

// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false))
       return warnInvalid (theField, iEmail);
    else return true;
}

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}

// grabs current value of radio box
function CurrentValue(obj) {
	for (i=0;i < obj.length;i++) {
		if (obj[i].checked) {
			return obj[i].value;
		}
	}
}

// select item from radio box
function SelectValue(obj,value) {
	for (i=0;i < obj.length;i++) {
		if (obj[i].value == value){
			obj[i].checked = 1;
			break;
		}
	}
}

//Returns true or false if item in selectbox optionaly selects item
function inSelect(pValue,selectObj,SelectItem) {

	//Set existance to false
	exists = false;

	//Loop through items in passed selectbox
	for (pos = 0 ; pos <= selectObj.length - 1 ; pos++) {
		if (selectObj.options[pos].value == pValue) {
			exists = true;
			break;
		}
	}
	
	//Check to see if item should be preselected from list
	if (SelectItem == true && exists == true) {
		selectObj.options.value = pValue
	}

	//Return True or False
	return exists;
}


function isValidDate(dateStr) { 
// Checks for the following valid date formats: 
// MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY 
// Also separates date into month, day, and year variables 

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/; 
// To require a 4 digit year entry, use this line instead: 
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; 

var matchArray = dateStr.match(datePat); // is the format ok? 
if (matchArray == null) { 
alert("The event date is not in a valid format. Please re-enter. Format mm/dd/yyyy") 
return false; 
} 
month = matchArray[1]; // parse date into variables 
day = matchArray[3]; 
year = matchArray[4]; 
if (month < 1 || month > 12) { // check month range 
alert("Month must be between 1 and 12."); 
return false; 
} 
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31."); 
return false; 
} 
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!") 
return false 
} 
if (month == 2) { // check for february 29th 
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 
if (day>29 || (day==29 && !isleap)) { 
alert("February " + year + " doesn't have " + day + " days!"); 
return false; 
} 
} 
return true; // date is valid 
} 

function IsNumeric(strString,s)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.value.length == 0){
	   return warnEmptyNumeric (strString, s);
   }

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.value.length; i++)
      {
      strChar = strString.value.charAt(i);

      if (strValidChars.indexOf(strChar) == -1)
         {
		 strString.value = "";
		 return warnEmptyNumeric (strString, s);
         }
      }
   return blnResult;
   }
   
function IsRadioSelected(strString,s)
   {
	// set var radio_choice to false
	var radio_choice = false;

	// Loop from zero to the one minus the number of radio button selections
	for (counter = 0; counter < strString.length; counter++)
	{
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (strString[counter].checked)
		radio_choice = true; 
	}
	
	if (!radio_choice){
		// If there were no selections made display an alert box 
		alert("Please select a " + s + ".")
		return (false);
	}else{
		return (true);}
}