function isDigit(event){
	var char = (event.which) ? event.which : event.keyCode;
	 
	// Handle the left/right arrow keys, backspace, enter, end, home, delete:
	if (char == 37 || char == 39 || char == 8 || char == 13 || char == 35 || char == 36 || char == 46)
		return true;
    
    // If not a number, don't do anything:
    if (char > 31 && (char < 48 || char > 57))
    	return false;

    return true;
}

/*function uppercase(event){
//	key = window.event.keyCode;
	var key = (event.which) ? event.which : event.keyCode;
	if ((key >= 97) && (key <= 122))
//	window.event.keyCode = key-0x20;
	if (event.which) 
		event.which = key-32;
	else 
		event.keyCode = key-32;
}*/
function updateMonthPrice(){
	if ($('lettingsPriceValue'))
	    $('lettingsPriceValue').value = getWeekPrice($('monthLettinsBudget').value);
    else
	    $('lettingsPrice').value = getWeekPrice($('monthLettinsBudget').value);    
}
function updateWeekPrice(){
    $('monthLettinsBudget').value = getMonthPrice(($('lettingsPriceValue')) ? 
    		$('lettingsPriceValue').value : $('lettingsPrice').value);
}
function updateMonthPriceInPenny(){
    $('lettingsPriceValue').value = getDoubleWeekPrice($('monthLettinsBudget').value);
}
function updateWeekPriceInPenny(){
    $('monthLettinsBudget').value = getMonthPrice($('lettingsPriceValue').value);
}
function getMonthPrice(weekPrice){
	return (isNaN(weekPrice.toInt())? 0 : Math.round(weekPrice * 52 / 12));
}
function getWeekPrice(monthPrice){
	return (isNaN(monthPrice.toInt())? 0 : Math.round(monthPrice * 12 / 52));;
}
function getDoubleWeekPrice(monthPrice){
	return (isNaN(monthPrice.toInt())? 0 : Math.round(monthPrice * 12 * 100 / 52) / 100);;
}
function updateFloorAreaMetre(){
	$('floorAreaMetre').value = getFloorAreaMetre($('floorArea').value);
}
function updateFloorAreaFt(){
	$('floorArea').value = getFloorAreaFt($('floorAreaMetre').value);
}
function getFloorAreaMetre(floorAreaFt){
	return (isNaN(floorAreaFt.toInt())? 0 : Math.round(floorAreaFt * 0.0929034 * 100) / 100);;
}
function getFloorAreaFt(floorAreaMetre){
	return (isNaN(floorAreaMetre.toInt())? 0 : Math.round(floorAreaMetre * 10.7638673163 * 100) / 100);;
}

function getMinPrice(price, deviationPercent){
	return (isNaN(price.toInt()) || isNaN(deviationPercent.toInt())) ? 0 : (price.toInt() - Math.round(price.toInt() * deviationPercent.toInt() / 100));
}
function getMaxPrice(price, deviationPercent){
	return (isNaN(price.toInt()) || isNaN(deviationPercent.toInt())) ? 0 : (price.toInt() + Math.round(price.toInt() * deviationPercent.toInt() / 100));
}
function getAvgPrice(minPrice, maxPrice){
	return (isNaN(minPrice.toInt()) || isNaN(maxPrice.toInt())) ? 0 : Math.round((minPrice.toInt() + maxPrice.toInt())/2);
}

function getRoundPrice(price){
	return isNaN(price.toInt()) ? 0 : Math.round(price*100)/100;
}
function getDeviation(minPrice, maxPrice){
	if (isNaN(minPrice.toInt()) || isNaN(maxPrice.toInt())) return 0;
	var avg = getAvgPrice(minPrice, maxPrice);
	if (avg == 0) return 0;
	return Math.round((avg - minPrice) * 100 / avg);
}

function parseDate(dateStr){
	var pattern = /(\d+) ([a-zA-Z]{3}) (\d+)/i;
	var result = pattern.exec(dateStr);
	if (result) {
		return new Date(result[3], getMonthNum(result[2]), result[1]);
	}
	return null;
}

function addDays(date, n) {
	var d = new Date();	
	d.setTime(date.getTime() + n * 24 * 60 * 60 * 1000);
	return d;
}

function getMonthNum(monthStr){
	for (var i=0; i<12; i++){
		if (g_Calendar.months[i].substr(0, 3) == monthStr)
			return i;
	}
	return 0;
}

