// Grava o texto do editor Html num campo do formulario
function editor(poCampo, pcEditor) {
	var loEditor = document.getElementById(pcEditor);
	if (loEditor.html_mode == false) {
		wp_send_to_html(loEditor)
	}
	poCampo.value = loEditor.html_edit_area.value;
}

// Muda a cor de fundo de um objeto
function MudaCor(poObj, pcCor) {
	poObj.style.background = pcCor;
}

// Carrega um conteudo dentro de uma camada via Ajax
function CarregaCamada(pcPagina, pcCamada) {
	var req = null;
	// Procura por um objeto nativo (Mozilla/Safari)
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", pcPagina, true);
		req.send(null);
	// Procura por uma versao ActiveX (IE)
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", pcPagina, true);
			req.send();
		}
	}
	
	function processReqChange() {
		if (req.readyState == 4) {
			if ((req.status == 200) && ((req.responseText) != "UNKNOWN")) {
				if (document.getElementById(pcCamada)) {
					document.getElementById(pcCamada).innerHTML = req.responseText;
				}
			} else {
				//alert("Houve um problema ao obter os dados:\n" + req.statusText);
			}
		}
	}
}

function ValidaLoginCliente(poForm) {
	if (poForm.txtLogin.value == "") {
		alert("Digite o nome de usuário.");
		poForm.txtLogin.focus();
		return false;
	}
	if (poForm.txtSenha.value == "") {
		alert("Digite a senha.");
		poForm.txtSenha.focus();
		return false;
	}
	poForm.txtOperacao.value = 2;
	window.open("", "acesso_cliente", "width=640,height=480,scrollbars=yes");
	poForm.submit();
	poForm.reset();
}

function ValidaNewsletter(poForm) {
	if ((poForm.txtNome.value == "") || (poForm.txtNome.value == "Nome")) {
		alert("Digite seu nome para se cadastrar.");
		poForm.txtNome.focus();
		return false;
	}
	if ((poForm.txtEmail.value == "") || (poForm.txtEmail.value == "E-mail")) {
		alert("Digite seu email para se cadastrar.");
		poForm.txtEmail.focus();
		return false;
	} else {
		if (ValidaEmail(poForm.txtEmail) == false) {
			return false;
		}
	}
	poForm.txtOperacao.value = 1;
	poForm.submit();
}

function ValidaIndique(poForm) {
	if ((poForm.txtNome1.value == "") || (poForm.txtNome1.value == "Nome")) {
		alert("Digite seu nome.");
		poForm.txtNome1.focus();
		return false;
	}
	if ((poForm.txtEmail1.value == "") || (poForm.txtEmail1.value == "E-mail")) {
		alert("Digite seu email.");
		poForm.txtEmail1.focus();
		return false;
	} else {
		if (ValidaEmail(poForm.txtEmail1) == false) {
			return false;
		}
	}
	if ((poForm.txtNome2.value == "") || (poForm.txtNome2.value == "Nome")) {
		alert("Digite o nome do seu amigo.");
		poForm.txtNome1.focus();
		return false;
	}
	if ((poForm.txtEmail2.value == "") || (poForm.txtEmail2.value == "E-mail")) {
		alert("Digite email do seu amigo.");
		poForm.txtEmail2.focus();
		return false;
	} else {
		if (ValidaEmail(poForm.txtEmail2) == false) {
			return false;
		}
	}
	poForm.txtOperacao.value = 1;
	poForm.submit();
}

// Verifica a quantidade de dias em fereveiro
function DiasInFevereiro (pnAno) {
    return (  ((pnAno % 4 == 0) && ( (!(pnAno % 100 == 0)) || (pnAno % 400 == 0) ) ) ? 29 : 28 );
}

// Validar Data
function ValidaData(field) {
	if (field.value == "") {
		return true;
	}
	var hoje = new Date();
	var anoAtual = hoje.getFullYear();
	var barras = field.value.split("/");
	if (barras.length == 3) {
		var dia = barras[0];
		var mes = barras[1];
		var ano = barras[2];
		var resultado = (!isNaN(dia) && (dia > 0) && (dia < 32)) && (!isNaN(mes) && (mes > 0) && (mes < 13)) && (!isNaN(ano) && (ano.length == 4));
		if (!resultado) {
			alert("Formato de data inválido. Ex.: (dd/mm/aaaa)");
			field.focus();
			field.select();
			return false;
		}
		if (ano.length != 4) {
			alert("O ano deve ter 4 digitos.");
			field.focus();
			field.select();
			return false;
		}
		//if (ano < anoAtual) {
		//	alert("O ano não pode ser menor que o ano atual.");
		//	field.focus();
		//	field.select();
		//	return false;
		//}
		dias = new Array(13);
		dias[1] = 31;
		dias[2] = DiasInFevereiro(ano);   // deve ser verificado o caso de anos bissextos
		dias[3] = 31;
		dias[4] = 30;
		dias[5] = 31;
		dias[6] = 30;
		dias[7] = 31;
		dias[8] = 31;
		dias[9] = 30;
		dias[10] = 31;
		dias[11] = 30;
		dias[12] = 31;
		if (dia > dias[mes]) {
			alert("Dia inválido.");
			field.focus();
			field.select();
			return false;
		}
	} else {
		alert("Formato de data inválido. Ex.: (dd/mm/aaaa)");
		field.focus();
		field.select();
		return false;
	}
}

