var ajax_in_progess = false;

// function to change the operator list when we change the country
function ajaxChangeOperator(country_id, from)
{
	if (from == "choose_phone"){
		document.getElementById('list-manufacturers').innerHTML = "";
		document.getElementById('list-platforms').innerHTML = "";
	}
	
	// preparation of the request
	var the_request 	= "ajax=1&ajax_process=changeOperatorList";
	the_request			+= "&country_id=" + country_id;	
	the_request			+= "&from=" + from;
	
	// the response will be displayed in this div
	var the_div			= 'div_choose_operator';
	
	// reset the operator list to the first element
	if (document.getElementById('platform_id'))
	{
		document.getElementById('platform_id').selectedIndex = "init";
	}
	
	// reset the manufacturer list to the first element
	if (document.getElementById('manufacturer_id'))
	{
		document.getElementById('manufacturer_id').selectedIndex = "init";
		ajax_in_progess = true;
	}
	
	// reset the zone list to the first element
	if (document.getElementById('zone_id'))
	{
		document.getElementById('zone_id').selectedIndex = "init";
	}
	
	// show the loading animation
	if (document.getElementById(the_div))
	{
		document.getElementById(the_div).innerHTML += ' <img src="'+JS_WEB_STATIC_COMMON+'images/billing/loading.gif" alt="" />';
		ajax_in_progess = true;
	}
	
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'disableInProgress', '-1');
}


// function to change the zones list when we change the country
function ajaxChangeZone(country_id)
{
	// preparation of the request
	var the_request 	= "ajax=1&ajax_process=changeZoneList";
	the_request			+= "&country_id=" + country_id;	
	
	//fetch the country id in hidden if possible
	if(document.getElementById('country') && document.getElementById('country').value != '')
	{
		the_request		+= '&country=' + document.getElementById('country').value;
	}
	
	// the response will be displayed in this div
	var the_div			= 'div_choose_zone';
	
	// show the loading animation
	if (document.getElementById(the_div) && country_id == 38)
	{
		if(document.getElementById('div_label_zone') && document.getElementById('error_zone') && document.getElementById(the_div))
		{
			document.getElementById('div_label_zone').style.display = 'block';
			document.getElementById('error_zone').style.display = 'block';
			document.getElementById(the_div).style.display = 'block';
		}
		ajax_in_progess = true;
	}
	else
	{
		if(document.getElementById('div_label_zone') && document.getElementById(the_div))
		{
			document.getElementById('div_label_zone').style.display = 'none';
			document.getElementById('error_zone').style.display = 'none';
			document.getElementById(the_div).style.display = 'none';
		}
	}
	
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'disableInProgress', '-1');
}

// function to change the manufacturer list when we change the carrier
function ajaxChangeManufacturers(operator_id, from)
{
	// preparation of the request
	var the_request 	= "ajax=1&ajax_process=changeManufacturerList";
	//the_request			+= "&manufacturer_id=" + manufacturer_id;
	the_request			+= "&operator_id=" + operator_id;		
	
	// the response will be displayed in this div
	var the_div			= 'div_choose_manufacturer';
	
	// reset the manufacturer list to the first element
	if (document.getElementById('manufacturer_id'))
	{
		document.getElementById('manufacturer_id').selectedIndex = "init";
		ajax_in_progess = true;
	}
	
	// reset the platform list to the first element
	if (document.getElementById('platform_id'))
	{
		document.getElementById('platform_id').selectedIndex = "init";
		ajax_in_progess = true;
	}	
	
	// show the loading animation
	if (document.getElementById(the_div))
	{
		document.getElementById(the_div).innerHTML += ' <img src="'+JS_WEB_STATIC_COMMON+'images/billing/loading.gif" alt="" />';
	}
	
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'disableInProgress', '-1');
}

