//Sale Form Calculations
var principal;
var landreg;
var costrate;
var costs;
var vatval;
var stotal;
var undefined;
var svat;
var bank_sale = 36; 
var lh;
var l_hold;
var	vat = 200/1000; // VAT
// Varibles for Anti Money Laundering
var amls = 480/100; // Anti Money Laundering Search - £4.80 per name.
var now; // number of owners input from slae form
var amlstotal; // Total AMLS vaue

function calculate() {

    principal = document.saleprice.principal.value;
	now = document.saleprice.no_own.value; //Obtain value from form on No Owners
	//Multiply and round to two decomal places
	amlstotal = Math.round((now*amls)*Math.pow(10,2))/Math.pow(10,2) ;
    landreg = 11.42; 
  	lh = document.saleprice.s_lease;
  	{if (lh.checked == true) 
	{l_hold=100}
	else
	l_hold=0
	}
	
    costrate = 600;  //changed from 500 26.09.10
    if (principal >= 250001)
    {costrate = 650}
	if (principal >= 350001)
    {costrate = 700}
    if (principal >= 500001)
    {costrate = 750}
   // if (principal >= 500001)
    //{costrate = 725}
    if (principal >= 750001)
    {costrate = 850} 
	if (principal >= 1000001)
    {costrate = 995}
 
    costs = costrate;
	
	if (l_hold >= 100)
	{
	vatval = (costrate + l_hold) * vat;
	}
	else
	{
	vatval = (costrate) * vat;
	}
	
	document.saleprice.box.value = l_hold
	
    document.saleprice.landreg.value = landreg;
	document.saleprice.amlsfee.value = amlstotal;  // AMLS by BS SALE
    document.saleprice.bank_sale.value = bank_sale;
    document.saleprice.salecosts.value = costs;


    svat = (Math.round(vatval * 100))/100;
    document.saleprice.vat.value = svat;
    
	if (l_hold >= 100)
	{
	stotal=(Math.round((vatval + landreg + bank_sale + costs + l_hold + amlstotal) * 100))/100;
	}
	else
	{
	stotal=(Math.round((vatval + landreg + bank_sale + costs + amlstotal) * 100))/100;
	}
    
    document.saleprice.total.value = stotal;

{if (document.saleprice.principal.value.length == 0){
saleprice.reset();
alert('You must enter a figure!')
return false;
}}


{if (principal < 30000){
saleprice.reset();
alert('Sale price too low!')
return false;
}} 

{if ( principal != parseInt(principal) ){ 
saleprice.reset();
alert('Numbers only please!')
return false;
}}

//Purchase Form Calculations
}

var purchase;
var stamprate;
var regrate;
var plcostsrate;
var stamp;
var reg;
var searchfee;	//search changed to searchfee
var bank;
var plcosts;
var pvat;
var ptotal;
var p_lhold;
var p_lh;
var sdlt;
var bankrupt;
var lregsrch;
var admin;
// Varibles for Anti Money Laundering
var pamls = 480/100; // Anti Money Laundering Search - £4.80 per name.
var pnow; // number of owners input from purchase form
var pamlstotal; // Total AMLS vaue

