// Global variables for selecting all & signaling when a users' marked list is full.

var selAllMLActive = false;
var selAllMLSignal = "";
var selAllMLCounter = 0;
var selAllMLCount = 0;


function selectAll(status)
{
	selectAllMarkedList(status);
}


// Core marked list JavaScript.

// Switch for debugging alert boxes.
var DEBUG = false;

// Global variables for selecting all & signaling when a users' marked list is full.
var selAllMLActive = false;
var selAllMLSignal = "";


/*
** Select all marked list checkboxes on the page, adding the items to the users' marked list.
** Params:	selected - whether the checkboxes should be checked (added) or unchecked (removed)
*/
function selectAllMarkedList(selected)
{
	if (!selAllMLActive)
	{
		selAllMLActive = true;
		selAllMLSignal = "";

		// Loop through the marked list checkboxes, until there are either no more checkboxes,
		// or the list limit has been reached & we're adding.
		checkboxes = document.getElementsByName("mlcb");
		selAllMLCount = 0;
		if( (checkboxes!=null && checkboxes.length>0) && !(selAllMLSignal == "limit" && selected) )
		{
			for(i=0; i<checkboxes.length; i++)
			{
				checkbox = checkboxes.item(i);	
				if (checkbox.checked != selected) 
				{
					checkbox.checked = selected;
					checkbox.onclick();
					selAllMLCount++;
				}
			}
			selAllMLCounter = selAllMLCount;
			
			if (selAllMLCount > 0)
			{
				// Disable the mark all checkbox/link/button if it has an id of 'markAllActivator'.
				var markAllActivator = document.getElementById('markAllActivator');
				if (markAllActivator) 
				{
					markAllActivator.disabled = true;
				}
			}
			selAllMLActive = false;
		}
	}
}

/*
** Process an action for an item, i.e. add/remove to/from the users' marked list.
** Params:	checkbox 	- the item's marked list checkbox object.
*/
function doMarkedList(checkbox)
{
	// Add the method to be executed to the parameters.
	var mlUrl;
	if (checkbox.checked == true) {
		mlUrl = checkbox.value + "&method=add";
	} else {
		mlUrl = checkbox.value + "&method=remove";
	}
	//alert(mlUrl);

	// Get an XMLHTTPConnection object.
	var mlConn = new mlXHConn();
	if (!mlConn)	// Browser doesn't support XMLHttpRequest.
		notifyMarkedListHttpReqNotSupported();

	var fnWhenDone =
		function (XHObj, selAllMLActiveOnConn)
		{
			if (XHObj.status == 200)
			{
				var response = XHObj.responseText.split(",");
				var method = response[1], result = response[2], reason = response[3];
				if (DEBUG)	alert("Response: method= " + method + ", result= " + result + ", reason= " + reason);

				// Process the response.
				if (method.indexOf("add") != -1)
				{
					if (result.indexOf("success") == -1)
					{
						// Couldn't be added, so uncheck the checkbox and give the user the reason.
						checkbox.checked = false;

						if (reason.indexOf("error") != -1) 
						{
							notifyMarkedListAddError();
						} 
						else if (reason.indexOf("reachedLimit") != -1) 
						{
							// Reached ML limit - if selecting all, set signal so user only notified once.
							if (selAllMLActiveOnConn) 
							{
								if (selAllMLSignal != "limit") 
								{
									selAllMLSignal = "limit";
									notifyMarkedListLimitReached();
								}
							} 
							else 
							{
								notifyMarkedListLimitReached();
							}
						}
					} 
					else 
					{
						markedListAddSuccess(checkbox);
					}
				}
				else if (method.indexOf("remove") != -1)
				{
					if (result.indexOf("success") == -1)
					{
						// Couldn't be removed, only recheck the checkbox if an error occurred.
						if (reason.indexOf("error") != -1) 
						{
							checkbox.checked = true;
							notifyMarkedListRemoveError();
						} 
						else if (reason.indexOf("notfound") != -1) 
						{
							// Notify the user only if not deselecting all. (NOTE: probably won't want to do this in a real app)
							if (selAllMLActiveOnConn) 
							{
								notifyMarkedListRemoveNotFound();
							}
						}
					} 
					else 
					{
						markedListRemoveSuccess(checkbox);
					}
				}
				else 
				{
					window.location.pathname="/resultsExpired.do";	
				}
			}
			else
			{	/* For developers sight only - should never happen once deployed anyway! */
				if (DEBUG)	alert("Error with response from server. status=" + XHObj.status + ", resp=" + XHObj.responseText);
			}
		};
	if (DEBUG)
		alert("Connecting to:\n" + mlUrl);

	mlConn.connect(mlUrl, fnWhenDone);	
}

/*
** mlXHConn - Simple XMLHttpRequest Interface Object, specific to marked list.
** Need an object so that many asynchronous requests can be handled simultaneously.
** Had to make this specific to marked list, see me for a general XHConn.
*/
function mlXHConn()
{
	var xmlhttp, bComplete = false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e)	{ try {	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch (e)	{ try {	xmlhttp = new XMLHttpRequest(); }
		catch (e)	{ xmlhttp = false; }}
	}
	if (!xmlhttp)	return null;

	
	this.connect = function(mlUrl, fnDone)
	{
		if (!xmlhttp) return false;
		bComplete = false;

		try {
			xmlhttp.open("POST", encodeURI(mlUrl), true);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

			if (selAllMLActive) {
				xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4 && !bComplete) {
						bComplete = true;
						fnDone(xmlhttp, true);
					}
				};
			} else {
				xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4 && !bComplete) {
						bComplete = true;
						fnDone(xmlhttp, false);
				}
				};
			}
			xmlhttp.send("");
		} catch(z) { return false; }
		return true;
	};
	return this;
}


// Product specific functions below

function notifyMarkedListLimitReached()
{
	alert("Your Marked List is full, with 500 items. Please delete some items before adding more.");
}
function notifyMarkedListAddError()
{
	alert("The server encountered an error, and was unable to add the entry to your marked list.");
}
function notifyMarkedListRemoveError()
{
	alert("The server encountered an error, and was unable to remove the entry from your marked list.");
}
function notifyMarkedListRemoveNotFound()
{
	//alert("That entry was not found in your marked list.");
}
function notifyMarkedListHttpReqNotSupported()
{
	alert("Couldn't get HttpRequest object, marked list functionality not availible! Please upgrade your browser.");
}

function getMarkedListTable(startNode)
{
	node = startNode;
	
	while(node!=null)
	{
		if(node.parentNode)
		{
			node = node.parentNode;
		}
		else
		{
			return null;
		}
		
		c = node.className;
		if(c!=null && (c=="inMarkedList" || c=="notInMarkedList") )
		{
			return node;
		}
	}
	return null;
}

