// function to update city,state and zip
var strIntraStateMsg = 'En este tiempo ABF U-Pack no le puede ofrecer una cotización para una mudanza dentro del mismo estado.';
var strIntraCAMsg = 'ABF U-Pack no hace mudanzas de Cañada a Cañada.';
var strIntraMXMsg = 'ABF U-Pack no proporciona los servicios de mudanza dentro de México.';
var blnSubmitOnce = false;
var iUSPhone = " tiene que se un numero de 10 – dígitos valido (como 415 555 1212).  Por favor entre el numero de nuevo.";

function ValidUPackZip(cboSt, txtZp) {

    if (cboSt.options[cboSt.selectedIndex].value.substr(2,2) == 'US') 
    {
       return ValidUSZip(txtZp);
    }
    else if (cboSt.options[cboSt.selectedIndex].value.substr(2,2) == 'CA') 
    {
       return ValidCAZip(txtZp);
    }
    else if (cboSt.options[cboSt.selectedIndex].value.substr(2,2) == 'MX') 
    {
       return ValidMXZip(txtZp);
    }

   return true;
}


function validatePageOne() {
   var objForm = document.frmQuote;

   if (objForm.quick) var pg = "HOME/QUOTE";
   else var pg = "QUOTE";

   var fromState = objForm.cboFromState.options[objForm.cboFromState.selectedIndex].value;
   var toState = objForm.cboToState.options[objForm.cboToState.selectedIndex].value;

   if (((objForm.txtFromCity.value == '') || (fromState == '')) && (objForm.txtFromZip.value == '')) {
      return warnInvalid(objForm.txtFromCity,'Una CUIDAD y ESTADO o CODIGO POSTAL valido de origen es requerido...')
   }

   if (((objForm.txtToCity.value == '') || (toState == '')) && (objForm.txtToZip.value == '')) {
      return warnInvalid(objForm.txtToCity,'Una CUIDAD y ESTADO o CODIGO POSTAL valido de destino es requerido...')
   }

   if (toState != '') {
      if (fromState.substring(2) == 'CA' && toState.substring(2) == 'CA') {
         trackClientEvent(pg + ";" + objForm.sid.value + ";INTRA-CANADIAN MOVE");
         return warnInvalid(objForm.cboFromState,strIntraCAMsg);
      }
      else if (fromState.substring(2) == 'MX' && toState.substring(2) == 'MX') {
         trackClientEvent(pg + ";" + objForm.sid.value + ";INTRA-MEXICAN MOVE");
         return warnInvalid(objForm.cboFromState,strIntraMXMsg);
      }
   }

   if (objForm.txtFromZip.value != '') {
      if (!ValidUPackZip(objForm.cboFromState, objForm.txtFromZip)) {
         return warnInvalid(objForm.txtFromZip,'Una CODIGO POSTAL valido de origen es requerido...');
      }
   }

   if (objForm.txtToZip.value != '') {
      if (!ValidUPackZip(objForm.cboToState, objForm.txtToZip)) {
         return warnInvalid(objForm.txtToZip,'Una CODIGO POSTAL valido de destino es requerido...');
      }
   }

   if (!isDate(objForm.cboShipYear.options[objForm.cboShipYear.selectedIndex].value, objForm.cboShipDay.options[objForm.cboShipDay.selectedIndex].value, objForm.cboShipMonth.options[objForm.cboShipMonth.selectedIndex].value)) {
      return warnInvalid(objForm.cboShipYear,'Un problema ocurrió cuando estábamos procesando la fecha de mudanza. Por favor cierre el hojeador y regrese a la pagina de cotización para continuar. Si el problema persiste, por favor llame le a un representante de U-Pack al 1-800-413-5182.');
   }

   if (objForm.cboMovingOutOf.options[objForm.cboMovingOutOf.selectedIndex].value == '') {
      return warnInvalid(objForm.cboMovingOutOf,'Tiene que especificar el TAMANIO DE SU RESIDENCIA…');
   }

   return true;
}

