/***************************Functions FO******************************/

/*
*	Permet d'envoyer des donnés à un script sans retour
*	dataPost : data à envoyer sous la forme de : (String) var=value&var2=value
*
*/

function sendDataUnsync(pageRequest, actionSelected, dataPost)
{
	loadAjaxUnsync(pageRequest, actionSelected, dataPost+'&'+formSerialize('formFooterWork'), function(){});
}


function sendDataSync(pageRequest, actionSelected, dataPost)
{
	overlayAjaxLoading(true,'work');
	loadAjaxSync(pageRequest, actionSelected, dataPost+'&'+formSerialize('formFooterWork'));
	overlayAjaxLoading(false,'work');
}

function putHtmlSync(pageRequest, actionSelected, dataPost, containerCible)
{
	overlayAjaxLoading(true,containerCible);
	httpResponse=loadAjaxSync(pageRequest, actionSelected, dataPost+'&'+formSerialize('formFooterWork'));
	if(sessionErrPopup(httpResponse))
	{
		setInnerHtml(containerCible,httpResponse);
		updateHeadAndFoot(containerCible);
	}
	overlayAjaxLoading(false,containerCible);
	return true;
}

function setInnerHtml(divId,htmlValue)
{
	$('#'+divId).html(htmlValue);
	//document.getElementById(divId).innerHTML=htmlValue;
	return false;
}

function putHtmlUnsync(pageRequest, actionSelected, dataPost, containerCible)
{
	overlayAjaxLoading(true,containerCible);
	loadAjaxUnsync(pageRequest, actionSelected, dataPost+'&'+formSerialize('formFooterWork'), 
					function(httpResponse) {
						if(sessionErrPopup(httpResponse))
						{
							setInnerHtml(containerCible,httpResponse);
							updateHeadAndFoot(containerCible);
						}
						overlayAjaxLoading(false,containerCible);
					}
				);
}

function updateHeadAndFoot(containerCible)
{
	if(C_TEMPLATE_TYPE=='mob') 
	{
		updateWorkHeadAndFoot();
	}
}

function getCodeSync(pageRequest, actionSelected, dataPost)
{
	httpResponse=loadAjaxSync(pageRequest, actionSelected, dataPost);
	return trim(httpResponse);
}

function getCodeUnsync(pageRequest, actionSelected, dataPost, callBackFunction)
{
	loadAjaxUnsync(pageRequest, actionSelected, dataPost, callBackFunction);
}

/*********************************************************/





//pageRequest : Page, script appelé
//actionSelected : Action à mener coté serveur
//containerCible : Div dans lequel sera chargé le html avec un innerHTML

/****************** Tools ajax *********************/
function getHTTPObject()
{
	if(window.XMLHttpRequest)
		return new XMLHttpRequest();
	else if(window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
	else {
		return;
	}
}

function getBoId()
{
	if(document.getElementById('bousrid'))
		return document.getElementById('bousrid').value;
	else 
		return 0;
}

/*
*	Permet de serialiser un formulaire pour la passer en argument dataPost
*	formId : Id du form à envoyer
*
*/
function formSerialize(formId)
{	
	var dataPost;
	if(document.getElementById(formId)){
		 dataPost = $('#'+formId).formSerialize(); //Jquery	
	}
	return dataPost;
}

function httpRequestError(httpCodeError, pageRequest, actionSelected, dataPost, callBackFunction)
{
	if(true==confirm('HTTP ERROR : '+httpCodeError+'\n\rRelancer la Requête ?'))
	{
		if(callBackFunction!='')			
			loadAjaxUnsync(pageRequest, actionSelected, dataPost, callBackFunction);
		else
			return loadAjaxSync(pageRequest, actionSelected, dataPost);
	}else{
		return 0;
	}
	//pageRequest, actionSelected, dataPost, callBackFunction
}

var requestCounter=0;

function overlayAjaxLoading(display, containerCible, request)
{
	/*
	if(display==true)
	{
		setCssVisibility('popup_wait','visible');
	}else{
		setCssVisibility('popup_wait','hidden');
	}
	*/
	if(display==true)
	{
		requestCounter++;
		showPopupWait();
	}else{

		requestCounter--;
		if(requestCounter==0)hidePopupWait();
	}
	/*
	 * afficher bloc par block
	if(containerCible)
	{
	
	}else{
	
	}
	*/
}

function sessionErrPopup(response)
{
	if(response=="sessionExpire" || response=="notLoggedIn")
	{
		showPopup('popup_session_expire');
		return false;
	}else if(response=="noRights"){
		// nothing to do
		return false;
	}else if(response=="badAuthFormat"){
		// nothing to do
		return false;
	}else{
		return true;
	}
}

/*********************************************************/


/****************** Ajax base function*********************/
function getHTTPObject()
{
	if(window.XMLHttpRequest)
		return new XMLHttpRequest();
	else if(window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
	else {
		return;
	}
}
 
// ajouter gestion du retour error|||errorCode affichage d'un popup

function loadAjaxSync(pageRequest, actionSelected, dataPost)
{
	var dDate=new Date();

	var postParameter = dataPost;

	if(postParameter && postParameter.length>0)
		postParameter += "&";
	
	postParameter += "pageRequest="+pageRequest+"&actionSelected="+actionSelected+"&bousrid="+getBoId();
	
	getParameter = "?page="+pageRequest+"&act="+actionSelected+"&dt="+dDate.getTime();
	
	var xhr_object = null;
	xhr_object = getHTTPObject();

	xhr_object.open("POST",  "index_ajax.php"+getParameter, false);
	
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.send(postParameter);
	
	if(xhr_object.status  == 200) 
	{
		return xhr_object.responseText;
	}else{
		return httpRequestError(xhr_object.status, pageRequest, actionSelected, dataPost, '');
	}
}
/*
readyState status
0: non initialisé.
1: connexion établie.
2: requête reçue.
3: réponse en cours.
4: terminé. 
*/
function loadAjaxUnsync(pageRequest, actionSelected, dataPost, callBackFunction)
{
	var dDate=new Date();

	var postParameter = dataPost;
	
	if(postParameter && postParameter.length>0)
		postParameter += "&";
	
	postParameter += "pageRequest="+pageRequest+"&actionSelected="+actionSelected+"&bousrid="+getBoId();
	
	getParameter = "?page="+pageRequest+"&act="+actionSelected+"&dt="+dDate.getTime();
	  
  //alert(parameter)
	var xhr_object = null;
	xhr_object = getHTTPObject();
	
	xhr_object.open("POST", "index_ajax.php"+getParameter, true);
	xhr_object.onreadystatechange = function() {
		if(xhr_object.readyState == 4) {
			if(xhr_object.status  == 200) 
			{
				var response = xhr_object.responseText;
				callBackFunction(trim(response));
			}else{
				httpRequestError(xhr_object.status, pageRequest, actionSelected, dataPost, callBackFunction);
			}
		}
	}
	
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.send(postParameter);
}
/*********************************************************/


