﻿// JScript File
var MCSRefreshWait= 750;
var existSession = 'False';
var favoritesInUser = 'False';

var insertFavoriteFeedBack = '';
var textToLinkAddFavorite = '';
var idLink = '';

function changeExistSessionAndFavoritesInUser (session, favsInUser)
{
	existSession = session;
	favoritesInUser = favsInUser;
}

function hideMRek ()
{
	if(document.getElementById('iFrameMrek'))
		document.getElementById('iFrameMrek').style.visibility = 'hidden';
	
	if(document.getElementById('SearchTopFrame'))
		document.getElementById('SearchTopFrame').style.visibility = 'hidden';
}

function showMRek ()
{
	if(document.getElementById('iFrameMrek'))
		document.getElementById('iFrameMrek').style.visibility = 'visible';
	
	if(document.getElementById('SearchTopFrame'))
		document.getElementById('SearchTopFrame').style.visibility = 'visible';
}

function addEstateFavoriteWithUserOption (params, popupDivID, popupHiddenID, popupIframeID, title)
{
	hideSessionFavorites ();
	if (existSession == 'False' && favoritesInUser == 'False')
	{
		var src = '/' + lingua + '/AreaPessoal/PopUps/FavoritesOption.aspx?FavType=ESTATE&'+params;
		
		HideVideo();
		LoadIframeForPopup_OnClick (popupDivID, popupHiddenID, popupIframeID, src, title);
	}
	else 
	{
		addEstateFavoriteByHttpRequest (params, popupDivID, popupHiddenID, popupIframeID, title);
	}
}

function addSearchFavoriteWithUserOption (params, popupDivID, popupHiddenID, popupIframeID, title)
{
	hideSessionFavorites ();
	if (existSession == 'False' && favoritesInUser == 'False')
	{
		var src = '/' + lingua + '/AreaPessoal/PopUps/FavoritesOption.aspx?FavType=SEARCH&'+params;
		
		HideVideo();
		LoadIframeForPopup_OnClick (popupDivID, popupHiddenID, popupIframeID, src, title);
	}
	else 
	{
		addSearchFavoriteByHttpRequest (params, popupDivID, popupHiddenID, popupIframeID, title);
	}
}

function addEstateFavoriteByHttpRequest (params)
{	
	hideSessionFavorites ();
	var url= "/" + lingua + "/AreaPessoal/Ajax/InsertEstateFavorite.aspx?" + params + "&ticks=" + new Date().getTime();
	RequestHttp (url, httpRequestCallbackInsertFavorite, null, null);
}

function addSearchFavoriteByHttpRequest (params)
{	
	hideSessionFavorites ();
	var url= "/" + lingua + "/AreaPessoal/Ajax/InsertSearchFavorite.aspx?" + params + "&ticks=" + new Date().getTime();
	RequestHttp (url, httpRequestCallbackInsertFavorite, null, null);
}

function removeAllFavorite (params)
{
	hideSessionFavorites ();
	var url= "/" + lingua + "/AreaPessoal/Ajax/RemoveAllFavorites.aspx?" + params + "&ticks=" + new Date().getTime();
	RequestHttp (url, httpRequestCallbackUpdateMCSMenu, null, null);
}

function removeFavorite (params)
{
	hideSessionFavorites ();
	var url = "/" + lingua + "/AreaPessoal/Ajax/RemoveFavorite.aspx?" + params + '&ticks=' + new Date().getTime();
	RequestHttp (url, httpRequestCallbackUpdateMCSMenu, null, null);
}

function activateAlert (params, id, title)
{
	var src = '/' + lingua + '/AreaPessoal/PopUps/ActivateAlert.aspx?' + params;
	
	HideVideo();	
	OpenPopupAndFinishLoading (id, src, title);
}

