function AddressValidate(field) {
   var valid = "#&"
   var ok = "yes";
   var temp;
   for (var i=0; i<field.length; i++) {
      temp = "" + field.substring(i, i+1);
      if (valid.indexOf(temp) != "-1") {
         ok = "no";
         alert("Invalid character. Position: "+temp);
      }
   }
   if (ok == "no") {
      return false; 
   }
   return true;
}

function AlphaValidation(field) {
   var valid = "#&"
   var ok = "yes";
   var temp;
   for (var i=0; i<field.length; i++) {
      temp = "" + field.substring(i, i+1);
      if (valid.indexOf(temp) != "-1") {
         ok = "no";
         alert("Invalid character. Position: "+temp);
      }
   }
   if (ok == "no") {
      return false; 
   }
   return true;
}

function ChangeCase(frmObj) {
   var index;
   var tmpStr;
   var tmpChar;
   var preString;
   var postString;
   var strlen;
   tmpStr = frmObj.value.toLowerCase();
   strLen = tmpStr.length;
   if (strLen > 0)  {
      for (index = 0; index < strLen; index++)  {
         if (index == 0)  {
            tmpChar = tmpStr.substring(0,1).toUpperCase();
            postString = tmpStr.substring(1,strLen);
            tmpStr = tmpChar + postString;
         }
         else {
            tmpChar = tmpStr.substring(index, index+1);
            if (tmpChar == " " && index < (strLen-1))  {
               tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
               preString = tmpStr.substring(0, index+1);
               postString = tmpStr.substring(index+2,strLen);
               tmpStr = preString + tmpChar + postString;
            }
         }
      }
   }
   frmObj.value = tmpStr;
}

function CheckCreditCard(object_value)
    {
   var white_space = " -";
   var creditcard_string="";
   var check_char;


    if (object_value.length == 0)
        return true;

   // squish out the white space
   for (var i = 0; i < object_value.length; i++)
   {
      check_char = white_space.indexOf(object_value.charAt(i))
      if (check_char < 0)
         creditcard_string += object_value.substring(i, (i + 1));
   }   

   // if all white space return error
    if (creditcard_string.length == 0)
        return false;
    
       
   // make sure number is a valid integer
   if (creditcard_string.charAt(0) == "+")
        return false;

   if (!CcheckInteger(creditcard_string))
      return false;

    // now check mod10

   var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
   var checkdigit = 0;
   var tempdigit;

   for (var i = 0; i < creditcard_string.length; i++)
   {
      tempdigit = eval(creditcard_string.charAt(i))

      if (doubledigit)
      {
         tempdigit *= 2;
         checkdigit += (tempdigit % 10);

         if ((tempdigit / 10) >= 1.0)
         {
            checkdigit++;
         }

         doubledigit = false;
      }
      else
      {
         checkdigit += tempdigit;
         doubledigit = true;
      }
   }   
   return (checkdigit % 10) == 0 ? true : false;
}

// The following date formats are accepted:
// mm-dd-yyyy, mm/dd/yyyy, mm.dd.yyyy, mm dd yyyy, 
// mmm dd yyyy, mmddyyyy, m-d-yyyy, m/d/yyyy, m.d.yyyy, 
// m d yyyy, mmm d yyyy, m-d-yy, m/d/yy, m.d.yy, m d yy, 
// mmm d yy (yy is 20yy) 

function CheckDate(objName) {
   var datefield = objName;
   if (chkdate(objName) == false) {
      datefield.select();
      alert("That date is invalid.  Please try again.");
      datefield.focus();
      return false;
   }
   return true;
}

