﻿// JScript File
var popup = null;

function openlink(URL,w,h) {
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'toolbar=0,scrollbars=0,location=0,status=0,menubar=0,resizable=0,width='+w+',height='+h+',left='+LeftPosition+',top='+TopPosition+''
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '"+id+"',settings);");
}

function tamanhoJanela(width, height, top, left){
    this.dlgFeatures =	"dialogHeight:" + height	+	"px;"	+
  	  					"dialogWidth:"	+ width		+	"px;"	+
  	  					"status:no;"	+
  	  					"help:no;"		+
  	  					"center:yes;"	+
  	  					"edge:sunken;"	+
  	  					"scroll:no";

}

function Poup(sURL, width, height, scroll, nome_janela, resizable){
    var popup = null;
    
    var url = sURL + '&rnd=' + Math.round(Math.random() * 899999) + 100000;
    var nome;
    
    if (nome_janela == ''){
         nome = 'popup_stc';
    }else{
        nome = nome_janela.replace(' ', '_');
    }
    
    if (resizable == ''){
         resizable = 'no';
    }
    
    if (popup!=null && !popup.closed){
        popup.focus();
    }else{
     
        LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
        TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
        
        settings = 'height='+ height +',width='+ width +',top=' + TopPosition + ',left='+ LeftPosition + ',scrollbars=' + scroll + ',resizable=' + resizable;
        
        popup = window.open(url, nome.replace(' ', '_'), settings);
        
        //popup = window.open(url,'popup_stc','height=' + height + ',width=' + width);
        popup.focus();
        
        
        
        
    }

}


function PopArquivo(sURL, width, height, scroll, nome_janela, resizable) {
    var popup = null;

    var url = sURL;
    var nome;

    if (nome_janela == '') {
        nome = 'popup_stc';
    } else {
        nome = nome_janela.replace(' ', '_');
    }

    if (resizable == '') {
        resizable = 'no';
    }

    if (popup != null && !popup.closed) {
        popup.focus();
    } else {

        LeftPosition = (screen.width) ? (screen.width - width) / 2 : 0;
        TopPosition = (screen.height) ? (screen.height - height) / 2 : 0;

        settings = 'height=' + height + ',width=' + width + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',resizable=' + resizable;

        popup = window.open(url, nome.replace(' ', '_'), settings);

        //popup = window.open(url,'popup_stc','height=' + height + ',width=' + width);
        popup.focus();

    }

}

function AbrePopupPesquisa(sURL, width, height, txt_id, txt_dsc){
    var popup = null;
    var url = sURL + '&campo_id=' + txt_id + '&campo_dsc=' + txt_dsc;
    
    if (popup!=null && !popup.closed){
        popup.focus();
    }else{
    
        LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
        TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
        
        settings = 'height='+ height +',width='+ width +',top=' + TopPosition + ',left='+ LeftPosition + ',scrollbars=yes' ;
    
        popup = window.open(url, 'STC', settings);
        popup.focus();
    }

}
 
function AbrePopupPesquisa2(sURL, width, height, txt_id, txt_dsc, txt_id2){
    
    var url = sURL + '&campo_id=' + txt_id + '&campo_dsc=' + txt_dsc + '&campo_id2=' + txt_id2;
    
    if (popup!=null && !popup.closed){
        popup.focus();
    }else{
    
        LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
        TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
        
        settings = 'height='+ height +',width='+ width +',top=' + TopPosition + ',left='+ LeftPosition + ',scrollbars=' + scroll;
    
        popup = window.open(url, 'STC', settings);
        popup.focus();
    }

}

// Pesquisa produto

function setValorPesquisaProduto(campo_id, campo_dsc, campo_id2){

    var array_id = campo_id.split('*');
    var array_dsc = campo_dsc.split('*');
    
    window.opener.document.getElementById(array_dsc[0]).value = array_dsc[1];
    window.opener.document.getElementById(array_id[0]).value = array_id[1];
    window.opener.post();

    window.close();    
}

//

//Baixa Recebimento

function setValorPesquisaBaixa(campo_id, campo_dsc, campo_id2){

    var array_id = campo_id.split('*');
    var array_dsc = campo_dsc.split('*');
    
    window.opener.document.getElementById(array_dsc[0]).value = array_dsc[1];
    window.opener.document.getElementById(array_id[0]).value = array_id[1];
    window.opener.load_valor_receb(array_id[0], campo_id2);

    window.close();    
}