function removeAlert (params)
{
	hideSessionFavorites ();
	var url = "/" + lingua + "/AreaPessoal/Ajax/RemoveAlert.aspx?" + params+ '&ticks=' + new Date().getTime();
	RequestHttp (url, httpRequestCallbackReloadPage, null, null);
}

function removeContactRequest (params)
{
	hideSessionFavorites ();
	var url = "/" + lingua + "/AreaPessoal/Ajax/RemoveContactRequest.aspx?" + params+ '&ticks=' + new Date().getTime();
	RequestHttp (url, httpRequestCallbackReloadPage, null, null);
}

function changeClassicAlert (params, popupDivID, popupHiddenID, popupIframeID, title)
{
	hideSessionFavorites ();
	var src = '/' + lingua + '/AreaPessoal/PopUps/AlterClassicAlert.aspx?'+params;
	
	HideVideo();
	LoadIframeForPopup_OnClick (popupDivID, popupHiddenID, popupIframeID, src, title);
}

function sendListToFriend (params, popupDivID, popupHiddenID, popupIframeID, title)
{
	hideSessionFavorites ();
	var src = '/' + lingua + '/AreaPessoal/PopUps/SendListToFriend.aspx?'+params;
	
	HideVideo();
	LoadIframeForPopup_OnClick (popupDivID, popupHiddenID, popupIframeID, src, title);
}

function saveSessionToMail (popupDivID, popupHiddenID, popupIframeID, title)
{
	hideSessionFavorites ();
	var src = '/' + lingua + '/AreaPessoal/PopUps/SaveSession.aspx';
	
	HideVideo();
	LoadIframeForPopup_OnClick (popupDivID, popupHiddenID, popupIframeID, src, title);
}

function showAgentDetails (id)
{
	var divElem = document.getElementById ('AgentDetails_' + id);
	if (divElem.style.display == 'none')
	{
		divElem.style.display = 'block';
	}
	else
	{
		divElem.style.display = 'none';
	}
}

function showContactData (id)
{
	var divElem = document.getElementById ('ContactRequestDetail_' + id);
	if (divElem.style.display == 'none')
	{
		divElem.style.display = 'block';
	}
	else
	{
		divElem.style.display = 'none';
	}
}

function hideSessionFavorites ()
{
	showMRek ();
	if (document.getElementById ('searchFavorites'))
	{
		animatedcollapse.hide('searchFavorites');
	}
	if (document.getElementById ('estateFavorites'))
	{
		animatedcollapse.hide('estateFavorites');
	}

	var divCloseMCS = document.getElementById ('__CloseMCS');
	if (divCloseMCS != null)
	{
		divCloseMCS.style.display = 'none';
	}	

	if (document.getElementById ('btnSearches'))
		document.getElementById ('btnSearches').className = 'myBots';

	if (document.getElementById ('btnEstates'))
		document.getElementById ('btnEstates').className = 'myBots';
}

function getSessionEstateFavorites ()
{
	var divCloseMCS = document.getElementById ('__CloseMCS');
	if (divCloseMCS != null)
	{
		divCloseMCS.style.display = '';
	}	
	
	var divElem = document.getElementById ('estateFavorites');
	if (divElem == null || divElem.innerHTML == '')
	{
		var url= "/" + lingua + "/AreaPessoal/Ajax/GetSessionFavorites.aspx?ticks=" + new Date().getTime();
		RequestHttp (url, bindSessionEstateFavorites, null, 'dropDownMenu');
	}
	else 
	{
		showSessionEstateFavorites();
	}
}

function bindSessionEstateFavorites (req, id)
{
	AtribDadosElem (req, id);
	showSessionEstateFavorites ();
}

function showSessionEstateFavorites ()
{
	hideMRek ();
	var divElem = document.getElementById ('estateFavorites');
	var divSearchFavorites = document.getElementById ('searchFavorites');
	if (divSearchFavorites != null)
	{
		animatedcollapse.hide('searchFavorites');
		if (document.getElementById ('btnSearches'))
			document.getElementById ('btnSearches').className = 'myBots';
	}
	if (divElem.style.display == 'none')
	{
		if (document.getElementById ('btnEstates'))
			document.getElementById ('btnEstates').className = 'myBots btn-slide active';
		animatedcollapse.show('estateFavorites');
	}
	else
	{
		hideSessionFavorites ();
	}
}

