// JavaScript Document

function ajaxfavorite( element, gameid, data ){
	element = $("#"+element);
	if( !element.length || !url_fav || !data ) return true;
	if( islogin ){alert("You must be logged in to add this game.");return true;}
	data.gameid = gameid;
	
	var ajax_data = {
		url: url_fav +"?nocache="+Math.random(),
		type: "POST",
		data: data,
		dataType: 'xml',
		timeout: 30000,
		complete:function(){
			if( element.hasClass("loading") )
				element.removeClass("loading");
		},
		success: function(data, status){
			var xml = $(data);
			var prompt_msg = xml.find('prompt').text();
			var error_no = parseInt( xml.find("error").text() );
			var status_no = parseInt( xml.find("status").text() );
			if( typeof(error_no) == 'undefined' ) return
		
			if( prompt_msg ) alert( prompt_msg );
			if( error_no ){
				if( !prompt_msg ) alert("Unknown error occured, try again later.");
			}else if( error_no == 0 ){}
			xml.find("replacehtmlid").children("*").each(function(){
				if( this.tagName == 'script' ){
					$(this).children("*").each(function(){
						eval( $(this).text() );
					});
				}else{
					$("#"+ this.tagName).html( $(this).text() );//.empty().remove();
				}
			});
		},
		error: function(data, status, e){
			if( status == 'timeout' )
				alert( "Server was taking too long to respond.\nThe request may have been submitted but a response wasn't recieved.\nPlease try again later or refresh the page." );
			else if( status )
				alert( "["+ e.number +"] "+ e.message +"\n"+ status +"..");
		}
	}
	if( ! jQuery.active ){
		element.addClass("loading");
		$.ajax( ajax_data );
	}else
		alert("Another request is being sent, please wait before requesting again.");
	
	return true;
}