﻿// JScript File
function AbreModalPopUpBuscaCEP(campo)
{
    var janela = window.showModalDialog('correio/busca_cep.aspx', '', 'height=380, width=380');
    
    cep = document.getElementById(campo);    
    cep.value = janela;
}

function ValidaCamposFavorecido(nome, documento, todos, campo, countItens, nomeGrid)
{    
    nome = document.getElementById(nome);
    documento = document.getElementById(documento);
    todos = document.getElementById(todos);    
    
    if(nome.value == '')
    {
        alert('Informe o nome do Favorecido');
        nome.focus();
        return false;
    }
    else
        if(documento.value == '')
        {
            alert('Informe o documento do Favorecido');
            documento.focus();
            return false;
        }
                    
    if(todos != null && todos.checked == true)
    {
        arrayNome = nome.id.split("_");
        novoCampoNome = arrayNome[0] + "_" + arrayNome[1] + "_" + arrayNome[2] + "_" + arrayNome[3] + "_" + arrayNome[4] + "_" + arrayNome[5];               
        
        arrayDocumento = documento.id.split("_");
        novoCampoDocumento = arrayDocumento[0] + "_" + arrayDocumento[1] + "_" + arrayDocumento[2];
        
        if(countItens > 10)
            nomeGrid = nomeGrid.substring(0, nomeGrid.length - 2);
        else
            nomeGrid = nomeGrid.substring(0, nomeGrid.length - 1);                        
                
        for(i = 0; i < countItens; i++)
        {            
            NovoNome = nomeGrid + (i + 1) + "_txtNome";
            NovoDocumento = nomeGrid + (i + 1) + "_txtDocumento";
            
            NovoNome = document.getElementById(NovoNome);
            NovoDocumento = document.getElementById(NovoDocumento);
            
            NovoNome.value = nome.value;
            NovoDocumento.value = documento.value;
        }        
    }
            
    MostraEsconde(campo)
}

function MostraEsconde(campo)
{
    campo = document.getElementById(campo);
    if(campo.style.display == '')
        campo.style.display = 'none';
    else
        campo.style.display = '';
}

function HabilitaCampo(textBox, habilita)
{
    campo = document.getElementById(textBox);
    
    if (campo != null)
    {
        campo.disabled = !habilita;
    	
	    if (habilita)
	    {
		    campo.style.background = '#FFFFFF';
	    }
	    else
	    {
	        campo.value = '';
		    campo.style.background = '#D7E6FE';
	    }
	}
}

function setarValor(campo, valor)
{
    campo.value = valor;
}

function LimpaCamposFavorecido(nome, documento, todos, countItens, nomeGrid)
{
    nome = document.getElementById(nome);
    documento = document.getElementById(documento);
    todos = document.getElementById(todos);
    
    nome.value = '';
    documento.value = '';
    
    if(todos != null && todos.checked == true)
    {
        arrayNome = nome.id.split("_");
        novoCampoNome = arrayNome[0] + "_" + arrayNome[1] + "_" + arrayNome[2];        
        
        arrayDocumento = documento.id.split("_");
        novoCampoDocumento = arrayDocumento[0] + "_" + arrayDocumento[1] + "_" + arrayDocumento[2];
                
        if(countItens > 10)
            nomeGrid = nomeGrid.substring(0, nomeGrid.length - 2);
        else
            nomeGrid = nomeGrid.substring(0, nomeGrid.length - 1);                        
                
        for(i = 0; i < countItens; i++)
        {            
            NovoNome = nomeGrid + (i + 1) + "_txtNome";
            NovoDocumento = nomeGrid + (i + 1) + "_txtDocumento";                        
            
            NovoNome = document.getElementById(NovoNome);
            NovoDocumento = document.getElementById(NovoDocumento);
            
            NovoNome.value = '';
            NovoDocumento.value = '';
        } 
        
        todos.checked = false;        
    }    
}
     
