/*
Page: postcodelookup.js
Created: 25-Apr-2007
Created by: Tony Sanson
Summary:
Set of JavaScript functions for handling the Ajax communications between the web site and the Hopewiser postcode lookup .NET application.

Related Files:
addressform.xsl

Change Control
[1.0] 2007-04-25 TJS - Original file creation pre-release
'*' version note [1.1] 13/03/2008 CaserID:2008-01-3039
*/

var g_Num = null;   // Global variable set to which address form we are using when there are multiple forms on the page (e.g. account address and delivery address)

function postcodeLookup(sPcode, num) {
	var sPostcode, sPropertyNumber;

	if (typeof(num)!="undefined") {
		g_Num = num;
	}

	// If we weren't passed a postcode, get it from the form fields
	if (typeof(sPcode)=="undefined" || sPcode=="") {
		sPostcode = document.getElementById("postcodeinput"+g_Num).value;
		sPropertyNumber = document.getElementById("housenumberinput"+g_Num).value;
	} else {
		sPostcode = sPcode;
		sPropertyNumber = "";
	}

	if (sPostcode!="") {
		// Add the property number if given
		if (sPropertyNumber!="") {
			sPostcode += "," + sPropertyNumber;
		}

		//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		// For testing - if we are drilling down to a single address set to true to use fixed XML file to avoid using up clicks
		if (false) {
			if (sPostcode.indexOf("+")!=-1) {
				var sData;
				sData = "<input xtype='hidden' id='hw_count' value='1' />"
				sData += "<input type='text' id='hw_address_line1' value='test address line1' />"
				sData += "<input type='text' id='hw_address_line2' value='test address line2' />"
				sData += "<input type='text' id='hw_address_line3' value='test address line3' />"
				sData += "<input type='text' id='hw_town' value='test town' />"
				sData += "<input type='text' id='hw_county' value='test county' />"
				sData += "<input type='text' id='hw_postcode' value='test postcode' />"
				sData += "<input type='text' id='hw_country' value='UNITED KINGDOM' />"
				hidePostcodeEntry();
				processReturnedAddress("", sData);
			} else {
				setTimeout("doPostcodeLookup('" + sPostcode + "');", 1);
				hidePostcodeEntry();
				showAddressList();
				showData("Please wait...");
			}
		} else {
			setTimeout("doPostcodeLookup('" + sPostcode + "');", 1);
			hidePostcodeEntry();
			showAddressList();
			showData("Please wait...");
		}
		//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	} else {
		document.getElementById("postcodeinput"+g_Num).focus();
	}
}

function doPostcodeLookup(sPostcode) {
	sPostcode = escape(sPostcode);
	// Escape the "+" signs (they don't get converted by the escape() function)
	sPostcode = sPostcode.replace(/\+/gi, "%2B");

	// Call the Ajax processor
	var sUrl = "/pages/hopewiser/ajax/postcodelookup.aspx?mode=lookuphtml&pcode=" + sPostcode + "&num=" + g_Num;
	var ajax = new AjaxDelegate(sUrl, processReturnedAddress);
	ajax.Fetch();
}

// Called on return from the Ajax call to get the postcode
function processReturnedAddress(url, strData) {
	var objXML, retval, oMatches, iMatches, oErr, sXSLFile;
	hideAddressList();
	// Copy the returned data into the <div> element
	showData(strData);
	// Check how many addresses we got back
	oMatches = document.getElementById("hw_count");
	if (oMatches==null) {
		iMatches = 0;
	} else {
		iMatches = oMatches.value;
	}

	if (iMatches==0) {
		// If an error occurred, show the postcode entry form again
		showPostcodeEntry();
		showAddressList();
	} else if (iMatches==1) {
		// If we got a single result back, copy the fields from the XSL translator to the address form
		copyToForm();
		showAddressForm();
	} else {
		// Multiple addresses returned, so show them
		showAddressList();
	}
}

