/*
        PBXSelect.js
        Version 1.0.0
        09 Oct 2007
        ejr

*/

function handleOnChange(dd1)
{
  var idx = dd1.selectedIndex;
  var val = dd1[idx].text;
  var par = document.forms["frmSelect"];
  var parelmts = par.elements;
  var citysel = parelmts["city"];
  var state = val;
  if(state != "Select State")
  {
   var directory = ""+document.location;
   directory = directory.substr(0, directory.lastIndexOf('/'));

   Http.get({
                url: "/services/local/getcities_pbx.php?state=" + state,
                callback: fillCities,
                cache: Http.Cache.Get
        }, [citysel]);
  }
  else {
     citysel.length = 1;
  }

}

function fillCities(xmlreply, cityelmt)
{
  if (xmlreply.status == Http.Status.OK)
  {
   var cityresponse = xmlreply.responseText;
   var cityar = cityresponse.split("|");
   var numcities = cityar.length; 
   cityelmt.length = 1;
   cityelmt.length = numcities;

   for (o=1; o < numcities; o++)
   {
     var marketresponse = cityar[o-1];  /* zero-based index where cityelmt skips [0] position as pre-filled */
     var marketar = marketresponse.split("#");

     if(marketar.length < 2) {
        cityelmt[o].text = cityar[o-1];
        }
     else {
        cityelmt[o].value = marketar[0];
        cityelmt[o].text = marketar[1];
        }
     }
  }
  else
  {
   alert("Cannot handle the Ajax call.");
  }

  if(cityelmt[1].text == "Toll Free") {
     cityelmt.selectedIndex = 1;
  }
}

function submitCity() {
  var par = document.forms["frmSelect"];
  var parelmts = par.elements;
  var citysel = parelmts["city"];
  var statesel = parelmts["state"];

  if (statesel.value == "Toll Free" && citysel.value == "Select City") {
     par.action = 'http://www.voicenation.com/800-pbx.shtml';
     par.submit();
     }
  else if (citysel.value != "Select City"){
     /* old method when sending directly to miva purchase page */
     /* par.action = 'http://www.voicenation.com/signup/'+citysel.value+'-PFM.htm'; */
     par.action = 'http://www.voicenation.com'+citysel.value;
     par.submit();
     }
  else {
     alert("Please select a city.");
     return false;
  }
}



