/**
 * dmosilveira
 * Funções padrões usadas no ajax
 * 2007-06-26
 */

//--class
function ajaxInit()
{
    ajax = null;
    if(window.XMLHttpRequest) { // Mozilla, Safari,...
        ajax = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        try
		{
            ajax = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
		{
            try
			{
                ajax = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
			{
                ajax = null;
                alert('AJAX não suportado neste navegador. Por favor entre em contato com o adminitrador do sistema.');
            }
        }
    }
}

//----funções complementares

// url_decode version 1.0  
// decodificar acentuação no retorno de dados
function ajaxUrlDecode(str)
{
    var n, strCode, strDecode = "";
    for(n=0;n<str.length;n++)
	{
        if(str.charAt(n)=='%') {
            strCode = str.charAt(n + 1) + str.charAt(n + 2);
            strDecode += String.fromCharCode(parseInt(strCode, 16));
            n += 2;
        } else {
            strDecode += str.charAt(n);
        }
    }
    return strDecode;  
} 
// url_encode version 1.0
// codificar acentuação no envio de dados
function ajaxUrlEncode(str)
{
    var hex_chars = '0123456789ABCDEF';
    var noEncode = /^([a-zA-Z0-9\_\-\.])$/;
    var n, strCode, hex1, hex2, strEncode = '';  
    for(n = 0; n < str.length; n++) {
        if(noEncode.test(str.charAt(n))) {
            strEncode += str.charAt(n);
        } else {
            strCode = str.charCodeAt(n);
            hex1 = hex_chars.charAt(Math.floor(strCode/16));
            hex2 = hex_chars.charAt(strCode%16);
            strEncode += '%'+(hex1+hex2);
        }
    }
    return strEncode;
}
// executa version 1.0
function ajaxExecute(response)
{
    var ini = 0;
    while (ini!=-1)
	{
        ini = response.indexOf('<script', ini);
        if(ini>=0) {
            ini = response.indexOf('>', ini) + 1;
            var fim = response.indexOf('</script>',ini);
            codigo=response.substring(ini,fim);
            eval(codigo);
        }
    }
}