//

function PoupCep(sURL,width, height, txt_cep, ddl_estado, txt_id_cidade, txt_cidade, txt_logradouro, txt_bairro, valor_cep){
    var popup = null;

    var str_cep = '';
    
    if(valor_cep != '' && valor_cep != undefined){
        str_cep = valor_cep;
    }else{
        if (document.getElementById(txt_cep).value != undefined) {
            str_cep = document.getElementById(txt_cep).value;
        }
    }
    
    var url = sURL + str_cep + '&rnd=' + Math.round(Math.random() * 899999) + 100000 + '&campo_cep=' + txt_cep + '&campo_estado=' + ddl_estado + '&campo_id_cidade=' + txt_id_cidade + '&campo_cidade=' + txt_cidade + '&campo_logradouro=' + txt_logradouro + '&campo_bairro=' + txt_bairro;
    
   // alert('URL' + url)
    
    if (popup!=null && !popup.closed){
        popup.focus();
    }else{  
        popup = window.open(url,'STC','height=' + height + ',width=' + width  + 'scrollbars=yes');
        popup.focus();
    }
}

function setValorPesquisaLanc(campo_id, campo_dsc){
    
    var array_id = campo_id.split('*');
    var array_dsc = campo_dsc.split('*');
    
    window.opener.document.getElementById(array_id[0]).value = array_dsc[0];
    window.opener.document.getElementById(array_id[1]).value = array_dsc[1];
    window.opener.document.getElementById(array_id[2]).value = array_dsc[2];
    window.opener.document.getElementById(array_id[3]).value = array_dsc[3];

    window.close();

}

function setValorPesquisa(campo_id, campo_dsc){
    
    var array_id = campo_id.split('*');
    var array_dsc = campo_dsc.split('*');
    
    window.opener.document.getElementById(array_dsc[0]).value = array_dsc[1];
    window.opener.document.getElementById(array_id[0]).value = array_id[1];

    window.close();

}

function setValorPesquisa2(campo_id, campo_dsc, campo_id2){

    var array_id = campo_id.split('*');
    var array_dsc = campo_dsc.split('*');
    var array_id2 = campo_id2.split('*');

    //alert(array_id2[1]);

    window.opener.document.getElementById(array_id[0]).value = array_id[1];
    window.opener.document.getElementById(array_dsc[0]).value = array_dsc[1];
    //window.opener.document.getElementById(array_id2[0]).disabled = false;
    window.opener.document.getElementById(array_id2[0]).value = array_id2[1];
    //window.opener.document.getElementById(array_id2[0]).selectedvalue = array_id2[1];
    //alert(window.opener.document.getElementById(array_id2[0]).value);    
    
    window.close();

    //window.opener.document.getElementById(array_id2[0]).disabled = true;
    
}

function setValorCep(campo_cep, campo_estado, campo_id_cidade, campo_cidade, campo_logradouro, campo_bairro){

    var array_cep = campo_cep.split('*');
    var array_estado = campo_estado.split('*');
    var array_id_cidade = campo_id_cidade.split('*');
    var array_cidade = campo_cidade.split('*');
    var array_logradouro = campo_logradouro.split('*');
    var array_bairro = campo_bairro.split('*');
    
    window.opener.document.getElementById(array_cep[0]).value = array_cep[1];
    if (array_estado[0] != '') window.opener.document.getElementById(array_estado[0]).value = array_estado[1];
    if (array_id_cidade[0] != '') window.opener.document.getElementById(array_id_cidade[0]).value = array_id_cidade[1];
    if (array_cidade[0] != '') window.opener.document.getElementById(array_cidade[0]).value = array_cidade[1];
    if (array_logradouro[0] != '') window.opener.document.getElementById(array_logradouro[0]).value = array_logradouro[1];
    if (array_bairro[0] != '') window.opener.document.getElementById(array_bairro[0]).value = array_bairro[1];
    
    window.close();
}