function mortgage_calculator() {
    var a = document.getElementById("amount").value;
    var p = document.getElementById("period").value;
    var r = document.getElementById("rate").value;
//    document.getElementById("calc0").style.display = "block";
//    document.getElementById("calc1").style.display = "block";
//    document.getElementById("calc2").style.display = "block";
//    document.getElementById("calc3").style.display = "block";
    if (a.indexOf("\xA3") >= 0) {
        a = a.replace("\xA3", "");
    }
    if (a.indexOf(",") >= 0) {
        a = a.replace(",", "");
    }
    if (r.indexOf("%") > 0) {
        r = r.replace("%", "");
    }
    if (p.indexOf("yrs") > 0) {
        p = p.replace("yrs", "");
    }
    if (p.indexOf("ys") > 0) {
        p = p.replace("ys", "");
    }
    if (p.indexOf("years") > 0) {
        p = p.replace("years", "");
    }
    if (p.indexOf("year") > 0) {
        p = p.replace("year", "");
    }
    if (p.indexOf("yers") > 0) {
        p = p.replace("yers", "");
    }
    if (p.indexOf("yars") > 0) {
        p = p.replace("yars", "");
    }
    p = trim(p);
    var ismonth = "";
    if (p.indexOf("months") > 0) {
        p = p.replace("months", "");
        ismonth = "1";
    }
    if (p.indexOf("mnths") > 0) {
        p = p.replace("mnths", "");
        ismonth = "1";
    }
    if (p.indexOf("moths") > 0) {
        p = p.replace("moths", "");
        ismonth = "1";
    }
    if (p.indexOf("mths") > 0) {
        p = p.replace("mths", "");
        ismonth = "1";
    }
    if (p.indexOf("mth") > 0) {
        p = p.replace("mth", "");
        ismonth = "1";
    }
    if (p.indexOf("mnth") > 0) {
        p = p.replace("mnth", "");
        ismonth = "1";
    }
    if (ismonth == "1") {
        p = p / 12;
    }
    if (IsNumeric(a) && IsNumeric(p) && IsNumeric(r)) {
        r = r / 100;
        var v = a * r / 12 * (1 / (1 - Math.pow(1 / (1 + r), p)));
        document.getElementById("monthly").innerHTML = "&pound;" + roundit(v, 0);
        v = a * r / 12;
        document.getElementById("interest").innerHTML = "&pound;" + roundit(v, 0);
    } else {
        alert("We are sorry but you seem to have entered some invalid data.  Please try again. \n");
    }
}

function calcOnFocus(sender){
	if(sender.type !=='checkbox' && sender.type !=='radio' && (sender.tagName =='INPUT' || sender.tagName =='TEXTAREA')) {
		sender.className="textformfocus";
		if (sender.value.charAt(0) == '-') { sender.value='' }
	}
}

function trim(str) {
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
} 


function roundit(Num, Places) {
    if (Places > 0) {
        if (Num.toString().length - Num.toString().lastIndexOf(".") > Places + 1) {
            var Rounder = Math.pow(10, Places);
            return Math.round(Num * Rounder) / Rounder;
        } else {
            return Num;
        }
    } else {
        return Math.round(Num);
    }
}

function IsNumeric(strString) {
    var strValidChars = "0123456789.-";
    var strChar;
    var blnResult = true;
    if (strString.length == 0) {
        return false;
    }
    for (i = 0; i < strString.length && blnResult == true; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            blnResult = false;
        }
    }
    return blnResult;
}

var BudgetCalculator = new Class({ 

	initialize: function(options) {
		this.budgetValueId = 'budgetValue';
		this.budgetDeviationId = 'budgetDeviation';
		this.budgetMinValueId = 'budgetMinValue';
		this.budgetMaxValueId = 'budgetMaxValue';
		
        window.addEvent('load', function() {
			$(this.budgetValueId).addEvent('keyup', this.updateMinMax.bind(this));
			$(this.budgetDeviationId).addEvent('keyup', this.updateMinMax.bind(this));
			$(this.budgetMinValueId).addEvent('keyup', this.updateBudgetAndDeviation.bind(this));
			$(this.budgetMaxValueId).addEvent('keyup', this.updateBudgetAndDeviation.bind(this));
        }.bind(this)); 
	},
	
	updateMinMax: function(){
		$(this.budgetMinValueId).value = getMinPrice($(this.budgetValueId).value, $(this.budgetDeviationId).value);
		$(this.budgetMaxValueId).value = getMaxPrice($(this.budgetValueId).value, $(this.budgetDeviationId).value);
	},
	
	updateBudgetAndDeviation: function(){
		$(this.budgetValueId).value = getAvgPrice($(this.budgetMinValueId).value, $(this.budgetMaxValueId).value);
		$(this.budgetDeviationId).value = getDeviation($(this.budgetMinValueId).value, $(this.budgetMaxValueId).value);
	}
	
});


