// CREATION OBJET HTTP REQUEST
function getXhr(){
	if (window.XMLHttpRequest) xhr=new XMLHttpRequest(); // Firefox et autres
	else if (window.ActiveXObject) try { // Internet Explorer
		xhr=new ActiveXObject('Msxml2.XMLHTTP');
	} catch (e) {
		xhr=new ActiveXObject('Microsoft.XMLHTTP');
	}else{ // XMLHttpRequest non supporté par le navigateur
		alert('Votre navigateur ne supporte pas les objets XMLHTTPRequest...');
		xhr=false;
	}
}//getXhr


// Content Hightlight
function getHightlightsScenario( scenarioID, lang ){
	
	// Div container
	var div = document.getElementById( 'hightlights_home' );
	var jdiv = $( '#hightlights_home' );
	
	// Reset content
	jdiv.slideToggle( 'slow' );
	setTimeout( "clearElement( 'hightlights_home' );", 400);
	
	// Appel de la fonction créant l'objet Xhr
	getXhr();

	// Traitement
	xhr.onreadystatechange=function(){

		// On a une réponse
		if (xhr.readyState == 4 && xhr.status == 200){
			
			// Initialisation
			window.status = '';
			
			// On recup la réponse
			var rep = xhr.responseText;
			
			// Réinitialise input
			div.innerHTML = rep;
			jdiv.slideToggle( 'slow' );
			
		}
		
	}
	
	// Initialisation des paramètres AJAX
	xhr.open('POST','/'+lang+'/layout/set/ajax/mach/getHightlightsScenario',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	window.status='Chargement en cours ...';
	xhr.send("scenarioID=" + scenarioID);
		
	
}

function clearElement( elementID ){
	var div = document.getElementById( elementID );
	div.innerHTML = '';
}

// Play game
function play( askID, pageID, lang ){

	// Get ask element
	var ask_link = $$( 'ask_link_' + askID );
	var ask_li = $$( 'ask_li_' + askID );
	var page_img = $$( 'page_' + pageID );
	var error_game = $$( 'error_game' );
	
	// Appel de la fonction créant l'objet Xhr
	getXhr();
	
	// Traitement
	xhr.onreadystatechange=function(){
		
		// On a une réponse
		if (xhr.readyState == 4 && xhr.status == 200){
			
			// Initialisation
			window.status = '';
			
			// On recup la réponse
			var status = xhr.responseText;
			
			// Traitement de la réponse
			switch( status ){
				case 'continue':
					ask_link.href = "javascript:void(0);";
					ask_li.className = '';
					page_img.style.display = 'none';
				break;
				case 'canceled':
					alert( error_game.value );
				break;
				case 'halted':
					ask_link.href = "javascript:void(0);";
					ask_li.className = '';
					page_img.style.display = 'none';
					
					document.location.href = "/" + lang + "/game/end";
					
				break;
			}
			
		}
		
	}
	
	// Initialisation des paramètres AJAX
	xhr.open('POST','/'+lang+'/layout/set/ajax/game/play',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	window.status='Chargement en cours ...';
	xhr.send("askID=" + askID + "&pageID=" + pageID);
	
}

function finish(){
	
	// Appel de la fonction créant l'objet Xhr
	getXhr();
	
	// Traitement
	xhr.onreadystatechange=function(){
		
		// On a une réponse
		if (xhr.readyState == 4 && xhr.status == 200){
			
			// Initialisation
			window.status = '';
			
			// On recup la réponse
			var status = xhr.responseText;
			
			return status;
			
		}
		
	}
	
	// Initialisation des paramètres AJAX
	
	xhr.open('POST','/'+lang+'/layout/set/ajax/game/finish',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	window.status='Chargement en cours ...';
	xhr.send();
	
}

function getYear( year, lang ) {
	
	if ( year )
	{
		// Appel de la fonction créant l'objet Xhr
		getXhr();
		
		// Traitement
		xhr.onreadystatechange=function(){
			
			// On a une réponse
			if (xhr.readyState == 4 && xhr.status == 200){
				
				// Initialisation
				window.status = '';
				
				$('#cal-container').html( xhr.responseText );
			}
		}
		// Initialisation des paramètres AJAX
		xhr.open('POST','/'+lang+'/layout/set/ajax/mach/tab/calendar',true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		window.status='Chargement en cours ...';
		xhr.send( "year=" + year );
	}
}

var courseID = new Array();

function changeSeminar() {
	
	var seminarID = $("select[name='seminarID']").val();
	
	$( "#trainingEvents" ).hide( 'slow', function() 
	{
		getXhr();
		
		xhr.onreadystatechange = function()
		{
			window.status = '';
			if (xhr.readyState == 4 && xhr.status == 200)
			{
				$( "#trainingEvents" ).html( xhr.responseText );
				
			}
		}
		xhr.open('POST','/en/layout/set/ajax/mach/getTrainingSeminar',true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		window.status='Chargement en cours ...';
		
		var tmp = "seminarID=" + seminarID;
		var str = '';
		
		// TODO
		/*for(i=0;i<=courseID.length;i++)
		{
			str += "&courseID[]=" + courseID[i];
		}*/
		
		xhr.send(tmp + str);
		
		$( "#trainingEvents" ).show( 'slow' );
		
	});
}