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.01
  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 && d.getElementById) x=d.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];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

// Used to toggle the visibility of a div..
function toggleDivVisibility(divname) {
    if (document.getElementById(divname).style.visibility == "hidden") showDiv(divname);
    else hideDiv(divname);
}

// Used to show a div...
function showDiv(divname) {
    document.getElementById(divname).style.display="block";
}

// Used to hide a div...
function hideDiv(divname) {
    document.getElementById(divname).style.display="none";
}


function toggleDiv(id){
	if(id=='new homes'){
		document.getElementById('sales_div').style.display='none';
		document.getElementById('property_of_interest_div').style.display='inline';
	}
	else if(id=='sales'){
		document.getElementById('property_of_interest_div').style.display='none';
		document.getElementById('sales_div').style.display='inline';
	}
	else{
		document.getElementById('sales_div').style.display='none';
		document.getElementById('property_of_interest_div').style.display='none';
	}
}



function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


var error_message = "Please correct the following errors: \n";

function isFieldValid(fieldname,fieldvalue){
	if((fieldvalue == "") || (!IsNumeric(fieldvalue))){
	error_message += "Invalid value '" + fieldvalue + "' for " + fieldname + "\n";
	errors = true;
	return false;
	}
	else {
	return true;
	}

}

function isFieldNumeric(fieldname,fieldvalue){
	if(!IsNumeric(fieldvalue)){
	error_message += "Invalid value '" + fieldvalue + "' for " + fieldname + "\n";
	errors = true;
	return false;
	}
	else {
	return true;
	}

}

function calculateBorrowAmount() {

	var total_income_app1 = 0;
	var total_income_app2 = 0;
	var single_applicant = 0;
	var joint_applicant = 0;

	var app1_income = 0;
	var app2_income = 0;
	var app1_commission = 0;
	var app2_commission = 0;

	var borrow_amount = 0;

	var errors = false;

	app1_income = document.getElementById('app1_income').value;
	app2_income = document.getElementById('app2_income').value;
	app1_commission = document.getElementById('app1_commission').value;
	app2_commission = document.getElementById('app2_commission').value;

	// Validate fields
	if(isFieldValid('app1_income',app1_income) == false) { errors = true };
	if(isFieldNumeric('app1_commission',app1_commission) == false) { errors = true };

	// Only validate if details entered for applicant 2
	if((app2_income == "") || (app2_income == 0)) {
		//alert("No Errors");
	}
	else { 
		if(isFieldValid('app2_income',app2_income) == false) { errors = true };
		if(isFieldNumeric('app2_commission',app2_commission) == false) { errors = true };
	}

	app1_income = Number(app1_income);
	app1_commission = Number(app1_commission);
	app2_commission = Number(app2_commission);
	app2_income = Number(app2_income);

	if(errors == true)
	{
		alert(error_message);
		error_message = "Please correct the following errors: \n";
	}else {
		total_income_app1 = app1_income + (app1_commission * 0.5);
		total_income_app2 = app2_income + (app2_commission * 0.5);
	
		single_applicant = Number(total_income_app1 * 3.0);
		joint_applicant = Number((total_income_app1 + total_income_app2) * 3.0);
	
		if(single_applicant > joint_applicant){
			borrow_amount = Number(single_applicant);
		}
		else {
			borrow_amount = Number(joint_applicant);
		}
		
		// Update form fields...
		document.getElementById('max_amount').value = borrow_amount;
		document.getElementById('borrow_amount').value = borrow_amount;	
	}
}

function calculateMonthlyPayments() {
	
	var repayment_type = document.getElementById("mortgage_type")[document.getElementById("mortgage_type").selectedIndex].value;
	var repayment_amount = 0;	
	var number_of_payments = 0;
	var borrow_amount = document.getElementById('borrow_amount').value;
	var loan_duration = Number(document.getElementById("loan_duration")[document.getElementById("loan_duration").selectedIndex].value);
	var interest_rate_1 = document.getElementById('interest_rate_1').value
	var interest_rate_2 = document.getElementById('interest_rate_2').value;
	var interest_rate = 0;
	
	var errors = false;

	// Validate fields
	if(isFieldValid('borrow_amount',borrow_amount) == false) { errors = true };
	if(isFieldValid('interest_rate_1',interest_rate_1) == false) { errors = true };
	if(isFieldValid('interest_rate_2',interest_rate_2) == false) { errors = true };

	borrow_amount = Number(borrow_amount);
	interest_rate = interest_rate_1 + "." + interest_rate_2;

	if(errors == true)
	{
		alert(error_message);
		error_message = "Please correct the following errors: \n";
	}else {
		if(repayment_type == "Interest-only"){
			repayment_amount = (borrow_amount * (interest_rate / 100)) / 12;
		} else {
			number_of_payments = loan_duration * 12;
			rate = (interest_rate / 100) / 12;
			repayment_amount = (borrow_amount * rate) / (1- Math.pow((1 + rate), 0 - number_of_payments));
		}
		
		repayment_amount = repayment_amount * 100;
		repayment_amount = Math.round(repayment_amount);
		repayment_amount = repayment_amount / 100;

		// Update form fields...
		document.getElementById('monthly_payment').value = repayment_amount;
	}

}


//here you place the ids of every element you want.
var ids=new Array('salessearch','lettingspanel','newhomespanel');

function switchid(id){
	hideallids(); 
	showdiv(id); 
} 
function hideallids(){
	
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){ 
	hidediv(ids[i]); } }
	
function hidediv(id) {
	//safe function to hide an element with a specified id
		
if (document.getElementById) {
	// DOM3 = IE5, NS6
	document.getElementById(id).style.display = 'none'; 
	}
	
	else {
		if (document.layers) { // Netscape 4 
		document.id.display = 'none'; } 
	
	else { // IE 4 
	document.all.id.style.display = 'none'; } 
	} 
}
	
function showdiv(id) { 
	//safe function to show an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
	document.getElementById(id).style.display = 'block';
	} 
else 
	{ if (document.layers) { // Netscape 4
	document.id.display = 'block'; } 
	else { // IE 4 
	document.all.id.style.display = 'block'; }
} 
}

