/* make sure to put the attribute in your form tag:   onSubmit="return checkrequired(this)" */
var whitespace = " \t\n\r";

function isWhitespace (s) {   var i;
  for (i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if (whitespace.indexOf(c) == -1) return false;
  }
  return true;
}

function checkVendorForm(f) {

  var pass=true;
  var temp2 = "";
 
  if (f.companyname.value == '' || isWhitespace(f.companyname.value)) {
    temp2 = "Company Name";
    f.companyname.focus();
    pass=false;
  }


  if (pass && (f.login.value == '' || isWhitespace(f.login.value))) {
    temp2 = "Login ID";
    f.login.focus();
    pass=false;
  }


  if (pass && (f.taxno.value == '' || isWhitespace(f.taxno.value))) {
    temp2 = "Federal Tax #";
    f.taxno.focus();
    pass=false;
  }

  if (pass && (f.password.value == '' || isWhitespace(f.password.value))) {
    temp2 = "Password";
    f.password.focus(); 
    pass=false;
  }

  if (pass && (f.confirmpassword.value == '' || isWhitespace(f.confirmpassword.value))) {
    temp2 = "Confirm Password";
    f.confirmpassword.focus();
    pass=false;
  }
  
  if (pass && (f.confirmpassword.value != f.password.value)) {
    alert("Please be sure the Password and Confirm Password fields match.")
    f.password.focus();
    return false;
  }
  
  if (pass && (f.puraddress1.value == '' || isWhitespace(f.puraddress1.value))) {
    temp2 = "Purchasing Address: Address 1";
    f.puraddress1.focus();
    pass=false;
  }

  if (pass && (f.purphoneno.value == '' || isWhitespace(f.purphoneno.value))) {
    temp2 = "Purchasing Address: Phone Number";
    f.purphoneno.focus();
    pass=false;
  }

/*
  if (pass && (f.purfaxno.value == '' || isWhitespace(f.purfaxno.value))) {
    temp2 = "Purchasing Address: Fax Number";
    f.purfaxno.focus();
    pass=false;
  }
*/

  if (pass && (f.purcity.value == '' || isWhitespace(f.purcity.value))) {
    temp2 = "Purchasing Address: City";
    f.purcity.focus();
    pass=false;
  }

  if (pass && (f.puremail.value == '' || isWhitespace(f.puremail.value))) {
    temp2 = "Purchasing Address: E-mail";
    f.puremail.focus();
    pass=false;
  }

  if (pass && (f.purstate.options[f.purstate.selectedIndex].value == '' || f.purstate.options[f.purstate.selectedIndex].value =="default")) {
    temp2 = "Purchasing Address: State";
    f.purstate.focus();
    pass=false;
  }
  
  if (pass && (f.purzip.value == '' || isWhitespace(f.purzip.value))) {
    temp2 = "Purchasing Address: Zip/Postal Code";
    f.purzip.focus();
    pass=false;
  } 
 /* 
  if (pass && (<%=CInt(session("codecount"))%> > 0)) {
    temp2 = "NAICS Code";
    f.addnaicscode.focus();
    pass=false;
  }    
  */
  if (pass && (f.purcountry.value == '')) {
    temp2 = "Purchasing Address: Country";
    f.purcountry.focus();
    pass=false;
  }
  
  if (pass && (f.purattention.value == '')) {
    temp2 = "Attention Of";
    f.purattention.focus();
    pass=false;
  }
/*
  if (pass && (f.remaddress1.value == '' || isWhitespace(f.remaddress1.value))) {
    temp2 = "Remittance Address: Address 1";
    f.remaddress1.focus();
    pass=false;
  }

  if (pass && (f.remphoneno.value == '' || isWhitespace(f.remphoneno.value))) {
    temp2 = "Remittance Address: Phone Number";
    f.remphoneno.focus();
    pass=false;
  }

  if (pass && (f.remfaxno.value == '' || isWhitespace(f.remfaxno.value))) {
    temp2 = "Remittance Address: Fax Number";
    f.remfaxno.focus();
    pass=false;
  }

  if (pass && (f.remcity.value == '' || isWhitespace(f.remcity.value))) {
    temp2 = "Remittance Address: City";
    f.remcity.focus();
    pass=false;
  }

  if (pass && (f.rememail.value == '' || isWhitespace(f.rememail.value))) {
    temp2 = "Remittance Address: E-mail";
    f.rememail.focus();
    pass=false;
  }

  if (pass && (f.remstate.options[f.remstate.selectedIndex].value == '' || f.remstate.options[f.remstate.selectedIndex].value =="default")) {
    temp2 = "Remittance Address: State";
    f.remstate.focus();
    pass=false;
  }

  if (pass && (f.remcountry.value == '')) {
    temp2 = "Remittance Address: Country";
    f.remcountry.focus();
    pass=false;
  }

  if (pass && (f.remzip.value == '' || isWhitespace(f.remzip.value))) {
    temp2 = "Remittance Address: Zip/Postal Code";
    f.remzip.focus();
    pass=false;
  }
*/


  if (pass && (f.certstatus.options[f.certstatus.selectedIndex].value == '' || f.certstatus.options[f.certstatus.selectedIndex].value =="default")) {
    temp2 = "Certification Status";
    f.certstatus.focus();
    pass=false;
  }

/*
  if (pass && isNaN(Number(f.taxno.value))) {
    alert("Please make sure the 'Federal Tax #' field is a numeric value.")
    f.taxno.focus();
    return false;
  }

  if (pass && checkEmail(f.puremail.value)) {
    alert("Please make sure the 'Purchasing Address: E-mail' field is a valid e-mail address.");
    f.puremail.focus();
    return false;
  }

  if (pass && checkEmail(f.rememail.value)) {
    alert("Please make sure the 'Remittance Address: E-mail' field is a valid e-mail address.");
    f.rememail.focus();
    return false;
  }
*/
 
  if (!pass) {
    requiredMessage(temp2)    
    return false;
  } 
    
  return true;

}

