function cJSCountriesList(C, CL, S, SL)
{
  this.CountriesList = null;
  this.Countries = C; 
  this.CountriesLookup = CL;
  this.States = S;
  this.StatesLookup = SL;
  this.StateIDGenerate = false;
  this.SelectStateMessage = 'Please Specify >>';
  this.UseDefinedStateMessage = false;
  this.ShowState = false;
  this.ShowStatesLookup = false;

  
  this.StateLookupRefresh = cJSCountriesList_StateLookupRefresh;
  this.StateRefresh = cJSCountriesList_StateRefresh;
  this.LookupFill = cJSCountriesList_LookupFill;
  this.LookupRefresh = cJSCountriesList_LookupRefresh;
  this.CountriesOnChange = cJSCountriesList_CountriesOnChange;
  this.Synchronize = cJSCountriesList_Synchronize;
}

function cJSCountriesList_StateLookupRefresh(ACountryID)
{
  if ((ACountryID < 0) || (this.CountriesList[ACountryID][0] == ''))
  {
    this.StatesLookup.length = 1;
    with (this.StatesLookup.options[0])
    {
      value = '';
      if (this.UseDefinedStateMessage)
        text = this.SelectStateMessage;
      else
      {
        text = 'Please Specify >>';
        this.StatesLookup.style.display = 'inline';
      }
      if (this.ShowState)
        this.States.style.display = 'inline';
      else
        this.States.style.display = 'none';
    }
    return;
  }
  else
    this.StatesLookup.length = this.CountriesList[ACountryID][2].length / 2 + 1;
  
  with (this.StatesLookup.options[0])
  {
    selected = true; 
    value = '';
    if ((this.CountriesList[ACountryID][3] == '') || (this.UseDefinedStateMessage))
      text  = this.SelectStateMessage;
    else
      text  = 'Select a ' + this.CountriesList[ACountryID][3] + '...';
  }  
  this.States.style.display = this.ShowState || (this.StatesLookup.length > 1) ? 'none' : 'inline';
  this.StatesLookup.style.display = this.ShowStatesLookup 
    || (this.StatesLookup.length > 1) 
    || (this.States.type == 'hidden') 
    ? 'inline' : 'none';
  
  for(var i = 0; i < this.CountriesList[ACountryID][2].length / 2; i++)
    with (this.StatesLookup.options[i+1])
    {
      value = this.CountriesList[ACountryID][2][2*i];
      text  = this.CountriesList[ACountryID][2][2*i + 1];
      selected = this.StateIDGenerate && (value == this.States.value)
        || !this.StateIDGenerate && (text == this.States.value);
    }
}

function cJSCountriesList_StateRefresh()
{
  if (this.StatesLookup.selectedIndex == 0)
  {
    if (this.CountriesList[this.CountriesLookup.selectedIndex][2].length == 0)
      this.States.style.display = 'inline';
    this.States.value = '';
  }
  else
  {
    this.States.style.display = 'none'; this.StatesLookup.style.display = 'inline';
    if (this.StateIDGenerate)
      this.States.value = this.StatesLookup.options[this.StatesLookup.selectedIndex].value;
    else
      this.States.value = this.StatesLookup.options[this.StatesLookup.selectedIndex].text;
  }
}

function cJSCountriesList_LookupFill(CLOption, CValue, i)
{
  with(CLOption) 
  {
    value = this.CountriesList[i][0];
    text  = this.CountriesList[i][1];
    if (value == CValue)
    {
      selected = true;
      this.StateLookupRefresh(this.CountriesLookup.selectedIndex); 
    }  
  }
}

function cJSCountriesList_LookupRefresh()
{
  this.CountriesLookup.length = this.CountriesList.length;
  for(var i = 0; i < this.CountriesList.length; i++)
    this.LookupFill(this.CountriesLookup.options[i], this.Countries.value, i); 
  
  this.StateLookupRefresh(this.CountriesLookup.selectedIndex);
  this.Synchronize();
} 

function cJSCountriesList_CountriesOnChange()
{
  this.Countries.value = this.CountriesLookup.value; 
  this.States.value = '';
  this.StateLookupRefresh(this.CountriesLookup.selectedIndex);
}

function cJSCountriesList_Synchronize()
{
  this.Countries.value = this.CountriesLookup.value;
  if (this.CountriesList[this.CountriesLookup.selectedIndex][2].length != 0)
    this.StateRefresh();
}

function StateInvalidate()
{
  var states = document.createElement("SELECT");
  states.name = this.states.name;
  states.className = "styled";
  states.tabIndex = this.states.tabIndex;

  var parent = this.states.parentNode;
  parent.innerHTML = "";

  if (this.states.div_select)  
    this.states.div_select.input.parentNode.removeChild(this.states.div_select.input);

  var oOption = document.createElement("OPTION");
  oOption.text = "STATE";
  oOption.value = "";
  oOption.selected = true;
  states.options.add(oOption);

  var index = this.selectedIndex;
  if (index < 0) index = 0;
  for (var i = 0; i < (this.JSCountriesList.CountriesList[index][2].length / 2); i++) {
    oOption = document.createElement("OPTION");
    LStateValue = this.JSCountriesList.CountriesList[index][2][i * 2 + 1];
    oOption.text = LStateValue;
    oOption.value = LStateValue;
    states.options.add(oOption);
    if (LStateValue == this.JSCountriesList.StateDefault)
      oOption.selected = true;
  }

  parent.appendChild(states);
  this.states = states;
  SCDropDown.div_select_init(this.states);
}

//var OrderForm = document.forms['OrderForm'];

function cJSCountriesList2_init()
{
  for (var i = 0; i < this.CountriesList.length; i++) {
    oOption = document.createElement("OPTION");
    oOption.text = this.CountriesList[i][1];
    oOption.value = this.CountriesList[i][0];
    this.CountriesSelect.options.add(oOption);  
    if (this.CountriesList[i][0] == this.CountryDefault)
      oOption.selected = true;
  }
    
    this.CountriesSelect.onchange();
}

function cJSCountriesList2(ACountriesList, ACoutriesSelect, AStatesSelect, ACountryDefault, AStateDefault)
{
  this.CountriesList = ACountriesList;
    this.CountriesSelect = ACoutriesSelect;
    this.StatesSelect = ACoutriesSelect;
    this.CountryDefault = ACountryDefault;
    this.StateDefault = AStateDefault;
    
    this.CountriesSelect.onchange = StateInvalidate;
    this.CountriesSelect.states = AStatesSelect;
    this.CountriesSelect.JSCountriesList = this;
    this.init = cJSCountriesList2_init;
}