// function to change the platform list when we change the manufacturer
function ajaxChangePlatform(manufacturer_id, operator_id)
{
	// preparation of the request
	var the_request 	= "ajax=1&ajax_process=changePlatformList";
	the_request			+= "&manufacturer_id=" + manufacturer_id;
	the_request			+= "&operator_id=" + operator_id;		//additional
	
	// the response will be displayed in this div
	var the_div			= 'div_choose_platform';
	
	// reset the platform list to the first element
	if (document.getElementById('platform_id'))
	{
		document.getElementById('platform_id').selectedIndex = "init";
		ajax_in_progess = true;
	}
	
	// show the loading animation
	if (document.getElementById(the_div))
	{
		document.getElementById(the_div).innerHTML += ' <img src="'+JS_WEB_STATIC_COMMON+'images/billing/loading.gif" alt="" />';
	}
	
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'disableInProgress', '-1');
}

// function used in the phone recognition (called from ajax_phone_recognition.js)
function ajaxRefreshManufacturerAndPlatformSelect()
{
	// REFRESH MANUFACTURER SELECT :
	// preparation of the request
	var the_request_1 	= "ajax=1&ajax_process=refreshManufacturerList";

	// the response will be displayed in this div
	var the_div_1		= 'div_choose_manufacturer';
		
	// execute the ajax request
	executeAjaxRequest(the_request_1, the_div_1, null, null);
	
	// REFRESH PLATFORM SELECT
	// preparation of the request
	var the_request_2 	= "ajax=1&ajax_process=refreshPlatformList";

	// the response will be displayed in this div
	var the_div_2		= 'div_choose_platform';
		
	// execute the ajax request
	executeAjaxRequest(the_request_2, the_div_2, null, null);
}


function ajaxDisplayError(array_error_code)
{
	// preparation of the request
	var the_request = "ajax=1&ajax_process=displayError";

	the_request		+= array_error_code;

	// the response will be displayed in this div
	var the_div			= 'error_box';
	
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, null, null);
}


/*---------------------
-- CHOOSE YOUR PHONE --
---------------------*/

// function to get a list of platforms
function ajaxGetPlatformsList(operator_id, manufacturer_id)
{
	displayWait("choose-loading");

	// preparation of the request
	var the_request 	= "ajax=1&ajax_process=getPlatformsList";
	the_request			+= "&manufacturer_id=" + manufacturer_id;	
	the_request			+= "&operator_id=" + operator_id;
	
	// the response will be displayed in this div
	var the_div			= 'choose-list-platforms';
	
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'stopDisplayWait', 'choose-loading');
}

/* old function
function ajaxGetPlatformsList(manufacturer_id)
{
	displayWait("choose-loading");

	// preparation of the request
	var the_request 	= "ajax=1&ajax_process=getPlatformsList";
	the_request			+= "&manufacturer_id=" + manufacturer_id;	
	
	// the response will be displayed in this div
	var the_div			= 'choose-list-platforms';
	
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'stopDisplayWait', 'choose-loading');
}
*/
// function to get the manufacturer list when a operator is selected
function ajaxGetManufacturers(operator_id, from)
{
	if (from == "choose_phone"){
		document.getElementById('list-manufacturers').innerHTML = "";
		document.getElementById('list-platforms').innerHTML = "";
	}
	// preparation of the request
	var the_request 	= "ajax=1&ajax_process=getManufacturersList";
	the_request			+= "&operator_id=" + operator_id;	
	
	// the response will be displayed in this div
	var the_div			= 'choose-list-manufacturers';
	
	// show the loading animation
	if (document.getElementById(the_div))
	{
		document.getElementById(the_div).innerHTML += ' <img src="'+JS_WEB_STATIC_COMMON+'images/billing/loading.gif" alt="" />';
		ajax_in_progess = true;
	}
	
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'disableInProgress', '-1');
}

