var queryString		= (new XParser()).deserializeString(location.search);
var windowEvents	= new Array();

// Set the array for events to fire when the window loads
windowEvents["onload"] = new Array();
function _window_onload()
{
	if (typeof(windowEvents["onload"]) == "object")
	{
		for (var i = 0; i < windowEvents["onload"].length; i++) windowEvents["onload"][i]();
	}
}
// If an event is already set load it into the array and then set the "hidden" event to fire
if (window.onload) windowEvents["onload"].push(window.onload);


// Set the array for events to fire when the window is resized
windowEvents["onresize"] = new Array();
function _window_onresize()
{
	if (typeof(windowEvents["onresize"]) == "object")
	{
		for (var i = 0; i < windowEvents["onresize"].length; i++) windowEvents["onresize"][i]();
	}
}
// If an event is already set load it into the array and then set the "hidden" event to fire
if (window.onresize) windowEvents["onresize"].push(window.onresize);


/**********************************************************************
Summary: configureFormCheckboxes
This function attaches click events to all input checkboxes with a name
of "LitId" in order to save the various literature ids to the cookie.
It also sets the checked property if the id has been saved on this or
any other page.
**********************************************************************/
function configureFormCheckboxes()
{
	if (document.getElementsByName)
	{
		var checkboxes = document.getElementsByName("LitId");
		
		// Bypass any further execution if we do not have any controls
		if (checkboxes.length != 0)
		{
			var literatureString = getCookie("orderItems");
			var orderItems = (literatureString == "" || literatureString == "undefined"? new Array(): literatureString.split(","));

			for (var i = 0; i < checkboxes.length; i++)
			{
				// Attach event
				checkboxes[i].onclick = function(e)
				{
					var ctl = e? (e.target && e.target.nodeType == 3? e.target.parentNode: e.target): window.event.srcElement;
					
					var literatureString = getCookie("orderItems");
					var orderItems = (literatureString == "" || literatureString == "undefined"? new Array(): literatureString.split(","));

					if (ctl.checked)
					{
						// Make sure that more than three items have not been selected
						if (orderItems.length >= 3)
						{
							alert(window.LocalizedText["LiteratureWarning"]);
							ctl.checked = false;
						}
						else
						{
							orderItems.push(ctl.value);
						}
					}
					else
					{
						// Delete the value
						for (var i = 0; i < orderItems.length; i++)
						{
							if (ctl.value == orderItems[i])
							{
								orderItems.splice(i, 1);
								break;
							}
						}
					}
					
					setCookie("orderItems", orderItems.toString());
				};

				for (var j = 0; j < orderItems.length; j++)
				{
					if (checkboxes[i].value == orderItems[j])
					{
						checkboxes[i].checked = true;
						break;
					}
				}
			}
		}

		// Now look for subscriptions
		checkboxes = document.getElementsByName("SubId");
		
		// Bypass any further execution if we do not have any controls
		if (checkboxes.length != 0)
		{
			var subscriptionString = getCookie("subscriptionItems");
			var subscriptionItems = (subscriptionString == "" || subscriptionString == "undefined"? new Array(): subscriptionString.split(","));

			for (var i = 0; i < checkboxes.length; i++)
			{
				// Attach event
				checkboxes[i].onclick = function(e)
				{
					var ctl = e? (e.target && e.target.nodeType == 3? e.target.parentNode: e.target): window.event.srcElement;
					
					var subscriptionString = getCookie("subscriptionItems");
					var subscriptionItems = (subscriptionString == "" || subscriptionString == "undefined"? new Array(): subscriptionString.split(","));

					if (ctl.checked)
					{
						subscriptionItems.push(ctl.value);
					}
					else
					{
						// Delete the value
						for (var i = 0; i < subscriptionItems.length; i++)
						{
							if (ctl.value == subscriptionItems[i])
							{
								subscriptionItems.splice(i, 1);
								break;
							}
						}
					}
					
					setCookie("subscriptionItems", subscriptionItems.toString());
				};

				for (var j = 0; j < subscriptionItems.length; j++)
				{
					if (checkboxes[i].value == subscriptionItems[j])
					{
						checkboxes[i].checked = true;
						break;
					}
				}
			}
		}
	}
}
windowEvents["onload"].push(configureFormCheckboxes);


