// Used to track __doPostback() event nesting.
var vSubmitStatus = false;
var UserTypeId = 0;
var GroupTypeId = 0;
// ===============================================================================
// go back one page in history - this has been pulled out into a function so that 
// any errors can be caught. It is possible to jump directly to a page with a back 
// button and so have no history.
// -------------------------------------------------------------------------------
function HistoryBack()
{
	try
	{
		history.back();
	}
	catch (ex)
	{
	}
}

// ===============================================================================
// register a new onload function
// -------------------------------------------------------------------------------
function RegisterOnloadEvent(FunctionName)
{
	var oOnLoad = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = FunctionName;
	}
	else 
	{
		window.onload = function() 
		{
			if (oOnLoad) 
			{
				oOnLoad();
			}
			FunctionName();
		}
	}
}
// ===============================================================================
// register a new onload function
// -------------------------------------------------------------------------------
function RegisterOnUnloadEvent(FunctionName)
{
	var oOnUnLoad = window.onunload;
	if (typeof window.onunload != 'function')
	{
		window.onunload = FunctionName;
	}
	else 
	{
		window.onunload = function() 
		{
			if (oOnUnLoad) 
			{
				oOnUnLoad();
			}
			FunctionName();
		}
	}
}
/********************************************************************************************************
Function to load a page into an iframe
********************************************************************************************************/
function GetIframe(TargetName, IsParent) 
{
	var objIframe;
	if (IsParent + '' == 'true')
		objIframe = parent.document.getElementById(TargetName);
	else
		objIframe = document.getElementById(TargetName);
	var docIframe;
	if (objIframe.contentDocument) 
	{
	// For NS6
		docIframe =  objIframe.contentDocument;
	}
	else if (objIframe.contentWindow) 
	{
	// For IE5.5 and IE6
		docIframe =  objIframe.contentWindow.document;
	}
	else if (objIframe.document)
	{
	// For IE5
		docIframe = objIframe.document;
	}
	return docIframe;
}
/********************************************************************************************************
used to sort numbers by Array.sort()
********************************************************************************************************/
function sortNumbers(first, second) 
{
	return first-second
}
/********************************************************************************************************
Returns the characters from certain HTML encodings
********************************************************************************************************/
function HTMLDecode(strValue)
{
	var strTemp = strValue;
	
	strTemp = strTemp.replace(/&amp;/ig, "&");
	strTemp = strTemp.replace(/&quot;/ig, "\"");
	strTemp = strTemp.replace(/&gt;/ig, ">");
	strTemp = strTemp.replace(/&lt;/ig, "<");
	strTemp = strTemp.replace(/&nbsp;/ig, " ");
	strTemp = strTemp.replace(/&apos;/ig, "\'");

	return strTemp;
}

/********************************************************************************************************
Validates the specified e-mail address

strEmail - string - The e-mail address that is validated
********************************************************************************************************/
function validateEmailAddress(strEmail) 
{
	 var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	 var regex = new RegExp(emailReg);
	 return regex.test(strEmail);
}

/********************************************************************************************************
Changes the visibility of the specified control

oControl - Page Control - the control on the page that is to be shown/hidden
DisplayControl - boolean - whether to show (true) or hide (false) the specified control
********************************************************************************************************/
function SetVisibility(oControl, DisplayControl)
{
	if (oControl != null)
	{
		if (DisplayControl == true)
		{
			if (oControl.style.visibility != 'visible')
				oControl.style.visibility = 'visible';
		}
		else
		{
			if (oControl.style.visibility != 'hidden')
				oControl.style.visibility = 'hidden';
		}
	}
}
/********************************************************************************************************
Converts a string representation of a boolean to a boolean value

value - string - The string value to be converted to a boolean
********************************************************************************************************/
function StringToBool(value)
{
	return ((value == "True") || (value == "true"));
}

/********************************************************************************************************
Returns the current visibility of the specified control

oControl - Page Control - the control on the page that is being examined
********************************************************************************************************/
function IsVisible(oControl)
{
	switch (oControl.style.visibility)
	{
		case 'hidden':
			return false;
			break;
		default:
			return true;
			break;
	}
}