// set a specific platform
function ajaxSetPlatform(platform_id, manufacturer_id)
{
	displayWait("choose-loading");

	// preparation of the request
	var the_request		= "ajax=1&ajax_process=setPlatform";
	the_request			+= "&platform_id=" + platform_id;
	the_request			+= "&manufacturer_id=" + manufacturer_id;
	the_request			+= "&country_id=" + document.getElementById('country_id').value;
	if (document.getElementById('country_id').value == 38)
	{
		the_request		+= "&zone_id=" + document.getElementById('zone_id').value;	
	}
	the_request			+= "&operator_id=" + document.getElementById('operator_id').value;

	//the response will be displayed in this div
	var the_div			= 'choose-list-platforms';
	
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'goRedirect', '-1');
}


// unset the currently selected platform
function ajaxUnsetPlatform()
{
	displayWait("choose-loading");
	
	// preparation of the request
	var the_request		= "ajax=1&ajax_process=unsetPlatform";
	
	//the response will be displayed in this div
	var the_div			= 'choose-list-platforms';
	
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'goRedirect', '-1');
}


/*----------------
-- PRODUCT PAGE --
----------------*/

function ajaxGetProductVideo(product_id)
{
	// prevent a user from navigating away if an AJAX request is underway
	if (ajax_in_progess && ajax_in_progess == true)
	{
		return;
	}
	displayWait("week-presentation-media");
	show_media();
	
	// preparation of the request
	var the_request		= "ajax=1&ajax_process=getProductVideo";
	the_request			+= "&product_id=" + product_id;
	
	//the response will be displayed in this div
	var the_div = "week-presentation-media";
	ajax_in_progess = true;
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'create_video', product_id);
}


function ajaxGetProductDemo(product_id)
{
	// prevent a user from navigating away if an AJAX request is underway
	if (ajax_in_progess && ajax_in_progess == true)
	{
		return;
	}
	displayWait("week-presentation-media");
	show_media();
	
	// preparation of the request
	var the_request		= "ajax=1&ajax_process=getProductDemo";
	the_request			+= "&product_id=" + product_id;
	
	//the response will be displayed in this div
	var the_div = "week-presentation-media";
	ajax_in_progess = true;
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'disableInProgress', '-1');
}


function ajaxGetProductHiScores(product_id)
{
	// prevent a user from navigating away if an AJAX request is underway
	if (ajax_in_progess && ajax_in_progess == true)
	{
		return;
	}
	displayWait("week-presentation-media");
	show_media();
	
	// preparation of the request
	var the_request		= "ajax=1&ajax_process=getProductHiScores";
	the_request			+= "&product_id=" + product_id;
	
	//the response will be displayed in this div
	var the_div = "week-presentation-media";
	ajax_in_progess = true;
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'disableInProgress', '-1');
}


function ajaxGetProductAvailability(product_id)
{
	// prevent a user from navigating away if an AJAX request is underway
	if (ajax_in_progess && ajax_in_progess == true)
	{
		return;
	}
	displayWait("week-presentation-media");
	show_media();
	
	// preparation of the request
	var the_request		= "ajax=1&ajax_process=getProductAvailability";
	the_request			+= "&product_id=" + product_id;
	
	//the response will be displayed in this div
	var the_div = "week-presentation-media";
	ajax_in_progess = true;
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'disableInProgress', '-1');
}


/*--------------------
-- PRODUCT HISCORES --
--------------------*/

function ajaxGetProductHiScoresList(product_id, community_id, start_at)
{
	//Set some default values
	if (!community_id)	community_id = document.getElementById('community_id').value;
	if (!start_at)		start_at = 1;
	
	displayWait("hiscores-loading");
	
	// preparation of the request
	var the_request		= "ajax=1&ajax_process=getProductHiScoresList";
	the_request			+= "&product_id=" + product_id;
	the_request			+= "&community_id=" + community_id;
	the_request			+= "&start_at=" + start_at;
	
	//the response will be displayed in this div
	var the_div = "community-" + community_id;
	if (!document.getElementById(the_div))
	{
		the_div = "hiscores-list-product-inside";
	}
	
	//Disable the AJAX progress indicator: this request happens inside another one
	disableInProgress();

	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'stopDisplayWait', 'hiscores-loading');
}


