/*|---------------------------------->>  <<----------------------------------|

Include para ser usado na estrutura padrão dos sites. deve chamado 
no arquivo head.inc.php.

Sr. Programador, todas as funções em javascript que serão utilizadas no site
deverão serem declaradas apenas neste arquivo.

|---------------------------------->>  <<----------------------------------|*/
// Função abre nova janela
// utilização: 											PROPRIEDADES
//				abrejanela('PAGINA.htm','TITULO','scrollbars=yes,resizable=no,width=500,height=400')"

function trimAll(sString) 
{
  while (sString.substring(0,1) == ' ')
   {
     sString = sString.substring(1, sString.length);
   }
  while (sString.substring(sString.length-1, sString.length) == ' ')
   {
     sString = sString.substring(0,sString.length-1);
   }
  return sString;
}


function rightTrim(sString) 
{
   while (sString.substring(sString.length-1, sString.length) == ' ')
    {
      sString = sString.substring(0,sString.length-1);
    }
      return sString;
}



function leftTrim(sString) 
{
   while (sString.substring(0,1) == ' ')
     {
       sString = sString.substring(1, sString.length);
     }
   return sString;
}


function formatar(src, mask) 
{
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
if (texto.substring(0,1) != saida) 
  {
	src.value += texto.substring(0,1);
  }
}

function abrejanela(url,nome,propriedades){ 
   window.open(url,nome,propriedades);
}

//Checa o campo E-Mail
function verifica_mail(email){
	var campo = email;
	//se a "@" não existe e é o primeiro caractere 
	if (campo.indexOf("@") < 1) return false
	//se o caracter antes da "@" é "." 
	if(campo.charAt(campo.indexOf("@")-1) == ".") return false;
	//se a última incidência de "." está antes da @ 
	if (campo.lastIndexOf(".") <= campo.indexOf("@")) return false;
	//se o último caracter é ponto,
	if (campo.lastIndexOf(".")  == (campo.length-1)) return false; 
	
	return true;
}

function val_cnpj(numero) {
	//numero = numero1.value;
	dig_1 = 0;
	dig_2 = 0;
	controle_1 = 5;
	controle_2 = 6;

if ( (numero.length != 18)  || (numero.substring(2, 3) != ".") || (numero.substring(6, 7) != ".") || (numero.substring(10, 11) != "/") || (numero.substring(15, 16) != "-") ) {
     return false;
}else{ 
  	   numero = (numero.substring(0, 2)) + "" + (numero.substring(3, 6)) + "" + (numero.substring(7, 10))+ "" + (numero.substring(11, 15)) + "" + (numero.substring(16, 18)) 
	   for ( i=0 ; i < 12 ; i++) {
	        dig_1 = dig_1 + parseFloat(numero.substring(i, i+1) * controle_1);
	          controle_1 = controle_1 - 1;
	        if (i == 3) {
	           controle_1 = 9;
	        }
	   }
	   
	   resto = dig_1 % 11;
	   dig_1 = 11 - resto;
	   
	   if ((resto == 0) || (resto == 1)){
	        dig_1 = 0;
	   }
	   
	   for ( i=0 ; i < 12 ; i++) {
	        dig_2 = dig_2 + parseInt(numero.substring(i, i+1) * controle_2);
	          controle_2 = controle_2 - 1;
	        if (i == 4) {
	           controle_2 = 9;
	        }
	   }
	   
	   dig_2 = dig_2 + (2 * dig_1);
	   resto = dig_2 %11;
	   dig_2 = 11 - resto;
	   
	   if ((resto == 0) || (resto == 1)){
	        dig_2 = 0;
	   }
	   
	   dig_ver = (dig_1 * 10) + dig_2;
	   
	   if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {
	          return false;
	   }
 }
 return true;
}

function FormataCPF(Campo, teclapres){

	var tecla = teclapres.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1 ;

	
	if (tecla != 9 && tecla != 8){
		if (tam >= 10 && tam < 12) {
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);
		} else if (tam >= 7 && tam < 10) {
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
		} else if (tam > 3 && tam < 7) {
			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
		}
	}
}

function FormataCNPJ(Campo, teclapres){

	var tecla = teclapres.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1 ;

	
	if (tecla != 9 && tecla != 8){
		if (tam > 2 && tam < 6)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
		if (tam >= 6 && tam < 9)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
		if (tam >= 9 && tam < 13)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
		if (tam >= 13 && tam < 15)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
		}
}