function MinhaFuncao()
{    
    setTimeout('VerificaEstado()', 1000); //javascript trabalha com milesimos
}

function VerificaEstado() {
	if (PopUpTeste!=null && PopUpTeste.closed)
    {
        document.getElementById('ctl00_cphConteudo_Pagamento_fechouPopUp').value = 'true';        
    }
    else
        setTimeout('VerificaEstado()', 1000); //javascript trabalha com milesimos
}

//fecha pop ups de pagamento
function FechaPopUp()
{    
    if(PopUpTeste.MPGpopup != null)
        PopUpTeste.MPGpopup.close();    
        
    PopUpTeste.close();
}

function validaPesquisa(){
	objs = document.getElementsByTagName("select");
	for(i=0;i<objs.length;i++){
	    obsAtual = objs[i].id;
	    campo = "dpListEmpresa";
	    tamCampo = campo.length;
	    tamTotal = obsAtual.length; 
	    tamTotal = tamTotal - tamCampo;

	    if (obsAtual.substr(tamTotal, tamCampo)==campo)
		    if(document.getElementById(obsAtual).value == "" || document.getElementById(obsAtual).value == 0)
		    {
		        alert("Para realizar a consulta você precisa informar a Viação.");    			    
		        return false;
		    }
	}
	
	for(i=0;i<objs.length;i++){
	    obsAtual = objs[i].id;
	    campo = "dpListOrigem";
	    tamCampo = campo.length;
	    tamTotal = obsAtual.length; 
	    tamTotal = tamTotal - tamCampo;

	    if (obsAtual.substr(tamTotal, tamCampo)==campo)
		    if(document.getElementById(obsAtual).value == "" || document.getElementById(obsAtual).value == 0)
		    {
		        alert("Para realizar a consulta você precisa informar a Origem.");    			    
		        return false;
		    }
	}	
	
	for(i=0;i<objs.length;i++){
	    obsAtual = objs[i].id;
	    campo = "dpListDestino";
	    tamCampo = campo.length;
	    tamTotal = obsAtual.length; 
	    tamTotal = tamTotal - tamCampo;

	    if (obsAtual.substr(tamTotal, tamCampo)==campo)
		    if(document.getElementById(obsAtual).value == "" || document.getElementById(obsAtual).value == 0)
		    {
		        alert("Para realizar a consulta você precisa informar a Destino.");    			    
		        return false;
		    }
	}			

	objs = document.getElementsByTagName("imput");		
	for(i=0;i<objs.length;i++){
		if (objs[i].type=="text"){
			obsAtual = objs[i].id;
			campo = "txtData";
			tamCampo = campo.length;
			tamTotal = obsAtual.length; 
			tamTotal = tamTotal - tamCampo;

			if (obsAtual.substr(tamTotal, tamCampo)==campo)
				if(document.getElementById(obsAtual).value == "")
				{
				    alert("Para realizar a consulta você precisa informar a Data.");					    
				    return false;
				}
		}
	}
    
	return true;
}

function setaValor(v)
{
            var botao = document.getElementById('ctl00_cphConteudo_OnibusBloqueio_btnForcaPostBack');
	        if(botao != null)
	        {
	            SetValue('hiddenPoltronas', 'input', v);
	            botao.click();
	        }
	        else
	        {
                var completo = v;
                var qnt = 0;
                var tudo = "";
                var contTemp = 1;
                if (v != ""){
	                for (i=0;i<v.length+1;i++){
		                if (v.substring(i,i+1)=="X"){
			                qnt++;
			                tudo+= ((tudo!="" && contTemp!=1)?"-":"")+(((i+1)<10)?"0"+(i+1):(i+1));
			                if (contTemp==10){
				                tudo+="<br>";
				                contTemp = 1;
			                }else
				                contTemp++;
		                }
	                }
                }

	            _valor = document.getElementById('txtValor').innerHTML;
	            document.getElementById('txtPoltronas').innerHTML = tudo;	
	            document.getElementById('txtValorTotal').innerHTML = formataValor(qnt,_valor.replace("R$ ","").replace(",","."));
    	        
                SetValue('hiddenValorTotal', 'input', formataValor(qnt,_valor.replace("R$ ","").replace(",",".")));	
	            SetValue('hiddenPoltronas', 'input', completo);
	        }
	        
}

