//
// cookies.js
//
// This file contains cookie handling javascript functions
// to be used to set and retrieve cookies related to geocoding
// functions in the City of Indianapolis ArcIMS applications.
//

// Global varaibles used by cookie functions.
var cookieLocType = "";
var cookieHN = "";
var cookiePD = "";
var cookieSN = "";
var cookieST = "";
var cookieSD = "";
var cookieZN = "";
var cookieP1 = "";
var cookieS1 = "";
var cookieT1 = "";
var cookieD1 = "";
var cookieP2 = "";
var cookieS2 = "";
var cookieT2 = "";
var cookieD2 = "";
var cookiePN = "";

function removeCookie(label){
	//alert("Removing "+ label);
	
	//Create the cookie expiration date.
	var exp = new Date();
	var oneYearAgo = exp.getTime() - (365 * 24 * 60 * 60 * 1000)
	exp.setTime(oneYearAgo)
	
	var strGeocodeCookie = label + "=junk"
	
	//Set the cookie with the path = "/"
	document.cookie = strGeocodeCookie + "; expires=" + exp.toGMTString() + "; domain=arcimsnt1.indygov.org; path=/";
	//document.cookie += "; expires=" + exp.toGMTString() ;
	//document.cookie += "; domain=arcimsnt1.indygov.org";
	//document.cookie += "; path=/";
}

function getCookieData(label) {
	var labelLen = label.length;
	var cLen = document.cookie.length;
	var i = 0;
	var cEnd
	while (i < cLen) {
		var j = i + labelLen;
		if (document.cookie.substring(i,j) == label) {
			cEnd = document.cookie.indexOf(";",j);
			if (cEnd == -1) {
				cEnd = document.cookie.length;
			}
			return unescape(document.cookie.substring(j, cEnd));
		}
		i++;
	}
	return ""
}

