
var regionsToCodes = new Array();
regionsToCodes["North East"] = "offersNE.jsp";
regionsToCodes["South East"] = "offersSE.jsp";
regionsToCodes["Western"] = "offersWE.jsp";
regionsToCodes["South Central"] = "offersSC.jsp";
/*regionsToCodes["North Central"] = "offersNC.jsp";*/
regionsToCodes["North Central - East"] = "offersNCE.jsp";
regionsToCodes["North Central - West"] = "offersNCW.jsp";

var codesToRegions = new Array();
codesToRegions["we"] = "Western";
codesToRegions["ne"] = "North East";
codesToRegions["se"] = "South East";
codesToRegions["nc"] = "North Central";
codesToRegions["sc"] = "South Central";
codesToRegions["nce"] = "North Central - East";
codesToRegions["ncw"] = "North Central - West";

var regionData  = null;
/*
//http://www.biasecurities.com/blogs/jim/archive/2005/04/28/1794.aspx
function AttachEvent(elementObj, eventName, eventHandlerFunctionName)
{
	if (elementObj.addEventListener) { // Non-IE browsers
		elementObj.addEventListener(eventName, eventHandlerFunctionName, false);
	} else if (elementObj.attachEvent) { // IE 6+
		elementObj.attachEvent('on' + eventName, eventHandlerFunctionName);
	} else { // Older browsers
		var currentEventHandler = elementObj['on' + eventName];
		if (currentEventHandler == null) {
			elementObj['on' + eventName] = eventHandlerFunctionName;
		} else {
			elementObj['on' + eventName] = function(e) { currentEventHandler(e); eventHandlerFunctionName(e); }
		}
	}
}
*/
function drawStateSelectBox(newRD) {
	if (regionData == null) regionData = newRD;
	var stateArr = regionData.getStates();
	var stateSelectBox =  $('state');

	stateSelectBox.length = 0;	// reset the list
	
	stateSelectBox[stateSelectBox.length] = new Option("Select a State", null);
	for (var i = 0; i < stateArr.length; i++) {
		stateSelectBox[stateSelectBox.length] = new Option(stateArr[i].getName(), stateArr[i].getAbbreviation());
		if (typeof deepState != "undefined") {
			if (deepState == stateArr[i].getAbbreviation()) {
				stateSelectBox[stateSelectBox.length - 1].selected = true;
				var stateObj = regionData.getState(deepState);
				var arrCounty = regionData.getCounties(deepState);
				if (arrCounty.length > 0) {
					$("countyarea").style.display = "block";
					drawCountySelectBox(arrCounty);
				}
			}
		}
	}
}

function drawCountySelectBox(arrCounty) {
	
	var countySelectBox =  $('county');

	if (typeof(countySelectBox.options) != "undefined") {
		countySelectBox.options.length = 0;		// reset the list
	}
	
	countySelectBox[countySelectBox.length] = new Option("Select a County", null);

	for (var i = 0; i < arrCounty.length; i++) {
		countySelectBox[countySelectBox.length] = new Option(arrCounty[i].getName(), arrCounty[i].getName());
		if (typeof deepCounty != "undefined") {
			if (deepCounty == arrCounty[i].getName()) {
				countySelectBox[countySelectBox.length - 1].selected = true;
				var arrRegion = regionData.getRegions($("state").value, $("county").value);
				if (arrRegion.length > 1) {
					$("regionarea").style.visibility = "visible";
					$("regionarea").style.display = "block";
					drawRegionSelectBox(arrRegion);
				}
			}
		}
	}


}

function drawRegionSelectBox(arrRegion) {
	
	var regionSelectBox =  document.getElementById('regionoption');

	if (typeof(regionSelectBox.options) != "undefined") {
		regionSelectBox.options.length = 0;
	}
	
	regionSelectBox[regionSelectBox.length] = new Option("Select a region", null);

	for (var i = 0; i < arrRegion.length; i++) {
		regionSelectBox[regionSelectBox.length] = new Option(arrRegion[i].getName(), arrRegion[i].getDescriptor());
		if (typeof deepRegion != "undefined") {
			if (codesToRegions[deepRegion] == arrRegion[i].getName()) {
				regionSelectBox[regionSelectBox.length - 1].selected = true;
			}
		}
	}


}




function selectState_onChange(event) {
	var target = event.target ? event.target : event.srcElement;

	var stateObj = regionData.getState(target.value);
	var arrCounty = regionData.getCounties(target.value);

	if (typeof(stateObj) == "undefined") {
		return;
	}

	$('regionarea').style.display = "none";
	
	// if this state has a list of counties, we need to display them
	if (arrCounty.length > 0) {
		$("countyarea").style.display = "block";
		//$("countyButton").className = "";
		drawCountySelectBox(arrCounty);

	} else {
		// otherwise, show the user the default region for this state
		var defaultRegion = regionData.getRegion(stateObj.getDefaultRegion());
		
		$("countyarea").style.display = "none";
		$("regionSelectionForm").action = regionsToCodes[defaultRegion.getName()];
		$("regionSelectionForm").submit();
		//debug(stateObj.getName() + " does not have incentives based on region.");
		//debug("-> Show default incentives - " + defaultRegion.getName());
	}

}

function selectCounty_onChange(event) {
	var target = event.target ? event.target : event.srcElement;

	//debug("county change: "+ target.value);

	var arrRegion = regionData.getRegions($("state").value, $("county").value);
	
	if (arrRegion.length < 1) {
		//this should never happen!
		return;
	}
	
	// if this county has a list of regions, we need to display them
	if (arrRegion.length > 1) {
		$("regionarea").style.visibility = "visible";
		$("regionarea").style.display = "block";
		//$("countyButton").className = "hide";
		drawRegionSelectBox(arrRegion);

	} else {
		// otherwise, show the user the data for their region
		$("regionarea").style.display = "none";

		var regionObj = arrRegion[0];
		$("regionSelectionForm").action = regionsToCodes[regionObj.getName()];
		$("regionSelectionForm").submit();
		

		//debug("This county has a single region.");
		//debug("-> Show incentives - " + regionObj.getName() );

	}


}
function selectRegion_onChange(event) {
	$("regionSelectionForm").action =  regionsToCodes[$("regionoption").value];
	$("regionSelectionForm").submit();
}

function selectYear_onChange(event) {
	if (typeof deepRegion != "undefined") {
		if (deepRegion != "") {
			$("regionSelectionForm").submit();
		}
	}
}

// as soon as our form is ready, 
function initSelectStateValues (eventArgs) {
	//resetDebugWindow();

	AttachEvent(document.getElementById('state'), "change", selectState_onChange);
	AttachEvent(document.getElementById('county'), "change", selectCounty_onChange);
	AttachEvent(document.getElementById('regionoption'), "change", selectRegion_onChange);
	AttachEvent(document.getElementById('modelYear'), "change", selectYear_onChange);
	regionData = new pdRegionData(drawStateSelectBox);	// init data, passing callback function

}
	
AttachEvent(window, "load", initSelectStateValues);