function formatTel(t,teclapres) {
	var tecla = teclapres.keyCode;
	if ( tecla != 9 && tecla != 8 ) {
		t = t.replace(/[^0-9]/g,"");
		var n = t;
		if (t.length>=2) n = "("+t.substr(0,2)+")"+t.substr(2,4);
		if (t.length>=6) n+= "-"+t.substr(6,4);
		if (t.length>=10) return n.substr(0,12);
		{
		}
		return n;
	}
	return t
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  
  for (i=0; i<(args.length-2); i+=3) 
    if ((obj=MM_findObj(args[i]))!=null) 
    { 
        v=args[i+2];
	
        if (obj.style) 
        { 
            obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; 
        }
	    
	    obj.visibility=v; 
	}
}



function formataValor(qtd,v){    
	var	valor = Number(v).toFixed(2);
	var	total = valor*qtd;

	total = Math.round(total*100);
	total = total/100;
	total = total.toString().replace(",",".");
	if (total.toString().indexOf(".")==-1)
		total = total.toString()+".00";
	else{
		var depoisVirgula = total.toString().substring(total.toString().indexOf(".")+1,total.toString().Length);
		if (depoisVirgula.length==1)
			total = total.toString()+"0";
		else if (depoisVirgula.length==0)
			total = total.toString()+"00";
	}

	return consertavalor(formatValue(total, "###,###,###,###.##"));
}

function consertavalor(valor){//Gamb ao maximo
	v = valor.replace(",","*").replace(",","*").replace(",","*").replace(",","*");
	v = v.replace(".",",");
	v = v.replace("*",".").replace("*",".").replace("*",".").replace("*",".");
	return  "R$ "+v;			
}

function validaCompra(){
	if ((Value('hiddenValorTotal', 'input')=="" && 
	     Value('hiddenPoltronas', 'input')=="") || 
	    (Value('hiddenValorTotal', 'input')=="R$ 0,00")){
		alert("Você deve selecionar alguma poltrona para concluir a compra");
		return false;
	}	
}

function noPostBack(sNewFormAction){
    document.forms[0].action = sNewFormAction;
	document.forms[0].__VIEWSTATE.name = 'NOVIEWSTATE';
}

function formatValue(argvalue, format) {    
  var numOfDecimal = 0;
  if (format.indexOf(".") != -1) {
	numOfDecimal = format.substring(format.indexOf(".") + 1, format.length).length;
  }
  argvalue = formatDecimal(argvalue, true, numOfDecimal);

  argvalueBeforeDot = argvalue.substring(0, argvalue.indexOf("."));
  retValue = argvalue.substring(argvalue.indexOf("."), argvalue.length);

  strBeforeDot = format.substring(0, format.indexOf("."));

  for (var n = strBeforeDot.length - 1; n >= 0; n--) {
	oneformatchar = strBeforeDot.substring(n, n + 1);
	if (oneformatchar == "#") {
	  if (argvalueBeforeDot.length > 0) {
		argvalueonechar = argvalueBeforeDot.substring(argvalueBeforeDot.length - 1, argvalueBeforeDot.length);
		retValue = argvalueonechar + retValue;
		argvalueBeforeDot = argvalueBeforeDot.substring(0, argvalueBeforeDot.length - 1);
	  }
	}
	else {
	  if (argvalueBeforeDot.length > 0 || n == 0)
		retValue = oneformatchar + retValue;
	}
  }

  return retValue;
}

