function ValidarInt(s)
{
	checkOK = "0123456789";
	for (i = 0;  i < s.length;  i++)	{
		ok = false;
		for (j = 0;  j < checkOK.length;  j++)
			if (s.charAt(i) == checkOK.charAt(j)) {
				ok = true;
			}
		if (!ok)
			return false;	 
	}
	return true;
}

function ValidarData(s)
{
	if (s.length < 10 || s.charAt(2) != "/" || s.charAt(5) != "/")
	{
        return false;      
	}
	var nDia;
	var nMes;
	var nAno;
	
	nDia = s.substr(0,2);
	nMes = s.substr(3,2);
	nAno = s.substr(6,4);
	
	if (!ValidarInt(nDia) || !ValidarInt(nMes) || !ValidarInt(nAno))
	{
        return false;      
	}

	nDia = parseInt(nDia);
	nMes = parseInt(nMes);
	nAno = parseInt(nAno);
	
	if (nDia > 31 || nMes > 12 || (nDia > 29 && nMes == 2) || nAno < 1880 || nAno > 2010)
	{
        return false;      
	}
	
	
	return true;
}

function ValidarCPF(numeroCPF) 
{

    var numero;
    
    numero = numeroCPF.substring(0,9);
    numero = digitoCPF(numero);
    numero = digitoCPF(numero);
    if (numero != numeroCPF)
        return false
    else
        return true;
} 

function digitoCPF(numero) 
{

    var num;
    var digito;
    var soma;
    var resultado;
    
    num	 = numero;
    digito = 2;
    soma = 0;
    for (i=num.length-1; i >= 0; i--) {
        soma += parseInt(num.substring(i,i+1),10) * digito;
        digito++;
    }
    resultado = soma % 11;
    if ( resultado == 0 || resultado == 1 )
        resultado = 0
    else
        resultado = 11 - resultado;
    num += resultado;
    return num;
}

function ComparaCampoPesquisa(campo1, campo2, nomeCampo)
{
	var c1;
	var c2;
	var s;

	c1 = document.frm[campo1];
	c2 = document.frm[campo2];
	s = c2.value;

	if (c1[0].checked && s.length == 0)
	{
		window.alert("Por favor, n\u00E3o deixe de dizer '" + nomeCampo + "'");
		c2.focus();
		return false;
	}

	return true;
}

function ValidaPalavras(campo, nome, opcional, max){
	var c;
	var s;

	c = document.frm[campo];
    s = c.value;

	if ((s == '' || s == 0 || s == null) && !opcional) {
		window.alert("Por favor, n\u00E3o deixe de preencher o campo '" + nome + "'");
		c.focus();
		return false;
	}

	if (c.value.split(' ').length > max){
		window.alert("O campo '" + nome + "' n\u00E3o pode ter mais do que " + max + " palavras!\nPor favor, verifique!");
		c.focus();
		return false;
	}

	return true;
}

function ComparaCampo(campo1, campo2, nomeCampo1, nomeCampo2)
{
	var c1;
	var c2;
	var s1;
	var s2;
	
	c1 = document.frm[campo1];
	s1 = c1.value;
	c2 = document.frm[campo2];
	s2 = c2.value;
	
	if (s1 == s2)
	{
		return true;
	}
	else 
	{
		window.alert("Os campos '" + nomeCampo1 + "' e '" + nomeCampo2 + "'\ndevem ser iguais!\nPor favor, verifique!");
		c1.focus();
		return false;
	}

	return true;
}

