/*
-------------------------------------------------
       Epostcode JavaScript Interface 1.01
            17-Oct-2005 Etellect Ltd.
               www.etellect.co.uk

More information on this product can be found at:
         http://www.epostcode.com/
-------------------------------------------------
*/
var reqList;
var returnedXMLList = ""; 
var reqGetAddress; 
var returnedXMLGetAddress = ""; 
var pageQS = ""; 
var sServiceName = ""; 
var sAccountName = ""; 
var sGUID = ""; 
var sIPAddress = ""; 
var sSearchPostcode=""; 
var sSearchStreet=""; 
var sSearchCompany=""; 
var sSearchLocality=""; 
var sSearchTownOrCity=""; 
var sSearchNumber=""; 
var sWebServiceURL = "http://ws.epostcode.com/uk/addressfinder.asmx";
var shipWhere = '';

window.onload = function( ) {
	pageQS = new Querystring();
}

function GetAddressList( shipWhere, sSearchPostcode ) {
	var xmlURL=sWebServiceURL;
	sServiceName = GetFormFieldValue("epcServiceName");
	sAccountName=GetFormFieldValue("epcAccountName");
	sGUID = GetFormFieldValue("epcGUID");
	sIPAddress = GetFormFieldValue("epcIPAddress");
	xmlURL += "/SearchMulti?sPostcode=" + escape(sSearchPostcode) + "&sCompany=" + escape(sSearchCompany)+ "&sStreet=" + escape(sSearchStreet)+ "&sLocality=" + escape(sSearchLocality) + "&sTownOrCity=" + escape(sSearchTownOrCity) + "&sNumber=" + escape(sSearchNumber) + "&sAccountName=" + escape(sAccountName)+ "&sGUID=" + escape(sGUID)+ "&sIPAddress=" + escape(sIPAddress);
	loadXMLDocList(xmlURL, shipWhere);
}

function GetMyAddressList( shipWhere ) {
	if(reqList.readyState==4) {
		if(reqList.status==200) {
			//Mozilla
			if (document.implementation && document.implementation.createDocument) {	
				returnedXMLList=(new DOMParser()).parseFromString(reqList.responseText, "text/xml"); 
				LoadAddressList( shipWhere );
			}
			//IE
			else if (window.ActiveXObject) {
				returnedXMLList= new ActiveXObject("MSXML2.DOMDocument.3.0");
				returnedXMLList.loadXML(reqList.responseText);
				LoadAddressList( shipWhere );
			}
			else {
	  			alert('Your browser cannot handle this script');
	  		}
		}
	}
}


function LoadAddressList( shipWhere ) {
	var StatusCode = returnedXMLList.getElementsByTagName('IsError').item(0).firstChild.data;
	if (StatusCode == "false") { 
		var intAddressList = returnedXMLList.getElementsByTagName('ListAddress').length;
		document.getElementById("epcAddressList_Ship" + shipWhere).options.length = 0;
		document.getElementById("epcAddressList_Ship" + shipWhere).options.length = intAddressList + 1;
		// add the first option in the list
		document.getElementById("epcAddressList_Ship" + shipWhere).options[0] = new Option('Please select an address...', '');
		for (i=0; i<intAddressList; i++) {
			// populate the drop down menu with the addresses found
			j = i + 1;
			fullAddress = returnedXMLList.getElementsByTagName('ListAddress').item(i).firstChild.data;
			absrecnum = returnedXMLList.getElementsByTagName('AbsRecNum').item(i).firstChild.data;
			document.getElementById("epcAddressList_Ship" + shipWhere).options[j] = new Option(fullAddress, absrecnum);
		}
		// enable the drop down menu
		document.getElementById("epcAddressList_Ship" + shipWhere).disabled = false;
		// change style of the drop down's container
		$('#addressListContainer_' + shipWhere).fadeOut(10);
		$('#addressListContainer_' + shipWhere).fadeIn(250);
		YAHOO.util.Dom.removeClass("addressListContainer_" + shipWhere, 'resultsDisabled');
		$('#ship'+shipWhere+'_lookup_fail').hide();
	} else {
		$('#addressListContainer_'+shipWhere).hide();
		$('#ship'+shipWhere+'_lookup_fail').show();
	}
}

