/*
// automate common address stuff.
*/

/*
// get zones based on country id
*/
ZoneLookup = Class.create();
ZoneLookup.cache = {};
ZoneLookup.loading = {};
ZoneLookup.objects = new Array();
ZoneLookup.prototype = {
  initialize: function(country,zoneInput,zoneSelect,options) {
    this.options = {
      preload: false,
      selectedZone: false,
      debugOutput: 'addrdebug',
      limitCountry: false,
      excludeZone: false,
      zipLookup: false
    };
    Object.extend(this.options, options || {});

    this.country    = $(country);
    this.zoneInput  = $(zoneInput);
    this.zoneSelect = $(zoneSelect);
    this.request    = false;
    this.waiting    = false;
    this.id         = ZoneLookup.objects.length;
    ZoneLookup.objects[this.id] = this;

    Event.observe(this.country,'change',this.onCountryChange.bind(this));
    Event.observe(this.zoneSelect,'change',this.onZoneChange.bind(this));

    // if zone box is empty, force a preload
    if (this.zoneSelect.options.length == 0) {
      this.options.preload = true;
    }
    // if we should look up zones right away
    if (this.options.preload) {
      this.onCountryChange();
    }
    // show the right input
    this.country_id = $F(this.country);
    this.showInput(true);
  },
  onZoneChange: function() {
    this.zoneInput.value = this.zoneSelect.options[this.zoneSelect.selectedIndex].name;
  },
  onCountryChange: function() {
    var cid = $F(this.country);
    this.country_id = cid;
    // first, disable the zone form elements
    var zsel = this.zoneSelect;
    var zinp = this.zoneInput;
    zsel.disabled = 1;
    zinp.disabled = 1;

    if (cid == 223 || cid == 38) {
      zsel.options.length = 0;
      zsel.options[0] = new Option('Loading...',0);
      this.showInput();
      this.waiting = true;
      if (ZoneLookup.loading[cid]) {
        this.debug("country "+cid+" is loading; zones will be filled in when request completes");
        // it will be filled in when the request completes
        return;
      }
      if (ZoneLookup.cache[cid]) {
        this.debug("populating from cache");
        this.populate(ZoneLookup.cache[cid]);
        return;
      }
      this.lookupZone(cid);
    } else {
      this.showInput(true);
      this.debug("Using text field for country "+cid);
      // this one just uses the text field method
    }
  },
  showInput: function(enable) {
    var cid = this.country_id;
    var good;
    var bad;
    if (cid == 223 || cid == 38) {
      good = this.zoneSelect;
      bad  = this.zoneInput;
    } else {
      good = this.zoneInput;
      bad  = this.zoneSelect;
    }
    Element.show(good);
    Element.hide(bad);
    bad.disabled = 1;
    if (enable) {
      good.disabled = '';
    }
  },
  lookupZone: function(cid) {
    ZoneLookup.loading[cid] = 1;
    this.debug("looking up country "+cid);
    var opt = {
      method: 'get',
      onSuccess: this.responseHandler.bind(this),
      onFailure: function(t) {
        alert("Some Error Looking up Zones for Country");
      },
      parameters: {
        action: 'zone',
        country_id: cid,
        exclude_zone: this.options.excludeZone
      }
    };
    this.request = new Ajax.Request('/xml/index.php',opt);
  },
  populate: function(json) {
    var zsel = this.zoneSelect;
    var zinp = this.zoneInput;
    zinp.value = '';
    this.debug("populating options");
    this.waiting = 0;
    zsel.options.length = 0;
    zsel.options[0] = new Option('-- choose --','0');
    for (var i = 0; i < json.length; i++) {
      zsel.options[zsel.options.length] = new Option(json[i].zone_name,json[i].zone_id);
    }
    if (this.options.zipLookup) {
      this.options.zipLookup.lookup();
    }
    zsel.disabled = 0;
  },
  responseHandler: function(t,json) {
    var cid = json[0].zone_country_id;
    this.debug("response handler called. country="+cid);
    ZoneLookup.cache[cid] = json;
    ZoneLookup.loading[cid] = 0;
    var objs = ZoneLookup.objects;
    for (var i = 0; i < objs.length; i++) {
      var zl = objs[i];
      if (zl.waiting && zl.country_id == cid) {
        zl.populate(json);
      }
    }
    this.request = false;
  },
  debug: function(str) {
    var o = this.options.debugOutput;
    if (!o) return;
    o.innerHTML += "<br><b>"+this.id+"</b>: "+str;
  }
};

ZipLookup = Class.create();
ZipLookup.cache = {};
ZipLookup.prototype = {
  initialize: function(zip,city,state,country) {
    this.zip = $(zip);
    this.city = $(city);
    this.state = $(state);
    if (country) {
      this.country = $(country);
    }
    this.busy = 0;
    Event.observe(this.zip,'change',this.lookup.bind(this));
  },
  lookup: function() {
    // ignore if they're not in the US
    if (this.country && ($F(this.country)+'' != '223')) return;
    var z = this.zip.value;
    if (z.length >= 5) {
      z = z.substring(0,5);
    }
    var opt = {
      method: 'get',
      onSuccess: this.responseHandler.bind(this),
      onFailure: this.fail.bind(this)
    };
    this.req = new Ajax.Request('/xml/index.php?action=zip&zip='+z,opt);
  },
  fail: function() {
    this.busy = 0;
  },
  responseHandler: function(t,json) {
    if (json.zip) {
      this.city.value = json.city;
      for (var i = 0; i < this.state.options.length; i++) {
        var v = this.state.options[i].value;
        if (v == json.id || v == json.code || v == json.name) {
          this.state.selectedIndex = i;
          break;
        }
      }
    }
  }
};

