// JavaScript Document
function dropLookupState(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete"){
		var xmlDoc=xmlhttp.responseXML;
		
		var errorField = xmlDoc.documentElement.getElementsByTagName('errorField');
		var errorFlag = errorField[0].getAttribute("errorFlag");
		var errorMess = errorField[0].getAttribute("errorMess");
		
		var agentField = xmlDoc.documentElement.getElementsByTagName('agent');
		var siteName = agentField[0].getAttribute("siteName");
		
		if(errorFlag == 0){
			
			var output = "";
			
			output += "<table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
				output += "<tr>";
					output += "<td valign=\"top\">";
						output += "<span class=\"bannerHeading\">Your selected nearest collect+ store.</span>";
					output += "</td>";
					output += "<td valign=\"top\" align=\"right\">";
						output += "<strong><a href=\"#\" class=\"link\" onclick=\"changeDropPoint(); return false;\">[edit]</a></strong>";
					output += "</td>";
				output += "</tr>";
				output += "<tr>";
					output += "<td valign=\"top\">";
						output += "<strong>" + agentField[0].getAttribute("siteName") + "</strong>";
					output += "</td>";
					output += "<td valign=\"top\" align=\"right\">";
						output += "<strong>(distance from " + getObj('dropPoint').value + " : " + agentField[0].getAttribute("miles") + " miles)</strong>";
					output += "</td>";
				output += "</tr>";
				output += "<tr>";
					output += "<td valign=\"top\" colspan=\"2\">";
						output += agentField[0].getAttribute("address") + ", " + agentField[0].getAttribute("city") + ", " + agentField[0].getAttribute("county") + ". " + agentField[0].getAttribute("postcode") + "." ;
					output += "</td>";
				output += "</tr>";
			output += "</table>";
			
			getObj('dropResultDiv').innerHTML = output;
			getObjStyle('dropPointDiv').display = "none";
			getObjStyle('dropTableDiv').display = "none";
			getObjStyle('dropMapWrapper').display = "none";
		} else {
			getObj('dropPointDiv').innerHTML = "<span class=\"errorTxt\">" + errorMess + "</span>";
		}
		getObj('dropPointDiv').scrollIntoView(true);
		
	}
}

function collectLookupState(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete"){
		var xmlDoc=xmlhttp.responseXML;
		
		var errorField = xmlDoc.documentElement.getElementsByTagName('errorField');
		var errorFlag = errorField[0].getAttribute("errorFlag");
		var errorMess = errorField[0].getAttribute("errorMess");
		
		var agentField = xmlDoc.documentElement.getElementsByTagName('agent');
		var siteName = agentField[0].getAttribute("siteName");
		
		if(errorFlag == 0){

			var output = "";
			
			output += "<table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
				output += "<tr>";
					output += "<td valign=\"top\">";
						output += "<span class=\"bannerHeading\">Your selected collect+ store for Collection.</span>";
					output += "</td>";
					output += "<td valign=\"top\" align=\"right\">";
						output += "<strong><a href=\"#\" class=\"link\" onclick=\"changeCollectPoint(); return false;\">[edit]</a></strong>";
					output += "</td>";
				output += "</tr>";
				output += "<tr>";
					output += "<td valign=\"top\">";
						output += "<strong>" + agentField[0].getAttribute("siteName") + "</strong>";
					output += "</td>";
					output += "<td valign=\"top\" align=\"right\">";
						output += "<strong>(distance from " + getObj('collectPoint').value + " : " + agentField[0].getAttribute("miles") + " miles)</strong>";
					output += "</td>";
				output += "</tr>";
				output += "<tr>";
					output += "<td valign=\"top\" colspan=\"2\">";
						output += agentField[0].getAttribute("address") + ", " + agentField[0].getAttribute("city") + ", " + agentField[0].getAttribute("county") + ". " + agentField[0].getAttribute("postcode") + "." ;
					output += "</td>";
				output += "</tr>";
			output += "</table>";

			getObj('collectResultDiv').innerHTML = output;
			getObjStyle('collectPointDiv').display = "none";
			getObjStyle('collectTableDiv').display = "none";
			getObjStyle('collectMapWrapper').display = "none";
			getObjStyle('senderContactWrapper').display = "block";
		} else {
			getObj('collectPointDiv').innerHTML = "<span class=\"errorTxt\">" + errorMess + "</span>";
		}
		getObj('collectPointDiv').scrollIntoView(true);
		
	}
}

