function popUpWindow(URLStr, left, top, width, height)
{
/*var popUpWin=0;
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }*/
  x= Math.random();
  popUpWin = open(URLStr,'', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function randomNumber(limit){
  return Math.floor(Math.random()*limit);
}

//Desabilita as teclas numerica
function isNum(key){ 
	if((key <48)||(key>57)) 
//		if((key!=46)&&(key!=45))//permitir o "."  e o  "-"
			return false 
}
function isCnpj_Cpf(key){ 
	if((key <48)||(key>57)) 
		if((key!=46)&&(key!=45)&&(key!=47))//permitir o "."  e o  "-"
			return false 
}
function isFloat(key){ 
//window.alert(key);
	if((key <48)||(key>57)) 
		if((key!=46)&&(key!=45) && (key!=44))//permitir o "." , "-",  ","
			return false 
}
//Desabilita as teclas alfa
function isTexto(key){ 
	if((key >42) & (key<58)) 
			return false 
}

function formataCNPJ (s){
	aux = s.value;
	if((aux.length==2) || (aux.length==6))
		s.value += '.';
	else



		if(aux.length==10)   
			s.value += '/';
		else
			if(aux.length==15)   
				s.value += '-';

}
// valida CNPJ
function isCNPJ( s ) {
  var r = s.value;
  r = r.replace(".","");
  r = r.replace(".","");
  r = r.replace("/","");
  r = r.replace("-","");
	
//  r = r.substring(0,2) +'' +  r.substring(3,6) +''+ r.substring(7,10) + '' + r.substring(11,15) + '' + r.substring(16,19);
 // window.alert(r);
  var iError = 0 ;
  var nCGC_ = r ;
	if (r.length == 0){
		return true
	} 
  if ( (r * 1) == 0 ) iError++ ;
 
  if ( nCGC_.length == 15 && nCGC_.charAt(0) == "0") nCGC_ = nCGC_.substring(1 , nCGC_.length) ;
  if ( nCGC_.length != 14 ) iError++ ;
 
  var nCGC = new Array() ;
 
  for ( i = 0 ; i < 15 ; i++ ) {
    nCGC[i] = nCGC_.charAt(i) ;
  }
 
  iDV1=(nCGC[0]*5+nCGC[1]*4+nCGC[2]*3+nCGC[3]*2+nCGC[4]*9+nCGC[5]*8+nCGC[6]*7+nCGC[7]*6+nCGC[8]*5+nCGC[9]*4+nCGC[10]*3+nCGC[11]*2)%11;
  if ( iDV1 == 1 || iDV1 == 0 ) { iDV1 = 0 } else { iDV1 = 11 - iDV1 }
  if ( iDV1 != nCGC[12] ) iError++ ;
 
  iDV2=(nCGC[0]*6+nCGC[1]*5+nCGC[2]*4+nCGC[3]*3+nCGC[4]*2+nCGC[5]*9+nCGC[6]*8+nCGC[7]*7+nCGC[8]*6+nCGC[9]*5+nCGC[10]*4+nCGC[11]*3+nCGC[12]*2)%11 ;
  if ( iDV2 == 1 || iDV2 == 0 ) { iDV2 = 0 } else { iDV2 = 11 - iDV2 }
  if ( iDV2 != nCGC[13] ) iError++ ;
 
  if ( iError > 0 ) {
   //  alert ( "O CGC/CNPJ está incorreto. Por favor, verifique.\n" ) ;
    return false ;
  } else {
   return true ;
  }
}





function isEmail(email){
	var vmail = email.value;
//	alert(email.value)

//	window.alert(vmail.length  + '')
	if (vmail.length == 0){
		return true
	}
		 //verifica a existência de espaços em brancos
		if(vmail.indexOf(" ") > -1){
			return false;
		}
		//verifica a existência de apóstrofes campo E-MAIL
		if(vmail.indexOf("'") > -1){
			return false;
		}		
		//Consistência de e-mail:
		// 2 @
		if(vmail.lastIndexOf("@") != vmail.indexOf("@")){
			return false;
		}
		//sem @
		if(vmail.indexOf("@") == -1){
			return false;
		}
		// @ no início
		if(vmail.charCodeAt(0) == 64){
			return false;
		}	
		// @ no final
		if(vmail.charCodeAt((vmail.length)-1) == 64){
			return false;
		}

		//VERIFICA EXISTÊNCIA DE PONTO APÓS DO @
		if(vmail.indexOf("@.") > 0){   
			return false; 
		}   
		//não aceita caracteres diferentes				de  alfanuméricos e_ e   -e.				
		for(I=0;I<=(vmail.length);I++){
			if((vmail.charCodeAt(I)<48||vmail.charCodeAt(I)>57)&&(vmail.charCodeAt(I)<65||vmail.charCodeAt(I)>90)&&(vmail.charCodeAt(I)<97||vmail.charCodeAt(I)>122)&&(vmail.charCodeAt(I)!=95&&vmail.charCodeAt(I)!=45&&vmail.charCodeAt(I)!=46&&vmail.charCodeAt(I)!=64)){
							returnfalse;
		}
	return true;
	}
  }

function formataCPF (s){
	aux = s.value;
	if((aux.length==3) || (aux.length==7) )
		s.value += '.';
	else
		if(aux.length==11)   
			s.value += '-';
}

function isCPF (x) {

  var r = x.value;
  r = r.replace(".","");
  r = r.replace(".","");
  r = r.replace("-","");

  //r = r.substring(0,3) +'' +  r.substring(4,7) +''+ r.substring(8,11) + '' + r.substring(12,14);
//window.alert(r);
 CPF =r;
	 if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
	  CPF == "22222222222" || CPF == "33333333333" || CPF == "44444444444" ||
	  CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
	  CPF == "88888888888" || CPF == "99999999999")
		  return false;
	 soma = 0;
	 for (i=0; i < 9; i ++)
	  soma += parseInt(CPF.charAt(i)) * (10 - i);
	
	 resto = 11 - (soma % 11);
	 if (resto == 10 || resto == 11)
	  	resto = 0;
	 if (resto != parseInt(CPF.charAt(9)))
		  return false;
	 soma = 0;
	 for (i = 0; i < 10; i ++)
	  	soma += parseInt(CPF.charAt(i)) * (11 - i);
	 
	 resto = 11 - (soma % 11);
	 if (resto == 10 || resto == 11)
	 	 resto = 0;
	 if (resto != parseInt(CPF.charAt(10)))
		  return false;
//			alert('Correto');
	 return true;
 }

function Excluir(){
	return confirm('Você irá excluir o registro!');
}
function ExcluirRegistros(){
	return confirm('Você irá excluir todos os registros selecionados!');
}

function trocaFigura(campo){
	
   document.getElementById('foto'+campo.name).innerHTML='<img src=\''+campo.value+'\' width=50 height=50>';
}
function formataCEP(x){
	aux = x.value;
	if(aux.length==2) 
		x.value += '.';
	if(aux.length==6) 
		x.value += '-';
}



function FormataReal(valorAux, casas){
	if(casas== undefined )
		casas = 2;
		
	aux = 0;
	aux2 ='';
	if(casas== 4 ){
		aux = 10000
		aux2 = ',0000'
	}
	if(casas== 2 ){
		aux = 100;
		aux2 = ',00'
	}
		
	var valor = Math.round(valorAux*aux)/aux;
	if(isNaN(valor))
		return '';
	var teste = FComma(valor) + '';
	var ponto = teste.indexOf('.') ;
	teste = teste.replace( ",", "." );	
	if(ponto>0){
		teste = teste.substr(0,ponto) + "," + teste.substr(ponto+1,casas)
		//coloca os '0' no final 
		var aux = (teste.length)-ponto-1
		if(aux==1)
		   	teste += '0';
	}else
		teste += aux2
	return teste
}

function FormataFloat(valor, casas){
	if(casas== undefined )
		casas = 2;
	aux = 0;
	if(casas== 4 )
		aux = 10000
	if(casas== 2 )
		aux = 100
	
	
	
	if(valor + '' == '')
		return 0;
//	valor =  campo.value;
	valor = valor.replace( ".", "" );
	valor = valor.replace( ",", "." );
	valor = valor.replace( "R", "" );
	valor = valor.replace( "$", "" );
	valor = valor.replace( " ", "" );
	valor = parseFloat(valor);
	valor = Math.round(valor*aux)/aux;
	return valor;	
}
function FComma(SS){ 
  var T='', S=String(SS), L=S.length-1, C, j, P = S.indexOf('.')-1
  if (P<0) P=L
  for (j=0; j<=L; j++) {
    T+=C=S.charAt(j)
    if ((j < P) && ((P-j)%3 == 0) && (C != '-')) T+=',' }
  return T 
}

//********************** comobox *************************************
function alteraItem(valor, comboFilho, vetor){//valor do pai, objeto combo filho e o vetor do filho (ex: var combo1=new Array(); combo1[0]= new Array(0,new Option("Selecione",""));combo1[1]= new Array(1,new Option('x',1));
	limpaCombo(comboFilho);
	comboFilho.add(vetor[0][1]);//objeto
	if(valor != ""){//se tiver valor no pai
		for (i=0;i<vetor.length;i++){
			if(vetor[i][0]==valor)
				comboFilho.add(vetor[i][1]);//objeto
		}
	}
}
function limpaCombo(itens){
	while(itens.options.length)
		itens.remove(0);
}
//-------------------------------------

function PagoRegistros(){
	return confirm('Serão creditadas todas as parcelas selecionadas!');
}
function desmarcar(aux){
	if(aux.checked == true)
		aux.checked =false;
}



function valida_form(formulario, v){
	var cor = "#00FFFF";
	var errormsg = "";
	var erro = false;
	var errormsg_aux = ""
	errormsg ='O seguintes campos são obrigatorio o preenchimento: \n'; 
	for(var i = 0; i < v.length ; i++){
		var aux = v[i];

		var campos = aux[0];
		
		var erro_campo = false
		for(var y=0; y < campos.length; y++){
			if(formulario[campos[y]]==undefined){
				alert('Erro no campo '+campos[y]);
				return false;
			}
			if (formulario[campos[y]].value == "") {
				 formulario[campos[y]].style.backgroundColor = cor;
				erro_campo= true;
			}else{
				formulario[campos[y]].style.backgroundColor = "";
			}
			
		}
		if (erro_campo== true) {
//			 formulario[aux[0]].style.backgroundColor = cor;
			 errormsg += " ---> " + aux[1] + "  \n";
			 erro = true;
		}
		if(aux[2] != undefined){
			if(aux[2] =='password' && formulario[campos[0]].value != formulario[campos[0] + '1'].value){
				errormsg_aux += "\n A senha e a confirmação precisam ser iguais!  ";
				erro = true;
				 formulario[campos[0] ].style.backgroundColor = cor;
			}
			if(aux[2] =='email' && !isEmail(formulario[campos[0]])){
				errormsg_aux += "\n O e-mail está incorreto! ";
				erro = true;
				 formulario[campos[0] ].style.backgroundColor = cor;
			}

			if(aux[2] =='cnpj' && !isCNPJ(formulario[campos[0] ])){
				errormsg_aux += "\n O CNPJ é incorreto! ";
				erro = true;
				 formulario[campos[0] ].style.backgroundColor = cor;
		}

			if(aux[2] =='cpf' && !isCPF(formulario[campos[0]])){
				errormsg_aux += "\n O CPF é incorreto! ";
				erro = true;
				 formulario[campos[0] ].style.backgroundColor = cor;
			}

		}


	}
	errormsg += errormsg_aux
  if(erro){
      window.alert(errormsg);
	  return false;
  }else{
      formulario.submit();
  }
	
}


function retorna_valida_form(formulario, v){
	var cor = "#00FFFF";
	var errormsg = "";
	var erro = false;
	var errormsg_aux = ""

	errormsg ='O seguintes campos são obrigatorio o preenchimento: \n'; 
	for(var i = 0; i < v.length ; i++){
		var aux = v[i];

		var campos = aux[0];
		
		var erro_campo = false
		for(var y=0; y < campos.length; y++){
			if(formulario[campos[y]]==undefined){
				alert('Erro no campo '+campos[y]);
				return false;
			}
			if (formulario[campos[y]].value == "") {
				 formulario[campos[y]].style.backgroundColor = cor;
				erro_campo= true;
			}
			
		}
		if (erro_campo== true) {
			 formulario[aux[0]].style.backgroundColor = cor;
			 errormsg += " ---> " + aux[1] + "  \n";
			 erro = true;
		}else{
			 formulario[aux[0]].style.backgroundColor = "";
		}
		if(aux[2] != undefined){
			if(aux[2] =='password' && formulario[campos[0]].value != formulario[campos[0] + '1'].value){
				errormsg_aux += "\n A senha e a confirmação precisam ser iguais!  ";
				erro = true;
				 formulario[campos[0] ].style.backgroundColor = cor;
			}
			if(aux[2] =='email' && !isEmail(formulario[campos[0]])){
				errormsg_aux += "\n O e-mail está incorreto! ";
				erro = true;
				 formulario[campos[0] ].style.backgroundColor = cor;
			}

			if(aux[2] =='cnpj' && !isCNPJ(formulario[campos[0] ])){
				errormsg_aux += "\n O CNPJ é incorreto! ";
				erro = true;
				 formulario[campos[0] ].style.backgroundColor = cor;
		}

			if(aux[2] =='cpf' && !isCPF(formulario[campos[0]])){
				errormsg_aux += "\n O CPF é incorreto! ";
				erro = true;
				 formulario[campos[0] ].style.backgroundColor = cor;
			}

		}


	}
	errormsg += errormsg_aux
  if(erro){
      window.alert(errormsg);
	  return false;
  }else{
      return true; 
  }
	
}

