﻿// JScript File

function MinhaFuncao()
{    
    setTimeout('VerificaEstado()', 1000); //javascript trabalha com milesimos
}

function VerificaEstado() {
	if (PopUpTeste!=null && PopUpTeste.closed)
    {
        document.getElementById('ctl00_ContentPlaceHolder1_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 abreSenha(){
    if (screen.width==1024)
	    posi = "top=115,left=350";
    else
	    posi = "top=115,left=220";
    window.open('pop_senha.aspx','senha','width=320,height=180,'+posi);	
}

function abreAlterarSenha(){
    if (screen.width==1024)
	    posi = "top=115,left=350";
    else
	    posi = "top=115,left=220";
    if(!window.open('pop_alterar_senha.aspx','senha','width=320,height=180,'+posi))	
        alert("Você possui algum bloqueador de Pop - up. Desative - o e tente alterar sua senha novamente.");
}

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 setaValor(v){
    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 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(campo)
{
	var bResultado = Testa_popup();
	
	if (bResultado == false)
	{
	    MM_showHideLayers('testepopup','','show');
	    MM_showHideLayers('pagamento','','hide');	
	    document.getElementById(campo).value = 'S';
	}
	else
	    document.getElementById(campo).value = 'N';
}

function popImprime()
{
    window.open('pop_imprime.aspx','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');
}