/**
 * Color the Results with alternating Color Bands
 */
function colorResults () {
  var shade = true;
  var resultsRoot = document.getElementById("results");
  if(resultsRoot) {
    var resultsChild = resultsRoot.firstChild;
    var resultsLastChild = resultsRoot.lastChild;
    while(resultsChild != resultsLastChild) {
      if(resultsChild.nodeType == 1) {
        if(shade) {
          resultsChild.style.backgroundColor = "#f8f8f8";
        }
        shade = !shade;
      }
      resultsChild = resultsChild.nextSibling;
    }
  }
} //colorResults

/**
 * Confirm with the user that they actually want the form reset
 *
 * @param form The Form to be Reset
 */
function yesReset (form) {
  if(confirm("You are about to reset the form and lose all the information you have entered.\n Click OK to Continue or Cancel")) {
    form.reset();
  }
} //yesReset

 /**
  * If the supplied condition is not true, alert the user with the supplied
  *  message and give the supplied field the focus.
  *
  * @param condition The Assertion Condition
  * @param msg The Message to Alert if <code>condition</code> is false
  * @param fld The Field to give focus if <code>condition</code> is false
  * @return The <code>condition</code>
  */
 function assert (condition,msg,fld) {
   if(!condition) {
     alert(msg);
     if(fld != null) {
       fld.focus();
     }
   }
   return condition;
 } //assert

/**
 * Format the Phone Number
 *
 * @param phone The Phone Number Field
 * @return true if properly formatted
 */
function fixPhone (phone) {
  var phoneVal = phone.value;
  phoneVal = phoneVal.replace(/\D/g,""); // Replace all non-digits
  if(phoneVal.charAt(0) == 1) {
    phoneVal = phoneVal.substring(1);
  }
  if(phoneVal.length == 10) {
    phone.value = phoneVal.replace(/(\d\d\d)(\d\d\d)(\d\d\d\d)/,"$1-$2-$3");
    return true;
  } else {
    return false;
  }
} //fixPhone

/**
 * Format the Zip Code
 *
 * @param zip Zip Code Field
 * @return true if properly formatted
 */
function fixZip(zip) {
  var zipVal = zip.value.replace(/\D/g,""); // Replace all non-digits
  return (zipVal.length == 5);
} //fixZip

/**
 * Confirm the format of the E-mail Address
 *
 * @param emailStr The Email Address String
 * @return true if properly formatted
 */
function emailCheck (emailStr) {
  var emailPat = /^(.+)@(.+)$/;
  var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
  var validChars = "\[^\\s" + specialChars + "\]";
  var quotedUser = "(\"[^\"]*\")";
  var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
  var atom = validChars + '+';
  var word = "(" + atom + "|" + quotedUser + ")";
  var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
  var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
  var matchArray = emailStr.match(emailPat);
  if(matchArray == null) {
    alert("Email address seems incorrect (check @ and .'s)");
    return false;
  }
  var user = matchArray[1];
  var domain=matchArray[2];
  if(user.match(userPat) == null) {
    alert("The username doesn't seem to be valid.");
    return false;
  }
  var IPArray = domain.match(ipDomainPat);
  if(IPArray != null) {
    for(var i = 1;i <= 4;i++) {
      if(IPArray[i] > 255) {
        alert("Destination IP address is invalid!");
        return false;
      }
    }
    return true;
  }
  var domainArray = domain.match(domainPat);
  if(domainArray == null) {
    alert("The domain name doesn't seem to be valid.");
    return false;
  }
  var atomPat = new RegExp(atom,"g");
  var domArr = domain.match(atomPat);
  var len = domArr.length;
  if((domArr[domArr.length-1] != "info") &&
     (domArr[domArr.length-1] != "name") &&
     (domArr[domArr.length-1] != "arpa") &&
     (domArr[domArr.length-1] != "coop") &&
     (domArr[domArr.length-1] != "aero")) {
    if((domArr[domArr.length-1].length < 2) || (domArr[domArr.length-1].length > 3)) {
      alert("The address must end in a three-letter domain, or two letter country.");
      return false;
    }
  }
  if(len < 2) {
    alert("This address is missing a hostname!");
    return false;
  }
  return true;
} //emailCheck

/**
 * Return the value of the radio button that is checked
 * Return an empty string if none are checked, or
 *  there are no radio buttons
 *
 * @param radio The Radio Button or Group
 * @return Checked Value
 */
