// JavaScript Document
function AjustarCEP(input){
	if ((event.keyCode < 48) || (event.keyCode > 57)){
		event.returnValue = false;
		}
    else {
    		if (input.value.length==5){
				input.value=input.value + "-" ;
			}
		}
}

function AjustarMoeda(input){
	if (((event.keyCode >= 48) && (event.keyCode <= 57)) || (event.keyCode == 44)) {
		event.returnValue = true;
	} else {
	    event.returnValue = false; 		
	} 
}

function AjustarInteiro(campo) 
{ 
	if ((event.keyCode >= 48) && (event.keyCode <= 57)){
		event.returnValue = true;
	} else {
		event.returnValue = false;
	}
} 

function AjustarData(input){
	if ((event.keyCode < 48) || (event.keyCode > 57)){
		event.returnValue = false;
		}
    else {
    		if ((input.value.length==2) || (input.value.length==5)){
				input.value=input.value + "/" ;
			}
		}
}

function AjustarHora(input){
	if ((event.keyCode < 48) || (event.keyCode > 57)){
		event.returnValue = false;
		}
    else {
    		if (input.value.length==2){
				input.value=input.value + ":" ;
			}
		}
}

function AjustarCPF(input)
{
	if ((event.keyCode<48)||(event.keyCode>57)){
		event.returnValue = 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 + "-" ;
		}
	}
}

function AjustarCNPJ(input)
{
	if (input.value.lenght > 18){
		return false;	
	}
	if ((event.keyCode<48)||(event.keyCode>57)){
		event.returnValue = false;
	}
    else {
    	if((input.value.length==2) || (input.value.length==6)) {
			input.value=input.value + "." ;
		} else {
			if(input.value.length==10) {
				input.value=input.value + "/" ;
			} else {
				if (input.value.length==15) {
					input.value=input.value + "-" ;					
				}
			}
		}
	} 
}