/********************************************************************************************************
Simple check to see if the specified control is valid or not

oControl - Page Control - the control on the page that is being validated
oHeader - Page Control - the header control for the oControl page control
oTable - HTML Table Page Control - the table control that contains the oControl page control
********************************************************************************************************/
function IsControlValid(oControl, oHeader, oTable)
{
	if (IsVisible(oTable) && !(oControl.disabled))
	{
		switch (oControl.type)
		{
			case 'password':
			case 'text':
			case 'file':
			case 'hidden':
			case 'textarea':
			{
				var IsValid = (oControl.value != "");
				HighlightControl(IsValid, oHeader);
				return IsValid;
			}
			case 'select-one':
			case 'select-multiple':
			{
				var IsValid = ((oControl.options.length > 0) || ((oControl.selectedIndex >= 0) && trim(oControl.options[oControl.selectedIndex].value) != ""));
				
				HighlightControl(IsValid, oHeader);
				return IsValid;
			}
			default:
			{
				return true;
			}
		}
	}
	else
	{
		//If the control is not visible, it is valid
		HighlightControl(true, oHeader);
		return true;
	}
}
/********************************************************************************************************
Simple check to see if the specified date is valid or not

oControl - Page Control - the control on the page that is being validated
oHeader - Page Control - the header control for the oControl page control
oTable - HTML Table Page Control - the table control that contains the oControl page control
********************************************************************************************************/
function IsDateValid(oControlDay, oControlMonth, oControlYear, oHeader, oTable)
{
	if (IsVisible(oTable) && !(oControl.disabled))
	{
		switch (oControl.type)
		{
			case 'select-one':
			{
				var lcvarDate = new Date();
				var lcvarNewDate = '', lcvarTempVar, lcvarOldDate;
				
				// set the day to be the first so that the month doesn't kill the date if it is the 31st today
				lcvarDate.setDate('1');
				// is the article date valid?
				lcvarTempVar = trim(oControlYear.options[oControlYear.selectedIndex].value);
				//document.frmTemp.txtArticleDate.value = lcvarTempVar;
				lcvarDate.setYear(lcvarTempVar);
				
				lcvarTempVar = trim(oControlMonth.options[oControlMonth.selectedIndex].value);
				//document.frmTemp.txtArticleDate.value += '' + lcvarTempVar;
				lcvarOldDate = lcvarTempVar;
				lcvarTempVar = lcvarTempVar * 1;
				lcvarTempVar--;
				lcvarDate.setMonth(lcvarTempVar);
		
				lcvarTempVar = trim(oControlDay.options[oControlDay.selectedIndex].value);
				//document.frmTemp.txtArticleDate.value += '' + lcvarTempVar;
				lcvarOldDate = lcvarOldDate + '' + lcvarTempVar;
				lcvarDate.setDate(lcvarTempVar);
			
				// Add Month
				lcvarTempVar = '' + (lcvarDate.getMonth() + 1);
				if (lcvarTempVar.length == 1)
					lcvarTempVar = '' + '0' + lcvarTempVar;
				lcvarNewDate +=  '' + lcvarTempVar;
			
				// Add Day
				lcvarTempVar = '' + lcvarDate.getDate();
				if (lcvarTempVar.length == 1)
					lcvarTempVar = '' + '0' + lcvarTempVar;
				lcvarNewDate +=  '' + lcvarTempVar;
				
				var IsValid = (lcvarNewDate != lcvarOldDate);
				HighlightControl(IsValid, oHeader);
				return IsValid;
			}
			default:
			{
				return true;
			}
		}
	}
	else
	{
		//If the control is not visible, it is valid
		HighlightControl(true, oHeader);
		return true;
	}
}
/********************************************************************************************************
Clears the values from the specified control

oControl - Page Control - the control on the page where the values are being removed
********************************************************************************************************/
function ClearControlValues(oControl)
{
	if (oControl)
	{
		switch (oControl.type)
		{
			case 'text':
			case 'file':
			case 'hidden':
			case 'textarea':
			{
				oControl.value = '';
				break;
			}
			case 'select-one':
			case 'select-multiple':
			{
				oControl.options.length = 0;
				break;
			}
			case 'radio':
			case 'checkbox':
			{
				oControl.checked = false;
				break;
			}
			default:
				alert('Cannot clear values for type: ' + oControl.type);
		}
	}
}

/********************************************************************************************************
Changes the colour of the text in the specified header control

IsValid - boolean - whether the header colour should indicate if the control is valid or not
oHeader - Page Control - the header control where the text colour is to be updated
********************************************************************************************************/
function HighlightControl(IsValid, oHeader)
{
	if (oHeader != null)
		oHeader.style.color = IsValid ? "" : "#FF6643";	
}