function calculate2() {
        purchase = document.purchaseprice.purchase.value;
		pnow = document.purchaseprice.p_no_own.value; //Obtain value from form on No Owners
		pamlstotal = Math.round((pnow*pamls)*Math.pow(10,2))/Math.pow(10,2) ;
			p_lh = document.purchaseprice.p_lease;
			{if (p_lh.checked == true)
			{pl_hold = 100}
			else
			pl_hold = 0
			}

      stamprate = 0;    
        if (purchase >= 125001)  //changed on 05/01/2010
        {stamprate = 10}  
        if (purchase >= 250001)
        {stamprate = 30}
        if (purchase >= 500001)
        {stamprate = 40}  //was50 -- cahged back to 40 26.3.10
        if (purchase >= 1000001) // new entry added on 05.04.2011
		{stamprate = 50}
      regrate = 50;
        if (purchase >= 50001)
        {regrate = 80}
        if (purchase >= 80001)
        {regrate = 130}
        if (purchase >= 100001)
        {regrate = 200}
        if (purchase >= 200001)
        {regrate = 280}
        if (purchase >= 500001)
        {regrate = 550}
        if (purchase >= 1000001)
        {regrate = 920}

      plcostsrate = 675; //plcostsrate = 500; chaged 02.10.10
        //if (purchase >= 100001)
       //{plcostsrate = 675}   //plcostsrate = 525
        //if (purchase >= 150001)
        //{plcostsrate = 675}
       // if (purchase >= 200001)
        //{plcostsrate = 675}
        if (purchase >= 225001)
        {plcostsrate = 700}
        //if (purchase >= 350001)
        //{plcostsrate = 700}
       // if (purchase >= 400001)
        //{plcostsrate = 700}
        if (purchase >= 450001)
        {plcostsrate = 750}
        if (purchase >= 500001)
        {plcostsrate = 850}
        if (purchase >= 750001)
        {plcostsrate = 900}
        if (purchase >= 1000001)
        {plcostsrate = 1000}
		
		admin = 15.00; // 14.69;  // changed on 05/01/2010
		
		if (pl_hold >= 100)
		{
		pvat = (pl_hold + plcostsrate) * vat; 
		}
		else
		{
		pvat = (plcostsrate) * vat;
		}
		
		document.purchaseprice.pbox.value = pl_hold

        stamp = document.purchaseprice.purchase.value * stamprate/1000;
		
	reg = regrate;
	searchfee = 250; //165; //250;  //changed on 05/01/2010
	bank = 36; //27;
	sdlt = 75; //60; //55;
	bankrupt = 5.02; //6.00;//5.00; //4.63;//rate changed 12/9/06
	lregsrch = 6.04; //23.63;// Land regitry and Check cancel search fee changed on 10/03/2010
	
	plcosts = plcostsrate;
	
	document.purchaseprice.bankrupt_fee.value = bankrupt;
	document.purchaseprice.sdlt_fee.value = sdlt;
	document.purchaseprice.lreg_search.value = lregsrch;
	document.purchaseprice.admin_fee.value = admin;
    document.purchaseprice.stampduty.value = stamp;
	document.purchaseprice.lreg.value = reg;
	document.purchaseprice.searchfee.value = searchfee;
	document.purchaseprice.bank.value = bank;
	document.purchaseprice.pamlsfee.value = pamlstotal //BS
	document.purchaseprice.purcosts.value = plcosts;
	document.purchaseprice.purvat.value = (Math.round(pvat * 100))/100;
	
	ptotal = (Math.round((stamp + reg + searchfee + bank + plcosts + pvat + sdlt + bankrupt + lregsrch + admin + pamlstotal) * 100))/100; //added new costs
        document.purchaseprice.purtotal.value = ptotal


{if (document.purchaseprice.purchase.value.length == 0){
purchaseprice.reset();
alert('You must enter a figure!')
return false;
}}


{if (purchase < 30000){
purchaseprice.reset();
alert('Purchase price too low!')
return false;
}}


{if ( purchase != parseInt(purchase) ){
purchaseprice.reset();
alert('Numbers only please!')
return false;
}}

}

//var title;		//added this to var list 19/08/03
var forename;
var surname;
var staddr1;
var staddr2;
var town;
var county;
var postcode;
var tel;
var fax;
var email;

function readform() {

        // Read the fields from the form into variables
        title = document.inputform.title.value;
        forename = document.inputform.forename.value;
        surname = document.inputform.surname.value;
        staddr1 = document.inputform.staddr1.value;
        //staddr2 = document.inputform.staddr2.value;
        town = document.inputform.town.value;
        county = document.inputform.county.value;
        postcode = document.inputform.postcode.value;
        tel = document.inputform.tel.value;
        fax = document.inputform.fax.value;
        email = document.inputform.email.value;
        mortcomp = document.inputform.mortcomp.value;
        mortacc = document.inputform.mortacc.value;

        }

