﻿function TextBoxFormatNumber(textBox) {
    try {
        if (textBox.value != '') {
            textBox.value = FormatNumber(Numero(textBox.value), 2, true, false) + '';
        }
    }
    catch (e) {
    }
}

function SomenteNumeros(textBox) {
    try {
        if (textBox.value != '') {
            textBox.value = RemoveNaoNumericos(textBox.value);
        }
    }
    catch (e) {
    }
}

function RemoveNaoNumericos(checkString) {
    rExp = /[^0-9]/g;
    return checkString.replace(rExp, "");
}


//FormatNumber(Numero(Valor),2, true, false);
function FormatNumber(strNum, nDec, bSep, bEng) {
    var cSep1, cSep2, ret = "", strint = "", strdec = "", nPos, nFlg, nIdx = 0, nLen;
    cSep1 = (!bEng ? "." : ",");
    cSep2 = (nDec > 0 ? (!bEng ? "," : ".") : "");

    strNum = strNum.toString();
    for (nPos = strNum.length - 1; nPos >= 0; nPos--)
        if (strNum.charAt(nPos) == "," || strNum.charAt(nPos) == ".") {
        nIdx = nPos;
        break;
    }
    if (nIdx) {
        ret = strNum.substring(0, nIdx).replace(/,|\./g, "");
        if (bSep)
            for (nPos = ret.length - 1, nFlg = 1; nPos >= 0; nPos--, nFlg++)
            strint = (nFlg % 3 == 0 && nPos ? cSep1 : "") + ret.charAt(nPos) + strint;
        else
            strint = ret;

        strdec = strNum.substring(nIdx + 1, nIdx + 1 + nDec) + "0000000000";
        ret = strint + cSep2 + strdec.substring(0, nDec);
    }
    else {
        if (bSep)
            for (nPos = strNum.length - 1, nFlg = 1; nPos >= 0; nPos--, nFlg++)
            strint = (nFlg % 3 == 0 && nPos ? cSep1 : "") + strNum.charAt(nPos) + strint;
        else
            strint = strNum;

        ret = strint + (strNum != "" ? cSep2 + "0000000000".substring(0, nDec) : "");
    }
    return (ret);
}

function Numero(Numero) {
    texto = (('000' + Numero.toString()).replace('.', '')).replace(',', '.');
    return parseFloat(texto);
}

function PopUpExibirVeiculo(VeiculoID) {
    url = document.forms[0].elements['txtUrl'].value;
    window.open(url + "/" + VeiculoID.toString(), "VeiculoID_" + VeiculoID, "status=yes,toolbar=yes,menubar=yes,location=yes,resizable=yes,scrollbars=yes,maximized=yes");
}

function PopUpAbreVeiculo(VeiculoID) {
    url = document.forms[0].elements['txtUrl'].value;

    window.location = url + "/" + VeiculoID.toString();
}


function Paginacao(Pagina) {
    var url = new String(window.location.href).replace("#", "");

    try {
        PaginaAtual = GetQueryString("Pagina");

        if (PaginaAtual != "") {
            url = url.replace("&Pagina=" + PaginaAtual, "");
        }

        url = url + "&Pagina=" + Pagina;
    } catch (Ex) {}

    window.location = url;
}

function GetQueryString(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);

    if (results == null)
        return "";
    else
        return results[1];
}