function validatePageTwo() {
   var objForm = document.frmQuote;

   if (isWhitespace(trimWhiteSpace(objForm.txtFirstName.value))) {
      return warnInvalid(objForm.txtFirstName,'Su primer nombre es requerido...');
   }
   if (isWhitespace(trimWhiteSpace(objForm.txtLastName.value))) {
      return warnInvalid(objForm.txtLastName,'Su apellido es requerido...');
   }
//corporate relocation
   if (objForm.txtEmployeeEmail) {
      if (objForm.txtEmployeeEmail.value > '') {
         if (!ValidEMail(objForm.txtEmployeeEmail.value)) {
            return warnInvalid(objForm.txtEmployeeEmail,'Un coreo electrónico valido es requerido...');
         }
      }
   }
   if (objForm.optCorpPaymentType) {
      if ((objForm.optCorpPaymentType[0].checked == false) && (objForm.optCorpPaymentType[1].checked == false)) {
         return warnInvalid(objForm.optCorpPaymentType[0], "Un TIPO DE PAGO se tiene que eligir...");
      }
   }

   if (!ValidEMail(objForm.txtEmail.value)) {
      return warnInvalid(objForm.txtEmail,'Un coreo electrónico valido es requerido...');
   }
   if (objForm.txtConfirmEmail) {
      if (objForm.txtEmail.value.toLowerCase() != objForm.txtConfirmEmail.value.toLowerCase()) {
         return warnInvalid(objForm.txtConfirmEmail,'La confirmación del coreo electrónico no combina correctamente...');
      }
   }


//checks to see if this is present before executing code
   if (objForm.cboContactBy) {
      if (objForm.cboContactBy.selectedIndex == 0) {
         return warnInvalid(objForm.cboContactBy,'Por favor elija el método de contacto preferido.');
      }
      if (objForm.cboContactBy.options[objForm.cboContactBy.selectedIndex].value == 'PHONE') {
         if (objForm.txtPhoneExt.value.length > 0) {
            if (!checkUSPhone(objForm.txtContactPhone,'Un NUMERO DE TELEFONO es requerido, si da una extensión, el numero','false')) {
               return false;
            }
         }

         if (!checkUSPhone(objForm.txtContactPhone,'Si quiere que lo contactemos por teléfono, el numero','false')) {
            return false;
         }
         if (objForm.cboDayOfWeek.selectedIndex == 0) {
            return warnInvalid(objForm.cboDayOfWeek,'Tiene que escoger un día de la semana...');
         }

         if (objForm.cboTimeOfDay.selectedIndex == 0) {
            return warnInvalid(objForm.cboTimeOfDay,'Tiene que escoger un tiempo en el día...');
         }
      }
      if (objForm.cboContactBy.options[objForm.cboContactBy.selectedIndex].value == 'FAX') {
         if (!checkUSPhone(objForm.txtFax,'Si quiere que lo contactemos por Fax, el numero','false')) {
            return false;
         }
      }
   }

   return true;
}

function justGo(intGoPage, strButton) {
   var objForm = document.frmQuote;

   if (intGoPage == 1) {
      objForm.hidGetService.value = '';
      objForm.t2t.value = '';
      objForm.txtOldT2T.value = '';
      objForm.hidBoxEquipment.value = ''
      objForm.hidTrackEvent.value = '';
   }
   else if ((intGoPage == 2) && (strButton == 'prev')) {
      if (objForm.txtOldT2T.value == 'Y') objForm.t2t.value = '';
      else objForm.t2t.value = objForm.txtOldT2T.value;

      objForm.hidBoxEquipment.value = '';
      objForm.hidTrackEvent.value = '';
   }
   objForm.page.value = intGoPage;
   objForm.getQuote.value = '';
   objForm.submit();
}

function validateThenGo(intThisPage, intGoPage) {
   var objForm = document.frmQuote;

   if (intThisPage == 1) {
      if (!validatePageOne()) return false;
      objForm.checkCities.value = 'Y';
   }
   else if (intThisPage == 2) {
      if (!validatePageTwo()) return false;
   }

  //if we are trying to actually get a quote then inGoPage will be 0
  //make sure every page is ok then submit quote, else go back to that page

   if (intGoPage == 0) {
      objForm.getQuote.value = 'Y';
   }
   else {
      objForm.page.value = intGoPage;
      objForm.getQuote.value = '';
   }

   if (!blnSubmitOnce) {
      blnSubmitOnce = true;
      objForm.submit();
   }
}

function resubmitQuote() {
   if (!blnSubmitOnce) {
      blnSubmitOnce = true;
      document.frmResubmit.submit();
   }
}


function justGoBox(intGoPage, strBoxEquipment, strFeet) {
   document.frmQuote.hidBoxEquipment.value = strBoxEquipment;
   document.frmQuote.cboLinearFeet.value = strFeet;
   justGo(intGoPage);
}


function showContact() {
   var objForm = document.frmQuote;

   contact = objForm.cboContactBy.options[objForm.cboContactBy.selectedIndex].value;

   if (contact == "PHONE") {
      document.getElementById("fax").style.visibility = "hidden";
      document.getElementById("fax").style.display = "none";
      document.getElementById("phone").style.visibility = "visible";
      document.getElementById("phone").style.display = "";
   }
   else if (contact == "FAX") {
      document.getElementById("phone").style.visibility = "hidden";
      document.getElementById("phone").style.display = "none";
      document.getElementById("fax").style.visibility = "visible";
      document.getElementById("fax").style.display = "";
   }
   else {
      document.getElementById("phone").style.visibility = "hidden";
      document.getElementById("phone").style.display = "none";
      document.getElementById("fax").style.visibility = "hidden";
      document.getElementById("fax").style.display = "none";
   }

   showContactTime(contact);
}


function showContactTime(contact) {
   if (contact == "PHONE") {
      document.getElementById("contacttime").style.visibility = "visible";
      document.getElementById("contacttime").style.display = "";
   }
   else {
      document.getElementById("contacttime").style.visibility = "hidden";
      document.getElementById("contacttime").style.display = "none";
   }
}