var LettingsBudgetCalculator = new Class({ 

	initialize: function(options) {
		this.budgetValueId = 'lettingsBudgetValue';
		this.budgetDeviationId = 'lettingsBudgetDeviation';
		this.budgetMinValueId = 'lettingsBudgetMinValue';
		this.budgetMaxValueId = 'lettingsBudgetMaxValue';
		
		this.budgetMonthValueId = 'lettingsBudgetMonthValue';
		this.budgetMonthMinValueId = 'lettingsBudgetMonthMinValue';
		this.budgetMonthMaxValueId = 'lettingsBudgetMonthMaxValue';
		
        window.addEvent('load', function() {
			$(this.budgetValueId).addEvent('keyup', this.onBudgetUpdated.bind(this));
			$(this.budgetDeviationId).addEvent('keyup', this.onBudgetUpdated.bind(this));
			$(this.budgetMinValueId).addEvent('keyup', this.onMinMaxUpdated.bind(this));
			$(this.budgetMaxValueId).addEvent('keyup', this.onMinMaxUpdated.bind(this));
			$(this.budgetMonthValueId).addEvent('keyup', this.onMonthBudgetUpdated.bind(this));
			$(this.budgetMonthMinValueId).addEvent('keyup', this.onMonthMinMaxUpdated.bind(this));
			$(this.budgetMonthMaxValueId).addEvent('keyup', this.onMonthMinMaxUpdated.bind(this));
        }.bind(this)); 
	},
	
	onBudgetUpdated: function(){
		var budget = $(this.budgetValueId).value;
		var deviation = $(this.budgetDeviationId).value;
		$(this.budgetMinValueId).value = getMinPrice(budget, deviation);
		$(this.budgetMaxValueId).value = getMaxPrice(budget, deviation);
		$(this.budgetMonthValueId).value = getMonthPrice(budget);
		$(this.budgetMonthMinValueId).value = getMonthPrice(getMinPrice(budget, deviation));
		$(this.budgetMonthMaxValueId).value = getMonthPrice(getMaxPrice(budget, deviation));
	},
	
	onMinMaxUpdated: function(){
		var min = $(this.budgetMinValueId).value;
		var max = $(this.budgetMaxValueId).value;
		$(this.budgetValueId).value = getAvgPrice(min, max);
		$(this.budgetDeviationId).value = getDeviation(min, max);
		$(this.budgetMonthValueId).value = getMonthPrice(getAvgPrice(min, max));
		$(this.budgetMonthMinValueId).value = getMonthPrice(min);
		$(this.budgetMonthMaxValueId).value = getMonthPrice(max);
	},
	
	onMonthBudgetUpdated: function(){
		var monthBudget = $(this.budgetMonthValueId).value;
		var deviation = $(this.budgetDeviationId).value;
		$(this.budgetValueId).value  = getWeekPrice(monthBudget);
		$(this.budgetMinValueId).value = getMinPrice(getWeekPrice(monthBudget), deviation);
		$(this.budgetMaxValueId).value = getMaxPrice(getWeekPrice(monthBudget), deviation);
		$(this.budgetMonthMinValueId).value = getMinPrice(monthBudget, deviation);
		$(this.budgetMonthMaxValueId).value = getMaxPrice(monthBudget, deviation);
	},
	
	onMonthMinMaxUpdated: function(){
		var monthMin = $(this.budgetMonthMinValueId).value;
		var monthMax = $(this.budgetMonthMaxValueId).value;
		$(this.budgetValueId).value = getAvgPrice(getWeekPrice(monthMin), getWeekPrice(monthMax));
		$(this.budgetDeviationId).value = getDeviation(monthMin, monthMax);
		$(this.budgetMonthValueId).value = getAvgPrice(monthMin, monthMax);
		$(this.budgetMinValueId).value = getWeekPrice(monthMin);
		$(this.budgetMaxValueId).value = getWeekPrice(monthMax);
	}
	
});

function shortenContactNotes(){
	$$(".shortDescription").each(function(item, index) {
		item.shortDescription= new SLDescription({divId:item.id, moreText:"more...", backText:""});
		item.shortDescription.start();
	});
}