function correctDropDown(dropDownName, selection) {
  for(i=0; i<document.forms[0].elements[dropDownName].length; i++) { 
	if (document.forms[0].elements[dropDownName].options[i].value == selection) {
		document.forms[0].elements[dropDownName].options[i].selected = true;
	}
  } 
}

function checkEmail(email) {
  if (email.indexOf('@') == -1 || email.indexOf('.') == -1) return true;
  else return false;
}

function checkLoginForm(f) {
  var pass=true;
  var temp2 = "";

  if (f.login.value == '' || isWhitespace(f.login.value)) {
    temp2 = "Login ID";
    f.login.focus();
    pass=false;
  }

  if (pass && (f.password.value == '' || isWhitespace(f.password.value))) {
    temp2 = "Password";
    f.password.focus();
    pass=false;
  }

  if (!pass) {
    requiredMessage(temp2)    
    return false;
  } else return true;
}

function requiredMessage(fieldname) {
  alert("Please make sure the '" + fieldname + "' field is properly completed.");
}

function checkdate(strMonth, strDay, strYear) {
  if (chkdate(strMonth, strDay, strYear) == false) {
    strMonth.select();
    strMonth.focus();
    return false;
  } else {
    return true;
  }
}
function chkdate(strMonth, strDay, strYear) {
  var intday;
  var intMonth;
  var intYear;
  var booFound = false;
  var datemonth = strMonth.value;
  var dateday = strDay.value;
  var dateyear = strYear.value;
  var intElementNr;
  var err = 0;

  if (dateyear.length == 2) {
	dateyear = '20' + dateyear;
  }
  intday = parseInt(dateday, 10);
  if (isNaN(intday)) {
	err = 2;
	return false;
  }
  intMonth = parseInt(datemonth, 10);
  if (isNaN(intMonth)) {
    err = 3;
    return false;
  }
  intYear = parseInt(dateyear, 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;
	  }
	}
  }
  strMonth.value = intMonth;
  strDay.value = intday;
  strYear.value = intYear;
  return true;
}

function LeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) { return true; }
  }  else {
    if ((intYear % 4) == 0) { return true; }
  }
  return false;
}
