function abrirJanela(url, lar, alt) {
	janela = window.open(url,'Janela','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width='+ lar +',height=' + alt);
}

function execUrl(url, mensagem){
	if(confirm(mensagem))
		window.location = url;
}

function alterarSituacao(nomeCampo, form) {
var formulario = document.getElementById(form);
var selObj = document.getElementById(nomeCampo);	
var selIndex = selObj.selectedIndex;
	if(confirm('Tem certeza que deseja Alterar a situação dessa venda?')) {
		if (selObj.options[selIndex].value == 3) {
			if(confirm('A alteração para esta situação irá dar baixa no estoque, tem certeza que deseja proceguir?'))		
				formulario.submit();
		}
		else
				formulario.submit();
	}
				
}
function execUrlEscolheEndereco(mensagem){
	if(confirm(mensagem)) {
		window.close();
		opener.document.frm.endereco.value = document.frmEnderecos.logradouro.value +  " "  + document.frmEnderecos.rua.value + " - " + document.frmEnderecos.pais.value + " - " + document.frmEnderecos.cidade.value + " / " + document.frmEnderecos.estado.value;
        opener.document.frm.id_endereco.value = document.frmEnderecos.id_endereco.value;
		opener.document.frm.method = 'get';  opener.document.frm.submit();
	}
}

function voltar() {
	history.go(-1);
}

function fecharJanela(janela) {
	window.close();
}

function ir(url) {
        window.location=url;
}

//-------------------- Funcao para redirecionar um pagina para seu seu "chamador"
function retornaTela(janela){
	location = janela.parent;
}
//--------------------------------------------------------------------------------
function aviso (campo, msg)
{   alert(msg);
    campo.focus();
    campo.select();
    return false;
}

//
function CPF() {
	alert("Informe o número de inscrição no Cadastro de Pessoas Físicas - CPF, inclusive os algarismos dos dígitos verificadores. Não são necessários os separadores do número.");
}
//
function Data() {
	alert("Informe a data de nascimento, com dois algarismos para o dia, dois para o mês e quatro para o ano. Formato: ddmmaaaa. Não são necessários os separadores do número.");
}


//-------------------------------
function RetiraCaracteresInvalidos(strCampo,tam) {
	nTamanho = strCampo.length;
	szCampo = "";
	j=0;
	for (i = nTamanho-1;i>=0;i--) 
	{
		if (isDigit(strCampo.charAt(i)))	{
			szCampo = strCampo.charAt(i) + szCampo;
			j++;
			if (j > tam) break;
		}
	}
	if (szCampo.length < tam) {
		for (i = szCampo.length;i<tam;i++) 
		{
			szCampo = "0" + szCampo;
		}
	}
    return szCampo;
}
//-------------------------------
function ContaDigitos(strCampo) {
	nTamanho = strCampo.length;
	szCampo = "";
	j=0;
	for (i = nTamanho-1;i>=0;i--) 
	{
		if (isDigit(strCampo.charAt(i)))	{
			szCampo = strCampo.charAt(i) + szCampo;
			j++;
		}
	}
    return j;
}
//-------------------------------
function validaCPF(rcpf1,rcpf2,c) {
  d1 = 0;
  for (i=0;i<9;i++) 
    d1 += rcpf1.charAt(i)*(10-i);
  d1 = 11 - (d1 % 11);
  if (d1>9) d1 = 0;
  
  if (rcpf2.charAt(0) != d1) 
    return false;
  
  d1 *= 2;
  for (i=0;i<9;i++) 
    d1 += rcpf1.charAt(i)*(11-i);
  d1 = 11 - (d1 % 11);
  if (d1>9) d1 = 0;
  
  if (rcpf2.charAt(1) != d1) 
    return false;
  return true;
}
//-------------------------------
/*function ajustar_data(input , evento)
{
         var BACKSPACE=  8; 
         var DEL=  46; 
         var FRENTE=  39; 
         var TRAS=  37; 
         var key; 
         var tecla; 
         var strValidos = "0123456789" ;
         var temp;
         tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
         //alert(tecla)
         if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)) {
             return true; 
             }
         if ( tecla == 13 )     return false; 
         if ((tecla<48)||(tecla>57)){
		    return false;
		    }
         key = String.fromCharCode(tecla); 
         input.value = input.value+key;
         temp="";
         for (var i = 0; i<input.value.length;i++ )
             {
               if (temp.length==2) temp=temp+"/";
               if (temp.length==5) temp=temp+"/";
               if ( strValidos.indexOf( input.value.substr(i,1) ) != -1 ) {
                  temp=temp+input.value.substr(i,1);
               }   
             }
         input.value = temp.substr(0,10);
         return false;
	//
}*/
//-------------------------------

