function numbersonly(myfield, e, dec)
{
var key;
var keychar;
if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);
// control keys
if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
return true;
// numbers
else if ((("0123456789").indexOf(keychar) > -1))
return true;
// decimal point jump
else if (dec && (keychar == "."))
{
return false;
}
else
return false;
}

function showNewsletterMessage(email)
{
	url = '/newsletter/index/addEmail';
	$('p#newsletterId').load(url, {
									'email' : email
	});
	return false;
}

function hideNewsletterForm()
{
	$('#message').css("display","none");
	return false;
}

$(function(){
	
	$('a#securedLoans').click(function(){
		url = '/page/index/getSecuredLoansForm';
		$('div#loadForm').load(url, null, function(){
			$('#commentForm').find('input#firstName').focus();
			$("#stage2").click(
			function()
			{
				$(this).is(':checked') && $('#stage2table').slideDown('slow') || $('#stage2table').slideUp('slow');
				if($(this).attr("checked")) {	
					try {
					
						document.getElementById('ext1').setAttribute("class", "required");
						document.getElementById('ext2').setAttribute("class", "required");
						document.getElementById('ext3').setAttribute("class", "required");
						document.getElementById('ext4').setAttribute("class", "required");
						document.getElementById('ext5').setAttribute("class", "required");
						document.getElementById('ext6').setAttribute("class", "required");
						document.getElementById('ext7').setAttribute("class", "required");
						document.getElementById('ext8').setAttribute("class", "required");
						document.getElementById('ext9').setAttribute("class", "required");
						document.getElementById('ext10').setAttribute("class", "required");
						document.getElementById('ext11').setAttribute("class", "required");
						document.getElementById('ext12').setAttribute("class", "required");
					
					 } catch(e) {
				 		return true;
				 	}
	        		$('#stage2table').slideDown('slow')
     			} else {
					try {
						$('#stage2table').slideUp('slow');
						document.getElementById('ext1').setAttribute("class", "empty");
						document.getElementById('ext2').setAttribute("class", "empty");
						document.getElementById('ext3').setAttribute("class", "empty");
						document.getElementById('ext4').setAttribute("class", "empty");
						document.getElementById('ext5').setAttribute("class", "empty");
						document.getElementById('ext6').setAttribute("class", "empty");
						document.getElementById('ext7').setAttribute("class", "empty");
						document.getElementById('ext8').setAttribute("class", "empty");
						document.getElementById('ext9').setAttribute("class", "empty");
						document.getElementById('ext10').setAttribute("class", "empty");
						document.getElementById('ext11').setAttribute("class", "empty");
						document.getElementById('ext12').setAttribute("class", "empty");
			 		} catch(e) {
			 			return true;
			 		}
     			}
			});
			
			$("#commentForm").validate();
		});
		
		return false;
	});
		
	
	$('a#mortgage').click(function(){
		url = '/page/index/getMortgageForm';
		$('div#loadForm').load(url, null, function(){
			$('#commentForm').find('input#firstName').focus();
			$("#propertyValue").attr("value",$('input#currentProperty').attr("value"));
			$("#stage2").click(
			function()
			{
				$(this).is(':checked') && $('#stage2table').slideDown('slow') || $('#stage2table').slideUp('slow');
				if($(this).attr("checked")) {	
					try {
					
						document.getElementById('ext1').setAttribute("class", "required");
						document.getElementById('ext2').setAttribute("class", "required");
						document.getElementById('ext3').setAttribute("class", "required");
						document.getElementById('ext4').setAttribute("class", "required");
						document.getElementById('ext5').setAttribute("class", "required");
						document.getElementById('ext6').setAttribute("class", "required");
						document.getElementById('ext7').setAttribute("class", "required");
						document.getElementById('ext8').setAttribute("class", "required");
						document.getElementById('ext9').setAttribute("class", "required");
						document.getElementById('ext10').setAttribute("class", "required");
						document.getElementById('ext11').setAttribute("class", "required");
						document.getElementById('ext12').setAttribute("class", "required");
					
					 } catch(e) {
				 		return true;
				 	}
	        		$('#stage2table').slideDown('slow')
     			} else {
					try {
						$('#stage2table').slideUp('slow');
						document.getElementById('ext1').setAttribute("class", "empty");
						document.getElementById('ext2').setAttribute("class", "empty");
						document.getElementById('ext3').setAttribute("class", "empty");
						document.getElementById('ext4').setAttribute("class", "empty");
						document.getElementById('ext5').setAttribute("class", "empty");
						document.getElementById('ext6').setAttribute("class", "empty");
						document.getElementById('ext7').setAttribute("class", "empty");
						document.getElementById('ext8').setAttribute("class", "empty");
						document.getElementById('ext9').setAttribute("class", "empty");
						document.getElementById('ext10').setAttribute("class", "empty");
						document.getElementById('ext11').setAttribute("class", "empty");
						document.getElementById('ext12').setAttribute("class", "empty");
			 		} catch(e) {
			 			return true;
			 		}
     			}
			});

			$("#commentForm").validate();
			
		});
		
		return false;
	});
	
	$('#calculator').find('input').focus(function(){
		if(this.value == "0.00"){
			this.value="";
		} 
	});
	$('#calculator').find('input').blur(function(){
		if(this.value == "" || this.value == "." || this.value == "0.0"){
			this.value= "0.00";
		}else {
			var value = this.value;
			value = Math.round(value*Math.pow(10,2))/Math.pow(10,2);
			if(this.value.match("(\\.)+") == null) {
				this.value = value + ".00";
			}
			if(this.value.match("(\\.)+$")) {
				this.value = value + ".00";
			}else if(this.value.match("[0-9]\.[0-9]$")) {
				this.value = value + "0";
			}else if(this.value.match("^(\\.)+")) {
				this.value = value;
			}
		}
		
	});
	
	$('input#currentProperty').bind('focus',function(){
		if(this.value == "0.00"){
			this.value="";
		}
	}).bind('blur',function(){
		if(this.value == "" || this.value == "." || this.value == "0.0"){
			this.value= "0.00";
		}else {
			var value = this.value;
			value = Math.round(value*Math.pow(10,2))/Math.pow(10,2);
			if(this.value.match("(\\.)+") == null) {
				this.value = value + ".00";
			}
			if(this.value.match("(\\.)+$")) {
				this.value = value + ".00";
			}else if(this.value.match("[0-9]\.[0-9]$")) {
				this.value = value + "0";
			}else if(this.value.match("^(\\.)+")) {
				this.value = value;
			}
		}
	}).bind('keypress',function(sign){
			if(this.value.match("(\\.)+") && sign.which == 46) {
				return false;
			}
			if(sign.which == 8 && this.value == "") {
				return false;
			}
			if(sign.which < 48 || sign.which > 57) {
				if( sign.which == 0 || sign.which == 46 || sign.which == 37 || sign.which == 38 || sign.which == 39 || sign.which == 40 || sign.which == 8 ||sign.which == 9) {
					
				}else{
					return false;
				}
			}
	});
	
	
	
	$('input.monthly').bind('keypress',function(sign){
		

		
			if(this.value.match("(\\.)+") && sign.which == 46) {
				return false;
			}
			if(sign.which == 8 && this.value == "") {
				return false;
			}
			if(sign.which < 48 || sign.which > 57) {
				if( sign.which == 0 || sign.which == 46 || sign.which == 37 || sign.which == 38 || sign.which == 39 || sign.which == 40 || sign.which == 8 ||sign.which == 9) {
					
				}else{
					return false;
				}
			}
	
			}).bind('keyup',function(sign){
			
			var value = 0.00;
			
			$('input.monthly').each(function(){
			
			if(this.value != "" && (this.value != ".") && (this.value != "0.00")) {
				value = value + parseFloat(this.value);
			}
			
			
			value = Math.round(value*Math.pow(10,2))/Math.pow(10,2);
			$('td#totalMonthly').html('₤' + value);
			
			var mount = value - $('#mortgageValue').attr('value');
		
			
			mount = Math.round(mount*Math.pow(10,2))/Math.pow(10,2);
			
			var total = $('td#totalMonthly').html();
			
			
			if(total.match("(\\.)+") == null) {
				$('td#totalMonthly').html('₤' + value + ".00");
			}else if(total.match("[0-9]\.[0-9]$")) {
				$('td#totalMonthly').html('₤' + value + "0");
			}
			
			if(total.match("(\\.)+") == null) {
				$('td#greenRemartgage').html('₤' + value + ".00");
			}else if(total.match("[0-9]\.[0-9]$")) {
				$('td#greenRemartgage').html('₤' + value + "0");
			}		
			
			if(total.match("(\\.)+") == null) {
				$('td#greenLoan').html('₤' + mount + ".00");
			}else if(total.match("[0-9]\.[0-9]$")) {
				$('td#greenLoan').html('₤' + mount + "0");
			}		
			
			
			var remortgagePayment = $('td#remortagePayment').html().substring(1);
			var loanPayment = $('td#loanPayment').html().substring(1);
			
			var remortgageSaving = value - remortgagePayment;
			remortgageSaving = Math.round(remortgageSaving*Math.pow(10,2))/Math.pow(10,2);
			
			var loanSaving = value - loanPayment;
			loanSaving = Math.round(loanSaving*Math.pow(10,2))/Math.pow(10,2);
			
			remortgageSaving = remortgageSaving == 0 ? '0.00' : remortgageSaving;
			
			if(remortgageSaving.toString().match("(\\.)+") == null) {
				remortgageSaving = remortgageSaving + ".00";
			}
			if(remortgageSaving.toString().match("(\\.)+$")) {
				remortgageSaving = remortgageSaving + ".00";
			}else if(remortgageSaving.toString().match("[0-9]\.[0-9]$")) {
				remortgageSaving = remortgageSaving + "0";
			}
			
			$('td#remortageSaving').html('₤' + remortgageSaving);
			
			loanSaving = loanSaving == 0 ? '0.00' : loanSaving;
			if(loanSaving.toString().match("(\\.)+") == null) {
				loanSaving = loanSaving + ".00";
			}
			if(loanSaving.toString().match("(\\.)+$")) {
				loanSaving = loanSaving + ".00";
			}else if(loanSaving.toString().match("[0-9]\.[0-9]$")) {
				loanSaving = loanSaving + "0";
			}
			
			$('td#loanSaving').html("₤" + loanSaving);
			
			})
			
	});
	
	
		$('input.balance').bind('keypress',function(sign){
		
		
			if(this.value.match("(\\.)+") && sign.which == 46) {
				return false;
			}
			if(sign.which == 8 && this.value == "") {
				return false;
			}
			if(sign.which < 48 || sign.which > 57) {
				if( sign.which == 46 || sign.which == 37 || sign.which == 38 || sign.which == 39 || sign.which == 40 || sign.which == 8 ||sign.which == 9) {
					
				}else{
					return false;
				}
				/*
				if(sign.which >=96 && sign.which <= 105) {
					return true;
				}*/
			}
			
		}).bind('keyup',function(sign){
			var value = 0.00;
			
			$('input.balance').each(function(){
				
			if(this.value != "" && (this.value != ".") && (this.value != "0.00")) {
				value = value + parseFloat(this.value);
			}
			
			var length = $('#remortageRate').html().length
			
			var remortageRate = $('#remortageRate').html().substring(0,length-1);
			remortageRate = remortageRate.match("[a-zA-Z]") ? 0 : remortageRate/100;
			
			var length = $('#loanRate').html().length
			
			var loanRate = $('#loanRate').html().substring(0, length-1);
			loanRate = loanRate.match("[a-zA-Z]") ? 0 : loanRate/100;

			var remortagePayment = value*(remortageRate/12);
			remortagePayment = Math.round(remortagePayment*Math.pow(10,2))/Math.pow(10,2);
			
			var loanPayment = value*(loanRate/12);
			loanPayment = Math.round(loanPayment*Math.pow(10,2))/Math.pow(10,2);
			
			remortagePayment = remortagePayment==0 ? '0.00' : remortagePayment;
			$('td#remortagePayment').html('₤' + remortagePayment);
			
			loanPayment = loanPayment==0 ? '0.00' : loanPayment;
			$('td#loanPayment').html('₤' + loanPayment);
			
			var remortageSaving = $('td#totalMonthly').html().substring(1) - remortagePayment;
			remoratgeSaving = Math.round(remortageSaving*Math.pow(10,2))/Math.pow(10,2);
			
			var loanSaving = $('td#totalMonthly').html().substring(1) - loanPayment;
			loanSaving = Math.round(loanSaving*Math.pow(10,2))/Math.pow(10,2);
			
			remoratgeSaving = remoratgeSaving == 0 ? '0.00' : remoratgeSaving;
			
			if(remoratgeSaving.toString().match("(\\.)+") == null) {
				remoratgeSaving = remoratgeSaving + ".00";
			}
			if(remoratgeSaving.toString().match("(\\.)+$")) {
				remoratgeSavin = remoratgeSavin + ".00";
			}else if(remoratgeSaving.toString().match("[0-9]\.[0-9]$")) {
				sremoratgeSavin = remoratgeSavin + "0";
			}
			$('td#remortageSaving').html('₤' + remoratgeSaving);
			
			loanSaving = loanSaving == 0 ? '0.00' : loanSaving;
			if(loanSaving.toString().match("(\\.)+") == null) {
				loanSaving = loanSaving + ".00";
			}
			if(loanSaving.toString().match("(\\.)+$")) {
				loanSaving = loanSaving + ".00";
			}else if(loanSaving.toString().match("[0-9]\.[0-9]$")) {
				loanSaving = loanSaving + "0";
			}
			$('td#loanSaving').html('₤' + loanSaving);
			
			})
		});
	
});

