if (document.getElementById && document.createTextNode) {
  addEvent(window, "load", initForm);
}

window.dhtmlHistory.create({
  toJSON: function(o) {
    return JSON.stringify(o);
  },
	fromJSON: function(s) {
  	return JSON.parse(s);
  }
});
function initForm() {
	if (document.forms["clubSelect"]) {
    if (JLTA.XMLRequest.XMLRequest()) {
      var req = new JLTA.XMLRequest.request();
			
		  dhtmlHistory.initialize();
		  dhtmlHistory.addListener(historyChange);
			if (historyStorage.hasKey("selection")) {
				document.forms["clubSelect"].selection = historyStorage.get("selection");
			} else {
				document.forms["clubSelect"].selection = new selection();
				if (document.forms["clubSelect"]["state"].value != "") {//if form is already filled, populate cached selection
					document.forms["clubSelect"].selection.state = document.forms["clubSelect"]["state"].value;
					if (document.forms["clubSelect"]["club"].value != "") {
						document.forms["clubSelect"].selection.club = document.forms["clubSelect"]["club"].value;
					}
				}
				historyStorage.put("selection", document.forms["clubSelect"].selection);
			}
			
			document.forms["clubSelect"]["loadClubs"].style.display = "none";
      document.forms["clubSelect"]["club"].disable = function () {
        this.disabled = true;
        this.length = 1;
      };
			document.forms["clubSelect"]["state"].onload = function () {
	      if (this.value != "Choose") {
					this.form["club"].disabled = 
					this.form["proceed"].disabled = 
					this.form["clubId"].disabled = false;
			  	req.handler = function(){
			  		JLTA.XMLRequest.fillSelect(document.forms["clubSelect"]["club"], req.data.getElementsByTagName("Club"), "ID", "COMPANY", true);
						if (document.forms["clubSelect"].selection.club != null && document.forms["clubSelect"].selection.club != "") {
							document.forms["clubSelect"]["club"].value = document.forms["clubSelect"].selection.club;
						}
			  	}
			  	req.load("livequery.asmx/LoadClubsByState?s=" + this.value);
			  } else {
					this.form["club"].disable();
					this.form["proceed"].disabled = 
					this.form["clubId"].disabled = true;
				}
			};
			document.forms["clubSelect"]["state"].onchange = function(){
				this.form.selection.state = this.value;
				historyStorage.put("selection", this.form.selection);
	      this.form["club"].disable();
				this.form.selection.club = null;
	      this.onload();
		  }
		 	document.forms["clubSelect"]["club"].onchange = function () {
				this.form.selection.club = this.value;
				historyStorage.put("selection", this.form.selection);
			};
			//load stored values
			if (!dhtmlHistory.isFirstLoad()) {
				document.forms["clubSelect"]["state"].value = document.forms["clubSelect"].selection.state;
				document.forms["clubSelect"]["state"].onload();
			}
		}
	}
}

var historyChange = function(newLocation, historyData) {
//nothing to do
}

//stored form selection values
function selection () {
  var state, club;
}