/********************************************************************************************************
Removes the spaces from the front of the specified string

sString - string - the string to be trimmed
********************************************************************************************************/
function leftTrim(sString) 
{
	if (sString != null && sString.constructor == String)
	{
		while (sString.substring(0,1) == ' ')
		{
			sString = sString.substring(1, sString.length);
		}
	}
	return sString;
}

/********************************************************************************************************
Removes the spaces from the back of the specified string

sString - string - the string to be trimmed
********************************************************************************************************/
function rightTrim(sString) 
{
	if (sString != null && sString.constructor == String)
	{
		while (sString.substring(sString.length-1, sString.length) == ' ')
		{
			sString = sString.substring(0,sString.length-1);
		}
	}
	return sString;
}

/********************************************************************************************************
Removes the spaces from the front and the back of the specified string

sString - string - the string to be trimmed
********************************************************************************************************/
function trim(sString) 
{
	if (sString != null && sString.constructor == String)
	{
		sString = leftTrim(sString);
		sString = rightTrim(sString);
	}
	return sString;
}

/********************************************************************************************************
Adds a new option to the specified list control

oListControl - List Control - the list control where the new option will be created
OptionInnerText - string - The text that the new option will have (this is shown)
OptionValue - string - The value that the new option will have (this is not shown)
********************************************************************************************************/
function AddOption(oListControl, OptionInnerText, OptionValue)
{
	oListControl.options[oListControl.options.length] = 
		new Option(OptionInnerText, OptionValue);
}

/********************************************************************************************************
Removes the last character from a textbox or hidden control if a value exists

oControl - Page Control - the control on the page which will be updated
********************************************************************************************************/
function RemoveTrailingSpacerCharacter(oControl)
{
	if (oControl.value != "")
		oControl.value = oControl.value.substring(0, oControl.value.length-1);
}

/********************************************************************************************************
Opens a popup where a list of e-mail addresses can be created and maintained

strControlName - string - The dropdown list on the popup opener page that holds the list of e-mail
	addresses
strHiddenControlName - string - The hidden control on the popup opener page that holds the 
	list of e-mail addresses
********************************************************************************************************/
function updateEmailAddresses(strControlName, strHiddenControlName, strFirstName, strSurname)
{
	//The control must exist on the page
	var oControl = document.getElementById(strControlName);
	if (oControl != null)
	{
		var NewWindowURL = "/Popups/UpdateEmailAddresses.aspx" + 
			"?ReturnControl=" + strControlName + 
			"&ValueControl=" + strHiddenControlName;				
		var NewWindow = window.open(NewWindowURL, "UpdateEmailAddressesPopup", 
			"width=700, height=390, toolbar=no, scrollbars=no, menubar=no, resizeable=no");
		NewWindow.focus();
	}
}

/********************************************************************************************************
Opens a popup where a list of groups can be created and maintained

strControlName			- string - The list on the popup opener page that holds the list of groups
strHiddenControlName	- string - The hidden control on the popup opener page that holds the 
						list of groups
blnExtranetDomain		- spefifies that the domain to be searched is the extranet domain
********************************************************************************************************/
function selectGroups(strControlName, strHiddenControlName, blnExtranetDomain)
{
	var NewWindowURL = "/Popups/SelectGroups.aspx" +
			"?ReturnControl=" + strControlName + 
			"&ValueControl=" + strHiddenControlName;
	if (blnExtranetDomain + '' == "true")
		NewWindowURL += "&ExtranetDomain=true";
	var NewWindow = window.open(NewWindowURL, "SelectGroupsPopup", 
		"width=700, height=650, top=15, left=25, toolbar=no, scrollbars=no, menubar=no, resizeable=no");
	NewWindow.focus();
}

/********************************************************************************************************
Opens a popup where a list of groups can be imported and created

SelectedFolder - string - The relative path where the list is to be created, or where the list
	currently exists
SelectedFile - string - The name of the existing file (if any)
CallbackFunction - string - The name of the function on the caller page that is called when the
	list is created/updated
********************************************************************************************************/
function selectImportGroup(SelectedFolder, SelectedFile, CallbackFunction, ExtranetDomain)
{
	var NewWindowURL = "/Popups/SelectGroups.aspx" + 
		"?ShowImportButton=true" + 
		"&SelectedFolder=" + encodeURI(SelectedFolder) + 
		"&SelectedFile=" + encodeURI(SelectedFile) + 
		"&CallbackFunction=" + CallbackFunction;	
	if (ExtranetDomain + '' == "true")
		NewWindowURL += "&ExtranetDomain=true";
	var NewWindow = window.open(NewWindowURL, "ImportGroupPopup", 
		"width=700, height=690, top=15, left=25, toolbar=no, scrollbars=no, menubar=no, resizeable=no");
	NewWindow.focus();
}