function chkdate(objName) {
   var strDatestyle = "US"; //United States date style
   //var strDatestyle = "EU";  //European date style
   var strDate;
   var strDateArray;
   var strDay;
   var strMonth;
   var strYear;
   var intday;
   var intMonth;
   var intYear;
   var booFound = false;
   var datefield = objName;
   var strSeparatorArray = new Array("-"," ","/",".");
   var intElementNr;
   var err = 0;
   var strMonthArray = new Array(12);
   strMonthArray[0] = "Jan";
   strMonthArray[1] = "Feb";
   strMonthArray[2] = "Mar";
   strMonthArray[3] = "Apr";
   strMonthArray[4] = "May";
   strMonthArray[5] = "Jun";
   strMonthArray[6] = "Jul";
   strMonthArray[7] = "Aug";
   strMonthArray[8] = "Sep";
   strMonthArray[9] = "Oct";
   strMonthArray[10] = "Nov";
   strMonthArray[11] = "Dec";
   strDate = datefield.value;
   if (strDate.length < 1) {
      return true;
   }
   for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
      if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
         strDateArray = strDate.split(strSeparatorArray[intElementNr]);
         if (strDateArray.length != 3) {
            err = 1;
            return false;
         }
         else {
            strDay = strDateArray[0];
            strMonth = strDateArray[1];
            strYear = strDateArray[2];
         }
         booFound = true;
      }
   }
   if (booFound == false) {
      if (strDate.length>5) {
         strDay = strDate.substr(0, 2);
         strMonth = strDate.substr(2, 2);
         strYear = strDate.substr(4);
      }
      else {
         return false;
      }
   }
   if (strYear.length == 2) {
      if (strYear < 20) {
         strYear = '20' + strYear;
      }
      else {
         strYear = '19' + strYear;
      }
   }
   if (strYear.length == 1) {
      if (strYear < 20) {
         strYear = '200' + strYear;
      }
      else {
         strYear = '190' + strYear;
      }
   }
   // US style
   if (strDatestyle == "US") {
      strTemp = strDay;
      strDay = strMonth;
      strMonth = strTemp;
   }
   intday = parseInt(strDay, 10);
   if (isNaN(intday)) {
      err = 2;
      return false;
   }
   intMonth = parseInt(strMonth, 10);
   if (isNaN(intMonth)) {
      for (i = 0;i<12;i++) {
         if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
            intMonth = i+1;
            strMonth = strMonthArray[i];
            i = 12;
         }
      }
      if (isNaN(intMonth)) {
         err = 3;
         return false;
      }
   }
   intYear = parseInt(strYear, 10);
   if (isNaN(intYear)) {
      err = 4;
      return false;
   }
   if (intMonth>12 || intMonth<1) {
      err = 5;
      return false;
   }
   if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
      err = 6;
      return false;
   }
   if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
      err = 7;
      return false;
   }
   if (intMonth == 2) {
      if (intday < 1) {
         err = 8;
         return false;
      }
      if (LeapYear(intYear) == true) {
         if (intday > 29) {
            err = 9;
            return false;
         }
      }
      else {
         if (intday > 28) {
            err = 10;
            return false;
         }
      }
   }
   datefield.value = intMonth + "/" + intday + "/" + strYear;
   //    if (strDatestyle == "US") {
   //       datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
   //    }
   //    else {
   //       datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
   //    }
   return true;
}

function LeapYear(intYear) {
   if (intYear % 100 == 0) {
      if (intYear % 400 == 0) { return true; }
   }
   else {
      if ((intYear % 4) == 0) { return true; }
   }
   return false;
}

function doDateCheck(to) {
   if (Date.parse("doDate") > Date.parse(to)) {
      return false;
   }
   else {
      return true;
   }
}

function doDateCheckCurrent(to) {
   if (Date.parse(new Date(-1)) > Date.parse(to)) {
      return false;
   }
   else {
      return true;
   }
}

function doDateCheckAppointment(to) {
   if (Date.parse("doDateApt") > Date.parse(to)) {
      return false;
   }
   else {
      return true;
   }
}

function doDateCheckEO(to) {
   if (Date.parse("doDateEO") > Date.parse(to)) {
      return false;
   }
   else {
      return true;
   }
}

function CheckEmail (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].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) {
      var errStr="This address is missing a hostname!"
      alert(errStr)
      return false
   }

   return true;
}

function CheckInteger(object_value) {
   if (object_value.length == 0)
      return true;
   var decimal_format = ".";
   var check_char;
   
   check_char = object_value.indexOf(decimal_format)
   if (check_char < 1)
      return CheckNumber(object_value);
   else
      return false;
}

function CheckNumber(object_value) {
   if (object_value.length == 0) return true;
   var start_format = " .+-0123456789";
   var number_format = " .0123456789";
   var check_char;
   var decimal = false;
   var trailing_blank = false;
   var digits = false;
   check_char = start_format.indexOf(object_value.charAt(0))
   if (check_char == 1) decimal = true;
   else if (check_char < 1) return false;
   
   for (var i = 1; i < object_value.length; i++) {
      check_char = number_format.indexOf(object_value.charAt(i))
      if (check_char < 0) return false;
      else if (check_char == 1) {
         if (decimal) return false;
         else decimal = true;
      }
      else if (check_char == 0) {
         if (decimal || digits) trailing_blank = true;
      }
 else if (trailing_blank) return false;
      else digits = true;
      }   
   return true
}


