﻿
Type.registerNamespace('IMS.Address');

IMS.Address.StateDropDown = function() {
  IMS.Address.StateDropDown.initializeBase(this);

  this._comboID = null;
}

IMS.Address.StateDropDown.prototype =
{
  set_comboID: function(id) {
    this._comboID = id;
  },

  get_comboID: function() {
    return this._comboID;
  },

  get_combo: function() {
    return $find(this.get_comboID());
  },

  get_stateID: function() {
    return this.get_combo().get_value();
  },
  
  clearSelection: function() {
    this.get_combo().clearSelection();
  },

  OnClientBlur: function(sender, eventArgs) {
    var textInTheCombo = sender.get_text();
    var item = sender.findItemByText(textInTheCombo);
    if (!item) {
      sender.set_text("");
    }
  },

  DropDownOpening: function(sender, eventArgs) {
    var items = sender.get_items();
    var count = items.get_count();
    if (count < 5) {
      sender.requestItems('', false);
    }
  },

  initHandlers: function() {
    var c = this.get_combo();
    c.add_onClientBlur(this.OnClientBlur);
    c.add_dropDownOpening(this.DropDownOpening);
  }
}

IMS.Address.StateDropDown.registerClass('IMS.Address.StateDropDown', Sys.Component);

IMS.Address.CountyDropDown = function() {
  IMS.Address.CountyDropDown.initializeBase(this);

  this._comboID = null;
}

IMS.Address.CountyDropDown.prototype =
{
  set_comboID: function(id) {
    this._comboID = id;
  },

  get_comboID: function() {
    return this._comboID;
  },

  get_combo: function() {
    return $find(this.get_comboID());
  },

  get_county: function() {
    return this.get_combo().get_value();
  },
  
  clearSelection: function() {
    this.get_combo().clearSelection();
  },

  OnClientBlur: function(sender, eventArgs) {
    var textInTheCombo = sender.get_text();
    var item = sender.findItemByText(textInTheCombo);
    if (!item) {
      sender.set_text("");
    }
  },

  DropDownOpening: function(sender, eventArgs) {
    var items = sender.get_items();
    var count = items.get_count();
    if (count < 5) {
      sender.requestItems('', false);
    }
  },

  initHandlers: function() {
    var c = this.get_combo();
    c.add_onClientBlur(this.OnClientBlur);
    c.add_dropDownOpening(this.DropDownOpening);
  }
}

IMS.Address.CountyDropDown.registerClass('IMS.Address.CountyDropDown', Sys.Component);

IMS.Address.Address = function() {
  IMS.Address.Address.initializeBase(this);

  this._street1CtlID = null;
  this._street2CtlID = null;
  this._cityCtlID = null;
  this._stateCtlID = null;
  this._zipCodeCtlID = null;
  this._countyCtlID = null;

  this._countyToLoadOnComplete = null;
  this._stateToLoadOnComplete = null;
  this._zipCodeNotFoundPanel = null;
}