function contactCallback(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete"){
		var xmlDoc=xmlhttp.responseXML;
		
		var errorField = xmlDoc.documentElement.getElementsByTagName('errorField');
		var errorFlag = errorField[0].getAttribute("errorFlag");
		var errorMess = errorField[0].getAttribute("errorMess");
		
		var senderField = xmlDoc.documentElement.getElementsByTagName('senderField');
		
		if(errorFlag == 0){
			
			var output = "";
			
			output += "<table width=\"100%\"  border=\"0\" cellspacing=\"2\" cellpadding=\"0\">";
			  output += "<tr>";
				output += "<td width=\"30%\"><span class=\"bannerHeading\">Your Details</span></td>";
				output += "<td width=\"40%\">&nbsp;</td>";
				output += "<td width=\"30%\" align=\"right\"><a href=\"#\" class=\"link\" onclick=\"changeContact(); return false;\">[edit]</a></td>";
			  output += "</tr>";
			  output += "<tr>";
				output += "<td width=\"30%\">Name: <strong>"+senderField[0].getAttribute("name")+"</strong></td>";
				output += "<td width=\"40%\">Email Address: <strong>"+senderField[0].getAttribute("email")+"</strong>";
				if(senderField[0].getAttribute("pref") == "email"){
					output += " <a href=\"#\" class=\"info\"><img src=\"../../images/email.png\" border=\"0\"/><span>This is your preferred method of contact</span></a>";
				}
				output+= "</td>";
				output += "<td width=\"30%\">Reference: <strong>"+senderField[0].getAttribute("ref")+"</strong></td>";
			  output += "</tr>";
			  output += "<tr>";
				output += "<td width=\"30%\">Company: <strong>"+senderField[0].getAttribute("company")+"</strong></td>";
				output += "<td width=\"40%\">Mobile Number: <strong>"+senderField[0].getAttribute("mobile")+"</strong>";
				if(senderField[0].getAttribute("pref") == "mobile"){
					output += " <a href=\"#\" class=\"info\"><img src=\"../../images/phone.png\" border=\"0\"/><span>This is your preferred method of contact</span></a>";
				}
				output+= "</td>";
				output += "<td width=\"30%\">Parcel Weight: <strong>"+senderField[0].getAttribute("weight")+"</strong></td>";
			  output += "</tr>";
			output += "</table>";

			
			getObj('senderContactResultDiv').innerHTML = output;
			getObjStyle('senderContactDiv').display = "none";
			getObjStyle('receiverContactWrapper').display = "block";
		} else {
			getObj('senderContactResultDiv').innerHTML = "<span class=\"errorTxt\">" + errorMess + "</span>";
		}
		getObj('senderContactWrapper').scrollIntoView(true);
		
	}
}

function receiverCallback(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete"){
		var xmlDoc=xmlhttp.responseXML;
		
		var errorField = xmlDoc.documentElement.getElementsByTagName('errorField');
		var errorFlag = errorField[0].getAttribute("errorFlag");
		var errorMess = errorField[0].getAttribute("errorMess");
		
		var receiverField = xmlDoc.documentElement.getElementsByTagName('receiverField');
		
		if(errorFlag == 0){
			
			var output = "";
			output += "<table width=\"100%\"  border=\"0\" cellspacing=\"2\" cellpadding=\"0\">";
			  output += "<tr>";
				output += "<td width=\"30%\"><span class=\"bannerHeading\">Recipient Details</span></td>";
				output += "<td width=\"40%\">&nbsp;</td>";
				output += "<td width=\"30%\" align=\"right\"><a href=\"#\" class=\"link\" onclick=\"changeReceipient(); return false;\">[edit]</a></td>";
			  output += "</tr>";
			  output += "<tr>";
				output += "<td width=\"30%\">Name: <strong>"+receiverField[0].getAttribute("name")+"</strong></td>";
				output += "<td width=\"40%\">Email Address: <strong>"+receiverField[0].getAttribute("email")+ "</strong>";
				if(receiverField[0].getAttribute("pref") == "email"){
					output += " <a href=\"#\" class=\"info\"><img src=\"../../images/email.png\" border=\"0\"/><span>This is their preferred method of contact</span></a>";
				}
				output+= "</td>";
				output += "<td width=\"30%\">&nbsp;</td>";
			  output += "</tr>";
			  output += "<tr>";
				output += "<td width=\"30%\">Company: <strong>"+receiverField[0].getAttribute("company")+"</strong></td>";
				output += "<td width=\"40%\">Mobile Number: <strong>"+receiverField[0].getAttribute("mobile")+"</strong>";
				if(receiverField[0].getAttribute("pref") == "mobile"){
					output += " <a href=\"#\" class=\"info\"><img src=\"../../images/phone.png\" border=\"0\"/><span>This is their preferred method of contact</span></a>";
				}
				output+= "</td>";
				output += "<td width=\"30%\">&nbsp;</td>";
			  output += "</tr>";
			output += "</table>";
				
			getObj('receiverContactResultDiv').innerHTML = output;
			getObjStyle('receiverContactDiv').display = "none";
			getObjStyle('sendersAddress').display = "block";

		} else {
			getObj('receiverContactResultDiv').innerHTML = "<span class=\"errorTxt\">" + errorMess + "</span>";
		}
		getObj('receiverContactWrapper').scrollIntoView(true);
		
	}
}

