// JavaScript Document

favoritos = function (elem, foto, tabela) {
	var caminho =  elem.src.substr(0, elem.src.lastIndexOf("/") + 1);
	var imgs = document.getElementsByTagName("img");
		
	for (x = 0; x < imgs.length - 1; x++) {
		try {
			if (imgs[x].src.indexOf("ok.gif") >= 0)
				imgs[x].src = caminho + "nk.gif";
		}
		catch (e) { }			
	}
	
	var ref = foto.substr(0, foto.indexOf("_")).replace("imovel", "");
	
	elem.src = caminho + "ok.gif";
	
	resposta = acheiembertioga.AjaxService.fotoFavorita(ref, foto, tabela);
}

legendaFoto = function (arquivo, foto, legenda) {
	resposta = vidamelhor.AjaxService.alterarLegendaFotos(arquivo, foto, legenda);
}


var estado = {
	qualId: function(obj) {
		$find('cidadeAutoComplete').set_contextKey(obj.value);
		
		$get("txtCidade").value = '';
		$get("hidCidade").value = 0;
		$get("txtBairro").value = '';
		$get("hidBairro").value = 0;
	}
}

var cidade = {
	qualId: function(obj) {
		acheiembertioga.AjaxService.qualIdCidade(obj.value, $get("cboEstado").value,
			cidade.sucesso, cidade.falha);
	},
	
	sucesso: function(arg) {
		$get("txtBairro").value = '';
		$get("hidBairro").value = 0;

		$get('hidCidade').value = arg;
		$find('bairroAutoComplete').set_contextKey(arg);
		
		$get('txtBairro').select();
	},
	
	falha: function(erro) {
	}
}

var bairro = {
	qualId: function(obj) {
		acheiembertioga.AjaxService.qualIdBairro(obj.value, $get("hidCidade").value,
			bairro.sucesso, bairro.falha);
	},
	
	sucesso: function(arg) {
		$get('hidBairro').value = arg;
	},
	
	falha: function(erro) {
	}
}


pesquisaMetodo = function(obj) {
	var x = 0;
	
	while(true) {
		try {
			var elem =	$get(obj.id + '_' + x);
			
			if (elem.checked) {
				var txtProcurar = $get('txtProcurar');
				
				txtProcurar.value = '';
				
				if (elem.value == 'cpfcnpj') {
					txtProcurar.setAttribute('onkeydown', 'return soNumeros(event);');
					txtProcurar.setAttribute('onkeyup', 'mascaraCPFCNPJ(event, this);');
					txtProcurar.setAttribute('maxlength', "18");
					
				} else {
					txtProcurar.removeAttribute('onkeydown');
					txtProcurar.removeAttribute('onkeyup');
					txtProcurar.removeAttribute('maxlength');
				}
				
				$find('procurarAutoComplete').set_contextKey(elem.value);
				break;
			}
		} catch(e) {
			break;
		}
		
		x++;
	}
}

var contato = {
	iniciar: function() {
		var objBody = document.getElementsByTagName("body").item(0);
		var div = document.createElement("div");
			
		div.setAttribute('id','divfundo');		

		with (div.style) {
			height = getPageSize()[1] + "px"; //"100%";
			
			if (navigator.appName.indexOf("MSIE 6") >= 0 )
				height = "100%";
			//else
			//	height = document.documentElement.scrollHeight + "px";
				
			width = "100%";
			top = "0px";
			position = "absolute";
			backgroundColor = "#000";
			filter = "alpha(opacity=50)";
			opacity = ".5";
			zIndex = 0;
			visibility = "hidden";
		}

		objBody.appendChild(div);
	},
	
	enviar: function(id, empresa) {
		contato.iniciar();

		var divFoco = $get("divContato");

		with (divFoco.style) {
			zIndex = 1;
			left = ((getPageSize()[0] - divFoco.clientWidth) / 2) + "px";
			top = ((document.all) ? document.documentElement.scrollTop : window.pageYOffset) +
				((getPageSize()[3] - divFoco.clientHeight) / 2) + "px";
			visibility = "visible";
		}

		$get("hidContato").value = id;
		$get("divfundo").style.visibility = "visible";
		$get("divEmpresa").innerHTML = empresa;
	},
	
	fim: function() {
		for (x=0; x < Page_Validators.length; x++) {
			Page_Validators[x].style.display = "none";
		}

		try {
			$get("txtNome").value = '';
			$get("txtEmail").value = '';
			$get("txtMsg").value = '';
		} catch(e) {}
		
		$get("divContato").style.visibility = "hidden";
		$get("divfundo").style.visibility = "hidden";
	}
}

