﻿// JScript File
var TestudoCityPickerVersion = "1.0"

var nCalls = 0;
function getRemoteData(url) 
{
    try 
    {
        nCalls++;
        var req
        req = new XMLHttpRequest() 
        req.open('POST',url + '&ivct=' + nCalls,false)
        req.send();

        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
        xmlDoc.async=false;
        xmlDoc.loadXML(req.responseText)
        //alert(req.responseText)
    } catch (e) {
        alert(e);
    }
    return xmlDoc
}
function jsCityPicker(strHandlerBaseUrl, strCountryComboDomId, strStateComboDomId, strCityComboDomId, strZipComboDomId)
{
   this.DynaFetchUrl = strHandlerBaseUrl;
   this.CountryComboDomId = strCountryComboDomId;
   this.StateComboDomId = strStateComboDomId;
   this.CityComboDomId = strCityComboDomId;
   this.ZipComboDomId = strZipComboDomId;
   this.countries = new Array();
   this.AddCountry = AddCountry;
   this.findCountry = findCountry;
   this.getCountry = getCountry;
   this.CountryCount = CountryCount;
    
   var nada = this.AddCountry("-none-", -1)
   nada.IsPopulated = true
    
    function CountryCount()
    {   
        return this.countries.length;
    }
   function getCountry(nIndex)
   {
      var countryRc = null;
      if (this.CountryCount() > nIndex)
      {
        countryRc = this.countries[nIndex];
      }
      return countryRc;
   }
   function AddCountry(strName, nPk)
   {
        var theJsCountry = new jsCountry(strName, nPk);
        this.countries[this.countries.length] = theJsCountry;
        theJsCountry.MasterCityPicker = this;
        return theJsCountry;
   }
   function findCountry(strName)
    {
        var countryRc = null;
        var oneCountry = null;
        var nIndex = 0;
        while (nIndex < this.countries.length)
        {
          oneCountry = this.countries[nIndex];
          if (oneCountry.name == strName)
          {
             countryRc = oneCountry;
          }
          nIndex++;
        }
        return countryRc;
    }
}
function jsCountry(strName, nPk)
{
    this.MasterCityPicker = null;
    this.PrimaryKey = nPk;
    this.name = strName;
    this.states = new Array();
    this.StateCount = StateCount;
    this.AddState = AddState;
    this.findState = findState;
    this.getState = getState;
    this.IsPopulated = false;
    this.dynaPopulate = dynaPopulate;

    var nada = this.AddState("-none-", -1)
    nada.IsPopulated = true;

    function dynaPopulate()
    {
        var strUrl = this.MasterCityPicker.DynaFetchUrl + "&act=s&pk=" + this.PrimaryKey;
        //alert(strUrl)
        var xmlDoc = getRemoteData(strUrl)

        var theNodes = xmlDoc.getElementsByTagName("state");
        var nNodes = theNodes.length;
        var nThisNode = 0;        
        while (nThisNode < nNodes)
        {
            var theNode = theNodes[nThisNode];
            var strState = theNode.firstChild.firstChild.xml; 
            var nPk = theNode.childNodes[1].firstChild.xml; 
            this.AddState(strState, nPk)
            nThisNode++;
        }
        //alert(this.name + ' dyna load ' + nNodes + ' nodes.')
        this.IsPopulated = true;
    }
   
    function StateCount()
    {   
        return this.states.length;
    }
    function AddState(strName, nPk)
    {
        var theState = new jsState(strName, nPk);
        this.states[this.states.length] = theState;
        theState.MasterCityPicker = this.MasterCityPicker;
        return theState;
    }
    function getState(nIndex)
    {
//alert('getState: this.StateCount()=' + this.StateCount())
//alert('getState: nIndex=' + nIndex)
      var stateRc = null;
      if (this.StateCount() > nIndex)
      {
      
        stateRc = this.states[nIndex];
      }
      return stateRc;
    }
    function findState(strName)
    {
        var stateRc = null;
        var oneState = null;
        var nIndex = 0;
        while (nIndex < states.length)
        {
          oneState = states[nIndex];
          if (oneState.name == strName)
          {
             stateRc = oneState;
          }
        }
        return stateRc;
    }
}
function jsState(strName, nPk)
{
    this.MasterCityPicker = null;
    this.PrimaryKey = nPk;
    this.name = strName;
    this.cities = new Array();
    
    this.CityCount = CityCount;
    this.AddCity = AddCity;
    this.findCity = findCity;
    this.getCity = getCity;
    this.IsPopulated = false;
    this.dynaPopulate = dynaPopulate;
    
    this.AddCity("-none-", -1)

    function dynaPopulate()
    {
        var strUrl = this.MasterCityPicker.DynaFetchUrl + "&act=c&pk=" + this.PrimaryKey;
        var xmlDoc = getRemoteData(strUrl)
        var theNodes = xmlDoc.getElementsByTagName("city");
        var nNodes = theNodes.length;
        var nThisNode = 0;        
        while (nThisNode < nNodes)
        {
            var theNode = theNodes[nThisNode];
            var strCity = theNode.firstChild.firstChild.xml; 
            var nPk = theNode.childNodes[1].firstChild.xml; 
//alert('outside AddCity: strCity ' + strCity + ' = ' + nPk )
            this.AddCity(strCity, nPk)
            nThisNode++;
        }
        this.IsPopulated = true;
    }

    function AddCity(strName, nPk)
    {
//alert('inside AddCity: strName ' + strName + ' = ' + nPk )
        var theJsCity = new jsCity(strName, nPk);
        this.cities[this.cities.length] = theJsCity;
        theJsCity.MasterCityPicker = this.MasterCityPicker;
        return theJsCity;
    }
    function CityCount()
    {   
        return this.cities.length;
    }
    function getCity(nIndex)
    {
      var cityRc = null;
      if (this.CityCount() > nIndex)
      {
        cityRc = this.cities[nIndex];
      }
      return cityRc;
    }
    function findCity(strName)
    {
        var cityRc = null;
        var oneCity = null;
        var nIndex = 0;
        while (nIndex < cities.length)
        {
          cityRc = cities[nIndex];
          if (oneCity.name == strName)
          {
             cityRc = oneCity;
          }
        }
        return cityRc;
    }    
}
function jsCity(strName, nPk)
{
    this.MasterCityPicker = null;
    this.name = strName;
    this.PrimaryKey = nPk;
    this.postalcodes = new Array();

    this.PostalCount = PostalCount;
    this.AddPostal = AddPostal;
    this.getPostal = getPostal;
    this.IsPopulated = false;
    this.dynaPopulate = dynaPopulate;
    
    this.AddPostal("-unknown-", -1)
    
    function PostalCount()
    {   
        return this.postalcodes.length;
    }
    function getPostal(nIndex)
    {
      var postalRc = null;
      if (this.PostalCount() > nIndex)
      {
        postalRc = this.postalcodes[nIndex];
      }
      return postalRc;
    }
    function dynaPopulate()
    {
        var strUrl = this.MasterCityPicker.DynaFetchUrl + "&act=z&pk=" + this.PrimaryKey;
        var xmlDoc = getRemoteData(strUrl)
        var theNodes = xmlDoc.getElementsByTagName("zip");
        var nNodes = theNodes.length;
        var nThisNode = 0;        
        while (nThisNode < nNodes)
        {
            var theNode = theNodes[nThisNode];
            var strPostal = theNode.firstChild.firstChild.xml; 
            //var nPk = theNode.childNodes[1].firstChild.xml; 
//alert('outside AddPostal: strPostal ' + strPostal + ' = ' + nPk )
            this.AddPostal(strPostal)
            nThisNode++;
        }
        this.IsPopulated = true;
    }

    function AddPostal(strName)
    {
//alert('inside AddPostal: strName ' + strName + ' = ' + nPk )
        var theJsPostal = new jsPostalCode(strName);
        this.postalcodes[this.postalcodes.length] = theJsPostal;
        theJsPostal.MasterCityPicker = this.MasterCityPicker;
        return theJsPostal;
    }
}
function jsPostalCode(strName)
{
    this.MasterCityPicker = null;
    this.name = strName;
    this.PrimaryKey = strName; // just the zip iteslef
}