//Valida o cpf 
function val_cpf(numero2) {
  dig_1 = 0;
  dig_2 = 0;
  controle_1 = 10;
  controle_2 = 11;
  lsucesso = 1;
  
  var numero = numero2.substr(0,3) + numero2.substr(4,3) + numero2.substr(8,3) + numero2.substr(12,2);
  
  if (numero == "00000000000" || numero == "11111111111" || numero == "22222222222" || numero == "33333333333" || numero == "44444444444" || numero == "55555555555" || numero == "66666666666" || numero == "77777777777" || numero == "88888888888" || numero == "99999999999") {
  	return false;
  }
  
  if ((numero.length != 11) && (numero.length != 0)) {
     return false;
  }
  else {
     for ( i=0 ; i < 9 ; i++) {
        dig_1 = dig_1 + parseInt(numero.substring(i, i+1) * controle_1);
          controle_1 = controle_1 - 1;
     }
    
     resto = dig_1 % 11;
     dig_1 = 11 - resto;
     if ((resto == 0) || (resto == 1)) {
          dig_1 = 0;
     }
     for ( i=0 ; i < 9 ; i++) {
          dig_2 = dig_2 + parseInt(numero.substring(i, i + 1) * controle_2);
        controle_2 = controle_2 - 1;
     }
     dig_2 = dig_2 + 2 * dig_1;
     resto = dig_2 % 11;
     dig_2 = 11 - resto;
     if ((resto == 0) || (resto == 1)) {
        dig_2 = 0;
     }
     dig_ver = (dig_1 * 10) + dig_2;
     if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {
          return false;
        }
  }

return true;
}

