var comboDependency;
function dependentCombos( combosArr ){
	comboDependency = new ComboDependency();
	for(var i=0;i<combosArr.length;i++){
		comboDependency.add(i,combosArr[i]);
	}
}
function addEvent(oTarget, sType, fpDest) {
	var oOldEvent = oTarget[sType];
	if (typeof oOldEvent != "function") {
		oTarget[sType] = fpDest;
	} else {
		oTarget[sType] = function(e) {
			oOldEvent(e);
			fpDest(e);
		}
	}
}
function initCombos(){
	dependentCombos([
		{id:"Counties",	nextView:"(Cities^County)", 	nextViewItem:"City"},
		{id:"Cities",		nextView:"(Streets^City)", 	nextViewItem:"Street"},
		{id:"Streets",		nextView:null, 		nextViewItem:null}
	]);
};
addEvent(window,"onload",function(){initCombos()});


/*
 *	ComboDependency
 * * * * * * * * * * * * * * * * * * * * * * */
function ComboDependency(){
 	this.combos = [];
 	this.iframe = null;
 	this.init();
}
ComboDependency.prototype.init = function(){this.buildIframe();}
ComboDependency.prototype.buildIframe = function(){var n = document.createElement("iframe");n.id = n.name = "ComboDependencyIframe";n.src = "javascript:void(0);";n.style.position = "absolute";n.style.left = n.style.top = "-100px";n.style.width = n.style.height = "10px";n.frameborder=0;document.body.appendChild(n);this.iframe = document.getElementById(n.id);}
ComboDependency.prototype.add = function(index,comboObj){this.combos.push(new DependentCombo(this,index,comboObj));}
ComboDependency.prototype.updateSiblings = function(index, resultObj){for(var i=index;i<this.combos.length;i++){this.combos[i].update((i==index)?resultObj:null);}}



/*
 *	DependentCombo
 * * * * * * * * * * * * * * * * * * * * * * */
function DependentCombo(supa,index,comboObj){
	this.supa = supa;
	this.index = index;
	this.comboObj = comboObj;
	this.domNode = null;
	this.previous = this.supa.combos[index-1]||null;
	this.init();
}
DependentCombo.prototype.init = function(){this.domNode = document.getElementById(this.comboObj.id);this.domNode.disabled = (this.previous)?true:false;this.domNode.defaultText = this.domNode.options[0].text;if(this.comboObj.nextView){this.domNode.supa = this;this.domNode.onchange = function(){if(this.selectedIndex==0){this.supa.supa.updateSiblings(this.supa.index+1);}else{if( nextCombo = this.supa.supa.combos[this.supa.index+1] ){nextCombo.domNode.options[0].text = "loading...";};this.supa.supa.iframe.src = "/Includes/GetOptions.jsp?index="+(this.supa.index+1)+"&view="+this.supa.comboObj.nextView+"&viewitem="+this.supa.comboObj.nextViewItem+"&key="+this.options[this.selectedIndex].value;}}}}
DependentCombo.prototype.update = function(resultsArr){this.removeOldOptions();if(resultsArr){this.domNode.disabled = false;this.addNewOptions(resultsArr);}else{this.domNode.disabled = true;}}
DependentCombo.prototype.removeOldOptions = function(){this.domNode.options[0].text = this.domNode.defaultText;if( startFrom = this.domNode.options[1] ){while(startFrom.nextSibling){buffNode = startFrom.nextSibling;this.domNode.removeChild(startFrom);startFrom = buffNode;}this.domNode.removeChild(startFrom);}}
DependentCombo.prototype.addNewOptions = function(resultsArr){for(var i=0;i<resultsArr.length;i++){var n = document.createElement("option");n.text = n.value = resultsArr[i];this.domNode.appendChild(n);}}