function getSelectedCountry(thePicker)
{
    var strCountryDomId = thePicker.CountryComboDomId;
    var cboCountry = document.getElementById (strCountryDomId)
    var nCountryId = cboCountry.selectedIndex;
    var theJsCountry = thePicker.getCountry(nCountryId);
    return theJsCountry;
}
function onChangeCountry(thePicker)
{
    var theJsCountry = getSelectedCountry(thePicker);

    if (! theJsCountry.IsPopulated)
    {
      theJsCountry.dynaPopulate();
    }
    // blank the provs
    var strStateDomId = thePicker.StateComboDomId;
    var cboStateProv = document.getElementById (strStateDomId)
    cboStateProv.options.length = 0;

    var nStates = theJsCountry.StateCount();
  
    var nCurrentIndex = 0;
    while (nCurrentIndex < nStates)
    {
       var oneJsState = theJsCountry.getState(nCurrentIndex);
       cboStateProv.options[nCurrentIndex] = new Option (oneJsState.name, oneJsState.PrimaryKey);
       nCurrentIndex++;
    }
    onChangeStateProv(thePicker)
}
function getSelectedProvince(thePicker)
{
    var theJsCountry = getSelectedCountry(thePicker);
//alert('theJsCountry=' + theJsCountry.name)
    var strStateDomId = thePicker.StateComboDomId;
//alert('strStateDomId=' + strStateDomId)
    var cboStateProv = document.getElementById (strStateDomId)
    var nSelIndex = cboStateProv.selectedIndex;
//alert('nSelIndex=' + nSelIndex)
    //var nStateId = cboStateProv.options[nSelIndex].value;
//alert('nStateId=' + nStateId)
    var theJsState = theJsCountry.getState(nSelIndex);
//alert('state=' + theJsState.name)
    return theJsState;
}
function onChangeStateProv(thePicker)
{
//alert('onChangeStateProv DynaFetchUrl=' + thePicker.DynaFetchUrl)
    var theJsState = getSelectedProvince(thePicker);
//alert('state=' + theJsState.name)
    if (! theJsState.IsPopulated)
    {
      theJsState.dynaPopulate();
    }
    var strCityDomId = thePicker.CityComboDomId;
    var cboCity = document.getElementById (strCityDomId)
    cboCity.options.length = 0; // blank  out the city combo

    // populate the cities
    var nCities = theJsState.CityCount();
    var nCurrentIndex = 0;
    while (nCurrentIndex < nCities)
    {
       var oneJsCity = theJsState.getCity(nCurrentIndex);
//alert('city ' + oneJsCity.name + ' = ' + oneJsCity.PrimaryKey )
       
       cboCity.options[nCurrentIndex] = new Option (oneJsCity.name, oneJsCity.PrimaryKey);
       nCurrentIndex++;
    }
}
function getSelectedCity(thePicker)
{
    var theJsState = getSelectedProvince(thePicker);
    var strCityDomId = thePicker.CityComboDomId;
    var cboCity = document.getElementById (strCityDomId)
    var nCityId = cboCity.selectedIndex;
    var theJsCity = theJsState.getCity(nCityId);
    return theJsCity;
}
function onChangeCity(thePicker)
{
    var theJsCity = getSelectedCity(thePicker);

    if (! theJsCity.IsPopulated)
    {
      theJsCity.dynaPopulate();
    }
    var strZipComboDomId = thePicker.ZipComboDomId;
    var cboZip = document.getElementById (strZipComboDomId)
    cboZip.options.length = 0; // blank  out the city combo

    // populate the cities
    var nZips = theJsCity.PostalCount();
    var nCurrentIndex = 0;
    while (nCurrentIndex < nZips)
    {
       var oneJsZip = theJsCity.getPostal(nCurrentIndex);
//alert('city ' + oneJsCity.name + ' = ' + oneJsCity.PrimaryKey )
       
       cboZip.options[nCurrentIndex] = new Option (oneJsZip.name, oneJsZip.PrimaryKey);
       nCurrentIndex++;
    }
}