function ValidarCampo(campo, nome, mascara, opcional, max)
{
	var i;
	var s;
	var f;
	var c;
	
	c = document.frm[campo];
	s = c.value;
	
	if (mascara == "radio") {
		ffok = false
		for (i=0; i < c.length; i++) {
			if (c[i].checked) {
				ffok = true
			}
		}
		if (!ffok && !opcional) {
			window.alert("Por favor, n\u00E3o deixe de selecionar alguma op\u00E7\u00E3o para o campo '" + nome + "'");
			c[0].focus();
			return false;
		}
		return true;
	}
	
	if (mascara == "combo") {
		var index = c.selectedIndex
		if (c.options[index].value == "0" && !opcional) {
			window.alert("Por favor, n\u00E3o deixe de selecionar alguma op\u00E7\u00E3o para o campo '" + nome + "'");
			c.focus();
			return false;
		}
		return true;
	}
		
	if (c.type == "select-one") {
		s = c.options[c.selectedIndex].value;
	}
	
	
	if ((s == '' || s == 0 || s == null) && !opcional) {
		window.alert("Por favor, n\u00E3o deixe de preencher o campo '" + nome + "'");
		c.focus();
		return false;
	}
	
	if (max && (s.length > max)) {
		window.alert("O campo '" + nome + "' n\u00E3o pode ter mais de " + max + " caracteres!\nPor favor, verifique!");
		c.focus();
		return false;
	}
	
	if (mascara == "dd/mm/yyyy" && !ValidarData(s)) {
		window.alert("Preencha o campo '" + nome + "' no formato 'dd/mm/aaaa'!\nPor favor, verifique!");
		c.focus();
		return false;
	}

	if (mascara == "num") {
		nPode = ",?!:;)<>|\/^~´`#$%¨&*()[]{}=+'";
		nPode+='"';
		nPode+="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
		for (x = 0;x<nPode.length;x++)
		{
			if (s.indexOf (" ") > -1 && !opcional)
			{
                window.alert("O campo '" + nome + "' n\u00E3o pode conter espa\u00E7os em branco.\nPor favor, verifique!");
				c.select();
				c.focus();
                return false;      
			}

			if (s.indexOf (nPode.charAt(x)) > -1 || s.indexOf ("@.") > -1 || s.indexOf (".@") > -1)
			{
                caracter = nPode.charAt(x);
                window.alert("O campo '" + nome + "' s\u00F3 pode conter n\u00FAmeros.\nPor favor verifique!");
				c.select();
				c.focus();
                return false;      
			}
		}
        return true;      
	}

	if (mascara == "email") {
		nPode = ",?!:;)<>|\/^~´`#$%¨&*()[]{}=+'";
		nPode+='"';
		for (x = 0;x<nPode.length;x++)
		{
			if (s.indexOf (nPode.charAt(x)) > -1)
			{
                caracter = nPode.charAt(x);
                window.alert("O campo '" + nome + "' n\u00E3o pode conter o caracter '" + caracter + "' \n Por favor verifique!");
				c.select();
				c.focus();
                return false;      
			}
		}

		if (s.indexOf (" ") > -1)
		{
            window.alert("O campo '" + nome + "' n\u00E3o pode conter espa\u00E7os em branco.\nPor favor verifique!");
			c.select();
			c.focus();
            return false;      
		}
        if (s.indexOf ("@.") > -1 || s.indexOf (".@") > -1 || s.indexOf ("@@") > -1 || s.indexOf ('@',0) == -1 || s.indexOf ('.',0) == -1 || s.substring(0,1) == "@" || s.substring(s.length,1) == "@")
        {      
            window.alert("O campo '" + nome + "' deve ser preenchido com um formato v\u00E1lido.\nEx: 'seunome@dominio.com.br'!");
			c.select();
			c.focus();
            return false;      
        }   
        else {      
                return true;      
        }   
	}
	
	if (mascara == "cpf") {
		if (s.length != 11) {
			window.alert("O campo '" + nome + "' deve ser preenchido com 11 n\u00FAmeros!\nPor favor, verifique!");
			c.focus();
			return false;
		}
		if (!ValidarInt(s)) {
			window.alert("O campo '" + nome + "' deve ser preenchido com um n\u00FAmero de CPF v\u00E1lido sem pontos ou tra\u00E7os!\nPor favor, verifique!");
			c.focus();
			return false;
		}
		if (!ValidarCPF(s)) {
			window.alert("O campo '" + nome + "' n\u00E3o est\u00E1 com o d\u00EDgito verificador correto!\nPor favor, verifique!");
			c.focus();
			return false;
		}
	}
	
	if (mascara == "telefone") {
		if (s.length != 10 && !opcional) {
			window.alert("O campo '" + nome + "' deve ser preenchido com 10 n\u00FAmeros!\nPor favor, verifique!");
			c.focus();
			return false;
		}
		if (!ValidarInt(s) && !opcional) {
			window.alert("O campo '" + nome + "' deve ser preenchido com um n\u00FAmero de telefone v\u00E1lido, com DDD. Ex: 2188881111\nPor favor, verifique!");
			c.focus();
			return false;
		}
	}

	if (mascara == "cep") {
		if (s.length != 8) {
			window.alert("O campo '" + nome + "' deve ser preenchido com 8 n\u00FAmeros!\nPor favor, verifique!");
			c.focus();
			return false;
		}
		if (!ValidarInt(s)) {
			window.alert("O campo '" + nome + "' deve ser preenchido apenas com n\u00FAmeros, sem pontos ou tra\u00E7os!\nPor favor, verifique!");
			c.focus();
			return false;
		}
	}


	if (mascara == "int") {
		if (!ValidarInt(s)) {
			window.alert("O campo '" + nome + "' deve ser preenchido apenas com n\u00FAmeros!\nPor favor, verifique!");
			c.focus();
			return false;
		}
	}
	
	return true;
}