function abre_impressao(url) {
	window.open(url, "impressao", "width=600,height=450");
}
//Utilização: onkeypress="evita_letra(event)" onKeydown="FormataHora('NOMEDOCAMPO','NOMEDOFORM',event)" maxlength="5"
function FormataHora(campo,formname,teclapres){
	var tecla = teclapres.keyCode;
	vr = document[formname][campo].value;
	vr = vr.replace( ":", "" );
	tam = vr.length + 1;
	
	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 ){
			document[formname][campo].value = vr.substr( 0, tam - 2  ) + ':' + vr.substr( tam - 2, tam );
		}
	}
}
// FUNÇÃO QUE AUTOCOMPLETA DATA COM '/'
// COMO UTILIZAR:    onkeydown="FormataData(this.name,this.form.name,event);" 
function FormataData(campo,formname,teclapres) { // Máscara para os campos de data
	var tecla = teclapres.keyCode;
	vr = document[formname][campo].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 )
			document[formname][campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			document[formname][campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
	}
}
// COMO UTILIZAR: onkeypress="evita_letra2(event)"
// Não permite digitar letras em um input type=text
function evita_letra2(tecla) {
	if (tecla.keyCode < 48 || tecla.keyCode > 57) 
		tecla.returnValue = false;
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function ToggleAll(checked) {
    len = document.listagem.elements.length;
    var i = 0;
    for(i = 0; i < len; i++) {
        document.listagem.elements[i].checked = checked;
    }
}

function in_array(s,a) {
  for (i = 0; i < a.lenght; i++) {
	  /*if (a[i] == s) {
	  	return true;
	  }*/
	  alert(a[i]+'=='+s.name);
  }
  //return false;
}

function valida(ffff,ele)
{//começa valida
  
  var c=ele.split(',');
  for(x = 0; x<ffff.elements.length; x++)//varre o formulário
  {  
    //alert(c[x]);//atriz com os elementos
	//e = document.getElementById(c[x]);
	
	for(j=0;j<c.length;j++)//varre os obrigatórios
	{
	   var formul=ffff.elements[x];
	   //alert(formul);
       if(formul.name==c[j] &&  leftTrim(formul.value)=='')//se tem o elemento no formulário
	   {
	      alert('O campo '+document.getElementById(formul.name+"_label").innerHTML+' é obrigatorio');
		  document.getElementById(formul.name).focus();
		  return;
		  
	      
	   }
	}//fim for
	
  }//fim for
  ffff.submit();
}//fim valida

// HINT
// Example:
// onMouseOver="hint('tool tip text here')";
// onMouseOut="hint()";
// -or-
// onMouseOver="hint('more good stuff', '#FFFF00', 'orange')";
// onMouseOut="hint()"; 
/*

MOVE this to the <body>:
<div id="hintLayer" style="position:absolute; visibility: hidden"></div>
<script language="JavaScript"><!--
initToolTips(); //--></script>
*/
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;
offsetX = 0;
offsetY = 20;
var hintSTYLE="";
function initToolTips()
{
  if(ns4||ns6||ie4)
  {
    if(ns4) hintSTYLE = document.hintLayer;
    else if(ns6) hintSTYLE = document.getElementById("hintLayer").style;
    else if(ie4) hintSTYLE = document.all.hintLayer.style;
    if(ns4) document.captureEvents(Event.MOUSEMOVE);
    else
    {
      hintSTYLE.visibility = "visible";
      hintSTYLE.display = "none";
    }
    document.onmousemove = moveToMouseLoc;
  }
}
function hint(msg, fg, bg)
{
  if(hint.arguments.length < 1) // hide
  {
    if(ns4) hintSTYLE.visibility = "hidden";
    else hintSTYLE.display = "none";
  }
  else // show
  {
    if(!fg) fg = "#777777";
    if(!bg) bg = "#FFFFFF";
    var content =
    '<table border="0" cellspacing="0" cellpadding="1" bgcolor="' + fg + '"><td>' +
    '<table border="0" cellspacing="0" cellpadding="1" bgcolor="' + bg + 
    '"><td align="center"><font face="sans-serif" color="' + fg +
    '" size="-2">&nbsp\;' + msg +
    '&nbsp\;</font></td></table></td></table>';
    if(ns4)
    {
      hintSTYLE.document.write(content);
      hintSTYLE.document.close();
      hintSTYLE.visibility = "visible";
    }
    if(ns6)
    {
      document.getElementById("hintLayer").innerHTML = content;
      hintSTYLE.display='block'
    }
    if(ie4)
    {
      document.all("hintLayer").innerHTML=content;
      hintSTYLE.display='block'
    }
  }
}
function moveToMouseLoc(e)
{
  if(ns4||ns6)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else
  {
    x = event.x + document.body.scrollLeft;
    y = event.y + document.body.scrollTop;
  }
  hintSTYLE.left = x + offsetX;
  hintSTYLE.top = y + offsetY;
  return true;
}

// FUNÇÃO DO AJAX PRA BUSCAR PROCESSOS

function buscador(form_name, moddiv, page) {
	var chave = document.getElementById('chave').value;
	var valmenor = document.getElementById('valmenor').value;
	var valmaior = document.getElementById('valmaior').value;
	ajax = ajaxInit();
	if(ajax){
 		if(chave=='Palavra-chave'){
			chave = '';
		}
		
				
	/*	pagina = "http://neoclic:8080/janeresina/site/modulos/processos/listagem.php?tp_processo="+document.getElementById('tp_processo').value+"&chave="+chave;*/
		
		pagina = "http://www.resinamarcon.com.br/modulos/processos/listagem.php?tp_processo="+document.getElementById('tp_processo').value+"&chave="+chave+"&valmenor="+valmenor+"&valmaior="+valmaior+"&pagina="+page;
		
		/*pagina = "http://www.resinamarcon.com.br/modulos/processos/listagem.php?tp_processo="+document.getElementById('tp_processo').value+"&chave="+chave+"&pagina="+page;*/
		
		ajax.open("GET", pagina, true);
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 1){ 
				document.getElementById(moddiv).innerHTML = "<h2>Carregando...</h2>";
			} 
			if(ajax.readyState == 4){
				if(ajax.status == 200){
					document.getElementById(moddiv).innerHTML = ajax.responseText;
				} else {
					alert(ajax.statusText);
			}
		}
	}
	ajax.send(null);
	}
}

// FUNÇÃO QUE FORMATA CAMPO DE ACORDO COM A ESPECIFICAÇÃO '/'
// COMO UTILIZAR:    onKeyPress="return txtBoxFormat(document.form1, 'telefone', '(99) 9999-9999', event);"
function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

if(document.all) { // Internet Explorer
nTecla = evtKeyPress.keyCode; }
else if(document.layers) { // Nestcape
nTecla = evtKeyPress.which;
}

sValue = objForm[strField].value;

// Limpa todos os caracteres de formatação que
// já estiverem no campo.
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( " ", "" );
sValue = sValue.toString().replace( " ", "" );
sValue = sValue.toString().replace( ":", "" );
fldLen = sValue.length;
mskLen = sMask.length;

i = 0;
nCount = 0;
sCod = "";
mskLen = fldLen;

while (i <= mskLen) {
bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
bolMask = bolMask || (sMask.charAt(i) == ":")

if (bolMask) {
sCod += sMask.charAt(i);
mskLen++; }
else {
sCod += sValue.charAt(nCount);
nCount++;
}

i++;
}

objForm[strField].value = sCod;

if (nTecla != 8) { // backspace
if (sMask.charAt(i-1) == "9") { // apenas números...
return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
else { // qualquer caracter...
return true;
} }
else {
return true;
}
}