function validarHora(valor) {
	if (valor.length < 5) {
		return false;
	}
	if (valor.indexOf(":") == 2 ){
		var hh = valor.slice(0,valor.indexOf(":"));
		var mm = valor.slice(valor.indexOf(":")+1,5);
		if (hh >= 0 && hh < 24) {
			if (mm >= 0 && mm < 60) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function validarData(valor){
	if (valor.length < 10) {
		return false;
	}
	if (valor.indexOf("/") == 2 && valor.lastIndexOf("/") == 5) {
		var DD = valor.slice(0,valor.indexOf("/"));
		var MM = valor.slice(valor.indexOf("/")+1,valor.lastIndexOf("/"));
		var YY = valor.slice(valor.lastIndexOf("/")+1,10);
		if (YY != 0){
			if (YY % 4 !=0){
				var DiaFinal = Array(31,28,31,30,31,30,31,31,30,31,30,31);
			} else {
				var DiaFinal = Array(31,29,31,30,31,30,31,31,30,31,30,31);		
			}
			if (MM > 0 && MM <= 12){
				if (DD > 0 && DD <= DiaFinal[MM-1]) {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} else {
			return false;
		}
	} else {
		return false;
	}
}


function validarEmail(email){
	if (email.length != 0) {
		if (email.indexOf(".") != -1 || email.indexOf("@") != -1) {
			if (email.lastIndexOf(".") > email.indexOf("@")) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	} else {
		return false;
	}
}


function validaTecla(campo) 
{ 
	if ((event.keyCode<48) || (event.keyCode>57)){
		event.returnValue = false;
	} else {
		campo.value=campo.value;
	}
} 

function FormataCNPJ(el) 
{ 
         vr = el.value; 
         tam = vr.length; 


      if ( vr.indexOf(".") == -1 ) 
      { 
      if ( tam <= 2 ) 
              el.value = vr; 
      if ( (tam > 2) && (tam <= 6) ) 
              el.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, tam ); 
      if ( (tam >= 7) && (tam <= 10) ) 
              el.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/'; 
      if ( (tam >= 11) && (tam <= 18) ) 
             el.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/' + vr.substr( 8, 4 ) + '-' + vr.substr( 12, 2 ); 
      } 
      return true; 
}

function FormataStringCNPJ(el) 
{ 
         vr = el; 
         tam = vr.length; 
		var retorno = "";

      if ( vr.indexOf(".") == -1 ) 
      { 
      if ( tam <= 2 ) 
              retorno = vr; 
      if ( (tam > 2) && (tam <= 6) ) 
              retorno = vr.substr( 0, 2 ) + '.' + vr.substr( 2, tam ); 
      if ( (tam >= 7) && (tam <= 10) ) 
              retorno = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/'; 
      if ( (tam >= 11) && (tam <= 18) ) 
             retorno = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/' + vr.substr( 8, 4 ) + '-' + vr.substr( 12, 2 ); 
      } 
      return retorno; 
}



function FormataCPF( el ) 
{ 
    vr = el.value; 
    tam = vr.length; 

    if ( vr.indexOf(".") == -1 ) 
    { 
        if ( tam <= 3 ) 
            el.value = vr; 

        if ( (tam > 3) && (tam <= 7) ) 
            el.value = vr.substr( 0, 3 ) + '.' + vr.substr( 4, tam ); 

        if ( (tam >= 8) && (tam <= 11) ) 
            el.value = vr.substr( 0, 3 ) + '.' + vr.substr( 3, 3 ) + '.' + vr.substr( 5, 3 ) + '-'; 

        if ( (tam >= 11) && (tam <= 18) ) 
            el.value = vr.substr( 0, 3 ) + '.' + vr.substr( 3, 3 ) + '.' + vr.substr( 6, 3 ) + '-' + vr.substr( 9, 2 ); 
    } 

    return true; 

}

function validarCPF (CPF) {
	
	CPF = LimpaFormatoStr(CPF);
	
	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;
	return true;
}

function validarCNPJ_old(CNPJ) {
//	CNPJ = obCNPJ.value;
//	CNPJ = document.validacao.CNPJID.value;
	erro = new String;
	
	if (CNPJ.length < 18) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
	
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
		if (erro.length == 0) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
	}
	//substituir os caracteres que nao sao numeros
	if(document.layers && parseInt(navigator.appVersion) == 4){
		x = CNPJ.substring(0,2);
		x += CNPJ.substring(3,6);
		x += CNPJ.substring(7,10);
		x += CNPJ.substring(11,15);
		x += CNPJ.substring(16,18);
		CNPJ = x; 
	} else {
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace("-","");
		CNPJ = CNPJ.replace("/","");
	}
	
	var nonNumbers = /\D/;
	
	if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n"; 
	
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];

	for (i=0; i<12; i++){
		a[i] = CNPJ.charAt(i);
		b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
	
	b = 0;
	for (y=0; y<13; y++) {
		b += (a[y] * c[y]); 
	}
	
	if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
		erro +="Digito verificador com problema!";
	}
	
	if (erro.length > 0){
//		alert(erro);
		return false;
	} else {
//		alert("CNPJ valido!");
		return true;
	}
return true;
}


function validarCNPJ(CNPJ) {
//	CNPJ = obCNPJ.value;
//	CNPJ = document.validacao.CNPJID.value;
	erro = new String;
	
	if (CNPJ.length < 18) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
	
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
		if (erro.length == 0) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
	}
	//substituir os caracteres que nao sao numeros
	if(document.layers && parseInt(navigator.appVersion) == 4){
		x = CNPJ.substring(0,2);
		x += CNPJ.substring(3,6);
		x += CNPJ.substring(7,10);
		x += CNPJ.substring(11,15);
		x += CNPJ.substring(16,18);
		CNPJ = x; 
	} else {
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace("-","");
		CNPJ = CNPJ.replace("/","");
	}
	
	var nonNumbers = /\D/;
	
	if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n"; 
	
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];

	for (i=0; i<12; i++){
		a[i] = CNPJ.charAt(i);
		b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
	
	b = 0;
	for (y=0; y<13; y++) {
		b += (a[y] * c[y]); 
	}
	
	if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
		erro +="Digito verificador com problema!";
	}
	
	if (erro.length > 0){
//		alert(erro);
		return false;
	} else {
//		alert("CNPJ valido!");
		return true;
	}
return true;
}


function AbreJanela(arq, nomejanela, width, height, scroll) {
	    var URL = arq
		var NM = nomejanela
	    var W = width
	    var H = height
	    var S = scroll
		var Wpopupsize = (W/2);
		var Hpopupsize = (H/2);
		var CenterPopUpX = (screen.width / 2) - (Wpopupsize);
		var CenterPopUpY = (screen.height / 2) - (Hpopupsize);
		var pos = "left=" + CenterPopUpX + ",top=" + CenterPopUpY;
		var janela = window.open("" + URL + "", "" + NM + "", "width=" + W + ",height=" + H + ",scrollbars=" + S + "," + pos);
		janela.focus()
}

function mOvr(src,clrOver) {
	if (!src.contains(event.fromElement)) {
		src.bgColor = clrOver;
	}
}

function mOut(src,clrIn) {
	if (!src.contains(event.toElement)) {
		src.bgColor = clrIn;
	}
}

function mOvrs(src,styleOver) {
	if (!src.contains(event.fromElement)) {
		src.className = styleOver;		
	}
}

function mOuts(src,styleIn) {
	if (!src.contains(event.toElement)) {
		src.className = styleIn;
	}
}

function mClks(src, styleClk) {
	if (!src.contains(event.toElement)) {
		src.className = styleClk;
	}
}

var styleMantem = new Array();
var styleTemp;

function menuOvr(src, stOver, indice) {
	mOvrs(src, stOver);	
}

function menuOut(src, stIn, indice) {
	if (verificaIndice(indice, false)) {
		styIn = styleTemp;
	} else {
		styIn = stIn;
	}
	mOuts(src, styIn);
}

function menuClick(src, stClk, indice) {
	mClks(src, stClk);
	
	if (!verificaIndice(indice, true)) {
		styleMantem.push(new Array(indice, stClk));
 	} else {
		removeIndice(indice);
	}
}

function verificaIndice(vIndice, isClique) {
	var ret = false;
	var j;
	
	for (j = 0; j < styleMantem.length; j++) {
		
		var style = styleMantem[j];
		
		if (style[0] == vIndice) {
			styleTemp = style[1];
			ret = true;
		} else {
			if (isClique && (style[0].substring(0,1) == vIndice.substring(0,1))){
				document.getElementById(style[0]).className = style[1].substring(0,style[1].indexOf("Clk"));
				styleMantem.splice(j,1);
				return false;
			}
		}
	}
	
	return ret;
}

function removeIndice(vIndice){
	for (j = 0; j < styleMantem.length; j++) {
		
		var style = styleMantem[j];
		
		if (style[0] == vIndice) {
			styleMantem.splice(j,1);
		}
	}	
}

function mascara_data(data){ 
	  var mydata = ''; 
	  mydata = mydata + data; 
	  if (mydata.length == 2){ 
		  mydata = mydata + '/'; 
		  document.forms[0].data.value = mydata; 
	  } 
	  if (mydata.length == 5){ 
		  mydata = mydata + '/'; 
		  document.forms[0].data.value = mydata; 
	  } 
	  if (mydata.length == 10){ 
		  verifica_data(); 
	  } 
  } 
           
  function verifica_data () { 

	dia = (document.forms[0].data.value.substring(0,2)); 
	mes = (document.forms[0].data.value.substring(3,5)); 
	ano = (document.forms[0].data.value.substring(6,10)); 

	situacao = ""; 
	// verifica o dia valido para cada mes 
	if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) { 
		situacao = "falsa"; 
	} 

	// verifica se o mes e valido 
	if (mes < 01 || mes > 12 ) { 
		situacao = "falsa"; 
	} 

	// verifica se e ano bissexto 
	if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { 
		situacao = "falsa"; 
	} 

	if (document.forms[0].data.value == "") { 
		situacao = "falsa"; 
	} 

	if (situacao == "falsa") { 
		alert("Data inválida!"); 
		document.forms[0].data.focus(); 
	} 
  } 

  function mascara_hora(hora){ 
	  var myhora = ''; 
	  myhora = myhora + hora; 
	  if (myhora.length == 2){ 
		  myhora = myhora + ':'; 
		  document.forms[0].hora.value = myhora; 
	  } 
	  if (myhora.length == 5){ 
		  verifica_hora(); 
	  } 
  } 
           