function getSessionSearchFavorites ()
{
	var divCloseMCS = document.getElementById ('__CloseMCS');
	if (divCloseMCS != null)
	{
		divCloseMCS.style.display = '';
	}
	
	var divElem = document.getElementById ('searchFavorites');
	if (divElem == null || divElem.innerHTML == '')
	{
		var url= "/" + lingua + "/AreaPessoal/Ajax/GetSessionFavorites.aspx?ticks=" + new Date().getTime();
		RequestHttp (url, bindSessionSearchFavorites, null, 'dropDownMenu');
	}
	else
	{
		showSessionSearchFavorites();
	}
}

function bindSessionSearchFavorites (req, id)
{
	AtribDadosElem (req, id);
	showSessionSearchFavorites ();
}

function showSessionSearchFavorites ()
{
	hideMRek ();
	var divElem = document.getElementById ('searchFavorites');
	var divEstateFavorites = document.getElementById ('estateFavorites');
	if (divEstateFavorites != null)
	{
		animatedcollapse.hide('estateFavorites');
		if (document.getElementById ('btnEstates'))
			document.getElementById ('btnEstates').className = 'myBots';
	}
	if (divElem.style.display == 'none')
	{
		if (document.getElementById ('btnSearches'))
			document.getElementById ('btnSearches').className = 'myBots btn-slide active';
		animatedcollapse.show('searchFavorites');
	}
	else
	{
		hideSessionFavorites ();
	}
}

function openProfile (popupDivID, popupHiddenID, popupIframeID, title)
{
	hideSessionFavorites ();
	var src = '/' + lingua + '/AreaPessoal/PopUps/Profile.aspx';
	
	HideVideo();
	LoadIframeForPopup_OnClick (popupDivID, popupHiddenID, popupIframeID, src, title);
}

function openRequestAreaPessoal (popupDivID, popupHiddenID, popupIframeID, title)
{
	hideSessionFavorites ();
	var src = '/' + lingua + '/AreaPessoal/PopUps/RequestAreaPessoal.aspx';
	
	HideVideo();
	LoadIframeForPopup_OnClick (popupDivID, popupHiddenID, popupIframeID, src, title);
}

function openConfirmSessionReplace (param, popupDivID, popupHiddenID, popupIframeID, title)
{
	var src = '/' + lingua + '/AreaPessoal/PopUps/ConfirmSessionReplace.aspx?' + param;
	
	HideVideo();
	LoadIframeForPopup_OnClick (popupDivID, popupHiddenID, popupIframeID, src, title);
}

