﻿function MostrarCidades(idEstado, campo, pagina, divID)
{
	campo = escape(campo);
	if(idEstado != "")
	{
		oXMLHTTP = createXMLHTTP();
		
		eval("document.getElementById('" + divID + "').innerHTML = 'carregando...'");
		oXMLHTTP.open("POST", pagina, true);
		oXMLHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		oXMLHTTP.onreadystatechange = function(){
						if(oXMLHTTP.readyState == 4){
							eval("document.getElementById('" + divID + "').innerHTML = oXMLHTTP.responseText");}}
		oXMLHTTP.send("id_estado=" + idEstado + "&campo=" + campo);
	}
}


function ValidarBusca(f)
{
	if(f.busca.value.length < 2)
	{
		alert("Entre com pelo menos 2 caracteres, para efetuar a busca.");
		f.busca.focus();
		return false;
	}
	return true;
}

function ValidarLogin(f)
{
	if(!ValidarEmail(f.usuario.value))
	{
		alert("Por favor, entre com seu Email de usuário.");
		f.usuario.focus()
		return false;
	}
	
	if(f.senha.value.length < 3)
	{
		alert("Por favor, entre com sua senha.");
		f.senha.focus()
		return false;
	}
	return true;
}

function ValidarLoginComStatus(f,divLogin, divSenha)
{
	if(f.login.value.length < 3)
	{
		document.getElementById(divLogin).innerHTML = "*";
		alert("Por favor, entre com seu login.");
		f.login.focus()
		return false;
	}
	
	if(f.senha.value.length < 3)
	{
		document.getElementById(divSenha).innerHTML = "*";
		document.getElementById(divLogin).innerHTML = "";
		alert("Por favor, entre com sua senha.");
		f.senha.focus()
		return false;
	}
	return true;
}


// JavaScript Document
function ValidarCampoTexto(s)
{
	if(s.length < 2)
		return false
	return true
}

function ValidarCombo(combo)
{
	if(combo.options[combo.selectedIndex].value == "")
		return false;
	else
		return true;
}

function ValidarCPF(s)
{ 
	var i; 
	s = s.replace(".","");
	s = s.replace(".","");
	s = s.replace("-","");
	var c = s.substr(0,9); 
	var dv = s.substr(9,2); 
	var d1 = 0; 
	for (i = 0; i < 9; i++) 
		d1 += c.charAt(i)*(10-i); 
	
	if (d1 == 0)
		return false; 
	
	d1 = 11 - (d1 % 11); 
	  
	if (d1 > 9) d1 = 0; 
	  
	if (dv.charAt(0) != d1) 
		return false; 
	  
	d1 *= 2; 

	for (i = 0; i < 9; i++) 
		d1 += c.charAt(i)*(11-i); 
	  
	d1 = 11 - (d1 % 11); 
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(1) != d1) 
		return false; 

	return true; 
}

function ValidarCNPJ(cnpj)
      {
	  var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
	  cnpj = cnpj.replace(".","");
	  cnpj = cnpj.replace(".","");
	  cnpj = cnpj.replace(".","");
	  cnpj = cnpj.replace("-","");
	  cnpj = cnpj.replace("-","");
	  cnpj = cnpj.replace("-","");
	  cnpj = cnpj.replace("-","");
	  cnpj = cnpj.replace("/","");
	  cnpj = cnpj.replace("/","");
	  cnpj = cnpj.replace("/","");
	  cnpj = cnpj.replace("/","");
	  
      digitos_iguais = 1;
      if (cnpj.length < 14 && cnpj.length < 15)
            return false;
      for (i = 0; i < cnpj.length - 1; i++)
            if (cnpj.charAt(i) != cnpj.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            tamanho = cnpj.length - 2
            numeros = cnpj.substring(0,tamanho);
            digitos = cnpj.substring(tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            tamanho = tamanho + 1;
            numeros = cnpj.substring(0,tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
      else
            return false;
      } 


function ValidarEmail(email)
{
	if (email == "")
		return false

	var invalidChars = " /\;:'"
	var badchar

	for(i=0;i < invalidChars.length;i++)
	{
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1)
			return false
	}
	atPos = email.indexOf("@",1)

	if (atPos == -1)
		return false

	if (email.indexOf("@",atPos+1) > -1)
		return false
	periodPos = email.indexOf(".",atPos)

	if (periodPos == -1)
		return false

	if (periodPos+3 > email.length)
		return false
		
	return true
}

//Função para exibir/ocultar a camada
function toggleMenu(currMenu)
{
	if(document.getElementById)
	{
		thisMenu = document.getElementById(currMenu).style
		
		if(thisMenu.display == "block")
				thisMenu.display = "none"
		else
			thisMenu.display = "block"
		return false
	}
	else
		return true
}


// Verifica se a data apresentada ou no uma data válida
function IsDate(day, month, year) {
	//IsDate(29, 2, 2005)
	//IsDate(29, 2, 2004)
	var date = new Date();
	var blnRet = false;
	var blnDay;
	var blnMonth;
	var blnYear;

	date.setFullYear(year, month -1, day);

	blnDay   = (date.getDate()      == day);
	blnMonth = (date.getMonth()     == month -1);
	blnYear  = (date.getFullYear()  == year);

	if (blnDay && blnMonth && blnYear)
	blnRet = true;

	return blnRet;
}



function ProtegerSelecao()
{
	alert('Todos os Direitos Resevados 2008');
	return false;
}

/*
function ProtegerCodigo()
{
	if (event.button==2||event.button==3)
   		alert('Todos os Direitos Resevados 2008');
}

function rejeitaTecla(oEvent){    
//by Micox - elmicox.blogspot.com - www.ievolutionweb.com
    var cod_tecla=62; //tecla que quer bloquear

    //filtrando o evento
    var oEvent = oEvent ? oEvent : window.event;
    var tecla = (oEvent.keyCode) ? oEvent.keyCode : oEvent.which;
    
    if(oEvent.type=="keydown" && navigator.appName.indexOf('Internet Explorer')<0 ){
        alert("alert 1");
		//se for keydown e não for o IE, vazarei pois o keypress já foi executado
        return false;
    }
    
    if (typeof(oEvent.keyCode)=='number' && oEvent.keyCode == cod_tecla){
        if (typeof(oEvent.preventDefault)=='function'){
			alert("alert 2");
            oEvent.preventDefault();
        } else {
			alert("3");
            oEvent.returnValue = false;
            oEvent.keyCode = 0;
        }
    }
    return false;
}
document.onkeypress = rejeitaTecla; //Pro Opereta e FF. O keydown nao tem preventDefault no OP.
document.onkeydown = rejeitaTecla; //Pro IE. O IE 6 não executa funcoes no keypress.

*/


function click() 
{
	if (event.button==2||event.button==3) 
	{
		oncontextmenu='return false';
	}
}
/*
document.onmousedown=click
document.oncontextmenu = new Function("alert('Todos os Direitos Reservados!');return false;")
*/

function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
target.style.MozUserSelect="none"
else //All other route (ie: Opera)
target.onmousedown=function(){return false}
target.style.cursor = "default"
}
//window.sidebar=disableSelection(document.body);

//document.onmousedown=ProtegerCodigo;
//document.onselectstart=ProtegerSelecao;

