// Class
function kAjaxUtility()
{
	// Id of the timeout
	var oIdTimeOut;
}

// Static function of kAjaxUtility class
// Show an ajax loading panel and hide an other control
kAjaxUtility.showLoading = function(vsIdLoading, vsIdControlToHide)
{
	var oLoading = document.getElementById(vsIdLoading);
	var oControlToHide = document.getElementById(vsIdControlToHide);
	var oDateNow = new Date();
	
	if (oControlToHide)
	{
		oLoading.style.height = oControlToHide.offsetHeight;
		oLoading.style.width = oControlToHide.offsetWidth;
		oControlToHide.style.display = "none";	
	}
	oLoading.style.display = "block";
	
	oLoading.dateSet = oDateNow.getTime();
}

// Static function of kAjaxUtility class
// Hide an ajax loading panel and show an other control
kAjaxUtility.hideLoading = function(vsIdLoading, vsIdControlToShow)
{
	var oLoading = document.getElementById(vsIdLoading);
	var oControlToShow = document.getElementById(vsIdControlToShow);
	var oDateNow  = new Date();
	
	if (this.oIdTimeOut)
	{
		window.clearTimeout(this.oIdTimeOut);
	}
	
	while(oDateNow - oLoading.dateSet < oLoading.getAttribute("TimerMinDisplayTime"))
	{
		oDateNow = new Date();
	}
	oLoading.style.display = "none";
	if (oControlToShow)
	{
		oControlToShow.style.display = "block";
	}
}

// Static function of kAjaxUtility class
// Set a timeout to show a loading when it expired
kAjaxUtility.showLoadingWithTimer = function (vsIdLoading, vsIdControlToHide , timeBeforeShow)
{
	var sCodeToExecute = "kAjaxUtility.showLoading('" + vsIdLoading + "','" + vsIdControlToHide + "')";
	this.oIdTimeOut = window.setTimeout(sCodeToExecute ,timeBeforeShow);
}

// Static function of kAjaxUtility class
// Clear the timeout setted with kAjaxUtility.setTimer and hide the loading
kAjaxUtility.hideLoadingWithTimer = function(vsIdLoading, vsIdControlToShow, minTimeToShow)
{
	var oLoading = document.getElementById(vsIdLoading);
	var oDateNow  = new Date();
	var sCodeToExecute = "kAjaxUtility.hideLoading('" + vsIdLoading + "','" + vsIdControlToShow + "')";
	var iTimeBeforeHide;
	
	if (this.oIdTimeOut)
	{
		window.clearTimeout(this.oIdTimeOut);
	}
		
	iTimeBeforeHide = minTimeToShow - (oDateNow - oLoading.dateSet);
	
	if (iTimeBeforeHide < 0)
	{
		iTimeBeforeHide = 0;
	}
	
	this.oIdTimeOut = window.setTimeout(sCodeToExecute, iTimeBeforeHide);
}

// Static function of kAjaxUtility class
// Revalidate the page to restor validator status
kAjaxUtility.validatePage = function()
{
	for (i=0; i < Page_Validators.length; i++)
	{
		ValidatorValidate(Page_Validators[i]);
	}
	MyValidation();
}

kAjaxUtility.resetAllValidationTextBoxes = function()
{
	var oInputs = document.getElementsByTagName("INPUT");
	
	for (var i = 0; i < oInputs.length; i++)
	{
		if (oInputs[i].type == "text" && oInputs[i].getAttribute("isKce"))
		{
			kAjaxUtility.resetValidationTextBox(oInputs[i].id);
		}
	}
}

// Static function of kAjaxUtility class
kAjaxUtility.resetValidationTextBoxes = function(vsIdControls)
{
	var oArrayId = vsIdControls.split("|");
	
	for (var i = 0; i < oArrayId.length; i++)
	{
		this.resetValidationTextBox(oArrayId[i]);
	}
}

// Static function of kAjaxUtility class
kAjaxUtility.resetValidationTextBox = function(vsIdControl)
{
	var oTextBox = document.getElementById(vsIdControl);
	
	if (oTextBox && self.hideMessage)
	{
		hideMessage(oTextBox);
	}
}

kAjaxUtility.scrollTop = function()
{
	window.document.body.scrollTop = 0;
	window.document.documentElement.scrollTop = 0;
}

kAjaxUtility.ajaxRequestSentRepeater = function(vsIdRepeater, vsEventTarget, vsIdLoadingPanel, vsIdControlToHide)
{
	var oLoadingPanel = document.getElementById(vsIdLoadingPanel);
	var sDelay;
		
	if (vsEventTarget.indexOf(vsIdRepeater) != -1)
	{ // The event come from inside the repeater
		sDelay = oLoadingPanel.getAttribute("InitialDelayTimeInside");
	}
	else
	{ // The event come from outside the repeater
		sDelay = oLoadingPanel.getAttribute("InitialDelayTimeOutSide");
	}
	
	this.showLoadingWithTimer(vsIdLoadingPanel, vsIdControlToHide, sDelay);
}

kAjaxUtility.ajaxResponseEndRepeater = function(vsIdRepeater, vsEventTarget, vsIdLoadingPanel, vsIdControlToHide)
{
	var oLoadingPanel = document.getElementById(vsIdLoadingPanel);
	var sDelay;
	
	if (vsEventTarget.indexOf(vsIdRepeater) != -1)
	{ // The event come from inside the repeater
		sDelay = oLoadingPanel.getAttribute("MinDisplayTimeInside");
	}
	else
	{ // The event come from outside the repeater
		sDelay = oLoadingPanel.getAttribute("MinDisplayTimeOutside");
	}
	
	this.hideLoadingWithTimer(vsIdLoadingPanel, vsIdControlToHide, sDelay);
}