
<!--
//Change the following variables if costs change
VAT = 17.5;
PurchasingLegalCostIncVat = 527.58;
SaleLegalCostIncVat = 527.58;
Lenders_Legal_Fee = 176.25;
Local_Search_Fee = 128;
LandChargesSearch = 8;
BankTransferFee = 35.25;
Office_Copy_Entries = 8;

function CalculateTotal() { //function to calculate form values
	//Remove any commas in the field values
	PurchaseCost = document.form1.Property_Price.value;
	PurchaseParts = PurchaseCost.split(",");
	PurchaseCost = PurchaseParts.join("");
	PurchaseCost = parseFloat(PurchaseCost);

	
	SaleCost = document.form1.Expected_Sale_Price.value;
	SaleParts = SaleCost.split(",");
	SaleCost = SaleParts.join("");
	SaleCost = parseFloat(SaleCost);
	
	if(document.form1.Obtaining_Mortgage.selectedIndex == 1){
	Lenders_Legal_Fee	 = 176.25;
	} else {
	Lenders_Legal_Fee	 = 0;
	}
	
	if(document.form1.Leasehold.selectedIndex == 2){
		document.form1.Leasehold_Fee_Purchase.value = "N/A";
	} else if(document.form1.Leasehold.selectedIndex == 1) {
		document.form1.Leasehold_Fee_Purchase.value = "See Note *";
	}
	
	if(document.form1.Leasehold_Fee_2.selectedIndex == 2){
		document.form1.Leasehold_Fee.value = "N/A";
	} else if(document.form1.Leasehold_Fee_2.selectedIndex == 1) {
		document.form1.Leasehold_Fee.value = "See Note *";
	}
	
	//define stamp duty fee
	if (PurchaseCost <= 125000) {
	Stamp_Duty	 = 0;
	} else if (PurchaseCost <= 250000) {
	Stamp_Duty	 = PurchaseCost / 100;
	} else if (PurchaseCost <= 250000) {
	Stamp_Duty	 = (PurchaseCost / 100) * 1;
	} else if (PurchaseCost <= 500000) {
	Stamp_Duty	 = (PurchaseCost / 100) * 3;
	} else {
	Stamp_Duty	 = (PurchaseCost / 100) * 4;
	}
	
	//Define land registry fee
	if (PurchaseCost <= 50000) {
		LandRegistry = 40;
	} else if (PurchaseCost <= 80000) {
		LandRegistry = 60;
	} else if (PurchaseCost <= 100000) {
		LandRegistry = 100;
	} else if (PurchaseCost <= 200000) {
		LandRegistry = 150;
	} else if (PurchaseCost <= 500000) {
		LandRegistry = 220;
	} else if (PurchaseCost <= 1000000) {
		LandRegistry = 450;
	} else {
		LandRegistry = 750;
	}
	
	//Update form Purchase field values
	if (PurchaseCost > 0 || PurchaseCost == "NaN") {
		document.form1.Property_Price.value = formatCurrency(PurchaseCost);
		document.form1.StepB_1.value = formatCurrency(PurchasingLegalCostIncVat/(1+(VAT/100)));
		document.form1.StepB_1_VAT.value = formatCurrency((PurchasingLegalCostIncVat/(1+(VAT/100)))*(VAT/100));
		document.form1.Stamp_Duty.value = formatCurrency(Stamp_Duty);
		document.form1.Land_Registry_Fee.value = formatCurrency(LandRegistry);
		document.form1.Lenders_Legal_Fee.value = formatCurrency(Lenders_Legal_Fee);
		document.form1.Local_Search_Fee.value = formatCurrency(Local_Search_Fee);
   		document.form1.Bankruptcy_or_Land_Charges_Searches.value = formatCurrency(LandChargesSearch);
		document.form1.Telegraphic_Transfer.value = formatCurrency(BankTransferFee);
	Total_Cost_of_Purchase	 = PurchasingLegalCostIncVat+Stamp_Duty+LandRegistry+Lenders_Legal_Fee+Local_Search_Fee+LandChargesSearch+BankTransferFee;
		document.form1.Total_Cost_of_Purchase.value = formatCurrency(Total_Cost_of_Purchase);
	} else {
		document.form1.Property_Price.value = 0;
		document.form1.StepB_1.value = 0;
		document.form1.StepB_1_VAT.value = 0;
		document.form1.Stamp_Duty.value = 0;
		document.form1.Land_Registry_Fee.value = 0
		document.form1.Lenders_Legal_Fee.value = 0;
		document.form1.Local_Search_Fee.value = 0;
		document.form1.Bankruptcy_or_Land_Charges_Searches.value = 0;
		document.form1.Telegraphic_Transfer.value = 0;
	Total_Cost_of_Purchase	 = 0;
		document.form1.Total_Cost_of_Purchase.value = 0;
	}
	
	//Update form Sale field values
	if (SaleCost > 0 || SaleCost == "NaN") {
		document.form1.Expected_Sale_Price.value = formatCurrency(SaleCost);
		document.form1.LegalCosts_for_Sale.value = formatCurrency(SaleLegalCostIncVat/(1+(VAT/100)));
		document.form1.LegalCosts_for_Sale_VAT.value = formatCurrency((SaleLegalCostIncVat/(1+(VAT/100)))*(VAT/100));
		document.form1.Office_Copy_Entries.value = formatCurrency(Office_Copy_Entries);
	Total_Cost_of_Sale	 = SaleLegalCostIncVat+Office_Copy_Entries;
		document.form1.Total_Cost_of_Sale.value = formatCurrency(Total_Cost_of_Sale);
	} else {
		document.form1.Expected_Sale_Price.value = 0;
		document.form1.LegalCosts_for_Sale.value = 0;
		document.form1.LegalCosts_for_Sale_VAT.value = 0;
		document.form1.Office_Copy_Entries.value = 0;
	Total_Cost_of_Sale	 = 0;
		document.form1.Total_Cost_of_Sale.value = 0;
	}
	
	//Update the grand total
	document.form1.Grand_Total.value = formatCurrency(Total_Cost_of_Purchase+Total_Cost_of_Sale);
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

function Validate() {
	if(parseFloat(document.form1.Grand_Total.value) == 0 || document.form1.Grand_Total.value == "") {
		alert("Please enter a purchase or sale price!");
		return false;
	}
	if(document.form1.Leasehold.selectedIndex == 0 && (parseFloat(document.form1.Property_Price.value) != 0 && document.form1.Property_Price.value != "")){
		alert("Please select whether the property you are purchasing is leasehold or not!");
		return false;
	}
	if(document.form1.Obtaining_Mortgage.selectedIndex == 0 && (parseFloat(document.form1.Property_Price.value) != 0 && document.form1.Property_Price.value != "")){
		alert("Please select whether you are obtaining a Obtaining_Mortgage or not!");
		return false;
	}
	if(document.form1.Leasehold_Fee_2.selectedIndex == 0 && (parseFloat(document.form1.Expected_Sale_Price.value) != 0 && document.form1.Expected_Sale_Price.value != "")){
		alert("Please select whether the property you are buying is leasehold or not!");
		return false;
	}
	if(document.form1.StepC_1.value.length < 1) {
		alert("Please enter your name!");
		return false;
	}
	if(document.form1.StepC_2.value.length < 1) {
		alert("Please enter your address!");
		return false;
	}
	if(document.form1.StepC_3.value.length < 1) {
		alert("Please enter your telephone number!");
		return false;
	}
	if(document.form1.StepC_4.value.length < 1) {
		alert("Please enter your email address!");
		return false;
	}
}
printed=false;
function setFormAction(formAction){
	if(formAction=="print"){
		oldAction=form1.action;
		oldTarget=form1.target;
		form1.action="conveyancing_print.html";
		form1.target="_blank";
		printed=true;
	}else if(formAction=="submit" && printed){
		form1.action=oldAction;
		form1.target=oldTarget;
		printed=true;
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->

