// JavaScript Document
var ultimo = new Array(2);
ultimo[0] = -1;

var ultimoCBO = "";

/*with (document) {
	oncontextmenu = function () { return false; }
	onselectstart = function () { return false; }
}*/


/*****************************************************************************************************
*****							   DETALHES E BUCA POR REFERÊNCIA								 *****
*****************************************************************************************************/
function cboCidade(obj)
{	if (ultimoCBO != "")
	{	var elem = ultimoCBO;
		elem.style.position = "absolute";
		elem.style.visibility = "hidden"; }
		
	if (obj.value == "") return;
	
	var elem = document.getElementById("div" + obj.value);
	elem.style.position = "";
	elem.style.visibility = "visible";
	ultimoCBO = elem;
}

maisDetalhes = function(obj) {
	var valor = (typeof(obj) == "object") ? obj.value : obj;

	if (valor == "") {
		alert("O campo busca por referência precisa de pelo menos uma referência");
		obj.select();
		return false;
	}
	
	self.location = (valor.indexOf(",") < 0) ? "/imoveis/" + valor : "/resultado/?ref=" + URLEncode(valor);

	return false;
}





/*****************************************************************************************************
*****											   MÁSCARAS										 *****
*****************************************************************************************************/
function mascaraReal(campo)
{	ie =  document.all ? true : false;

	if (ie)
		FormataValorIE(campo);
      else
		FormataValorNS(campo);
}

function FormataValorNS(campo)
{	campo.value = FiltraCampo(campo);
	
	vr = campo.value;
			
	if (vr.length == 0)
	{	campo.value = "0,00";	}
	
	while (vr.substr(0, 1) == 0)
	{	if (vr == "0,00")
			break;
		vr = vr.substr(1, campo.value.length - 1);	}
	
	campo.value = vr;
	tam = vr.length;

	if ( tam <= 2 ) 
		campo.value = tam == 1 ? "0,0" + vr.substr(tam - 1, 1) : "0," + vr.substr(tam - 2, 2); 
 	if ( (tam > 2) && (tam <= 5) ) 
 		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; 
 	if ( (tam >= 6) && (tam <= 8) ) 
 		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
 	if ( (tam >= 9) && (tam <= 11) ) 
 		campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
 	if ( (tam >= 12) && (tam <= 14) ) 
 		campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
 	if ( (tam >= 15) && (tam <= 17) )
 		campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;
}

