///////////////////////////////////////////////////////

// JavaScript form validation functions script  

///////////////////////////////////////////////////////



function isBlank(Ctrl) {  // returns true if blank	

	if (Ctrl.value.length < 1)

		return true;

	else if (isEmpty(Ctrl.value))

		return true;

	else return false;

}



function isEmpty(s) { // prevents entering empty strings

	for (var i = 0; i < s.length; i++) {

		var c = s.charAt(i);

		if ((c != ' ') && (c != '\n') && (c != '\t')) return false; 

	}

	return true;

}



function isSpacey(s) { // prevents entering strings with spaces

	for (var i = 0; i < s.length; i++) {

		var c = s.charAt(i);

		if (c == ' ') return true; 

	}

	return false;

}



function isTooShort(Ctrl,num) {

	if (Ctrl.value.length < num)

		return true;

	else return false;

}



function isNotANumber(Ctrl) {  // returns true if not a number

	if (isNaN(Ctrl.value))

		return true;

	else return false;

}



function isChecked(Ctrl) {

	if (Ctrl.checked) return true;

	else return false;

}



// for multiple checkboxes with same name, true if at least one is checked

function isCheckedByLength(Ctrl) {	

	var boxIsChecked = false;

	for (var i=0; i < Ctrl.length; i++) {

		if (Ctrl[i].checked) { 

			boxIsChecked = true;

			break;

		}

	}

	if (boxIsChecked) return true;

	else return false;

}



function isSelected(Ctrl, index){  // returns true if the index indicated is selected

	if (Ctrl.options[index].selected)

		return true;

	else return false;

}



function isSelectedOrHigher(Ctrl, upLimit){ // works with numerically-valued select options only!

	var ctrlValue = Ctrl.options[Ctrl.selectedIndex].value;

	if (ctrlValue > upLimit)

		return true;

	else return false;

}



function isSelectedRange(Ctrl, loLimit, upLimit){ // works with numerically-valued select options

	var ctrlRangeValue = Ctrl.options[Ctrl.selectedIndex].value;

	if (!(ctrlRangeValue > loLimit && Ctrl.value < upLimit))

		return true;

	else return false;

}



function isSelectedValue(Ctrl, myValue){ 

	var ctrlRangeValue = Ctrl.options[Ctrl.selectedIndex].value;

	if (ctrlRangeValue == myValue)

		return true;

	else return false;

}



function isUSZipCode(Ctrl) {  // returns true if properly formatted zip code

	zipString = Ctrl.value;

	if (zipString.length == 5) {

		if (isNaN(zipString)) return false;

		else return true;

	} else if (zipString.length < 5) {

		return false;

	} else if ( 

	   zipString.length < 10 ||

	   isNaN( zipString.substring(0,5) ) || 

	   isNaN( zipString.substring(6,10) ) ||

	   (zipString.substring(5,6) != '-') 

		) {

			return false;

	} 	

	else 

		return true;

}	// end isUSZipCode()



function isEmail(Ctrl) {  // returns true if valid e-mail address

	var err=0;

	emailString = Ctrl.value;

	if (emailString.indexOf("@",1) == -1) err=1;  // need @ symbol

	if (emailString.indexOf(".",3) == -1) err=1;  // need at least one "."

	if (emailString.lastIndexOf(".") == (emailString.length-1)) err=1;  // can't end with a "."

	// check length

	if (err==0) {

		var at = (emailString.indexOf("@"))+1;

		var lastDot = (emailString.lastIndexOf("."))+1;

		// test to make sure there's at least one character between "at" and "lastDot"

		if (lastDot - at == 1) err=1;

	}

	if (err==1) return false;

	else return true;

	

}	// end testSimpleEmail()

function isValidDate(Ctrl,delim) {
   var err=0;
   var a = Ctrl.value;
   var firstSlash=(a.indexOf(delim)) + 1;
   var secondSlash=(a.lastIndexOf(delim)) + 1;   
   if (a.length != secondSlash+4) err=1;         // four-digit year
   var noFutureDate = false;
   var earliestYear = 1950;
   if (arguments.length >= 3) noFutureDate = arguments[2];
   if (arguments.length >= 4) earliestYear = arguments[3];
   if (err == 0) {
      // set date variables for testing
      var b = a.substring(0, firstSlash-1);             // month
      var c = a.substring(firstSlash-1, firstSlash);    
      var d = a.substring(firstSlash, secondSlash-1);    // day      
      var e = a.substring(secondSlash-1, secondSlash); 
      var f = a.substring(secondSlash, secondSlash+4); // year      
      if (isNaN(b)) err=1;
      if (isNaN(d)) err=1;
      if (isNaN(f)) err=1;      
      if (d.indexOf(delim) != -1) err=1;
      if (b<1 || b>12) err=1;
      if (c != delim) err=1;
      if (d<1 || d>31) err=1;
      if (e != delim) err=1;
      if (f<1800 || f>2500) err=1;  // valid year range
      if ((b==4 || b==6 || b==9 || b==11) && d==31)  err=1;
      if (b==2) {                     // February
          var g=parseInt(f/4);
          if (isNaN(g)) err=1;
          else if (d>29) err=1;
          else if (d==29) {
            /* Leap year rules: the year is a leap year if it is divisible by 4, e.g. 1960 
               UNLESS divisible by 100 - it is not a leap year, e.g. 1900
               UNLESS divisible by 400 - it is a leap year, e.g. 2000 */
            var isDivBy4   = (f %   4 == 0) ? true : false;
            var isDivBy100 = (f % 100 == 0) ? true : false;
            var isDivBy400 = (f % 400 == 0) ? true : false;
            if (!isDivBy4) err = 1;
            if (isDivBy100 && !isDivBy400) err = 1;
          } 
      }
   }
   if (noFutureDate) {
		if (new Date().getTime() < new Date(f, b-1, d-1, 23, 59, 59, 0).getTime() ) err = 1;
   }
   
   if (err==1) return false;
   return true; 
} // end isValidDate()


