// (C) Copyright 2004 - 2010 Jeppessen Productions and TraciDesign.
// Prepared for the Property Power Website.
// Copyright: This software is protected by copyright laws and international copyright treaties, 
// as well as other intellectual property laws and treaties. The software is licensed not sold.
// Restriction: You may not attempt to reverse compile, modify, translate or disassemble this software 
// in whole or in part. You may not remove or modify any copyright notice.

function RoundNumbers(TheNum) {
	return TheNum;
}

function addComma(TheNum)
{
    numVar=String(TheNum);
    var commaNumber = "";
    var thenumber = ""
    var thefraction = "";
    var j = numVar.indexOf(".");
    var l = numVar.length;
    
    if (j > 0)
    {    
	 tvar=numVar+"00"; 
	 numVar=numVar.substr(0,j);
         for(i = j; i<j+3; i++) {     
               thefraction = thefraction + tvar.charAt(i); }
    }  else { 
	    thefraction = ".00"; }
	j = numVar.length;

    for (i = j; i > 0; i--) {
         if ((i != j) && ((j - i) % 3 == 0))
                thenumber = thenumber + ",";
         thenumber = thenumber + numVar.charAt(i-1);
    }
    j = thenumber.length;
    for (i = j; i >= 0; i--)
        commaNumber = commaNumber + thenumber.charAt(i);
    
    commaNumber=commaNumber+thefraction; 
    return commaNumber;
}

function remComma(TheNum)
{
    numVar=String(TheNum);
    var thenumber = ""
    var l = numVar.length;
    
    for (i = l; i > 0; i--) {
         if (numVar.charAt(i-1) != ",")
         	thenumber = thenumber + numVar.charAt(i-1);
    }
	
	numVar=""
	j = thenumber.length;
    for (i = j; i >= 0; i--)
        numVar = numVar + thenumber.charAt(i);
		
    return numVar;
}

function CalculateAll() {
var tot1 = 0;
var tot2 = 0;

	if (document.frmCalc.txtAmt.value==0) {
		alert("Please enter a purchase price.")
	}
		else
	{
	document.frmCalc.txtAmt2.value=document.frmCalc.txtAmt.value;

	tot1=CalculateTransferFees();
	tot2=CalculateBondFees();
	tot3=CalculateRepayments();
	
	document.frmCalc.txtTotal.value=addComma(RoundNumbers(tot1+tot2));	
	document.frmCalc.txtTtotal2.value=document.frmCalc.txtTtotal.value;
	document.frmCalc.txtBtotal2.value=document.frmCalc.txtBtotal.value;
	document.frmCalc.txtPurch2.value=addComma(RoundNumbers(document.frmCalc.txtPurch.value));
	document.frmCalc.txtAmt3.value=addComma(RoundNumbers(document.frmCalc.txtAmt.value));
	}
}

function ValidateNumberOnlyInput(allowdot) {
    if (!((event.keyCode>=48) && (event.keyCode<=57))) {
    	event.returnValue = false;}
    	
    if ((allowdot>0) && (event.keyCode==46)) {
        event.returnValue = true;
	}
}


function UpdateInfo() {
	document.frmCalc.txtAmt.value=document.frmCalc.txtPurch.value;	
	document.frmCalc.txtAmt.value=document.frmCalc.txtPurch.value-document.frmCalc.txtDep.value;
	document.frmCalc.txtMaxRepay.value=document.frmCalc.txtSalary.value*30/100;
	document.frmCalc.txtMonths.value=document.frmCalc.txtYears.value*12;		
}


function UpdateRepayment() {
	CalculateRepayments();
}

// TRANSFERING ATTORNEYS FEES

function GetTxferCost(Amt) {
var AmtTXfer1 = 1000;
var AmtTXfer2 = 500;
var AmtTXfer3 = 250;

	txfer=0;
	if (Amt<=80000) { txfer=3200; }
	if ((Amt>80000) && (Amt<=90000))  { txfer=3400; }
	if ((Amt>90000) && (Amt<=100000))  { txfer=3650; }
	if ((Amt>100000) && (Amt<=125000))  { txfer=3750; }
	if ((Amt>125000) && (Amt<=150000))  { txfer=3900; }
	if ((Amt>150000) && (Amt<=175000))  { txfer=4200; }
	if ((Amt>175000) && (Amt<=200000))  { txfer=4400; }
	if ((Amt>200000) && (Amt<=250000))  { txfer=4800; }
	if ((Amt>250000) && (Amt<=300000))  { txfer=5500; }
	if ((Amt>300000) && (Amt<=350000))  { txfer=5900; }
	if ((Amt>350000) && (Amt<=400000))  { txfer=6400; }
	if ((Amt>400000) && (Amt<=450000))  { txfer=6900; }
	if ((Amt>450000) && (Amt<=500000))  { txfer=7500; }

	x=0
	if (txfer==0) {
		txfer=txfer+7500;	
	
		if ((Amt>500000) && (Amt<=1000000))  { 
			x=Math.ceil((Amt-500000)/100000);
			txfer=txfer+(x*AmtTXfer1);
		
		}
		if ((Amt>1000000) && (Amt<=5000000))  { 
			x=Math.ceil((Amt-1000000)/100000);
			//alert(x);		
			txfer=txfer+(5*AmtTXfer1);
			txfer=txfer+(x*AmtTXfer2);
		}
		
		if (Amt>5000000)  { 
			x=Math.ceil((Amt-5000000)/100000);		
			txfer=txfer+(5*AmtTXfer1);
			txfer=txfer+(40*AmtTXfer2);
			txfer=txfer+(x*AmtTXfer3);
		}
	}
	return txfer;			
	
}