/**********************************************************************
Summary: setLanguageOption
The purpose of this function is to set the default language for the 
user and then refresh the page to reflect that preference
**********************************************************************/
function setLanguageOption()
{
	if (document.getElementsByTagName)
	{
		var links	= document.getElementsByTagName("A");
		
		for (var i = 0; i < links.length; i++)
		{
			if (links[i].name.indexOf("LanguageOption") != -1)
			{
				links[i].onclick = function(e)
				{
					var ctl = e? (e.target && e.target.nodeType == 3? e.target.parentNode: e.target): window.event.srcElement;
					var langOpt = ctl.name.split("_");

					if (langOpt.length == 2)
					{
						setCookie("UserDefinedLanguage", langOpt[1]);
						location.reload();
					}
				};
			}
		}
	}
}
windowEvents["onload"].push(setLanguageOption);


/**********************************************************************
Summary: configureCountrySelect
This function sets the CountryId as well as retrieving the 
CountryDivisions for the selected Country
**********************************************************************/
function configureCountrySelect()
{
	if (document.getElementsByName)
	{
		var ctls = document.getElementsByName("CountryId");
		
		if (ctls.length == 1 && ctls[0].tagName.toUpperCase() == "SELECT")
		{
			ctls[0].onchange = function(e)
			{
				var ctl = e? (e.target && e.target.nodeType == 3? e.target.parentNode: e.target): window.event.srcElement;

				// Set the cookie information so it will default back to the right page
				setCookie("countryId", ctl.options[ctl.selectedIndex].value);
				
				// Reload the page with the new CountryDivision information
				location.href = "/Default.asp?siteMapId=Request&CountryId=" + ctl.options[ctl.selectedIndex].value + "#CountryId";
			};
		
			// If a value was passed in via the query string we will use that otherwise we will try to use cookie information
			if (queryString.CountryId != null)
			{
				ctls[0].value = queryString.CountryId;
			}
			else
			{
				// This may be the first time the page is being hit, check for a saved cookie
				if (getCookie("countryId") != "" && getCookie("countryId") != "undefined" && getCookie("countryId") != "70")
				{
					// Reload the page with the new CountryDivision information
					location.href = "/Default.asp?siteMapId=Request&CountryId=" + getCookie("countryId");
				}
			}
		}
	}
}
windowEvents["onload"].push(configureCountrySelect);


/**********************************************************************
Summary: configureCountryDivisionSelect
This function sets the CountryId as well as retrieving the 
CountryDivisions for the selected Country
**********************************************************************/
function configureCountryDivisionSelect()
{
	if (document.getElementsByName)
	{
		var ctls = document.getElementsByName("CountryDivisionId");
		
		if (ctls.length == 1 && ctls[0].tagName.toUpperCase() == "SELECT")
		{
			ctls[0].onchange = function(e)
			{
				var ctl = e? (e.target && e.target.nodeType == 3? e.target.parentNode: e.target): window.event.srcElement;

				// Set the cookie information so it will default back to the right page
				if (ctl.selectedIndex != -1) setCookie("countryDivisionId", ctl.options[ctl.selectedIndex].value);
			};
		
			// If a value was passed in via the query string we will use that otherwise we will try to use cookie information
			if (queryString.CountryDivisionId != null)
			{
				ctls[0].value = queryString.CountryDivisionId;
			}
			else
			{
				// This may be the first time the page is being hit, check for a saved cookie
				if (getCookie("countryDivisionId") != "" && getCookie("countryDivisionId") != "undefined")
				{
					ctls[0].value = getCookie("countryDivisionId");
				}
			}
		}
	}
}
windowEvents["onload"].push(configureCountryDivisionSelect);


/**********************************************************************
Summary: configureGroupSelect
This function sets the Group of programs to view
**********************************************************************/
function configureGroupSelect()
{
	if (document.getElementsByName)
	{
		var ctls = document.getElementsByName("Group");
		
		if (ctls.length == 1 && ctls[0].tagName.toUpperCase() == "SELECT")
		{
			ctls[0].onchange = function(e)
			{
				var ctl = e? (e.target && e.target.nodeType == 3? e.target.parentNode: e.target): window.event.srcElement;

				// Reload the page with the new CountryDivision information
				location.href = "/Default.asp?siteMapId=" + queryString.siteMapId + "&Year=" + queryString.Year + "&Group=" + ctl.options[ctl.selectedIndex].value;
			};
		}
	}
}
windowEvents["onload"].push(configureGroupSelect);


/**********************************************************************
Summary: configureBackButton
The purpose of this function is to set the BackButton
**********************************************************************/
function configureBackButton()
{
	if (document.getElementsByName)
	{
		var ctls = document.getElementsByName("BackButton");

		if (ctls.length == 1 && ctls[0].tagName.toUpperCase() == "INPUT" && ctls[0].type.toUpperCase() == "BUTTON")
		{
			ctls[0].onclick = function(e)
			{
				history.back();
			};
		}
	}
}
windowEvents["onload"].push(configureBackButton);