function reloadPage(url) {
	$('#content').load(url);
	
	return false;	
}

$(function() {
	
	$('input#stage2').click(function() {
     if($(this).attr("checked")) {
     			try {
				
				document.getElementById('ext1').setAttribute("class", "required");
				document.getElementById('ext2').setAttribute("class", "required");
				document.getElementById('ext3').setAttribute("class", "required");
				document.getElementById('ext4').setAttribute("class", "required");
				document.getElementById('ext5').setAttribute("class", "required");
				document.getElementById('ext6').setAttribute("class", "required");
				document.getElementById('ext7').setAttribute("class", "required");
				document.getElementById('ext8').setAttribute("class", "required");
				document.getElementById('ext9').setAttribute("class", "required");
				document.getElementById('ext10').setAttribute("class", "required");
				document.getElementById('ext11').setAttribute("class", "required");
				document.getElementById('ext12').setAttribute("class", "required");
				
			 } catch(e) {
			 	return true;
			 }
        $('#stage2table').slideDown('slow')
     } else {
				try {
					$('#stage2table').slideUp('slow');
					document.getElementById('ext1').setAttribute("class", "empty");
					document.getElementById('ext2').setAttribute("class", "empty");
					document.getElementById('ext3').setAttribute("class", "empty");
					document.getElementById('ext4').setAttribute("class", "empty");
					document.getElementById('ext5').setAttribute("class", "empty");
					document.getElementById('ext6').setAttribute("class", "empty");
					document.getElementById('ext7').setAttribute("class", "empty");
					document.getElementById('ext8').setAttribute("class", "empty");
					document.getElementById('ext9').setAttribute("class", "empty");
					document.getElementById('ext10').setAttribute("class", "empty");
					document.getElementById('ext11').setAttribute("class", "empty");
					document.getElementById('ext12').setAttribute("class", "empty");
			 } catch(e) {
			 	return true;
			 }
       $('#stage2table').slideUp('slow')
     }  

	})});

