//var addType="billing";
var paramValue = "";
var params = "";




function CreateBookmark() { 
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(document.title, location.href,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( location.href, document.title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
}


function findmyaddress() {
		//alert("Accessing...");
		
		
		//if($F(addType+'_postcode')=='') {
			//alert("Please enter a Post Code in the Search Post Code box.");
			//return;
		//}
		
		var addType="billing";
		Element.hide($(addType+'_address_container'));	
		
		
		
			if (addType=="billing"){
				paramValue = document.checkoutsubmitform.billing_postcode.value;
			}else{
				paramValue = document.checkoutsubmitform.delivery_postcode.value;
			}
			params = "SearchPostCode="+paramValue;

       
			var url = "address_finder.php";
			
			
			var ajax = new Ajax.Request(url, {method: 'post', onComplete: showmyaddress, parameters: params} );		
		
		 
	
	show_spinner(addType);
		
		
}





function showmyaddress(output) {
	var addType="billing";
	hide_spinner(addType);
	if(output.responseText=='') {
		//hide_spinner();
		Element.hide($(addType+'_select_address'));
		alert("No addresses found with this Post Code. Please ensure that you have entered the correct Post Code.");
		return;
	}
	var output_array = output.responseText.split("|");
	//hide_spinner();
	// show multiline-textbox and fill in the details
	Element.show($(addType+'_select_address'));
	var optionstr;
	for(i=0;i<output_array.length;i++) {
		opval = output_array[i].split("^");
		if("undefined"!=opval[0])
			optionstr += "<option value='" + opval[0] + "'>" + opval[1] + "</option>";
	}
	$(addType+'_select_address').innerHTML="<select class='selectboxaddress' name='seladdress' id='seladdress' size=5 onClick='getmyaddress()'>" + optionstr + "</select>";
}

function getmyaddress() {
	
	var addType="billing";
	show_spinner(addType);
	var url = "address_finder.php";
	
	
	//var paramValue = document.checkoutsubmitform.seladdress.options[selectedIndex].value;			
	
	
	var r = document.getElementById("seladdress");	
	
	//alert(r);	
	
	var rr = r.options[r.selectedIndex].value;	
	
 	//var paramValue = rr;

	//params = "address="+$F('seladdress');	
	params = "address="+rr;
	//alert(params);
	var ajax = new Ajax.Request( url, {method: 'post', onComplete: fillmyaddress, parameters: params} );
}

function fillmyaddress(output) {
	var addType="billing";
	addval = output.responseText.split("|");
	hide_spinner(addType);
	$(addType+'_select_address').innerHTML='';		// empty select
	Element.hide($(addType+'_select_address'));
	Element.show($(addType+'_address_container'));
	//alert(addval);
	if(addType=='billing') {
		//$('Address1').value=addval[0];
		//$('Address2').value=addval[1];
		//$('city').value=addval[2];
		//$('county').value=addval[3];
		//$('postcode').value=addval[4];
		document.checkoutsubmitform.address1.value=addval[0];
		document.checkoutsubmitform.address2.value=addval[1];
		document.checkoutsubmitform.address3.value=addval[2];
		document.checkoutsubmitform.city.value=addval[3];
		//document.checkoutsubmitform.county.value=addval[4];
		document.checkoutsubmitform.zipcode.value=addval[5];
		document.checkoutsubmitform.company.value=addval[6];
	}
	//$('dAddress1').value=addval[0];
	//$('dAddress2').value=addval[1];
	//$('dcity').value=addval[2];
	//$('dcounty').value=addval[3];
	//$('dpostcode').value=addval[4];
	//
	
		document.checkoutsubmitform.szipcode.value = addval[5];
		document.checkoutsubmitform.saddress1.value=addval[0];
		document.checkoutsubmitform.saddress2.value=addval[1];
		document.checkoutsubmitform.saddress3.value=addval[2];
		document.checkoutsubmitform.scity.value=addval[3];
		document.checkoutsubmitform.scompany.value=addval[6];
		//document.checkoutsubmitform.scounty.value=addval[4];
	if(addType=='billing') {	
		if(document.checkoutsubmitform.scountry=="GB" && document.checkoutsubmitform.sameasbilling.checked){			
			checkPostCode();	
		}
	}else{
		checkPostCode();
	}
		
		
		
		
}


function findmyaddress_2(addressType) {
		//alert("Accessing...");
		
		
		//if($F(addType+'_postcode')=='') {
			//alert("Please enter a Post Code in the Search Post Code box.");
			//return;
		//}
		
		var addType="delivery";
		Element.hide($(addType+'_address_container'));	
		
		
		
			if (addType=="billing"){
				paramValue = document.checkoutsubmitform.billing_postcode.value;
			}else{
				paramValue = document.checkoutsubmitform.delivery_postcode.value;
			}
			params = "SearchPostCode="+paramValue;

       
			var url = "/address_finder.php";
			
			
			var ajax = new Ajax.Request(url, {method: 'post', onComplete: showmyaddress_2, parameters: params} );		
		
		 
	
	show_spinner(addType);
		
		
}
function showmyaddress_2(output) {
	var addType="delivery";
	hide_spinner(addType);
	if(output.responseText=='') {
		//hide_spinner();
		Element.hide($(addType+'_select_address'));
		alert("No addresses found with this Post Code. Please ensure that you have entered the correct Post Code.");
		return;
	}
	var output_array = output.responseText.split("|");
	//hide_spinner();
	// show multiline-textbox and fill in the details
	Element.show($(addType+'_select_address'));
	var optionstr;
	for(i=0;i<output_array.length;i++) {
		opval = output_array[i].split("^");
		if("undefined"!=opval[0])
			optionstr += "<option value='" + opval[0] + "'>" + opval[1] + "</option>";
	}
	$(addType+'_select_address').innerHTML="<select class=\"selectboxaddress\" name=\"seladdress_2\" id=\"seladdress_2\" size=5 onClick=\"getmyaddress_2()\">" + optionstr + "</select>";
}

function getmyaddress_2() {
	var addType="delivery";
	show_spinner(addType);
	var url = "/address_finder.php";
	
	
	//paramValue = document.checkoutsubmitform.seladdress_2.options[selectedIndex].value;			
	var r = document.getElementById("seladdress_2");	
	//var r = document.checkoutsubmitform.elements['seladdress_2'];
	//var r = document.checkoutsubmitform.getElementById['seladdress_2'];
	//alert("checking");
	//alert(r);
	//var r = document.checkoutsubmitform.seladdress_2;
	var a = r.selectedIndex;
	//alert(a);
	var rr = r.options[a].value;	
 	paramValue = rr;
	
	//params = "address="+$F('seladdress');	
	params = "address="+paramValue;
	var ajax = new Ajax.Request( url, {method: 'post', onComplete: fillmyaddress_2, parameters: params} );
}

function fillmyaddress_2(output) {
	//alert(output.responseText);
	var addType="delivery";
	addval = output.responseText.split("|");
	hide_spinner(addType);
	$(addType+'_select_address').innerHTML='';		// empty select
	Element.hide($(addType+'_select_address'));
	Element.show($(addType+'_address_container'));
	//alert(addval);
	if(addType=='billing') {
		//$('Address1').value=addval[0];
		//$('Address2').value=addval[1];
		//$('city').value=addval[2];
		//$('county').value=addval[3];
		//$('postcode').value=addval[4];
		document.checkoutsubmitform.address1.value=addval[0];
		document.checkoutsubmitform.address2.value=addval[1];
		document.checkoutsubmitform.address3.value=addval[2];
		document.checkoutsubmitform.city.value=addval[3];
		//document.checkoutsubmitform.county.value=addval[4];
		document.checkoutsubmitform.zipcode.value=addval[5];
		document.checkoutsubmitform.company.value=addval[6];
	}
	//$('dAddress1').value=addval[0];
	//$('dAddress2').value=addval[1];
	//$('dcity').value=addval[2];
	//$('dcounty').value=addval[3];
	//$('dpostcode').value=addval[4];
	//
	
		document.checkoutsubmitform.szipcode.value = addval[5];
		document.checkoutsubmitform.saddress1.value=addval[0];
		document.checkoutsubmitform.saddress2.value=addval[1];
		document.checkoutsubmitform.saddress3.value=addval[2];
		document.checkoutsubmitform.scity.value=addval[3];
		document.checkoutsubmitform.scompany.value=addval[6];
		//document.checkoutsubmitform.scounty.value=addval[4];
		
		
		
		
}




function show_spinner(addType) {
	Element.show($(addType+'_spinner'));
}
function hide_spinner(addType) {
	Element.hide($(addType+'_spinner'));
}



