var currentOpenDivName = null;
function showDiv(name, onlyClose)
{
	onlyClose = onlyClose || (currentOpenDivName == name);
	currentOpenDivName = null;
	var div = document.getElementById(name);
	if ((div.style.display == "") && !onlyClose)
	{
		if (!jQuery.browser.mozilla)
		{
			var txtContainer = document.getElementById(name.replace('div', 'txt'));
			if (!txtContainer)
			{
				txtContainer = document.getElementById("ctl00_" + name.replace('div', 'txt'));
			}
			var container = $(txtContainer);
			var top = (container.position().top + container.height());
			top = top - 30;// + $("#divSearchInner").scrollTop();
			div.style.top = top + "px";
		}
		div.style.display = "block";
		currentOpenDivName = name;
	}
	else
	{
		div.style.display = "";
	}
}

function closeAll(sender)
{
	showDiv("divMinPrice", true);
	showDiv("divMaxPrice", true);
	showDiv("divSector", true);
}

var catchEnterSet = false;
function setCatchEnter()
{
	if (!catchEnterSet)
	{
		catchEnterSet = true;
		$(document).keydown(function (e) {
			if (e.keyCode == 13)
			{
				document.location = document.getElementById("ctl00_lnkCenturySearch").href;
				return false;
			}
			else
			{
				return true;
			}
		});
	}
}

function setVal(name, sender, hiddenKey)
{
	if (hiddenKey)
	{
		document.getElementById(name).innerHTML = sender.innerHTML;
		if (hiddenKey != -1)
		{
			val = hiddenKey + "|" + sender.innerHTML;
		}
		else
		{
			val = "";
		}
		document.getElementById("ctl00_" + name.replace('txt', 'hid')).value = val;
	}
	else
	{
		document.getElementById("ctl00_" + name).value = sender.innerHTML.replace(/&nbsp;/g, "").replace(" ", "").replace("$", "").replace(".", "").replace(/,/g, "");
	}
	showDiv(name.replace('txt', 'div'), true);
}

function openMenu(sender)
{
	var subMenu = sender.getElementsByTagName("DIV");
	if (subMenu.length > 0)
	{
		subMenu[0].style.display = "block";
	}
}

function closeMenu(sender)
{
	var subMenu = sender.getElementsByTagName("DIV");
	if (subMenu.length > 0)
	{
		subMenu[0].style.display = "none";
	}
}

function go2News(newsKey)
{
	document.location = $(".lnkAllNews")[0].href + '#newsKey' + newsKey;
}

function doPrint()
{
	var divAllPictures = document.getElementById("divAllPictures");
	var innerHtm = "";
	var breakCount = 0;
	for (var i = 0; i < arrImages.length; i++)
	{
		if ((i % 2) == 0)
		{
			innerHtm += "<br style='clear: both' />";
			if (breakCount == 5)
			{
				breakCount = 0;
				innerHtm += "<br style='page-break-after: always;' />";
			}
			else
			{
				innerHtm += "<br style='clear: both' />";
			}
			breakCount++;
		}
		innerHtm += String.format("<img src='/cms/controller/services/Thumbnail.ashx?width=300&height=210&fileName=/media/century21_listingPicture.picture/{0}' style='float: left' />", arrImages[i]);
	}
	divAllPictures.innerHTML = innerHtm;
	window.print();
}

function browserPrintWarn()
{
	if (jQuery.browser.msie)
	{
		alert(SBPhrases["CENTURY21_MSIEPRINT"]);
	}
	return true;
}

var fading = new Array();
function showInfo(index)
{
	if (!fading[index])
	{
		fading[index] = true;
		var divInfo = $("#divInfo" + index);
		divInfo[0].style.marginTop = "-" + (divInfo.height() + 180) + "px";
		divInfo.fadeIn('fast', function() {
			fading[index] = false;
		});
	}
}
function hideInfo(index)
{
	fading[index] = true;
	$("#divInfo" + index).fadeOut('fast', function() {
		fading[index] = false;
	});
}

function setPagesize(sender)
{
	setCookie("PAGESIZE", sender.selectedIndex);
	window.location.reload();
}

function setSortorder(sender)
{
	setCookie("SORTORDER", sender.selectedIndex);
	window.location.reload();
}

function getPaiement(amount, down, rate, years)
{
	setCookie("CALC_DOWN", down);
	setCookie("CALC_RATE", rate);
	setCookie("CALC_YEARS", years);
	var Pret = amount - down;
	var TauxAnnuel = rate/100;
	var TauxMensuel = Math.pow(1+(TauxAnnuel/2),1/6)-1;
	var NbPaiement = years*12;
	return Math.floor((Pret*TauxMensuel)/(1-Math.pow((1+TauxMensuel),(-1*NbPaiement)))*100)/100;
}

function showAdvanced()
{
	document.getElementById("divSearchInner").style.overflow = "auto";
	$("#divSearchInner").animate({scrollTop: 185}, 1000);
}


function doLogin()
{
	UserLogin(document.getElementById("txtUser").value, document.getElementById("txtPass").value, UserLogin_CallBack)
}

function doLogout()
{
	UserLogout(UserLogout_CallBack)
}

function UserLogin_CallBack(data)
{
	if (data.toString().toLowerCase() == trueStr.toLowerCase())
	{
		document.location = loginUrl;
	}
	else
	{
		$('.invalidPassword')[0].style.display = "block";
	}
}

function UserLogout_CallBack(data)
{
	window.location.reload();
}

var frmCaptchaUrl = null;
function setContactLink(emailClicked, agentKey)
{
	window.emailClicked = emailClicked;
	var frmCaptcha = document.getElementById("frmCaptcha");
	if (frmCaptchaUrl == null)
	{
		frmCaptchaUrl = frmCaptcha.src;
	}
	frmCaptcha.src = frmCaptchaUrl + agentKey;
}