﻿// JScript File
function AbortPostBack() {
    var prm = Sys.WebForms.PageRequestManager.getInstance();    
  if (prm.get_isInAsyncPostBack()) {
       prm.abortPostBack();           
  }        
}

function CheckAll(me){
    var index = me.name.indexOf('_');
    var prefix = me.name.substr(0, index);
    var tamanho = document.forms[0].length;
    for(i=0; i < tamanho; i++){
        var o = document.forms[0][i];
        if(o.type == 'checkbox'){
            if(me.name != o.name){
                if(o.name.substring(0, prefix.length) == prefix){
                    o.checked = me.checked;
                }
            }
        }
    }
}

function valida_CUIT(source, obj) {
    var regex = new RegExp(/^\d{2}-\d{8}-\d{1}$/);
    var str = obj.Value;

    if (regex.test(str) == false)
        obj.IsValid = false;
    else {
        str = str.replace("-", "");

        var itemsVerificador = [
            5, 4, 3, 2, 7, 6, 5, 4, 3, 2
        ];

        var soma = 0;
        for (var i = 0; i < itemsVerificador.length; i++) {
            soma += itemsVerificador[i] * parseInt(str.charAt(i));
        }

        obj.IsValid = ((11 - (soma % 11)) % 11) == parseInt(str.charAt(str.length - 1));
    }
}

// Retorna posicao de elemento dentro do array
function array_indexOf(ar, valor) {
    for (var i = 0; i < ar.length; i++) {
        if (ar[i] == valor) {
            return i;
        }
    }

    return -1;
}

function valida_CEP_esAR(source, objControl) {
    // Formato A1234BCD
    if (objControl.Value.length != 8) 
    {
        objControl.IsValid = false;
    }
    else 
    {
        var provincias = [
            'C', 'B', 'K', 'X', 'W', 'H', 'U', 'E',
            'P', 'Y', 'L', 'F', 'M', 'N', 'Q', 'R',
            'A', 'J', 'D', 'Z', 'S', 'G', 'V', 'T'
        ];

        var regex = new RegExp(/^[a-zA-Z]{1}\d{4}[a-zA-Z]{3}$/);
        objControl.IsValid = regex.test(objControl.Value) && array_indexOf(provincias, objControl.Value.charAt(0)) != -1;
    }
}

function CheckInv() {
    var tamanho = document.forms[0].length; 
    for(i=0; i < tamanho; i++){
        var o = document.forms[0][i];
        if(o.type == 'checkbox' && o.name.indexOf('Header') == '-1'){
            o.checked = !o.checked;
        }
    }
}

function showTime() {
	var digital = new Date()
	var hours = digital.getHours()
	var minutes = digital.getMinutes()
	var seconds = digital.getSeconds()
	var dn = "am" 
	
	if (hours == 0) hours = 12
	if (minutes <= 9) minutes = '0' + minutes
	if (seconds <= 9) seconds = '0' + seconds
	
	meurelogio = hours + ':' + minutes; 
	
	var clock = document.getElementById("relogio");
	if(clock != null)
	{
	   clock.innerHTML = meurelogio;	
	   setTimeout("showTime()",1000)
	}
}

function linkTabela(linkReceived) {
	window.location.href = linkReceived;
}

function changeMenu(openID) {
	if(openID != "" && openID != null) {
		
		if(document.getElementById(openID).className == "on") {
			rollOut();
		} else {
			rollOut();
			document.getElementById(openID).className = "on"
		}
	}
}


