// JavaScript Document

function roll(a) {
	a.style.cursor='pointer';
	if (a.className.substr((a.className.length-1),1) == 'n') {b='o';}
	else {b='n';}
	a.className=a.className.substr(0,(a.className.length-1))+b;
}

function rollo(a){
	if (a.className.substr((a.className.length-2),2) == '_o')
		a.className=a.className.substr(0,(a.className.length-2));
	else
		a.className=a.className+'_o';
}

function rollId(a) {
	a.style.cursor='pointer';
	if (a.id.substr((a.id.length-1),1) == 'n') {b='o';}
	else {b='n';}
	a.id=a.id.substr(0,(a.id.length-1))+b;
}

function rollimg(a) {
	a.style.cursor='pointer';
	ext = a.src.substr((a.src.length-4),4);
	state = a.src.substr((a.src.length-6),2);
	if (state == '_o')
		a.src=a.src.substr(0,(a.src.length-6))+ext;
	else
		a.src=a.src.substr(0,(a.src.length-4))+'_o'+ext;
}

function popPic(){
}

function insert_flash (){
	parameters = '';
	file = '';
	HTMLobj = '';
	width = '';
	height = '';
	bgcolor = '#ffffff';
	wmode = 'transparent';
	
	for(var i=0; i<arguments.length; i++){
	    arg = arguments[i].split('=');
	  	if (arg[0] == 'file'){file = arg[1]}
		else if (arg[0] == 'HTMLobj'){HTMLobj = arg[1]}
		else if (arg[0] == 'width'){width = arg[1]}
		else if (arg[0] == 'height'){height = arg[1]}
		else if (arg[0] == 'bgcolor'){bgcolor = arg[1]}
		else if (arg[0] == 'wmode'){wmode = arg[1]}
		else {parameters += arguments[i]}
	}
	//alert(file + '\n' + HTMLobj + '\n' + width + '\n' +height + '\n' +bgcolor + '\n' +wmode + '\n' +parameters);
	document.getElementById(HTMLobj).innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" id="temp" align="middle">'+
	'<param name="allowScriptAccess" value="sameDomain" />'+
	'<param name="movie" value="'+file+'" />'+
	'<param name="quality" value="high" />'+
	'<param name="bgcolor" value="'+bgcolor+'" />'+
	'<param name="wmode" value="'+wmode+'" />'+
	'<param name="FlashVars" value="'+parameters+'">'+
	'<embed src="'+file+'" FlashVars="'+parameters+'" quality="high" wmode="'+wmode+'" bgcolor="'+bgcolor+'" width="'+width+'" height="'+height+'" name="temp" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'+
	'</object>';
}

var picWin;
function showImg(a) {
	if (typeof(picWin) != 'undefined' && !(picWin.closed)) {picWin.close();}
	picWin = window.open('','picWin','scrollbars=1,menubar=0,left=0,top=0,width=300px,height=300px,dependent=1,resizable=0', replace=1);
	picWin.document.open();
	picWin.document.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>image popup</title>');
	picWin.document.write('<style type="text/css">body{background-color:#FFF8EA;background-image:url(images/background.gif);margin:0px;}.text{font-family: Arial, Helvetica, sans-serif;font-size:12px;color:#660000;text-align:center;}.load{font-family:Arial,Helvetica,sans-serif;font-size:12px;font-weight:bold;color:#CCCCCC;width:80px;text-align:left;}.hide{display:none;}.show{display:;}</style>');
	picWin.document.write('</head><body onLoad="checksize();"scroll=yes>');
	picWin.document.write('<script language=javascript>document.oncontextmenu=new Function("return false");function checksize(){if(document.images[0].complete){document.getElementById("load").className="hide";document.getElementById("show").className="show";h=document.images[0].width+40;v=document.images[0].height+110;if(h>(screen.availWidth-10)){h=screen.availWidth-10;document.body.scroll="yes";}if(v>(screen.availHeight-20)){v=screen.availHeight-20;document.body.scroll="yes";}window.resizeTo(h,v);window.focus();}else{setTimeout("checksize()",250)}}</script>');
	picWin.document.write('<table width="100%" height="100%"  border="0" cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle"><div class="load" id="load">LOADING . . .</div><div class="hide" id="show"><img src="'+propImagesPop[a]+'"></div></td></tr></table></body></html>');
	picWin.document.close();
}



// Down Payment / Monthly Payment functions
var loan_length = new Array(48,60,96,120,180,240);
var calulatedInterestedRates = new Array(5,6,7,8,9,10);
var calulatedMonthlyPayments = new Array();

roundWithZero = function(number,fraction){
	if ((fraction == undefined) || (fraction == null)){fraction = 1}
	number = Math.round(number*Math.pow(10,fraction))/Math.pow(10,fraction);
	if (number.toString().substring(number.toString().indexOf('.')+1*fraction) == ''){ number = number.toString().concat('0') };
	if (number == 0){number = 0}
	return number;
}

setDownPayment = function(downpayment,original_price){
	document.getElementById('downpayment').value = roundWithZero(downpayment.substring(1),2);
	document.getElementById('price_reduced_by').value = roundWithZero(downpayment.substring(1)*0.5,2);
	document.getElementById('final_price').value = roundWithZero(original_price - downpayment.substring(1)*0.5, 0);
}

// calculateMonthlyPayment(14995,180/12,12,9);
// 14995 - final property price, 180/12 - for how many months/12 - months in a year, 12 - months base, 9 - interest rate

calculateMonthlyPayment = function(loan_amount,loan_length,pay_periodicity,annual_interest){
		c_balance         = loan_amount;
		total_periods     = loan_length * pay_periodicity;
		interest_percent  = annual_interest / 100;
		period_interest   = interest_percent / pay_periodicity;
		c_period_payment  = loan_amount * (period_interest / (1 - Math.pow((1 + period_interest), -(total_periods))));

		if (c_period_payment < 50){c_period_payment = 50}

		return roundWithZero(c_period_payment,2);
}

calculateMonthlyPayments = function(){
	var rate = 5;
	for (i = 0; i < loan_length.length; i++){
		calulatedMonthlyPayments[i] = calculateMonthlyPayment(document.getElementById('final_price').value,loan_length[i]/12,12,calulatedInterestedRates[i]);
		// check if the new calulatedMonthlyPayments is equal to th eprevious one or equal to 50. Then assign to calulatedInterestedRates the first calulatedInterestedRates belongs to the first 50
		if (calulatedMonthlyPayments[i] != calulatedMonthlyPayments[i-1]){last = i} else {
			calulatedInterestedRates[i] = calulatedInterestedRates[last];
			loan_length[i] = loan_length[last];
			}
	}
}

setMonthlyPayments = function(){
	for (i=0; i < calulatedMonthlyPayments.length; i++){
		document.getElementById('monthlyPayment_'+(calulatedMonthlyPayments.length-1-i)).value = '$'+calulatedMonthlyPayments[i];
	}
}

setMonthlyPaymentFields = function(obj){
	if(obj == undefined){num = 0} else { num = obj.id.substring(obj.id.indexOf('_')+1) }
	document.getElementById('monthly_payment').value = document.getElementById('monthlyPayment_'+num).value.substring(1);
	document.getElementById('interest_rate').value = calulatedInterestedRates[5-num];
	document.getElementById('number_payments').value = loan_length[5-num];
}
paymentsValidation = function() {
	if (document.getElementById('downpayment').value == '0'){
		alert('You must choose both a down payment and a monthly payment option');
		return false;
	}
}