function onLoad()
{
	// Mortgage Not Available
	if(document.getElementById('calcPrice'))
	{
		addEvent(document.getElementById('calcPrice'), 'change', calculatePayment);
		addEvent(document.getElementById('calcDownPayment'), 'change', calculatePayment);
		addEvent(document.getElementById('calcInsurance'), 'change', calculatePayment);
		addEvent(document.getElementById('calcTaxes'), 'change', calculatePayment);
		addEvent(document.getElementById('calcTerm'), 'change', calculatePayment);
		addEvent(document.getElementById('calcInterestRate'), 'change', calculatePayment);
		
		calculatePayment();
	}
	
	// Prevent this from running if user need to update email (user.active = 0)
	if(document.getElementById('requestMoreInfoBtn'))
	{
		// Attach Events for Messages on 'What would you like to do?' Buttons
		addEvent(document.getElementById('requestMoreInfoBtn'), 'mouseover', displayMessage);
		addEvent(document.getElementById('requestMoreInfoBtn'), 'mouseout', hideMessage);
		addEvent(document.getElementById('requestAVisitBtn'), 'mouseover', displayMessage);
		addEvent(document.getElementById('requestAVisitBtn'), 'mouseout', hideMessage);
		addEvent(document.getElementById('addToMyListingsBtn'), 'mouseover', displayMessage);
		addEvent(document.getElementById('addToMyListingsBtn'), 'mouseout', hideMessage);
		addEvent(document.getElementById('saveThisSearchBtn'), 'mouseover', displayMessage);
		addEvent(document.getElementById('saveThisSearchBtn'), 'mouseout', hideMessage);
		addEvent(document.getElementById('printListingBtn'), 'mouseover', displayMessage);
		addEvent(document.getElementById('printListingBtn'), 'mouseout', hideMessage);
		addEvent(document.getElementById('sendToAFriendBtn'), 'mouseover', displayMessage);
		addEvent(document.getElementById('sendToAFriendBtn'), 'mouseout', hideMessage);
		addEvent(document.getElementById('goBackBtn'), 'mouseover', displayMessage);
		addEvent(document.getElementById('goBackBtn'), 'mouseout', hideMessage);
	
		if(document.getElementById('prevBtn'))
		{
			addEvent(document.getElementById('prevBtn'), 'mouseover', displayMessage);
			addEvent(document.getElementById('prevBtn'), 'mouseout', hideMessage);
		}
		
		if(document.getElementById('nextBtn'))
		{
			addEvent(document.getElementById('nextBtn'), 'mouseover', displayMessage);
			addEvent(document.getElementById('nextBtn'), 'mouseout', hideMessage);
		}
		
		addEvent(document.getElementById('printListingBtn'), 'click', printIt);
		addEvent(document.getElementById('sendToAFriendBtn'), 'click', refer);
		addEvent(document.getElementById('addToMyListingsBtn'), 'click', myListings);
		addEventImageRotate();
	}
}

// ***************** functions for 'What would you like to do?' Messages **********
function displayMessage(event)
{
	var messageMap = {
		'requestMoreInfoBtn':'Request more information about this listing.',
		'requestAVisitBtn': 'Like it? Why not come and see it.',
		'addToMyListingsBtn': 'Clicking here will save this listing for future reference.',
		'saveThisSearchBtn': 'Still haven\'t found your dream home? Click here to get email when new houses just like this one come on the market.',
		'printListingBtn': 'Print this listing and take it with you.',
		'sendToAFriendBtn': 'Know someone that would like this? Why not drop them an email.',
		'goBackBtn': 'Click here to go back to the main serach page.',
		'nextBtn': 'View the next listing.',
		'prevBtn': 'View the previous listing.'
	};
	
	var e = event || window.event;
	var src = e.target || e.srcElement;
	var messageDiv = document.getElementById('actionRemark');
	messageDiv.innerHTML = messageMap[src.id];
	
}

function hideMessage()
{
	var messageDiv = document.getElementById('actionRemark');
	messageDiv.innerHTML = '';
}

function printIt()
{
	window.open("print.html?mlsid="+mlsId,"print","height=450,width=800,status=0,scrollbars=1,location=0,menubar=0,resizable=0");
}

function refer()
{
	window.open("refer.html?mlsid="+mlsId,"talk","height=351,width=323,status=0,scrollbars=0,location=0,menubar=0,resizable=0");
}

function myListings()
{
	document.location = "mypage.html?mlsid="+mlsId+"&back=" + request_uri + "&show=mylistings&add=false";
}
// ****************** Payment Calculator **************

function formatCurrency(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;

	for(var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));

	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function calculatePayment()
{
	var price = document.getElementById('calcPrice').value;
	var downPayment = document.getElementById('calcDownPayment').value;
	var taxes = document.getElementById('calcTaxes').value;
	var insurance = document.getElementById('calcInsurance').value;
	
	var termElem = document.getElementById('calcTerm');
	var interestRateElem = document.getElementById('calcInterestRate');
	var term = parseInt(termElem.options[termElem.selectedIndex].value);
	var interestRate = parseInt(interestRateElem.options[interestRateElem.selectedIndex].value);
	
	var paymentElem = document.getElementById('calcPayment');
	
	if(isNaN(taxes))
		taxes = 0;
		
	if(isNaN(insurance))
		insurance = 0;
	
	price = parseFloat(price);
	downPayment = parseFloat(downPayment);
	taxes = parseFloat(taxes);
	insurance = parseFloat(insurance);
	
	var princ = price - downPayment;
	interestRate = (interestRate / 100) / 12;
	var months = term * 12;
	
	var payment = Math.floor((princ*interestRate)/(1-Math.pow(1+interestRate,(-1*months)))*100)/100;
	payment += Math.floor((taxes + insurance) / 12);
	
	paymentElem.value = formatCurrency(payment);
}

function isEmailAddr(email)
{
	var theStr = new String(email);
	var index = theStr.indexOf("@");

	if(index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if((pindex > index+1) && (theStr.length > pindex+1))
			return true;
	}
	
	alert(email + " does not appear to be a valid email address."); 
	return false;
}


// ******************** 