function ajaxHiScoresReloadOptions(type, param1, param2)
{
	//If we switched to a new product, change of URL
	if (type == 'product')
	{
		redirect(param1);
	}
	
	//If we switched to a different country, reload the list of communities
	else if (type == 'country')
	{
		displayWait("hiscores-loading");
		
		// preparation of the request
		var the_request		= "ajax=1&ajax_process=hiScoresReloadOptions";
		the_request			+= "&product_id=" + param1;
		the_request			+= "&country_id=" + param2;
		
		//the response will be displayed in this div
		var the_div = "hiscores-suboptions";
		ajax_in_progess = true;
		// execute the ajax request
		executeAjaxRequest(the_request, the_div, 'ajaxGetProductHiScoresList', param1);
	}
	else
	{
		alert ('Error: ' + type + 'is not defined!');
	}
}


/*------------------
---PRODUCT RATING---
--------------------*/
function ajaxRateThisGame(product_id)
{
	displayWait("week-content");
	
	// preparation of the request
	var the_request		= "ajax=1&ajax_process=getRateThisGame";
	the_request			+= "&product_id=" + product_id;
	
	//the response will be displayed in this div
	var the_div = "week-content";
	
	// execute the ajax request
	executeAjaxRequest(the_request, the_div, 'checkRedirectOnRating', '-1');
}

function ajaxSendRating(product_id, user_id, redirect)
{
	var the_request 	= "ajax=1&ajax_process=sendRating";
	var error_request = '';
	
	//the request
	//is RATING set ?
	if(document.getElementById('note').value != 'default')
	{
		var rate 			= document.getElementById('note').value;
		the_request			+= '&note=' + rate;
	}
	else
	{
		error_request	+= "&ratingError=&error_code[]=enter_note";	
	}
	//is COMEMNT set ?
	if(document.getElementById('comment').value != '')
	{
		var comment 		= document.getElementById('comment').value;
		the_request			+= '&comment=' + comment;
	}
	else
	{
		error_request	+= "&ratingError=&error_code[]=enter_comment";	
	}
	//is NICKNAME set ?
	if(document.getElementById('nickname').value != '')
	{
		the_request			+= '&nickname=' + document.getElementById('nickname').value;
	}
	else
	{
		error_request	+= "&ratingError=&error_code[]=nickname_missing";	
	}
	
	the_request 		+=	"&product_id=" + product_id;
	the_request 		+= "&user_id=" + user_id;
	the_request			+= "&redirect=" + redirect;

	//the response will be displayed in this div
	var the_div = "week-content";
	
	// if there's at least one error, add the array of error to the request
	if(error_request != '')
	{
		the_request += "&error=1"+error_request;
		// execute the ajax request
		executeAjaxRequest(the_request, the_div, '', '');
	}
	else
	{
		// execute the ajax request
		executeAjaxRequest(the_request, the_div, '', '-1');
	}
	
	
}

function ajaxTicketShowSelectedProduct(product_id)
{
	if(product_id > 0)
	{
		var the_request 	= "ajax=1&ajax_process=ticketShowSelectedProduct";
		var the_div = 'ticket-products';
		
		displayWait(the_div);
		
		the_request += '&product_id=' + product_id;
		
		// execute the ajax request
		executeAjaxRequest(the_request, the_div, 'stopDisplayWait', '-1');
	}
	else
	{
		document.getElementById('ticket-products').style.display = 'none';
	}
}


/*----------------------
-- UMPC DEMO DOWNLOAD --
----------------------*/

function ajaxUMPCDemoDownload(game_id)
{
	// preparation of the request
	var the_request = "ajax=1&ajax_process=umpcDemoDownload";

	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, 'launchUMPCDemoDownload', '-1');
}
