//Retorna o objeto
function get(id) { 
	try { 
		var a = document.getElementById(id); 
	} catch(e) { 
		alert("Erro encontrado: "+e); 		
	} 
	return 	a;
}
function abreAtendimento()
{
  var width = 450;
  var height = 465;
  var left = (screen.width - width)/2;
  var top = (screen.height - height)/2;;
  window.open('../atd/view/?action=AppUsr/login','atendimento', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=no, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no,maximized=no,titlebar=no, fullscreen=no');
}
function addFocus (obj) {
	get(obj).focus();
}

//Limpa/Preenche os campos
function valueField (event, obj) {
	e = event.type;
	var obj = get(obj);
	
	if(!obj.save)
	{
		obj.save = obj.value;		
	}
	
	if(e=="blur")
	{
		if(obj.value=="")
		{
			obj.value = obj.save;
		}
	}
	else if(e=="focus")
	{
		if(obj.value=="")
		{
			obj.value = obj.save;
		}
		else if(obj.value==obj.save)
		{
			obj.value = "";
		}
	}
}

function addFlash (file, width, height, id, wmode, bgcolor) {
	if(!wmode)
		wmode = false;

	var fo = new FlashObject(file, "notify", width, height, "9", bgcolor, true);
	fo.addParam("scale", "noscale");
	fo.addParam("wmode", "opaque");
	fo.useExpressInstall('expressinstall.swf');
	
	if(wmode=='true')
		fo.addParam("wmode", "transparent");
		
	fo.write(id);
}

function show (id)
{
	get(id).style.display = 'block';
}

function hide (id)
{
	get(id).style.display = 'none';
}
function abreGaleria (dir, id, atual)
{
	   var width = 550;
	   var height = 550;
	   var left = (screen.width - width)/2;
	   var top = (screen.height - height)/2;;   
	   var galeria = window.open('galeria.php?dir='+dir+'&id='+id+'&atual='+atual,'geleria', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
	   galeria.focus();
}

function abrePop(src) {
   var width = 782;
   var height = 550;
   var left = (screen.width - width)/2;
   var top = (screen.height - height)/2;;  
   var janela = "";
   var src = src;
   var jan = window.open(src,janela, 'width='+width+', height='+height+', top='+top+', left='+left+', status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
   
   if(jan == false) {
   		alert("Desabilite seu bloqueador de popups");
   }
}

function changeImage(obj) {
	var parts = obj.src.split('/');
	var nome  = parts[parts.length-1].split('.');

	var nomeOver = nome[0].replace('_over', '');
	
	if(nome[0] == nomeOver) {
		obj.src = 'imgs/logos/'+nome[0]+'_over.'+nome[1];
	} else {
		obj.src = 'imgs/logos/'+nomeOver+'.'+nome[1];
	}
	
}

var campos = Array("nome","cidade","estado","telefone","email","msg");
var nomes = Array("Nome","Cidade","Estado","Telefone","E-mail","Mensagem");
var expres = Array("o","a","o","o","o","a");

function val(i) {
	obj = get(campos[i]);
	if(obj.value=="") {
		sendErro(obj, nomes[i]+" inválid"+expres[i]);
		return false;
	}
	return true;
}
var camposCond = Array("nome", "nomei", "endereco","bairro", "cidade", "email","telefone","msg");
var nomesCond = Array("Nome do Imóvel", "Seu Nome", "Endereço do Imóvel", "Bairro do Imóvel", "Cidade do Imóvel", "E-mail","Telefone","Mensagem");
var expresCond = Array("o","o","a","o","o","a");

function valCond(i) {
	obj = get(camposCond[i]);
	if(obj.value=="") {
		sendErro(obj, nomesCond[i]+" inválid"+expresCond[i]);
		return false;
	}
	return true;
}

function sendErro(obj, msg, local)
{
	if(!local)
		local = 'erro_form';
	get(local).innerHTML = "<span style='color: red;'>"+msg+"</span>";
	obj.focus();
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
                    return true;
                }
    }else{
        return false;
        }
}

function validaContato () {
	for(i=0;i<campos.length;i++) {
		if(!val(i)) return false;
	}
	
	get('erro_form').innerHTML = "<span style='color: black;'>enviando mensagem...</span>";
	get('form_contato').submit();
	
}
function validaContatoCond () {
	for(i=0;i<campos.length;i++) {
		if(!valCond(i)) return false;
	}
	
	get('erro_form').innerHTML = "<span style='color: black;'>enviando mensagem...</span>";
	get('form_contato_cond').submit();
	
}
function cadastrese()
{
	var email = get('email_newsletter');
	if(checkMail(email.value))
	{
		return true;
	} else {
		email.focus();
		return false;
	}
}

function validaCadastro()
{
	if(get('nome').value=="" || get('nome').value.length < 2) {
		sendErro(get('nome'), 'Digite seu nome corretamente.');
		return false;
	}
	
	if(get('cidade').value=="") {
		sendErro(get('cidade'), 'Digite sua cidade.');
		return false;
	}
	
	if(get('estado').value=="") {
		sendErro(get('estado'), 'Digite seu estado.');
		return false;
	}
	
	if(!checkMail(get('email').value)) {
		sendErro(get('email'), 'Digite seu e-mail corretamente.');
		return false;
	}
	
	if(get('senha').value=="" || get('senha').value.length < 2) {
		sendErro(get('senha'), 'Digite sua senha corretamente.');
		return false;
	}
	
	if(get('csenha').value=="" || get('csenha').value.length < 2) {
		sendErro(get('csenha'), 'Digite a confirmação da senha.');
		return false;
	}
	
	if(get('senha').value != get('csenha').value)
	{
		sendErro(get('csenha'), 'As senhas não confirmam.');
		return false;
	}
	
	get('erro_form').innerHTML = 'Enviando cadastro...';
	get('form_cadastro').submit();
}

function validaExcluir()
{
	if(!checkMail(get('e_email').value)) {
		sendErro(get('e_email'), 'Digite seu e-mail corretamente.', 'erro_form_e');
		return false;
	}
	
	if(get('e_senha').value=="" || get('e_senha').value.length < 2) {
		sendErro(get('e_senha'), 'Digite sua senha corretamente.', 'erro_form_e');
		return false;
	}
	
	get('erro_form_e').innerHTML = 'excluindo cadastro...';
	get('form_excluir').submit();
}

var abas  = new Array();
var menus = new Array();

function setaAba(aba, obj)
{
	obj.push(aba);
	
	get(aba).onclick = function ()
	{
		for(i = 0; i < obj.length; i++)
		{
			get('div_'+obj[i]).style.display = 'none';
			get(obj[i]).style.backgroundColor = 'white';
			get(obj[i]).style.color = '#4dafc8';

		}
		get('div_'+aba).style.display = 'block';
		get(aba).style.backgroundColor = '#4dafc8';
		get(aba).style.color = 'white';

	}
}

function montaAbas()
{
	setaAba('aba1', abas);
	setaAba('aba2', abas);
	
	get('aba1').style.backgroundColor = '#4dafc8';
	get('aba1').style.color = 'white';
}


function loadPage (pg, local, method)
{
	if(!method)
		method = 'GET';
		
	var url = pg.split('?');
	
	var data = new Object();
	
	if(url[1])
	{
		var params = url[1].split('&');
		for(i=0; i < params.length; i++)
		{
			var kv = params[i].split('=');
			data[kv[0]] = kv[1];
		}
	}	

	var html = $.ajax({
		url: url[0],
	    type: 'POST',	   
		data: data,
		success: function(html){
			$("#" + local).html(html);
		}		
	});
	
}

function getFotos(id, pg) {
	loadPage('lista_fotos.php?id='+id+'&pg='+pg, 'fotos', 'POST');
}


 function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function abreImagem(obj, img)
{
	get('carregador').style.display = 'block';
	get('carregador').style.left = findPosX(obj) + 8 +'px';
	get('carregador').style.top  = findPosY(obj) + 22 + 'px';

	var obj = get('imgGaleria');
	obj.src = img;

	obj.onload = function () {
		get('carregador').style.display = 'none';
	}
}

function ampliaFoto(obj, dir, id)
{
	var atual = obj.src;
	var parts = atual.split('/');

	abreGaleria(dir, id, parts[parts.length-1]);
}
function mudaPesquisa(obj)
{
	if(obj.value == 'locacao')
	{
		window.open('http://visitacaovirtual.suprisoft.com.br/QuickSearchPDA.aspx?sigla=PDA&wmodo=L', 'busca_rapida');
	} else {
		window.open('http://visitacaovirtual.suprisoft.com.br/QuickSearchPDA.aspx?sigla=PDA&wmodo=V', 'busca_rapida');	
	}
}
$(document).ready(function(){
	jQuery(function($){
	   $("#telefone").mask("(99) 9999-9999");
	});
});
