﻿    //Formatações e máscaras
    
    function replaceAll(texto, substituirDe, substituirPara) {
        while(texto.indexOf(substituirDe)>-1) {
            texto = texto.replace(substituirDe, substituirPara);
        }
        return texto;
    }
    
    function converterNumerico(valor){
        var retorno = replaceAll(valor, ".", "");
        retorno = retorno.replace(",", ".");
        if(retorno=="") retorno = 0;
        return parseFloat(retorno);
    }
    
    function validarNumero(obj) {
        var chrDigito = String.fromCharCode(event.keyCode);
        
	    if (isNaN(chrDigito))
	    {
		    if(chrDigito==",") {
		        if(obj.value.indexOf(",")>=0) {return false;}
		        else {return true;}
		    }
		    else {
		        if(chrDigito==".") {return true;}
		        else {return false;}
		    }
	    }
	    else { return true; }
    }
    
    function validarNumeroComFormatacao(obj) {
        var chrDigito = String.fromCharCode(event.keyCode);
        
	    if(isNaN(chrDigito) || chrDigito == " ") {return false;}
	    
	    var strTemp = obj.value;
	        strTemp = strTemp.replace(",", "");
	        strTemp = replaceAll(strTemp, ".", "");
	        strTemp = strTemp + chrDigito;
	    
	    if(strTemp.length > 2) {
	        var parteInteira = strTemp.substring(0, (strTemp.length-2));
	        var parteDecimal = strTemp.substring((strTemp.length-2), (strTemp.length));

	        strTemp =  formatarMilhar(parteInteira) + "," + parteDecimal;
	    }
	    
	    obj.value = strTemp;
	    return false;
    }

    function formatarNumero(valor){
        //arredondamento
        return Math.round(parseFloat(valor)*100)/100;
    }
    
    function formatarNumeroPadrao(valor){
        var retorno = formatarNumero(valor).toString();
        //0
        //0.0
        //0.00
        var parte_inteira = "0";
        var parte_decimal = "00";

        if(retorno.indexOf(".")<0) { parte_inteira = retorno; }
        else { 
            parte_inteira = retorno.substring(0, retorno.indexOf(".")); 
            parte_decimal = retorno.substring(retorno.indexOf(".")+1, retorno.length); 
        }
        if(parte_decimal.length==1) { parte_decimal = parte_decimal + "0"; }
        
        retorno = formatarMilhar(parte_inteira) + "," + parte_decimal;
        return retorno;
    }
    function formatarMilhar(valor) {
        var retorno = valor.toString();
        
        if(valor.toString().length>3) { 
            var comprimento = valor.toString().length;
            retorno = "";
            
            for(var i=comprimento; i>0; i--) {
                if(retorno.length == 3 || retorno.length == 7 || retorno.length == 11 || retorno.length == 15) {
                    retorno = "." + retorno;
                }
                retorno = valor.substring(i-1,i) + retorno;
            }
        }
        return retorno;
    }
    function formatarMeuValor(obj) {
        obj.value = formatarNumeroPadrao(converterNumerico(obj.value));
    }

    function mascararTelefone(obj){
        var chrDigito = String.fromCharCode(event.keyCode);
	    if(isNaN(chrDigito) || chrDigito == " ") {return false;}
	    
	    var strTelefone = obj.value.replace("(", "").replace(")", "").replace("-", "").replace(" ","");
        if (strTelefone.length == 10) {return false;}
            strTelefone = strTelefone + chrDigito;
	    
	    var ddd = "";
	    var telefone = "";
	    
	    if(strTelefone.length > 2) {
	        ddd = strTelefone.substring(0, 2);
	        telefone = strTelefone.substring(2, strTelefone.length);
	        
	        if(telefone.length > 4) {
	            telefone = telefone.substring(0, 4) + "-" + telefone.substring(4, telefone.length);
	        }
	    }
	    else {
	        ddd = strTelefone;
	    }

	    obj.value = "(" + ddd + ") " + telefone;

        return false;
    }    
    
    
    //Coordenadas    
    
    // getPageSize()
    // Returns array with page width, height and window width, height
    // Core code from - quirksmode.org
    // Edit for Firefox by pHaez
    function getPageSize(){
     
        var xScroll, yScroll;
         
        if (window.innerHeight && window.scrollMaxY) {
            xScroll = document.body.scrollWidth;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
         
        var windowWidth, windowHeight;
        if (self.innerHeight) {	// all except Explorer
            windowWidth = self.innerWidth;
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }
         
        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight){
            pageHeight = windowHeight;
        } else {
            pageHeight = yScroll;
        }
         
        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth){
            pageWidth = windowWidth;
        } else {
            pageWidth = xScroll;
        }
         
        arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
         
        return arrayPageSize;
    }                    

    function getCoords(){
        var coords = [0,0];

        var x, y;
        if (self.pageYOffset) // all except Explorers
        {
            x = self.pageXOffset; // not used in event code
            y = self.pageYOffset; // not used in event code
        }
        else if(document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
        {
            x = document.documentElement.scrollLeft;
            y = document.documentElement.scrollTop;
        }
        else if (document.body) // all other Explorers
        {
            x = document.body.scrollLeft;
            y = document.body.scrollTop;
        }
        coords[0] = x;
        coords[1] = y;

        return coords;
    }    


    //outros
    function createBackground(){
        var myBackground = document.getElementById( "myBackground" );
        var form =  document.getElementsByTagName("form")[0];
        if( ! myBackground ){
	        if( form ){
	        
		        var myBackground = document.createElement("div");
		            myBackground.style.position = "absolute";
		            myBackground.id = "myBackground";
		            myBackground.style.width = getPageSize()[0] + "px";
		            myBackground.style.height = getPageSize()[1] + "px";
		            myBackground.style.left = "0px";
		            myBackground.style.top = "0px";
		            myBackground.style.backgroundColor = "#000000";
		            myBackground.style.filter = "alpha(opacity=70)";
		            myBackground.style.opacity = 0.7;
		            myBackground.style.zIndex = 10000;
		        
		            form.appendChild( myBackground );
	        }
        }
    }

    function closeBackground(){
        var form = window.parent.document.getElementsByTagName("form")[0];
        var myBackground = window.parent.document.getElementById( "myBackground" );
        
        if( myBackground ){
	        if( form ){
		        form.removeChild( myBackground );
		        delete myBackground;
	        }
        }
    }
    
    function createFrame(url,w,h,s) {
        var form = window.parent.document.getElementsByTagName("form")[0];
        var myFrame = document.createElement( "iframe" );
            myFrame.id = "myFrame";
            myFrame.style.position = "absolute";
            myFrame.align = "center";
            myFrame.frameBorder = 0;
            myFrame.style.width = w+"px";
            myFrame.style.height = h+"px";
            myFrame.style.left = ((getPageSize()[2] - w) / 2) + "px";
            myFrame.style.top = (getCoords()[1] + 50) + "px";
            myFrame.style.zIndex = 10001;
            myFrame.src = url;
            myFrame.allowTransparent = true;
            
            if(typeof(s)!="undefined")
                myFrame.scrolling = s;
                
            form.appendChild( myFrame );
    }
    
    function closeFrame() {
        var form = window.parent.document.getElementsByTagName("form")[0];
        var myFrame = window.parent.document.getElementById("myFrame");
        
        if( myFrame){
	        if( form ){
	            form.removeChild( myFrame );
		        delete myFrame;
	        }
        }
    }
    
    function abrirJanela(url,w,h,s) {
        createBackground();
        createFrame(url,w,h,s);
    }
    
    function fecharJanela(reloadparent) {
        if(typeof(window.opener) != "undefined") { window.close(); }
        if(typeof(window.parent) != "undefined") { 
            if(typeof(reloadparent) != "undefined") { window.parent.location.reload(); } 
            else { closeMe(); }
        }
    }
    
    function closeMe(){
        //var form1 = window.parent.document.getElementsByTagName("form")[0];
        var myBackground = window.parent.document.getElementById( "myBackground" );
        var myFrame = window.parent.document.getElementById( "myFrame" );
        
        if( myBackground && myFrame){
	        //if( form1 ){
                myFrame.parentNode.removeChild( myFrame );
		        delete myFrame;
		        
		        myBackground.parentNode.removeChild( myBackground );
		        delete myBackground;
	        //}
        }
    }
    
    function exibirDetalhesVeiculo(tp,cod,exibir,cia) {
        window.location.replace("VisualizarVeiculo.aspx?tpv="+tp+"&cod="+cod);
    }
    
    function comoChegar(cod){
        //window.open("Pop_ComoChegar.aspx?loja=" + cod,"","width=597px,height=580px,menubar=0,scrollbars=yes");
        abrirJanela("pop_comochegar.aspx?loja=" + cod, 618, 898);
    }
    
     function RedirecionaComoChegar(cod) {
        window.location = "Pop_ComoChegar.aspx?loja=" + cod;
    }
    
    function imprimir(tp,cod){
        window.open("ImprimirVeiculo.aspx?tpv=" + tp + "&cod=" + cod,"","width=800,height=520,menubar=0,scrollbars=yes");
    }
    function indicarAmigo(tp,cod){
        window.open("PopIndique.aspx?tpv=" + tp + "&cod=" + cod,"","width=618,height=940,menubar=0,scrollbars=yes");
    }

    function avaliarMeuUsado(id) {
        alert(id);
        //var txtValorMeuUsado = window.document.getElementById(id+"_txtValorMeuUsado");
        //var hCodigoAvaliacaoMeuUsado = window.document.getElementById(id+"_hCodigoAvaliacaoMeuUsado");
        abrirJanela("AvaliacaoVeiculoUsado.aspx?id="+id, 608, 625, "no");
        /*var valorAvaliado = window.showModalDialog("AvaliacaoVeiculoUsado.aspx","","dialogWidth:600px; dialogHeight:600px; scroll:yes;");
        if(valorAvaliado != null) {
            hCodigoAvaliacaoMeuUsado.value = valorAvaliado[0];
            txtValorMeuUsado.value = valorAvaliado[1];
            txtValorMeuUsado.onchange();
        }*/
    }
    function avaliarMeuUsado_callback(id,cod,vlr) {
        if(typeof(window.parent) == "undefined") {return true;}
        
        var txtValorMeuUsado = window.parent.document.getElementById(id+"_txtValorMeuUsado");
        var hCodigoAvaliacaoMeuUsado = window.parent.document.getElementById(id+"_hCodigoAvaliacaoMeuUsado");
        hCodigoAvaliacaoMeuUsado.value = cod;
        
        txtValorMeuUsado.style.display = "inline";
        txtValorMeuUsado.style.display = "block";
        txtValorMeuUsado.value = vlr;
        //txtValorMeuUsado.onchange();
        //fecharJanela();
    }
    
    function SiglasMapa(uf) {
        var id = "";
        
        switch(uf) {
            case "AC":
                id = 1; break;
            case "AL":
                id = 2; break;
            case "AM":
                id = 3; break;
            case "AP":
                id = 4; break;
            case "BA":
                id = 5; break;
            case "CE":
                id = 6; break;
            case "DF":
                id = 7; break;
            case "ES":
                id = 8; break;
            case "GO":
                id = 9; break;
            case "MA":
                id = 10; break;
            case "MG":
                id = 11; break;
            case "MS":
                id = 12; break;
            case "MT":
                id = 13; break;

            case "PA":
                id = 14; break;
            case "PB":
                id = 15; break;
            case "PE":
                id = 16; break;
            case "PI":
                id = 17; break;
            case "PR":
                id = 18; break;
            case "RJ":
                id = 19; break;
            case "RN":
                id = 20; break;
            case "RO":
                id = 21; break;
            case "RR":
                id = 22; break;
            case "RS":
                id = 23; break;
            case "SC":
                id = 24; break;
            case "SE":
                id = 25; break;
            case "SP":
                id = 26; break;
            case "TO":
                id = 27; break;
        }
        window.location.replace("novoResultadosBusca.aspx?tpv=cs&uf="+id);

    }
    
    function adicionarMinhaLista(tpv,cod){
        UserControl_TemplateVeiculoSemi.AdicionarAMinhaLista(tpv,cod,adicionarMinhaLista_callback);
    }
    function adicionarMinhaListaOferta(tpv,cod){
        WebHome_OfertaSeminovos.AdicionarAMinhaLista(tpv,cod,adicionarMinhaLista_callback);
    }
    function adicionarMinhaLista_callback(response) {
        if(response.error != null) {
            alert(response.error);
            return;
        }
        //response.value = error.message
        alert("Veículo adicionado com sucesso!");
    }



