/*
 * Makes an AJAX call to populate the 'geoSearch' element.
 * See 'showCurrentLocation' for behavior on return from AJAX call.
 * Parameters:
 * 		clientIpAddress - The IP address that is used for determining the search
 */
function getLocation(clientIpAddress) {
	var req = getAjaxObject();
	if (req == null) return;
	req.onreadystatechange = function() { showCurrentLocation(req); }
	var guid = getGuid();
	
	if (clientIpAddress == "174.46.164.254" || clientIpAddress == "172.27.51.23") {
		var geo = document.getElementById("geoSearch");
		geo.innerHTML="<span>Looking for schools near<br/></span><a href='/SearchSchools.do?location=1500&zip=45241&radius=25&spiral=true&spiralfield=RADIUS&steps=25,75,100'>Cincinnati, OH</a>"
	} else {
		var url = "/GetQuovaData.do?ip=" + clientIpAddress + "&guid=" + guid;
		//alert("url : " + url);
		req.open("GET", url, true);
		req.send(null);
	}
}

/*
 * AJAX callback function for getLocation(...)
 */
function showCurrentLocation(req) {
	try {
		if (req.status == 200) {
			var location = req.responseXML.getElementsByTagName("item");
			for (var i=0; i < location.length; i++) {
				var ihtml = null;
				try {
					ihtml=location[i].getElementsByTagName("ihtml")[0].firstChild.nodeValue;
				} catch(ignore) {
				}

				if (ihtml != null) {
					// default label
					ihtml = "<span>Featured article:<br/></span><a href='/articles/CV/campuslife/what_to_bring.html'>What to Bring to College</a>";

				} else {
					var city = location[i].getElementsByTagName("city")[0].firstChild.nodeValue;
					var stateName = location[i].getElementsByTagName("state")[0].firstChild.nodeValue.toUpperCase();
					var stateID = location[i].getElementsByTagName("stateID")[0].firstChild.nodeValue;
					var zip = location[i].getElementsByTagName("zip")[0].firstChild.nodeValue;

					var ihtml="<span>Looking for schools near<br/></span><a href='/SearchSchools.do?location=1500&zip=" + 
								zip + "&radius=25&spiral=true&spiralfield=RADIUS&steps=25,75,100'" +
								"onClick='document.body.style.cursor=\"wait\";'>" + city + ", " + stateName + "?";		
					/* older versions....
						var ihtml="<span>Looking for schools near<br/></span><a href='/GetSchoolsNear.do?zip=" + 
								zip + "&city=" + city +"&state=" +stateID + "' onClick='document.body.style.cursor=\"wait\";'>" + city + ",<br>" + stateName + "?";		
						var ihtml="Looking for schools near<br/><BR/><a href='/GetSchoolsNear.do?zip=59923&state=35&city=Libby'>Libby MT</a>";
					*/
				}
				var geo = document.getElementById("geoSearch");
				geo.innerHTML = ihtml;
			}
		}

	} catch(ignore) {
	}
}
