// Zips - Find the zip code
function findZipCode(cityId) {

	if (!xmlHttp) {
		xmlHttp = createXMLHttpRequestObject();
	}

	// Make sure xml object exists
	if (xmlHttp) {
		// add field to check array
		if (cityId) {
			// add to cache array
			cache.push(cityId);
		}
		try {
			if ((xmlHttp.readyState == 4 || xmlHttp.readyState==0) && cache.length>0) {
				var cacheEntry = cache.shift();
				xmlHttp.open("GET", "http://www.shipozim.co.il/index.php?design=xml&module=index&submodule=zips&task=view&id=" + cacheEntry, true);
				xmlHttp.onreadystatechange = handleZipStateChange;
				xmlHttp.send(null);
			}
		} catch (e) {
			displayError(e.toString());
		}
	}
}

// function will enter the real function, when ajax object is done
function handleZipStateChange() {
	if (xmlHttp.readyState==4) {
		if (xmlHttp.status==200) {
			try {
				readZipResponse();
			} catch (e) {
				displayError(e.toString());
			}
		}
	}
}

function readZipResponse() {
	var response = xmlHttp.responseText;
	if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0) {
		throw (response.length == 0 ? "Server error." : response);	
	}
	
	// get response in XML format (assume the response is valid XML)
	responseXml = xmlHttp.responseXML.documentElement;
	stripWhiteSpace(responseXml);

	if (responseXml.childNodes.length) {
		var zipCode = responseXml.getElementsByTagName("zipcode")[0].firstChild.data;
		var cityName = responseXml.getElementsByTagName("cityName")[0].firstChild.data;
		
	}
		

	document.getElementById("cityName").innerHTML = cityName;
	document.getElementById("zipCode").innerHTML = zipCode;

	var responseDiv = document.getElementById("responseDiv");
	responseDiv.style.display = "";	
	
	setTimeout("findZipCode();", 100);
	
}

function enterZipCode() {
	window.opener.document.getElementById('zipcode').value = document.getElementById("zipCode").innerHTML;
	window.opener.document.getElementById("cityName").innerHTML = document.getElementById("cityName").innerHTML;
	window.close();
}

// Zips - Find the zip code
function findCity(zipCode) {

	if (!xmlHttp) {
		var xmlHttp = createXMLHttpRequestObject();
	}

	// Make sure xml object exists
	if (xmlHttp) {
		// add field to check array
		if (zipCode) {
			// add to cache array
			cache.push(zipCode);
		}
		try {
			if ((xmlHttp.readyState == 4 || xmlHttp.readyState==0) && cache.length>0) {
				var cacheEntry = cache.shift();
				xmlHttp.open("GET", "http://www.shipozim.co.il/index.php?design=xml&module=index&submodule=zips&task=city&id=" + cacheEntry, true);
				xmlHttp.onreadystatechange = handleCityStateChange;
				xmlHttp.send(null);
			}
		} catch (e) {
			displayError(e.toString());
		}
	}
}

// function will enter the real function, when ajax object is done
function handleCityStateChange() {
	if (xmlHttp.readyState==4) {
		if (xmlHttp.status==200) {
			try {
				readCityResponse();
			} catch (e) {
				displayError(e.toString());
			}
		}
	}
}

function readCityResponse() {
	var response = xmlHttp.responseText;
	if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0) {
		throw (response.length == 0 ? "Server error." : response);	
	}
	
	// get response in XML format (assume the response is valid XML)
	responseXml = xmlHttp.responseXML;
	xmlDoc = responseXml.documentElement;
	var zipCode = xmlDoc.getElementsByTagName("zipcode")[0].firstChild.data;
	var cityName = xmlDoc.getElementsByTagName("cityName")[0].firstChild.data;
		

	document.getElementById("cityName").innerHTML = cityName;
	
	setTimeout("findCity();", 100);
	
}