//Valida CPF
function valida_CPF(source,cpfObj)
{ 
    var i; 
    
    var cpf = cpfObj.Value;
    cpf = cpf.replace(".", ""); 
    cpf = cpf.replace(".", ""); 
    cpf = cpf.replace("/", ""); 
    cpf = cpf.replace("-", ""); 
    
    var c = cpf.substr(0,9); 
    var dv = cpf.substr(9,2); 
    
    if ((cpf == "00000000000")||(cpf == "11111111111")||(cpf == "22222222222")||(cpf == "33333333333")||(cpf == "44444444444")||(cpf == "55555555555")||(cpf == "66666666666")||(cpf == "77777777777")||(cpf == "88888888888")||(cpf == "99999999999"))
    {
        cpfObj.IsValid=false;
    } 
    else
    {
        var d1 = 0; 
        
        for (i = 0; i < 9; i++) 
        { 
            d1 += c.charAt(i)*(10-i); 
        } 
        
        d1 = 11 - (d1 % 11); 
        
        if (d1 > 9) d1 = 0; 
        
        d2 = d1 * 2; 
        
        for (i = 0; i < 9; i++) 
        { 
            d2 += c.charAt(i)*(11-i); 
        } 
        
        d2 = 11 - (d2 % 11); 
        
        if (d2 > 9) d2 = 0; 
        
        if (dv.charAt(0) != d1 || dv.charAt(1) != d2) 
        { 
            cpfObj.IsValid=false; 
        }
        else
        {    
            cpfObj.IsValid=true;
        }
    }
}

function valida_CNPJ(source,cnpjObj) 
{ 
    var i; 
    
    var cnpj = cnpjObj.Value;
    
    cnpj = cnpj.replace(".", ""); 
    cnpj = cnpj.replace(".", ""); 
    cnpj = cnpj.replace("/", ""); 
    cnpj = cnpj.replace("-", ""); 
    
    var c = cnpj.substr(0,12); 
    var dv = cnpj.substr(12,2); 
    
    if ((cnpj == "00000000000000")||(cnpj == "11111111111111")||(cnpj == "22222222222222")||(cnpj == "33333333333333")||(cnpj == "44444444444444")||(cnpj == "55555555555555")||(cnpj == "66666666666666")||(cnpj == "77777777777777")||(cnpj == "88888888888888")||(cnpj == "99999999999999"))
    { 
        cnpjObj.IsValid=false; 
    } 
    else
    {
        var d1 = 0; 
        var ms = "543298765432"; 
        
        for (i = 0; i < 12; i++) 
        { 
            d1 += c.charAt(i)*ms.charAt(i);
        } 
        
        d1 = (d1 % 11); 
        
        if (d1 == 0 || d1 == 1) 
        { 
            d1 = 0; 
        } 
        else 
        { 
            d1 = 11 - d1; 
        } 
        
        var d1 = 0; 
        var ms = "543298765432"; 
        
        for (i = 0; i < 12; i++) 
        { 
            d1 += c.charAt(i)*ms.charAt(i); 
        } 
        
        d1 = (d1 % 11); 
        
        if (d1 == 0 || d1 == 1) 
        { 
            d1 = 0; 
        } 
        else 
        { 
            d1 = 11 - d1; 
        } 
        
        var c = c + d1; 
        var d2 = 0; 
        var ms = "6543298765432"; 
        
        for (i = 0; i < 13; i++) 
        { 
            d2 += c.charAt(i)*ms.charAt(i); 
        } 
        
        d2 = (d2 % 11); 
        
        if (d2 == 0 || d2 == 1) 
        { 
            d2 = 0; 
        } 
        else 
        { 
            d2 = 11 - d2; 
        } 
        
        if (dv.charAt(0) != d1 || dv.charAt(1) != d2) 
        { 
            cnpjObj.IsValid=false; 
        } 
        else
        {
            cnpjObj.IsValid=true; 
        }
    }
}

