function updateNumber (data, textStatus) {
	$(".savedPropertiesNumber").text($.evalJSON(data).size);
	$(".tabsSavedPropertiesNumber").text($.evalJSON(data).size);
}

function addSavedProperty(id, event) {
    event.stopImmediatePropagation();
	$.get("/searchajax/saveproperty/id/"+id,"", updateNumber);
    $('#savedLink_'+id).text('Saved');
    $('#savedLink_'+id).attr('title','Saved');
    $('#savedLink_'+id).attr('class','saved');
    $('#savedLink_'+id).unbind('click');
    $('#savedLink_'+id).click(function(event) {
        return deleteSavedProperty(id, event);
    });
    return false;
}

function deleteSavedProperty(id, event) {
    event.stopImmediatePropagation();
    $.get("/searchajax/deleteproperty/id/"+id,"", updateNumber);
    $('#savedLink_'+id).text('Save');
    $('#savedLink_'+id).attr('title','Save');
    $('#savedLink_'+id).attr('class','');
    $('#savedLink_'+id).unbind('click');
    $('#savedLink_'+id).click(function(event) {
        return addSavedProperty(id, event);
    });
    return false;
}

function cleanUpProperties() {
	$.get("/searchajax/cleanupproperties", updateNumber);
	$("#unmatch").hide();
}

function swapFloorplan (src) {
	document.floorplan.src = src;
}

function mc_calculatedeposit() {
	 var price = $('#price').val();
	 var deposit = $('#deposit').val();
	 
	 if (isNaN(price)) {
		 $('#price').val("");
		 return;
	 }
	 
	 if (isNaN(deposit)) {
		 $('#mortgage').val("");
		 return;
	 }
	 
	 var mortgage = price-deposit;
	 if (mortgage<0) {
         mortgage=0;
     }
	 $('#mortgage').val(mortgage);
}

function decimalPlaces(numberToFix, noDecimalPlaces ) {
	var div = Math.pow(10,noDecimalPlaces);
	numberToFix = Math.round(numberToFix* div) /div;
	return numberToFix;
}

function mc_calculateinterest() {
	var status = true;
	var price = $('#price').val();
	var deposit = $('#deposit').val();
	var mortgage = $('#mortgage').val();	
	var duration = $('#duration').val();		
	var rate = $('#rate').val();
	
	if (price === "" || isNaN(price)) {
		alert("You must enter a Property Price");
		status = false;
		$('#price').focus();
	}
	if (mortgage === "" || isNaN(mortgage)) {

		alert("You must enter a Mortgage Amount");
		status = false;
		$('#mortgage').focus();
	}

	if (duration === "" || isNaN(duration)) {
		alert("You must enter the Mortgage Duration (measured in years)");
		status = false;
		$('#duration').focus();
	}

	if (isNaN(deposit)) {
		alert("The Deposit Amount must be a number");
		status = false;
		$('#deposit').focus();
	}


	if (status === true)
	{
		rate = rate/100;
		rate = rate/12;
		//calculate interest portion monthly payment to 2 decimal places
		var interest= ((rate*12)*(mortgage)*1)/12;	
		$('#resultinterest').val(decimalPlaces(interest,2));

		//calculate interest & capital monthly payment to 2 decimal places
		loanDuration = duration *12;
		var capitalInterest =decimalPlaces((mortgage *(Math.pow((1+rate), loanDuration ))*rate)/(Math.pow((1+rate),loanDuration)-1),2);

		capitalInterest = parseFloat(capitalInterest);
		$('#resultcapital').val(capitalInterest);
	}
	
	return false;
}


