﻿// JavaScript File PopupList.js

function clsqsPopupList(pstrPopupListName, pstrAJAXUrl, plngFocusCol, pstrListType)
{
	this.strPopupListName = pstrPopupListName;
	this.strName = "obj" + this.strPopupListName;
	this.lngTimeoutId = -1;
	this.strUrl = pstrAJAXUrl;
	this.lngFocusCol = plngFocusCol;
	this.lngCurrentPage = 1;
	this.lngSortColomnNo = -1;
	this.blnSortOrder = false;
	this.strSearchCriterion = "";
	this.strListType = pstrListType;
	this.SetFocus();
	this.strHtmlImgWait = "<img src='/qsPortal/Images/AjaxProcess.gif' alt='Please wait...'/>";
	//this.strJSFunction = function();
}

// obtient le texte de filtre en cours de frappe
clsqsPopupList.prototype.GetSearchCriterion = function()
{
	var strSearchCriterion = "";
	// var ObjSearchElements = document.getElementsByTagName("input");
	$("input[name=" + this.strPopupListName + "_ColFilter]").each(
		function() {
			if(strSearchCriterion != "")
				strSearchCriterion += "||";
			if($(this).attr("value"))
				strSearchCriterion += $(this).attr("col") + "::" + $(this).attr("value");
			else
				strSearchCriterion += $(this).attr("col") + "::";
		}
	);
	
	return strSearchCriterion;
}

clsqsPopupList.prototype.GetParameters = function(pstrPopupListAction)
{
	return Array(
		//{	name:'url',					value:this.strUrl },
		{	name:'PopupListAction',		value:pstrPopupListAction },
		{	name:'ListType',			value:this.strListType },
		{	name:'PopupListName',		value:this.strPopupListName },
		{	name:'PageNo',				value:this.lngCurrentPage },
		{	name:'ColomnNo',			value:this.lngSortColomnNo },
		{	name:'SortOrder',			value:this.blnSortOrder },
		{	name:'Filter',				value:this.strSearchCriterion },
		{	name:'PostBack',			value:true }
	);
	
}

clsqsPopupList.prototype.SetFocus = function()
{
	strColFilterId = this.strPopupListName + '_ColFilter' + this.lngFocusCol;

	if(this.lngFocusCol != -1)
	{
		objElement = $("#" + strColFilterId).get(0);
		try
		{
			objElement.focus();
			var pos = (objElement.value.length);
			var range = objElement.createTextRange();
			range.collapse(true);
			range.moveStart('character', pos);
			range.moveEnd('character', pos);
			range.select();
		}catch(e){}		
	}
}
	
clsqsPopupList.prototype.ShowPage = function (plngPageNumber)
{
	var strCurrentDiv = 'divContentList_' + this.strPopupListName;
	var objCurrentObj = this;
	
	this.lngCurrentPage = plngPageNumber
	
	var arrParams = this.GetParameters('Page');
	
	$.ajax({
			type:			'GET',
			cache:			false,
			dataType:		'html',
			url:			this.strUrl,
			data:			$.fn.paramForm(arrParams),
			beforeSend:		function()
							{
								document.getElementById(strCurrentDiv).innerHTML = objCurrentObj.strHtmlImgWait; 	
							},
			success:		function(req)
							{
								document.getElementById(strCurrentDiv).innerHTML = req; 
								objCurrentObj.SetFocus();
							},
			error:			function(req, err)
							{
								alert('Error!');
								document.getElementById(strCurrentDiv).innerHTML = req.responseText; 
							}
	});
	
}

clsqsPopupList.prototype.Sort = function (plngColomnNumber)
{
	var strCurrentDiv = 'divContentList_' + this.strPopupListName;
	var objCurrentObj = this;
	
	this.lngSortColomnNo = plngColomnNumber;
	if(this.blnSortOrder)
		this.blnSortOrder = false;
	else
		this.blnSortOrder = true;
	
	var arrParams = this.GetParameters('Sort');
	
	$.ajax({
			type:			'GET',
			cache:			false,
			dataType:		'html',
			url:			this.strUrl,
			data:			$.fn.paramForm(arrParams),
			beforeSend:		function()
							{
								document.getElementById(strCurrentDiv).innerHTML = objCurrentObj.strHtmlImgWait; 	
							},
			success:		function(req)
							{
								document.getElementById(strCurrentDiv).innerHTML = req; 
								objCurrentObj.SetFocus();
							},
			error:			function(req, err)
							{
								alert('Error!');
								document.getElementById(strCurrentDiv).innerHTML = req.responseText; 
							}
	});	

}

// applique un délai avant de lancer la recherche par filtre
clsqsPopupList.prototype.DelayFilter = function (plngCol)
{
	this.lngFocusCol = plngCol;
		
	clearTimeout(this.lngTimeoutId);
	this.lngTimeoutId = setTimeout(this.strName + ".Filter()", 1500); 
}

// filtre l'une des colonnes
clsqsPopupList.prototype.Filter = function ()
{
	var strCurrentDiv = 'divContentList_' + this.strPopupListName;
	var objCurrentObj = this;

	this.strSearchCriterion = this.GetSearchCriterion();
	
	// DTA v427 19.5.2008 
	// place le curseur de la page courant à 1 dès qu'une opération de filtre
	// est effectuée
	this.lngCurrentPage = 1;
	var arrParams = this.GetParameters('Filter');
	
	$.ajax({
			type:			'GET',
			cache:			false,
			dataType:		'html',
			url:			this.strUrl,
			data:			$.fn.paramForm(arrParams),
			beforeSend:		function()
							{
								document.getElementById(strCurrentDiv).innerHTML = objCurrentObj.strHtmlImgWait; 	
							},
			success:		function(req)
							{
								document.getElementById(strCurrentDiv).innerHTML = req; 
								objCurrentObj.SetFocus();
							},
			error:			function(req, err)
							{
								alert('Error!');
								document.getElementById(strCurrentDiv).innerHTML = req.responseText; 
							}
	});	

}