IMS.Address.Address.prototype =
{
  set_street1CtlID: function(id) {
    this._street1CtlID = id;
  },

  get_street1CtlID: function() {
    return this._street1CtlID;
  },

  set_street2CtlID: function(id) {
    this._street2CtlID = id;
  },

  get_street2CtlID: function() {
    return this._street2CtlID;
  },

  set_cityCtlID: function(id) {
    this._cityCtlID = id;
  },

  get_cityCtlID: function() {
    return this._cityCtlID;
  },

  set_stateCtlID: function(id) {
    this._stateCtlID = id;
  },

  get_stateCtlID: function() {
    return this._stateCtlID;
  },

  set_countyCtlID: function(id) {
    this._countyCtlID = id;
  },

  get_countyCtlID: function() {
    return this._countyCtlID;
  },

  set_zipCodeCtlID: function(id) {
    this._zipCodeCtlID = id;
  },

  get_zipCodeCtlID: function() {
    return this._zipCodeCtlID;
  },

  get_street1Ctl: function() {
    return $get(this._street1CtlID);
  },

  get_street2Ctl: function() {
    return $get(this._street2CtlID);
  },

  get_cityCtl: function() {
    return $get(this._cityCtlID);
  },

  get_stateCtl: function() {
    return $find(this._stateCtlID);
  },

  get_zipCodeCtl: function() {
    return $find(this.get_zipCodeCtlID());
  },

  get_countyCtl: function() {
    return $find(this._countyCtlID);
  },

  set_street1: function(value) {
    var s1 = this.get_street1Ctl();
    s1.value = value;
  },

  set_street2: function(value) {
    var s2 = this.get_street2Ctl();
    s2.value = value;
  },

  set_city: function(value) {
    var c = this.get_cityCtl();
    c.value = value;
  },

  set_countyToLoadOnComplete: function(c) {
    this._countyToLoadOnComplete = c;
  },

  get_countyToLoadOnComplete: function() {
    return this._countyToLoadOnComplete;
  },

  set_stateToLoadOnComplete: function(s) {
    this._stateToLoadOnComplete = s;
  },

  get_stateToLoadOnComplete: function() {
    return this._stateToLoadOnComplete;
  },

  set_stateAndCounty: function(stateID, countyName) {
    var cyl = this.get_countyCtl();
    var sl = this.get_stateCtl();
    var s = sl.get_combo();

    if (cyl) {
      var cy = cyl.get_combo();
      if (cy) {
        this.set_countyToLoadOnComplete(countyName);
      }
    }

    if (s) {
      var stateItem = s.findItemByValue(stateID);
      if (stateItem) {
        stateItem.select();
        s.commitChanges();
      }
      else {
        this.set_stateToLoadOnComplete(stateID + '');
        s.requestItems('', false);
      }
    }
  },

  set_zipCode: function(value) {
    var z = this.get_zipCodeCtl();
    z.set_value(value);
  },

  get_zipCode: function() {
    return this.get_zipCodeCtl().get_value();
  },

  set_zipCodeNotFoundPanel: function(pnl) {
    this._zipCodeNotFoundPanel = pnl;
  },

  get_zipCodeNotFoundPanel: function() {
    return this._zipCodeNotFoundPanel;
  },

  set_address: function(value) {
    if (value) {
      this.set_street1(value.Street1);
      this.set_street2(value.Street2);
      this.set_city(value.City);
      this.set_stateAndCounty(value.StateID, value.County);
      this.set_zipCode(value.ZipCode);
    }
  },

  clear: function() {
    this.set_street1('');
    this.set_street2('');
    this.set_city('');
    this.get_stateCtl().clearSelection();
    var c = this.get_countyCtl();
    if (c) {
      c.clearSelection();
    }
    this.set_zipCode('');
  },

  getCityStateCounty: function(sender, eventArgs) {
    var zipCode = sender.get_value();
    theschedulecenter.com.services.ajax.AddressWebService.GetCityStateCounty(zipCode,
     Function.createDelegate(this, this.onGetCityStateCountyComplete));
  },

  onGetCityStateCountyComplete: function(results) {

    var zipCodeNotFoundPanel = this.get_zipCodeNotFoundPanel();
    if (zipCodeNotFoundPanel) {
      zipCodeNotFoundPanel.className = "hide";
    }

    if (results != null) {
      if (results.Found) {
        this.set_city(results.City);
        this.set_stateAndCounty(results.State, results.County);
      }
      else {
        if (zipCodeNotFoundPanel) {
          zipCodeNotFoundPanel.className = "show";
        }
      }
    }
    else {
      if (zipCodeNotFoundPanel) {
        zipCodeNotFoundPanel.className = "show";
      }
    }
  },

  loadCounties: function() {
    var state = this.get_stateCtl();
    var countyCtl = this.get_countyCtl();
    var stateID = state.get_stateID();

    if (countyCtl) {
      var county = countyCtl.get_combo();
      if (county) {
        county.clearSelection();
        county.requestItems(stateID, false);
      }
    }
  },

  loadCountiesComplete: function() {
    var county = this.get_countyCtl().get_combo();
    var countyToLoad = this.get_countyToLoadOnComplete();
    if (countyToLoad != '') {
      var countyItem = county.findItemByValue(countyToLoad);
      if (countyItem) {
        countyItem.select();
        county.commitChanges();
      }
    }
    this.set_countyToLoadOnComplete('');
  },

  loadStatesComplete: function() {
    var stateToLoad = this.get_stateToLoadOnComplete();
    if (stateToLoad != '') {
      var s = this.get_stateCtl();
      var c = s.get_combo();
      stateItem = c.findItemByValue(stateToLoad);
      if (stateItem) {
        stateItem.select();
        c.commitChanges();
      }
    }

    this.set_stateToLoadOnComplete('');
  },

  initHandlers: function() {

    //$addHandler(this.get_zipCodeCtl(), "TextChanged", Function.createDelegate(this, this.getCityStateCounty));
    var zc = this.get_zipCodeCtl();
    if (zc) {
      zc.add_valueChanged(Function.createDelegate(this, this.getCityStateCounty));
    }

    var s = this.get_stateCtl();
    if (s) {
      var cbo = s.get_combo();
      if (cbo) {
        cbo.add_selectedIndexChanged(Function.createDelegate(this, this.loadCounties));
        cbo.add_itemsRequested(Function.createDelegate(this, this.loadStatesComplete));
      }
    }

    var c = this.get_countyCtl();
    if (c) {
      var ccbo = c.get_combo();
      if (ccbo) {
        ccbo.add_itemsRequested(Function.createDelegate(this, this.loadCountiesComplete));
      }
    }
  }
}

IMS.Address.Address.registerClass('IMS.Address.Address', Sys.Component);

Sys.Application.notifyScriptLoaded();