// BOND ATTORNEYS FEES 

function GetBondCost(Amt) {

var AmtBondC1 = 800;
var AmtBondC2 = 400;
var AmtBondC3 = 200;

	bond=0;
	if (Amt<=100000) { bond=2500; }
	if ((Amt>100000) && (Amt<=125000))  { bond=2600; }
	if ((Amt>125000) && (Amt<=150000))  { bond=2700; }
	if ((Amt>150000) && (Amt<=175000))  { bond=2900; }
	if ((Amt>175000) && (Amt<=200000))  { bond=3000; }
	if ((Amt>200000) && (Amt<=250000))  { bond=3300; }
	if ((Amt>250000) && (Amt<=300000))  { bond=3700; }
	if ((Amt>300000) && (Amt<=350000))  { bond=4100; }
	if ((Amt>350000) && (Amt<=400000))  { bond=4600; }
	if ((Amt>400000) && (Amt<=450000))  { bond=5000; }
	if ((Amt>450000) && (Amt<=500000))  { bond=5400; }	
	
	x=0
	if (bond==0) {
		bond=bond+5400;
	
		if ((Amt>500000) && (Amt<=1000000))  { 
			x=Math.ceil((Amt-500000)/100000);		
			bond=bond+(x*AmtBondC1);
		
		}
		if ((Amt>1000000) && (Amt<=5000000))  { 
			x=Math.ceil((Amt-1000000)/100000);		
			bond=bond+(5*AmtBondC1);
			bond=bond+(x*AmtBondC2);
		}
		
		if (Amt>5000000)  { 
			x=Math.ceil((Amt-5000000)/100000);		
			bond=bond+(5*AmtBondC1);
			bond=bond+(40*AmtBondC2);
			bond=bond+(x*AmtBondC3);
		}
	}
	return bond;	
}

// TRANSFER DUTY 
function GetTxferDuty(Amt) {
	txfd=0;
	if (document.frmCalc.rdg[0].checked) {
		//seller is a vat vendor
		return (0);		
	} else {
	//seller is a natural person
	if (document.frmCalc.rdg[1].checked) {
		x=0;
		if (Amt<=500000) { txfd=0; }
		if ((Amt>500000) && (Amt<=1000000))  { 
			x=(Amt-500000)*(5/100);		
			txfd=x; }
		if (Amt>1000000) { 
			x=(500000)*(5/100);
			x=x+(Amt-1000000)*(8/100);		
			txfd=x; }
	} else {
	//purchaser is a legal entity
		txfd=Amt*(8/100);	
	}
	return (txfd);
	}
}

function GetVat(Amt) {
	var VAT_rate=14
	return RoundNumbers(Amt * (VAT_rate / 100));
}

// PROPERTY TRANSFER FEES 
function CalculateTransferFees() {
	var Petties = 550;
	var SearchFee = 100;
	var Levies = 5000;
	var zz = 0;
	var Tfica = 250;
	var Tdocfee = 114;
	var tvat = 0;
	var runTotal = 0;
	

	v=document.frmCalc.txtPurch.value;
	tc=GetTxferCost(v);
	td=GetTxferDuty(v);
	tvat=GetVat(tc+Petties+SearchFee+Tfica+Tdocfee);

// PROPERTY TRANSFER DEEDS OFFICE LEVY (EXAMINATION) FEES 	
	exam=0;
	if (v<=150000) { exam=70; }
	if ((v>150000) && (v<=300000))  { exam=300; }
	if ((v>300000) && (v<=500000))  { exam=400; }
	if ((v>500000) && (v<=1000000))  { exam=500; }
	if ((v>1000000) && (v<=2000000))  { exam=600; }
	if ((v>2000000) && (v<=3000000))  { exam=800; }
	if ((v>3000000) && (v<=5000000))  { exam=1000; }
	if (v>5000000) { exam=1200; }


	document.frmCalc.txtTduty.value=addComma(td);
	document.frmCalc.txtTtariff.value=addComma(tc);
	document.frmCalc.txtTpost.value=addComma(Petties);
	document.frmCalc.txtTsearch.value=addComma(SearchFee);
	document.frmCalc.txtTVat.value=addComma(tvat);
	document.frmCalc.txtTrates.value=addComma(Levies);
	document.frmCalc.txtTexam.value=addComma(exam);
	document.frmCalc.txtTfica.value=addComma(Tfica);
	document.frmCalc.txtTdocfee.value=addComma(Tdocfee);	
	
	with (document.frmCalc) {
		
		runTotal=td+exam+tc+Petties+SearchFee+tvat+Levies+Tfica+Tdocfee;
		document.frmCalc.txtTtotal.value=addComma(RoundNumbers(runTotal));	
	}
	return runTotal;
}

