///////////////////////////////////////////////////////
// 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;
   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(Ctrl) { // true if string has spaces, return, or tab
    var s = Ctrl.value;
   for (var i = 0; i < s.length; i++) { var c = s.charAt(i);
      if ((c == ' ') || (c == '\n') || (c == '\t')) return true; }
   return false; }
function isTooShort(Ctrl,num) {
   if (Ctrl.value.length < num) return true;
   return false; }
function isTooLong(Ctrl,num) {
   if (Ctrl.value.length > num) return true;
   return false; }
function isIndexOf(Ctrl,str) { // returns true if substring found in string
   if (Ctrl.value.indexOf(str) > -1) return true;
   return false; }
function isNotANumber(Ctrl) {  // returns true if not a number
   if (isNaN(Ctrl.value)) return true;
   return false; }
function isChecked(Ctrl) {
   if (Ctrl.checked) return true;
   return false; }
function isCheckedByLength(Ctrl) {   // for multiple checkboxes with same name
   var boxIsChecked = false;
   for (i=0; i < Ctrl.length; i++) { if (Ctrl[i].checked) { boxIsChecked = true; break; }}
   return boxIsChecked; }
function isSelected(Ctrl, index){  // returns true if the index indicated is selected
   if (Ctrl.options[index].selected) return true;
   return false; }
function isAnySelected(Ctrl){  // returns true if any index indicated is selected
	 for (var iter = 0; iter < Ctrl.options.length; iter++){
	 	if (isSelected(Ctrl, iter)) return true;
	 }
	 return false; }
function isSelectedOrHigher(Ctrl, upLimit){ // works with numerically-valued select options only!
   var ctrlValue = Ctrl.options[Ctrl.selectedIndex].value;
   if (ctrlValue == 0) return true;
   else if (ctrlValue > upLimit) return true;
   return false; }
function isSelectedRange(Ctrl, loLimit, upLimit){ // works with select options
   if ((Ctrl.selectedIndex >= loLimit && Ctrl.selectedIndex < upLimit)) return true;
   return false; }
function checkZip(Ctrl) {  // returns true if *not* properly formatted zip code
   zipString = Ctrl.value;
   if (zipString.length == 5) {
      if (isNaN(zipString)) return true;
   } else if (zipString.length < 5) {
      return true;
   } else if ( 
      zipString.length < 10 ||
      isNaN( zipString.substring(0,5) ) || 
      isNaN( zipString.substring(6,10) ) ||
      (zipString.substring(5,6) != '-') ) {
         return true;
   } 
   return false;
}   // end checkZip()
function checkUSPhone(Ctrl) {  // returns true if properly formatted US phone number - 10 digits
	var s = onlyNumbers(Ctrl.value);
	return (s.length == 10);
}   // end checkUSPhone()