function formatDecimal(argvalue, addzero, decimaln) {
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;

  number = Math.pow(10, numOfDecimal);

  argvalue = Math.round(parseFloat(argvalue) * number) / number;
  // If you're using IE3.x, you will get error with the following line.
  // argvalue = argvalue.toString();
  // It works fine in IE4.
  argvalue = "" + argvalue;

  if (argvalue.indexOf(".") == 0)
	argvalue = "0" + argvalue;

  if (addzero == true) {
	if (argvalue.indexOf(".") == -1)
	  argvalue = argvalue + ".";

	while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
	  argvalue = argvalue + "0";
  }

  return argvalue;
}

function SetValue(idCampo, tgName, valor)
{
    objs = document.getElementsByTagName(tgName);
    for(i=0;i<objs.length;i++){
        obsAtual = objs[i].id;
        campo = idCampo;
        tamCampo = campo.length;
        tamTotal = obsAtual.length; 
        tamTotal = tamTotal - tamCampo;            
        if (obsAtual.substr(tamTotal, tamCampo)==campo)             
            document.getElementById(obsAtual).value = valor;    
    }        
}

function Value(idCampo, tgName)
{
    objs = document.getElementsByTagName(tgName);
    for(i=0;i<objs.length;i++){
        obsAtual = objs[i].id;
        campo = idCampo;
        tamCampo = campo.length;
        tamTotal = obsAtual.length; 
        tamTotal = tamTotal - tamCampo;
        
        if (obsAtual.substr(tamTotal, tamCampo)==campo) 
            return document.getElementById(obsAtual).value;    
    }
    
    return "";    
}

function Testa_popup()
{		
	var abre = window.open('about:blank','teste_popup','height=1, width=1, location=no, menubar=no, resizable=no, scrollbars=no, status=no, titlebar=no, toolbar=no, left=0, top=0');
	
	if (abre==null)
		return false;
	else
	{
		if (abre.closed==false)
		{
			abre.close();
		}
		
		return true;
	}
}

function TestaBloqueador(id, name)
{
	var bResultado = Testa_popup();
	
	if (bResultado == false)
	{
	    MM_showHideLayers('testepopup','','show');
	    MM_showHideLayers('pagamento','','hide')
	    
	    if(document.getElementById(id) != null)
	        document.getElementById(id).value = 'S';
        else
            document.getElementsByName(name).value = 'S';
	}
	else
	{
	    if(document.getElementById(id) != null)
	        document.getElementById(id).value = 'N';
	    else
            document.getElementsByName(name).value = 'N';
    }
}

function popImprime()
{
    var hoje   = new Date();
    
    window.open('pop_imprime.aspx?id='+hoje.getHours()+hoje.getMinutes()+hoje.getSeconds(),'teste_popup','height=500, width=700, location=no, menubar=no, resizable=no, scrollbars=yes, status=no, titlebar=no, toolbar=no, left=0, top=0');
}

function popPagamento(cartao)
{
    window.open('pop_pagamento.aspx?cartao='+cartao, 'tst', 'height=200, width=330, location=no, menubar=no, resizable=no, scrollbars=yes, status=no, titlebar=no, toolbar=no, left=0, top=0');
}

function getFlashVersion()
{ 
  // ie 
  try { 
	try { 
	  // avoid fp6 minor version lookup issues 
	  // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/ 
	  var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6'); 
	  try { axo.AllowScriptAccess = 'always'; } 
	  catch(e) { return '6,0,0'; } 
	} catch(e) {} 
	return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1]; 
  // other browsers 
  } catch(e) { 
	try { 
	  if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){ 
		return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1]; 
	  } 
	} catch(e) {} 
  } 
  return '0,0,0'; 
} 

function detectaFlash()
{
	if(getFlashVersion().split(',').shift() < 6)
	{
	    MM_showHideLayers('plugin_flash','','show');
	}
}

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

