theURL = "http://" + document.location.host;

function resetAppCookie(name){
	var appCookie = getCookie("circApp")
	if (appCookie != null) {
		if (appCookie != name){
			deleteCookie("mapAOI")
			deleteCookie("mapParam")
			deleteCookie("selection")
			deleteCookie("selectionName")
		}
	}
	setCookie("circApp", name);
}

//Called from statelist.asp body onLoad event.
function initStateAOI() {
	//Clear all Cookies if STATUS=new
	var urlParam = document.location.search;
	if((urlParam == "") || (document.cookie.length == 0)){
		deleteCookie("mapAOI")
		deleteCookie("mapParam")
		deleteCookie("selection")
		deleteCookie("selectionName")

		updateCookie("mapAOI","AOIcurrent","0,0,0,0");
		updateCookie("mapParam","mapsize","full");
		updateCookie("mapParam","funclevel","1");

		//Always display 'Lakes & Ponds'
		setCookie("selection","hy1|hy1b|hy1c,");
		updateCookie("selectionName","hy1|hy1b|hy1c","Lakes and Ponds");
	}

	//Set the AOI cookie to 48 states if no state is currently selected.
	//Or set the AOI cookies to the currently selected state.
	if( extractCookie("mapAOI","AOItype") != "state" ){
		updateCookie("mapAOI","AOIvalue", extArray[0]);
		updateCookie("mapAOI","AOItext", "US (Lower 48 States)");
		updateAOIdataLayer("state");
	}else {
		var stateList = document.frmAOI.stateList;
		var aoiValue = extractCookie("mapAOI","AOItext");
		for (i=0; i<stateList.length; i++){
			if(stateList.options[i].text == aoiValue){
				stateList.selectedIndex = i;
				break;
			}
		}
	}
}


function updateAOIlist(){
	var stateList = document.frmAOI.stateList;
	var state = escape(stateList.options[stateList.selectedIndex].text);
	var url = window.location.href;

	var pos = url.indexOf("&STATE=");

	url = (pos > -1)? url.substring(0,pos): url;
	url = url + "&STATE=" + state;
	window.location.href = url;
}

function loadAOI(aoiType){
	//Set selected state
	if(document.frmAOI.stateList != null){
		for(i=0;i<document.frmAOI.stateList.length; i++){
			if(document.frmAOI.stateList.options[i].text == theState)
				document.frmAOI.stateList.selectedIndex = i;
		}
	}

	//Set selected item from the aoiList from the pulldown menu
	var resetState = true;
	var aoiList = document.frmAOI.aoiList;
	if( extractCookie("mapAOI","AOItype") == aoiType) {
		var aoiText = extractCookie("mapAOI","AOItext");
		var pos = aoiText.indexOf(", "+theState);
		var theSel = (pos >= 0)? aoiText.substring(0,pos): aoiText;
		for(i=0;i<aoiList.length; i++){
			if(aoiList.options[i].text == theSel){
				aoiList.selectedIndex = i;
				resetState = false;
				break;
			}
		}
	}else
		updateAOIdataLayer(aoiType);

	//Set the first item selected if this is the first time
	if(resetState && (aoiList.length > 0)){
		aoiList.selectedIndex = 0;

		var aoiText = ((aoiType == "county") && (theState == "District of Columbia"))? "": ", "+theState;

		updateCookie("mapAOI","AOItext",aoiList.options[0].text + aoiText);
		updateCookie("mapAOI","AOIvalue",extArray[0]);
	}
	
}

//Set Area of Interest with text and extent values.
function setAOI() {
	var aoiType = extractCookie("mapAOI","AOItype");

	var listObject = (aoiType == "state")? document.frmAOI.stateList: document.frmAOI.aoiList;

	var listIndex = listObject.selectedIndex;
	var aoiText = listObject.options[listIndex].text;
	aoiText = (aoiType == "state")? aoiText: (aoiText+", "+theState);

	var extValue = extArray[listIndex];

	updateCookie("mapAOI","AOIvalue",extValue);
	updateCookie("mapAOI","AOItext",aoiText);

	if(aoiText == "Chicago, Illinois")
		setDataCookie("chi_comm","Chicago Community Areas");

	document.location.href = "/step2.Data/administrative.asp" + document.location.search;
}

// Called from panellist.asp
function setAreaAOI(type,ext,id,name,obj){
	var text = obj.value;
	updateCookie("mapAOI","AOIvalue",ext);
	updateCookie("mapAOI","AOItext",text);
	updateCookie("mapAOI","AOItype",type);
	setDataCookie(id,name);
}

// Called from panellist.asp
function checkAreaAOI(type){
	if(extractCookie("mapAOI","AOItype") == type){
		var aoiTxt = extractCookie("mapAOI","AOItext");
		var radObj = document.forms[0].researchArea;
		var n = radObj.length;
		if (n == null){
			if(aoiTxt == radObj.value)
				radObj.checked = true;
		}else if(n > 1){
			for(var i=0; i<n; i++){
				if(aoiTxt == radObj[i].value)
					radObj[i].checked = true;
			}
		}
	}
}

//Called from initStateAOI() and the AOI pages
function updateAOIdataLayer(newType) {
	//Remove existing AOI data layer.
	var oldType = extractCookie("mapAOI","AOItype");
	oldType = (oldType == null)? "state": oldType;
	
	var theSelection = getCookie("selection");
	theSelection = (theSelection == null)? "": theSelection;
	
	var oldValue = "";
	var newValue = "ad1";


	//Get the data layer field values for the old and new AOI types.
	var aoiLayers = new Array();
	aoiLayers['state'] = "ad1,State Boundaries";
	aoiLayers['county'] = "ad2|ad2b,County Boundaries";
	aoiLayers['city'] = "ad3|ad3b,Cities and Towns";
	aoiLayers['senate'] = "pl3a|pl3a_1,State Senate Districts";
	aoiLayers['house'] = "pl4a|pl4a_1,State House Districts";
	aoiLayers['uscong'] = "pl1_110,US 110th Congressional Districts";
	
	for(var n in aoiLayers){
		var ids = aoiLayers[n].substring(0, aoiLayers[n].indexOf(","))

		if (oldType == n) oldValue = ids + ",";
		if (newType == n) {
			newValue = ids;
			var dataName = aoiLayers[n].substring(aoiLayers[n].indexOf(",")+1, aoiLayers[n].length)
		}
	}	

	//Replace the value in the 'selection' cookie.
	updateCookie("selectionName",newValue,dataName);
	newValue = newValue + ",";

	if((theSelection.indexOf(oldValue) > -1) && (oldValue != newValue)){
		if(theSelection.indexOf(newValue) == -1)
			theSelection = strReplace(theSelection, oldValue, newValue);
		else
			theSelection = strReplace(theSelection, oldValue, "");
	}else if(theSelection.indexOf(newValue) == -1)
		theSelection = theSelection + newValue;

	setCookie("selection", theSelection);
	updateCookie("mapAOI","AOItype", newType);
}

function openMap(url,w,h){
	w = w + 10;
	h = h + 10;
	var aWin = window.open(url,"mapW","width="+w+",height="+h+",left="+(screen.width-w-10)+",top=0");
}

function trim(string){
	while((string.length > 0) && (string.indexOf(" ") == 0))
		string = string.substring(1,string.length);

	while((string.length > 0) && (string.lastIndexOf(" ") == (string.length - 1)))
		string = string.substring(1,string.length -1);


	return string;
}


