// JavaScript Document

function validaLogin() {
	var usuario  = document.getElementById('txtUsuario');
	var senha  = document.getElementById('txtPassword');
	
	if((usuario.value == '') || (senha.value == '')) {
		alert('Informe Usuário e/ou Senha');
		return false;
	} else {
		return true;	
	}
}

function validaMeusDados() {
	var nome = document.getElementById('txtNome');
	var email = document.getElementById('txtEmail');
	
	if(nome.value == '') {
		alert("Informe o nome");
		return false;
	}
	
	if(email.value == '') {
		alert('Informe o e-mail');
		return false;
	}
	
	if(!(validaEmail(email.value))) {
		alert('O e-mail é inválido');
		return false;
	}
	
	var senha1 = document.getElementById('txtNSenha');
	var senha2 = document.getElementById('txtNSenhaConfirma');
	
	if(senha1.value != '') {
		if(senha1.value != senha2.value) {
			alert('A senha informada não está igual a senha de conferência.');
			return false;
		}
	}
	
	return true;
}

function validaCadastroImobiliaria() {
	
	var nome = document.getElementById('txtNome');
	var usuario = document.getElementById('txtUsuario');
	var senha = document.getElementById('txtSenha');
	var numimoveis = document.getElementById('txtNumImoveis');
	
	if(nome.value == '') {
		alert('Informe o nome da imobiliária');	
		return false;
	}
	
	if(usuario.value == '') {
		alert('Informe um usuário para administrar a imobiliária');	
		return false;
	}
	
	if(senha.value == '') {
		alert('Informe uma senha');	
		return false;
	}
	
	if(numimoveis.value  == '') {
		alert('Informe o número máximo de imóveis.');	
		return false;
	}
	
	return true;	
}

function validaAlteracaoImobiliaria() {
	var nome = document.getElementById('txtNome');
	var numimoveis = document.getElementById('txtNumImoveis');
	if(nome.value == '') {
		alert('Informe o nome da imobiliária');	
		return false;
	}
	
	if(numimoveis.value  == '') {
		alert('Informe o número máximo de imóveis.');	
		return false;
	}
	
	return true;
}

function validaImagem(obj, div) {
	var foto = document.getElementById(obj);
	var divimagem = document.getElementById(div);
	
	nomeimagem = foto.value.substr(foto.value.lastIndexOf('\\')+1);
	extensao = foto.value.substr(foto.value.lastIndexOf('.')+1);
	
	if(extensao.toLowerCase() != "jpg") {
		alert("Utilize imagem do tipo JPG");
		divimagem.innerHTML = "Utilize imagem do tipo JPG" ;
		foto.value = "";
		return false;
	}
	
	if(nomeimagem.indexOf(" ") != -1) {
		alert("O nome da imagem contém espaço em branco!");
		divimagem.innerHTML = "O nome da imagem contém espaço em branco!" ;
		foto.value = "";
		return false;
	}
	var texto= new Array('ã','Ã','õ','Õ','ç','Ç','é','É','ú','Ú','ó','Ó','+','/','@','!','?','#','$','%','¨','&','*','(',')','}','{','[',']','ª','º',';',':','|',',','"');
	for(i=0;i<=texto.length;i++)
    {
         if(nomeimagem.indexOf(texto[i])!='-1')
                {
                    alert("O nome da imagem contém caracteres inválidos, utilize somente letras e números");
                    divimagem.innerHTML = "O nome da imagem contém caracteres inválidos, utilize somente letras e números";
					foto.value = "";
                    return (false);                
                }
    } 
	divimagem.innerHTML = "";
}

function validaCadastroImovelPasso1() {
	var nome = document.getElementById('titulo');
	var foto = document.getElementById('foto');
	var imagem = document.getElementById('bla');
	var valor = document.getElementById('vlr');
	var ccc = document.getElementById('ccc');
	
	// trata as informações sobre a foto
	if(foto.value != ''){
		
		imagem.innerHTML = "<img src='"+foto.value+"' />";
		
		return false;	
	}	
	
	if(nome.value == '') {
		alert('Informe um título para o imóvel');	
		return false;
	}

	if(valor.value == '') {
		if(ccc.value == '') {
			alert('Voce deve informar ou o valor, ou a quantia de CCC');	
			return false;
		}
	} else {
		var vlr = valor.value;
		vlr = vlr.replace(',','');
		vlr = vlr.replace('.','');
		if(isNaN(vlr)) {
			alert('Informe um valor válido.');
			return false;	
		}
	}
	
	campoccc = ccc.value;
	
	campoccc = campoccc.replace(',','');
	campoccc = campoccc.replace('.','');

	if(ccc.value != "") {
		if(isNaN(campoccc) == true) {
			alert('O campo CCC parece conter um valor incorreto, favor verificar.');
			return false;
		}
	
		if(campoccc == 0 || campoccc < 0) {
			alert('O campo CCC parece conter um valor incorreto, favor verificar.');
			return false;	
		}
	}
	return true;
}

function validaCadastroImovelPasso2() {
	var titulo = document.getElementById('titulocaracteristica');
	var descricao = document.getElementById('descricaocaracteristica');
	
	if(titulo.value == "" || descricao.value == "") {
		alert("Preencha todos os campos");
		return false;
	}
	
	return true;
}

function validaCadastroImagem() {
		var foto = document.getElementById('foto');
		if(foto.value == '') {
			alert('Selecione uma imagem!');
			return false;
		}
		return true;
}
function validaCadastroImagem2() {
		var foto = document.getElementById('foto2');
		if(foto.value == '') {
			alert('Selecione uma imagem!');
			return false;
		}
		return true;
}

function justificativa(mostra, esconde) {
	var mostra = document.getElementById(mostra);
	var esconde = document.getElementById(esconde);
	
	mostra.className = "mostra";
	esconde.className = "esconde";
	parent.document.getElementById("frameConteudo").height = document.getElementById("divConteudo").scrollHeight+100;

	
}

function avancaPasso(pagina, passo) {
	document.location = pagina+"?passo="+passo+"&idimovel="+document.getElementById('idimovel').value;
}

function validaEmail(email) {
	if(email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) {
   		return false;
  	} else {
		return true;	
	}
}

function FormataReais(fld, milSep, decSep, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13 || whichCode == 8) return true;
key = String.fromCharCode(whichCode);  // Valor para o código da Chave
if (strCheck.indexOf(key) == -1) return false;  // Chave inválida
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '';
if (len == 1) fld.value = '0'+ decSep + '0' + aux;
if (len == 2) fld.value = '0'+ decSep + aux;
if (len > 2) {
	aux2 = '';
	for (j = 0, i = len - 3; i >= 0; i--) {
		if (j == 3) {
		aux2 += milSep;
		j = 0;
		}
		aux2 += aux.charAt(i);
		j++;
	}
	fld.value = '';
	len2 = aux2.length;
	for (i = len2 - 1; i >= 0; i--)
		fld.value += aux2.charAt(i);
	fld.value += decSep + aux.substr(len - 2, len);
}
return false;
}

function formatar(src, mask){
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
if (texto.substring(0,1) != saida)
  {
    src.value += texto.substring(0,1);
  }
}