/**********************************************************************
Summary: configureResetButton
The purpose of this function is to set the ResetButton to not only 
clear the form but also delete the orderItems cookie
**********************************************************************/
function configureResetButton()
{
	if (document.getElementsByName)
	{
		var ctls = document.getElementsByName("ResetButton");

		if (ctls.length == 1 && ctls[0].tagName.toUpperCase() == "INPUT" && ctls[0].type.toUpperCase() == "RESET")
		{
			ctls[0].onclick = function(e)
			{
				var ctl = e? (e.target && e.target.nodeType == 3? e.target.parentNode: e.target): window.event.srcElement;

				deleteCookie("orderItems");
				deleteCookie("subscriptionItems");

				// Uncheck any checked literature items
				var checkboxes = document.getElementsByName("LitId");
				if (checkboxes.length > 0)
				{
					for (var i = 0; i < checkboxes.length; i++)
					{
						checkboxes[i].checked = false;
					}
				}

				// Uncheck any checked subscription items
				checkboxes = document.getElementsByName("SubId");
				if (checkboxes.length > 0)
				{
					for (var i = 0; i < checkboxes.length; i++)
					{
						checkboxes[i].checked = false;
					}
				}
			};
		}
	}
}
windowEvents["onload"].push(configureResetButton);


/**********************************************************************
Summary: configureContinueButton
**********************************************************************/
function configureContinueButton()
{
	if (document.getElementsByName)
	{
		var ctls = document.getElementsByName("ContinueButton");

		if (ctls.length == 1 && ctls[0].tagName.toUpperCase() == "INPUT" && ctls[0].type.toUpperCase() == "BUTTON")
		{
			ctls[0].onclick = function(e)
			{
				var ctl = e? (e.target && e.target.nodeType == 3? e.target.parentNode: e.target): window.event.srcElement;
				
				if (document.getElementById("FirstName"))	setCookie("firstName", document.getElementById("FirstName").value);
				if (document.getElementById("LastName"))	setCookie("lastName", document.getElementById("LastName").value);
				if (document.getElementById("Line1"))		setCookie("line1", document.getElementById("Line1").value);
				if (document.getElementById("Line2"))		setCookie("line2", document.getElementById("Line2").value);
				if (document.getElementById("Line3"))		setCookie("line3", document.getElementById("Line3").value);
				if (document.getElementById("Locality"))	setCookie("locality", document.getElementById("Locality").value);
				if (document.getElementById("PostalCode"))	setCookie("postalCode", document.getElementById("PostalCode").value);

				if (document.getElementById("CountryId") && document.getElementById("CountryId").selectedIndex != -1)
				{
					with (document.getElementById("CountryId").options[document.getElementById("CountryId").selectedIndex])
					{
						setCookie("countryId", value);
						setCookie("countryName", text);
					}
				}

				if (document.getElementById("CountryDivisionId") && document.getElementById("CountryDivisionId").selectedIndex != -1)
				{
					with (document.getElementById("CountryDivisionId").options[document.getElementById("CountryDivisionId").selectedIndex])
					{
						setCookie("countryDivisionId", value);
						setCookie("countryDivisionName", text);
					}
				}
				else
				{
					setCookie("countryDivisionId", "");
					setCookie("countryDivisionName", "");
				}

				if (document.getElementById("SourceId") && document.getElementById("SourceId").selectedIndex != -1)
				{
					with (document.getElementById("SourceId").options[document.getElementById("SourceId").selectedIndex])
					{
						setCookie("sourceId", value);
						setCookie("sourceName", text);
					}
				}
				else
				{
					setCookie("sourceId", "");
					setCookie("sourceName", "");
				}

				// Have the user confirm their order
				var literatureString = getCookie("orderItems");
				var orderItems = (literatureString == "" || literatureString == "undefined"? new Array(): literatureString.split(","));

				var subscriptionString = getCookie("subscriptionItems");
				var subscriptionItems = (subscriptionString == "" || subscriptionString == "undefined"? new Array(): subscriptionString.split(","));

				if (orderItems.length > 0 || subscriptionItems.length > 0)
				{
					var queryString = "";

					if (orderItems.length > 0)			queryString += "&LitList=" + literatureString;
					if (subscriptionItems.length > 0)	queryString += "&SubList=" + subscriptionString;

					location.href = "/Default.asp?siteMapId=ConfirmOrder" + queryString;
				}
				else alert(LocalizedText["NothingSelectedWarning"]);
			};
		}
	}
}
windowEvents["onload"].push(configureContinueButton);