function loadXMLDocList(url, shipWhere) {
	reqList=false;
	reqList=new XMLHTTP();
	reqList.onreadystatechange = function(e) {
		GetMyAddressList( shipWhere );
	}
	reqList.open("GET",url,true);
	reqList.send();
}

function loadXMLDocGetAddress(url, shipWhere) { 
	reqGetAddress=false; 
	reqGetAddress=new XMLHTTP(); 
	reqGetAddress.onreadystatechange = function(e) {
		GetMyAddress( shipWhere );
	}
	reqGetAddress.open("GET",url,true); 
	reqGetAddress.send(null);
}

function GetMyAddress( shipWhere ) {
	if(reqGetAddress.readyState==4) {
		if(reqGetAddress.status==200) {
			//Mozilla
			if (document.implementation && document.implementation.createDocument) {	
				returnedXMLGetAddress=(new DOMParser()).parseFromString(reqGetAddress.responseText, "text/xml"); 
				LoadAddress( shipWhere );
			}
			//IE
			else if (window.ActiveXObject) {
				returnedXMLGetAddress= new ActiveXObject("MSXML2.DOMDocument.3.0");
				returnedXMLGetAddress.loadXML(reqGetAddress.responseText);
				LoadAddress( shipWhere );
			} else {
	  			alert('Your browser cannot handle this script');
	  		}
		}
	}
}


function LoadAddress( shipWhere ) {
	var defaultAddress = getEmptyAddress();
	if ( shipWhere == 'From' ) {
		defaultAddress = defaultAddress.from;
	} else {
		defaultAddress = defaultAddress.to;
	}
	try
	{
		document.getElementById("cmsServicesShipping_ship" + shipWhere + "_address_3").value=returnedXMLGetAddress.getElementsByTagName('PostTown').item(0).firstChild.data;
	}
	catch(e)
	{
		try
		{
			document.getElementById("cmsServicesShipping_ship" + shipWhere + "_address_3").value = defaultAddress.town;
		}
		catch(e)
		{
			
		}
	}
	try
	{
		document.getElementById("cmsServicesShipping_ship" + shipWhere + "_company").value=returnedXMLGetAddress.getElementsByTagName('Organisation').item(0).firstChild.data;
		// got a company name, so set residential to false
		document.getElementById("cmsServicesShipping_ship" + shipWhere + "_residential_address").value = 0;
		changeResidential( 0, 'ship' + shipWhere );
	}
	catch(e)
	{
		try
		{
			// not got a company name, so set residential to true
			document.getElementById("cmsServicesShipping_ship" + shipWhere + "_residential_address").value = 1;
			changeResidential( 1, 'ship' + shipWhere );
			document.getElementById("cmsServicesShipping_ship" + shipWhere + "_company").value = defaultAddress.company;
		}
		catch(e)
		{
			
		}
	}
	try
	{
		if ( document.getElementById("cmsServicesShipping_ship" + shipWhere + "_address_3").value.toLowerCase() == 'bournemouth' ) {
			document.getElementById("cmsServicesShipping_ship" + shipWhere + "_city").value = 'Dorset';
		} else {
			document.getElementById("cmsServicesShipping_ship" + shipWhere + "_city").value=returnedXMLGetAddress.getElementsByTagName('County').item(0).firstChild.data;
		}
	}
	catch(e)
	{
		try
		{
			document.getElementById("cmsServicesShipping_ship" + shipWhere + "_city").value = defaultAddress.county;
		}
		catch(e)
		{
			
		}
	}
	
	var address1 = '';
	var address2 = '';
	
	try
	{
		var houseNumber = returnedXMLGetAddress.getElementsByTagName('Number').item(0).firstChild.data;
	}
	catch(e)
	{
		var houseNumber = '';
	}
	
	try
	{
		address2 = returnedXMLGetAddress.getElementsByTagName('AddressLine2').item(0).firstChild.data;
	}
	catch(e)
	{
		try
		{
			address2 = defaultAddress.address2;
		}
		catch(e)
		{
			
		}
	}
	try
	{
		address1 = returnedXMLGetAddress.getElementsByTagName('AddressLine1').item(0).firstChild.data;
	}
	catch(e)
	{
		try
		{
			if ( ( address2 != '' ) && ( address1 == '' ) ) {
				address1 = address2;
				address2 = defaultAddress.address2;
			}
		}
		catch(e)
		{

		}
	}
	
	// if the house number is part of the address1 field, try and separate them
	if ( houseNumber == '' ) {
		var address1Split = address1.split(' ');
		if ( !isNaN( address1Split[0].replace('-','') ) ) {
			// ie 42 Letsbee Avenue
			houseNumber = address1Split[0];
		} else {
			if ( !isNaN( address1Split[1].replace('-','') ) ) {
				// ie Flat 69 Baltic Wharf
				houseNumber = address1Split[0] + ' ' + address1Split[1];
			}
		}
	}

	// strip the house number from the address one to avoid it duplicating
	address1 = trim( address1.replace( houseNumber, '' ) );
	
	if ( address1 == '' ) {
		if ( address2 != defaultAddress.address2 ) {
			address1 = address2;
			address2 = defaultAddress.address2;
		} else {
			address1 = defaultAddress.address1;
		}
	}
	
	try
	{
		document.getElementById("cmsServicesShipping_ship" + shipWhere + "_house_number").value = houseNumber;
	}
	catch(e)
	{
		try
		{
			document.getElementById("cmsServicesShipping_ship" + shipWhere + "_house_number").value = defaultAddress.houseNumber;
		}
		catch(e)
		{
			
		}
	}

	document.getElementById("cmsServicesShipping_ship" + shipWhere + "_address_1").value = address1;
	document.getElementById("cmsServicesShipping_ship" + shipWhere + "_address_2").value = address2;
	
	validateShippingAddress(shipWhere);
}

