function nuevoAjax()
{ 
    var xmlhttp=false; 
    try 
    { 
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
    }
    catch(e)
    { 
        try
        { 
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        catch(E) { xmlhttp=false; }
    }
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); } 

    return xmlhttp; 
}

/* encode, decode functions */
var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

/* profile */
function add_friend(f)
{
    ajax=nuevoAjax();
    ajax.open("GET","/ajax/ajax.php?type=addfriend&f="+f, true);
	ajax.onreadystatechange=function() 
    { 
        if (ajax.readyState==4)
        { 
            document.getElementById("add_friend").innerHTML=ajax.responseText;
        } 
    }
    ajax.send(null);   
}

function delete_friend(i,n)
{
    var con ;
    con = confirm("¿Eliminas a "+n+" de tus amigos?");
    if (con == true) {
		document.form_bor.vborrar.value = i;
	  	document.form_bor.submit();
	}
}

function delete_game(i,g)
{
    var con ;
    con = confirm("¿Eliminas el juego "+g+" de tus favoritos?");
    if (con == true) {
		document.form_bor.vborrar.value = i;
	  	document.form_bor.submit();
	}
}

function profile_comment(){
	var com, prf, contenedor;
	
	document.getElementById('div_load_comment').style.display = 'block';
	
	contenedor = document.getElementById('comments_div');
	com = document.getElementById('txt_c').value;
	prf = document.getElementById('prf').value;
	
	// com = escape(com);
	com = Url.encode(com);
	
	ajax=nuevoAjax();
	ajax.open("POST", "/ajax/ajax.php?type=prfcom",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			contenedor.innerHTML = ajax.responseText
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("com="+com+"&prf="+prf);
	
	document.getElementById('txt_c').value = '';
	document.getElementById('div_load_comment').style.display = 'none';
}

function delete_comment(c,p)
{
    var con ;
    con = confirm("¿Eliminas el comentario?");
    if (con == true) {
		ajax=nuevoAjax();
    	ajax.open("GET","/ajax/ajax.php?type=delcomment&c="+c+"&p="+p, true);
		ajax.onreadystatechange=function() 
    	{ 
        	if (ajax.readyState==4){ 
            	document.getElementById("comments_div").innerHTML=ajax.responseText;
			} 
		}
		ajax.send(null);
	}
}

function up(){
	ajax=nuevoAjax();
    ajax.open("GET","/ajax/ajax.php?type=up", true);
	ajax.onreadystatechange=function() 
    { 
        if (ajax.readyState==4){ 
           	document.getElementById("div_up_list").innerHTML=ajax.responseText;
		} 
	}
	ajax.send(null);
}
/* reviews */
function review_comment(){
	var com, rev, contenedor;
	
	contenedor = document.getElementById('comments_div');
	com = document.getElementById('txt_c').value;
	rev = document.getElementById('rev').value;
	
	com = Url.encode(com);
	
	ajax=nuevoAjax();
	ajax.open("POST", "/ajax/ajax.php?type=revcom",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			contenedor.innerHTML = ajax.responseText
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("com="+com+"&rev="+rev);
	
	document.getElementById('txt_c').value = '';
}

/* juegos */
function add_favorite(f)
{
    ajax=nuevoAjax();
    ajax.open("GET","/ajax/ajax.php?type=favgame&f="+f, true);
	ajax.onreadystatechange=function() 
    { 
        if (ajax.readyState==4)
        { 
            document.getElementById("div_fav_game").innerHTML=ajax.responseText;
        } 
    }
    ajax.send(null);   
}