// Funcao de validacao de e-mail
function isEmail(email){
	var s = new String(email);
	// { } ( ) < > [ ] | \ /
	if ((s.indexOf("{")>=0) || (s.indexOf("}")>=0) || (s.indexOf("(")>=0) || (s.indexOf(")")>=0) || (s.indexOf("<")>=0) || (s.indexOf(">")>=0) || (s.indexOf("[")>=0) || (s.indexOf("]")>=0) || (s.indexOf("|")>=0) || (s.indexOf("\"")>=0) || (s.indexOf("/")>=0) )
		return false;
	// & * $ % ? ! ^ ~ ` ' "
	if ((s.indexOf("&")>=0) || (s.indexOf("*")>=0) || (s.indexOf("$")>=0) || (s.indexOf("%")>=0) || (s.indexOf("?")>=0) || (s.indexOf("!")>=0) || (s.indexOf("^")>=0) || (s.indexOf("~")>=0) || (s.indexOf("`")>=0) || (s.indexOf("'")>=0) )
		return false;
	// , ; : = #
	if ((s.indexOf(",")>=0) || (s.indexOf(";")>=0) || (s.indexOf(":")>=0) || (s.indexOf("=")>=0) || (s.indexOf("#")>=0) )
		return false;
	// procura se existe apenas um @
	if ( (s.indexOf("@") < 0) || (s.indexOf("@") != s.lastIndexOf("@")) )
		return false;
	// verifica se tem pelo menos um ponto após o @
	if (s.lastIndexOf(".") < s.indexOf("@"))
		return false;
	return true;
}
// Funcao de validacao de campo vazio
function isEmpty(text){
	var enter1 = "\n",
		enter2 = "\r",
		espaco = " ",
		tab = "\t";
	if (text =="") return true;
	
	//A verifica se o caracter selecionado possui valor valido
	for (var indice = 0; indice < text.length; indice++){
		if (text.charAt(indice) != espaco && 
			text.charAt(indice) != tab &&
			text.charAt(indice) != enter1 && 
			text.charAt(indice) != enter2 )
		return false;
	}
	return true;
}
// Funcao de validacao do formulario de orcamento
function validaForm() {
	if (isEmpty($('frm_nome').value)) {
		alert('Por favor, preencha o campo nome.');
		$('frm_nome').focus();
		return false;
	}
	if (isEmpty($('frm_endereco').value)) {
		alert('Por favor, preencha o campo endereço.');		
		$('frm_endereco').focus();
		return false;
	}
	if (isEmpty($('frm_numero').value)) {
		alert('Por favor, preencha o campo número.');		
		$('frm_numero').focus();
		return false;
	}
	if (isEmpty($('frm_bairro').value)) {
		alert('Por favor, preencha o campo bairro.');
		$('frm_bairro').focus();
		return false;
	}
	if (isEmpty($('frm_cep').value)) {
		alert('Por favor, preencha o campo cep.');
		$('frm_cep').focus();
		return false;
	}	
	if (isEmpty($('frm_cidade').value)) {
		alert('Por favor, preencha o campo cidade.');		
		$('frm_cidade').focus();
		return false;
	}
	if (isEmpty($('frm_telefone').value)) {
		alert('Por favor, preencha o campo telefone.');		
		$('frm_telefone').focus();
		return false;
	}
	if (!isEmail($('frm_email').value)) {
		alert('Por favor, preencha o campo e-mail.');		
		$('frm_email').focus();
		return false;
	}
	if (!isEmpty($('frm_portao_modelo').value)) {
		if (isEmpty($('frm_portao_medidas_altura').value)) {
			alert('Por favor, preencha o campo altura.');			
			$('frm_portao_medidas_altura').focus();
			return false;
		}
		if (isEmpty($('frm_portao_medidas_largura').value)) {
			alert('Por favor, preencha o campo largura.');			
			$('frm_portao_medidas_largura').focus();
			return false;
		}	
	}
	ajaxEnviaOrcamento();
}