// variáveis globais
var arVersion   = navigator.appVersion.split("MSIE")
var version     = parseFloat(arVersion[1])
var isValid     = false;

// tkz to Anderson Ouriques - mundopixel.com.br
function FormataValor(campo, tammax, teclapres)
{
    //var tecla = teclapres.keyCode;
    var tecla = window.event ? teclapres.keyCode : teclapres.which;

    vr = document.getElementById(campo).value;
    vr = vr.replace( "/", "" );
    vr = vr.replace( "/", "" );
    vr = vr.replace( ",", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    tam = vr.length;

    if (tam < tammax && tecla != 8){
        tam = vr.length + 1 ;
    }

    if (tecla == 8 ){
        tam = tam - 1 ;
    }

    if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
    {
        if ( tam <= 2 )
        {
            document.getElementById(campo).value = vr ;
        }
        if ( (tam > 2) && (tam <= 5) )
        {
            document.getElementById(campo).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ;
        }
        if ( (tam >= 6) && (tam <= 8) )
        {
            document.getElementById(campo).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;
        }
        if ( (tam >= 9) && (tam <= 11) )
        {
            document.getElementById(campo).value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;
        }
        if ( (tam >= 12) && (tam <= 14) )
        {
            document.getElementById(campo).value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;
        }
        if ( (tam >= 15) && (tam <= 17) )
        {
            document.getElementById(campo).value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;
        }
    }
}

// Gera um select com o array repassado
function generateSelect (selectId, campos, selecionado)
{
    var tmpCampoSel = false;
    var indiceStart = 0;

    while (selectId.options.length > 0) {
        selectId.options[0] = null;
    }

    if (campos != "") {
        for (var i=indiceStart; i < campos.length; i++) {
            tmpCampos       = unescape( campos[i] );
            aTmpCampos      = tmpCampos.split(";");
            tmpCampoId      = aTmpCampos[0];
            tmpCampoValue   = aTmpCampos[1];
            tmpCampoSel     = false;

            if ( tmpCampoId == selecionado) {
                tmpCampoSel = true;
            }

            selectId.options[i] = new Option(tmpCampoValue, tmpCampoId, tmpCampoSel);

            if ( tmpCampoId == selecionado) {
                selectId.options[i].selected = true;
            }
        }
    } else {
        selectId.options[0] = new Option("------------", 0);
    }
}

// tkz to http://rogeriolino.wordpress.com
function replaceAll (string, token, newtoken)
{
    while (string.indexOf(token) != -1) {
        string = string.replace(token, newtoken);
    }

    return string;
}

function paises ()
{
    new Ajax.Request('mundo.php?param=paises', {
        method: 'get',
        onComplete: function(requisicaoOriginal)
        {
            // Transforma a lista JSON em Javascript
            var p           = $("fgPais");
            var pais        = $F("fgPaisOrig");
            var aCampos     = requisicaoOriginal.responseText.split(",");

            generateSelect(p, aCampos, pais);

            buscaEstados();
        }
    });
}

function buscaEstados ()
{
    new Ajax.Request('mundo.php?param=estados&pais=' + $F('fgPais'), {
        method: 'get',
        onComplete: function (requisicaoOriginal)
        {
            // Transforma a lista JSON em Javascript
            var e           = $("fgEstado");
            var estado      = $F("fgEstadoOrig");
            var aCampos     = requisicaoOriginal.responseText.split(",");

            generateSelect(e, aCampos, estado);

            buscaCidades ()
        }
    });
}

function buscaCidades ()
{
    new Ajax.Request('mundo.php?param=cidades&pais=' + $F("fgPais") + "&estado=" + $F("fgEstado"), {
        method: 'get',
        onComplete: function (requisicaoOriginal)
        {
            // Transforma a lista JSON em Javascript
            var c           = $("fgCidade");
            var cidade      = $F("fgCidadeOrig");
            var aCampos     = requisicaoOriginal.responseText.split(",");

            generateSelect(c, aCampos, cidade);
        }
    });
}

function validateEmail(e)
{
    if(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/.test(e.value))
    {
        if(!isValid)
        {
            $(e).morph('border-color:#00FF00', {
                duration:.3
            });
            isValid = true;
        }
    } 
    else
    {
        if(isValid)
        {
            $(e).morph('border-color:#FF0000', {
                duration:.3
            });
            isValid = false;
        }
    }
}
