/* Copyright Grovelawn 2005-2007 */
var open_ = false, mode_ = false, mcalc = false;
function menu ( i )
{
	var m = document.getElementById( 'menu-' + i );
	if( m )
	{
		m.parentNode.menu_ = m;
		m.parentNode.onclick = function()
		{
			if( open_ && open_ != m ) open_.className = 'hide';
			m.className = ( 'show' == m.className ? 'hide' : 'show' );
			open_ = m;
			return true;
		};
		m.parentNode.onmousedown = function()
		{
			return mode_ = false;
		};
		document.onmousedown = function()
		{
			if( open_ && mode_ ) open_.className = 'hide';
			return mode_ = true;
		};
	}
}
function scrollText_f( id, td, ht )
{
	var i = document.getElementById( id ), m = parseInt( i.style.marginTop ) - 1
	if( m > -ht ) { i.style.marginTop = ( m - 1 ) + 'px'; } else { i.style.marginTop = '130px'; }
	setTimeout( 'scrollText_f("' + id + '",' + td + ',' + ht + ')', td );
}
function scrollText( id, td, ht )
{
	var i = document.getElementById( id ), s = ( '' + i.innerHTML ).replace( '<ul>', '' ).replace( '</ul>', '' ).replace( '</li>', '' ).split( '<li>' );
	if( 2 > s.length ) { s = ( '' + i.innerHTML ).replace( '<UL>', '' ).replace( '</UL>', '' ).replace( '</LI>', '' ).split( '<LI>' ); }
	i.innerHTML = '<table border=0 cellpadding=0 cellspacing=0><tr><td>' + s.join( '</td></tr><tr><td>' ) + '</td></tr></table>';
	i.style.marginTop = '30px';
	setTimeout( 'scrollText_f("' + id + '",' + td + ',' + ( ht * s.length ) + ')', td );
}
function checkNumber( t )
{
	return true;
}
var Calculators=function( frm )
{
	/* Defaults */
	var	stamp_duty_normal		= [ 125000, 0.01, 250000, 0.03, 500000, 0.04 ],
		stamp_duty_disadvantaged	= [ 150000, 0.01, 250000, 0.03, 500000, 0.04 ],
		borrow_single_min = 350,	borrow_joint_min = 275,		borrow_ltv = 90,
		borrow_single_max = 500,	borrow_joint_max = 450,		borrow_bonus_ratio = 100,
		buy_to_let_pay_rate_pc = 100,	buy_to_let_interest_rate_pc = 5.35,
		buy_to_let_maximum_ltv = 75,
		early_repayment_charge = 1750;

	/* Helpers */
	function getInt(n)	{ var x = frm[n].value; return Math.round( x.replace( /[^0-9.]/g, '' )-0 ); }
	function getNum(n)	{ var x = frm[n].value; return x.replace( /[^0-9.]/g, '' )-0.0; }
	function getTxt(n)	{ return ( frm[n].selectedIndex ? frm[n].options[ frm[n].selectedIndex ].value : frm[n].value ); }
	function setInt(n,i)	{ if( frm && frm[n] ) { frm[n].value = Math.round(i-0); } }
	function setMny(n,i)	{ if( frm && frm[n] ) { frm[n].value = Math.round((i-0)*100)/100; } }
	function setNum(n,i)	{ if( frm && frm[n] ) { frm[n].value = i-0.0; } }
	function setTxt(n,t)	{ if( frm && frm[n] ) { frm[n].value = t; } }
	function getChk(n)	{ if( frm && frm[n] ) { return frm[n].checked ? true : false; } return false; }
	function getRepay( price, rate, term )
	{
		var n, r=rate/12.0, t=term*12, tp=1;
		for( n=0; n<t; ++n ) tp *= ( 1+(r/100) );
		return ( price*r/100 ) / ( 1-(1/tp) );
	}
	function getInterest( price, rate, term )
	{
		var r=rate/12.0, t=term*12;
		return ( price*r/100 );
	}

	/* Calculators */
	this.stampDuty = function ()
	{
		var n, r=0.0, v=getInt( 'price' ), a=( frm.disadv[0].checked ? stamp_duty_disadvantaged : stamp_duty_normal );
		for( n=0; n<a.length; n+=2 ) if( a[n]<v ) r=a[n+1]; else break;
		setInt( 'total', v*r );
		setTxt( 'tell', 'Stamp Duty on a £' + v + ' property would be approx £' + Math.round(v*r) );
	};
	this.mortgageRepayment = function ()
	{
		var n, p=getInt( 'price' ), r=getNum( 'rate' ), t=getInt( 'term' );
		var p1=getRepay( p,r,t ), i1=getInterest( p,r,t ), p2=getRepay( p,r+1,t ), i2=getInterest( p,r+1,t );
		setMny( 'month', p1 );		setMny( 'monthp1', p2 );
		setMny( 'intrs', i1 );		setMny( 'intrsp1', i2 );
		setMny( 'total', t*p1*12 );	setMny( 'totalp1', t*p2*12 );
		setTxt( 'tell', 'Mortgage repayment on a £' + p + ' loan over ' + t + ' years at ' + r + '% would be £' + p1 + ' pcm (£' + t*p1*12 + ' total) or £' + i1 + ' pcm (£' + t*i1*12 + ' total)' );
	};
	this.remortgageRepayment = function ()
	{
		var n, p=getInt( 'price' ), r=getNum( 'rate' ), t=getInt( 'term' )*12;
		var np=getNum( 'nprice' ), nr=getNum( 'nrate' ), nt=getInt( 'nterm' )*12;
		var p1=getRepay( p,r,t/12 ), p2=getRepay( p,r+1,t/12 ), p1n=getRepay( np,nr,nt/12 ), p2n=getRepay( np,nr+1,nt/12 );
		var s1=t*p1-nt*p1n, s2=t*p2-nt*p2n, s1y=(t<24?t:24)*p1-(nt<24?nt:24)*p1n, s2y=(t<24?t:24)*p2-(nt<24?nt:24)*p2n;
		setMny( 'month', p1 );		setMny( 'monthp1', p2 );
		setMny( 'nmonth', p1n );		setMny( 'nmonthp1', p2n );
		setTxt( 'saves', ( 0<=s1 ? Math.round(s1) + ' saving' : (0-Math.round(s1)) + ' more' ) );
		setTxt( 'save2', ( 0<=s1y ? Math.round(s1y) + ' saving' : (0-Math.round(s1y)) + ' more' ) );
		setTxt( 'savesp1', ( 0<=s2 ? Math.round(s1) + ' saving' : (0-Math.round(s2)) + ' more' ) );
		setTxt( 'save2p1', ( 0<=s2y ? Math.round(s1y) + ' saving' : (0-Math.round(s2y)) + ' more' ) );
		setTxt( 'tell', 'By remortgaging there would be ' + ( 0<=s1 ? ' a £' + Math.round(s1) + ' saving' : ' £' + (0-Math.round(s1)) + ' more' ) + ' when changing the term to ' + t + ' years at ' + r + '%' );
	};
	this.howMuchBorrow = function ()
	{
		var m=getInt( 'maxltv' ), p=getInt( 'property' );
		var i1=getInt( 'incomem' ), i2=getInt( 'incomep' ), b1=getInt( 'bonusm' ), b2=getInt( 'bonusp' );
		var xx=getNum( 'mulbonus' ), xj=getNum( 'muljoint' ), xb=getNum( 'mulbest' );
		var d, a=xj*( i1+i2+xx*(b1+b2) ), am=xb*( i1+xx*b1 ), ap=xb*( i2+xx*b2 );
		d = ( ( p-( a = Math.max( a, Math.max( am, ap ) ) ) )/100 + 1 ) * 100;
		setInt( 'deposit', d>0 ? d:'none (' + d + ')' );
		setInt( 'borrow', Math.min( a, p ) );
		setTxt( 'tell', 'A mortgage of upto £' + Math.min( a, p ) + ' with a typical deposit of around £' +
			(d>0?d:0) + ' should be availiable for your wages (subject to terms and conditions)' );
	};
	this.howMuchBorrow2 = function ()
	{
		var i1=getInt('income1')+getInt('bonus1')*borrow_bonus_ratio/100, i2=getInt('income2')+getInt('bonus2')*borrow_bonus_ratio/100;
		setInt( 'borrow', Math.floor( ( frm.joint.checked ? (i1+i2)*borrow_joint_min : i1*borrow_single_min )*borrow_ltv/10000 ) );
		setInt( 'borrowmax', Math.floor( ( frm.joint.checked ? (i1+i2)*borrow_joint_max : i1*borrow_single_max )*borrow_ltv/10000 ) );
	};
	this.howMuchBorrowBTL = function ()
	{
		var p=getInt( 'property' ), r=getNum( 'rent' );
		var ml=p*buy_to_let_maximum_ltv/100, mx=(r*12/(buy_to_let_pay_rate_pc/100.0))/(buy_to_let_interest_rate_pc/100.0);
		setInt( 'maxloan', Math.floor( mx<ml?mx:ml ) );
	};
	this.requiredRentalBTL = function ()
	{
		var b=getInt( 'property' )-getInt( 'deposit' );
		setInt( 'rent', Math.ceil( ((b*(buy_to_let_interest_rate_pc/100.0))/12.0) * (buy_to_let_pay_rate_pc/100.0) ) );
	};
	this.requiredRate = function ()
	{
		var l=getInt( 'loan' ), r=getNum( 'rate' ), t=getInt( 'term' ), p=getNum( 'penalty' );
		if( 'p'==getTxt( 'type' ) ) { p=l*(p/100); }
		setNum( 'nofee', getChk( 'addv' ) ? ((l*r/100*(t/12))-p) / (((t/12)/100)*(l+p)) : r-((p/l)*1200)/t );
		setNum( 'payfee', getChk( 'addv' ) ? ((l*r/100*(t/12))-p-early_repayment_charge) / (((t/12)/100)*(l+p)) : r-((((early_repayment_charge*100)/l)+(p/l)*100)*12)/t );
	};
};
function form_change() {
	var f = this.form; var n;
	if( f.cqt && f.xst && f.stage_1 && f.stage_2 ) {	var s;
		var c1 = f.stage_1.zz.split(',');		var s1 = f.stage_1[f.cqt.value].split(',');
		var c2 = f.stage_2.zz.split(',');		var s2 = f.stage_2['c'+f.ccn.value].split(',');
//		if( 2!=( s=f.xst.value-0 ) ) { f.xst.value = s = 1; f.cqb.value = 'Continue'; } else { f.cqb.value = 'Get Quote'; }
		for( n=0; n<c1.length; ++n ) {
			f['c'+c1[n]].disabled = ( '--'==s1[n] ? true : false );
			if( f['c'+c1[n]].style ) f['c'+c1[n]].style.backgroundColor = '#fff';
			if( document.getElementById && document.getElementById('h'+c1[n]) ) document.getElementById('h'+c1[n]).className = ( '--'==s1[n] ? 'hide' : 'show' );
		}
		for( n=0; n<c2.length; ++n ) {
			f['c'+c2[n]].disabled = ( '--'==s2[n] ? true : false );
			if( f['c'+c2[n]].style ) f['c'+c2[n]].style.backgroundColor = '#fff';
			if( document.getElementById && document.getElementById('h'+c2[n]) ) document.getElementById('h'+c2[n]).className = ( '--'==s2[n] ? 'hide' : 'show' );
		}
		if( !f.cit.checked ) {
			f.cje.disabled = f.cjm.disabled = true;
			if( document.getElementById )		document.getElementById('hje').className = document.getElementById('hjm').className = 'hide';
		} else if( s!=1 && document.getElementById )	document.getElementById('hje').className = document.getElementById('hjm').className = 'hide';
	}
	return true;
}
function form_check() {
	var f = this.form; var e=''; var l; var n;
	for( n=0, l=f.stage_1[f.cqt.value].split(','); n<l.length; ++n ) {
		if( '--'!=l[n] && ''==f['c'+l[n]].value && ( f.cje.disabled || 'je'==l[n] || 'jm'==l[n] ) && 'nl'!=l[n] ) {
			if( f['c'+l[n]].style ) f['c'+l[n]].style.backgroundColor = '#fbb';
			e = 'Sorry, the information you\'ve entered appears incomplete.\nPlease fill in the highlighted fields.\n';
		} else	if( '--'!=l[n] && f['c'+l[n]].style ) f['c'+l[n]].style.backgroundColor = '#fff';
	}
	if( ''!=e ) { f.xst.value=1; alert( e ); return false; } else {
//		if( 1==f.xst.value ) { f.xst.value=2; f.cqt.onchange(); return false; }
		for( n=0, l=f.stage_2['c'+f.ccn.value].split(','); n<l.length; ++n ) {
			if( '--'!=l[n] && ''==f['c'+l[n]].value && 't1'!=l[n] && 't2'!=l[n] && 't3'!=l[n] && 't4'!=l[n] ) {
				if( f['c'+l[n]].style ) f['c'+l[n]].style.backgroundColor = '#fbb';
				e = 'Sorry, the information you\'ve entered appears incomplete.\nPlease fill in the highlighted fields.\n';
			} else	if( '--'!=l[n] && f['c'+l[n]].style ) f['c'+l[n]].style.backgroundColor = '#fff';
		}
		if( 2 != f.cpe.value.split('@').length ) { e += 'Please enter a valid email address.'; f.cpe.style.backgroundColor = '#fbb'; }
		if( ''!=e ) { f.xst.value=2; alert( e ); return false; } else f.xst.value=3;
	}
	return true;
}
function form_info(e) {
	if( !e ) var e = window.event;
	var b = document.getElementById('box-info');
	if( 'mouseover'==e.type && this.form ) {
		var p; var i = this.form.info_data[this.id?this.id:this.getAttribute('for')];
		if( i ) {
			document.getElementById('box-info-data').innerHTML = '<h3>' + i[0] + '</h3>' + i[1] + '<br>';
			var f = document.getElementById('form-conveyancing');
			var ob = 0; for( p=this; p.offsetParent; p=p.offsetParent ) ob+=p.offsetTop;
			var of = 0; for( p=f; p.offsetParent; p=p.offsetParent ) of+=p.offsetTop;
			var ol = 0; for( p=f; p.offsetParent; p=p.offsetParent ) ol+=p.offsetLeft;
			b.style.top = (ob-of+150)+'px';
			b.style.left = (ol-100) + 'px';
			b.style.display = 'block';
		}
	} else if( 'mouseout'==e.type ) { b.style.display = 'none'; }
}
window.onload = function ()
{
	var i, n, e, l;
	if( !document.getElementById ) { document.getElementById = function(t) { return document.all[t]; }; }
	menu( 'mortgages' );
	menu( 'insurance' );
	menu( 'conveyancing' );
	menu( 'calculators' );
	menu( 'other' );
	document.getElementById( 'head' ).onclick = function() { window.location.href = '/'; };
	for( n=0; n<document.forms.length; ++n ) { mcalc = new Calculators( document.forms[n] ); }
	if( !!( i = document.getElementById( 'form-conveyancing' ) ) )
	{
		var f = i;
		if( !f || !f.cqb || !f.cqt || !f.ccn || !f.cit ) return false;
		f.cqb.onclick = form_check;
		f.cqt.onchange = f.ccn.onchange = f.cit.onchange = form_change;
		f.stage_1 = {	po:'qt,--,--,--,--,--,--,--,--,--,--,--,nv,nl,nt,na,np,nc,--,ir,is,in,iu,ib',
				so:'qt,sc,st,--,sq,--,sa,sp,js,ju,--,--,--,--,--,--,--,--,--,--,--,--,--,--',
				sp:'qt,sc,st,--,sq,--,sa,sp,js,ju,--,--,nv,nl,nt,na,np,nc,--,ir,is,in,iu,ib',
				rm:'qt,sc,st,sv,--,sm,sa,sp,--,--,je,jm,--,nl,--,--,--,--,it,--,is,--,iu,--',
				zz:'qt,sc,st,sv,sq,sm,sa,sp,js,ju,je,jm,nv,nl,nt,na,np,nc,it,ir,is,in,iu,ib' };
		f.stage_2 = {
				c1:'pt,pe,cn,c1,--,--,--,t1,--,--,--',
				c2:'pt,pe,cn,c1,c2,--,--,t1,t2,--,--',
				c3:'pt,pe,cn,c1,c2,c3,--,t1,t2,t3,--',
				c4:'pt,pe,cn,c1,c2,c3,c4,t1,t2,t3,t4',
				zz:'pt,pe,cn,c1,c2,c3,c4,t1,t2,t3,t4' };
		f.info_data = { cqt:['Quote Type',		'<p>Select the type of conveyancing you wish to get a quote for form the options in the dropdown list.</p><ol><li>1. Sale only.</li><li>2. Purchase only.</li><li>3. Sale &amp; Purchase - when moving address and you want the solicitor to deal with both the sale and purchase.</li><li>4. Remortgage.</li></ol>'],
				csc:['Country',			'<p>Select the country from the 3 options on the dropdown list...</p><ol><li>1. England</li><li>2. Northern Ireland</li><li>3. Scotland</li><li>4. Wales</li></ol><p>The conveyancing process is slightly different for each country so various different fees will be applied to your quotes depending on which country is selected.</p>'],
				cst:['Tenure',			'<p>Select the Tenure of this property from the following 3 options on the dropdown list...</p><ol><li>1. Freehold - Absolute ownership of the land</li><li>2. Leasehold - Ownership of the land (usually for a fixed period) that is subject to an annual payment of a ground rent to the landlord/owner of the freehold. This usually applies to a flat or apartment</li><li>3. Commonhold - Commonhold is a system of property ownership in England and Wales. It was introduced in 2004 as an alternative to freehold and leasehold. Common parts of the property such as stairs and corridors are held and managed by a Commonhold Association which is a private limited company of which the property owners are members.</li></ol>'],
				csa:['Address',			'<p>Enter the first line of your address and your postcode.</p>'],
				cnv:['Agreed Price',		'<p>Enter the exact amount that you are purchasing the property for.</p>'],
				cnl:['Mortgage Lender',		'<p>Select the Lender for this mortgage/loan (this is optional, however the solicitor may be able to begin your conveyanying work faster if they have this information straight away).</p>'],
				cis:['Shared Ownership',	'<p>This is a scheme operated by a Housing Association where the borrower owns part of a property, and pays the mortgage on this, while a Housing Association owns the rest of the property, and the borrower pays rent on this.</p>'],
				cir:['Right To Buy',		'<p>This is when a tenant living in a council-owned property purchases it at a discount, the size of which depends on the length of their tenancy.</p>'],
				cin:['New Build',		'<p>Where a property is being purchased for the first time from the Builder or Developer.</p>'],
				ciu:['Unregistered Land',	'<p>Land which has not yet been registered at the Land Registry.</p>'],
				cib:['Buy To Let',		'<p>This is where a buyer buys a property with the intention of letting it out on a commercial basis.</p>'],
				cit:['Transfer of Equity',	'<p>Tick this box if there is a transfer of equity involved.</p>'],
				cje:['Equity Payment',		'<p>Enter the Amount being paid for the Equity when another partner in the mortgage is being brought out.</p>'],
				cjm:['Existing Mortgage',	'<p>Enter the outstanding amount of your current mortgage.</p>'],
				csq:['Sale Price',		'<p>Enter the exact amount that you are selling the property for.</p>'],
				csv:['Property Value',		'<p>Enter the value of the property you are remortgaging.</p>'],
				csm:['New Amount',		'<p>Enter the amount of the new mortgage.</p>'],
				ccn:['People Applying',		'<p>The number of clients who own or will own the property in question. This is usually either 1 or 2 (for a couple) but can sometimes be more than 2. (For more than 4 people, call us on 0845 458 2633.)</p>'],
				cc1:['1st Applicant',		'<p>Select the title and then enter the <strong>first and last name</strong> of the primary applicant (typically yourself).</p>'],
				cc2:['Applicant',		'<p>Select the title and then enter the <strong>first and last name</strong> of the applicant.</p>']
		};
		f.info_data.cnc = f.info_data.csc;	f.info_data.cnt = f.info_data.cst;	f.info_data.cna = f.info_data.csa;
		f.info_data.cjs = f.info_data.cis;	f.info_data.cju = f.info_data.ciu;
		f.info_data.cc3 = f.info_data.cc4 = f.info_data.cc5 = f.info_data.cc6 = f.info_data.cc2;
		if( document.getElementsByTagName ) { var n, l;
			for( n=0, l=document.getElementsByTagName('LABEL'); n<l.length; ++n )	{ l[n].onmouseover = l[n].onmouseout = form_info; if( !l[n].form ) l[n].form = f; }
			for( n=0, l=document.getElementsByTagName('INPUT'); n<l.length; ++n )	{ l[n].onmouseover = l[n].onmouseout = form_info; }
			for( n=0, l=document.getElementsByTagName('SELECT'); n<l.length; ++n )	{ l[n].onmouseover = l[n].onmouseout = form_info; }
		}
		f.cqt.onchange();
	}
	l = document.getElementById( 'bestresidential' );	if( l ) l.onclick = function() { window.location.href = '/mortgages/best-buys.html'; };
	l = document.getElementById( 'bestbuytolet' );		if( l ) l.onclick = function() { window.location.href = '/mortgages/best-buys.html'; };
	for( n=0, e = document.getElementsByTagName( 'A' ); n<e.length; ++n )
		if( -1 != e[n].href.toLowerCase().indexOf( 'http://' ) && -1 == e[n].href.toLowerCase().indexOf( 'http://www.grovelawnfinancial.co.uk/' ) )
			e[n].onclick = function() { alert( 'You are now leaving the regulated site of Grovelawn Limited.\nNeither Grovelawn Limited, nor Sesame Ltd, is responsible for the accuracy of the information contained within the linked site.' ); };
};