function getCheckedValue (radio) {
  if(radio) {
    var len = radio.length;
    if(len == undefined) {
      return (radio.checked) ? radio.value : "";
    } else {
      for(var i = 0;i < len;i++) {
        if(radio[i].checked) {
          return radio[i].value;
        }
      }
    }
  }
  return "";
} //getCheckedValue

/**
 * Set the radio button with the given value as being checked
 *  do nothing if there are no radio buttons
 *  if the given value does not exist, all the radio buttons
 *  are reset to unchecked
 *
 * @param radio The Radio Button or Group
 * @param value The value of the Radio Button to check
 */
function setCheckedValue (radio,value) {
  if(radio) {
    var len = radio.length;
    if(len == undefined) {
      radio.checked = (radio.value == value.toString());
    } else {
      for(var i = 0;i < len;i++) {
        radio[i].checked = (radio[i].value == value.toString());
      }
    }
  }
} //setCheckedValue

function validDate(dt){

if (!inValidFormat(dt))
{
   return false;
}

removeAllSpace(dt)
var nTokens=0
var tokens=new Array("","","")
var inDelim=false
for (var i=0;i<dt.length;i++){
 c=dt.charAt(i)
 if (c>='0' && c<='9'){
  tokens[nTokens]+=c
  inDelim = false
  }
 else if (!inDelim){
  nTokens++
  inDelim=true;
  }
 if (nTokens == 3){
  return false
  }
 }
if (nTokens==0){
 if (tokens[0].length == 6 || tokens[0].length == 8){
  month=tokens[0].substring(0,2)
  day=tokens[0].substring(2,4)
  year=tokens[0].substring(4,tokens[0].length)
  }
 else{
  return false
  }
 }
else if (nTokens==2){
 month=tokens[0]
 day=tokens[1]
 year=tokens[2]
 }
else{
 return false
 }
iMonth=parseInt( trimLeadingZeroes( month ) )
iDay=parseInt( trimLeadingZeroes( day ) )
iYear=parseInt( trimLeadingZeroes( year ) )
/**
// Y2K check
if (iYear>=0 && iYear<=49)
 iYear += 2000
if (iYear>=50 && iYear <=99)
 iYear += 1900
 */
 dt=(iMonth<10?'0':'')+iMonth+'/'+(iDay<10?'0':'')+iDay+'/'+iYear
 if (iMonth<1 || iMonth>12){
  return false
  }

if (iYear<1900 || iYear>2049){
 return false
 }
if (iDay<1 || iDay>numDaysIn(iMonth,iYear)){
 return false
 }
return true
}


function removeAllSpace(s){
var temp = ""
var c
for (var i=0;i<s.length;i++){
 c = s.charAt(i)
 if (c > ' ')
  temp += c
  }
s = temp
return s
}

function numDaysIn(mth,yr){
if (mth==4 || mth==6 || mth==9 || mth==11)
 return 30
else if ((mth==2) && isLeapYear(yr))
 return 29
else if (mth==2)
 return 28
else return 31
}

function isLeapYear(i){
return ((i%4==0 && i%100!=0) || i%400==0)
}

function trimLeadingZeroes(s){
var iStart = 0
var iEnd = s.length - 1
while( iStart<=iEnd && s.charAt(iStart)=='0' )
 iStart++
 if (iStart>iEnd)
  s = "0"
 else
  s = s.substring(iStart, iEnd+1)
  return s
 }
 
 function compareDates(dt1,dt2){ // accept as edited string in the format mm/dd/ccyy
 mm1=dt1.split("/")[0]*1
 dd1=dt1.split("/")[1]*1
 ccyy1=dt1.split("/")[2]*1
 mm2=dt2.split("/")[0]*1
 dd2=dt2.split("/")[1]*1
 ccyy2=dt2.split("/")[2]*1
 
 if (ccyy2 < ccyy1)
   return(-1)
 else if (ccyy2 > ccyy1)
   return(1)
 else if (mm2 < mm1)
   return(-1)
 else if (mm2 > mm1)
   return(1)
 else if (dd2 < dd1)
   return(-1)
 else if (dd2 > dd1)
   return(1)
 return(0)
}

 function inValidFormat(dt)
 { 
   var delim1 = dt.charAt(2);
   var delim2 = dt.charAt(5);
   return (delim1=='/' && delim2=='/');
 }