// Fix browser display issues
function sniffBrowser() {
	if (navigator.appName.indexOf("Microsoft") >= 0) { // if Browser is Microsoft...
		if (document.getElementById('divQuickSearch')) {
			document.getElementById('divQuickSearch').style.marginLeft = "0";
			}
	}
	else {
		document.getElementById("divNavPrimary").style.marginTop = "25px";
		if (document.getElementById('divColLeft')) {	
			document.getElementById('divColLeft').style.width = "150px";
		}
	}
}

// Preload images for nav bar
navAboutUs = new Image();
	navAboutUs.src = 'images/navAboutUs.gif';
navAboutUsCurrent = new Image();
	navAboutUsCurrent.src = 'images/navAboutUs-current.gif';
navContactUs = new Image();
	navContactUs.src = 'images/navContactUs.gif';
navContactUsCurrent = new Image();
	navContactUsCurrent.src = 'images/navContactUs-current.gif';
navLocations = new Image();
	navLocations.src = 'images/navLocations.gif';
navLocationsCurrent = new Image();
	navLocationsCurrent.src = 'images/navLocations-current.gif';
navBuyersGuide = new Image();
	navBuyersGuide.src = 'images/navBuyersGuide.gif';
navBuyersGuideCurrent = new Image();
	navBuyersGuideCurrent.src = 'images/navBuyersGuide-current.gif';
navSearchMLS = new Image();
	navSearchMLS.src = 'images/navSearchMLS.gif';
navSearchMLSCurrent = new Image();
	navSearchMLSCurrent.src = 'images/navSearchMLS-current.gif';

// create arrays for navigation control
	arrSubNav = new Array('divNavLocations','divNavAboutUs','divNavContactUs','divNavBuyersGuide','divNavSearchMLS');
	arrNavImages = new Array('imgNavLocations','imgNavAboutUs','imgNavContactUs','imgNavBuyersGuide','imgNavSearchMLS');
	
	
// function to secondary nav bar content
function swapNav(imgName,imgObj) {
	//swapTimer = window.setTimeout(functionName(),400)

	document.getElementById(imgName).src = imgObj.src; // highlight current nav button
	for (i in arrNavImages) {
		if (arrNavImages[i] != imgName) {
			origImgSrc = 'nav' + arrNavImages[i].substring(6);
			document.getElementById(arrNavImages[i]).src = eval(origImgSrc).src; // swap back other nav images
		}
	}
	
	divName = 'div' + imgName.substring(3); // set new subnav div
	document.getElementById(divName).style.display = "block"; // display div
	
	for (i in arrSubNav) {
		if (arrSubNav[i] != divName)
			document.getElementById(arrSubNav[i]).style.display = "none"; // hide other divs
	}
}

// function to stop nav swap if 
function stopSwap(theDiv) {
	myTimer=window.setTimeout("finishHide('"+theDiv+"')",400);
	timerOn=true;
}
function finishHide(theDiv) {
	document.getElementById(theDiv).style.visibility="hidden";
}

// function to resize divGalleryImages (for Property Detail Page)
function resizeDiv() {
	//get browser width
	theBrowserWidth = 700; // set default
	if (navigator.appName.indexOf("Microsoft") >= 0) { //if Microsoft
	 theBrowserWidth = document.body.offsetWidth;
	}
	else {
		theBrowserWidth = window.innerWidth;
	}
	theDivWidth = parseInt(theBrowserWidth) - 240;
	if (document.getElementById('divGalleryImages')) {
		document.getElementById('divGalleryImages').style.width = (theDivWidth + 'px');
	}
}

// function to set initial nav bar options
function setNav(theItem) {
	theURL = window.location.href.toLowerCase()
	if ((theURL.indexOf('about.aspx') > 0) && (theURL.indexOf('country=') > 0)) { //if it's an "about" page and it has a country specified in the query string
		theItem = 'Locations';
	}
	primaryNavImage = 'nav' + theItem + 'Current';
	primaryNavImageID = 'imgNav' + theItem;
	secondaryNavDiv = 'divNav' + theItem;
	document.getElementById(primaryNavImageID).src = eval(primaryNavImage).src; // highlight primary nav button
	document.getElementById(secondaryNavDiv).style.display = "block"; // display div
}


var strHighResImgName = ""; //PAGE VARIABLE FOR CURRENT HIGHT RES IMAGE NAME
// function to display images in gallery
function showPic(thePic,theCaption, highResImgName) {
	document.images['imgGalleryCurrent'].src = '/images/' + thePic;
	document.getElementById('imageCaption').innerHTML = theCaption;
	
	//SET THE 
	if(highResImgName != "")
	{
		strHighResImgName = highResImgName;
		document.getElementById('DivHighRes').style.visibility = 'visible';
	}else{
		strHighResImgName = "";
		document.getElementById('DivHighRes').style.visibility = 'hidden';
	}
}

//function to display high res image on property details page
function showHighResImg()
{
	window.open('/images/' + strHighResImgName, 'higRes','toolbars=no,resizable=yes,scrollbars=yes');
}

//function to launch the Compare Properties page
function compareProps() {
	propWin = window.open('PropertyCompare.aspx','propWin','toolbars=no,resizable=yes,scrollbars=yes');
}

