//------------------------------------------------------------------------------------------------------------------------------
//
// asyncAffiche ( Class )
// -----------------------
//	AJAX wrapper
//
// By	: Sébastien Pilon (spilon@groupehytech.com)
// FOR	: Le groupe Hytech Inc
// DATE : Febuary 14 2007
// Copyright Le Groupe Hytech Inc.
//
//------------------------------------------------------------------------------------------------------------------------------
//	That class offers simple methods to process various
//	server side asyncronous execution. 
//--------------------------------------------------------------
//
// Simple HOWTO
// Getting started:
//	1. 	make an instance of the class asyncAffiche
//		By default, the constructor initialize various variables, 
//		your can override some of them for an example if you are 
//		gonna use liste box filling functionality.
//	2.	Override default or additionnal vars
//		ex: oInstance.setLoadingMsg("loading in progress ...); 
//		instead of the default "Chargement en cours ..." message
//	3.	call either sendToServer or fillListe to fill a listebox
//
//--------------------------------------------------------------
//
// 	*********************
// 	*	Public methods	*
//	*********************
//
//	setLoadingMsg( msg )		
//		Set the loading message for list filling option
//
//	setListe( liste )
//		Set the liste to work with. * list filling option *
//
//	getListe()
//		Returns the current list set in the object 
//
//	setAsyncData( datas )
//		Set the datas to send ( server side )
//		standard = name=data&name2=data2 etc
//
//	getAsyncData()
//		Returns the current datas set in the object
//
//	setAsyncMethod( method )
//		Set the async mode true or false ** string vals are dont case sensitive, ex:	"tRue" will work
//																						"TRUE" will work
//
//	getAsyncMethod()
//		Returns if it uses async or not 
//
//	setAsyncPath( path )
//		Set the server path source
//
//	getAsyncPath()
//		Returns the current server path
//
//	setAsyncSendMethod()
//		Set the sending method ( POST or GET )
//
//	getAsyncSendMethod()
//		Returns sending method 
//
//	sendToServer()
//		* 	Must be called after all overrides !
//			Otherwise, your overrides will not be taken
//		Send datas to the server
//
//	fillListe()
//		Like sendServer, exept you can put a dynamic message in a liste
//------------------------------------------------------------------------------------------------------------------------------

