function setStateValue(m, stateValue) {
  if (isIE()) {
	m.state.value = stateValue;
  } else {
	for (i_state=0; i_state < m.state.options.length; i_state++) {
	  if (m.state.options[i_state].value == stateValue) {
		m.state.options[i_state].selected = true;
		break;
	  }
	}
  }
}

function ShippingAddress(ls_company, ls_address1, ls_address2, ls_city, ls_state, ls_zip, ls_zipplus4) {
  this.company = ls_company;
  this.address1 = ls_address1;
  this.address2 = ls_address2;
  this.city = ls_city;
  this.state = ls_state;
  this.zip = ls_zip,
  this.zipplus4 = ls_zipplus4;
}

function customerNumber_onChange(lb_isDS) {
  var f = document.frmApp;
  var li_custNumIndex = f.customerNumber.options.selectedIndex;
  if (js_shipToAddr.length > 0) {
	updateAddress(f);
	lo_dropShip = null;
  }
}

function updateAddress(m) {
  if (js_shipToAddr.length > 0) {
	var is_DS = m.shipType.checked;
	if (is_DS) {
	  //Check for previous drop ship text values
	  if (typeof(lo_dropShip) == "undefined" || lo_dropShip == null) {
		m.company.value = "";
		m.address1.value = "";
		m.address2.value = "";
		m.city.value = "";
		m.zip.value = "";
		m.zipplus4.value = "";
		m.state.options[0].selected = true;
	  } else {
	  //Get drop ship text values from stored values
		m.company.value = lo_dropShip.company;
		m.address1.value = lo_dropShip.address1;
		m.address2.value = lo_dropShip.address2;
		m.city.value = lo_dropShip.city;
		m.zip.value = lo_dropShip.zip;
		m.zipplus4.value = lo_dropShip.zipplus4;
		setStateValue(m, lo_dropShip.state);
	  }
	m.shipType.checked = true;
	} else {
	  //Not a drop ship
	  var li_custNumIndex = m.customerNumber.options.selectedIndex;
	  m.company.value = js_shipToAddr[li_custNumIndex].company;
	  m.address1.value = js_shipToAddr[li_custNumIndex].address1;
	  m.address2.value = js_shipToAddr[li_custNumIndex].address2;
	  m.city.value = js_shipToAddr[li_custNumIndex].city;
	  setStateValue(m, js_shipToAddr[li_custNumIndex].state)
	  m.zip.value = js_shipToAddr[li_custNumIndex].zip;
	  m.zipplus4.value = js_shipToAddr[li_custNumIndex].zipplus4;
	  m.shipType.checked = false;
	}

	if (isIE()) {
	  m.company.disabled = !(is_DS);
	  m.address1.disabled = !(is_DS);
	  m.address2.disabled = !(is_DS);
	  m.city.disabled = !(is_DS);
	  m.state.disabled = !(is_DS);
	  m.zip.disabled = !(is_DS);
	  m.zipplus4.disabled = !(is_DS);
	} else {
	  m.company.onfocus = (is_DS) ? "" : m.company.blur;
	  m.address1.onfocus = (is_DS) ? "" : m.address1.blur;
	  m.address2.onfocus = (is_DS) ? "" : m.address2.blur;
	  m.city.onfocus = (is_DS) ? "" : m.city.blur;
	  m.state.onfocus = (is_DS) ? "" : m.state.blur;
	  m.zip.onfocus = (is_DS) ? "" : m.zip.blur;
	  m.zipplus4.onfocus = (is_DS) ? "" : m.zipplus4.blur;
	}
  }
}

function validateFormElement( formElement,str) {
  inputValue = formElement.value
  var okay = true;
  if( inputValue == null || inputValue.length == 0 || inputValue.value == ' ' ) {
	alert ( str );
	formElement.focus();
	okay = false;
  }
  return okay
}