$(function() {$('#numbersonly').bind('keypress',function(sign){
			alert(sign.code);
			if(this.value.match("(\\.)+") && sign.keyCode == 190) {
				return false;
			}
			if(this.value.match("([a-zA-Z])+")) {
				return false;
			}
			
			if(sign.keyCode < 48 || sign.keyCode > 57) {
				if( sign.keyCode == 190 || sign.keyCode == 37 || sign.keyCode == 38 || sign.keyCode == 39 || sign.keyCode == 40 || sign.keyCode == 8 ||sign.keyCode == 9) {
					return true;
				}
				if(sign.keyCode >=96 && sign.keyCode <= 105) {
					return true;
				}
				return false;
			}else {
				return true;
			}
		
	});
});


function loadPersonalForm(formname) {
	url = '/page/index/' + formname;
	$('#loadPersonalFormDiv').load(url, null, function() {
		$("#stage2").click(
			function()
			{
				$(this).is(':checked') && $('#stage2table').slideDown('slow') || $('#stage2table').slideUp('slow');
				if($(this).attr("checked")) {	
					try {
					
						document.getElementById('ext1').setAttribute("class", "required");
						document.getElementById('ext2').setAttribute("class", "required");
						document.getElementById('ext3').setAttribute("class", "required");
						document.getElementById('ext4').setAttribute("class", "required");
						document.getElementById('ext5').setAttribute("class", "required");
						document.getElementById('ext6').setAttribute("class", "required");
						document.getElementById('ext7').setAttribute("class", "required");
						document.getElementById('ext8').setAttribute("class", "required");
						document.getElementById('ext9').setAttribute("class", "required");
						document.getElementById('ext10').setAttribute("class", "required");
						document.getElementById('ext11').setAttribute("class", "required");
						document.getElementById('ext12').setAttribute("class", "required");
					
					 } catch(e) {
				 		return true;
				 	}
	        		$('#stage2table').slideDown('slow')
     			} else {
					try {
						$('#stage2table').slideUp('slow');
						document.getElementById('ext1').setAttribute("class", "empty");
						document.getElementById('ext2').setAttribute("class", "empty");
						document.getElementById('ext3').setAttribute("class", "empty");
						document.getElementById('ext4').setAttribute("class", "empty");
						document.getElementById('ext5').setAttribute("class", "empty");
						document.getElementById('ext6').setAttribute("class", "empty");
						document.getElementById('ext7').setAttribute("class", "empty");
						document.getElementById('ext8').setAttribute("class", "empty");
						document.getElementById('ext9').setAttribute("class", "empty");
						document.getElementById('ext10').setAttribute("class", "empty");
						document.getElementById('ext11').setAttribute("class", "empty");
						document.getElementById('ext12').setAttribute("class", "empty");
			 		} catch(e) {
			 			return true;
			 		}
     			}
			});

			
			$("#commentForm").validate();
			
		});	
	return false;
};