function FormataValorIE(campo)
{	var tecla = window.event.keyCode;

	if (tecla < 48 || tecla > 57 && tecla < 96 || tecla > 105)
		if (tecla != 8 && tecla != 46)
			return false;
	vr = campo.value;
			
	if (vr == "")
	{	campo.value = "0,00";
		vr = campo.value;	}
	
	while (vr.substr(0, 1) == 0)
	{	if (vr == "0,00" || vr == "")
			break;

	vr = vr.substr(1, campo.value.length - 1);	}
	
	campo.value = vr;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if ( tam <= 2 )
	{	campo.value = tam == 1 ? "0,0" + vr.substr(tam - 1, 1) : "0," + vr.substr(tam - 2, 2); }
	if ( (tam > 2) && (tam <= 5) )
	{	campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }
	if ( (tam >= 6) && (tam <= 8) )
	{	campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
	if ( (tam >= 9) && (tam <= 11) )
	{	campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	if ( (tam >= 12) && (tam <= 14) )
	{	campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
	if ( (tam >= 15) && (tam <= 17) )
	{	campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );	}
}

//Formatação de M
function mascaraKm(campo)
{	var tecla = window.event.keyCode;

	if (tecla < 48 || tecla > 57 && tecla < 96 || tecla > 105)
		if (tecla != 8 && tecla != 46)
			return false;
	vr = campo.value;
			
	campo.value = vr;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if ( (tam > 3) && (tam <= 6) )
	{	campo.value = vr.substr( 0, tam - 3 ) + '.' + vr.substr( tam - 3, tam ); }
	if ( (tam >= 7) && (tam <= 10) )
	{	campo.value = vr.substr( 0, tam - 6 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, tam ); }
}

function mascaraData(t)
{	var tecla = window.event.keyCode;

	if (tecla < 48 || tecla > 57 && tecla < 96 || tecla > 105)
			return false;
	
	tam = t.value.length;
	
	if (tecla != 8 && tecla != 46)
	{	if (tam == 2)
			t.value = t.value + '/';
		if (tam == 5 )
			t.value = t.value + '/';
	}
}

function mascaraFone(t)
{	var tecla = window.event.keyCode;

	if (tecla < 48 || tecla > 57 && tecla < 96 || tecla > 105)
			return false;
	
	tam = t.value.length;
	
	if (tecla != 8 && tecla != 46)
	{	if (tam == 1)
			t.value =  '(' + t.value;
		if (tam == 3 )
			t.value = t.value + ') ';
		if (tam == 9 )
			t.value = t.value + '-';
	}
}

function mascaraCEP(t)
{	var tecla = window.event.keyCode;

	if (tecla < 48 || (tecla > 57 && tecla < 96) || tecla > 105)
			return false;
			
	if (tecla != 8 && tecla != 46)
	{	if (t.value.length == 5)
			t.value = t.value + '-';
	}
}

function mascaraCPF(t)
{	var tecla = window.event.keyCode;

	if (tecla != 8 && tecla != 46)
	{	if (t.value.length == 3)
			t.value = t.value + '.';
		else if (t.value.length == 7)
			t.value = t.value + '.';
		else if (t.value.length == 11)
			t.value = t.value + '-';
	}
}


/*****************************************************************************************************
*****									VALIDAÇÕES E BLOQUEIOS									 *****
*****************************************************************************************************/
function checaCPF(source, arguments)
{	var elem = document.getElementById("txtcpf");
	
	if (validaCPF(elem.value))
	{	arguments.IsValid = true;	}
	else
	{	arguments.IsValid = false;	}
}

function validaCPF(CPF)
{	var charinvalidos = '-/,.() ';	

	for (var i=0; i<=6; i++)
	{	while (( cx = CPF.indexOf(charinvalidos.substring(i, i + 1) )) != -1)
		{	CPF = CPF.substring(0,cx) + CPF.substring(cx+1);	}
	}
	
	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999")
	{	return false;	}		
		
	soma = 0;
	
	for (i=0; i < 9; i ++)
	{	soma += parseInt(CPF.charAt(i)) * (10 - i);	}
	
	resto = 11 - (soma % 11);
	
	if (resto == 10 || resto == 11)
	{	resto = 0;	}
	
	if (resto != parseInt(CPF.charAt(9)))
	{	return false;	}
	
	soma = 0;
	
	for (i = 0; i < 10; i ++)
	{	soma += parseInt(CPF.charAt(i)) * (11 - i);	}
	
	resto = 11 - (soma % 11);
	
	if (resto == 10 || resto == 11)
	{	resto = 0;	}
	
	if (resto != parseInt(CPF.charAt(10)))
	{	return false;	}
	
	return true;

 }

function soNumeros()
{	var tecla = window.event.keyCode;

	if (tecla < 48 || tecla > 57)
	{	window.event.returnValue = false;	}
}


/*****************************************************************************************************
*****									CALENDÁRIO E VALIDAÇÃO									 *****
*****************************************************************************************************/
function calendario(campo)
{	var w = 300;
	var h = 230;
	var left = (screen.width - w) / 2;
	var top = (screen.height - h) / 2;
	
	var cdr = window.open("", "", "width=" + w + ", height=" + h + ", scrollbars=no, top=" + top + ", left=" + left);
	cdr.document.location.href = "calendario.aspx?campo=" + campo + "&data=" + document.getElementById(campo).value;
}

function checarDatas()
{	var dataInicial = document.getElementById("txtdatainicial").value;
	var dataFinal = document.getElementById("txtdatafinal").value;
	
	if (dataInicial != "" && dataFinal != "")
	{	if (parseInt(dataFinal.split("/")[2].toString() +
				 dataFinal.split("/")[1].toString() +
				 dataFinal.split("/")[0].toString()) <
			parseInt(dataInicial.split("/")[2].toString() +
				 dataInicial.split("/")[1].toString() +
				 dataInicial.split("/")[0].toString()))
		{	alert("Atenção!\nA data inicial é maior que a data final, por favor insira um período válido.");
			
			with (document.Form1)
			{	txtdatainicial.value = "";
				txtdatafinal.value = ""; }
		}
		else
		{	document.Form1.botadicionardata.disabled = false; }
	}
}


/*****************************************************************************************************
*****									  PACOTE E VALIDAÇÃO									 *****
*****************************************************************************************************/
function pacote()
{	with (document.Form1)
	{	if (txtnomepacote.value != "" &&
			txtprecopacote.value != "" &&
			txtprecopacote.value != "0,00" &&
			txtdiapacote.value != "" &&
			txtpessoapacote.value != "")
		{	document.Form1.botadicionapacote.disabled = false; }
	}
}

function mudarMoeda()
{	var obj = document.getElementsByTagName("span");
	var moeda = "";
	var sender = document.getElementById("cbomoeda");

	switch (sender.value)
	{	case "real" :
			moeda = "R$";
			break;
		case "dolar" :
			moeda = "U$";
			break;
	}

	for (x = 0; x <= obj.length-1; x++)
	{	if (obj[x].id == "moeda")
		{	obj[x].innerText = moeda; }
	}
}


/*****************************************************************************************************
*****									  PACOTE E VALIDAÇÃO									 *****
*****************************************************************************************************/
function checarPlanta(botao)
{	var elem = document.getElementsByName("botadicionaplanta");

	if (document.getElementById("txtplantanome").value != "" && document.getElementById("txtplantamedida").value != "")
	{	elem[0].disabled = false; }
	else
	{	elem[0].disabled = true; }
}


/*****************************************************************************************************
*****									 		 FOTOS											 *****
*****************************************************************************************************/
function verFoto(foto, w, h)
{	//var w = 500;
	//var h = 375;
	var pic = window.open('','','width=' + w + ',height=' + h);
	var fotoStr = '';

	if (foto.src.indexOf("aspx") > 0)
	{	var inicio = foto.src.indexOf("=") + 1;
		var fim = foto.src.indexOf("&");
		foto = foto.src.substr(inicio, fim - inicio)
		fotoStr = '<img src="../thumb.aspx?foto=' + foto + '&w=' + w + '&h=' + h + '" border="0" alt="Fechar Janela">';
	}
	else
	{	fotoStr = '<img src="' + foto.src.replace('mini/','') + '" border="0" alt="Fechar Janela">';	}
	
	pic.document.write('<html><body topmargin=0 leftmargin=0>');
	pic.document.write('<a href="javascript:window.close()">');
	pic.document.write(fotoStr);
	pic.document.write('</a></body></html>');
}

function initFotos()
{	var checks = document.getElementsByTagName("input")

	for (x = 0; x <= checks.length - 1; x++)
	{	with (checks[x])
		{	//var tab = document.getElementById("datalistfotos__ctl" + x + "_tabela" + x)

			try
			{	if (checks[x].id.indexOf("chkfoto") >= 0)
				{	var y = checks[x].id.substring(checks[x].id.indexOf("ctl")+3, checks[x].id.lastIndexOf("_"))
					var tab = document.getElementById("datalistfotos__ctl" + y + "_tabfoto");

					if (checks[x].checked)
					{	tab.className = tab.className + "-sele"; }
					else
					{	tab.className = "tabela-foto"; }
				}
			}
			catch (e) { }
			
		}
	}
}


function validarExtensao(obj)
{	var retorno = 0;
	var arrImagens = new Array(1);
	arrImagens[0] = '.JPG'; //aki vc coloca as extensões que o arquivo pode aceitar
	//arrImagens[1] = '.GIF'; //aki vc coloca as extensões que o arquivo pode aceitar

	var extensao = null;
      
	if (obj.value == '')	//document.forms[0].elements[campo].value == '')
	{	return false; }
	else
	{	extensao = obj.value.substring(obj.value.lastIndexOf(".")).toUpperCase();
		//extensao = document.forms[0].elements[campo].value.substring(document.forms[0].elements[campo].value.lastIndexOf(".")).toUpperCase();
	
		for(i = 0; i < arrImagens.length; i++)
		{	if (extensao == arrImagens)
				retorno += 1;
		}     
                                 
  		if (retorno == 0)
		{	alert("Utilize imagens com extensão .JPG");
			obj.select();
			return false; }
		else
		{	return true; }
	}     
}


/*****************************************************************************************************
*****											DIVERSOS										 *****
*****************************************************************************************************/
function verCookie(labelName)
{	var labelLen = labelName.length

	// read cookie property only once for speed
	var cookieData = document.cookie
	var cLen = cookieData.length
	var i = 0
	var cEnd
	
	while (i < cLen)
	{	var j = i + labelLen
	
		if (cookieData.substring(i,j) == labelName)
		{	cEnd = cookieData.indexOf(";",j)
			
			if (cEnd == -1)
			{	cEnd = cookieData.length	}
			
			return unescape(cookieData.substring(j + 1, cEnd))
		}
		
		i++
	}
	
	return ""
}

function verMapa()
{	var pic = window.open('','','width=511, height=500');
		
	pic.document.write('<html><body topmargin=0 leftmargin=0 oncontextmenu="return false" ondragstart="return false">');
	pic.document.write('<a href="javascript:window.close()">');
	pic.document.write('<img src="fotos/mapa.jpg" border="0" alt="Fechar Janela">');
	pic.document.write('</a></body></html>');
}


escondeMostra = function (objID) {
	var obj = document.getElementById(objID);
	var sombra = document.getElementById("sombra_" + objID);

	if (obj.style.visibility == "hidden" || obj.style.visibility == "") {
		obj.style.visibility = "visible";
		
		try {
			sombra.style.visibility = "visible";
			sombra.style.width = obj.clientWidth + "px";
			sombra.style.height = obj.clientHeight + "px";
		} catch(e) {alert(e)}
		
	} else {
		obj.style.visibility = "hidden";
		
		try {
			sombra.style.visibility = "hidden";
		} catch(e) {}
	}	
}

function URLEncode (clearString)
{	var output = '';
	var x = 0;

	clearString = clearString.toString();
	
	var regex = /(^[a-zA-Z0-9_.]*)/;
	
	while (x < clearString.length)
	{	var match = regex.exec(clearString.substr(x));
	
		if (match != null && match.length > 1 && match[1] != '')
		{	output += match[1];
			x += match[1].length; }
		else
		{	if (clearString[x] == ' ')
				output += '+';
			else
			{	var charCode = clearString.charCodeAt(x);
				var hexVal = charCode.toString(16);
				output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
			}
		
			x++;
		}
	}
	
	return output;
}

tipoImoveis = function (obj) {
	var hdtipo = document.getElementById("menu_hdtipo")
	hdtipo.value = obj.value;
	document.getElementById("divmaisinfo").style.visibility = "hidden";
}

var janela = null;
var zera = false;
escondeMostraInfo = function (objID, fora) {
	var operacao = document.getElementsByName("menu$rdooperacao")[0].checked;
	var tipo = parseInt(document.getElementById("menu_hdtipo").value);
	var obj = document.getElementById(objID);
	var rdovalor = document.getElementsByName("menu$rdovalor");
	var rdodorm = document.getElementsByName("menu$rdodorm");
	
	if (tipo != 4 && tipo != 1) {
		rdovalor[0].checked = true;
		rdodorm[0].checked = true;
		zera = true;
		return;
	}
	
	if (operacao) {
		if (zera) {
			rdovalor[1].checked = true;
			rdodorm[1].checked = true;
			zera = false;
		}
		
		obj.style.top = "-220px";
		obj.style.height = "";				
	} else {
		rdovalor[0].checked = true;
		zera = true;
		
		obj.style.top = "-70px";
		obj.style.height = "150px";
		obj.style.overflow = "hidden";
	}
	
	if (!janela && fora) {
		janela = setInterval("escondeMostraInfo('" + objID + "',true)", 5000);
		return;
	} else {
		if (!janela && !fora) {
			obj.style.visibility = "visible";
			clearInterval(janela);
			janela = null;
		} else if (janela && fora) {
			obj.style.visibility = "hidden";
			clearInterval(janela);
			janela = null;
		}
	}
}

verFachada = function (foto) {
	var fch = window.open('','','width=300, height=225');

	with (fch.document) {
		write("<html><body style='margin:0px;'>");
		write("<a href='' onclick='window.close();'><img src='/thumb.aspx?src=" + foto + "' alt='clique para fechar' border='0'></a>");
		write("</body></html>");
	}
}


/****************************************/
/****************************************/

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


/* DIVERSOS ***********************************************************************************/
var exclusividade = {
	on: function() {
		var objBody = document.getElementsByTagName("body").item(0);
		var divca = document.getElementById("divexclusividade")
		var div = document.createElement("div");

		div.setAttribute('id','divfundo');		
	
		with (divca.style) {
			zIndex = 1;
			left = ((getPageSize()[0] - 500) / 2) + "px";
			top = ((getPageSize()[3] - 300) / 2) + "px";
			position = "absolute";
		}
		
		with (div.style) {
			height = getPageSize()[1] + "px";//"100%";
			width = "100%";
			top = "0px";
			position = "absolute";
			backgroundColor = "#fff";
			filter = "alpha(opacity=50)";
			opacity = ".50";
			zIndex = 0;
			visibility = "hidden";
		}
	
		objBody.appendChild(div);
		//var divca = document.getElementById("divcalendario")
		//var div = document.getElementById("divfundo");

		div.style.visibility = "visible";
		divca.style.visibility = "visible";
	},
	
	off: function () {
		var divca = document.getElementById("divexclusividade")
		var div = document.getElementById("divfundo");
		
		div.style.visibility = "hidden";
		//div.style.height = "1px;";
		divca.style.visibility = "hidden";
	}
}

getPageSize = function() {
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight) 
	
	return arrayPageSize;
}

vEmail = function(obj) {
	obj = document.getElementById(obj);

	if (obj.value == "") {
		alert('Preencha o e-mail antes de adicioná-lo!');
		return false;
	}

	if (!validarEmail(obj)) {
		alert('E-mail inválido!');
		return false;
	}
	
	return true;
}

vFone = function(ddd, fone) {
	var txtDDD	= document.getElementById(ddd);
	var txtFone	= document.getElementById(fone);
	
	if (txtDDD.value == "" || txtFone.value == "") {
		alert('Preencha os campos DDD e Telefone');
		return false;
	}
	
	return true;
}