function valida_DATA(source,objData)
{ 
    var data = objData.Value;
    
    var data_array = data.split("/");
    
    if(data_array.length==3)
    {
        var data_ano = data_array[2];
        var data_mes = data_array[1];
        var data_dia = data_array[0];
        
        if((data_ano.length==4)&&((data_mes.length==2)||(data_mes.length==1))&&((data_dia.length==2)||(data_dia.length==1)))
        {
            timeA = new Date(data_ano, data_mes, 1);
            timeD = timeA - 86400000;
            timeB = new Date(timeD);
            
            var daysInMonth = timeB.getDate(); 
            if (daysInMonth >= data_dia && data_mes <= 12) 
            { 
                objData.IsValid=true;
                return;
            }
            else
            {
                objData.IsValid=false;
                return;
            } 
        }
        else
        {
            objData.IsValid=false;
            return;
        }
    }
    else
    {
        objData.IsValid=false;
        return;
    }
}

function valida_DATA_NULABLE(source,objData)
{ 
    var data = objData.Value;
    
    if(data=="")
    {
        objData.IsValid=true;
    }
    else
    {
        var data_array = data.split("/");

        var data_ano = data_array[2];
        var data_mes = data_array[1];
        var data_dia = data_array[0];
        
        if((data_ano.length==4)&&(data_mes.length==2)&&(data_dia.length==2))
        {
            objData.IsValid=true;
        }
        else
        {
            objData.IsValid=false;
        }
        
        timeA = new Date(data_ano, data_mes, 1);
        timeD = timeA - 86400000;
        timeB = new Date(timeD);
        
        var daysInMonth = timeB.getDate(); 
        if (daysInMonth >= data_dia && data_mes <= 12) 
        { 
            objData.IsValid=true;
        }
        else
        {
            objData.IsValid=false;
        } 
    }
}


// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

//Valida preenchimento dos campos em contatos.
function valida_Controls(source, objControl)
{
    var telefone=trim(document.getElementById("ctl00_cpCont_UCContatos1_txtTelefone").value);
    var celular=trim(document.getElementById("ctl00_cpCont_UCContatos1_txtCelular").value);
    var email=trim(document.getElementById("ctl00_cpCont_UCContatos1_txtEmail").value);
    var fax=trim(document.getElementById("ctl00_cpCont_UCContatos1_txtFax").value);
    if((telefone!="")||(fax!="")||(celular!="")||(email!=""))
    {
        objControl.IsValid=true;
    }
    else
    {
        objControl.IsValid=false;
    }
}

function validaControlEstado(source, objControlEstado)
{
    var estado=document.getElementById("ctl00_cpCont_UCEnderecos1_dpdEstado").value;
    if(estado!="Selecione")
    {
        objControlEstado.IsValid=true;
    }
    else
    {
        objControlEstado.IsValid=false;
    }
}

function validaControlSubTipo(source, objControlSubtipo)
{
    var subtipo=document.getElementById("ctl00_cpCont_dpdSubTipo").value;
    
    if(subtipo!="0")
    {
        objControlSubtipo.IsValid=true;
    }
    else
    {
        objControlSubtipo.IsValid=false;
    }
}

function validaControlEstudanteLimite(source, objControlSubtipo)
{
    var subtipo=document.getElementById("ctl00_cpCont_dpdEstudanteLimite").value;
    
    if(subtipo!="0")
    {
        objControlSubtipo.IsValid=true;
    }
    else
    {
        objControlSubtipo.IsValid=false;
    }
}

function valida_SoNumeros(source, objControl) 
{ 
    var i;
    var str=objControl.Value;
    
    for(i=0; i<str.length; i++)
    {
        if((str.charCodeAt(i)<48)||(str.charCodeAt(i)>57))
        {
            objControl.IsValid=false;
        }
        else
        {
            objControl.IsValid=true;
        }
    }
}

function valida_CEP(source, objControl)
{
    var str = objControl.Value;
    var straux="";
    var strresult="";
    
    for(i=0; i<str.length; i++)
    {
        if(str.charCodeAt(i)!=45)
            straux = straux+str.charAt(i);
    }
    
    for(i=0; i<straux.length; i++)
    {
        if((straux.charCodeAt(i)>=48)&&(straux.charCodeAt(i)<=57))
        {
            strresult=strresult+straux.charAt(i);
        }
    }
    
    if(straux.length>0)
    {
        if(strresult.length == 8)
        {
            objControl.IsValid=true;
        }
        else
        {
            objControl.IsValid = false;
        }
    }
    else
    {
        objControl.IsValid = false;
    }
    
}

