var objStatus;
var frm;
var objForm;
var objRPMAjax;

function init() {
   objForm = document.frmRPM;
   frm = objForm;
   frm.zip.focus();
}

function clearHidCityStateData() {
   objForm.city.value = ''
   objForm.state.value = ''
   if (document.getElementById("DropDown")) {
      document.getElementById("DropDown").className = 'formTxt hidden';
      document.getElementById("DropDown").innerHTML = '';
      document.getElementById("DropDown").style.padding = '0px';
      document.getElementById("DropDown").style.backgroundColor = '#FFFFFF';
   }
}

function Validate() {
   frm = document.frmRPM;
   frm.zip.value = frm.zip.value.replace(/-/,'');
   if (isEmpty(frm.zip.value)) {
      return warnInvalid(frm.city,"A valid zip/postal code is required.")
   }
   if (!isEmpty(frm.zip.value)) {
      if ((eval('frm.country.selectIndex') == 1) && (!ValidCAZip(frm.zip))) {
         return warnInvalid(frm.zip,"An invalid Canadian zip/postal code has been entered.");
      }
      if ((eval('frm.country.selectIndex') == 2) && (!ValidMXZip(frm.zip))) {
         return warnInvalid(frm.zip,"An invalid Mexican zip/postal code has been entered.");
      }
      if ((eval('frm.country.selectIndex') == 0) && (!ValidUSZip(frm.zip))) {
         return warnInvalid(frm.zip,"An invalid US zip/postal code has been entered.");
      }
   }
   return true;
}

function SubmitForm() {
   if (Validate()) {
      frm.btnGetMap.disabled = true
      GetRPMMap();
   }
}

function GetRPMMap() {
   objRPMAjax = new RPMAjax();
   if (objRPMAjax){
      objRPMAjax.City    = frm.city.value
      objRPMAjax.State   = frm.state.value
      objRPMAjax.Zip     = frm.zip.value
      objRPMAjax.Country = frm.country.value
      objRPMAjax.GetRPMMap();
   }
}

function RPMAjax() {
   this.City     = null;
   this.State    = null;
   this.Zip      = null;
   this.Country  = null;

   var objRequest = new Ajax();
   var strPath
   var strQueryString = ''
   var strFldType

   this.GetRPMMap = function () {
      strPath = '/tools/shared/Ajax.asp'
      strQueryString = 'page=RPM&city='+escape(this.City)+'&state='+escape(this.State)+'&zip='+escape(this.Zip)+'&country='+escape(this.Country)
      if (objRequest) {
         objRequest.responseFormat = 'text'
         objRequest.doPost(strPath,strQueryString,handleRPMResponse);
      }
   }
}

function handleRPMResponse(strResponse) {
   var arrResults = strResponse.split("]||[")
   frm.btnGetMap.disabled = false

   switch (arrResults[0]) {
      case 'select':
         // CityCoding found multiple Cities for this zip code...show dropdown
         if (document.getElementById("DropDown")) {
            document.getElementById("DropDown").className = 'formTxt visible';
            document.getElementById("DropDown").innerHTML = arrResults[2];
            document.getElementById("DropDown").style.padding = '5px';
            document.getElementById("DropDown").style.backgroundColor = '#FFCECE';
         }
      case 'error':
         // Did not return anything because of an error
         warnInvalid(frm.zip,arrResults[1]);
         break;
      case 'success':
         upWinXY('xWin',arrResults[1],725,500,false,false,true)
      default:
         // Did not return anything because of an unknown error that took place
         // The Ajax object should handle this...
         break;
   }
}