function limpa_string(S)
{
	// Deixa so' os digitos no numero
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";

	for (var i=0; i<S.length; i++)
	{
		digito = S.charAt(i);
		if (Digitos.indexOf(digito)>=0) 
			temp=temp + digito
	}
	return temp;
}

function Valida_Fone(campo)
{
	var aux = new String(campo.value);
	campo.value = "";

	for (i = 0; i < aux.length; i++)
	{
		if (aux.charCodeAt(i) >= 48 && aux.charCodeAt(i) <= 57)
			campo.value = campo.value + aux.charAt(i);
	}
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789,";	//Considerando "," para decimal - ignorando "."
   var IsNumber=true;
   var Char, count = 0;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (Char == ",")
		count += 1;
      if (ValidChars.indexOf(Char) == -1) 
         IsNumber = false;
   }
   if (count > 1)
	IsNumber = false;
   return IsNumber;
}

function IsInteger(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         IsNumber = false;
   }
   return IsNumber;
}

function IsMoney(sText)
{
   var ValidChars = "0123456789,.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         IsNumber = false;
   }
   return IsNumber;
}

function IsAlfabeto(sText)
{
   var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";	//.
   var IsOK=true;
   var Char;

   sText = sText.toUpperCase();
   for (i = 0; i < sText.length && IsOK == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         IsOK = false;
   }
   return IsOK;
   
}

function ValidaEmail(email) 
{
	var posicao_antes = email.indexOf("@")
	var posicao_depois = email.charAt(posicao_antes+1)
	if ((posicao_antes <= 0) || (posicao_depois == "")) {
		return false;
	}
	posicao_antes = email.indexOf(".")
	posicao_depois = email.charAt(posicao_antes+1)
	if ((posicao_antes <= 0) || (posicao_depois == "")) {
		return false;
	}
	return true
}

function ValidaHora(form,campo)
{		
	var hr = document.forms[form].elements[campo].value;						
	hr = hr.replace('-','');
		
	if (!IsNumeric(form,campo))
	{
		return false;		
	}		
	else if (hr.length < 3)
	{
		return false;			
	} 
	else if (hr.indexOf(':') == -1)
	{
		return false;
	}
	else
	{
		var hora    = hr.substring( 0 , hr.indexOf(':') );
		var minu    = hr.substring( hr.indexOf(':') +1 , hr.length );
		hora		= '0' + hora;
		minu		= '0' + minu;
		hora		= hora.substring( hora.length - 2, hora.length);
		minu		= minu.substring( minu.length - 2, minu.length);					
			
		if ( hora > 24 )
		{
			return false;	
		}
			
		if ( hora + minu > 2400 )
		{
			return false;	
		}
		else
		{
			if (hora + minu == 2400)
			{
				document.forms[form].elements[campo].value = '00:00';				
			}	
			else				
			{
				document.forms[form].elements[campo].value = hora + ':' + minu;			
			}	
			return true;		
		}			
	}
}
	
function Trim( str ) 
{
	var resultStr = "";	
	resultStr = TrimLeft(str);
	resultStr = TrimRight(resultStr);	
	return resultStr;
} 

function TrimLeft( str ) 
{
	var resultStr = "";
	var i = len = 0;

	if (str+"" == "undefined" || str == null)	
		return null;

	str += "";

	if (str.length == 0) 
		resultStr = "";
	else 
	{	
		len = str.length;					
  		while ((i <= len) && (str.charAt(i) == " "))
			i++;
  		resultStr = str.substring(i, len);
  	}
				
  	return resultStr;
} 
		

function TrimRight( str ) {
	var resultStr = "";
	var i = 0;

	if (str+"" == "undefined" || str == null)	
		return null;
	str += "";
			
	if (str.length == 0) 
		resultStr = "";
	else 
	{
  		i = str.length - 1;
  		while ((i >= 0) && (str.charAt(i) == " "))
 			i--;			 		
	  		resultStr = str.substring(0, i + 1);
 	} 	
 	return resultStr;  	
} 	