function GetAddress( shipWhere ) { 
	var xmlURL=sWebServiceURL; 
	sServiceName = GetFormFieldValue("epcServiceName"); 
	sAccountName=GetFormFieldValue("epcAccountName"); 
	sGUID = GetFormFieldValue("epcGUID"); 
	sIPAddress = GetFormFieldValue("epcIPAddress"); 
	sSearch=GetFormFieldValue("epcPostcode"); 
	iTypeOfSearch=GetFormFieldValue("epcSearchType"); 
	var selIndex = document.getElementById("epcAddressList_Ship" + shipWhere).options[document.getElementById("epcAddressList_Ship" + shipWhere).selectedIndex].value; 
	xmlURL += "/GetAddress?sAbsRecNum=" + selIndex + "&sAccountName=" + escape(sAccountName)+ "&sGUID=" + escape(sGUID)+ "&sIPAddress=" + escape(sIPAddress); 
	loadXMLDocGetAddress(xmlURL, shipWhere);
	
	$('#ship'+shipWhere+'_address_fields').show();
}

function Querystring(qs) {
	this.params=new Object();
	this.get=Querystring_get;
	if(qs==null) { 
		qs=location.search.substring(1,location.search.length);
	}
	if(qs.length==0){
		return;
	}
	var args=qs.split('&');
	for(var i=0;i<args.length;i++){
		var value;var pair=args[i].split('=');
		var name=unescape(pair[0]);
		if(pair.length==2){
			value = unescape(pair[1]);
		}else{
			value=name
		}
		this.params[name]=value;
	}
}

function Querystring_get(key, default_) {
	if(default_==null){
		default_=null;
	}
	var value=this.params[key];
	if(value==null){
		value=default_;
	}
	return value;
}

function GetFormFieldValue(what) {
	var intFieldCounter=0;
	for(intFormCounter=0;intFormCounter<=document.forms.length-1;intFormCounter++){
		for(elementCounter=0;elementCounter<=document.forms[intFormCounter].length-1;elementCounter++) {
			if(document.forms[intFormCounter].elements[elementCounter].name==what) {
				return document.forms[intFormCounter].elements[elementCounter].value;
			}
		}
	}
}