function checkAppForm(h) {

  //if ( h.customerNumber.value == '' && h.customerNumber[0].selected == true){
	//alert('Please choose a customer number');
	//h.customerNumber.focus();
	//return false;
  //}
  if(!validateFormElement( h.company, "Please enter a company name"))
	return false;
  if(!validateFormElement( h.dba, "Please enter a DBA name"))
	return false;
  if(!validateFormElement( h.address1, "Please enter an address"))
	return false;
  if(!validateFormElement( h.city, "Please enter a city"))
	return false;
  if(!validateFormElement( h.zip, "Please enter a zip"))
	return false;
  if ( h.state.value == 'BAD' || h.state.options[0].selected == true) {
	alert('Please choose a state');
	h.state.focus();
	return false;
  }
  var expObj = /\D/;
  var lsZip = h.zip.value;
  var lsZip4 = h.zipplus4.value;
  if (lsZip.length != 5) {
	alert('You entered an invalid number for your zip code');
	h.zip.focus();
	return false;
  }
  var isValidZip = lsZip.match(expObj);
  if (isValidZip != null) {
	alert('You entered invalid characters for your zip code');
	h.zip.focus();
	return false;
  }
  if (lsZip4.length != 0) {
	if (lsZip4.length != 4) {
	  alert('You entered an invalid number for your zip code');
	  h.zipplus4.focus();
	  return false;
	}
	var isValidZipPlus4 = lsZip4.match(expObj);
	if (isValidZipPlus4 != null) {
	  alert('You entered invalid characters for your zip code');
	  h.zipplus4.focus();
	  return false;
	}
  }

  if (validatePhone(h.phone0a,3) == false) return false;
  if (validatePhone(h.phone0b,3) == false) return false;
  if (validatePhone(h.phone0c,4) == false) return false;
  if (h.taxid.value.length < 9) {
    alert("Federal Tax Id is required and must be at least 9 numbers");
	h.taxid.select();
	h.taxid.focus();
	return false;
  }
  if(Checknum(h.locations.value)==false || (h.locations.value.length == 0)) {
    alert("Please enter the number of Total Sales Locations");
    h.locations.select();
    h.locations.focus();
	return false;
  }
  //if(Checknum(h.employees.value)==false || (h.employees.value.length == 0)) {
  //  alert("Please enter the total number of employees");
  //  h.employees.select();
  //  h.employees.focus();
  //return false;
  //}
  if(!validateFormElement( h.primary, "Please enter a primary contact name"))
	return false;
  if (validatePhone(h.phone13a,3) == false) return false;
  if (validatePhone(h.phone13b,3) == false) return false;
  if (validatePhone(h.phone13c,4) == false) return false;

  if ((h.campus[0].checked == false) && (h.campus[1].checked == false))	{
    alert("Please select Yes if you are a Campus Reseller, otherwise select No");
	h.campus[0].select();
	h.campus[0].focus();
	return false;
  }

  if (h.proav && (h.proav[0].checked == false) && (h.proav[1].checked == false))	{
    alert("Please select Yes if you are a Pro AV Reseller, otherwise select No");
	h.proav[0].select();
	h.proav[0].focus();
	return false;
  }

  if ((h.dist[0].value.length == 0) && (h.dist[1].value.length == 0)  && (h.dist[2].value.length == 0)  && (h.dist[3].value.length == 0) && (h.dist[4].value.length == 0) && (h.direct.checked == false)){
    alert("Enter a customer number for at least one of the distributors. \nIf you purchase directly from Toshiba, check Direct Customer.");
	h.dist[0].select();
	h.dist[0].focus();
	return false;
  }
  if ((h.dist[0].value.length > 0 || h.dist[1].value.length > 0) && (h.dist[0].value.length < 2 || h.dist[1].value.length < 9)){
    alert("Ingram customer number should be 11 digits");
	h.dist[0].select();
	h.dist[0].focus();
	return false;
  }
  if ((h.dist[2].value.length > 0) && (h.dist[2].value.length < 6)){
    alert("Tech Data customer number should be 6 or 7 digits");
	h.dist[2].select();
	h.dist[2].focus();
	return false;
  }
  if ((h.dist[3].value.length > 0) && (h.dist[3].value.length < 5)){
    alert("Synnex customer number should be 5 or 6 digits");
	h.dist[3].select();
	h.dist[3].focus();
	return false;
  }
  if ((h.dist[4].value.length > 0) && (h.dist[4].value.length != 7)){
    alert("ICG customer number should be 7 digits");
	h.dist[4].select();
	h.dist[4].focus();
	return false;
  }
  /*
  if ((h.dist[2].value.length > 0) && (h.dist[2].value.length < 4)){
    alert("PC Wholesale customer number should be at least 4 digits");
	h.dist[2].select();
	h.dist[2].focus();
	return false;
  }*/
  if ( (h.sale[0].checked == false) && (h.sale[2].checked == false) && (h.sale[4].checked == false) )	{
    alert("Please select at least one product you will be focusing sales efforts on through the program.");
	h.sale[1].select();
	h.sale[1].focus();
	return false;
  }
  var liSum = 0;
  if (h.sale[0].checked == true)
	liSum += validatePercent(h.sale[1], "Notebook was selected, but a percentage was not entered or is not numeric");
  if (h.sale[2].checked == true)
	liSum += validatePercent(h.sale[3], "Projector was selected, but a percentage was not entered or is not numeric");
  if (h.sale[4].checked == true)
    liSum += validatePercent(h.sale[5], "Accessories was selected, but a percentage was not entered or is not numeric");
  if (liSum > 100) {
    alert("The sales effort percentages can not be greater than 100%, you entered " + liSum + "%");
	h.sale[1].select();
	h.sale[1].focus();
	return false;
  }
  if ((h.sell[5].checked == true) && (h.sell[6].value.length == 0))	{
    alert("Please enter a manufacturer for other");
	h.sell[6].select();
	h.sell[6].focus();
	return false;
  }
  if ((h.serv[5].checked == true) && (h.serv[6].value.length == 0))	{
    alert("Please enter a manufacturer for other");
	h.serv[6].select();
	h.serv[6].focus();
	return false;
  }
  if ((h.market[10].checked == true) && (h.market[11].value.length == 0))	{
    alert("Please enter a vertical market for other");
	h.market[11].select();
	h.market[11].focus();
	return false;
  }
  if ((h.act[10].checked == true) && (h.act[11].value.length == 0))	{
    alert("Please enter an activity for other");
	h.act[11].select();
	h.act[11].focus();
	return false;
  }
  var liSum = 0;
  liSum += validatePercent(h.revenue[0], "Total System percent must be a number");
  liSum += validatePercent(h.revenue[2], "Periperals percent must be a number");
  liSum += validatePercent(h.revenue[5], "Software percent must be a number");
  liSum += validatePercent(h.revenue[8], "Services percent must be a number");
  liSum += validatePercent(h.revenue[10], "Supplies percent must be a number");
  if (liSum > 100) {
    alert("The revenue percentages can not be greater than 100%, you entered " + liSum + "%");
	h.revenue[0].select();
	h.revenue[0].focus();
	return false;
  }
  if (Checknum(h.revenue[1].value)==false && h.revenue[1].value.length != 0) {
    alert("Total System amount must be a number");
	h.revenue[1].select();
	h.revenue[1].focus();
	return false;
  }
  if (Checknum(h.revenue[3].value)==false && h.revenue[3].value.length != 0) {
    alert("Periperals amount must be a number");
	h.revenue[3].select();
	h.revenue[3].focus();
	return false;
  }
  if (Checknum(h.revenue[6].value)==false && h.revenue[6].value.length != 0) {
    alert("Software amount must be a number");
	h.revenue[6].select();
	h.revenue[6].focus();
	return false;
  }
  if (Checknum(h.revenue[9].value)==false && h.revenue[9].value.length != 0) {
    alert("Sevices amount must be a number");
	h.revenue[9].select();
	h.revenue[9].focus();
	return false;
  }
  if (Checknum(h.revenue[11].value)==false && h.revenue[11].value.length != 0) {
    alert("Supplies amount must be a number");
	h.revenue[11].select();
	h.revenue[11].focus();
	return false;
  }
  var liSum = 0;
  liSum += validatePercent(h.revenue[4], "Notebook percent must be a number");
  liSum += validatePercent(h.revenue[7], "Projector percent must be a number");
  if (liSum > 100) {
    alert("The revenue percentages for Notebook and Projector can not be\n greater than 100%, you entered " + liSum + "%");
	h.revenue[4].select();
	h.revenue[4].focus();
	return false;
  }
  liSum = 0;
  for (var i = 0; i < 6; i++){
	if (h.seg[i].value.length != 0)
	  if (Checknum(h.seg[i].value)==false) {
    	alert("Segment percent must be numeric");
		h.seg[i].select();
		h.seg[i].focus();
		return false;
	  } else
    	liSum += parseInt(h.seg[i].value);
  }
  if (liSum > 100 || liSum < 90) {
    alert("The segment percentages should equal 100%, you entered " + liSum + "%");
	h.seg[0].select();
	h.seg[0].focus();
	return false;
  }
  if ((h.addserv[10].checked == true) && (h.addserv[11].value.length == 0))	{
    alert("Please describe the service offered for other");
	h.addserv[11].select();
	h.addserv[11].focus();
	return false;
  }


  // Validate contacts
  var liContactLength = reqContacts.length
  for (var i = 0; i < liContactLength; i++){

    if (h.contact[i] != null) {

		// Check for required contacts. Compare against reqContacts array to see if this element is required.
		if (reqContacts[i] == true)
		  if(!validateFormElement(h.contact[i], "Please enter a contact name for " + h.label[i].value))
			return false;

		// If contact is not empty, other fields are required for this contact
		if (h.contact[i].value.length != 0) {
		  if (validatePhone(h.phonea[i],3) == false) return false;
		  if (validatePhone(h.phoneb[i],3) == false) return false;
		  if (validatePhone(h.phonec[i],4) == false) return false;
		  if (h.email[i].value.length == 0 || h.title[i].value.length == 0) {
		    alert("Title, phone, and email are required if a contact is provided");
			h.contact[i].select();
			h.contact[i].focus();
		    return false;
		  }
		}

		// Validate e-mail addresses
		if ((h.email[i].value.length != 0) && (validEmail(h.email[i]) == false)) return false;

		// If contact is blank other fields must be blank too
		if (h.contact[i].value.length == 0 && (h.phonea[i].value.length != 0 || h.phoneb[i].value.length != 0 ||
			h.phonec[i].value.length != 0 || h.email[i].value.length != 0 || h.title[i].value.length != 0)) {
		  alert("Phone number and email must be blank if a Contact Name is not entered");
		  h.contact[i].select();
		  h.contact[i].focus();
		  return false;
		}
	}
  }


  if ((h.refer[0].checked == false) && (h.refer[1].checked == false) && (h.refer[2].checked == false) && (h.refer[3].checked == false) && (h.refer[4].checked == false) && (h.refer[5].checked == false))	{
    alert("Please indicate how you heard about the Preferred Partner Program");
	h.refer[0].select();
	h.refer[0].focus();
	return false;
  }
   if ((h.refer[5].checked == true) && (h.refer2.value.length == 0)) {
    alert("Please describe how you heard about the Preferred Partner Program");
	h.refer2.select();
	h.refer2.focus();
	return false;
  }
  if ((h.refer[2].checked == true) && ( h.refer3.value == ' ' || h.refer3.options[0].selected == true)) {
    alert("Please select the distributor that referred you to the Preferred Partner Program");
	//h.refer3.select();
	h.refer3.focus();
	return false;
  }
  if (isIE()) {
	h.company.disabled = false;
	h.address1.disabled = false;
	h.address2.disabled = false;
	h.city.disabled = false;
	h.state.disabled = false;
	h.zip.disabled = false;
	h.zipplus4.disabled = false;
  }
  return true;
}

