// JavaScript Document
function openAjax() {
	var ajax;
	try{
		ajax = new XMLHttpRequest(); // Firefox, Safari, dentre outros.
	}catch(ee){
		try{
			ajax = new ActiveXObject("Msxml2.XMLHTTP"); //IE da MS
		}catch(e){
			try{
				ajax = new ActiveXObject("Microsoft.XMLHTTP"); // IE da MS
			}catch(E){
				ajax = false;
			}
		}
	}
	return ajax;
}

function campoInvalido(campo,obrigatorio){
	if (campo.value == '' && obrigatorio){
		alert('O campo \''+campo.title+'\' deve ser preenchido!');
		campo.focus();
		return true;
	} else if (campo.value.indexOf("\\") != -1 || campo.value.indexOf("'") != -1 || campo.value.indexOf('"') != -1){
		alert('O campo \''+campo.title+'\' contém caractere(s) inválido(s)!');
		campo.select();
		return true;	
	}
	return false;
}

//funcao que previne enviar um formulario com um email invalido
function emailInvalido(campo){
	if (campo.value != ""){
		var reEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
		if(!reEmail.test(campo.value)) {
			alert('Por favor preencha seu e-mail corretamente.');
			campo.select();
			return true;
		}
		return false;
	}
	return false;	
}

function limpar(campo, padrao, proc){
	//proc = 1: onfocus; proc = 2: onblur
	if (campo.value == padrao && proc == 1)
		campo.value = "";
	if (campo.value == "" && proc == 2)
		campo.value = padrao;
}

function campoIgualAPadrao(campo, padrao){
	if (campo.value == padrao){
		alert('Preencha o campo \'busca\' corretamente!');
		campo.focus();
		campo.value = padrao;
		campo.select();
		return true;	
	}
	return false;
}

function validaBusca(f, padrao){
	if (campoInvalido(f.busca, true) || campoIgualAPadrao(f.busca,padrao))
		return false;
	return true;
}

function validaContato(f){
	if (campoInvalido(f.nome,true) || campoInvalido(f.email,true) || emailInvalido(f.email) || campoInvalido(f.cod,true) || campoInvalido(f.fone,true) || campoInvalido(f.cidade,true) || campoInvalido(f.mensagem,true) )
		return false;
	return true;
}

function validaEnviar(f){
	if (campoInvalido(f.nome,true) || campoInvalido(f.email,true) || emailInvalido(f.email) || campoInvalido(f.nome_amigo,true) || campoInvalido(f.email_amigo,true) || emailInvalido(f.email_amigo))
		return false;
	return true;
}
function validaComentario(f){
	if (campoInvalido(f.nome,true) || campoInvalido(f.email,true) || emailInvalido(f.email) || campoInvalido(f.comentario,true))
		return false;
	return true;
}

function tamFonte(id, maismenos){
	var conteudo = document.getElementById(id);
	if (conteudo.style.fontSize == "")
		conteudo.style.fontSize = "14px";
	if (conteudo.style.lineHeight == "")
		conteudo.style.lineHeight = "18px";

	tamanho = conteudo.style.fontSize.substring(0,conteudo.style.fontSize.length-2);
	font = parseInt(tamanho);
	
	altura = conteudo.style.lineHeight.substring(0,conteudo.style.lineHeight.length-2);
	line = parseInt(altura);

	if (maismenos == 1) {
		if (font != 10) {
			conteudo.style.fontSize = (font - 2) + "px";
			conteudo.style.lineHeight = (line - 2) + "px";
		}
	} else {
		if (font != 20) {
			conteudo.style.fontSize = (font + 2) + "px";
			conteudo.style.lineHeight = (line + 2) + "px";
		}
	}
}

function max(txarea) { 
	total = total_caracteres_comentario; 
	tam = txarea.value.length; 
	str=0; 
	str=str+tam;  
	document.getElementById('Restante').innerHTML = total - str; 
	if (tam > total){ 
		aux = txarea.value; 
		txarea.value = aux.substring(0,total); 
		document.getElementById('Restante').innerHTML = 0;
	} 
}
