function sendback(path)
{
var dest = path;
var sURL= dest;
location.replace(sURL);
return true;
}

/***********************************************************************
*	Check all required fields are present for registation
*
************************************************************************/
// 
function checkdetails(phone,name,mobile,fstname,title,post,add1)
{
	if (!fstname)
		{
			getError('fstname');
			document.forms['cus_reg'].INVOICEFIRSTNAME.focus();
        	return false;
        } 
	if (!name)
		{
			getError('name');
			document.forms['cus_reg'].INVOICENAME.focus();
        	return false;
        } 		
	if (!phone)
		{
			if(!mobile)
			{
			getError('phone');
			document.forms['cus_reg'].FC_PHONE.focus();
        	return false;
			}
        } 
	else {checkPhone(phone);}
	if (!mobile)
		{
			if(!phone)
			{
			getError('phone');
			document.forms['cus_reg'].FC_PHONE.focus();
        	return false;
			}
        }
	if (!post){
			getError('post');
			document.forms['cus_reg'].FC_POSTALCODE.focus();
        	return false;
        } 
	if (!add1){
			getError('add1');
			document.forms['cus_reg'].FC_POSTALCODE.focus();
        	return false;
        } 
	document.forms['cus_reg'].submit();
}
/***************************************************
* PHONE NUMBER CHECKER
* This function checks PHONE NUMBER and returns true or false
* True Patterns = 023 92571567 or 01489 583828 or 01489 58382
***************************************************/
function checkPhone(string) 
{
	if (string.search(/^([0-9][0-9][0-9]( )[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$)/)!= -1
		|| string.search(/^([0-9][0-9][0-9][0-9][0-9]( )[0-9][0-9][0-9][0-9][0-9][0-9]$)/)!= -1
		|| string.search(/^([0-9][0-9][0-9][0-9][0-9]( )[0-9][0-9][0-9][0-9][0-9]$)/)!= -1)
	{ 
		hideError();
		return true;
	}
	else 
	{
		getError('phone_wrong');
		document.forms['cus_reg'].FC_PHONE.focus();
        return false;
     }
}
/***************************************************
* PHONE NUMBER CHECKER
* This function checks PHONE NUMBER and returns true or false
* True Patterns = 023 92571567 or 01489 583828 or 01489 58382
***************************************************/
function checkmobPhone(string) 
{
	/*if (string.search(/^([0-9][0-9][0-9][0-9][0-9]( )[0-9][0-9][0-9][0-9][0-9][0-9]$)/)!= -1)
	{ 
		//hideError();
		return true;
	}
	else 
	{
		getError('mobphone_wrong');
		document.forms['cus_reg'].INVOICEMOBILE.focus();
        return false;
     }*/
}
function checkFirstName(string)
{
   	if (string.length > 1)
		{
        return true;
		format(string,'initials');
        } 
		else 
		{
        alert( "Your first name '" + string + "' must be more than 1 character long.");
		document.forms['cus_reg'].INVOICEFIRSTNAME.focus();
        return false;
    	}
}
/********************************************************
* Email CHECKER
* This function checks email and returns true or false
* True Pattern = @ or .
*********************************************************/ 
function checkemail(str) 
{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   getError('email');
		   document.forms['eml_chk'].FC_EMAIL.focus();
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   getError('email');
		   document.forms['eml_chk'].FC_EMAIL.focus();
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    getError('email');
			document.forms['eml_chk'].FC_EMAIL.focus();
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    getError('email');
			document.forms['eml_chk'].FC_EMAIL.focus();
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    getError('email');
			document.forms['eml_chk'].FC_EMAIL.focus();
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    getError('email');
			document.forms['eml_chk'].FC_EMAIL.focus();
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    getError('email');
			document.forms['eml_chk'].FC_EMAIL.focus();
		    return false
		 }
		 hideError();
 		 return true					
	}
/********************************************************
* Format data input
********************************************************/
function format(el,f)
{
if (!el.value)
{
	return false;
}
if (f=="upper")
{
	el.value=el.value.toUpperCase();
	return false;
}
if (f=="post")
{
	el.value=(el.value.split(" ")).join("");
	el.value=el.value.substring(0,(el.value.length-3))+" "+el.value.substring((el.value.length-3),el.value.length)
	el.value=el.value.toUpperCase();
	}
else if (f=="coupon")
	{
	el.value=(el.value.split(" ")).join("");
	el.value=el.value.toUpperCase();
	}
else if (f=="lower"){
	el.value=el.value.toLowerCase();
	}
else if (f=="capitals"){
	el.value=el.value.toLowerCase();
	var re=/(^.|\s.)/gi;
	var arr=el.value.match(re);
	for (i=0;i<arr.length;i++){
		el.value=el.value.replace(arr[i],arr[i].toUpperCase());
		}
	var re=/(Mc(.)|Mac(.)|O'(.))/g;
	var arr=el.value.match(re);
	if (arr){
		for (i=0;i<arr.length;i++){
			el.value=el.value.replace(arr[i],arr[i].substring(0,arr[i].length-1)+(arr[i].substring(arr[i].length-1,arr[i].length)).toUpperCase());
			}
		}
	}
else if (f=="initials")
	{
		el.value=el.value.toLowerCase();
		var re=/(^.|\s.)/gi;
		var arr=el.value.match(re);
		for (i=0;i<arr.length;i++){
		el.value=el.value.replace(arr[i],arr[i].toUpperCase());
		}
		var re=/(Mc(.)|Mac(.)|O'(.))/g;
		var arr=el.value.match(re);
		if (arr){
		for (i=0;i<arr.length;i++){
			el.value=el.value.replace(arr[i],arr[i].substring(0,arr[i].length-1)+(arr[i].substring(arr[i].length-1,arr[i].length)).toUpperCase());
			}
		}
		createintial(el.value);		
	}
}
/////////////////////////////////////////
function createintial(firstname)
{
	var char = firstname.charAt(0);
	char = char.toUpperCase();
	document.forms['0']['INVOICEINITS'].value = char;
}
/**************************************
*	Get new Address Button
***************************************/
function adminaddressbut(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 = "adminpcaByPostcodeBegin()";	
	var selectbut ="adminpcaFetchBegin()";
	
		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 adminpcaByPostcodeBegin()
		   {
			  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=adminpcaByPostcodeEnd";		      
		      //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 adminpcaByPostcodeEnd()
		   {
			  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 adminpcaFetchBegin()
		   {
			  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=adminpcaFetchEnd";

		      //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 adminpcaFetchEnd()
		   {
		      //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];
		               }
		         }
		   }




