var preloadedImages = new Array();
var global_comment_type = 0;
var global_commentid = 0;

function preloadImage(url, imageId)
{
    preloadedImages[imageId] = new Image();
    preloadedImages[imageId].src = url;
}

function http_request_with_callback(method, command, urlComplement, timer, data, callback)
{
    var url = famooz_base_url + "xmlhttp.php?page=" + command;

    if (urlComplement != null && urlComplement != '') {
        url = url  + '&' + urlComplement;
    }
    if(document.all) { var XhrObj = new ActiveXObject("Microsoft.XMLHTTP"); }
    else { var XhrObj = new XMLHttpRequest(); }
        
    if (method == 'GET' && data != null) {
        url = url + "&" + data;
        data = null;
    }
	//alert("url="+url);
    XhrObj.open(method, url, true);
    XhrObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    XhrObj.onreadystatechange = function() {
        if (XhrObj.readyState == 4) {
            if (callback != null) {
                callback(XhrObj);
            }
        }
    }
    
    if (method == "POST")
    XhrObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    setTimeout (function () { XhrObj.send(data); }, timer);
}

function comment_removed(request)
{
    if (request.status == 200) {
        var xml = request.responseXML;
        if (xml != null) {
            var response = xml.getElementsByTagName('response').item(0);         
			if (response.firstChild.nodeValue != "false") {
				remove_comment(global_comment_type, global_commentid);
			} else {
				alert("Famooz Error : comment remove");
			}
        }
    }
}


function accept_freekiss(commentid, freekissid, fromuserid, touserid,comment_type)
{
	global_comment_type = comment_type;
	global_commentid = commentid;
	http_request_with_callback("GET", "acceptfreekiss", "commentid=" + commentid + "&freekissid=" + freekissid + "&fromuserid=" + fromuserid + "&touserid=" + touserid, 0, null, comment_removed);
}

function disaprove_freekiss(commentid, freekissid, fromuserid, touserid,comment_type)
{
	global_comment_type = comment_type;
	global_commentid = commentid;
    http_request_with_callback("GET", "disaprovefreekiss", "commentid=" + commentid + "&freekissid=" + freekissid + "&fromuserid=" + fromuserid + "&touserid=" + touserid, 0, null, comment_removed);
}

function accept_freefuck(commentid, freefuckid, fromuserid, touserid,comment_type)
{
	global_comment_type = comment_type;
	global_commentid = commentid;
	http_request_with_callback("GET", "acceptfreefuck", "commentid=" + commentid + "&freefuckid=" + freefuckid + "&fromuserid=" + fromuserid + "&touserid=" + touserid, 0, null, comment_removed);
}

function disaprove_freefuck(commentid, freefuckid, fromuserid, touserid,comment_type)
{
	global_comment_type = comment_type;
	global_commentid = commentid;
    http_request_with_callback("GET", "disaprovefreefuck", "commentid=" + commentid + "&freefuckid=" + freefuckid + "&fromuserid=" + fromuserid + "&touserid=" + touserid, 0, null, comment_removed);
}

function accept_marriage(commentid, fromuserid, touserid,comment_type)
{
	global_comment_type = comment_type;
	global_commentid = commentid;
	http_request_with_callback("GET", "acceptmarriage", "commentid=" + commentid + "&fromuserid=" + fromuserid + "&touserid=" + touserid, 0, null, comment_removed);
}

function disaprove_marriage(commentid, fromuserid, touserid,comment_type)
{
	global_comment_type = comment_type;
	global_commentid = commentid;
	http_request_with_callback("GET", "disaprovemarriage", "commentid=" + commentid + "&fromuserid=" + fromuserid + "&touserid=" + touserid, 0, null, comment_removed);
}

function change_user_voting_star(starId, betId, index)
{
    var i;
    for (i = 1; i <= index; i++) {
        var starName = "star_bg";
        document.getElementById(starId + i).src = preloadedImages[starName].src;
    }
    for (; i <= 5; i++) {
        var starName;
        starName = "star_bg"+i;
        document.getElementById(starId + i).src = preloadedImages[starName].src;
    }
}

function giveMoney (intro,userID,nb_points)
{
	message=intro+" : <br/><br/>";
	for (var money=1;money<nb_points;money=money+money) {
		message = message + "<span class'big dark bold'>&gt;</span> <a class='pink_link_grey' href='"+famooz_base_url+"give_money/"+userID+"/"+money+"' >"+money+"B$</a><br/> ";
	}
	FamoozPopup(message,"#","ANNULER","ANNULER",1,true);
}

function buyWallOfFame (intro,min,max)
{
	message=intro+" : <br/><br/>";
	price_step=parseInt((max-min)/5);
	for (var price=min;price<max+price_step;price=price+price_step) {
		message = message + "<span class'big dark bold'>&gt;</span> <a class='pink_link_grey' href='"+famooz_base_url+"buy_wall_of_fame/"+price+"' >acheter sa place à "+price+"B$</a><br/> ";
	}
	FamoozPopup(message,"#","ANNULER","ANNULER",1,true);
}