// forms(0)
function writepage() {
	// Start writing to the document...
	document.open();
	document.write ("<HTML><BODY BGCOLOR='#ffffff'>");
	//document.write ('<link href="bkgnd.css" rel="stylesheet" type="text/css">');


        // Sale info
              //If Sale has no value in it print message
              if ((principal == undefined) || (principal == 0)){
              document.write ("<P>No sale price entered.  If you are expecting to see a price here go back and press calculate again.</P><BR><BR><BR>"); 
             }
               else
             {
              document.write ("<P>Sale Price:<B>" +" £"+ principal + "</B></P>");
			  document.write ("<P>Leasehold:<B>" +" £"+ l_hold + "</B></P>");
              document.write ("<P>Land Registry Office Copy Entries:<B>" +" £"+ landreg + "</B></P>");
              document.write ("<P>Pumfrey and Lythaby costs:<B>" +" £"+ costs + "</B></P>");
              document.write ("<P>VAT:<B>"+" £"+ svat + "</B></P>");
              document.write ("<P>Total:<B>" +" £"+ stotal + "</B></P><BR><BR><BR>");
              }
             
        // Purchase info
              //If Purchase has no value in it print message
              if ((purchase == undefined) || (purchase == 0)){
              document.write ("<P>No purchase price entered.  If you are expecting to see a price here go back and press calculate again.</P><BR><BR><BR>"); 
              }
              else
              {
              document.write ("<P>Purchase Price:<B>" + " £"+ purchase + "</B></P>");
			  document.write ("<P>Leasehold:<B>" +" £"+ pl_hold + "</B></P>");
              document.write ("<P>Stamp Duty:<B>" + " £" + stamp + "</B></P>");
			  document.write ("<P>Stamp Duty Land Tax:<B>" + " £"+ sdlt + "</B></P>");
			  document.write ("<P>Bankruptcy Search Fee:<B>" + " £"+ bankrupt + "</B></P>");
			  document.write ("<P>Land Registry Search Fee:<B>" + " £"+ lregsrch + "</B></P>");
              document.write ("<P>Land Registry Fees:<B>" + " £" + reg + "</B></P>");
              document.write ("<P>Search Fees:<B>" + " £" + searchfee + "</B></P>");
              document.write ("<P>Bank Telegraphic Transfer Fee:<B>" + " £" + bank + "</B></P>");
			  document.write ("<P>Administration Fee:<B>" + " £"+ admin + "</B></P>");
              document.write ("<P>Pumfrey and Lythaby Costs:<B>" + " £" + plcosts + "</B></P>");
              document.write ("<P>VAT:<B>" + " £" + pvat + "</B></P>");
              document.write ("<P>Total:<B>" + " £" + ptotal + "</B></P><BR><BR><BR>");
              }

      // Personal details
	document.write ("<P>Name:<B>" + title + " " + forename + " " + surname + "</B></P>");
	document.write ("<P>Address:</P>" + "<P><B>" + staddr1 + "</B></P>");
	document.write ("<P><B>" + town + "</B></P>");
	document.write ("<P><B>" + county + "</B></P>");
	document.write ("<P><B>" + postcode + "</B></P>");
	document.write ("<P>Telephone number :<B>" + tel + "</B></P>");
	document.write ("<P>Fax number :<B>" + fax + "</B></P>");
	document.write ("<P>Email address :<B>" + email + "</B></P>");
	document.write ("<P>Mortage Company :<B>" + mortcomp + "</B></P>");
	document.write ("<P>Mortage Account Number :<B>" + mortacc + "</B></P>");

	// Start a form on our new page...
	document.write ("<FORM name='FormX' method='post' action='/cgi-bin/FormMail.pl'>");

	// Write the information into hidden fields so we can send it back to the recipient
	// Watch out for nested quotes! Use double and single quotes.

             // Sale hidden
              document.write ("<input type='hidden' name='Sale Price' value='" + principal + "'>");
			  document.write ("<input type='hidden' name= 'Sale Leasehold' value='" + l_hold + "'>");
              document.write ("<input type='hidden' name='Land Registry Office Copy Entries' value='" +  landreg + "'>");
              document.write ("<input type='hidden' name='Pumfrey and Lythaby costs' value='" +  costs + "'>");
              document.write ("<input type='hidden' name='Sale VAT' value='" + svat + "'>");
              document.write ("<input type='hidden' name= 'Sale Total' value='" +  stotal + "'>");

             // Purchase hidden
              document.write ("<input type='hidden' name='Purchase Price' value='" + purchase + "'>");                                                                 document.write ("<input type='hidden' name='Stamp duty' value='" + stamp + "'>");
              document.write ("<input type='hidden' name='Purchase Leasehold' value='" + pl_hold + "'>");
			  document.write ("<input type='hidden' name='Stamp Duty' value='" + stamp + "'>");
			  document.write ("<input type='hidden' name='stamp duty land tax' value='" + sdlt + "'>");
			  document.write ("<input type='hidden' name='bankruptcy search fee' value='" + bankrupt + "'>"); 
			  document.write ("<input type='hidden' name='land registry search fee' value='" + lregsrch + "'>");
			  document.write ("<input type='hidden' name='Land Registry Fees' value='"  + reg + "'>");
              document.write ("<input type='hidden' name= 'Search Fees' value='"  +  searchfee + "'>");
              document.write ("<input type='hidden' name= 'Bank Telegraphic Transfer Fee' value='"  +  bank + "'>");
			  document.write ("<input type='hidden' name= 'Administration fee' value='"  +  admin + "'>");
              document.write ("<input type='hidden' name= 'Pumfrey and Lythaby Costs' value='"  +  plcosts + "'>");
              document.write ("<input type='hidden' name= 'Purchase VAT' value='"  +  pvat + "'>");
              document.write ("<input type='hidden' name= 'Purchase Total' value='"  +  ptotal + "'>");

             // Personal details hidden
	document.write ("<input type='hidden' name='Name' value='" + title + " " + forename + " " + surname + "'>");
	document.write ("<input type='hidden' name='Address' value='" + staddr1 + "'>");
	document.write ("<input type='hidden' name='Town' value='" + town + "'>");
	document.write ("<input type='hidden' name='County' value='" + county + "'>");
	document.write ("<input type='hidden' name='Postcode' value='" + postcode + "'>");
	document.write ("<input type='hidden' name='Telephone Number' value='" + tel + "'>");
	document.write ("<input type='hidden' name='Fax Number' value='" + fax + "'>");
	document.write ("<input type='hidden' name='Email Address' value='" + email + "'>");
	document.write ("<input type='hidden' name='Mortgage Company' value='" + mortcomp + "'>");
	document.write ("<input type='hidden' name='Mortage Account Number' value='" + mortacc+ "'>");

             // Mail stuff
	document.write ("<input type='hidden' name='recipient' value='enquiries@pumf.co.uk'>");
	document.write ("<input type='hidden' name='redirect' value='http://www.pumf.co.uk/files/thanks_2.htm'>");
	document.write ("<input type='hidden' name='subject' value='Sale_Purchase_Form'>");

	// Display some buttons to go back and to post the form
	document.write ("<P>");
	document.write ("<input type='button' value='Back to details' onclick='history.back();'>");
              document.write (" ");
	document.write ("<input type='submit' value='Send' name='SendForm'>");
	document.write ("</P>");

	// Finish and close the document
	document.write ("</FORM>");
	document.write ("</BODY></HTML>");
	document.close();
}

function go() {
	// Read the existing form and then write a new page
	readform();
	writepage();
}