// Valida hora e minuto
function ValidaHora(field) {
	if (field.value == "") {
		return true;
	}
	if (field.value.length != 5) {
		alert("Formato de hora inválido. Ex.: (hh:mm)");
		field.focus();
		field.select();
		return false;
	}
	var barras = field.value.split(":");
	if (barras.length == 2) {
		var hora = barras[0];
		var minuto = barras[1];
		var resultado = (!isNaN(hora) && (hora >= 0) && (hora < 24)) && (!isNaN(minuto) && (minuto >= 0) && (minuto < 60));
		if (!resultado) {
			alert("Formato de hora inválido. Ex.: (hh:mm)");
			field.focus();
			field.select();
			return false;
		}
	}
	else {
		alert("Formato da hora inválido. Ex.: (hh:mm)");
		field.focus();
		field.select();
		return false;
	}
}

// Validar tecla nos campos de Email e usuario
function ValidaTeclaEmail() {
	//letras maiúsculas
	if ( (window.event.keyCode >= 65) && (window.event.keyCode <= 90) ) {
		// tranforma letras maiúsculas em minúsculas
		window.event.keyCode = window.event.keyCode + 32;
		return true;
	}
	else {
		// letras minusculas, numeros, hifen, ponto, arroba, underline
		if ( ((window.event.keyCode >= 97) && (window.event.keyCode <= 122))
				|| ((window.event.keyCode >= 48) && (window.event.keyCode <= 57))
				|| (window.event.keyCode == 45)
				|| (window.event.keyCode == 46)
				|| (window.event.keyCode == 64)
				|| (window.event.keyCode == 95) ) {
					return(true);
		}
		else {
			window.event.keyCode = 0;
			return false;
		}
	}
}

// Valida as teclas somente números
function ValidaNumero() {
	if (! ((window.event.keyCode >= 48) && (window.event.keyCode <= 57)) ) {
		window.event.keyCode = 0;
		return false;
	}
}

// Valida as teclas para numeros e virgula
function ValidaReal(poObj) {
	if (!( ((window.event.keyCode >= 48) && (window.event.keyCode <= 57)) || (window.event.keyCode == 44) )) {
		window.event.keyCode = 0;
		return false;
	} else {
		if (window.event.keyCode == 44) {
			if (poObj.value.length <= 0) {
				window.event.keyCode = 0;
			}
			if (poObj.value.indexOf(",") > 0) {
				window.event.keyCode = 0;
			}
		}
		
		if ((poObj.value.indexOf(",") > 0) && (poObj.value.length - (poObj.value.indexOf(",") + 1) >= 2)) {
			window.event.keyCode = 0;
		}
	}
}

// Valida as teclas para data e hora ( numeros : / )
function ValidaTeclaData() {
	if (!( ((window.event.keyCode >= 48) && (window.event.keyCode <= 57)) || (window.event.keyCode == 58) || (window.event.keyCode == 47) )) {
		window.event.keyCode = 0;
		return false;
	}
}

// Valida o CEP
function ValidaCEP(field) {
	var lcValor = new String(field.value); 
	if (lcValor.length == 0) {
		return true;
	}
	// Último Caracter Digitado
	var lcCaracter_Digitado = lcValor.substring(lcValor.length - 1, 10);
	
	// Verifica se o usuário entrou com um caracter válido
	if ( !(lcCaracter_Digitado.charCodeAt() >=48 && lcCaracter_Digitado.charCodeAt() <= 57) ) {
		field.value = lcValor.substring(0, lcValor.length - 1);
		alert("Caracter inválido!");
		field.focus();
	}
	if (lcValor.length < 5) {
		if (lcCaracter_Digitado == "/") {
			// Remove caracter
			field.value = lcValor.substring(0, lcValor.length - 1);
			field.value = "";
			field.focus();
		}
	}
	if (lcValor.length == 5) {
		// Adiciona caracter
		field.value = poObjeto.value + "-";
		field.focus();
	}
	if (lcValor.length > 9) {
		if (lcCaracter_Digitado == "/") {
			// Remove caracter
			field.value = lcValor.substring(0, lcValor.length - 1);
			field.value = "";
			field.focus();
		}
	}
	return true;
}