function verifica_hora(){ 
  hrs = (document.forms[0].hora.value.substring(0,2)); 
  min = (document.forms[0].hora.value.substring(3,5)); 
  
  alert('hrs '+ hrs); 
  alert('min '+ min); 
   
  situacao = ""; 
  // verifica data e hora 
  if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59)){ 
	  situacao = "falsa"; 
  } 
   
  if (document.forms[0].hora.value == "") { 
	  situacao = "falsa"; 
  } 

  if (situacao == "falsa") { 
	  alert("Hora inválida!"); 
	  document.forms[0].hora.focus(); 
  } 
}

function LimpaFormato(objeto){
	objeto.value = 	objeto.value.replace(",","");
	objeto.value = 	objeto.value.replace("/","");
	objeto.value = 	objeto.value.replace("-","");
	objeto.value =  objeto.value.replace(".","");
	objeto.value =  objeto.value.replace(".","");	
}

function LimpaFormatoStr(strnoformat){
	strnoformat = strnoformat.replace(",","");
	strnoformat = strnoformat.replace("/","");
	strnoformat = strnoformat.replace("-","");
	strnoformat = strnoformat.replace(".","");
	strnoformat = strnoformat.replace(".","");	
	return strnoformat;
}


function MudaTamMaximo(objeto, tamanho){
	objeto.maxLength = tamanho;
}

function LimitaTamanho(objeto, tamanho){
	
	if (objeto.value.length >= tamanho){
		objeto.value = objeto.value.substring(0,tamanho);
	}
}