function ModalCep(sURL,width, height, top, left, txt_cep, ddl_estado, txt_cidade, txt_logradouro, txt_bairro, valor_cep){
        var str_cep;
        if(valor_cep != ''){
            str_cep = valor_cep;
        }else{
            str_cep = document.getElementById(txt_cep).value;
        }
        
		var url = sURL + str_cep + '&rnd=' + Math.round(Math.random() * 899999) + 100000;
		var sFeatures = new tamanhoJanela(width, height, top, left);
		var retornoModal = window.showModalDialog(url,'',sFeatures.dlgFeatures);

		if(retornoModal != undefined){
			var array_cep = retornoModal.split('*');
    		
    		document.getElementById(txt_cep).value = array_cep[0];
    		
    		document.getElementById(txt_logradouro).value = array_cep[1];
    		document.getElementById(txt_logradouro).disabled = true;
    		
    		document.getElementById(txt_bairro).value = array_cep[2];
    		document.getElementById(txt_bairro).disabled = true;
    		
    		document.getElementById(ddl_estado).value = array_cep[3];
    		document.getElementById(ddl_estado).disabled = true;
    		
    		document.getElementById(txt_cidade).value = array_cep[4];
    		document.getElementById(txt_cidade).disabled = true;
    		
		}else{
    		document.getElementById(txt_logradouro).value = '';
            document.getElementById(txt_logradouro).disabled = false;

    		document.getElementById(txt_bairro).value = '';
    		document.getElementById(txt_bairro).disabled = false;
    		
    		document.getElementById(ddl_estado).value = '';
    		document.getElementById(ddl_estado).disabled = false;
    		
    		document.getElementById(txt_cidade).value = '';
    		document.getElementById(txt_cidade).disabled = false;
		}

}

/* Para usar radio button em Repeater */
function SetUniqueRadioButton(nameregex, current)
{
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i]
      if (elm.type == 'radio')
      {
         if (re.test(elm.name))
         {
            elm.checked = false;
         }
      }
   }
   current.checked = true;
}
/***************************************************/

function MM_formtCep(e,src,mask) {
    if(window.event) { _TXT = e.keyCode; } 
    else if(e.which) { _TXT = e.which; }
    if(_TXT > 47 && _TXT < 58) { 
 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); } 
    return true; } else { if (_TXT != 8) { return false; } 
 else { return true; }
    }
}

function MascaraCNPJ(cnpj){
    if(mascaraInteiro(cnpj)==false){
        event.returnValue = false;
    }    
    return formataCampo(cnpj, '00.000.000/0000-00', event);
}

//adiciona mascara de cep
function MascaraCep(cep){
        if(mascaraInteiro(cep)==false){
        event.returnValue = false;
    }    
    return formataCampo(cep, '00.000-000', event);
}

//adiciona mascara de data
function MascaraData(data){
    if(mascaraInteiro(data)==false){
        event.returnValue = false;
    }    
    return formataCampo(data, '00/00/0000', event);
}

//adiciona mascara ao telefone
function MascaraTelefone(tel){    
    if(mascaraInteiro(tel)==false){
        event.returnValue = false;
    }    
    return formataCampo(tel, '(00) 0000-0000', event);
}

//adiciona mascara ao CPF
function MascaraCPF(cpf){
    if(mascaraInteiro(cpf)==false){
        event.returnValue = false;
    }    
    return formataCampo(cpf, '000.000.000-00', event);
}

//valida telefone
function ValidaTelefone(tel){
    exp = /\(\d{2}\)\ \d{4}\-\d{4}/
    if(!exp.test(tel.value))
        alert('Numero de Telefone Invalido!');
}

//valida CEP
function ValidaCep(cep){
    exp = /\d{2}\.\d{3}\-\d{3}/
    if(!exp.test(cep.value))
        alert('Numero de Cep Invalido!');        
}

//valida data
function ValidaData(data){
    exp = /\d{2}\/\d{2}\/\d{4}/
    if(!exp.test(data.value))
        alert('Data Invalida!');            
}