// BOND REG. FEES  	
function CalculateBondFees() {
	var runTotal = 0;
	var Petties = 550;
	var Aktex = 100;
	var InitFee = 2000;
	var xyz = 0;
	var Bfica = 250;
	var Bdocfee = 114;
	var bvat = 0;
	
	
	v=document.frmCalc.txtAmt.value;
	bc=GetBondCost(v);
	bvat=GetVat(bc+Petties+Aktex+Bfica+Bdocfee);
	
	document.frmCalc.txtBtariff.value=addComma(bc);	
	document.frmCalc.txtBpost.value=addComma(Petties);

// BOND REG DEEDS OFFICE LEVY (EXAMINATION) FEES 	
	deeds=0;	
	if (v<=150000) { deeds=260; }
	if ((v>150000) && (v<=300000))  { deeds=300; }
	if ((v>300000) && (v<=500000))  { deeds=400; }
	if ((v>500000) && (v<=1000000))  { deeds=500; }
	if ((v>1000000) && (v<=2000000))  { deeds=650; }
	if ((v>2000000) && (v<=5000000))  { deeds=1000; }
	if (v>5000000) { deeds=2000; }
	
	document.frmCalc.txtBdeeds.value=addComma(deeds);
	document.frmCalc.txtBbank.value=addComma(InitFee);
	document.frmCalc.txtBaktex.value=addComma(Aktex);
	document.frmCalc.txtBfica.value=addComma(Bfica);
	document.frmCalc.txtBdocfee.value=addComma(Bdocfee);
	document.frmCalc.txtBvat.value=addComma(bvat);
	
	with (document.frmCalc) {
		runTotal=deeds+InitFee+bc+Petties+Aktex+bvat+Bfica+Bdocfee;
		
		document.frmCalc.txtBtotal.value=addComma(RoundNumbers(runTotal));	
	}
	return runTotal;
}

function CalculateRepayments() {
	var int_perc = 0;
	var monthly_int_rate=0;
	var month_term=0;
	
	v=document.frmCalc.txtAmt2.value;
	w=document.frmCalc.txtInt.value;
	y=document.frmCalc.txtTerm.value;
	
	int_perc=w/100;
	monthly_int_rate=int_perc/12;
	month_term=12*y;
	
	p=Math.pow((1+monthly_int_rate),(-1*month_term));
	r=v*(monthly_int_rate/(1-p));
	
	document.frmCalc.txtRepay.value=addComma(RoundNumbers(r));
//	CalculatePeriod();
}

function CalcTerm(CalcType) {

//document.frmCalc.txtRepay2.value="11"
//document.frmCalc.txtExtra.value="22"
//document.frmCalc.txtTerm2.value="33"

	x=1*(remComma(document.frmCalc.txtRepay.value));
	y=1*(remComma(document.frmCalc.txtExtra.value));
	z=1*(remComma(document.frmCalc.txtRepay2.value));
	
	if (CalcType==1) { document.frmCalc.txtRepay2.value=addComma(x+y); }
	if (CalcType==2) { if (z-x<=0) {document.frmCalc.txtExtra.value=0;} else {document.frmCalc.txtExtra.value=addComma(z-x)} }
	
	if (z-x<0) {
		document.frmCalc.txtTerm2.value=document.frmCalc.txtTerm.value;
	} else {
		document.frmCalc.txtTerm2.value=CalculatePeriod();
	}
}

function CalculatePeriod() {
	var int_val = 0;
	var month_term=0;
	
	v=1*(remComma(document.frmCalc.txtAmt.value));
	w=1*(remComma(document.frmCalc.txtInt.value));
	x=1*(remComma(document.frmCalc.txtRepay2.value));

	int_val=((12/w)*100);

	month_term=(-1*Math.log(1-(v/(x*int_val)))) / (Math.log(int_val+1)-Math.log(int_val));
	return  (Math.round((month_term/12)*10)/10);
}

function compute() { 
  var rate, endOfYear, annualAmt, nPer, answer; 
	
    rate = (frmCalc.txtInterest.value / 100) / 12; 
    nPer = frmCalc.txtMonths.value; 
    pmt = frmCalc.txtMaxRepay.value; 
    endOfYear = false;
    fv = 0; 
    answer = fv * Math.pow(1/(1+rate), nPer) + pmt * (1-Math.pow(1/(1+rate), nPer)) / rate; 

    if (endOfYear) { 
      answer = answer * (1+rate); 
    } 
  answer = Math.round(answer * 1000) / 1000; 
  return answer; 
} 


function UpdateInfoPV() {
	UpdateInfo();
	document.frmCalc.txtLoanQual.value=addComma(compute());	
}