function aplicarAcentuacao(str){
	
	if (str.length > 0) {
		var acentos = Array('á','é','í','ó','ú','À','È','Ì','Ò','Ù','Á','É','Í','Ó','Ú','Ç','ç','ã','õ','Õ','Ã','ô','Ô','Â','â','î','Î','Û','û','ê','Ê');
		var cod_acentos = Array("&#225;","&#233;","&#237;","&#243;","&#250;","&#192;","&#200;","&#204;","&#210;","&#217;","&#193;","&#201;","&#205;","&#211;","&#218;","&#199;","&#231;","&#227;","&#245;","&#213;","&#195;", "&#244;", "&#212;","&#194;","&#226;","&#238;","&#206;","&#219;","&#251;","&#234;","&#202;");
	
		for (i = 0; i < acentos.length; i++) {
			while (str.lastIndexOf(cod_acentos[i]) != -1) {
				str = str.replace(cod_acentos[i],acentos[i]);
			}
		}
	}
	return str;
}

var i_obHttp = fn_getHTTPObject(); // We create the HTTP Object

function verificaRegistro(Url, NomeCampo, Tabela, CampoRetorno, Valor, Filtro)
{
	var url = Url + "&NomeCampo=" + NomeCampo + "&Tabela=" + Tabela + "&Valor=" + Valor + "&CampoRetorno=" + CampoRetorno + "&Filtro=" + Filtro;
	obHttp.open("GET", url, false);
	obHttp.send(null);
	return obHttp.responseText;
}


function executaURL(vURL) {
	var url = vURL;
	i_obHttp.open("GET", url, false);
	i_obHttp.send(null);
	return i_obHttp.responseText;
}

function fn_getHTTPObject() {
  var xmlhttp;

  /*@cc_on
  @if (@_jscript_version >= 5)
	try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
		xmlhttp = false;
	  }
	}
  @else
  xmlhttp = false;
  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	try {
	  xmlhttp = new XMLHttpRequest();
	} catch (e) {
	  xmlhttp = false;
	}
  }
  return xmlhttp;
}

function retiraFormato(vlrDesformatar) {
	while (vlrDesformatar.indexOf('.') != -1) {
		vlrDesformatar = vlrDesformatar.replace('.','');
	}
	vlrDesformatar = vlrDesformatar.replace(',','.');
	return vlrDesformatar;
}

//Verifica se a data inicial é maior que a data final  
function ComparaData(DtHrInicio, DtHrTermino){  
	 var AgeDtHrIniciodt   		= document.getElementById(DtHrInicio).value;
     var AgeDtHrTerminodt   	= document.getElementById(DtHrTermino).value;
     var DiaInicial        		= AgeDtHrIniciodt.substring(0,2);
     var DiaFinal        		= AgeDtHrTerminodt.substring(0,2);
     var MesInicial      		= AgeDtHrIniciodt.substring(3,5);
     var MesFinal       	    = AgeDtHrTerminodt.substring(3,5);
     var AnoInicial      		= AgeDtHrIniciodt.substring(6,10);
     var AnoFinal        		= AgeDtHrTerminodt.substring(6,10);  

	if(AnoFinal != ""){
		if(AnoInicial > AnoFinal) {  
			 alert("A data Inicial deve ser menor que a data final.");   
			 document.getElementById(DtHrTermino).focus();
			 return false;  
		}else{  
			if(AnoInicial == AnoFinal){  
				if(MesInicial >  MesFinal){  
					alert("A data inicial deve ser menor que a data final.");  
					document.getElementById(DtHrTermino).focus();  
					return false;  
				}else{  
					if(MesInicial == MesFinal){  
						if(DiaInicial > DiaFinal){  
							alert("A data inicial deve ser menor que a data final.");  
							document.getElementById(DtHrTermino).focus();  
							return false;  
						 }  
					 }  
				 }  
			 }  
		 }  
	}
	 return true;
 }

function limita(formulario) { 
	var maximo = 100; 
	if(formulario.value.length > maximo) { 
		return false; 
	} 
	else 
		{return true;}
} 


//formata telefone com "()" exemplo onkeyup="formataTel(event)"
function formataTel(evt) {
var obj;
    if (navigator.appName.indexOf("Netscape") != -1) obj = evt.target;
    else obj = evt.srcElement;
    qtd = obj.value.length;
    if (qtd == 2) obj.value = "("+obj.value+")";
    if (qtd == 7) obj.value = obj.value+"-";
    if (qtd == 12 && evt.keyCode == 8) {
    character = tiraChar(obj.value, "-");
        obj.value = character.substring(0,7)+"-"+character.substring(7,12);
    }
    if (qtd == 13) {
    character = tiraChar(obj.value, "-");
    obj.value = character.substring(0,8)+"-"+character.substring(8,12);
}
}
function tiraChar(texto, caracter) {
var ret;
    for (i=0; i < texto.length; i++) {
    if (texto.substring(i, i+1) == caracter)
            ret = texto.substring(0, i)+texto.substring(i+1, texto.length);
    }
    return ret;
}