function openFAQs ()
{
	var Topo=window.screen.availHeight/2-250;
	var Dta=window.screen.availWidth/2-275;
	
	window.open('/' + lingua + '/AreaPessoal/Faqs.aspx','MyCasaSapoFAQs','height=520,width=620,top='+Topo+',left='+Dta+',status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

function showHideRegister()
{
	var divRegister = document.getElementById('registerUser');
	var rOptionUserAccount = document.getElementById('rOptionUserAccount');
	if (rOptionUserAccount.checked)
	{
		divRegister.style.display = 'block';
		if (document.getElementById('divSaveOption'))
		{
			document.getElementById('divSaveOption').style.display = 'block';
		}
		if (document.getElementById('btSaveOption'))
		{
			try
			{
				document.getElementById('btSaveOption').title = btnRegisterUserText;
				document.getElementById('btSaveOption').value = btnRegisterUserText;
			}
			catch (ex) { }
		}
	}
	else
	{
		divRegister.style.display = 'none';
		if (document.getElementById('divSaveOption'))
		{
			document.getElementById('divSaveOption').style.display = 'block';
		}
		if (document.getElementById('btSaveOption'))
		{
			try
			{
				document.getElementById('btSaveOption').title = btnSaveFavoriteText;
				document.getElementById('btSaveOption').value = btnSaveFavoriteText;
			}
			catch (ex) { }
		}
	}
}

/* #################################### HTTPREQUEST CALLBACK ################################### */

// Recarregar IFrame
function ReloadMenuMCS ()
{
	var url= "/" + lingua + "/AreaPessoal/Ajax/UpdateSessionMenu.aspx?ticks=" + new Date().getTime();
	RequestHttp (url, AtribDadosElem, null, 'MCSSessionMenu');
	showMRek ();
}

function httpRequestCallbackUpdateMCSMenu (req, id)
{
	ReloadMenuMCS();
}

function httpRequestCallbackInsertFavorite (req, id)
{
	AtribDadosElem(req, 'insertFavoriteHttpRequestResult');
	
	if(insertFavoriteFeedBack == '')
	{
		if (document.getElementById('insertFavoriteHttpRequestResultEvalFail'))
		{
			insertFavoriteFeedBack = document.getElementById('insertFavoriteHttpRequestResultEvalFail').innerHTML.split(":|:")[0];
			textToLinkAddFavorite = document.getElementById('insertFavoriteHttpRequestResultEvalFail').innerHTML.split(":|:")[1];
			idLink = document.getElementById('insertFavoriteHttpRequestResultEvalFail').innerHTML.split(":|:")[2];
		}
	}
	
	switch (insertFavoriteFeedBack)
	{
		case 'SUCCESS':
			if (document.getElementById(idLink))
			{
				document.getElementById(idLink).innerHTML = textToLinkAddFavorite;
			}
			ReloadMenuMCS();
			break;
		case 'ALREADY_EXISTS':
			if (document.getElementById(idLink))
			{
				document.getElementById(idLink).innerHTML = textToLinkAddFavorite;
			}
			break;
		default:
			ReloadMenuMCS();
			break;
	}
}

function httpRequestCallbackReloadPage (req, id)
{
	window.location.reload();
}
/* ####################################### IFRAMES CLOSE ####################################### */
function closeWithoutExecute (time)
{
	if(time==0)
		RemoveIf('ifMCS');
	else
		setTimeout('RemoveIf(\'ifMCS\');',time);
}

function closeWithPageReload (time)
{
	if(time==0)
	{
		RemoveIf ('ifMCS', 'window.location.reload();', MCSRefreshWait);
	}
	else
	{
		setTimeout ('RemoveIf(\'ifMCS\',\'window.location.reload();\',' + MCSRefreshWait.toString() + ');', time);
	}
}

function closeWithAlertPageReload (time)
{
	if(time==0)
	{
		RemoveIf ('ifMCS', 'window.location = /' + lingua + '/AreaPessoal/AreaPessoal.aspx', MCSRefreshWait);
	}
	else
	{
		setTimeout ('RemoveIf(\'ifMCS\',\'window.location = "/' + lingua + '/AreaPessoal/AreaPessoal.aspx"\',' + MCSRefreshWait.toString() + ');', time);
	}
}

function closeWithMenuReload (time)
{
	if (time == 0)
	{
		RemoveIf ('ifMCS', 'ReloadMenuMCS();', MCSRefreshWait);
	}
	else
	{
		setTimeout ('RemoveIf("ifMCS", "ReloadMenuMCS();", ' + MCSRefreshWait.toString() + ');', time);
	}
	hideMRek();
}

function RemoveIf (ifName,comand2execute,comand2execute_timeout)
{
	if(!comand2execute_timeout)
	{
		comand2execute_timeout = 1;
	}
		
	if(comand2execute && comand2execute != '')
	{
		setTimeout(comand2execute,comand2execute_timeout);
	}
	CloseOpenedPopup ();
}