/********************************************************************************************************
Opens a popup where a list of groups can be imported and created

SelectedFolder - string - The relative path where the list is to be created, or where the list
	currently exists
SelectedFile - string - The name of the existing file (if any)
CallbackFunction - string - The name of the function on the caller page that is called when the
	list is created/updated
********************************************************************************************************/
function selectImportUsers(SelectedFolder, SelectedFile, CallbackFunction, ExtranetDomain)
{
	var NewWindowURL = "/Popups/SelectUsers.aspx" + 
		"?ShowImportButton=true" + 
		"&SelectedFolder=" + encodeURI(SelectedFolder) + 
		"&SelectedFile=" + encodeURI(SelectedFile) + 
		"&CallbackFunction=" + CallbackFunction;
	if (ExtranetDomain + '' == "true")
		NewWindowURL += "&ExtranetDomain=true";
	var NewWindow = window.open(NewWindowURL, "ImportUsersPopup", 
		"width=700, height=550, toolbar=no, scrollbars=no, menubar=no, resizeable=no");
	NewWindow.focus();
}

/********************************************************************************************************
Opens a popup where a user can be selected

strControlName - string - The name of the control on the caller page where the selected username will
	be returned to
********************************************************************************************************/
function selectOwner(strControlName)
{
	var UserType = document.getElementById("ddAccountTypes");
	var intUserTypeId = UserType.options[UserType.selectedIndex].value;

	var NewWindowURL = "/Popups/SelectUser.aspx" + 
		"?ReturnControl=" + strControlName +
		"&UserTypeId=" + intUserTypeId;
	var NewWindow = window.open(NewWindowURL, "SelectUserPopup", 
		"width=700, height=440, toolbar=no, scrollbars=no, menubar=no, resizeable=no");
	NewWindow.focus();
}

/********************************************************************************************************
Opens a popup where a location can be selected

strControlName - string - The name of the control on the caller page where the selected location will
	be returned to
********************************************************************************************************/
function selectLocation(strControlName)
{
	var UserType = document.getElementById("ddAccountTypes");
	var intUserTypeId = UserType.options[UserType.selectedIndex].value;
	
	//The control must exist on the page
	var oControl = document.getElementById(strControlName);
	if (oControl != null)
	{
		var NewWindowURL = "/Popups/SelectLocation.aspx" + 
			"?ReturnControl=" + strControlName + 
			"&CurrentValue=" + escape(oControl.value) + 
			"&UserTypeId=" + intUserTypeId;
		
		var NewWindow = window.open(NewWindowURL, "SelectLocationPopup", 
			"width=700, height=400, toolbar=no, scrollbars=no, menubar=no, resizeable=no");
		NewWindow.focus();	
	}
}

/********************************************************************************************************
Opens a popup where a group can be selected

strControlName - string - The name of the control on the caller page where the selected group will
	be returned to
********************************************************************************************************/
function selectGroup(strControlName)
{
	//The control must exist on the page
	var oControl = document.getElementById(strControlName);
	if (oControl != null)
	{
		var NewWindowURL = "/Popups/SelectGroup.aspx" + 
			"?ReturnControl=" + strControlName + 
			"&CurrentValue=" + escape(oControl.value);
		var NewWindow = window.open(NewWindowURL, "SelectGroupPopup", 
			"width=700, height=400, toolbar=no, scrollbars=no, menubar=no, resizeable=no");
		NewWindow.focus();	
	}
}

/********************************************************************************************************
Opens a popup where a machine name can be selected

strControlName - string - The name of the control on the caller page where the selected machine will
	be returned to
********************************************************************************************************/
function selectMachine(strControlName)
{
	var UserType = document.getElementById("ddAccountTypes");
	if (UserType == null)
		UserType = document.getElementById("Users_DropDownFilter");
	var intUserTypeId = UserType.options[UserType.selectedIndex].value;

	//The control must exist on the page
	var oControl = document.getElementById(strControlName);
	if (oControl != null)
	{
		var NewWindowURL = "/Popups/SelectMachine.aspx" + 
			"?ReturnControl=" + strControlName + 
			"&CurrentValue=" + escape(oControl.value) +
			"&UserTypeId=" + intUserTypeId;
		var NewWindow = window.open(NewWindowURL, "SelectMachinePopup",
			"width=700, height=400, toolbar=no, scrollbars=no, menubar=no, resizeable=no");
		NewWindow.focus();
	}
}