var indique = {
	iniciar: function() {
		var objBody = document.getElementsByTagName("body").item(0);
		var div = document.createElement("div");
			
		div.setAttribute('id','divfundo');		

		with (div.style) {
			height = getPageSize()[1] + "px"; //"100%";
			
			if (navigator.appName.indexOf("MSIE 6") >= 0 )
				height = "100%";
			//else
			//	height = document.documentElement.scrollHeight + "px";
				
			width = "100%";
			top = "0px";
			position = "absolute";
			backgroundColor = "#000";
			filter = "alpha(opacity=50)";
			opacity = ".5";
			zIndex = 0;
			visibility = "hidden";
		}

		objBody.appendChild(div);
	},
	
	enviar: function() {
		indique.iniciar();

		var divIndique = $get("divIndique");
		
		with (divIndique.style) {
			zIndex = 1;
			left = ((getPageSize()[0] - divIndique.clientWidth) / 2) + "px";
			top = ((document.all) ? document.documentElement.scrollTop : window.pageYOffset) +
				((getPageSize()[3] - divIndique.clientHeight) / 2) + "px";
			visibility = "visible";
		}
		
		$get("divfundo").style.visibility = "visible";
	},
	
	fim: function() {
		for (x=0; x < Page_Validators.length; x++) {
			Page_Validators[x].style.display = "none";
		}
	
		$get("divIndique").style.visibility = "hidden";
		$get("divfundo").style.visibility = "hidden";
	}
}

var news = {
	enviar: function() {
		var txtNome = $get("ctl06_txtNome");
		var txtEmail = $get("ctl06_txtEmail");
		
		
		if (txtNome.value == "" || txtEmail.value == "") {
			alert("Você precisa preencher seu nome e e-mail!");
			return false;
		}
		
		if (!validarEmail(txtEmail.value)) {
			alert("E-mail inválido!\nVerifique se o digitou corretamente!");
			return false;
		}
	
		acheiembertioga.AjaxService.cadastrarNewsletter(txtNome.value, txtEmail.value,
														news.sucesso, news.erro);
		
		return false;
	},
	
	sucesso: function(arg) {
		$get("ctl06_txtNome").value = "";
		$get("ctl06_txtEmail").value = "";

		alert("Sua solicitação foi enviada com sucesso!");
	},
	
	erro: function(erro) {
		alert("Ocorreu um erro na sua solicitação!\nAguarde alguns instantes e tente novamente.");
	}
}

var contatoEmpresa = {
	iniciar: function() {
		var objBody = document.getElementsByTagName("body").item(0);
		var div = document.createElement("div");
			
		div.setAttribute('id','divfundo');		

		with (div.style) {
			height = getPageSize()[1] + "px"; //"100%";
			
			if (navigator.appName.indexOf("MSIE 6") >= 0 )
				height = "100%";
			//else
			//	height = document.documentElement.scrollHeight + "px";
				
			width = "100%";
			top = "0px";
			position = "absolute";
			backgroundColor = "#000";
			filter = "alpha(opacity=50)";
			opacity = ".5";
			zIndex = 0;
			visibility = "hidden";
		}

		objBody.appendChild(div);
	},
	
	enviar: function() {
		contato.iniciar();

		var divFoco = $get("divContatoEmp");

		with (divFoco.style) {
			zIndex = 1;
			left = ((getPageSize()[0] - divFoco.clientWidth) / 2) + "px";
			top = ((document.all) ? document.documentElement.scrollTop : window.pageYOffset) +
				((getPageSize()[3] - divFoco.clientHeight) / 2) + "px";
			visibility = "visible";
		}

		$get("divfundo").style.visibility = "visible";
	},
	
	fim: function() {
		for (x=0; x < Page_Validators.length; x++) {
			Page_Validators[x].style.display = "none";
		}

		$get("divContatoEmp").style.visibility = "hidden";
		$get("divfundo").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;
}

