// MUDA FUNDO GALERIA ==================================================================
function mudafundo(campo, acao){
	if(acao == 'entra'){
		document.getElementById(campo).style.border = 'solid 1px #EEEEEE';
		document.getElementById(campo).style.background = '#f9f9f9';
	} else {
		document.getElementById(campo).style.border = 'solid 1px #ffffff';
		document.getElementById(campo).style.background = '#FFFFFF';
	}
}
// POPUP ===============================================================================
function NewWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable, statusbar=1'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// ATUALIZA PRODUTOS ====================================================================
function atualiza_prod(id) {
	x_atualiza_prod(id, atualiza_prod_cb); 
} 
function atualiza_prod_cb(z) {
	document.getElementById('ert_produto').innerHTML = unescape(z);
}

// CADASTRO NO MAILING ==================================================================
function cadastra_news() { 
	var nome, email, categoria;
	 
	nome = document.getElementById("news_nome").value; 
	email = document.getElementById("news_mail").value; 
	
	categoria = "";
	if (document.getElementById("chk_1").checked == true) {
		if (categoria != "") categoria += "==";
		categoria += document.getElementById("chk_1").value;
	}
	if (document.getElementById("chk_2").checked == true) {
		if (categoria != "") categoria += "==";
		categoria += document.getElementById("chk_2").value;
	}
	if (document.getElementById("chk_3").checked == true) {
		if (categoria != "") categoria += "==";
		categoria += document.getElementById("chk_3").value;
	}
	
	x_cadastra_news(nome, email, categoria, cadastra_news_cb); 
} 
function cadastra_news_cb(z) {
	document.getElementById('news').innerHTML = unescape(z);
}

// VALIDAR E-MAILS =====================================================================
function testaemail(form, email, func) {
	var campo;
	
	campo = document.getElementById(email); 
	
	if(validaemail(campo.value)) {
		if (func) eval(func); else form.submit();
	} else {
		campo.select();
		alert("E-mail Inválido");
	}
}
function validaemail(email) {
	var objRegExp  = /^[A-Za-z0-9\.]{3}([\w\.]*)@([A-Za-z0-9\.]*)\.(([A-Za-z]{3}\.[A-Za-z]{2}$)|([A-Za-z]{3}$)|([a-z]{2}$))/i ;
	return objRegExp.test(email);
}

// RESULTADO DO ENDURO =================================================================
function resultado(tipo, id) {
	//document.getElementById('tipo').style.display = 'none';
	document.getElementById('div_resultado').style.display = '';
	window.scroll(0,200);
	x_resultado(tipo, id, resultado_cb); 
} 
function resultado_cb(z) {
	document.getElementById('div_resultado').innerHTML = unescape(z);
}

// MÁSCARAS PARA CAMPOS DO FORMULÁRIO ==================================================
function Mascara (formato, objeto, e) {
var tecla; 
if (e.keyCode) tecla = e.keyCode; 
	else
		if (e.which) tecla = e.which; 
			else return false;

	campo = eval (objeto);
	if (formato=='CEP') {
		caracteres = '01234567890';
		separacoes = 2;
		separacao2 = '-';
		conjuntos = 2;
		conjunto1 = 5;
		conjunto2 = 9;
		if ((caracteres.search(String.fromCharCode (tecla))!=-1) && campo.value.length < (conjunto2)) {
			if (campo.value.length == conjunto1) campo.value = campo.value + separacao2;
		}
		else e.returnValue = false;
	} 
	if (formato=='CPF') {
		caracteres = '01234567890';
		separacoes = 3;
		separacao1 = '.';
		separacao2 = '-';
		conjuntos = 4;
		conjunto1 = 3;
		conjunto2 = 7;
		conjunto3 = 11;
		conjunto4 = 14;
		if ((caracteres.search(String.fromCharCode (tecla))!=-1) && campo.value.length < (conjunto4)) {
			if (campo.value.length == conjunto1) campo.value = campo.value + separacao1;
			else if (campo.value.length == conjunto2) campo.value = campo.value + separacao1;
			else if (campo.value.length == conjunto3) campo.value = campo.value + separacao2;
		}
		else e.returnValue = false;
	} 
	if (formato=='DATA') {
		caracteres = '01234567890';
		separacoes = 2;
		separacao1 = '\/';
		conjuntos = 3;
		conjunto1 = 2;
		conjunto2 = 5;
		conjunto3 = 10;
		if ((caracteres.search(String.fromCharCode (tecla))!=-1) && campo.value.length < (conjunto3)) {
			if (campo.value.length == conjunto1 || campo.value.length == conjunto2) campo.value = campo.value + separacao1;
		}
		else e.returnValue = false;
	}
}

// VALIDAÇÃO DE CPF ====================================================================
function validaCPF(objeto) {
	campo = eval (objeto);
	cpf = campo.value;
	cpf = cpf.replace ('.','');
	cpf = cpf.replace ('.','');
	cpf = cpf.replace ('.','');
	cpf = cpf.replace (',','');
	cpf = cpf.replace (',','');
	cpf = cpf.replace (',','');
	cpf = cpf.replace ('-','');
	valor = true;
	erro = new String;
	if (cpf.length < 11) erro += 'Sao necessarios 11 digitos para verificacao do CPF! \n\n'; 
	var nonNumbers = /\D/;
	if (nonNumbers.test(cpf)) erro += 'A verificacao de CPF suporta apenas numeros! \n\n';	
	if (cpf == '00000000000' || cpf == '11111111111' || cpf == '22222222222' || cpf == '33333333333' || cpf == '44444444444' || cpf == '55555555555' || cpf == '66666666666' || cpf == '77777777777' || cpf == '88888888888' || cpf == '99999999999'){
		  erro += 'Numero de CPF invalido!';
	}
	var a = [];
	var b = new Number;
	var c = 11;
	for (i=0; i<11; i++){
		a[i] = cpf.charAt(i);
		if (i < 9) b += (a[i] *  --c);
	}
	if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
	b = 0;
	c = 11;
	for (y=0; y<10; y++) b += (a[y] *  c--); 
	if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
		erro +='Digito verificador com problema!';
	}
	if (erro.length > 0){
		alert(erro);
		return false;
	}
	return true;
}

// VERIFICA PREENCHIMENTO DOS CAMPOS DO FORMULÁRIO =====================================
function verifica(campo){
	i = 0;
	while(campo[i].name != 'Submit'){
		if (campo[i].id != '') {
		 	if (campo[i].value == '' || campo[i].value == '**OBRIGATÓRIO**') {
	      			alert('Campo '+campo[i].id+' é de preenchimento obrigatório!');
					if(campo[i].disabled == true){
						campo[i].disabled = false;
					}
					if(campo[i].name != 'estado' && campo[i].name != 'end_estado' && campo[i].name != 'info_busca'){
						campo[i].value = '**OBRIGATÓRIO**';
						campo[i].focus();
						campo[i].select();
	      				return false;
					} else {
						campo[i].focus();
	      				return false;
					}
					return false;
				} 
	   		i++;
		} else {
			i++;
		}
	}
}