function validEmail(tbox) {
  var email=tbox.value ;
  if(email.length>0) {
    if(email.length < 5 ) {
      alert('Invalid Email');
      tbox.focus();
      return false;
    }
    var atSign = email.indexOf('.');
    if( atSign <= 0 ) {
      alert('Invalid Email');
      tbox.focus();
      return false;
    }
    var atSign = email.indexOf('@');
    if( atSign <= 0 ){
      alert('Invalid Email');
      tbox.focus();
      return false;
    }
  }
  return true;
}

var phone_field_length=0;
function TabNext(obj,event,len,next_field) {
  if (event == "down") {
    phone_field_length=obj.value.length;
  }
  else if (event == "up") {
    if (obj.value.length != phone_field_length) {
      phone_field_length=obj.value.length;
      if (phone_field_length == len) {
        next_field.focus();
      }
    }
  }
}

function validatePhone(fName,numLen) {
  var fVal=fName.value;
  if(Checknum(fVal)==false || (fVal.length != numLen)) {
    alert("Please make sure the phone number field contains "+ numLen+" digit numbers.");
    fName.select();
    fName.focus();
    return false;
  }
  return true ;
}

function validatePercent(fName, errmsg) {
  if (fName.value.length != 0)
	if (Checknum(fName.value)==false) {
    alert(errmsg);
	  fName.select();
	  fName.focus();
	} else
      return parseInt(fName.value);
  return 0;
}

function Checknum(num) {
  var valid = true;
  var number = new String(num);
  for (var i = 0; i < number.length; i++){
    var n = number.charAt(i);
    //ONLY NUMERIC CHARACTERS ALLOWED
    if ((!((parseFloat(n) >= 0)||(parseFloat(n) <= 9)))||isNaN(n)){
      valid = false;
      break;
    }
  }
  return valid;
}