//valida o CPF digitado
function ValidarCPF(Objcpf){
    var CPF = Objcpf.value;

	var wVr, wTam, wSoma, wSoma2, i, wDig1, wDig2,
		wVETOR_CC = new Array(11),
		wVETOR_PESO = new Array(11),
		Erro = false;

	wVr = "";
	
	for (i = 0; i < CPF.length; i++) {
		if (!isNaN(parseInt(CPF.charAt(i)))) {
			wVr = wVr + CPF.charAt(i);
		}
	}
	
	wTam = wVr.length + 1;
	
	if (wTam < 11) {
		alert("Faltam dígitos no CPF. Por favor, tente novamente.");
		return false;
	}
	
	
	if (wVr == '11111111111' || wVr == '22222222222' || wVr == '33333333333' || wVr == '44444444444' || wVr == '55555555555' ||
		wVr == '66666666666' || wVr == '77777777777' || wVr == '88888888888' || wVr == '99999999999') {
		alert("CPF Inválido. Por favor, tente novamente.");
		return false;
	}
	
	wSoma = 0;
	wSoma2 = 0;
	
	for (i = 0; i < 11; i++) {
		wVETOR_CC[i] = wVr.charAt(i);
		wVETOR_PESO[i] = i + 2;
	}
	
	i = 0;
	while (i < 9)  {
		i++;
		
		if (i < 10) {
			wSoma += wVETOR_CC[9 - i] * wVETOR_PESO[i - 1];
		}
		
		wSoma2 += wVETOR_CC[10 - i] * wVETOR_PESO[i - 1];
	}
		
	wDig1 = (wSoma * 10) % 11;
	wDig2 = (wSoma2 * 10) % 11;
	
	if (wDig1 == 10) {
		wDig1 = 0;
	}
	
	if (wDig2 == 10) {
		wDig2 = 0;
	}

	if (parseInt(wVr.charAt(9)) != wDig1 || parseInt(wVr.charAt(10)) != wDig2) {
		alert("CPF inválido. Por favor, tente novamente.");
		return false;
	}

	return true;
	
}

//valida numero inteiro com mascara
function mascaraInteiro(){
    if (event.keyCode < 48 || event.keyCode > 57){
        event.returnValue = false;
        return false;
    }
    return true;
}

//valida o CNPJ digitado
function ValidarCNPJ(ObjCnpj){
    var cnpj = ObjCnpj.value;
    var valida = new Array(6,5,4,3,2,9,8,7,6,5,4,3,2);
    var dig1= new Number;
    var dig2= new Number;
    
    exp = /\.|\-|\//g
    cnpj = cnpj.toString().replace( exp, "" ); 
    var digito = new Number(eval(cnpj.charAt(12)+cnpj.charAt(13)));
        
    for(i = 0; i<valida.length; i++){
        dig1 += (i>0? (cnpj.charAt(i-1)*valida[i]):0);    
        dig2 += cnpj.charAt(i)*valida[i];    
    }
    dig1 = (((dig1%11)<2)? 0:(11-(dig1%11)));
    dig2 = (((dig2%11)<2)? 0:(11-(dig2%11)));
    
    if(((dig1*10)+dig2) != digito){    
        alert('CNPJ Invalido!');
        ObjCnpj.value = '';
        ObjCnpj.focus();
    }
}
function Limpar(valor, validos) { 
var result = ""; 
var aux; 
	for (var i=0; i < valor.length; i++) { 
		aux = validos.indexOf(valor.substring(i, i+1)); 
		if (aux>=0) { 
			result += aux; 
		} 
	} 
	return result; 
} 
function FormataMoeda(campo,tammax,teclapres,decimal) { 
var tecla = teclapres.keyCode; 
vr = Limpar(campo.value,"0123456789"); 
tam = vr.length; 
dec=decimal 
	
	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; } 
	
	if (tecla == 8 ){ tam = tam - 1 ; } 
	
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) 
	{ 
	
		if ( tam <= dec ){ campo.value = vr ; } 
		
		if ( (tam > dec) && (tam <= 5) ){ 
			campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; 
		}
		
		if ( (tam >= 6) && (tam <= 8) ){ 
			campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
		} 
		
		if ( (tam >= 9) && (tam <= 11) ){ 
			campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
		} 
		
		if ( (tam >= 12) && (tam <= 14) ){ 
			campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
		}
		
		if ( (tam >= 15) && (tam <= 17) ){ 
			campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;
		} 
	} 

} 

//formata de forma generica os campos
function formataCampo(campo, Mascara, evento) { 
    var boleanoMascara; 
    
    var Digitato = evento.keyCode;
    exp = /\-|\.|\/|\(|\)| /g
    campoSoNumeros = campo.value.toString().replace( exp, "" ); 
   
    var posicaoCampo = 0;     
    var NovoValorCampo="";
    var TamanhoMascara = campoSoNumeros.length;; 
    
    if (Digitato != 8) { // backspace 
        for(i=0; i<= TamanhoMascara; i++) { 
            boleanoMascara  = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                || (Mascara.charAt(i) == "/")) 
            boleanoMascara  = boleanoMascara || ((Mascara.charAt(i) == "(") 
                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " ")) 
            if (boleanoMascara) { 
                NovoValorCampo += Mascara.charAt(i); 
                  TamanhoMascara++;
            }else { 
                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo); 
                posicaoCampo++; 
              }            
          }     
        campo.value = NovoValorCampo;
          return true; 
    }else { 
        return true; 
    }
}

