﻿        var account_code='SECUR11156';
		var license_code='RH18-TM98-UF92-UU73';
		// Removes leading whitespaces
        function LTrim( value ) {
	
	     var re = /\s*((\S+\s*)*)/;
	     return value.replace(re, "$1");
	
        }

       // Removes ending whitespaces
       function RTrim( value ) {
	
	    var re = /((\s*\S+)*)\s*/;
	    return value.replace(re, "$1");
	
       }

      // Removes leading and ending whitespaces
       function trim( value ) {
	
	   return LTrim(RTrim(value));
	
       }

//-------------- Billing Address --------------		
		function pcaByPostcodeBegin()
		{
			  var postcode = document.forms[0]["ctl00$plcMain$ucCart$cartElem$wzdStep2$txtBillingZip"].value;
		      var scriptTag = document.getElementById("pcaScriptTag");
		      var headTag = document.getElementsByTagName("head").item(0);
		      var strUrl = "";
		      
		      if(trim(postcode) != "" )
		      {
		      document.getElementById("divLoading").style.display = '';
		      
		      //Build the url
		      strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
		      strUrl += "&action=lookup";
		      strUrl += "&type=by_postcode";
		      strUrl += "&postcode=" + escape(postcode);
		      strUrl += "&account_code=" + escape(account_code);
		      strUrl += "&license_code=" + escape(license_code);
		      strUrl += "&callback=pcaByPostcodeEnd";
		      
		      //Make the request
		      if (scriptTag)
		         {
		            //The following 2 lines perform the same function and are interchangeable
		            headTag.removeChild(scriptTag);
		            //scriptTag.parentNode.removeChild(scriptTag);
		         }
		      scriptTag = document.createElement("script");
		      scriptTag.src = strUrl
		      scriptTag.type = "text/javascript";
		      scriptTag.id = "pcaScriptTag";
		      headTag.appendChild(scriptTag);		      
		      
		      }
		      else
		      {
		        alert("Please enter a valid post code");
		      }
		      
		   }

		function pcaByPostcodeEnd()
		   {
			  document.getElementById("divLoading").style.display = 'none';
			  
		      //Test for an error
		      if (pcaIsError)
		         {
		            //Show the error message
		            alert(pcaErrorMessage);
		         }
		      else
		         {
		            //Check if there were any items found
		            if (pcaRecordCount==0)
		               {
		                   alert("Sorry, no matching items found");
		                   document.forms[0]["selectaddress"].style.display = 'none';
		                   document.forms[0]["btnFetch"].style.display = 'none';
		                   document.getElementById("divMessage").style.display = 'none';
		               }
		            else
		               {
		                   document.forms[0]["selectaddress"].style.display = '';
		                   document.forms[0]["selectaddress"].style.width = '440px';
		                   document.forms[0]["btnFetch"].style.display = '';
		                   document.getElementById("divMessage").style.display = '';
		                   
						  for (i=document.forms[0]["selectaddress"].options.length-1; i>=0; i--){
							  document.forms[0]["selectaddress"].options[i] = null;
							}
						  for (i=0; i<pca_id.length; i++){
		                    document.forms[0]["selectaddress"].options[document.forms[0]["selectaddress"].length] = new Option(pca_description[i], pca_id[i]);
		                    document.forms[0]["selectaddress"].options[document.forms[0]["selectaddress"].length-1].title = pca_description[i];
		                  }
		                  
		               }
		         }
		   }
		
		function pcaFetchBegin()
		   {
			  var address_id = document.forms[0]["selectaddress"].value;
		      var scriptTag = document.getElementById("pcaScriptTag");
		      var headTag = document.getElementsByTagName("head").item(0);
		      var strUrl = "";

		      //Build the url
		      strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
		      strUrl += "&action=fetch";
		      strUrl += "&id=" + escape(address_id);
		      strUrl += "&account_code=" + escape(account_code);
		      strUrl += "&license_code=" + escape(license_code);
		      strUrl += "&style=raw";
		      strUrl += "&callback=pcaFetchEnd";

		      //Make the request
		      if (scriptTag)
		         {
		            //The following 2 lines perform the same function and are interchangeable
		            headTag.removeChild(scriptTag);
		            //scriptTag.parentNode.removeChild(scriptTag);
		         }
		      scriptTag = document.createElement("script");
		      scriptTag.src = strUrl
		      scriptTag.type = "text/javascript";
		      scriptTag.id = "pcaScriptTag";
		      headTag.appendChild(scriptTag);
		      
		      document.forms[0]["selectaddress"].style.display = 'none';
		      document.forms[0]["btnFetch"].style.display = 'none';
		      document.getElementById("divMessage").style.display = 'none';
		   }

		function pcaFetchEnd()
		   {
		      //Test for an error
		      if (pcaIsError)
		         {
		            //Show the error message
		            alert(pcaErrorMessage);
		         }
		      else
		         {
		            //Check if there were any items found
		            if (pcaRecordCount==0)
		               {
		                  alert("Sorry, no matching items found");
		               }
		            else
		               {
						  
						  document.forms[0]["ctl00$plcMain$ucCart$cartElem$wzdStep2$txtBillingAddr1"].value = '' + pca_line1[0];
						  var address2 = '' + pca_line2[0];
						  if( trim(pca_line3[0])!='') {address2 += ', '+ pca_line3[0];}
						  if( trim(pca_line4[0])!='') {address2 += ', '+ pca_line4[0];}
						  document.forms[0]["ctl00$plcMain$ucCart$cartElem$wzdStep2$txtBillingAddr2"].value = address2;						  
						  document.forms[0]["ctl00$plcMain$ucCart$cartElem$wzdStep2$txtBillingCity"].value = '' + pca_post_town[0];
						 
		               }
		         }
		   }