function asyncAffiche()
{
	// données membres de la classe	
	//private:
	var liste				= "";
	var loading_msg			= "Chargement en cours ...";
	var form				= "";
	var check				= "";
	var async_send_method	= "POST";
	var async_path			= "";
	var async_method		= true; 
	var async_data			= ""; // sent datas
	var err_code			= "";
	var loading_icon		= false;
	var toDo				= "";
	var xhr_object; // a la construction on definit le navigateur ...
	
	//public:
	//this.setCheck				= setCheck;
	this.setToDo				= setToDo;
	this.setLoadingMsg			= setLoadingMsg;
	this.setListe				= setListe;
	this.getListe				= getListe;
	//this.setForm				= setForm;
	this.setAsyncData			= setAsyncData;
	this.getAsyncData			= getAsyncData;
	this.setAsyncMethod			= setAsyncMethod;
	this.getAsyncMethod			= getAsyncMethod;
	this.getAsyncPath			= getAsyncPath;
	this.setAsyncPath			= setAsyncPath;
	this.setAsyncSendMethod		= setAsyncSendMethod;
	this.getAsyncSendMethod		= getAsyncSendMethod;
	this.sendToServer			= sendToServer;
	this.fillListe				= fillListe;
	this.setLoadingIconActive	= setLoadingIconActive;
	this.getLoadingIconActive	= getLoadingIconActive;

	//this.XHTTPsend			= XHTTPsend;
	//this.XHTTPopen			= XHTTPopen;
	
	// à la construction ...
	setXHTTPObj();
	
	function setXHTTPObj()
	{
		if(window.XMLHttpRequest) // Firefox
    		xhr_object = new XMLHttpRequest();
	   	else if(window.ActiveXObject) // Internet Explorer
    		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
   		else 
		{ 
   			err_code = "Async not avaible for your browser !";
   		}
	}
	
	//open connection (server side)
	function XHTTPopen()
	{	
		var err = "";
		
		if( getAsyncSendMethod() == "" )
			err += "Send method must be set !\n";
		if( getAsyncPath() == "" )
			err += "Server path must be set !\n";
		if( getAsyncMethod() == "" )
			err += "Syncronous mode must be set !";
		if( err == "" )
		{		
			xhr_object.open( getAsyncSendMethod(), getAsyncPath(), getAsyncMethod() ); 
		}
		else
		{
			alert( err );
		}
		
	}
	
	// send datas (server side)
	function XHTTPsend()
	{
		xhr_object.send( getAsyncData() );
	}
	
	function getAfficheType() //
	{		
		return getTypeValue( check );
	}

	function getTypeValue( leCheck )
	{
		var value = "";
		var bOk = true;
		for (var i=0; (i < leCheck.length) && (bOk); i++)
		{
			if ( leCheck[i].checked )
 			{
				value = leCheck[i].value;
				bOk = false;
			}
		}
		return value;
	}
	
	function getAsyncSendMethod()
	{
		return async_send_method;
	}
	
	//	POST or GET
	function setAsyncSendMethod( send_method )
	{
		if( ( send_method.toUpperCase() == "POST" ) || ( send_method.toUpperCase() == "GET" ) )		
			async_send_method = send_method;
		else
			alert("Send method can only be POST or GET !");
	}
	
	function getAsyncPath()
	{
		return async_path;
	}
	
	function setAsyncPath( path )
	{
		async_path = path;
	}
	
	function getAsyncMethod()
	{
		return async_method;
	}
	
	
	function setAsyncMethod( method )
	{
		// validation si string ou bool
		if( ( method == true ) || ( method == false) )
			this.async_method = method;
		else if( method.toUpperCase() == "TRUE" )
			this.async_method = true;
		else if( method.toUpperCase() == "FALSE" )
			this.async_method = false;
		else
			alert("Async mode must be a boolean value !");
	}
	
	function getAsyncData()
	{
		return async_data;
	}

	// doit etre sous format &data=data ...
	function setAsyncData( data )
	{
		async_data = data;
	}

	function getForm()
	{
		return form;
	}
	
	function setForm( fform )
	{
		form = fform;
	}
	
	function getListe()
	{
		return liste;
	}
	
	function setListe( laliste )
	{
		liste = laliste;
	}
	
	function setCheck( lecheck )
	{
		check = lecheck;
	}
	
	function getCheck()
	{
		return check;
	}
	
	function setLoadingMsg( msg )
	{
		if( msg != "" )		
			loading_msg = msg;
		else
			alert("Loading message cannot be empty !");	
	}	
	
	function getLoadingMsg()
	{
		return loading_msg;
	}	

	function DefaultListeMsg()
	{
		var s = getListe();
		s.options.length = 0;
		s.options[s.options.length] = new Option( getLoadingMsg() ,"");
		s.disabled = true;
	}
	
	function fillListe()
	{
		DefaultListeMsg();
		sendToServer();
		
	}
	
	function setHeader()
	{
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	}
	
	function sendToServer()
	{
		XHTTPopen();
		xhr_object.onreadystatechange = callback;
		setHeader();
		XHTTPsend();		
	}
	
	function callback()
	{
		if(xhr_object.readyState == 4)
	  	{
        	eval(xhr_object.responseText);	
			if( getLoadingIconActive() && loadingIconIsPresent() )
			{
				loading( false );
			}
			if(toDo != "")
			{
				try
				{
					eval( toDo );
				}
				catch(e)
				{
					alert("Check toDo instruction syntax ! " + e );
				}
			}
		}
	}
	
	function loadingIconIsPresent()
	{
		return document.getElementById("loading");
	}
	
	function setLoadingIconActive( active )
	{
		loading_icon = active;
	}
	
	function getLoadingIconActive()
	{
		return loading_icon;
	}
	
	// New, Now you can pass instructions to do after a server response !!!
	function setToDo( action )
	{
		toDo = action;
	}	

}