<!--
/*-----------------------------------------------------------------------------------------*
*Author:S. Mohammed Alsharaf
*Date start:15/11/2006
*Date end:16/11/2006
*Email:devteam@hairylemon.co.nz
*Project: MCF Calculator v1.0
*------------------------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------------------*	
*The following variable can be edit.
* - These are created below or stored in the variables.js file
* - PPSRFee 		-->> is variable that hold the value of PPSR fee.
* - EstablishmentFee	-->> is variable that hold the value of Establishment Fee.
* - AdminFee		-->> is variable that hold the value of Monthly Admin Fee.
* - List		-->> is array that hold the text in the list box for the repayment frequency. The first value is the data of the variable " List[0] " which is Select.
* - txt and txt2	-->> are two variable hold the text that users will see in the calculation result. (see line 238 and 239)
* - Rate1,...etc	-->> each hold the value of the Interest Rate.
* - Rate1		-->> for Amount Financed less than 10000.
* - Rate2		-->> for Amount Financed bigger than 10000 and less than 15000.
* - Rate3		-->> for Amount Financed bigger than 15000.
* - Frequency		-->> is variable for the cost of the Repayment Frequency (Weekly - Fortnightly - Monthly). No need to add value to it.
* - Frequency1		-->> if the Frequency selected is "Weekly" this is the cost will be used.
* - Frequency2		-->> if the Frequency selected is "Fortnightly" this is the cost will be used.
* - Frequency3		-->> if the Frequency selected is "Monthly" this is the cost will be used.
*-----------------------------------------------------------------------------------------*/	

var List = new Array();
List[0] = 'Select';
List[1] = 'Weekly';
List[2] = 'Fortnightly';
List[3] = 'Monthly';