// Valida CPF e CGC
function ValidaCPF(field) {
	var i = 0, k = 0; i = 0, j = 0, soma = 0, mt = 0;
	var cpf = '', cgc = '', digito = '', digitoc = '', temp = '', dg = '';
	if (field.value == '') {
 		return false;
 	}
 	else {
 		cpf = field.value;
 	}
	if ( (field.value == "00000000000") || (field.value == "11111111111") || (field.value == "22222222222") ||
	     (field.value == "33333333333") || (field.value == "44444444444") || (field.value == "55555555555") ||
		 (field.value == "66666666666") || (field.value == "77777777777") || (field.value == "88888888888") ||
		 (field.value == "99999999999") || ((field.value.length != 11) && (field.value.length != 14)) ) {
		alert("CPF inválido!");
		field.value = '';
		field.focus();
		return false;
	}
	if (((cpf.length > 13) && (cpf.length < 19)) && (cpf.substring(3,4) !='.')) {
		if (cpf.length == 18) {
			temp = cpf.substring(0,2) + cpf.substring(3,6) + cpf.substring(7,10) + cpf.substring(11,15) + cpf.substring(16,18);
		}
		if (cpf.length == 16) {
			temp = cpf.substring(0,2) + cpf.substring(2,5) + cpf.substring(5,8) + cpf.substring(9,13) + cpf.substring(14,16);
		}
		if (cpf.length == 15 && cpf.substring(12,13) == '-') {
			temp = cpf.substring(0,2) + cpf.substring(2,5) + cpf.substring(5,8) + cpf.substring(8,12) + cpf.substring(13,15);
		}
		if (cpf.length == 15 && cpf.substring(8,9) == '/') {
			temp = cpf.substring(0,2) + cpf.substring(2,5) + cpf.substring(5,8) + cpf.substring(9,13) + cpf.substring(13,15);
		}
		if (cpf.length == 14) {
			temp = cpf.substring(0,2) + cpf.substring(2,5) + cpf.substring(5,8) + cpf.substring(8,12) + cpf.substring(12,14);
		}
		cgc = temp.substring(0,12);
		digito = temp.substring(12,14);
		mult = '543298765432';
		for (j = 1; j <= 2; j++) {
			soma = 0;
			for (i = 0; i <= 11; i++) {
				k = i + 1;
				soma += parseInt((cgc.substring(i,k)) * (mult.substring(i,k)));
			}
			if (j == 2) {
				soma = soma + (2 * digitoc);
			}
			digitoc = ((soma * 10) % 11);
			if (digitoc == 10) {
				digitoc = 0;
			}
			dg += digitoc;
			mult = '654329876543';
		}
		if (dg != digito) {
			alert('O CGC informado não é válido!');
			field.value = '';
			field.focus();
			return false;
		}
		else {
			field.value=temp.substring(0,2)+'.'+temp.substring(2,5)+'.'+temp.substring(5,8)+'/'+temp.substring(8,12)+'-'+temp.substring(12,14);
			return true;
		}
	}
	else {
		if (cpf.length < 11) {
			alert( 'Tamanho do campo CPF/CGC inválido. Verifique.');
			field.value = '';
			field.focus();
			return false;
		}
		if (cpf.length == 11) {
			temp = cpf.substring(0,3) + cpf.substring(3,6) + cpf.substring(6,9) + cpf.substring(9,11);
		}
		if (cpf.length == 12) {
			temp = cpf.substring(0,3) + cpf.substring(3,6) + cpf.substring(6,9) + cpf.substring(10,12);
		}
		if (cpf.length == 14) {
			temp = cpf.substring(0,3) + cpf.substring(4,7) + cpf.substring(8,11) + cpf.substring(12,15);
		}
		cpf = temp.substring(0,9);
		digito = temp.substring(9,11);
		for (j = 1; j <= 2; j++) {
			soma = 0;
			mt = 2;
			for (i = 8 + j; i >= 1; i--) {
				soma += parseInt(cpf.charAt(i-1),10) * mt;
				mt++;
			}
			dg = 11 - (soma % 11);
			if (dg > 9) { dg = 0 };
			cpf += dg;
		}
		if (digito != cpf.substring(9,11)) {
			alert('O CPF informado não é válido!');
			field.value = '';
			field.focus();
			return true;
		}
		else {
			field.value = cpf.substring(0,3) + '.' + cpf.substring(3,6) + '.' + cpf.substring(6,9) + '-' + cpf.substring(9,11);
			return true;
		}
	} // fim if (cpf.length < 15)
	return true;
}

// Verifica o email
function ValidaEmail(field) {
	var mail='';
	if (field.value == '') { return false; }
	else { mail = field; }
	
	if (mail.value == "") {
		alert("Informe seu e-mail.");
		mail.focus();
		mail.select();
		return false;
	}
	else {
		prim = mail.value.indexOf("@")
		if (prim < 2) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf("@",prim + 1) != -1) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf(".") < 1) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf(" ") != -1) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf("zipmeil.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf("hotmeil.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf(".@") > 0) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf("@.") > 0) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf(".com.br.") > 0) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf("/") > 0) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf("[") > 0) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf("]") > 0) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf("(") > 0) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf(")") > 0) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
		if (mail.value.indexOf("..") > 0) {
			alert("O e-mail informado parece não estar correto.");
			mail.focus();
			mail.select();
			return false;
		}
	}
	return true;
}