function setLocationCookies(locateForm) {
// Function name         – SetLocationCookies
// Description           – This function uses the controls in the 
//                         passed in form to create a cookie
//                         containing the last address and type of 
//                         address the user attempted to geocode.
// Usage                 – SetLocationCookies(locateForm)
// Parameters passed     – locateForm
// Return value          – NONE
// Function called by    –  
// Functions called      – parseGeocoderCookie()
// Global variables used – cookieLocType, cookieXX where XX is one of
//                         the geocoding components (HN, PD, SN, etc.)
// History               - 02/19/2002 Rick Petrecca - Original Coding
// Body of function –

	//Initialize variables for cookie components
	var strDocumentCookie;
	
	// Check for a current geocoder cookie.
	strDocumentCookie = getCookieData("GeocodeInfo")
	//alert("Cookie: \n"+strDocumentCookie);

	// If one exists, parse the cookie into variables.
	if (strDocumentCookie.length > 0) {
		parseGeocoderCookie(strDocumentCookie);
	}

	//alert("Number of form elements: " + locateForm.elements.length);
	
	// Determine what type of location is being geocoded.
	var strLocType;

	// If the locateForm contains a LocType radio Button then
	// loop through the form's elements array to find one or more LocType radio buttons
	var bolLocTypePresent = false;
	var i = 0;
	while (i < locateForm.elements.length) {
		//alert(locateForm.elements[i].name)
		if (locateForm.elements[i].name == "LocType") {
			bolLocTypePresent = true;
			break;
		}
		i++;
	}

	if (bolLocTypePresent) {
		//alert("Checking which LocType radio button is checked.");
		//Determine which radio button is selected
		for (var i = 0; i < locateForm.LocType.length; i++) {
			if (locateForm.LocType[i].checked) {
				//alert("Found checked LocType button");
				strLocType=locateForm.LocType[i].value;
			}
		}

	} else {
		if (locateForm.HN.value.length > 0) {
			strLocType = "address";
		} else { 
			if (locateForm.S1.value.length > 0) { 
				strLocType = "intersection";
			} else {
				if (locateForm.PN.value.length > 0) {
					strLocType = "parcel";
				}
			}
		} //end if
	} // end if
	
	//alert("LocType: " + strLocType);
	
	switch (strLocType) {
		case "address" :
			//alert("Setting address values");
			// Clear existing values
			cookieHN = "";
			cookiePD = "";
			cookieSN = "";
			cookieST = "";
			cookieSD = "";
			cookieZN = "";

			// Now set the values based on the locateForm input
			cookieHN = locateForm.HN.value;
			cookiePD = locateForm.PD.options[locateForm.PD.selectedIndex].value;
			cookieSN = locateForm.SN.value;
			cookieST = locateForm.ST.options[locateForm.ST.selectedIndex].value;
			cookieSD = locateForm.SD.options[locateForm.SD.selectedIndex].value;

			// Make sure ZN input exists
			var bolZNPresent = false;
			var i = 0;
			while (i < locateForm.elements.length) {
				//alert(locateForm.elements[i].name)
				if (locateForm.elements[i].name == "ZN") {
					bolZNPresent = true;
					break;
				}
				i++;
			}
			if (bolZNPresent) {
				cookieZN = locateForm.ZN.value;
			}
			break;
		case "intersection" :
			//alert("Setting intersection values");
			// Clear existing values
			cookieP1 = "";
			cookieS1 = "";
			cookieT1 = "";
			cookieD1 = "";
			cookieP2 = "";
			cookieS2 = "";
			cookieT2 = "";
			cookieD2 = "";

			// Now set the values based on the locateForm input
			cookieP1 = locateForm.P1.options[locateForm.P1.selectedIndex].value;
			cookieS1 = locateForm.S1.value;
			cookieT1 = locateForm.T1.options[locateForm.T1.selectedIndex].value;
			cookieD1 = locateForm.D1.options[locateForm.D1.selectedIndex].value;
			cookieP2 = locateForm.P2.options[locateForm.P2.selectedIndex].value;
			cookieS2 = locateForm.S2.value;
			cookieT2 = locateForm.T2.options[locateForm.T2.selectedIndex].value;
			cookieD2 = locateForm.D2.options[locateForm.D2.selectedIndex].value;
			break;
		case "parcel" :
			//alert("Setting Parcel values");
			// Clear existing values
			cookiePN = "";

			// Now set the values based on the locateForm input
			cookiePN = locateForm.PN.value;
			break;
	} // End switch

	// Build the cookie string.
	var strGeocodeCookie = "";
	strGeocodeCookie = strGeocodeCookie + "LocType=" + strLocType + "|";
	if (cookieHN.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "HN=" + cookieHN + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "HN=|";
	}
	if (cookiePD.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "PD=" + cookiePD + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "PD=|";
	}
	if (cookieSN.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "SN=" + cookieSN + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "SN=|";
	}
	if (cookieST.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "ST=" + cookieST + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "ST=|";
	}
	if (cookieSD.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "SD=" + cookieSD + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "SD=|";
	}
	if (cookieZN.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "ZN=" + cookieZN + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "ZN=|";
	}
	if (cookieP1.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "P1=" + cookieP1 + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "P1=|";
	}
	if (cookieS1.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "S1=" + cookieS1 + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "S1=|";
	}
	if (cookieT1.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "T1=" + cookieT1 + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "T1=|";
	}
	if (cookieD1.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "D1=" + cookieD1 + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "D1=|";
	}
	if (cookieP2.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "P2=" + cookieP2 + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "P2=|";
	}
	if (cookieS2.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "S2=" + cookieS2 + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "S2=|";
	}
	if (cookieT2.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "T2=" + cookieT2 + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "T2=|";
	}
	if (cookieD2.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "D2=" + cookieD2 + "|";
	} else {
		strGeocodeCookie = strGeocodeCookie + "D2=|";
	}
	if (cookiePN.length > 0) {
		strGeocodeCookie = strGeocodeCookie + "PN=" + cookiePN;
	} else {
		strGeocodeCookie = strGeocodeCookie + "PN=";
	}
	
	//alert("Cookie String: \n\n" + strGeocodeCookie);
	
	//Encode the cookie string.
	strGeocodeCookie = escape(strGeocodeCookie)
	strGeocodeCookie = "GeocodeInfo=" + strGeocodeCookie

	//Create the cookie expiration date.
	var exp = new Date();
	var oneYearFromNow = exp.getTime() + (365 * 24 * 60 * 60 * 1000)
	exp.setTime(oneYearFromNow)
	
	//Set the cookie with the path = "/"
	document.cookie = strGeocodeCookie + "; expires=" + exp.toGMTString() + "; domain=arcimsnt1.indygov.org; path=/";
	//document.cookie += "; expires=" + exp.toGMTString() ;
	//document.cookie += "; domain=arcimsnt1.indygov.org";
	//document.cookie += "; path=/"

}

function parseGeocoderCookie(strCookie){
// Function name         – parseGeocoderCookie
// Description           – This function parses the geocoderCookie 
//                         into its separate components.
// Usage                 – parseGeocoderCookie()
// Parameters passed     – strCookie
// Return value          – NONE
// Function called by    – SetLocationCookies(), RetrieveLocationCookies()
// Functions called      – NONE
// Global variables used – Global variables used – cookieLocType, cookieXX where XX
//                         is one of the geocoding components (HN, PD, SN, etc.)
// History               - 02/19/2002 Rick Petrecca - Original Coding
// Body of function –

	//Split the geocoderInfo cookie into an array based on a vertical bar separator
	var arrayCookie = strCookie.split("|");

	//Loop through the array
	for (var i = 0; i < arrayCookie.length; i++) {
		
		//Split the element into an array based on an equal sign separator
		arrayElement = arrayCookie[i].split("=")
		switch (arrayElement[0]) {
			case "LocType" :
				cookieLocType = arrayElement[1];
				break;
			case "HN" :
				cookieHN = arrayElement[1];
				break;
			case "PD" :
				cookiePD = arrayElement[1];
				break;
			case "SN" :
				cookieSN = arrayElement[1];
				break;
			case "ST" :
				cookieST = arrayElement[1];
				break;
			case "SD" :
				cookieSD = arrayElement[1];
				break;
			case "ZN" :
				cookieZN = arrayElement[1];
				break;
			case "P1" :
				cookieP1 = arrayElement[1];
				break;
			case "S1" :
				cookieS1 = arrayElement[1];
				break;
			case "T1" :
				cookieT1 = arrayElement[1];
				break;
			case "D1" :
				cookieD1 = arrayElement[1];
				break;
			case "P2" :
				cookieP2 = arrayElement[1];
				break;
			case "S2" :
				cookieS2 = arrayElement[1];
				break;
			case "T2" :
				cookieT2 = arrayElement[1];
				break;
			case "D2" :
				cookieD2 = arrayElement[1];
				break;
			case "PN" :
				cookiePN = arrayElement[1];
				break;
		} // End switch
	}
}