function senderGetAdd(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete"){

		var Credits="",LINE1="",LINE2="",LINE3="",TOWN="",COUNTY="",POSTCODE="",COUNTRY="";
		
		var XMLtest = xmlhttp.responseText;
		if (XMLtest.indexOf("<line1>")>=0){
			
			// read the message from the server
			var xmlResponse = xmlhttp.responseXML;
		
			// obtain the XML's document element
			xmlRoot = xmlResponse.documentElement;
		  
			if (xmlRoot.getElementsByTagName("credits_display_text").item(0).firstChild) 
				{Credits = xmlRoot.getElementsByTagName("credits_display_text").item(0).firstChild.data ;}
		
			if (xmlRoot.getElementsByTagName("line1").item(0).firstChild) 
				{LINE1 = xmlRoot.getElementsByTagName("line1").item(0).firstChild.data;}
		
			if (xmlRoot.getElementsByTagName("line2").item(0).firstChild) 
				{LINE2 = xmlRoot.getElementsByTagName("line2").item(0).firstChild.data;}
		
			if (xmlRoot.getElementsByTagName("line3").item(0).firstChild) 
				{LINE3 = xmlRoot.getElementsByTagName("line3").item(0).firstChild.data;}
		
			  if (xmlRoot.getElementsByTagName("town").item(0).firstChild) 	
				{TOWN = xmlRoot.getElementsByTagName("town").item(0).firstChild.data;}
		
			  if (xmlRoot.getElementsByTagName("county").item(0).firstChild) 
				{COUNTY = xmlRoot.getElementsByTagName("county").item(0).firstChild.data;}
		
			  if (xmlRoot.getElementsByTagName("postcode").item(0).firstChild) 
				{POSTCODE = xmlRoot.getElementsByTagName("postcode").item(0).firstChild.data;}
		
			  if (xmlRoot.getElementsByTagName("country").item(0).firstChild) 
				{COUNTRY = xmlRoot.getElementsByTagName("country").item(0).firstChild.data;}
				
			getObj('sender_building').value = getObj('sender_building_lookup').value;
			getObj('sender_postcode').value = POSTCODE;
			getObj('sender_line1').value = LINE1;
			getObj('sender_line2').value = LINE2;
			getObj('sender_line3').value = LINE3;
			
			if(TOWN == ""){
				getObj('sender_town').value = COUNTY;
			} else {
				getObj('sender_town').value = TOWN;
				getObj('sender_county').value = COUNTY;
			}
			
			getObjStyle('sendersPafResultDiv').display = "block";
			getObjStyle('sendersPafDiv').display = "none";

		} else {
			getObj('senderAddressResult').innerHTML = "<span class=\"errorTxt\">Please enter a valid postcode.</span>";  
		}
		getObj('sendersAddress').scrollIntoView(true);
		
	}
}
function senderAddCallback(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete"){
		var xmlDoc=xmlhttp.responseXML;
		
		var errorField = xmlDoc.documentElement.getElementsByTagName('errorField');
		var errorFlag = errorField[0].getAttribute("errorFlag");
		var errorMess = errorField[0].getAttribute("errorMess");
		
		var addressField = xmlDoc.documentElement.getElementsByTagName('addressField');
		
		var addressOutput = addressField[0].getAttribute("building") + " ";

		if(addressField[0].getAttribute("line1") != ""){
			addressOutput += addressField[0].getAttribute("line1") + ", ";
		}
		if(addressField[0].getAttribute("line2") != ""){
			addressOutput += addressField[0].getAttribute("line2") + ", ";
		}
		if(addressField[0].getAttribute("line3") != ""){
			addressOutput += addressField[0].getAttribute("line3") + ", ";
		}
		if(addressField[0].getAttribute("town") != ""){
			addressOutput += addressField[0].getAttribute("town") + ", ";
		}
		if(addressField[0].getAttribute("county") != ""){
			addressOutput += addressField[0].getAttribute("county") + ", ";
		}
		if(addressField[0].getAttribute("postcode") != ""){
			addressOutput += addressField[0].getAttribute("postcode") + ".";
		}
		
		if(errorFlag == 0){
			
			var output = "";
				output += "<table width=\"100%\"  border=\"0\" cellspacing=\"2\" cellpadding=\"0\">";
				output += "<tr>";
					output += "<td><span class=\"bannerHeading\">Your Address</span></td>";
					output += "<td align=\"right\"><a href=\"#\" class=\"link\" onclick=\"changeSenderAdd(); return false;\">[edit]</a></td>";
				output += "</tr>";
				output += "<tr>";
					output += "<td colspan=\"2\">"+addressOutput +".</td>";
				output += "</tr>";
			output += "</table>";
				
			getObj('senderAddressResult').innerHTML = output;
			getObjStyle('sendersPafResultDiv').display = "none";
			getObjStyle('billingAddress').display = "block";

		} else {
			getObj('senderAddressResult').innerHTML = "<span class=\"errorTxt\">" + errorMess + "</span>";
		}
		getObj('sendersAddress').scrollIntoView(true);
		
	}
}
function billingGetAdd(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete"){

		var Credits="",LINE1="",LINE2="",LINE3="",TOWN="",COUNTY="",POSTCODE="",COUNTRY="";
		
		var XMLtest = xmlhttp.responseText;
		if (XMLtest.indexOf("<line1>")>=0){
			
			// read the message from the server
			var xmlResponse = xmlhttp.responseXML;
		
			// obtain the XML's document element
			xmlRoot = xmlResponse.documentElement;
		  
			if (xmlRoot.getElementsByTagName("credits_display_text").item(0).firstChild) 
				{Credits = xmlRoot.getElementsByTagName("credits_display_text").item(0).firstChild.data ;}
		
			if (xmlRoot.getElementsByTagName("line1").item(0).firstChild) 
				{LINE1 = xmlRoot.getElementsByTagName("line1").item(0).firstChild.data;}
		
			if (xmlRoot.getElementsByTagName("line2").item(0).firstChild) 
				{LINE2 = xmlRoot.getElementsByTagName("line2").item(0).firstChild.data;}
		
			if (xmlRoot.getElementsByTagName("line3").item(0).firstChild) 
				{LINE3 = xmlRoot.getElementsByTagName("line3").item(0).firstChild.data;}
		
			  if (xmlRoot.getElementsByTagName("town").item(0).firstChild) 	
				{TOWN = xmlRoot.getElementsByTagName("town").item(0).firstChild.data;}
		
			  if (xmlRoot.getElementsByTagName("county").item(0).firstChild) 
				{COUNTY = xmlRoot.getElementsByTagName("county").item(0).firstChild.data;}
		
			  if (xmlRoot.getElementsByTagName("postcode").item(0).firstChild) 
				{POSTCODE = xmlRoot.getElementsByTagName("postcode").item(0).firstChild.data;}
		
			  if (xmlRoot.getElementsByTagName("country").item(0).firstChild) 
				{COUNTRY = xmlRoot.getElementsByTagName("country").item(0).firstChild.data;}
				
			getObj('billing_building').value = getObj('billing_building_lookup').value;
			getObj('billing_postcode').value = POSTCODE;
			getObj('billing_line1').value = LINE1;
			getObj('billing_line2').value = LINE2;
			getObj('billing_line3').value = LINE3;

			if(TOWN == ""){
				getObj('billing_town').value = COUNTY;
			} else {
				getObj('billing_town').value = TOWN;
				getObj('billing_county').value = COUNTY;
			}
			
			getObjStyle('billingPafResultDiv').display = "block";
			getObjStyle('billingPafDiv').display = "none";

		} else {
			getObj('billingAddressResult').innerHTML = "<span class=\"errorTxt\">Please enter a valid postcode.</span>";  
		}
		getObj('billingAddress').scrollIntoView(true);
		
	}
}
function billingAddCallback(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete"){
		var xmlDoc=xmlhttp.responseXML;
		
		var errorField = xmlDoc.documentElement.getElementsByTagName('errorField');
		var errorFlag = errorField[0].getAttribute("errorFlag");
		var errorMess = errorField[0].getAttribute("errorMess");
		
		var addressField = xmlDoc.documentElement.getElementsByTagName('addressField');
		
		var addressOutput = addressField[0].getAttribute("building") + " ";

		if(addressField[0].getAttribute("line1") != ""){
			addressOutput += addressField[0].getAttribute("line1") + ", ";
		}
		if(addressField[0].getAttribute("line2") != ""){
			addressOutput += addressField[0].getAttribute("line2") + ", ";
		}
		if(addressField[0].getAttribute("line3") != ""){
			addressOutput += addressField[0].getAttribute("line3") + ", ";
		}
		if(addressField[0].getAttribute("town") != ""){
			addressOutput += addressField[0].getAttribute("town") + ", ";
		}
		if(addressField[0].getAttribute("county") != ""){
			addressOutput += addressField[0].getAttribute("county") + ", ";
		}
		if(addressField[0].getAttribute("postcode") != ""){
			addressOutput += addressField[0].getAttribute("postcode") + ".";
		}
		
		if(errorFlag == 0){
			
			var output = "";
				output += "<table width=\"100%\"  border=\"0\" cellspacing=\"2\" cellpadding=\"0\">";
				output += "<tr>";
					output += "<td><span class=\"bannerHeading\">Billing Address</span></td>";
					output += "<td align=\"right\"><a href=\"#\" class=\"link\" onclick=\"changeBillingAdd(); return false;\">[edit]</a></td>";
				output += "</tr>";
				output += "<tr>";
					output += "<td colspan=\"2\">"+addressOutput +".</td>";
				output += "</tr>";
			output += "</table>";
				
			getObj('billingAddressResult').innerHTML = output;
			getObjStyle('billingPafResultDiv').display = "none";
			getObjStyle('confirmDivWrapper').display = "block";

		} else {
			getObj('billingAddressResult').innerHTML = "<span class=\"errorTxt\">" + errorMess + "</span>";
		}
		getObj('billingAddress').scrollIntoView(true);
		
	}
}