function NumericText(e)
{
  if(document.all) // Internet Explorer
    var tecla = event.keyCode;
  else if(document.layers) // Nestcape
    var tecla = e.which;

  //alert(tecla);

  if ((tecla > 47 && tecla < 58) || (tecla > 95 && tecla < 106)) // numeros de 0 a 9
    return true;
  else
    {
      if (tecla == 8 || tecla == 9 || tecla == 46 || (tecla >= 37 && tecla <= 40)) // backspace, delete, setas, tab;
        return true;
      else
        return false;
    }
}

function ConteudoText(e)
{
  if(document.all) // Internet Explorer
    var tecla = event.keyCode;
  else if(document.layers) // Nestcape
    var tecla = e.which;

  if(tecla > 96 && tecla < 123) // LETRAS DE a-z
    return true;
  else 
  if(tecla > 64 && tecla < 91) // LETRAS DE a-z
    return true;
  else
    {
      if (tecla != 8) // backspace
        return false;
      else
        return true;
    }
}
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 formatarMascara(src, mask) 
{
// Função utilizada para formatação de letras, numeros e ambos.
// # -> [0-9]
// Z -> [a-z] ou [A-Z]
// * -> [0-9],[a-z] ou [A-Z]

    var i = src.value.length;
    var saida = mask.substring(i,i+1);
    var texto = mask.substring(i)
    
    if (saida==''){
        event.keyCode = 0;
        return false;
    }
    if (!(mask.substring(i,i+1) == '#' || mask.substring(i,i+1) == 'Z' || mask.substring(i,i+1) =='*')){
        i = i + 1;
       saida = mask.substring(i,i+1); 
    }
    if (texto.substring(0,1) != saida) 
    {
        src.value += texto.substring(0,1);
    }
    
    if((saida =='#')&&(NumericText(event) == false)){ // Formata a mascara como numero = #
        event.keyCode = 0;
        return false;
    }else if((saida =='Z')&&(ConteudoText(event) == false)){ // Formata a mascara como texto [a-z][A-Z] = #
        event.keyCode = 0;
        return false;
    }else if((saida =='*')&&((NumericText(event) == false)&&(ConteudoText(event) == false))){ // Formata a mascara como numero e texto= #
        event.keyCode = 0;
        return false;
    }
}


function formatarMascaraDireita(src, mask) 

{


// Função utilizada para formatação de letras, numeros e ambos.
// # -> [0-9]
// Z -> [a-z] ou [A-Z]
// * -> [0-9],[a-z] ou [A-Z]

//00
//alert(String.fromCharCode(event.keyCode)); 
    var i = src.value.length;
    var j = mask.length - i;
    
    var saida = mask.substring(j -1, j);
    var texto = mask.substring(j)
    alert(saida);
    alert(texto);
    

    if (saida==''){
        event.keyCode = 0;
        return false;
    }
    if (!(saida  == '#' || saida  == 'Z' || saida  =='*')){
        i = i + 1;
       //saida =  mask.substring(j - i, j);
       saida =  '#';
       alert(saida);
    }
    if (texto.substring(0,1) != saida) 
    {
        src.value += texto.substring(0,1);
    }
    
    if((saida =='#')&&(NumericText(event) == false)){ // Formata a mascara como numero = #
        event.keyCode = 0;
        return false;
    }else if((saida =='Z')&&(ConteudoText(event) == false)){ // Formata a mascara como texto [a-z][A-Z] = #
        event.keyCode = 0;
        return false;
    }else if((saida =='*')&&((NumericText(event) == false)&&(ConteudoText(event) == false))){ // Formata a mascara como numero e texto= #
        event.keyCode = 0;
        return false;
    }
}
function ValidaNotZero(source, arguments)
{
    var vlr = arguments.Value
    if (vlr == '0') {
        arguments.IsValid = false;
        return;
    }
    else
    {
        arguments.IsValid = true;
        return;
    }
}
function MaxLength(campo, max) //ex.: MaxLength(this,15)
{
    if (campo.value.length > max && event.keyCode != 8 && event.keyCode != 46 && (event.keyCode < 35 || event.keyCode > 40))
        {return false} 
    else
        {return true}
}