//FUNCTION TO CHECK THE SELECTED COMPARED PROPERTIES
function checkSelectedComparable()
{
	var selectedValues = getCookie("compareProps");
	if(selectedValues != null && selectedValues != "null"){
		var PropIds = new Array();
		PropIds = selectedValues.split(',');		
		for(x=0; x < PropIds.length; x++){
			var elmChk = document.getElementById("Comp_" + PropIds[x]);
			if(elmChk != null) elmChk.checked = true;
		}
	}
}

//FUNCTION TO SET COMPARE PROPERTIES
function setComparedProp(compProp)
{
	var currCookieVal = getCookie("compareProps");
	var proId = compProp.value;
	var removeId = proId +",";
	
	//remove any null values
	if(currCookieVal == null || currCookieVal == 'null') currCookieVal = "";
	
	var commaCount = 0;
	var strTempStr = currCookieVal;
	while(strTempStr.indexOf(",") != -1){
		strTempStr = strTempStr.substring(strTempStr.indexOf(",") + 1,strTempStr.length);
		commaCount++;
	}
	
	//REMOVE OPTION FROM COOKIE
	if(!compProp.checked){
		var startPos = currCookieVal.indexOf(removeId);		
		var endPos	= startPos + removeId.length;
		
		//alert(startPos + " " + endPos);
		var firstStr = currCookieVal.substring(0,startPos);
		var lastStr = currCookieVal.substring(endPos,currCookieVal.length);
		currCookieVal = firstStr + lastStr;
		document.cookie = "compareProps=" + currCookieVal + "; path=/;";
		commaCount--;
	}else{
		if(commaCount < 4){
			currCookieVal +=  proId + ",";
			document.cookie = "compareProps=" + currCookieVal + "; path=/;";
		}
	}
	
	//DISPLAY MAX PROP MESSAGE
	var maxPropDIV = document.getElementById('maxPropElem');
	if(commaCount >= 4){
		maxPropDIV.style.display = 'block';
		compProp.checked = false;
		window.scrollTo(0,0);
	}else{
		maxPropDIV.style.display = 'none';
	}
	
}

//THIS FUNCTION GETS A VALUE FROM A COOKIE BY NAME
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function setConversions()
{
	var date = new Date();
	date.setFullYear(date.getFullYear()+1);
	var objSelectLists = document.getElementsByTagName("SELECT");		
	
	for(x = 0; x < objSelectLists.length; x++)
	{	
		//SET CURRENCY
		if(	objSelectLists[x].name.indexOf("lstCurrency") != -1)
		{
			document.cookie = "prefsCurrency=" + objSelectLists[x].options[objSelectLists[x].selectedIndex].value + 
				";path=/;expires=" + date.toGMTString();			
		}
		
		//SET AREA
		if(	objSelectLists[x].name.indexOf("lstArea") != -1)
		{
			document.cookie = "prefsArea=" + objSelectLists[x].options[objSelectLists[x].selectedIndex].value + 
				";path=/;expires=" + date.toGMTString();
		}
			
		//SET DISTANCE
		if(	objSelectLists[x].name.indexOf("lstDistance") != -1)
		{
			document.cookie = "prefsDistance=" + objSelectLists[x].options[objSelectLists[x].selectedIndex].value + 
				";path=/;expires=" + date.toGMTString();
		}		
	}		
	document.forms[0].submit();
}

// generic function to open a new window
function popWin(theFile) {
	poppedWin = window.open(theFile,
						'poppedWin',
						'width=400,height=550,top=10,left=10,screenx=10,screeny=10,resizable=yes');
}

// BEGIN Code related to the Mortgage Calculator
function floor(number)
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function _calc() {
  var mi = document.piti.interest.value / 1200;
  var base = 1;
  var mbase = 1 + mi;
  for (i=0; i<document.piti.years.value * 12; i++)
  {
    base = base * mbase;
  }
  document.piti.p_i.value = floor(document.piti.amt.value * mi / ( 1 - (1/base)));
  document.piti.mtax.value = floor(document.piti.tax.value / 12);
  document.piti.mins.value = floor(document.piti.ins.value / 12);
  document.piti.mhoassn.value = floor(document.piti.hoassn.value / 12);
  var _sum = document.piti.amt.value * mi / ( 1 - (1/base)) +
	document.piti.tax.value / 12 + 
	document.piti.ins.value / 12 +
	document.piti.hoassn.value / 12;
  document.piti.mpay.value = floor(_sum);
}

function clearVal(_field) {
	curVal = _field.value;
	_field.value='';
}

function checkVal(_field) {
	if (_field.value == '') { //if, on exit, the field is blank and a
		if (curVal)			  // previous value exists, re-enter it
		_field.value=curVal;
	}
	else {					  // otherwise check the new value
		var newVal = '';
		var v = _field.value;
		for (i=0;i<v.length;i++) {
			_char = v.substring(i,i+1);  // check each character for num or dot
			if ((!isNaN(_char) && !isNaN(parseInt(_char))) || _char == '.')
				newVal += _char;  // create new value composed of only those chars
		}
		if (newVal == '')
			newVal = curVal;
		_field.value=newVal;
	}
}

function _reset() {
	document.piti.reset();
}
// END Code related to the Mortgage Calculator
