function xmlhttpPost(strURL,thepcode,thepcode_result) 
	{
		var xmlHttpReq = false;
		var thisform = this;
		// Checks if browser is Mozilla or Safari
		if (window.XMLHttpRequest){thisform .xmlHttpReq = new XMLHttpRequest();}
		// Checks if browser is Internet Explorer
		else if (window.ActiveXObject){thisform .xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");}
		
		thisform .xmlHttpReq.open('POST', strURL, true);
		thisform .xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		thisform .xmlHttpReq.onreadystatechange = function() 
		{
			if (thisform .xmlHttpReq.readyState == 4) {updatepage(thisform .xmlHttpReq.responseText,thepcode_result);}
		}
		thisform .xmlHttpReq.send(getquerystring(thepcode));
	}
	
	function getquerystring(thepcode) 
	{
	    var postcode= document.getElementById(thepcode).value;
	    qstr = 'postcode=' + escape(postcode);  // NOTE: no '?' before querystring
	    return qstr;
	}
	
	function updatepage(str,thepcode_result)
	{
	    document.getElementById(thepcode_result).innerHTML = str;
	}


// Postcode Window
function postcodeWindow(thepcode,thepcode_result,targetAddress,passCompanyOrNot,type) {
	

			
	
	$('#'+thepcode_result).dialog({
		autoOpen: false,
		bgiframe: true,
		modal: true,
		height: 320,
		width: 440
	});
	
	xmlhttpPost("pcode_api/result.php?type="+type+"&thepcode="+thepcode+"&thepcode_result="+thepcode_result+"&passCompanyOrNot="+passCompanyOrNot+"&targetAddress="+targetAddress,thepcode,thepcode_result);
	$('#'+thepcode_result).dialog('open'); 
	
}

// Pass Firsst Line of Address
function passFirstLine(val,passCompanyOrNot,type) {


		if(passCompanyOrNot == 1) {
					if(val.indexOf("<comp>") > -1 ) {
						val = val.split("</comp>");
						var comp = val[0].replace("<comp>","");
						val = val[1].replace(/<lbreak>/ig, "\n");
						$('#company').empty();
						$('#company').val(comp);
					} else {
						val = val.replace(/<lbreak>/ig, "\n");
						//$('#company').val('');
					}
		} else {
					if(val.indexOf("<comp>") > -1 ) {
						val = val.split("</comp>");
						var comp = val[0].replace("<comp>","");
						val = val[1].replace(/<lbreak>/ig, "\n");
					} else {
						val = val.replace(/<lbreak>/ig, "\n");
					}
	}





var second_add = $('#second_address').val();
var two_address = $('#second_address').val().split("\n");
var house_number = val;

var street = two_address[0];
var town_first = two_address[1];
var town= two_address[2];
var postc = two_address[3];


	if(type == "pickup") {
	
	$("#fnumber").val(house_number);
	$("#fstreet").val(street);
	$("#fsettlement").val(town);
	$('#from_address').val(house_number+second_add);
	
	bind_from_elements();
	}
	else if(type =="return"){
	$("#tnumber").val(house_number);
	$("#tstreet").val(street);
	$("#tsettlement").val(town);
	$('#to_address').val('');
	$('#to_address').val(house_number+second_add);
	bind_to_elements();
	}


}

// Pass the Addrss!!
function passAddress(thepcode_result,targetAddress) {
	$('#'+targetAddress).val($('#final_address').val());
	$('#'+thepcode_result).dialog('close');
}

// Cancel Postcode Window
function cancelPostcode(thepcode_result) {
	$('#'+thepcode_result).dialog('close');
}
	
