function addToFavorites(t, u)
{
	if(document.all)
		window.external.AddFavorite(u, t);
	else if(window.sidebar)
		alert('Your web browser does not support automatically adding bookmarks\nPress Ctrl D to bookmark this page.');
}
function setImage(i)
{
	if(!imgs.length) return;
			  
	document.getElementById("img"+last_img).style.border = "2px solid #ffffff";
	document.getElementById("img"+i).style.border = "2px solid #0c6bc4";
	document.getElementById("img").src = imgs[i];
	last_img = i;
}
function nextImage()
{
	if(last_img < (imgs.length-1))
		setImage(last_img+1);
}
function prevImage()
{
	if(last_img > 0)
		setImage(last_img-1);
}
function clickImage()
{
	window.open(imgurls[last_img],"","width="+wdths[last_img]+",height="+hghts[last_img], 'scrollbars=no');
}
function preloadImages()
{
	var i,j;
	
	if(!document.MM_p) document.MM_p = new Array();
	
	for(i = 0, j = document.MM_p.length; i < preloadImages.arguments.length; i++) if(preloadImages.arguments[i].indexOf("#") != 0)
	{
		document.MM_p[j] = new Image;
		document.MM_p[j].src = preloadImages.arguments[i];
		j++;
	}
}

function recalculateDownpayment()
{
	var price, downpayment, percent, rate, i, j;
	
	price = parseIntImproved(document.getElementById("a1").value);
	downpayment =  parseIntImproved(document.getElementById("a2").value);
	percent = Math.round(downpayment * 400 / price);
	for(i = 0, j = 1; i < 100; i += 5, j++) if(percent == (i * 4))
	{
		document.getElementById("a3").selectedIndex = j;
		break;
	}
	
	if(i == 100)
		document.getElementById("a3").selectedIndex = 0;
	
	rate = parseFloat(document.getElementById("a4").value);

	recalculate(price, downpayment, rate);
}

function recalculatePercent()
{
	var price, downpayment, rate;
	
	price = parseIntImproved(document.getElementById("a1").value);
	downpayment = price * parseIntImproved(document.getElementById("a3").value) / 100;
	
	document.getElementById("a2").value = downpayment;

	rate = parseFloat(document.getElementById("a4").value);

	recalculate(price, downpayment, rate);
}

function recalculate(price, downpayment, rate)
{
	var ttl, i;
	var loanAmount = price - downpayment;
	var monthlyPayment = new Array;
	var r = rate / 1200;

	monthlyPayment[0] = Math.floor( loanAmount * r / (1 - Math.pow(1/(1 + r), 30*12)) );
	monthlyPayment[1] = Math.floor( loanAmount * r );
	monthlyPayment[2] = Math.floor( loanAmount * r / (1 - Math.pow(1/(1 + r), 15*12)) );
	
	setElementText("askingPrice", " $"+addCommas(price)+" ");
	setElementText("downpaymentAmount", " $"+addCommas(downpayment)+" ");
	setElementText("loanAmount", " $"+addCommas(loanAmount));
	
	for(i = 0; i < 3; i++)
	{
		ttl = monthlyPayment[i] + Math.floor(price * 0.015 / 12) + Math.floor(price * 0.005 / 12);

		setElementText("monthlyPayment"+i, "$"+addCommas(monthlyPayment[i]));
		setElementText("taxEstimate"+i, "$"+addCommas(Math.floor(price * 0.012 / 12)));
		setElementText("insuranceEstimate"+i, "$"+addCommas(Math.floor(price * 0.0043 / 12)));
		
		if(i == 1)
			setElementText("pmiEstimate"+i, "");
		else if((downpayment / price) >= 0.2)
			setElementText("pmiEstimate"+i, "$0");
		else
		{
			setElementText("pmiEstimate"+i, "$"+addCommas(Math.floor(loanAmount * 0.0005)));
			ttl += Math.floor(loanAmount * 0.0005);
		}
		
		setElementText("totalPayment"+i, "$"+addCommas(ttl));
		setElementText("requiredIncome"+i, "$"+Math.floor(ttl * 12 / 280)+"k");
	}
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function setElementText(c, t)
{
	var obj = document.getElementById(c);
	var text = document.createTextNode(t);
	while(obj.firstChild) obj.removeChild(obj.firstChild);
	obj.appendChild(text);
}
  
function clearElement(obj)
{
	while(obj.firstChild) obj.removeChild(obj.firstChild);
}

function parseIntImproved(i)
{
	i = i.replace(/\,/g, "");
	i = i.replace(/\$/g, "");
	return parseInt(i, 10);
}

function scrollLeft()
{
	var o = document.getElementById('thumbsInner');
	var i = parseInt(o.style.left, 10);

	if(isNaN(i))
		i = 0;
	i += 3;

	if(i >= 0)
	{
		i = 0;
		stopScrolling();
	}
	
	o.style.left = i+"px";
}
function scrollRight()
{
	var o = document.getElementById('thumbsInner');
	var i = parseInt(o.style.left, 10);

	if(isNaN(i))
		i = 0;
	i -= 3;
	
	if(i <= (scrollMax*-1))
	{
		i = (scrollMax*-1);
		stopScrolling();
	}
		
	o.style.left = i+"px";
}
function startScrollLeft()
{
	stopScrolling();
	scrollTimer = setInterval("scrollLeft()", 33);
}
function startScrollRight()
{
	stopScrolling();
	scrollTimer = setInterval("scrollRight()", 33);
}
function stopScrolling()
{
	if(scrollTimer == -1)
		return;
		
	clearInterval(scrollTimer);
	scrollTimer = -1;
}