
function CountryStateControl(countryStateControl, context, countryCode, stateName) {
  this.browseStatesWindow = null;
  this.context = context == null ? "" : context;
  this.countryCode = document.getElementById(countryCode);
  this.countryStateControl = countryStateControl;
  this.stateName = document.getElementById(stateName);
  this.stateNameIFrame = null;
  this.stateNameLookupButton = null;
  this.init();
}

CountryStateControl.prototype.countryHasStates = function () {
  var cc = this.getCountryCode();
  return cc == "CA" || cc == "MX" || cc == "US";
}

CountryStateControl.prototype.closeWindow = function () {
  if (this.browseStatesWindow != null) {
    if (!this.browseStatesWindow.closed) {
      this.browseStatesWindow.close();
    }
    this.browseStatesWindow = null;
  }
}

CountryStateControl.prototype.getCountryCode = function () {
  return this.countryCode.options[this.countryCode.options.selectedIndex].value;
}

CountryStateControl.prototype.getStateName = function () {
  return this.stateName.value;
}

CountryStateControl.prototype.init = function () {
  this.createStateNameIFrame();
  this.createStateNameLookupButton();
  this.setStateNameLookupButtonVisibility();

  var _this = this;
  
  this.countryCode.onchange = function() {
    _this.closeWindow();
    _this.setStateNameLookupButtonVisibility();
  };

  this.stateName.onblur = function() {
    _this.verifyStateName();
  };

  this.stateNameLookupButton.onclick = function() {
    _this.closeWindow();
    _this.openWindow();
    return false;
  };

}

CountryStateControl.prototype.createStateNameIFrame = function () {
  this.stateNameIFrame = document.createElement("iframe");
  this.stateNameIFrame.style.display = "none";
  this.stateName.parentNode.insertBefore(this.stateNameIFrame, this.stateName.nextSibling);
}

CountryStateControl.prototype.createStateNameLookupButton = function () {
  this.stateNameLookupButton = document.createElement("a");
  this.stateNameLookupButton.href = "#";

  var img = document.createElement("img");
  img.alt = "lookup";
  img.src = "/tksst/common/world/image/lookup.gif";
  img.style.border = "none";
  img.style.height = "20px";
  img.style.width = "21px";

  this.stateNameLookupButton.appendChild(img);

  this.stateName.parentNode.insertBefore(this.stateNameLookupButton, this.stateName.nextSibling);
}

CountryStateControl.prototype.openWindow = function () {
  var cc = this.getCountryCode();
  if (cc != "") {
    var url = "/tksst/common/world/browseStates.do" + this.getQuery();
    this.browseStatesWindow = window.open(url, "browseStates", "width=320,height=480,toolbar=no,scrollbars=yes,resizable=yes");
  }
}

CountryStateControl.prototype.verifyStateName = function () {
  var cc = this.getCountryCode();
  var sn = this.getStateName();
  if (cc != "" && sn != "") {
    var url = "/tksst/common/world/displayState.do" + this.getQuery();
    this.stateNameIFrame.src = url;
  }
}

CountryStateControl.prototype.getContext = function () {
  try {
    return this.context();
  } catch (e) {
  }
  return this.context;
}

CountryStateControl.prototype.getQuery = function () {
  return "?countryStateControl=" + escape(this.countryStateControl)
       + "&context=" + escape(this.getContext())
       + "&countryCode=" + escape(this.getCountryCode())
       + "&stateName=" + escape(this.getStateName());
}

CountryStateControl.prototype.setStateNameLookupButtonVisibility = function () {
  this.stateNameLookupButton.style.visibility = this.countryHasStates() ? "visible" : "hidden";
}

CountryStateControl.prototype.setState = function (state) {
  var stateName = state.stateName;
  this.stateName.value = stateName == null ? "" : stateName;
}