function valida_CID_Apos(source, objControl)
{
    var cid=document.getElementById("ctl00_cpCont_txtCid").value;
    var aposentadoria=document.getElementById("ctl00_cpCont_txtAposentadoria").value;
    
    if((cid == "")&&(aposentadoria == ""))
    {
        objControl.IsValid=false;
    }
    else
    {
        objControl.IsValid=true;
    }
}

//****************************************************************** DATA *************************************************88

function validation_ShortDateOnKeyPress(evt, field)
{
   return format_field('99/99/9999', field, evt);
}

function format_date(valor, evt)
{ 		  
   var charCode = (evt.which) ? evt.which : event.keyCode
   
   if (charCode != 8)
   {   
      valor.value = valor.value.replace("/","");
      valor.value = valor.value.replace("/","");
      
      if(valor.value.length > 1) valor.value = valor.value.substr(0,2) + "/" +valor.value.substr(2);    
      if(valor.value.length > 4) valor.value = valor.value.substr(0,5) + "/" +valor.value.substr(5);
   }
}

function format_field(formato, campo, e){
    var keynum = (window.event) ? e.keyCode : e.which;
    if( keynum == 8 || keynum == 0 ) return true; //BackSpace;
    
    if( keynum == 13 ){ 
        campo.select();
        return false;
    }
    
    clean_field();

    var tamCampo = campo.value.length;
    var keychar = String.fromCharCode(keynum);

    if(tamCampo == formato.length) return false;
    
    var charFormato = formato.charAt(tamCampo);
    while(charFormato != '9' && charFormato != '#'){
        campo.value += charFormato;
        if(formato.length > tamCampo)
            charFormato = formato.charAt( ++tamCampo );
        else
            break;
    }
    
    if(charFormato == '9')
        expressao = /\d/;
    else if(charFormato == '#')
        expressao = /\w/;

    return expressao.test(keychar);
}

function clean_field(){
    if (document.selection && document.selection.createRange) {
        var range = document.selection.createRange();
        range.text = '';
    }
}



function validador_data(param) 
{
      datainserida = param.value;
      idinserido = param.id;
      sizesatainserida = datainserida.length;
	   ano=datainserida.substring(0,4);
	   mes=datainserida.substring(5,7);
	   dia=datainserida.substring(8,sizesatainserida);

      if(mes > 12 || mes==0)
      {
         alert("Invalidate Initial Date");
         document.getElementById(idinserido).value=""; 
      }else
      {
         anoBissexto = 0;

         if (((ano % 4 == 0) && !(ano % 100 == 0))||(ano % 400 ==0))
	      {
		      anoBissexto = "sim é bissexto";
            DiasMes = new Array();
	         DiasMes[1] = 31;
	         DiasMes[2] = 29;
	         DiasMes[3] = 31;
	         DiasMes[4] = 30;
	         DiasMes[5] = 31;
	         DiasMes[6] = 30;
	         DiasMes[7] = 31;
	         DiasMes[8] = 31;
	         DiasMes[9] = 30;
	         DiasMes[10] = 31;
	         DiasMes[11] = 30;
	         DiasMes[12] = 31;
	      }
	      else
	      {
		      anoBissexto = "nao é bissexto";
            DiasMes = new Array();
	         DiasMes[1] = 31;
	         DiasMes[2] = 28;
	         DiasMes[3] = 31;
	         DiasMes[4] = 30;
	         DiasMes[5] = 31;
	         DiasMes[6] = 30;
	         DiasMes[7] = 31;
	         DiasMes[8] = 31;
	         DiasMes[9] = 30;
	         DiasMes[10] = 31;
	         DiasMes[11] = 30;
	         DiasMes[12] = 31;
	      }

         
         diasmaximo= DiasMes[parseFloat(mes)]; 

         if(dia>diasmaximo || dia==0)
         {
            alert("Invalidate Initial Date");
            document.getElementById(idinserido).value="";
 
         }else
         {
         verificaCampo(param,idinserido);
         }
      }
}
  