/********************************************************************************************************
Opens a popup where a date can be selected

strControlName - string - The name of the control on the caller page where the selected date will
	be returned to
********************************************************************************************************/
function selectDate(strControlName)
{
	var NewWindowURL = "/Popups/SelectDate.aspx" + 
		"?ReturnControl=" + strControlName;
	var NewWindow = window.open(NewWindowURL, "SelectDatePopup",
		"width=212, height=190, toolbar=no, scrollbars=no, menubar=no, resizeable=no");
	NewWindow.focus();
}

/********************************************************************************************************
Calls the function in the header to reset the main frame back to the home page
********************************************************************************************************/
function GoToHomePage()
{
	parent.frames['Header'].ShowHomePage();
}

/********************************************************************************************************
Handler for the click event for the final 'OK' button that is shown once the 
operation has successfully completed

oControl - Page Control - the button control that was originally clicked ("OK" or "Apply")
********************************************************************************************************/
function clickSuccess(oControl)
{
	if (oControl.attributes["buttonClicked"].value == "OK")
		GoToHomePage();
	else
		//Returns the page to its default state
		location.href = location.href;
}

/********************************************************************************************************
Changes the cursor for all page controls to the hourglass
********************************************************************************************************/
function DisableControls()
{
	//Change the mouse cursor to the wait (hourglass) image
	for (var x=0; x < document.all.length; x++)
		document.all[x].style.cursor = "wait";
}

/********************************************************************************************************
Changes the cursor for all page controls back to the default (the arrow)
********************************************************************************************************/
function EnableControls()
{
	//Change the mouse cursor back to normal
	for (var x=0; x<document.all.length; x++)
		document.all[x].style.cursor = "auto";
}

/********************************************************************************************************
Opens the popup where a folder can be created or updated

UpdateFolder - boolean - whether the folder is being updated or deleted
SelectedFolder - string - either the parent folder when creating a folder, or the current folder
	when updating a folder
OpenerSuccessFunction - string - the name of the function on the popup opener page (where this call is made from)
	that is called when the folder is created.  The call is made with the new folder path as the sole parameter
OpenerErrorFunction - string - the name of the function on the popup opener page (where this call is made from)
	that is called when any error occurs when creating or updating a folder
********************************************************************************************************/
function OpenFolderPopup(UpdateFolder, SelectedFolder, OpenerSuccessFunction, OpenerErrorFunction)
{
	var NewWindow = window.open("/Popups/CreateNewFolder.aspx" + 
		"?ParentDirectory=" + escape(SelectedFolder) +
		"&UpdateFolder=" + UpdateFolder + 
		"&OpenerSuccessFunction=" + OpenerSuccessFunction + 
		"&OpenerErrorFunction=" + OpenerErrorFunction,
		"EditFolderPopup",
		"width=700, height=300, toolbar=no, scrollbars=no, menubar=no, resizeable=no");
	NewWindow.focus();
}

/********************************************************************************************************
Checks to see if an XML parameter is valid or not

parameter - XML parameter - the parameter that is to be validated
********************************************************************************************************/
function IsParameterValid(parameter)
{
	var IsValid = true;
	
	if (parameter != null)
	{
		if (isArray(parameter))
		{
			for (var i=0; i<parameter.length; i++)
				if (!IsParameterValid(parameter[i]))
					IsValid = false;
		}
		else if (!isNaN(parameter))
		{
			// BUGFIX: changed the condition to see that the value is less than zero, rather than less or equal to zero
			if (parseInt(parameter) < 0)			
				IsValid = false;
		}
		else if (trim(parameter) == "")
		{	
			IsValid = false;
		}
	}
	return IsValid;
}

/********************************************************************************************************
Whether the specified object is an array or not

obj - object - the object to be validated
********************************************************************************************************/
function isArray(obj)
{
   if (obj.constructor.toString().indexOf("Array") == -1)
	  return false;
   else
	  return true;
}

function CheckCharactersForEmailAddress(Value)
{
	return Value.replace(/[\\\/\"\<\>\(\)\,\;\:\$\&\!\`\'\^\*\|\[\]\{\} \t]/ig, "");
}
