// function needed for ajax
function getXhr(){
    
    var xhr = null; 
	if(window.XMLHttpRequest) // Firefox and co
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
	}
	else { // XMLHttpRequest not supported by the browser
	   xhr = false; 
	} 
    return xhr;
}

// function that prepares the beginning of the ajax request
// additional_function : javascript function to execute once the response is given
// additional_parameter : only one argument to be given to the additional_function / If null, it takes the ajax_response
function executeAjaxRequest(ajax_request, ajax_div, additional_function, additional_parameter)
{
	if(typeof javascript_called_time != 'undefined' && typeof javascript_secret_key != 'undefined'){
		var sec = '&mtime=' + javascript_called_time + '&hash=' + javascript_secret_key;
	}else{
		var sec = '';
	}	
	var xhr = getXhr();
	var server = window.location.hostname;
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			ajax_response = xhr.responseText;
			ajax_running= false;
			if(document.getElementById('paybox') &&  document.getElementById('paybox-content') )
			{
				
			}
			objdiv=	document.getElementById(ajax_div);
			if(objdiv!=null)
				objdiv.innerHTML = ajax_response;
			if(additional_function)
			{
				// initialize additional_parameter with the ajax response if not exists
				(additional_parameter != null) ? null : additional_parameter = ajax_response;
				eval("" + additional_function + "('" + additional_parameter + "')");
				if(document.getElementById('paybox') &&  document.getElementById('paybox-content') )
			{
				
			}
			}
		}
	}
          
	xhr.open("POST",JS_WEB_URL + "ajax_functions.php",true);
	xhr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); 
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send(ajax_request + sec);
}

//for xml
var xmlHttp


/*----------------------
-- UMPC DEMO DOWNLOAD --
----------------------*/

function ajaxOSXDemoDownload(game_id)
{
	// preparation of the request
	var the_request = "ajax=1&ajax_process=osxDemoDownload";

	the_request		+= "&game_id=" + game_id;

	// the response will be displayed in this div
	var the_div			= 'demo-download-url';
	
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'launchOSXDemoDownload', '-1');
}

//launch the download of a UMPC game
function launchOSXDemoDownload()
{
	if (document.getElementById('demo-download-url'))
	{
		url = document.getElementById('demo-download-url').innerHTML;
		url = url.replace(/&amp;/, '&');
		window.location = url;
	}
}

