﻿function StringBuffer() { this.buffer = []; }
StringBuffer.prototype.append = function(string) {
    this.buffer.push(string);
    return this;
}

StringBuffer.prototype.toString = function() {
    return this.buffer.join("");
}

var N = 0;
var Capit = true;
var Enabled = true;
function SelecCidade()
{
    N = 0;
    Capit = true;
    Enabled = (arguments[2] != null ? arguments[2] : true);
    doRequest('server.aspx?action=selcidade&est='+ arguments[0].value + '&ret='+ arguments[1] ,ResponseCidade);
}

function AutoSelecCidade()
{
    N = arguments[2];
    Capit = false;
    Enabled = (arguments[3] != null ? arguments[3] : true);
    doRequest('server.aspx?action=selcidade&est='+ arguments[0] + '&ret='+ arguments[1] ,ResponseCidade);
}

function ResponseCidade() {
    try {
        var i, x;
        var f = eval('(' + arguments[0] + ')');

        for (i = 0; i < f.length; i++) {
            var ddl = document.getElementById(f[i]["RET"]);
            ddl.length = 0;

            if (ddl.disabled && Enabled)
                ddl.disabled = "";

            var Cidades = f[i]["CIDADES"].split(",");

            for (x = 0; x < Cidades.length; x++) {
                var sCidade = Cidades[x].split(";");
                ddl.options[x] = new Option(sCidade[0], x);
                if (sCidade[1] != null && Capit)
                    N = x;
            }
            Selecione(f[i]["RET"]);
        }
    }
    catch (ee)
    { }
}

function Selecione()
{
    var CMP = document.getElementById(arguments[0]);
    try
    {
        CMP.value = N;
    }
    catch(ee)
    {
        CMP.value = 0;
    }
    if (!Enabled)
        CMP.disabled = "disabled";
}

function ChecaData()
{   
    doRequest('server.aspx?action=checadata&data='+ arguments[0] ,ResponseData);
}

function ResponseData()
{
    try
    {
     var i;
     var f = eval('('+ arguments[0] +')');
     
     var dvAlert = document.getElementById('dvAlert');
     var dvPeriodo = document.getElementById('dvPeriodo');
     
     var P1 = document.getElementById('rdPeriodo1');
     var P2 = document.getElementById('rdPeriodo2');
     var P3 = document.getElementById('rdPeriodo3');
     var P4 = document.getElementById('rdPeriodo4');
     var P5 = document.getElementById('rdPeriodo5');
     
     P1.checked = "";
     P1.disabled = "disabled";
     P2.checked = "";
     P2.disabled = "disabled";
     P3.checked = "";
     P3.disabled = "disabled";
     P4.checked = "";
     P4.disabled = "disabled";
     P5.checked = "";
     P5.disabled = "disabled";
     
     dvAlert.style.display = '';
     dvPeriodo.style.display = 'none';
     var Valida = false;
     
     for(i = 0; i < f.length; i++) 
     {
        if(eval(f[i]["Per1"]) < 2)
        {
            P1.disabled = "";
            Valida = true;
        }
        
        if(eval(f[i]["Per2"]) < 2)
        {
            P2.disabled = "";
            Valida = true;
        }
        
        if(eval(f[i]["Per3"]) < 2)
        {
            P3.disabled = "";
            Valida = true;
        }
        
        if(eval(f[i]["Per4"]) < 2)
        {
            P4.disabled = "";
            Valida = true;
        }
        
        if(eval(f[i]["Per5"]) < 2)
        {
            P5.disabled = "";
            Valida = true;
        }
        
        if(Valida)
        {
           dvAlert.style.display = 'none';
           dvPeriodo.style.display = '';
        }
        else
        {
           dvAlert.innerHTML = "<span style=\"color:#FF0000\"><b>DATA INDISPONÍVEL</b></span>";
        }
     }
    }
    catch(ee)
    {}
}

var Location = true;
var Go = "";
function Reload() {
    var Path = "";
    if (arguments[0] != null) {
        Location = false;
        Path = "../"
    }

    doRequest(Path + 'server.aspx?action=reload', ResponseReload);
}

function ResponseReload()
{
    try {
        var i;
        var f = eval('(' + arguments[0] + ')');

        for (i = 0; i < f.length; i++) {
            if (f[i]["RESET"] == "True") {
                if (Location) {
                    FadeRecX();
                    window.location.reload();
                }
                else {
                    window.location.reload();
                }
            }
        }
    }
    catch (ee) {
        //alert(ee + "-" + Location);
    }
}


