/***********************************************************************
*	Check all required fields are present for registation
*
************************************************************************/
// 
function checkadddetails(phone,post,add1)
{	
	if (!phone)
		{
		getError('phone');
		document.forms['add_form'].FC_PHONE.focus();
        return false;
        } 
	else {checkPhone(phone);}
	if (!post)
	{
			getError('post');
			document.forms['add_form'].FC_POSTALCODE.focus();
        	return false;
        } 
	if (!add1)
		{
			getError('add1');
			document.forms['add_form'].FC_POSTALCODE.focus();
        	return false;
        } 
	document.forms['add_form'].submit();
}

/**************************************
*	Get new Address Button
***************************************/
function addressbut(type)
{
var butv6="<SCRIPT LANGUAGE=JAVASCRIPT SRC='https://services.postcodeanywhere.co.uk/popups/javascript.aspx?account_code=farme11111&license_key=cx99-ce28-fz53-uw76'></SCRIPT>";

var but1S ="<input type='button' value='Find Address' onClick='";
var but1E ="' id=btnFind name=btnfind><BR />";
var divload ="<div id='divLoading' style='display:none'>Loading. Please wait...</div>";
var selS ="<select name='selectaddress' id='selectaddress' style='display:none'></select>";
var but2S ="<br><input type='button' value='Select' onClick='";
var but2E ="' id=btnFetch name=btnFetch style='display:none'>";
	var post = "pcaByPostcodeBegin()";	
	var selectbut ="pcaFetchBegin()";
	
		document.write(but1S);
		document.write(post);
		document.write(but1E);
		document.write(divload);
		document.write(selS);
		document.write(but2S);
		document.write(selectbut);
		document.write(but2E);
}
/********************************************************
* Get address from postcode plus open diary
*********************************************************/
		function pcaByPostcodeBegin()
		   {
			  var account_code = 'farme11111';
			  var license_code = 'ru95-ry19-bp19-ku64';
			  var machine_id ='';
			  var postcode = document.forms[0]["FC_POSTALCODE"].value;
		      var scriptTag = document.getElementById("pcaScriptTag");
		      var headTag = document.getElementsByTagName("head").item(0);
			  //alert (headTag + '<br>' + scriptTag);
		      var strUrl = "";
		      
		      document.getElementById("divLoading").style.display = '';
		      
		      //Build the url
		      strUrl = "https://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 += "&machine_id=" + escape(machine_id);
		      strUrl += "&callback=pcaByPostcodeEnd";		      
		      //Make the request
		      if (scriptTag)
		         {
		            headTag.removeChild(scriptTag);
		         }
		      scriptTag = document.createElement("script");
		      scriptTag.src = strUrl
		      scriptTag.type = "text/javascript";
		      scriptTag.id = "pcaScriptTag";
		      headTag.appendChild(scriptTag);	
		
		   }

		function pcaByPostcodeEnd()
		   {
			  document.getElementById("divLoading").style.display = 'none';
			  
		      //Test for an error
		      if (pcaIsError)
		         {
		            //Show the error message
		            document.getElementById("selectaddress").style.display = 'none';
		            document.getElementById("btnFetch").style.display = 'none';
		            alert(pcaErrorMessage);
		         }
		      else
		         {
		            //Check if there were any items found
		            if (pcaRecordCount==0)
		               {
		                  document.getElementById("selectaddress").style.display = 'none';
						  document.getElementById("btnFetch").style.display = 'none';
		                  alert("Sorry, no matching items found. Please try another postcode.");
		               }
		            else
		               {
						  document.forms[0]["selectaddress"].style.display = '';
						  document.forms[0]["btnFetch"].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]);
		                  }
		               }
		         }
		   }
		
		function pcaFetchBegin()
		   {
			  var account_code = 'farme11111';
			  var license_code = 'ru95-ry19-bp19-ku64';
			  var machine_id ='';
			  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 = "https://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 += "&machine_id=" + escape(machine_id);
		      strUrl += "&callback=pcaFetchEnd";

		      //Make the request
		      if (scriptTag)
		         {
		            headTag.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';
		   }

		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
		               {
						// add address details into form
						document.forms[0]["FC_ADDRESS1"].value =pca_line1[0];
						document.forms[0]["FC_ADDRESS2"].value =pca_line2[0]+' '+ pca_line3[0];
						var add3 = pca_post_town[0];
						add3=add3.toUpperCase();						
						document.forms[0]["FC_ADDRESS3"].value = add3;
						document.forms[0]["FC_ADDRESS4"].value =pca_county[0];
						document.forms[0]["FC_POSTALCODE"].value =pca_postcode[0];
						str = 'Selected Address:&nbsp;'+pca_line1[0]+' '+ pca_line2[0]+' '+ pca_line3[0]+' '+ pca_county[0] +' '+ pca_postcode[0];
						document.getElementById('address').innerHTML = str;
		               }
		         }
		   }