function NumberRange(object_value, min_value, max_value) {
   if (min_value != null) {
      if (object_value < min_value)
         return false;
   }
   if (max_value != null) {
      if (object_value > max_value)
         return false;
   }
   return true;
}
function CheckPhone(object_value) {
   if (object_value.length == 0) return true;
   if (object_value.length == 12) {
      if (!CheckNumber(object_value.substring(0,3))) return false;
      else
         if (!NumberRange((eval(object_value.substring(0,3))), 100, 1000)) return false;
      if (object_value.charAt(3) != "-" && object_value.charAt(3) != " ") return false
      if (!CheckNumber(object_value.substring(4,7))) return false;
      else
         if (!NumberRange((eval(object_value.substring(4,7))), 100, 1000)) return false;
      if (object_value.charAt(7) != "-" && object_value.charAt(7) != " ") return false;
      if (object_value.charAt(8) == "-" || object_value.charAt(8) == "+") return false;
         else return (CheckInteger(object_value.substring(8,12)));
   }
}

function CheckPhoneShort(object_value) {
   if (object_value.length == 8) {
      if (!CheckNumber(object_value.substring(0,3))) return false;
      else
         if (!NumberRange((eval(object_value.substring(0,3))), 100, 1000)) return false;
      if (object_value.charAt(3) != "-" && object_value.charAt(3) != " ") return false
      if (!CheckNumber(object_value.substring(4,7))) return false;
      else
         if (!NumberRange((eval(object_value.substring(4,7))), 100, 1000)) return false;
      return true;
   }
}
   
function CheckRange(object_value, min_value, max_value) {
   if (object_value.length == 0)
      return true;
   if (!CheckNumber(object_value)) {
      return false;
   }
   else {
      return (NumberRange((eval(object_value)), min_value, max_value));
   }
   return true;
}

function Keyboard() {
   if (event.keyCode == 35 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 34)
      event.returnValue = false;
}

SSNArray = new Array("111-11-1111","222-22-2222","333-33-3333","444-44-4444","555-55-5555","666-66-6666","777-77-7777","888-88-8888","000-00-0000","123-45-6789")

function SSNCheck(ssn) {
   for (var i=0; i <= SSNArray.length; i++) {
      if (ssn == SSNArray[i]){
         alert("Social Security number "+SSNArray[i]+" is an invalid choice. Enter 999-99-9999 if the customer will not give out their SSN.");
         return false;
         }
      }
      return true;
}

function CheckSSN(object_value) {
   var white_space = " -+.";
   var ssc_string="";
   var check_char;
   if (object_value.length != 11)
      return false;
   if (object_value.charAt(3) != "-")
      return false;
   if (object_value.charAt(6) != "-")
      return false;
   for (var i = 0; i < object_value.length; i++) {
      check_char = white_space.indexOf(object_value.charAt(i))
      if (check_char < 0)
         ssc_string += object_value.substring(i, (i + 1));
   }   
   if (ssc_string.length != 9)
      return false;
   if (!CheckInteger(ssc_string))
      return false;
   return true;
}

StateArray = new Array("AK","AL","AR","AZ","CA","CO","CT","DE","FL","GA","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VA","VT","WA","WI","WV","WY")

function CheckState(field) {
   for (var i=0; i <= StateArray.length; i++) {
      if (field == StateArray[i]){
         alert("State "+StateArray[i]+" is an invalid entry.");
         return false;
         }
      }
      return true;
}

function CheckZip(object_value) {
   if (object_value.length == 0) return true;
   if (object_value.length != 5 && object_value.length != 10) return false;
   if (object_value.charAt(0) == "-" || object_value.charAt(0) == "+") return false;
   if (!CheckInteger(object_value.substring(0,5))) return false;
   if (object_value.length == 5) return true;
   if (object_value.charAt(5) != "-" && object_value.charAt(5) != " ") return false;
   if (object_value.charAt(6) == "-" || object_value.charAt(6) == "+") return false;
   return (CheckInteger(object_value.substring(6,10)));
}

function UncheckPrimary(index) {
   for (var j = 1; j <= 3; j++) {
   box = eval("Form.Primary" + j); 
   if(index != j)
      box.checked = 0;
   }
} 