function retrieveLocationCookies(locateForm){
// Function name         – RetrieveLocationCookies
// Description           – This function sets the controls in the passed 
//                         in form from the set of cookies containing the
//                         last address and type of address the user
//                         attempted to geocode.
// Usage                 – retrieveLocationCookies(locateForm)
// Parameters passed     – locateForm
// Return value          – NONE
// Function called by    –  NONE
// Functions called      – parseGeocoderCookie()
// Global variables used – Global variables used – cookieLocType, cookieXX where XX
//                         is one of the geocoding components (HN, PD, SN, etc.)
// History               - 02/19/2002 Rick Petrecca - Original Coding
// Body of function –

	//alert("RetrieveLocationCookie");
	//Initialize variables for cookie components
	var strDocumentCookie;
	
	// Read cookie.
	strDocumentCookie = getCookieData("GeocodeInfo")
	//alert(strDocumentCookie);
	
	parseGeocoderCookie(strDocumentCookie);
	
	// Set value of LocType radio button
	// Check to see if the locateForm contains a LocType radio Button
	var bolLocTypePresent = false;
	var i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "LocType") {
			bolLocTypePresent = true;
			break;
		}
		i++;
	}
	if (bolLocTypePresent) {
		for (var i = 0; i <locateForm.LocType.length; i++) {
			if (locateForm.LocType[i].value == cookieLocType) {
				locateForm.LocType[i].checked = true;
			}
		}
	}

	//Set values of remaining inputs based on values in cookie variables.

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "HN") {
			bolPresent = true;
			break;
		}
		i++;
	}
	//alert(bolPresent+"\n"+cookieHN);
	if (bolPresent) {
		locateForm.HN.value = cookieHN;
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "PD") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		i = 0;
		while (i < locateForm.PD.options.length){
			if (locateForm.PD.options[i].value == cookiePD) {
				locateForm.PD.options[i].selected = true;
				break;
			}
			i++;
		}
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "SN") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		locateForm.SN.value = cookieSN;
	}
	
	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "ST") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		i = 0;
		while (i < locateForm.ST.options.length){
			if (locateForm.ST.options[i].value == cookieST) {
				locateForm.ST.options[i].selected = true;
				break;
			}
			i++;
		}
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "SD") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		i = 0;
		while (i < locateForm.SD.options.length){
			if (locateForm.SD.options[i].value == cookieSD) {
				locateForm.SD.options[i].selected = true;
				break;
			}
			i++;
		}
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "ZN") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		locateForm.ZN.value = cookieZN;
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "P1") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		i = 0;
		while (i < locateForm.P1.options.length){
			if (locateForm.P1.options[i].value == cookieP1) {
				locateForm.P1.options[i].selected = true;
				break;
			}
			i++;
		}
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "S1") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		locateForm.S1.value = cookieS1;
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "T1") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		i = 0;
		while (i < locateForm.T1.options.length){
			if (locateForm.T1.options[i].value == cookieT1) {
				locateForm.T1.options[i].selected = true;
				break;
			}
			i++;
		}
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "D1") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		i = 0;
		while (i < locateForm.D1.options.length){
			if (locateForm.D1.options[i].value == cookieD1) {
				locateForm.D1.options[i].selected = true;
				break;
			}
			i++;
		}
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "P2") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		i = 0;
		while (i < locateForm.P2.options.length){
			if (locateForm.P2.options[i].value == cookieP2) {
				locateForm.P2.options[i].selected = true;
				break;
			}
			i++;
		}
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "S2") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		locateForm.S2.value = cookieS2;
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "T2") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		i = 0;
		while (i < locateForm.T2.options.length){
			if (locateForm.T2.options[i].value == cookieT2) {
				locateForm.T2.options[i].selected = true;
				break;
			}
			i++;
		}
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "D2") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		i = 0;
		while (i < locateForm.D2.options.length){
			if (locateForm.D2.options[i].value == cookieD2) {
				locateForm.D2.options[i].selected = true;
				break;
			}
			i++;
		}
	}

	bolPresent = false;
	i = 0;
	while (i < locateForm.elements.length) {
		if (locateForm.elements[i].name == "PN") {
			bolPresent = true;
			break;
		}
		i++;
	}
	if (bolPresent) {
		locateForm.PN.value = cookiePN;
	}
}
