// JavaScript: Validacao e formatacao dos formularios
// Autora: Andressa K. Pires (Cinte)

function limpaCampo(item, msg) {
	var cmp = item;
	var txt = msg;

	if (cmp.value == txt) {
		cmp.value = "";
	}
}

function preencheCampo(item, msg) {
	var cmp = item;
	var txt = msg;

	if (cmp.value == "") {
		cmp.value = txt;
	}
}

// -------------------------------- Funcoes para os campos -------------------------------- //
// Separadores
function separadorTexto(item) {
	var texto = item.value;
	var tam = texto.length;
	var temp = '';
	var x = 0;
	for(x=0; x<tam; x++) {
	  if (isNaN(texto.charAt(x)) || (texto.charAt(x) == " "))
		temp = temp + texto.charAt(x);
	}

	return temp;
}

function separadorNumero(item) {
	var textonum = "";
	var texto = item.value;
	var tam = texto.length;
	var x = 0;
	for(x=0; x<tam; x++) {
		if ((!isNaN(texto.charAt(x))) && (texto.charAt(x) != " "))
			textonum = textonum + texto.charAt(x);
	}
	return textonum;
}

function separadorPreco(texto) {
	var textonum = "";
	var tam = texto.length;
	var x;
	for(x = 0; x < tam; x++) {
		if (!isNaN(texto.charAt(x)) || texto.charAt(x) == ",")
		   textonum = textonum + texto.charAt(x);
	}
	return textonum;
}

function separadorPonto(item) {
	var textonum = "";
	var texto = item.value;
	var tam = texto.length;
	var x = 0;
	for(x=0; x<tam; x++) {
		if ((!isNaN(texto.charAt(x))) || (texto.charAt(x) == "."))
			textonum = textonum + texto.charAt(x);
	}
	return textonum;
}

function separadorDigito(texto) {
	var textonum = "";
	var tam = texto.length;
	var x = 0;
	for(x=0; x<tam; x++) {
		if (!isNaN(texto.charAt(x)))
			textonum = textonum + texto.charAt(x);
	}
	return textonum;
}

// Numero
function formataNumero(item) {
	var num = separadorNumero(item);
   	item.value = num;
	return true;
}

// Numero com ponto
function formataNumeroPonto(item) {
	var num = separadorPonto(item);
   	item.value = num;
	return true;
}

// Texto
function formataTexto(item) {
	var num = separadorTexto(item);
   	item.value = num;
	return true;
}

// E-mail
function verificaEmail(item) {
	var email = item.value;
	var erro = "O e-mail deve conter um endereço eletrônico válido!";

	if (email != '') {
		//Expressao Regular utilizada para validar o endereço de email
		var expressaoRegular = /^[a-zA-Z0-9_\.-]{2,}@([A-Za-z0-9_-]{2,}\.)+[A-Za-z]{2,4}$/;
		if ( !expressaoRegular.test(email) ) {
			alert(erro);
			item.value = "";
			return false;
		}
		return true;
	}
}
// ---------------------------------------------------------------------------------------- //
// Newsletter
function abreNews() {
	var top, esq, alt, lar;

	if (document.all) {
		top = 290;
		esq = 430;
		lar = 450;
		alt = 205;
	} else {
		top = 310;
		esq = 445;
		lar = 450;
		alt = 205;
	}

	window.open("cancela_newsletter.php","","toolbar=no, location=no, directories=yes, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, fullscreen=no, top="+ top +", left="+ esq +", width="+ lar +", height="+ alt +"");
}

function fechaNews() {
	var janela;
	janela = window.close('cancela_newsletter.php');
}

// Contato
function verifContato() {
	var nom = $('input[@id=nome]').val();
	var emp = $('input[@id=empresa]').val();
	var tel = $('input[@id=telefone]').val();
	var ema = $('input[@id=email]').val();
	var men = $('textarea[@id=mensagem_contato]').val();

	if (nom == "Nome")
		alert('Informe o nome!');
	else if (emp == "Empresa")
			alert('Informe o nome da empresa!');
		else if (tel == "Telefone")
				alert('Informe o telefone para contato!');
			else if (ema == "E-mail")
					alert('Informe o e-mail para contato!');
				else if (men == "Mensagem")
						alert('Informe a mensagem!');
					else {
						e = document.getElementById("email");

						if (verificaEmail(e) != false) {
							$('#formContato').submit();
						}
					}
}