/**********************************************************************
Summary: configureOrderLitButton
The purpose of this function is to set the BackButton
**********************************************************************/
function configureOrderLitButton()
{
	if (document.getElementsByName)
	{
		var ctls = document.getElementsByName("OrderLitButton");

		if (ctls.length == 1 && ctls[0].tagName.toUpperCase() == "INPUT" && ctls[0].type.toUpperCase() == "BUTTON")
		{
			ctls[0].onclick = function(e)
			{
				var ctlId = document.getElementById("OrderId");

				var literatureString = getCookie("orderItems");
				var orderItems = (literatureString == "" || literatureString == "undefined"?
					new Array(): 
					literatureString.split(","));

				if (orderItems.length >= 3)
				{
					alert(window.LocalizedText["LiteratureWarning"]);
				}
				else
				{
					orderItems.push(ctlId.value);
				}
				
				setCookie("orderItems", orderItems.toString());
				
				location.href = "/Default.asp?siteMapId=Request";
			};
		}
	}
}
windowEvents["onload"].push(configureOrderLitButton);


/**********************************************************************
Summary: configureSubmitButton
**********************************************************************/
function configureSubmitButton()
{
	if (document.getElementById)
	{
		var ctl = document.getElementById("SubmitButton");

		if (ctl && ctl.tagName.toUpperCase() == "INPUT" && ctl.type.toUpperCase() == "BUTTON")
		{
			// Before setting any events to submit the order make sure we have orderItems or subscriptionItems
			if ((getCookie("orderItems") == "" || 
				getCookie("orderItems") == "undefined") && 
				(getCookie("subscriptionItems") == "undefined" || 
				getCookie("subscriptionItems") == "undefined"))
			{
				// Nothing can take place - push them back to the literature library
				location.href = "/Default.asp?siteMapId=LiteratureLibrary";
				return;
			}
		
			ctl.onclick = function(e)
			{
				// If they want to be rememebered we will leave their information intact 
				// otherwise we will clear all their cookie information
				var ctl = document.getElementById("RememberMe");
				if (ctl && !ctl.checked)
				{
					deleteCookie("firstName");
					deleteCookie("lastName");
					deleteCookie("line1");
					deleteCookie("line2");
					deleteCookie("line3");
					deleteCookie("locality");
					deleteCookie("postalCode");
					deleteCookie("countryId");
					deleteCookie("countryName");
					deleteCookie("countryDivisionId");
					deleteCookie("countryDivisionName");
				}

				// If the form exists
				if (document.getElementById("OrderForm"))
				{
					// No matter what clear their order information
					deleteCookie("sourceId");
					deleteCookie("sourceName");
					deleteCookie("orderItems");
					deleteCookie("subscriptionItems");

					// Submit the form
					document.getElementById("OrderForm").submit();
					
					// Make sure they do not submit this order again, remove the event handler from this button
					this.onclick = null;
					if (this && this.style) this.style.visibility = 'hidden';
				}
				
				return true;
			};
		}
	}
}
windowEvents["onload"].push(configureSubmitButton);


/**********************************************************************
Summary: configureResetButton
The purpose of this function is to set the ResetButton to not only 
clear the form but also delete the orderItems cookie
**********************************************************************/
function configureFormValues()
{
	if (!document.getElementsByName) return;

	var controlNames = new Array(
		"FirstName", 
		"LastName", 
		"Line1", 
		"Line2", 
		"Line3", 
		"Locality", 
		"CountryDivisionId", 
		"CountryDivisionName", 
		"CountryId", 
		"CountryName", 
		"PostalCode", 
		"OrderItems", 
		"SubscriptionItems", 
		"SourceId", 
		"SourceName");

	for (var i = 0; i < controlNames.length; i++)
	{
		var ctls = document.getElementsByName(controlNames[i]);
		for (var j = 0; j < ctls.length; j++)
		{
			if (ctls[j].tagName.toUpperCase() == "SPAN")
			{
				ctls[j].innerHTML = getCookie(controlNames[i]) == "undefined"? "": getCookie(controlNames[i]);
			}
			else if (ctls[j].tagName.toUpperCase() == "INPUT")
			{
				ctls[j].value = getCookie(controlNames[i]) == "undefined"? "": getCookie(controlNames[i]);
			}
		}
	}
}
windowEvents["onload"].push(configureFormValues);


// Attach event handlers
window.onload	= _window_onload;
window.onresize	= _window_onresize;





