var Rate;
var Frequency;	

	
/*-----------------------------------------------------------------------------------------*/	
	
	/*total gross  cost variable*/
	var TotalGross;
	
	/*purchase variable*/
	var Price;
	
	/*error var*/
	var error;
	
	/*var for the select box text and tags*/
	var listTxt_1;
	var listTxt_2;
	var listTxt_3;

	/*the above less the following*/
	var Deposit;
	var Allowance;
	var totalDepAllw;

	/*the different between the above*/
	var AmountFinanced;

	/*number of months*/
	var LoanTerm;

	/*monthly payemnt var*/
	var MonthlyPayment;

	/*result payement*/
	var resultRepayment;

	/*validation function*/
	function validate_form(Price,Deposit,Frequency,LoanTerm)
	{
		if ( Price == "" )
		{
			error = ( "Please fill in the 'Price' box." );
			return false;
		}

		if ( Deposit == "" )
		{
			error = ( "Please fill in the 'Deposit' box." );
			return false;
		}

		if ( LoanTerm == "" )
		{
			error = ( "Please fill in the 'Loan Term' box." );
			return false;
		}

		if ( Frequency == 0 )
		{
			error = ( "Please select the 'Monthly Frequency'." );
			return false;
		}

		if(isNaN(Price) || Price < 0 )
		{
			 error = ("Only positive numbers are allowed for 'Price' box.");
			return false;
		}

		if(isNaN(Deposit) || Deposit < 0 )
		{
			 error = ("Only positive numbers are allowed for 'Deposit' box.");
			return false;
		}

		if(isNaN(LoanTerm) || LoanTerm < 0 )
		{
			 error = ("Only positive numbers are allowed for 'Loan Term' box.");
			return false;
		}

		return true;
	}

	/*do the calculation*/
	function Calculator()
	{
		/*grab the data from the form*/
		Price 		= document.calForm.Price.value;
		Price		= removeComma(Price);
		Deposit 	= document.calForm.Deposit.value;
		Deposit		= removeComma(Deposit);
		//Allowance 	= document.calForm.Allowance.value;
		//Allowance	= removeComma(Allowance);
		Frequency 	= document.calForm.Frequency.value;
		Frequency	= removeComma(Frequency);
		LoanTerm	= document.calForm.LoanTerm.value;
		LoanTerm	= removeComma(LoanTerm);

		/*if there is error show it else run the calculation*/
		if(validate_form(Price,Deposit,Frequency,LoanTerm) == false)
		{
			alert(error);
		}
		else
		{

			/*total gross cost*/
			TotalGross 	= parseFloat(Price) + parseFloat(PPSRFee) + parseFloat(EstablishmentFee);

			//alert("TotalGross: " + TotalGross);

			/*total deposit*/
			totalDepAllw 	= parseFloat(Deposit);

			//alert("total deposit: " +totalDepAllw);

			/*the different between total deposit and total gross cost*/
			AmountFinanced 	= parseFloat(TotalGross) - parseFloat(totalDepAllw);

			//alert("AmountFinanced: " +AmountFinanced);

			/*find the right rate depend on the Amount financed*/
			if(TotalGross < 10000)
			{
				Rate = Rate1;
			}
			else if(TotalGross > 10000 && AmountFinanced < 15000)
			{
				Rate = Rate2;
			}
			else if(TotalGross > 15000)
			{
				Rate = Rate3;
			}

			/*calculate monthly payemnt*/
			var i;
			var m;

			i = Rate / 12;

			//alert("i: " + i);

			m = Math.pow((1 + i),LoanTerm);

			//alert("m: " + m);

			MonthlyPayment = (AmountFinanced * i * m) / (m - 1);

			//alert("MonthlyPayment: " + MonthlyPayment);

			/*Calculating Indicative Repayment*/
			/*find the Frequency value depend on the user selection*/
			if(Frequency == 1)
			{
				Frequency = Frequency1;
				FrqSelected = List[1];
			}
			else if(Frequency == 2)
			{
				Frequency = Frequency2;
				FrqSelected = List[2];
			}
			else if(Frequency == 3)
			{
				Frequency = Frequency3;
				FrqSelected = List[3];
			}

			p = MonthlyPayment + AdminFee;
			resultRepayment = Math.round(p / Frequency * 100) / 100;

			//alert("resultRepayment: " + resultRepayment);

			/*if the calculation is positive value show it else show error message.*/
			if(resultRepayment > 0)
			{
				// result text
				var txt  = "Your "+ FrqSelected +" Repayment is:  <span id=PaymentValue>"
				var txt2 = "</span><br />(This is an indicative estimate only and is subject to MCF normal lending criteria.)";

				document.getElementById('result').style.visibility="visible";
				 
				/*if the result bigger than 1000 then use comma in the result*/
				if(resultRepayment < 1000)
				{
				  document.getElementById('result').innerHTML= txt + "&#36;" + resultRepayment + txt2;
				}
				else
				{
				  document.getElementById('result').innerHTML= txt + "&#36;" + addCommas(resultRepayment)+ txt2;
				}
				
			}
			else
			{
				alert("Please try again for some reason your calculation returned 0 value!");
			}

		} // end validation
	} // end function


	/*if the input number has a comma then this function will remove it for the calculate or the validation will return error
	  that this input is not number*/
	function removeComma(S)
	{
		S = S.replace(/,/, '') // for input
		return S;
	}

	/*function to add comman in the number for a better reading*/
	function addCommas(numberStr)
	{
		numberStr += '';
		x = numberStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}

	/*reset button to clear the form*/
	function clearForm()
	{
		document.calForm.Price.value=""
		document.calForm.Deposit.value=""
		//document.calForm.Allowance.value=""
		document.calForm.LoanTerm.value=""
		document.calForm.Frequency.value=""
		document.getElementById('result').style.visibility="hidden"
		document.getElementById('result').innerHTML= "";
		document.calForm.Price.focus()
	}

	/*this function will will hide the result text if the users changed one of the fields.*/
	function checkIt(id)
	{
		if(document.calForm.id.value != "")
		{
			document.getElementById('result').style.visibility="hidden"
		}
	}

	/*on load run this function to set the curse into Price box and generate the list box*/
	function onLoadDo() 	{
	  /*set focus to Price box*/
	  document.calForm.Price.focus();

	  /*create the list box*/
	  var p = List.length;
	  var i= 0;
	  for(i=0 ; i < p ; i++)
	  {
	    listTxt_2 +=  "<option value=" + i + ">" + List[i] + "</optio>";
	  }
	 
	 listTxt_1 = "<label><b>Select your preferred Repayment Frequency</b></label><select name=Frequency  onfocus=checkIt(this.id)>";
	 listTxt_3 = "</select>";
	 document.getElementById('frequency').innerHTML= listTxt_1 + listTxt_2 + listTxt_3;

	}
//-->