function ValidarCampoLogin(campo, nome, mascara, opcional, max)
{
	var i;
	var s;
	var f;
	var c;
	
	c = document.frmLogin[campo];
	s = c.value;
	
	if ((s == '' || s == 0 || s == null) && !opcional) {
		window.alert("Por favor, n\u00E3o deixe de preencher o campo '" + nome + "'");
		c.focus();
		return false;
	}
	
	if (max && (s.length > max)) {
		window.alert("O campo '" + nome + "' n\u00E3o pode ter mais de " + max + " caracteres!\nPor favor, verifique!");
		c.focus();
		return false;
	}
	
	if (mascara == "email") {
		nPode = ",?!:;)<>|\/^~´`#$%¨&*()[]{}=+'";
		nPode+='"';
		for (x = 0;x<nPode.length;x++)
		{
			if (s.indexOf (nPode.charAt(x)) > -1)
			{
                caracter = nPode.charAt(x);
                window.alert("O campo '" + nome + "' n\u00E3o pode conter o caracter '" + caracter + "' \n Por favor verifique!");
				c.select();
				c.focus();
                return false;      
			}
		}

		if (s.indexOf (" ") > -1)
		{
            window.alert("O campo '" + nome + "' n\u00E3o pode conter espa\u00E7os em branco.\nPor favor verifique!");
			c.select();
			c.focus();
            return false;      
		}
        if (s.indexOf ("@.") > -1 || s.indexOf (".@") > -1 || s.indexOf ("@@") > -1 || s.indexOf ('@',0) == -1 || s.indexOf ('.',0) == -1 || s.substring(0,1) == "@" || s.substring(s.length,1) == "@")
        {      
            window.alert("O campo '" + nome + "' deve ser preenchido com um formato v\u00E1lido.\nEx: 'seunome@dominio.com.br'!");
			c.select();
			c.focus();
            return false;      
        }   
        else {      
                return true;      
        }   
	}
	
	return true;
}




function AbrirJanela(theURL,winName,features) 
{
	window.open(theURL,winName,features);
}

function fala(texto)
{
	if (texto.length > 0)
	{
		falaMoca.innerHTML = texto;
	}
	else
	{
		falaMoca.innerHTML = "<b>Cadastre-se</b> agora no fique teen e se ligue na sua galera!<br>&Eacute; <b>Gr&aacute;tis</b>.";	
	}
	return;
}