var AddressFormUtil = new Object();
AddressFormUtil.onHandlers = new Array();
AddressFormUtil.domId = null;

AddressFormUtil.toggleCountryChange = function(select, prefix)
{
  var val = select.options[select.selectedIndex].value;
  if ((val == AddressFormUtil.domId) ||
      (val == "0") ||
      (!val))
  {
    document.getElementById(prefix+"_state_label_domestic").style.display = "inline";
    document.getElementById(prefix+"_state_label_intl").style.display = "none";
    document.getElementById(prefix+"_state_input_domestic").style.display = "block";
    document.getElementById(prefix+"_state_input_intl").style.display = "none";
    select.form[prefix+"_state_other"].value = "";
  }
  else
  {
    document.getElementById(prefix+"_state_label_domestic").style.display = "none";
    document.getElementById(prefix+"_state_label_intl").style.display = "inline";
    document.getElementById(prefix+"_state_input_domestic").style.display = "none";
    document.getElementById(prefix+"_state_input_intl").style.display = "block";
    select.form[prefix+"_state_id"].options[0].selected = true;
  }
}

AddressFormUtil.copyShipVal = function(elSrc, isShipSrc, elType)
{
  var i, c, elTgt, tgtElName;
  if (elSrc.form.bill_is_ship.checked)
  {
    if (isShipSrc)
    {
      tgtElName = elSrc.name.replace(/^ship_/, "bill_");
    }
    else
    {
      tgtElName = elSrc.name.replace(/^bill_/, "ship_");
    }

    for (i = 0, c = elSrc.form.elements.length; i < c; i++)
    {
      elTgt = elSrc.form.elements[i];
      if (elTgt.name == tgtElName)
      {
        if (elType == "text")
        {
          if (isShipSrc)
          {
            elTgt.value = elSrc.value;
          }
          else
          {
            elSrc.value = elTgt.value;
          }
        }
        else if (elType == "select")
        {
          if (isShipSrc)
          {
            elTgt.options[elSrc.selectedIndex].selected = true;
          }
          else
          {
            elSrc.options[elTgt.selectedIndex].selected = true;
          }
        }
        return;
      }
    }
  }
}


AddressFormUtil.init = function(form)
{
  var i, c, el;
  for (i = 0, c = form.elements.length; i < c; i++)
  {
    el = form.elements[i];
    switch (el.name)
    {
      case 'ship_name_first':
      case 'ship_name_last':
      case 'ship_co_name':
      case 'ship_attn':
      case 'ship_addr1':
      case 'ship_addr2':
      case 'ship_city':
      case 'ship_zip':
      case 'ship_state_other':
        if (el.onchange)
        {
          AddressFormUtil.onHandlers[el.name] = el.onchange;
        }
        el.onchange = AddressFormUtil.onTextShipChange;
        AddressFormUtil.copyShipVal(el, true, "text");
        break;

      case 'bill_name_first':
      case 'bill_name_last':
      case 'bill_co_name':
      case 'bill_attn':
      case 'bill_addr1':
      case 'bill_addr2':
      case 'bill_city':
      case 'bill_zip':
      case 'bill_state_other':
        if (el.onchange)
        {
          AddressFormUtil.onHandlers[el.name] = el.onchange;
        }
        el.onchange = AddressFormUtil.onTextBillChange;
        break;

      case 'ship_country_id':
        AddressFormUtil.toggleCountryChange(el, "ship");
      case 'ship_state_id':
        if (el.onchange)
        {
          AddressFormUtil.onHandlers[el.name] = el.onchange;
        }
        el.onchange = AddressFormUtil.onSelShipChange;
        AddressFormUtil.copyShipVal(el, true, "select");
        break;

      case 'bill_country_id':
        AddressFormUtil.toggleCountryChange(el, "bill");
      case 'bill_state_id':
        if (el.onchange)
        {
          AddressFormUtil.onHandlers[el.name] = el.onchange;
        }
        el.onchange = AddressFormUtil.onSelBillChange;
        break;

      case 'bill_is_ship':
        if (el.onclick)
        {
          AddressFormUtil.onHandlers[el.name] = el.onclick;
        }
        el.onchange = AddressFormUtil.onShipBillChange;
        break;
    }
  }
}

AddressFormUtil.onTextShipChange = function()
{
  var h;
  AddressFormUtil.copyShipVal(this, true, "text");

  if (AddressFormUtil.onHandlers[this.name])
  {
    h = this.onchange;
    this.onchange = AddressFormUtil.onHandlers[this.name];
    this.onchange();
    this.onchange = h;
  }  
}

AddressFormUtil.onTextBillChange = function()
{
  var h;
  /* AddressFormUtil.copyShipVal(this, false, "text"); */
  this.form.bill_is_ship.checked = false;

  if (AddressFormUtil.onHandlers[this.name])
  {
    h = this.onchange;
    this.onchange = AddressFormUtil.onHandlers[this.name];
    this.onchange();
    this.onchange = h;
  }  
}

AddressFormUtil.onSelShipChange = function()
{
  var h;
  AddressFormUtil.copyShipVal(this, true, "select");

  if (this.name == "ship_country_id")
  {
    AddressFormUtil.toggleCountryChange(this, "ship");
    if (this.form.bill_is_ship.checked)
    {
      AddressFormUtil.toggleCountryChange(this.form.bill_country_id, 'bill');
    }
  }

  if (AddressFormUtil.onHandlers[this.name])
  {
    h = this.onchange;
    this.onchange = AddressFormUtil.onHandlers[this.name];
    this.onchange();
    this.onchange = h;
  }  
}


AddressFormUtil.onSelBillChange = function()
{
  var h;
  /* AddressFormUtil.copyShipVal(this, false, "select"); */
  this.form.bill_is_ship.checked = false;

  if (this.name == "bill_country_id")
  {
    AddressFormUtil.toggleCountryChange(this, 'bill');
  }

  if (AddressFormUtil.onHandlers[this.name])
  {
    h = this.onchange;
    this.onchange = AddressFormUtil.onHandlers[this.name];
    this.onchange();
    this.onchange = h;
  }  
}


AddressFormUtil.onShipBillChange = function()
{
  var h, i, c, el, billElName;
  if (this.checked)
  /* Copy the shipping value to billing address */
  {
    for (i = 0, c = this.form.elements.length; i < c; i++)
    {
      el = this.form.elements[i];
      if (el.name.match(/^ship_/) &&
          (el.name != 'ship_is_bill'))
      {
        if ((el.name == "ship_country_id") ||
            (el.name == "ship_state_id"))
        {
          AddressFormUtil.copyShipVal(el, true, "select");
          if (el.name == "ship_country_id")
          {
            AddressFormUtil.toggleCountryChange(this.form.bill_country_id, 'bill');
          }
        }
        else
        {
          AddressFormUtil.copyShipVal(el, true, "text");
        }
      }
    }
  }

  if (AddressFormUtil.onHandlers[this.name])
  {
    h = this.onclick;
    this.onclick = AddressFormUtil.onHandlers[this.name];
    this.onclick();
    this.onclick = h;
  }  
}
