function calc(){
	var dimDiv = document.getElementById("dimDiv");
	
	var windowHeight = document.body.clientHeight+100;
	var windowWidth = document.documentElement.clientWidth;
	dim = document.createElement('div');
	dim.style.width = windowWidth+"px";
	dim.style.height = windowHeight+"px";	
	dim.style.background = "#000000";
	dim.style.zIndex = 2;
	dim.style.position = "absolute";
	dim.style.filter = 'alpha(opacity = 30)';
        dim.style.opacity = 0.3;
	dimDiv.appendChild(dim);
	
	divHolder = document.getElementById("calcHolder");
	divResult = document.createElement('div');	
	divResult.style.width = "176px";
	divResult.style.height = "212px";
	divResult.style.zIndex = 3;
	divResult.style.position = "absolute";
	divResult.style.marginTop = "125px";
	divResult.style.marginLeft = "75px";
	divResult.style.background = "url('images/bg_calc_result.png')";
	divHolder.appendChild(divResult);
	
	aCloser = document.createElement('img');
	aCloser.src = "images/btn_close.gif";
        aCloser.style.zIndex = 5;
	aCloser.style.position = "absolute";
	aCloser.style.margin = "15px 0 0 150px";
        aCloser.style.cursor = "pointer";
	if(aCloser.addEventListener){
            aCloser.addEventListener('click', closeResult, true);
        }else{            
            aCloser.attachEvent('onclick', closeResult);
        }
	divResult.appendChild(aCloser);
        
        divText = document.createElement('div');
        divText.setAttribute("class", "white");
        divText.style.padding = "15px 8px 0 8px";
        divText.style.lineHeight = "1.3";
        divResult.appendChild(divText);
        
       // divText.innerHTML = doCalc();
	   divText.innerHTML = computeForm();
}

function closeResult(){
	divResult.removeChild(aCloser);
	divHolder.removeChild(divResult);
        dimDiv.removeChild(dim);
}

function doCalc(){
    var propVal = parseFloat(document.getElementById("propVal").value);
    var deposit = parseFloat(document.getElementById("deposit").value);
    var intRate = parseFloat(document.getElementById("intRate").value);
    var term    = parseFloat(document.getElementById("term").value);
    var result = "";

    if(isNaN(propVal) || isNaN(deposit) || isNaN(intRate) || isNaN(term)){
        result = "You have made an error filling in this form. Please check and try again.";
    }
    else{
        propVal = propVal - deposit;
        intRate = intRate / 12 * 0.01;

        term = term * 12;
        term = (term - term * 2);


        var sum = Math.pow((1 + intRate), term);

        sum = 1 - sum;

        var repayment = intRate / sum;
        repayment = repayment * propVal;
        repayment = repayment.toFixed(2);

        var interest = propVal * intRate;
        interest = interest.toFixed(2);

        result = "Your estimated<br/>monthly repayments are:<br/><br/>Repayment<br/>";
        result+= "<div class=\"resultBox\">&pound;"+repayment+"</div>";
        result+= "Interest Only<br/>";
        result+= "<div class=\"resultBox\">&pound;"+interest+"</div>";
        result+= "<a href=\"mortgages.php#contact\"><img id=\"contactAdvisor\" src=\"images/btn_contact_advisor.gif\" alt=\"Contact Advisor\" width=\"134\" height=\"23\" /></a>";
        result+= "<a on";
		result+= "click=\"";
		result+= "al";
		result+= "ert('This calcuator is based on how much you want to borrow and calculated against the initial rate.  It does not consider any repayments after the end of a deal period.  The calculation is for illustration purposes only and may vary according to your personal circumstances and the mortgage provider.' )\" class=\"white pointer\" ><span class=\"font09em\">Terms Of Use</span></a>";
    }
    return result;
}

function numericInput(e){
        /*
	* This function only allows the input of numeric characters (Key codes 48 - 57 incl.),
	* The period key (Key Code 46) and the tab and backspace key (Key Codes 0 and 8 respectively (Non IE only))
	*/
	var key;
	var keyChar;
	var result;
	if (window.event){
   		key = window.event.keyCode;
	}
	else if (e){
   		key = e.which;
	}
	if(key == 0 || key == 8){
		result = true;
	}else{
		if(key >= 46 && key <= 57){
			if(key == 47){
				result = false;
			}else{
				result = true;
			}
		}else{
			result = false;
		}
	}
	return result;
}



