
var gobjFormInfo = null;

//
// Recherche tous les formulaires qui
$(document).ready( function () {

	var objForm = $('form[@processusname]').get(0);
	
	if(typeof(objForm) != 'undefined')
	{
		gobjFormInfo = new clsFormInfo(objForm);
		// ajoute les paramètres
		gobjFormInfo.getParameters();

		// attache le submit au formulaire
		$(objForm).bind('submit', qsOnSubmitFormular);
		
		// lance le formulaire
		qsPostFormular(gobjFormInfo);
	}

});

// class d'information du status du processus
function clsFormInfo(pobjForm)
{
	this.strProcessusName = '';
	this.strXmlParameters = '';
	this.lngNextStep = 1;
	this.objForm = pobjForm;
	this.strProcessusName = $(pobjForm).attr('processusname');
	if(gobjSessionVar)
		this.lngNodeFK = gobjSessionVar.lngCurrentNodeFK;
	else
		this.lngNodeFK = -1;
	
	
	this.getParameters = function() {
	
		// récupère les paramètres		
		var objParameters = $(this.objForm).find('.parameters').get(0);
		if(typeof(objParameters)  != 'undefined')
		{
			$(objParameters).hide();
			this.strXmlParameters = $(objParameters).html();
		}
		else
			this.strXmlParameters = '';
	
	}
}

//  action de submit par le formulaire
qsOnSubmitFormular = function(objEvent){
	qsPostFormular(gobjFormInfo);
	//objEvent.stopPropagation();
	return false;
}

// fonction de traitement de l'appel à la page post
qsPostFormular = function(pobjFormInfo){

	if(pobjFormInfo == null)
		return false;
	
	if(pobjFormInfo.lngNextStep == -1)
		return false;
	
	// prépare les paramètres d'envoi
	var arrParams = Array(
		{	name:'lngNextStep',				value:pobjFormInfo.lngNextStep }
		,{	name:'strProcessusName',		value:pobjFormInfo.strProcessusName }
		,{	name:'strXmlParameters',		value:pobjFormInfo.strXmlParameters }
		,{	name:'N',						value:pobjFormInfo.lngNodeFK }
	);
	
	$.ajax({
		type:		'GET',
		cache:		false,
		dataType:	'json',
		url:		'/qsPortal/ProcessWorkflow/Post.asp',
		data:		$.fn.paramForm(arrParams) + "&" + $(pobjFormInfo.objForm).formSerialize(),
		beforeSend:	function(){
						$(pobjFormInfo.objForm).html("<img src='/qsPortal/Images/AjaxProcess.gif' alt='Please wait...'/>");
					},
		success:	function(req){
						$(pobjFormInfo.objForm).html(req.strHtmlContent);
						pobjFormInfo.lngNextStep = req.lngNextStep;
						// ajoute les paramètres
						pobjFormInfo.getParameters();
					},
		error:		function(req, err){
						//alert('Ajax error!');
						alert('Ajax error! ' + err);
						//$(pobjFormInfo.objForm).html('<textarea cols=50 rows=10 >' + req.responseText + '</textarea>');
						$(pobjFormInfo.objForm).html(req.responseText);
					}
	});

	return true;
	
}