function setFocus(elementos){
    for(var i = 0; i < elementos.length; i++){
        var elemento = document.getElementById(elementos[i]);
        if(elemento != null && elemento.style.visibilty != 'hidden' && elemento.style.display != 'none'){
            elemento.focus();
            return;
        }
    }
}

function verificaCampo(param,idinserido)
{
    datainserida=param.value;

    if(datainserida != "")
    {
      tamanho=datainserida.length;
      arrData = datainserida.split("/");
     
      if (eval(arrData[2]) < 10) 
      {
         dia = "0" + eval(arrData[2]);
      }
      else { dia = arrData[2];}

      if (eval(arrData[1]) < 10)
      { mes = "0" + eval(arrData[1]);}
      else
      { mes = arrData[1];}

      ano = arrData[0];

      datainserida= dia + "/" + mes + "/" + ano;
      document.getElementById(idinserido).value=datainserida;   
    }
}


function winmodal(Page, name, height, width) {
  if (window.showModalDialog) {
    window.showModalDialog(Page, name, 'dialogWidth:' + width + 'px;dialogHeight:' + height + 'px;center=yes;status=no;');
  } else {
    window.open(Page, name, 'height='+ height +',width=' + widgth + ',toolbar=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no ,modal=yes');
  }
  
  if(intervalID > 0)
    clearInterval(intervalID);
} 


function disabledElements(){
    for(var i = 0; i < document.forms[0].elements.length; i++){
        var element = document.forms[0].elements[i];
        if(element != null){
            element.disabled = true;
            return true;
        }
    }    
}

function openBoleto(cod, codCli)
{
    window.open('ImprimeBoleto' + codCli + '.aspx?cod='+ cod, 'WebCommerce' , 'width=800,height=600,scrollbars=YES,Resizable=YES,Menubar=YES,Toolbar=YES');
}

function openBoletoE(cod, codCli)
{
    window.open('ImprimeBoleto' + codCli + 'e.aspx?cod='+ cod, 'WebCommerce' , 'width=800,height=600,scrollbars=YES,Resizable=YES,Menubar=YES,Toolbar=YES');
}

function qtdChk(chk, label)
{
    if(chk.checked)
        label.innerText = parseInt(label.innerText) + 1;
    else
        label.innerText = parseInt(label.innerText) - 1;
}

function qtdChkAll(chk, label, qtdIni, qtdPag)
{
    if(chk.checked)
        label.innerText = qtdIni + qtdPag;
    else
        label.innerText = qtdIni - qtdPag;
}
// Exemplo de utilização: <a href="#" target="name" onclick="winmodal('http://web.transdatasmart.com.br','Nome',25,25); return false;">click here</a>


var janelaSaldo = null;
function openSaldo(numSerie, id, familiaID){
    CEItens.selectCEItem(id);
    
    var pagina;
    if (familiaID == 3){ // Escolar
        pagina = "SaldoCartao.aspx?fm=3";
    } else { // VT
        pagina = "SaldoCartao.aspx?fm=1";
    }
    var url = pagina + "&ns="+ numSerie;
    if(janelaSaldo == null || janelaSaldo.closed){
        janelaSaldo = window.open(url, 'janelaSaldo', 'width=480, height=250, location=no, statusbar=no, scrollbars=no, resizable=no');
    }else{
        janelaSaldo.document.location = url;
    }
    janelaSaldo.focus();
    
    return false;
}

function getItensChecked(grid){
    var itens = grid.getElementsByTagName("input");
    var checked = 0;
    for(i in itens){
        if(itens[i].type == 'checkbox' && itens[i].checked){
            checked++;
        }
    }
    return checked;
}
