/**
 * Show the spinner
 * @return
 */
function ShowSpinner()
{
	var scrollX = 0;
	var scrollY = 0;

	if (window.innerHeight)
	{
		scrollX = window.pageXOffset;
		scrollY = window.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		scrollX = document.documentElement.scrollLeft;
		scrollY = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;
	}

	var pageSize = getPageSize();
	var AjaxSpinner = document.getElementById('AjaxSpinner');

	AjaxSpinner.style.left = (scrollX + Math.round(pageSize[0] / 2) - 8) + "px";
	AjaxSpinner.style.top  = (scrollY + Math.round(pageSize[3] / 2) - 8) + "px";
	AjaxSpinner.style.display = "block";
}

/**
 * Hide the spinner
 * @return
 */
function HideSpinner()
{
	var AjaxSpinner = document.getElementById('AjaxSpinner');
	AjaxSpinner.style.display = "none";
}

/**
 * getPageSize() from lightbox.
 * Returns array with page width, height and window width, height
 * Core code from - quirksmode.org
 * Edit for Firefox by pHaez
 * @return array An array containing the page's width and height, the window's width and height
 */
function getPageSize()
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY)
	{
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else
	{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight)
	{	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
		pageHeight = windowHeight;
	else
		pageHeight = yScroll;

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
		pageWidth = windowWidth;
	else
		pageWidth = xScroll;

	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
	return arrayPageSize;
}

/**
 * This function is used to show an  auto completer for Unique
 */
function showUniqueAutoCompleter(guid)
{
	var autoCompleter = $('autocomplete_' + guid);
	var pSelectName = $('pSelectName_' + guid);
	if (autoCompleter)
		autoCompleter.style.display = '';
	if (pSelectName)
		pSelectName.style.display = 'none';
}

/**
 * This function is used to hide an auto completer
 */
function hideUniqueAutoCompleter(guid, dontChangeName, buttonNameAfterSelectionAddSlashes)
{
	var autoCompleter = $('autocomplete_' + guid);
	var pSelectName = $('pSelectName_' + guid);
	var spanSelectName = $('spanSelectName_' + guid);
	if (autoCompleter)
		autoCompleter.style.display = 'none';
	if (pSelectName)
		pSelectName.style.display = '';
	if(spanSelectName && !dontChangeName)
		spanSelectName.innerHTML = buttonNameAfterSelectionAddSlashes;
}

/**
 * This function is called by ajax and add a new line
 * in the list of element
 */
function addUniqueElementInListWithResultLine(inputTextValue, liContainer)
{
	//create li prototype object
	var theLi = $(liContainer);

	//check the li element
	if (theLi)
	{
		var guid = $(inputTextValue).id.substr('autocomplete_'.length);
		var callBackJavascriptWithSlashes = $(inputTextValue).getAttribute('callBackJavascriptWithSlashes');
		var buttonNameAfterSelectionAddSlashes = $(inputTextValue).getAttribute('buttonNameAfterSelectionAddSlashes');
		var inputName = $(inputTextValue).name.substr('autocomplete_'.length);

		//retrieve informations
		var content = theLi.innerHTML.stripTags().unescapeHTML();
		addUniqueElementInList(content, theLi.readAttribute('id'), guid, callBackJavascriptWithSlashes, inputName, buttonNameAfterSelectionAddSlashes);
	}
}

/**
 * This function is called by ajax and add a new line
 * in the list of element
 */
function addElementInListWithResultLine(inputTextValue, liContainer)
{
	//create li prototype object
	var theLi = $(liContainer);

	//check the li element
	if (theLi)
	{
		var guid = $(inputTextValue).id.substr('autocomplete_'.length);
		var callBackJavascriptWithSlashes = $(inputTextValue).getAttribute('callBackJavascriptWithSlashes');
		var easyquizzServerURL = $(inputTextValue).getAttribute('easyquizzServerURL');
		var cssLiCloseResultContainer = $(inputTextValue).getAttribute('cssLiCloseResultContainer');
		var cssLiSpanResultContainer = $(inputTextValue).getAttribute('cssLiSpanResultContainer');
		var cssLiResultContainer = $(inputTextValue).getAttribute('cssLiResultContainer');
		var basicErrorMessage = $(inputTextValue).getAttribute('basicErrorMessage');
		var inputName = $(inputTextValue).name.substr('autocomplete_'.length);

		// Check if we have to add a new item
		if (theLi.readAttribute('id').substring(0, 4) == 'new[')
		{
			addElementInList(	theLi.readAttribute('initialValue'), theLi.readAttribute('id'),
												guid, callBackJavascriptWithSlashes, inputName, true,
												easyquizzServerURL, cssLiCloseResultContainer, 
												cssLiSpanResultContainer, cssLiResultContainer, basicErrorMessage);
			return;
		}

		//retrieve informations and call method
		var content = theLi.innerHTML.stripTags().unescapeHTML();
		addElementInList(	content, theLi.readAttribute('id'), guid, callBackJavascriptWithSlashes,
											inputName, true, easyquizzServerURL, cssLiCloseResultContainer,
										  cssLiSpanResultContainer, cssLiResultContainer, basicErrorMessage);
	}
}

/**
 * this function modify the element in the link
 */
function addUniqueElementInList(nameOfElement, idOfElement, guid, callBackFunctionName, inputName, buttonNameAfterSelectionAddSlashes)
{
	//update the span
	var span = $('spanName_' + guid);
	if (span)
		span.innerHTML = nameOfElement;

	//update the id
	var inputId = $('theInputHiddenElementId_'+guid);
	if (inputId)
		inputId.value = idOfElement;

	//finally hide the auto completer
	hideUniqueAutoCompleter(guid, false, buttonNameAfterSelectionAddSlashes);

	//call the function with the content of the li
	if (callBackFunctionName && (callBackFunctionName != ''))
		eval(callBackFunctionName+'('+idOfElement+');');

	inputId.fire('autoCompleter:change', {inputName: inputName, inputId: inputId});
}

/**
 * this function add a element in the list
 *
 */
function addElementInList(nameOfElement, idOfElement, guid, callBackFunctionName,
													elementName, noCallbackCall, easyquizzServerURL,
													cssLiCloseResultContainer, cssLiSpanResultContainer,
													cssLiResultContainer, basicErrorMessage)
{
	var listOfElementInList = $('listContainer_' + guid);
	if (listOfElementInList)
	{
		var count = 0;

		if (listOfElementInList.childNodes)
			count = listOfElementInList.childNodes.length;

		// Create the input hidden element and cast to prototype object
		var inputHiddenElement = document.createElement("input");
		inputHiddenElement = $(inputHiddenElement);

		if (idOfElement.substring(0, 4) == 'new[')
		{
			inputHiddenElement.setAttribute('name', idOfElement);
			inputHiddenElement.setAttribute('value', nameOfElement);
		}
		else
		{
			inputHiddenElement.setAttribute('name', elementName + '[' + idOfElement + ']');
			inputHiddenElement.setAttribute('value', idOfElement);
		}

		inputHiddenElement.setAttribute('type', 'hidden');

		// Create the div element that should destruct the block
		var imageElement = document.createElement("div");
		imageElement = $(imageElement);
			imageElement.setAttribute('class', cssLiCloseResultContainer);
			imageElement.setAttribute('id', 'imageClose_' + guid + '_' + idOfElement);


		if (callBackFunctionName && (callBackFunctionName != ''))
		{
				imageElement.observe('click', function()
																			{
																				removeElement(guid, imageElement.id, basicErrorMessage);
																				listOfElementInList.fire('autoCompleter:removeListElement', {guid: guid, id: idOfElement, listElement: listOfElementInList});
																				eval(callBackFunctionName+'('+idOfElement+');');
																			});
		}
		else
		{
				imageElement.observe('click', function()
																			{
																				removeElement(guid, imageElement.id, basicErrorMessage);
																				listOfElementInList.fire('autoCompleter:removeListElement', {guid: guid, id: idOfElement, listElement: listOfElementInList});
																			});
		}

		var spanElement = document.createElement("span");
		spanElement = $(spanElement);
		spanElement.setAttribute('class', cssLiSpanResultContainer);


		var textElement = document.createTextNode(nameOfElement);

		var liElement = document.createElement("li");
		liElement = $(liElement);
		liElement.setAttribute('class', cssLiResultContainer);


		spanElement.appendChild(textElement);
		liElement.appendChild(spanElement);
		liElement.appendChild(imageElement);
		liElement.appendChild(inputHiddenElement);

		listOfElementInList.appendChild(liElement);

		//call the function with the content of the li
		if (callBackFunctionName && (callBackFunctionName != '') && !noCallbackCall)
			eval(callBackFunctionName+'('+idOfElement+');');

		//remove value from user input
		var inputUser = $('autocomplete_' + guid);
		if (inputUser)
					inputUser.value = '';

		listOfElementInList.fire('autoCompleter:addListElement', {element: liElement, guid: guid, id: idOfElement, listElement: listOfElementInList});
	}

		updateClearButton(guid, basicErrorMessage);
}

/**
 * This function is used to remove a liElement
 */
function removeElement(guid, idImageElement, basicErrorMessage)
{
	var liElement = $(idImageElement).up('LI');

	if (liElement)
		liElement.remove();

	updateClearButton(guid, basicErrorMessage);
}

/**
 * Clear all element in the multi select
 * Empty the list and update the clear button
 */
function clearMultiSelect(guid, basicErrorMessage)
{
		var listContainer = $('listContainer_' + guid);
		if (listContainer)
		{
			listContainer.update('');
			updateClearButton(guid, basicErrorMessage);
		}
		else
			alert(basicErrorMessage);
}

/**
 * Update the clear button
 */
function updateClearButton(guid, basicErrorMessage)
{
	var listContainer = $('listContainer_' + guid);
	if (listContainer)
	{
		if (listContainer.down('LI'))
			listContainer.next('DIV.autocompleterClearAll').style.display = '';
		else
			listContainer.next('DIV.autocompleterClearAll').style.display = 'none';
	}
	else
		alert(basicErrorMessage);
}