/*function ajustar_cpf(input , evento)
{
         var BACKSPACE=  8; 
         var DEL=  46; 
         var FRENTE=  39; 
         var TRAS=  37; 
         var key; 
         var tecla; 
         var strValidos = "0123456789" ;
         var temp;
         tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
         if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)) {
             return true; 
             }
         if ( tecla == 13 )     return false; 
         if ((tecla<48)||(tecla>57)){
		    return false;
		    }

         key = String.fromCharCode(tecla); 
         input.value = input.value+key;
         temp="";
         for (var i = 0; i<input.value.length;i++ )
             {
               if (temp.length==3) temp=temp+".";
               if (temp.length==7) temp=temp+".";
               if (temp.length==11) temp=temp+"-";
               if ( strValidos.indexOf( input.value.substr(i,1) ) != -1 ) {
                  temp=temp+input.value.substr(i,1);
               }   
             }
         input.value = temp.substr(0,14);
         return false;
}*/
//-------------------------------
function ajustar_data(input, evento)
{
         var BACKSPACE=  8; 
         var DEL=  46; 
         var FRENTE=  39; 
         var TRAS=  37; 
         var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
         if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)) {
             return true; 
             }
         if ( tecla == 13 )     return false; 


		if ((tecla<48)||(tecla>57)) {
			evento.returnValue = false; 
			return false;
			} else { 
				if ((input.value.length==2)||(input.value.length==5)) {
				   input.value=input.value + "/" ;
				   }
			}
			//
			//
  return true;			
};
//---------------
function ajustar_cpf(input, evento)
{
         var BACKSPACE=  8; 
         var DEL=  46; 
         var FRENTE=  39; 
         var TRAS=  37;          
         var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
         if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)) {
             return true; 
             }
         if ( tecla == 13 )     return false; 

		if ((tecla<48)||(tecla>57)){
			evento.returnValue =false;
			return false;
		} else { 
			if((input.value.length==3)||(input.value.length==7)) {
			    input.value=input.value + "." ;
			}  else {
			      if(input.value.length==11) {
			           input.value=input.value + "-" ;
			         }
			}
		}
 return true;		
  		
}
//-------------------------------
function editadata(strCampo,tam) {
    var i;
    var j;
    var nTamanho;
    var szCampo;
	nTamanho = strCampo.length;
	szCampo = "";
	j=0;
	for (i = nTamanho-1;i>=0;i--) 
	{
		if (isDigit(strCampo.charAt(i)))	{
			szCampo = strCampo.charAt(i) + szCampo;
		    j++;
			if (j > tam) break;
        }
	}
	if (szCampo.length < tam) {
		for (i = szCampo.length;i<tam;i++) 
		{
			szCampo = "0" + szCampo;
		}
	}
	if (j==0) return "";
    return (szCampo.substr(0,2)+"/"+szCampo.substr(2,2)+"/"+szCampo.substr(4,4));
}

//-------------------------------
function editacpf(strCampo,tam) {
    var i;
    var j;
    var nTamanho;
    var szCampo;
    j=0;
	nTamanho = strCampo.length;
	szCampo = "";
	for (i = nTamanho-1;i>=0;i--) 
	{
		if (isDigit(strCampo.charAt(i)))	{
			szCampo = strCampo.charAt(i) + szCampo;
			j++;
			if (j > tam) break;
        }
	}
	if (szCampo.length < tam) {
		for (i = szCampo.length;i<tam;i++) 
		{
			szCampo = "0" + szCampo;
		}
	}
	if (j==0) return "";
    return (szCampo.substr(0,3)+"."+szCampo.substr(3,3)+"."+szCampo.substr(6,3)+"-"+szCampo.substr(9,2));
}
//-------------------------------
function isDigit (c){   
	
	    return ((c >= "0") && (c <= "9"))
    }  
//-------------------------------
function isInteger (s) {
    var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return false;
     
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}
//-------------------------------
function isEmpty(s){
    return ((s == null) || (s.length == 0))
}
function ValidaData(strData)
{
	if (strData.length != 10)
	{
		msgstatus = msgstatus + "Informe a data de nascimento no formato (DDMMAAAA).\n\n";
		return false;
	}
	var i = parseInt(strData.substring(6,10));
	if (i < 1840 || i > 2003)  
	{
		msgstatus = msgstatus + "O ano da data de nascimento deve ser igual ou superior a 1840 e anterior a 2004.\n\n";
		return false;
	}
	var j = parseInt(strData.substring(3,5),10);
	if (j < 1 || j > 12)  
	{
		msgstatus = msgstatus + "O mês da data de nascimento deve estar entre 1 e 12.\n\n";
		return false;
	}
	var k = parseInt(strData.substring(0,2),10);
	if (k < 1 || k > 31)  
	{
		msgstatus = msgstatus + "O dia da data de nascimento deve estar entre 1 e 31.\n\n";
		return false;
	}	return true;
}
function ValidaForm() {
		foco = "";
		msgstatus = "";
     	var f 	  = document.formAddFuncionarios;
     	if (isEmpty(f.funcionario_nome.value)){
			foco = f.funcionario_nome;
			msgstatus = msgstatus + "Informe o nome.\n\n";
			     	
     	}
     	

			if ((ContaDigitos(f.funcionario_cpf.value) > 11 ) && (isEmpty(foco))) {
				if (isEmpty(foco))
				foco = f.funcionario_cpf;
				msgstatus = msgstatus +  "CPF inválido.\n\n";
			}
			var CPFaux = RetiraCaracteresInvalidos(f.funcionario_cpf.value,11);
			NR_CPF = CPFaux.substr(0,9);
			var NR_DV = CPFaux.substr(9,2);
	
			if ((isEmpty(NR_CPF)) && (isEmpty(foco))) {			
				foco = f.funcionario_cpf;
				msgstatus = msgstatus +  "CPF inválido.\n\n";
			}
			else if(! validaCPF(NR_CPF, NR_DV,"CPF") && (isEmpty(foco)))
					{
						foco = f.funcionario_cpf;
						msgstatus = msgstatus +  "CPF inválido.\n\n";
			}

		    	
		if (! isEmpty(foco)) 
		{
			alert(msgstatus);
		}
		else
			f.submit();
}