//-------------- Shipping Address ------------------------		   
		   function pcaByPostcodeBeginShipping()
		   {
			  var postcode = document.forms[0]["ctl00$plcMain$ucCart$cartElem$wzdStep2$txtShippingZip"].value;
			  
		      var scriptTag = document.getElementById("pcaScriptTag");
		      var headTag = document.getElementsByTagName("head").item(0);
		      var strUrl = "";
		      
		      if(trim(postcode) != "" )
		      {
		      document.getElementById("divLoadingShipping").style.display = '';
		      
		      //Build the url
		      strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
		      strUrl += "&action=lookup";
		      strUrl += "&type=by_postcode";
		      strUrl += "&postcode=" + escape(postcode);
		      strUrl += "&account_code=" + escape(account_code);
		      strUrl += "&license_code=" + escape(license_code);
		      strUrl += "&callback=pcaByPostcodeEndShipping";
		      
		      //Make the request
		      if (scriptTag)
		         {
		            //The following 2 lines perform the same function and are interchangeable
		            headTag.removeChild(scriptTag);
		            //scriptTag.parentNode.removeChild(scriptTag);
		         }
		      scriptTag = document.createElement("script");
		      scriptTag.src = strUrl
		      scriptTag.type = "text/javascript";
		      scriptTag.id = "pcaScriptTag";
		      headTag.appendChild(scriptTag);		      
		      
		      }
		      else
		      {
		        alert("Please enter a valid post code");
		      }
		      
		   }

		   function pcaByPostcodeEndShipping()
		   {
			  document.getElementById("divLoadingShipping").style.display = 'none';
			  
		      //Test for an error
		      if (pcaIsError)
		         {
		            //Show the error message
		            alert(pcaErrorMessage);
		         }
		      else
		         {
		            //Check if there were any items found
		            if (pcaRecordCount==0)
		               {
		                   alert("Sorry, no matching items found");
		                   document.forms[0]["selectaddressShipping"].style.display = 'none';
		                   document.forms[0]["btnFetchShipping"].style.display = 'none';
		                   document.getElementById("divMessageShipping").style.display = 'none';
		               }
		            else
		               {
		                   document.forms[0]["selectaddressShipping"].style.display = '';
		                   document.forms[0]["selectaddressShipping"].style.width = '440px';
		                   document.forms[0]["btnFetchShipping"].style.display = '';
		                   document.getElementById("divMessageShipping").style.display = '';
		                   
						  for (i=document.forms[0]["selectaddressShipping"].options.length-1; i>=0; i--){
							  document.forms[0]["selectaddressShipping"].options[i] = null;
							}
						  for (i=0; i<pca_id.length; i++){
		                    document.forms[0]["selectaddressShipping"].options[document.forms[0]["selectaddressShipping"].length] = new Option(pca_description[i], pca_id[i]);
		                    document.forms[0]["selectaddressShipping"].options[document.forms[0]["selectaddressShipping"].length-1].title = pca_description[i];
		                  }
		                  
		               }
		         }
		   }
		
		   function pcaFetchBeginShipping()
		   {
			  var address_id = document.forms[0]["selectaddressShipping"].value;
		      var scriptTag = document.getElementById("pcaScriptTag");
		      var headTag = document.getElementsByTagName("head").item(0);
		      var strUrl = "";

		      //Build the url
		      strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
		      strUrl += "&action=fetch";
		      strUrl += "&id=" + escape(address_id);
		      strUrl += "&account_code=" + escape(account_code);
		      strUrl += "&license_code=" + escape(license_code);
		      strUrl += "&style=raw";
		      strUrl += "&callback=pcaFetchEndShipping";

		      //Make the request
		      if (scriptTag)
		         {
		            //The following 2 lines perform the same function and are interchangeable
		            headTag.removeChild(scriptTag);
		            //scriptTag.parentNode.removeChild(scriptTag);
		         }
		      scriptTag = document.createElement("script");
		      scriptTag.src = strUrl
		      scriptTag.type = "text/javascript";
		      scriptTag.id = "pcaScriptTag";
		      headTag.appendChild(scriptTag);
		      
		      document.forms[0]["selectaddressShipping"].style.display = 'none';
		      document.forms[0]["btnFetchShipping"].style.display = 'none';
		      document.getElementById("divMessageShipping").style.display = 'none';
		   }

		function pcaFetchEndShipping()
		   {
		      //Test for an error
		      if (pcaIsError)
		         {
		            //Show the error message
		            alert(pcaErrorMessage);
		         }
		      else
		         {
		            //Check if there were any items found
		            if (pcaRecordCount==0)
		               {
		                  alert("Sorry, no matching items found");
		               }
		            else
		               {
						  
						  document.forms[0]["ctl00$plcMain$ucCart$cartElem$wzdStep2$txtShippingAddr1"].value = '' + pca_line1[0];						  
						  
						  var address2 = '' + pca_line2[0];
						  if( trim(pca_line3[0])!='') {address2 += ', '+ pca_line3[0];}
						  if( trim(pca_line4[0])!='') {address2 += ', '+ pca_line4[0];}
						  document.forms[0]["ctl00$plcMain$ucCart$cartElem$wzdStep2$txtShippingAddr2"].value = address2;						  
						  document.forms[0]["ctl00$plcMain$ucCart$cartElem$wzdStep2$txtShippingCity"].value = '' + pca_post_town[0];
						 
		               }
		         }
		   }
		   
		   
		   