function valida_CPF(s) {
	var i;
	//s = limpa_string(s);
	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 valida_Vazio(s)
{
  return(s.match("^ *$"));
}  

function valida_CNPJ(s)
{
	
	var i;
	s = limpa_string(s);
 
	var c = s.substr(0,12);
	var dv = s.substr(12,2);
	var d1 = 0;
	for (i = 0; i < 12; i++)
		d1 += c.charAt(11-i)*(2+(i % 8));

    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 < 12; i++)
		d1 += c.charAt(11-i)*(2+((i+1) % 8));

	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1)
		return false;

	return true;
}

function validar_data(data){
var dia = parseInt(data.substring(0,2),10);
var mes = parseInt(data.substring(3,5),10);
var ano = parseInt(data.substring(6,10),10);

if (dia <= 31 && mes <=12 && ano >= 1000){
if (data.substring(0,1)=='0' && data.substring(1,2) != '0' ||
data.substring(0,1)!='0'){
if (data.substring(2,3)=="/"){
if (data.substring(3,4)=='0' && data.substring(4,5)!='0' ||
data.substring(3,4)!='0'){
if (data.substring(5,6)=="/"){
if (data.substring(6,7)== '0' ||
data.substring(6,7)=='' && data.substring(7,8)!='0'){
window.alert('O ano que você digitou não existe!');
return false;

if (mes == 2){
if ((dia > 0 ) && (dia <= 29)){
if (dia == 29){
if ((ano % 4) == 0){
return true;
}else{
window.alert('Este dia não existe, certifique - se de que digitou corretamente!');
return false; 
}
}
}else{
window.alert('Este dia não existe, certifique - se de que digitou corretamente!');
return false; 
}
} 
if ((mes == 4)||(mes == 6)||(mes == 9)||(mes == 11)){
if ((dia > 0 ) && (dia <= 30)){
return true;
}else{
window.alert('Este dia não existe, certifique - se de que digitou corretamente!');
return false;
}
}
if ((mes == 1)||(mes == 3)||(mes == 5)||(mes ==7)||(mes == 8)||(mes == 10)||(mes == 12)){
if ((dia > 0) && (dia <= 31)) {
return true;
}else{
window.alert('Este dia não existe, certifique -se de que digitou corretamente!');
return false;
}
}
}
}else{
window.alert('A data foi digitada fora do padrão (dd/mm/aaaa) !');
return false;
}
}else{
window.alert('Você digitou um mês que não existe!');
return false;
}
}else{
window.alert('A data foi digitada fora do padrão(dd/mm/aaaa) !');
return false;
}
}else{
window.alert('Você digitou um dia que não existe!');
return false;
}
}else{
window.alert('O dia e/ou o mês que você digitou não existe, ou Você digitou fora do padrão (dd/mm/aaaa) !');
return false;
}
return true;
}

function mostrar2(campo, f1, f2, f3, itm){
	if (campo.value=='P') {	
		f1.style.height=21;
		f1.style.width=21;
		f2.style.height=16;
		f2.style.width=16;
		f3.style.height=16;
		f3.style.width=16;
		itm.style.height="";
		itm.style.width="";
	}		
	else{
		f1.style.height=0;
		f1.style.width=0;
		f2.style.height=0;
		f2.style.width=0;
		f3.style.height=0;
		f3.style.width=0;
		itm.style.height=0;
		itm.style.width=0;
	}
}
function local(campo, sql,titulo,tipo){
	if (campo.value=='P')
	{
		showModalDialog('../../includes/filtro_personalizado.asp?sql='+sql+'&titulo='+titulo+'&campo='+campo.name+'&tipo='+tipo,self,'dialogWidth:500px;dialogHeight:160px;center:yes;status:no;scroll:no;help:no;resizable:yes');
//		window.open('../../includes/filtro_personalizado.asp?sql='+sql+'&titulo='+titulo+'&campo='+campo.name+'&tipo='+tipo,'teste','width=500px,height=160px,resizable=yes');
	}
}

function del_item(field){
	var count = field.options.length;
	for(i=0;i<count;i++){
		if (field.selectedIndex >= 0){
			field.options[field.selectedIndex] = null;
		}
	}
}

function sel_all(opt){

	var i, j, frm = document.forms[0];
	
	for (i = 0; i < frm.length; i++)
	{
		if (frm[i].type == "select-multiple")
		{
			for(j = 0; j < frm[i].options.length; j++)
				frm[i].options[j].selected = opt;
		}
	}
}

function deletar (par){
	par.options[par.options.selectedIndex]=null;
}