function paymentCallback(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete"){
		var xmlDoc=xmlhttp.responseXML;
		
		var errorField = xmlDoc.documentElement.getElementsByTagName('errorField');
		var errorFlag = errorField[0].getAttribute("errorFlag");
		var errorMess = errorField[0].getAttribute("errorMess");
		
		if(errorFlag == 0){
			
			var formFields = xmlDoc.documentElement.getElementsByTagName('formFields');
			
			getObj('intInstID').value = formFields[0].getAttribute("intInstID");
			getObj('strCartID').value = formFields[0].getAttribute("strCartID");
			getObj('fltAmount').value = formFields[0].getAttribute("fltAmount");
			getObj('strDesc').value = formFields[0].getAttribute("strDesc");
			//getObj('intAccountID').value = formFields[0].getAttribute("intAccountID");
			getObj('strCardHolder').value = formFields[0].getAttribute("strCardHolder");
			getObj('strAddress').value = formFields[0].getAttribute("strAddress");
			getObj('strCity').value = formFields[0].getAttribute("strCity");
			getObj('strState').value = formFields[0].getAttribute("strState");
			getObj('strPostcode').value = formFields[0].getAttribute("strPostcode");
			getObj('strCountry').value = formFields[0].getAttribute("strCountry");

			makeBooking(getObj('strCartID').value);
			//var form=getObj('paymentForm');
			//form.submit();

		} else {
			getObjStyle('paymentErrorDiv').display = "block";
			getObj('paymentErrorDiv').innerHTML = "<span class=\"errorTxt\">" + errorMess + "</span>";
		}
		
	}
}

function bookingCallback(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete"){
		var xmlDoc=xmlhttp.responseXML;
		
		var errorField = xmlDoc.documentElement.getElementsByTagName('errorField');
		var errorFlag = errorField[0].getAttribute("errorFlag");
		var errorMess = errorField[0].getAttribute("errorMess");

		var jobField = xmlDoc.documentElement.getElementsByTagName('jobField');

		var form=getObj('paymentForm');
		form.submit();
		
	}
}
