﻿
//AJAX global object
//var xmlHttp = GetXmlHttpObject();

function GetXmlHttpObject()
{
	var objXmlHttp = null;

	try
	{
		//For Firefox, Opera, Safari
		objXmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			//For Internet Explorer 6.0+
			objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			alert("Your browser does not support AJAX!");
			objXmlHttp = null;
		}
	}
	
	return objXmlHttp;
}



//*************************************************************************************************
//* SessionPing
//*
//* Keeps the session alive by a simple AJAX call sent repeatedly.
//*************************************************************************************************
function sessionPing()
{
	//Activate SessionPing in every 5 minutes
	var command = "sessionPingExec()";
	setTimeout(command,  5 * 60 * 1000);
}

function sessionPingExec()
{
	var xmlHttp = GetXmlHttpObject();

	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			//SessionPing sent, response received, re-initialize the timed sender!
			sessionPing();
		}
	}

	var url = "/Ajax.aspx?func=SessionPing";

	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}



//*************************************************************************************************
//* TextBoxSuggest
//*
//* Text input control with AJAX suggestions while typing.
//*************************************************************************************************
function suggestDisplay(baseControl, func, config, event)
{
	var objDiv = document.getElementById(baseControl +"divSuggest");
	var objText = document.getElementById(baseControl +"txtSuggest");
	var objList = document.getElementById(baseControl +"ddlSuggest");
	
	if(objText.value != "")
	{
		//Has the text changed?
		if(objText.valuePrev == null || objText.valuePrev != objText.value)
		{
			objText.valuePrev = objText.value;
			
			var command = "suggestListFill(\""+ baseControl +"\",\""+ func +"\",\""+ config +"\",\""+ objText.value +"\")";
			setTimeout(command, 250);
		}
	}
	else
	{
		objText.valuePrev = "";
		
		objDiv.style.display = "none";
		objText.focus();
	}
	
	suggestText(baseControl);
}

function suggestListFill(baseControl, func, config, textValue)
{
	var objDiv = document.getElementById(baseControl +"divSuggest");
	var objText = document.getElementById(baseControl +"txtSuggest");
	var objList = document.getElementById(baseControl +"ddlSuggest");
	
	if(objText.value == textValue)
	{
		var xmlHttp = GetXmlHttpObject();

		xmlHttp.onreadystatechange = function()
		{
			if(xmlHttp.readyState == 4)
			{
				if(xmlHttp.responseText.length > 0)
				{
					var items = xmlHttp.responseText.split("\\");
					
					objList.options.length = 0;

					for(var i=0; i!=items.length; i++)
					{
						objList.options[i] = new Option(items[i]);
					}

					if(items.length == 1 && (items[0] == objText.value || objText.value.length > items[0].length))
					{
						objDiv.style.display = "none";
						objText.focus();
					}
					else
					{
						objDiv.style.display = "";
					}
				}
				else
				{
					objDiv.style.display = "none";
					objText.focus();
				}
			}
		}
		
		var url = "/Ajax.aspx?func="+ func;
		url += "&param="+ objText.value;
		url += "&config="+ config;

		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}

function suggestList(baseControl, e, mode)
{
	var objDiv = document.getElementById(baseControl +"divSuggest");
	var objText = document.getElementById(baseControl +"txtSuggest");
	var objList = document.getElementById(baseControl +"ddlSuggest");

	//Get the last pressed key's keycode
	var key = e.keyCode;
	
	//Is the suggestion list visible?
	if(objDiv.style.display == "")
	{
		if(mode == 0)
		{
			//Jump to the suggestion list on pressing the down arrow in the input field
			if(key == 40)
			{
				objList.focused = true;
				objList.focus();
				return;
			}
		}
		else if(mode == 1)
		{
			//Take selected item from suggestion list on pressing the enter key
			if(key == 13 || key == 9 || key == 32)
			{
				if(objList.selectedIndex >= 0)
				{
					objText.value = objList.options[objList.selectedIndex].text;
					objDiv.style.display = "none";
					objList.focused = false;
					objText.focus();
					
					suggestText(baseControl);
					return;
				}
			}
			else if(objList.selectedIndex == 0 && key == 38)
			{
				//Jump to the text input on pressing the up arrow on top of the suggestion list

				objDiv.style.display = "none";
				objText.focus();
				return;
			}
		}
		else if(mode == 2)
		{
			//Take selected item from suggestion list on clicking on it
			if(objList.selectedIndex >= 0)
			{
				objText.value = objList.options[objList.selectedIndex].text;
				objDiv.style.display = "none";
				objList.focused = false;
				objText.focus();
				
				suggestText(baseControl);
				return;
			}
		}
		else if(mode == 3)
		{
			//Hide the list on losing the focus
			objDiv.style.display = "none";
			objList.focused = false;
			return;
		}
		else if(mode == 4)
		{
			//Hide the list on losing the focus on the input field, delayed!
			var command = "suggestListHide(\"" + baseControl + "\")";
			setTimeout(command, 250);
		}
	}
}

function suggestListHide(baseControl)
{
	var objDiv = document.getElementById(baseControl +"divSuggest");
	var objText = document.getElementById(baseControl +"txtSuggest");
	var objList = document.getElementById(baseControl +"ddlSuggest");

	//Check custom property to see if the suggestion list is the focused element on the form
	if(objList.focused == null || objList.focused == false)
	{
		//Not focused, hide it!
		objDiv.style.display = "none";
	}
}

function suggestText(baseControl)
{
	var objText = document.getElementById(baseControl +"txtSuggest");
	var objHidden = document.getElementById(baseControl +"hidText");
	
	objHidden.value = objText.value;
}



//*************************************************************************************************
//* PageSelector
//*
//* Control to switch pages of long lists.
//*************************************************************************************************
function pageSelectorJump(idForm, idHidden, value)
{
	var objForm = document.getElementById(idForm);
	var objHidden = document.getElementById(idHidden);
	
	//Set the selected page number into the form's dedicated hidden value
	objHidden.value = value;
	
	//Submit the form
	objForm.submit();
}
