function ajax(url, div){
    if (div == "")
        div = 'testetesteteste';
    div = "#"+div;
    jQuery.ajax({
        url: url,
        timeout: 10000,
        error: function(event, request, settings){
            //alert("Ocorreu um Problema Inesperado.\nPressione CTRL+F5 e tente novamente!");
            spinner("off");
        },
        success: function(data) {
            if (data[0] == '$')
                alert(data.replace('$',''));
            if (data[0] == '%'){ // é de um formulário
                var ret = data.split('¨');
                alert(ret[0].replace('%',''));
                window.location = window.location;
            }
            if (data[0] == '^')
                jQuery(div).html(data.split('^'));
            if (data[0] == '#'){ // veio do sistema Completus
                //var type = substr(data,3,2);
                var type = data.substr(2,2);
                if (type == 'LG'){
                    if (data.search("false") != -1){
                        alert("Login Incorreto!\nTente Novamente.");
                    }
                    else{
                        token = data.split('|')[4];
                        jQuery.ajax({
                            url: '/painel/login.php?retorno='+data.replace('#','')+'&url='+window.location,
                            success: function(data) {
                                if (data == "sucesso"){
                                    alert("Login efetuado com sucesso.\nEsta página será recarregada.\nAguarde!");
                                    window.location = window.location;
                                }
                                else{
                                    alert("Houve um Problema Inesperado.\nPressione CTRL+F5 e tente novamente!");
                                }
                            }
                        });
                        //window.location='/painel/login.php?retorno='+data.replace('#','')+'&url='+window.location;
                    }
                }
                else if (type == 'US'){
                //alert('é de usuário');
                }
            }
            if (data[0] == '¨'){ // deve preencher campos
                var ret = data.replace('¨','').split('&+&');
                for (var i in ret){
                    if (!isNaN(i)){
                    temp = ret[i].split('=+=');
                    id = temp[0];
                    vl = temp[1];
                    if (document.getElementById(id).nodeName == 'INPUT')
                        document.getElementById(id).value = vl;
                    else
                        document.getElementById(id).innerHTML = vl;
                    }
                }
            }
            if (data[0] == "*"){
                alert(data);
                window.location=window.location;
            }
            if (data[0] != '¨' && data[0] != '^' && data[0] != '$' && data[0] != '#'){
                jQuery(div).html("");
                jQuery(div).html(data);
            }
            spinner("off");
        }
    });
}


function spinner(mode){
    jQuery('#spinner').height(jQuery('#body_page').innerHeight());    
    if (mode == "on"){
        jQuery('#spinner').css("display", "block");
        jQuery('#spinner').css("z-index", "10000");
        return true;
    }
    if(mode == "off"){
        jQuery('#spinner').css("display", "none");
        jQuery('#spinner').css("z-index", "-10000");
        return true;
    }
}
function ajax2(url, div){    
    if (div == "") div = 'testetesteteste';
    div = "#"+div;
    spinner("on");
    jQuery('#body_page').append("<input type='hidden' id='ajax_ajax_url' value='"+url+"'>");
    jQuery('#body_page').append("<input type='hidden' id='ajax_ajax_div' value='"+div+"'>");
}
function out_spinner(){
    if (jQuery('#ajax_ajax_url').attr('value') != "")
        make_ajax(jQuery('#ajax_ajax_url').attr('value'), jQuery('#ajax_ajax_div').attr('value'));
    jQuery('#ajax_ajax_url').remove();
    jQuery('#ajax_ajax_div').remove();
}
function make_ajax(url, div){
    jQuery.ajax({
      url: url,
      timeout: 10000,
      error: function(event, request, settings){
        //alert("Ocorreu um Problema Inesperado.\nPressione CTRL+F5 e tente novamente!");
        spinner("off");
      },
      success: function(data) {
        //alert(data);
        if (data[0] == '$') alert(data.replace('$',''));
    	if (data[0] == '%'){ // é de um formulário
    		var ret = data.split('¨');
    		alert(ret[0].replace('%',''));
    		window.location = window.location;
    	}
        if (data[0] == '^') jQuery(div).html(data.split('^'));
    	if (data[0] == '#'){ // veio do sistema Completus
            //var type = substr(data,3,2);
            var type = data.substr(2,2);
            if (type == 'LG'){
        		if (data.search("false") != -1){
                    alert("Login Incorreto!\nTente Novamente.");
                    spinner("off");
        		}
        		else{
        		  token = data.split('|')[4];
                  jQuery.ajax({
                      url: '/painel/login.php?retorno='+data.replace('#','')+'&url='+window.location,
                      success: function(data) {
                        if (data == "sucesso"){
                            alert("Login efetuado com sucesso.\nEsta página será recarregada.\nAguarde!");
                            window.location = window.location;
                        }
                        else{
                            alert("Houve um Problema Inesperado.\nPressione CTRL+F5 e tente novamente!");
                            spinner("off");
                        }
                      }
                  });
        		  //window.location='/painel/login.php?retorno='+data.replace('#','')+'&url='+window.location;
        		}
            }
            else if (type == 'US'){
                //alert('é de usuário');
            }
    	}
        if (data[0] == '¨'){ // deve preencher campos
            var ret = data.replace('¨','').split('&+&');
            for (var i in ret){
                if (!isNaN(i)){
                    temp = ret[i].split('=+=');
                    id = temp[0];
                    vl = temp[1];
                    if (document.getElementById(id).nodeName == 'INPUT') document.getElementById(id).value = vl;
                    else document.getElementById(id).innerHTML = vl;
                }
        	}
            spinner("off");
        }
        if (data[0] != '¨' && data[0] != '^' && data[0] != '$' && data[0] != '#'){
            jQuery(div).html("");
            jQuery(div).html(data);
            spinner("off");
        }
      }
    });
}
function ajaxForm(formulario, div)
{    
    var metodo = "GET";
    var method = formulario.method.toUpperCase();
    //var url = formulario.getAttribute('action');
    var url = formulario.action;
    var action = "/includes/ajax_form.php?method_ajax="+method+"&url_ajax="+url+"&";
    var variaveis = "";
    var NomeTag;
    var TipoInput;
    for (var i=0;i<formulario.length;i++)
    {
        NomeTag = formulario.elements[ i ].tagName.toUpperCase();
        if(NomeTag == "INPUT" || NomeTag == "SELECT" || NomeTag == "TEXTAREA")
        {
            TipoInput = formulario.elements[ i ].type.toUpperCase();
            if(TipoInput == "FILE")
                alert('ERRO: Năo é possível enviar arquivos por AJAX');
            if(TipoInput  == "RADIO" || TipoInput == "CHECKBOX")
            {
                if(formulario.elements[ i ].checked) variaveis += formulario.elements[ i ].name + "=" + escape(formulario.elements[ i ].value) + "&";
            }
            else{
                if(TipoInput != "SUBMIT") variaveis += formulario.elements[ i ].name + "=" + escape(formulario.elements[ i ].value) + "&";
            }
        }
    }
    if(metodo == "GET")
    {
        action += variaveis;
        variaveis = null;
    }
    //alert(action);
    ajax(action, div);
    return false;
}