function copyToForm() {
	var oForm;
	// Assume we are on the profiles or checkout page
	oForm = document.forms["frmUserAddress"];
	if (oForm==null) {
		// If we couldn't find the form, assume the catalogue request page
		oForm = document.forms["frmCatRequest"];
		if (oForm==null) {
			// If we couldn't find the form, assume the address.xsl page
			if (g_Num==1) {
				oForm = document.forms["frmAccount"];
			} else {
				oForm = document.forms["frmDelivery"];
			}
		}
	}

	oForm.elements["ad_gi_address_line1"].value = document.getElementById("hw_address_line1").value;
	oForm.elements["ad_gi_address_line2"].value = document.getElementById("hw_address_line2").value;
	oForm.elements["ad_gi_address_line3"].value = document.getElementById("hw_address_line3").value;
	oForm.elements["ad_gi_city"].value          = document.getElementById("hw_town").value;
	oForm.elements["ad_gi_region_name"].value   = document.getElementById("hw_county").value;
	oForm.elements["ad_gi_postal_code"].value   = document.getElementById("hw_postcode").value;

	// Set the country dropdown select box
	var sCountry = document.getElementById("hw_country").value.toLowerCase();
	var oCountry = oForm.elements["ad_ctrl_country"];
	for (var i=0; i<oCountry.length; i++) {
		if (oCountry.options[i].text.toLowerCase()==sCountry) {
			oCountry.selectedIndex = i;
			break;
		}
	}
}

function showData(sData) {
	document.getElementById("oAddressList"+g_Num).innerHTML = sData;
}

function hideAddressList(num) {
	if (typeof(num)=="undefined") {
		num = g_Num;
	} else {
		g_Num = num;
	}
	document.getElementById("oAddressList"+num).style.visibility = "hidden";
	document.getElementById("oAddressList"+num).style.display = "none";
}

function showAddressList(num) {
	if (typeof(num)=="undefined") {
		num = g_Num;
	} else {
		g_Num = num;
	}
	document.getElementById("oAddressList"+num).style.visibility = "";
	document.getElementById("oAddressList"+num).style.display = "block";
	document.getElementById("oAddressForm"+num).style.visibility = "hidden";
	document.getElementById("oAddressForm"+num).style.display = "none";
	if (document.getElementById("oAddressFormExtra")!=null) {
		document.getElementById("oAddressFormExtra").style.visibility = "hidden";
		document.getElementById("oAddressFormExtra").style.display = "none";
	}
}

function hidePostcodeEntry(num) {
	if (typeof(num)=="undefined") {
		num = g_Num;
	} else {
		g_Num = num;
	}
	document.getElementById("oEnterPostcode"+num).style.visibility = "hidden";
	document.getElementById("oEnterPostcode"+num).style.display = "none";
}

function showPostcodeEntry(num) {
	if (typeof(num)=="undefined") {
		num = g_Num;
	} else {
		g_Num = num;
	}
	document.getElementById("oEnterPostcode"+num).style.visibility = "";
	document.getElementById("oEnterPostcode"+num).style.display = "block";
	document.getElementById("oAddressForm"+num).style.visibility = "hidden";
	document.getElementById("oAddressForm"+num).style.display = "none";
	if (document.getElementById("oAddressFormExtra")!=null) {
		document.getElementById("oAddressFormExtra").style.visibility = "hidden";
		document.getElementById("oAddressFormExtra").style.display = "none";
	}
	document.getElementById("postcodeinput"+num).focus();
	//start[1.1]
	document.getElementById("spacer").style.height="55px";
	//end[1.1]
	
}

function showAddressForm(num) {
	if (typeof(num)=="undefined") {
		num = g_Num;
	} else {
		g_Num = num;
	}
	hidePostcodeEntry(num);
	hideAddressList(num);
	document.getElementById("oAddressForm"+num).style.visibility = "";
	document.getElementById("oAddressForm"+num).style.display = "block";
	if (document.getElementById("oAddressFormExtra")!=null) {
		document.getElementById("oAddressFormExtra").style.visibility = "";
		document.getElementById("oAddressFormExtra").style.display = "block";
	}
	//start[1.1]
	document.getElementById("spacer").style.height="116px";
	//end[1.1]
}