function testSimpleEmail(Ctrl){  // returns true if invalid email
   var err=0;
   emailString = Ctrl.value;
   if (window.RegExp) {
      var regexEmail = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/;
      return !regexEmail.test(emailString);
   } else {
      if (emailString.indexOf("@",1) == -1) err=1;  // need @ symbol
      if (emailString.indexOf("@",1) != emailString.lastIndexOf("@")) err=1;  // only one @ 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 true;
   return false;
}   // end testSimpleEmail()

function isCCNumber(Ctrl) {
   var cnum = onlyNumbers(Ctrl.value);
   var lgth = cnum.length;

   if (lgth > 19) return false;
   else if (lgth < 15) return false;
   else {
      // validate number
      tally = 0; multTemp = 0; 
      lengthCheck = lgth % 2;
      if (lengthCheck == 0) { counter = 2; } else { counter = 1; }
      
      for (i = 0; i < lgth; i++) {
         tempNumb = parseInt(cnum.charAt(i));
         multTemp = tempNumb * counter;
         if (multTemp >= 10) {
            multTemp2 = parseInt(multTemp.charAt(0)) + parseInt(multTemp.charAt(multTemp.length));
            multTemp = multTemp2;
         }
         tally += multTemp; multTemp = 0;
         if (counter == 1) counter = 2; else counter = 1;
      }
      if (tally % 10 != 0) return false;
   } 
   return true;
}     
function onlyNumbers(str) {
	// clean number
	if (window.RegExp) return str.replace(/[^0-9]*/g,"");
	else {
		var a = '';  var allnum =  '1234567890';
		for (var i = 0; i < str.length; i++) {
			var c = str.charAt(i);
			if (allnum.indexOf(c) > -1) a += c;
		}
	}
	return a;
}

function checkDateWSlash(Ctrl) {
   var err=0;
   a = Ctrl.value;
   var firstSlash=(a.indexOf("/")) + 1;
   var secondSlash=(a.lastIndexOf("/")) + 1;   
   if (a.length != secondSlash+4) err=1;         // four-digit year
   if (err == 0) {
      // set date variables for testing
      b = a.substring(0, firstSlash-1);             // month
      c = a.substring(firstSlash-1, firstSlash);    
      d = a.substring(firstSlash, secondSlash-1);    // day      
      e = a.substring(secondSlash-1, secondSlash); 
      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("/") != -1) err=1;
      if (b<1 || b>12) err=1;
      if (c != '/') err=1;
      if (d<1 || d>31) err=1;
      if (e != '/') 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 (err==1) return true;
   return false; 
} // end checkDateWSlash()

function moveItemOver(srcList, destList, moveAll ) {
	//var newDestList; //holder for updated Destination List
	
	// Do nothing if nothing is selected
  if (  ( srcList.selectedIndex == -1 ) && ( moveAll == false )   )  {    return;  }
	
	
	 for( var i = 0; i < srcList.options.length; i++ ) { 
     if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) ){
       // if selected, incorporate into destination list
       destList.options[destList.options.length ] = new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected );
     	
	 		 // remove from source list
			 srcList.removeChild(srcList.options[i]) ;
			 
			 //since item at index was deleted move index back
			 i--;
		 }
   }	
	 
}

function moveOptionsUp(selectList) {
 var selectOptions = selectList.getElementsByTagName('option');
 for (var i = 1; i < selectOptions.length; i++) {
  var opt = selectOptions[i];
  if (opt.selected) {
   selectList.removeChild(opt);
   selectList.insertBefore(opt, selectOptions[i - 1]);
     }
    }
}


function moveOptionsDown(selectList) {
 var selectOptions = selectList.getElementsByTagName('option');
 for (var i = selectOptions.length - 2; i >= 0; i--) {
  var opt = selectOptions[i];
  if (opt.selected) {
   var nextOpt = selectOptions[i + 1];
   opt = selectList.removeChild(opt);
   nextOpt = selectList.replaceChild(opt, nextOpt);
   selectList.insertBefore(nextOpt, opt);
     }
    }
}

function copyAllSelected(srcList, destList) {
		// clear destination list
		while (destList.childNodes[0]) {
			destList.removeChild(destList.childNodes[0]);
		}
		
		for(iter = 0; iter < srcList.options.length; iter++) {
			destList.options[iter] = new Option( srcList.options[iter].text, srcList.options[iter].value);
			destList.options[iter].selected = true;
		}
}

function errorAlert(e) {
    // If ErrorString "e" has content, there was at least one error; let them know.
   if (e.length > 0) {
      msg  = "____________________________________________________\n\n";
      msg += "  The form was not submitted for the following reason(s): \n";
      msg += "____________________________________________________\n";
      alert(msg + e);
      return false;
   } 
   return true;
} // end errorAlert()

function displayErrors(ErrorString, errorspace) {
	if (ErrorString.length) {
		try { //easy way
			document.getElementById(errorspace).innerHTML = ErrorString;
		}
		catch(e) { //innerHTML is not standard
			try{ //hard way 
				var ErrMessages = ErrorString.split("<br />\n");
				for(i = 0; i < ErrMessages.length ; i++) {
					document.getElementById(errorspace).appendChild(document.createTextNode(ErrMessages[i]));
					document.getElementById(errorspace).appendChild(document.createElement('br'));
				}
			}
			catch(e){ //all else fails
				alert(ErrorString.replace(/<br \/>/g,''));
			}
		}
		try {
			window.location.hash = "top";
		} 
		catch(e) {}
		return false;
	}
	else {
		return true; 
	}
}