//-------------- Profile Address ------------------------		   
		   function pcaByPostcodeBeginProfile()
		   {
			  var postcode = document.forms[0]["ctl00$plcMain$ucMyAccount$ucMyAddresses$txtAddressZip"].value;
			  
		      var scriptTag = document.getElementById("pcaScriptTag");
		      var headTag = document.getElementsByTagName("head").item(0);
		      var strUrl = "";
		      
		      if(trim(postcode) != "" )
		      {
		      document.getElementById("divLoadingProfile").style.display = '';
		      
		      //Build the url
		      strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
		      strUrl += "&action=lookup";
		      strUrl += "&type=by_postcode";
		      strUrl += "&postcode=" + escape(postcode);
		      strUrl += "&account_code=" + escape(account_code);
		      strUrl += "&license_code=" + escape(license_code);
		      strUrl += "&callback=pcaByPostcodeEndProfile";
		      
		      //Make the request
		      if (scriptTag)
		         {
		            //The following 2 lines perform the same function and are interchangeable
		            headTag.removeChild(scriptTag);
		            //scriptTag.parentNode.removeChild(scriptTag);
		         }
		      scriptTag = document.createElement("script");
		      scriptTag.src = strUrl
		      scriptTag.type = "text/javascript";
		      scriptTag.id = "pcaScriptTag";
		      headTag.appendChild(scriptTag);		      
		      
		      }
		      else
		      {
		        alert("Please enter a valid post code");
		      }
		      
		   }

		   function pcaByPostcodeEndProfile()
		   {
			  document.getElementById("divLoadingProfile").style.display = 'none';
			  
		      //Test for an error
		      if (pcaIsError)
		         {
		            //Show the error message
		            alert(pcaErrorMessage);
		         }
		      else
		         {
		            //Check if there were any items found
		            if (pcaRecordCount==0)
		               {
		                   alert("Sorry, no matching items found");
		                   document.forms[0]["selectaddressProfile"].style.display = 'none';
		                   document.forms[0]["btnFetchProfile"].style.display = 'none';
		                   document.getElementById("divMessageProfile").style.display = 'none';
		               }
		            else
		               {
		                   document.forms[0]["selectaddressProfile"].style.display = '';
		                   document.forms[0]["selectaddressProfile"].style.width = '450px';
		                   document.forms[0]["btnFetchProfile"].style.display = '';
		                   document.getElementById("divMessageProfile").style.display = '';
		                   
						  for (i=document.forms[0]["selectaddressProfile"].options.length-1; i>=0; i--){
							  document.forms[0]["selectaddressProfile"].options[i] = null;
							}
						  for (i=0; i<pca_id.length; i++){
		                    document.forms[0]["selectaddressProfile"].options[document.forms[0]["selectaddressProfile"].length] = new Option(pca_description[i], pca_id[i]);
		                    document.forms[0]["selectaddressProfile"].options[document.forms[0]["selectaddressProfile"].length-1].title = pca_description[i];
		                  }
		                  
		               }
		         }
		   }
		
		   function pcaFetchBeginProfile()
		   {
			  var address_id = document.forms[0]["selectaddressProfile"].value;
		      var scriptTag = document.getElementById("pcaScriptTag");
		      var headTag = document.getElementsByTagName("head").item(0);
		      var strUrl = "";

		      //Build the url
		      strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
		      strUrl += "&action=fetch";
		      strUrl += "&id=" + escape(address_id);
		      strUrl += "&account_code=" + escape(account_code);
		      strUrl += "&license_code=" + escape(license_code);
		      strUrl += "&style=raw";
		      strUrl += "&callback=pcaFetchEndProfile";

		      //Make the request
		      if (scriptTag)
		         {
		            //The following 2 lines perform the same function and are interchangeable
		            headTag.removeChild(scriptTag);
		            //scriptTag.parentNode.removeChild(scriptTag);
		         }
		      scriptTag = document.createElement("script");
		      scriptTag.src = strUrl
		      scriptTag.type = "text/javascript";
		      scriptTag.id = "pcaScriptTag";
		      headTag.appendChild(scriptTag);
		      
		      document.forms[0]["selectaddressProfile"].style.display = 'none';
		      document.forms[0]["btnFetchProfile"].style.display = 'none';
		      document.getElementById("divMessageProfile").style.display = 'none';
		   }

		function pcaFetchEndProfile()
		   {
		      //Test for an error
		      if (pcaIsError)
		         {
		            //Show the error message
		            alert(pcaErrorMessage);
		         }
		      else
		         {
		            //Check if there were any items found
		            if (pcaRecordCount==0)
		               {
		                  alert("Sorry, no matching items found");
		               }
		            else
		               {
						  
						  document.forms[0]["ctl00$plcMain$ucMyAccount$ucMyAddresses$txtAddressLine1"].value = '' + pca_line1[0];						  
						  
						  var address2 = '' + pca_line2[0];
						  if( trim(pca_line3[0])!='') {address2 += ', '+ pca_line3[0];}
						  if( trim(pca_line4[0])!='') {address2 += ', '+ pca_line4[0];}
						  document.forms[0]["ctl00$plcMain$ucMyAccount$ucMyAddresses$txtAddressLine2"].value = address2;						  
						  document.forms[0]["ctl00$plcMain$ucMyAccount$ucMyAddresses$txtAddressCity"].value = '' + pca_post_town[0];
						 
		               }
		         }
		   }