if (!Hash)
{
   throw "Include Prototype.js";
}
var ClassStatesProvider =  Class.create({
   initialize: function()
   {
      this.statesTree= new Hash();
      this.countryTree = new Hash();
   },
   add: function(stateCode,countryCode )
   {
      this.statesTree.set(stateCode, countryCode);
      if (!this.countryTree.get(countryCode))
      {  
         this.countryTree.set(countryCode, new Array());
      }
      this.countryTree.get(countryCode).push(stateCode);
   },
   getCountry: function(stateCode)
   {
      return this.statesTree.get(stateCode);
   },
   getFirstState: function(countryCode)
   {
      var countryCode = this.countryTree.get(countryCode);
      return countryCode? countryCode[0]:"";
   }
});

/**
*
*/
function createComboPair(formName, stateComboName, countryName)
{
   var stateCombo = document.forms[formName].elements[stateComboName];
   var countryCombo = document.forms[formName].elements[countryName];
   
   stateCombo.onchange = stateEvent;
   countryCombo.onchange = countryEvent;
   
   stateCombo.contryReference = countryCombo;
   countryCombo.stateReference = stateCombo;
   
}
function stateEvent()
{
   countryCode = objStatesProvider.getCountry(getSelectedValue(this));
   selectOption(this.contryReference, countryCode);
}
function countryEvent()
{
   stateCode = objStatesProvider.getFirstState(getSelectedValue(this));
   selectOption(this.stateReference, stateCode);
}

function getSelectedValue(combobox)
{
   return combobox.options[combobox.selectedIndex].value;
}
function selectOption(combobox, optionValue)
{
   combobox.selectedIndex = 0;
   var options = $A(combobox.options);
   options.each(function (option,index)
   {
      if (option.value == optionValue)
      {
         combobox.selectedIndex = index;
         option.selected = true;
      }
   });
}

var objStatesProvider = new ClassStatesProvider();