//checks that data are valid 

	function checkNumber(input, min, max, msg) {



		msg = "The " + msg + " field is invalid. ";



		//this makes sure that the number is a number

		var str = input.value;

		for (var i = 0; i < str.length; i++) {

			var ch = str.substring( i, i + 1)

			if ((ch < "0" || "9" < ch) && ch != '.') {

				alert(msg);

				return false;

			}

		}



		//this makes sure that the number lies between the min and max values allowed

		var num = 0 + str

		if (num < min || max < num) {

			alert(msg + "The figure should be between " + min + " and " + max + ".");

			return false;

		}

		input.value = str;

		return true;

	}



	function computeField(input) {

		if (input.value != null && input.value.length != 0)

		{

			input.value = "" + eval(input.value);

		}

		computeForm(input.form);

		//clearForm(input.form);

		return false;

	}



	function computeForm(form) {
		
    	var result = "";
	
		var A=parseFloat(document.getElementById("propVal").value); // Principal amount
		
		var D=parseFloat(document.getElementById("deposit").value); // Deposit amount
		
		var T=parseFloat(document.getElementById("term").value); // Term - number of years

		var R=parseFloat(document.getElementById("intRate").value); // Rate of interest

		A = parseFloat(A-D); // Amount to be borrowed

		//making sure that an entry has been made in each field.

		if ((A == null || A.length == 0) ||

			(R == null || R.length == 0)) 

		{

			//alert('not all fields filled in');

			return;

		}



		// making sure that entries are valid by using check number

		if (!checkNumber(document.getElementById("propVal"), 1, 99999999, "'Mortgage required'") ||

			!checkNumber(document.getElementById("intRate"), .001, 1000, "Interest rate") ||

			!checkNumber(document.getElementById("term"), 5, 40, "Repayment period")) 

		{

			form.Cm.value = "Invalid";

			return;

		}



		// maths et al to be computed

		R = R / 100;

		var repayment = ((A*R)/12) * (1/(1-(Math.pow(1/(1+R),T))));

		repayment = poundsPence( repayment );

		var interest = (A*R)/12;

		interest = poundsPence( interest );
		
		result = "Your estimated<br/>monthly repayments are:<br/><br/>Repayment<br/>";
        result+= "<div class=\"resultBox\">&pound;"+repayment+"</div>";
        result+= "Interest Only<br/>";
        result+= "<div class=\"resultBox\">&pound;"+interest+"</div>";
        result+= "<a href=\"mortgages.php#contact\"><img id=\"contactAdvisor\" src=\"images/btn_contact_advisor.gif\" alt=\"Contact Advisor\" width=\"134\" height=\"23\" /></a>";
        result+= "<a on";
		result+= "click=\"";
		result+= "al";
		result+= "ert('This calcuator is based on how much you want to borrow and calculated against the initial rate.  It does not consider any repayments after the end of a deal period.  The calculation is for illustration purposes only and may vary according to your personal circumstances and the mortgage provider.' )\" class=\"white pointer\" ><span class=\"font09em\">Terms Of Use</span></a>";
		
		return result;

	}



	function poundsPence( N ) {

		// won't work as intended in ie3

		if ((navigator.appName.indexOf('Microsoft')>-1)

			&& (navigator.appVersion.indexOf('3.0')>-1) )

		{

			return N;

		}

		S = new String( N );

		var i = S.indexOf('.');

		if (i != -1) {

			S = S.substr( 0, i+3 );

			if (S.length-i < 3)

				S = S + '0';

		}

		return S;

	}



	//clears form

	function clearForm(form) {

		

		form.A.value = "";

		form.T.value = "";

		form.R.value = "";

		

		form.Cm.value = "";

		form.CI.value = "";

		form.CCm.value = "";

		form.CCI.value = "";

		return false;

	}

