var szErrMsg;
                  
function EmptyField(input, szMsg)
{
	// Declare local variables
	var szTemp = '';
	var bBadInput = false;
	
	// If the input field is empty then tell the user
	if (input == undefined)
		bBadInput = true;
	else if (input == '')
		bBadInput = true;
	else
	{
		szTemp = Trim(input);
		if (szTemp == '') 
			bBadInput = true;
	}
		
	// Tell the user if it's an invalid input			
	if (bBadInput == true)
	{
		// Use generic message if programmer did not include one
		if (szMsg == '')
			szMsg = 'The input field cannot be empty';
			
		// Tell the user
		alert(szMsg);
		
		// Return failure (assumes this function is called during 
		// OnSubmit click, otherwise return value probably ignored.
		return false;
	}
	else
		return true;
}	


function RadioButtonClick(form, element, nSelectedID)
{
	// Declare local variables
	var nIndex = 0;
	var szName = '';
	var szTemp = '';
	
	// Clear the hidden form field element we use to capture
	// which items are selected.
	szTemp = '';
	
	// Go through and set the radio button whose value is
	// 'nSelectedID' to checked and all the others to not
	// checked.
	for (nIndex = 0; nIndex < form.elements.length; nIndex++)
	{
		// Get a convenient handle to the name.  We're looking
		// only at the controls whose name is 'TipOfTheDay'
		// or 'Todays' article
		szName = form.elements[nIndex].name;
		//alert(szName);
		if (szName.indexOf(element) >= 0)
		{
			if (form.elements[nIndex].value == nSelectedID)
				form.elements[nIndex].checked = true;
			else
				form.elements[nIndex].checked = false;

			// Need to return the selected key in our hidden form
			// field.
			szTemp = nSelectedID;
		}
	}
	
	// Set our flag indicating something has changed
	SetChangedFlag();
	
}

function ChangeColor(DestElement, SrcElement)
{
	// Declare local variables
	var szTemp = '';
	var szMsg = 'Invalid color.  Must begin with "#" and ' +
				'have six digits 0-9, A-F'

	// Make sure we indicated the change
	SetChangedFlag();

	// Validate the length
	szTemp = Trim(SrcElement.value);
	if (szTemp.length != 7)
	{
		alert(szMsg);
		SrcElement.click();
		return -1;
	}
		
	// Make sure it starts with the appropriate sign
	if (szTemp.indexOf('#') != 0)
	{
		alert(szMsg);
		SrcElement.click();
		return -1;
	}
		
	// Change the color of the selected element
	if (DestElement != undefined)
		if (DestElement.style != undefined)
			DestElement.style.background = szTemp;
}

function SetChangedFlag()
{
	// This is called anytime the user selects one of
	// the company checkboxes
	document.Form1.txtChanged.value = '1';
}

function ConfirmDelete(element, szMsg)
{
	// Prompt the user to save the changes
	if (szMsg == '')
	{
		szMsg = 'Do you want to delete the record?\n\n' + 
				'Click OK to delete the record or Cancel ' + 
				'to abort';
	}
	
	// Intialize response to false
	element.value = '0';

	if (confirm(szMsg) == true)
	{
		// Set the form field element to true
		element.value = '1';
	}
}	

function GenericSaveChangesPrompt(element, szMsg)
{
	// Prompt the user to save the changes
	if (szMsg == '')
	{
		szMsg = 'Do you want to save the changes?\n\n' + 
				'Click OK to save the changes or Cancel ' + 
				'to ignore the changes';
	}
	
	// Intialize response to false
	element.value = '0';

	if (confirm(szMsg) == true)
	{
		// Set the form field element to true
		element.value = '1';
	}
}	

function GenericSaveChangesPrompt2(response, changed, szMsg)
{
	// If the user changed something then prompt them
	if (changed.value == '1')
	{
		// Prompt the user to save the changes
		if (szMsg == '')
		{
			szMsg = 'Do you want to save the changes?\n\n' + 
					'Click OK to save the changes or Cancel ' + 
					'to ignore the changes';
		}
	
		// Intialize response to false
		response.value = '0';

		if (confirm(szMsg) == true)
		{
			// Set the form field element to true
			response.value = '1';
		}
	}
}	

function PromptSaveChanges(szMsg)
{
	// Prompt the user to save the changes
	if (window.document.Form1.FreeTextBox1.value != '')
	{
		if (szMsg == '')
		{
			szMsg = 'Do you want to save the changes to the window ' +
					'you are current editing?\n\n' + 'Click OK to ' + 
					'save the changes to the document or Cancel ' + 
					'to ignore the changes';
		}
		
		// Clear flag telling us to user file paths hidden field
		window.opener.document.Form1.txtUseFilePaths.value = '0';

		if (confirm(szMsg) == true)
		{
			// Transfer the contents of the uploaded FreeTextBox to 
			// a hidden field on the AdminArticleEdit.aspx page.  We'll
			// then postback to the form by calling submit() and in the
			// AdminArticleEdit.aspx page_load() we'll load the FreeTextBox
			// on that form with the contents of the hidden form field.
			// We can't just directly copy from one FreeTextBox to the other
			// because we're using the value attribute instead of the Text
			// attribute.  This causes us problems with the format of the data.
			window.opener.document.Form1.TextFreeTextBoxTransfer.value = window.document.Form1.FreeTextBox1.value;
			window.opener.document.Form1.txtUseFilePaths.value = '1';
			window.opener.document.Form1.submit();
		}
	}
	
	// Always closing the window no matter what the user
	// selected.
	self.close();
}	

function PopupWindow(szURL, szName, nHeight, nWidth)
{
	// Declare local variables
	var szFeatures;

	// Show the help window at the specified bookmark
	szFeatures = 'width=' + nWidth + ',height=' + nHeight + ',resizable=yes,scrollbars=yes,toolbar=no';
	return MM_openBrWindow(szURL, szName, szFeatures);
}

function MM_openBrWindow(theURL,winName,features)
{ //v2.0
	
	// Need to clean up the window name
	var szTemp = winName;
	szTemp = FixWindowName(szTemp);

	// Open the window
  	return window.open(theURL,szTemp,features);
}

function ShowArticlePreview(szArticleName)
{
	// Declare local variables
	var szFeatures;

	// Show the help window at the specified bookmark
	szFeatures = 'width=200,height=600,resizable=yes,scrollbars=yes,toolbar=no';
	MM_openBrWindow(szArticleName, 'Article', szFeatures);
}

function FixWindowName(szInput)
{
	// The winName parameter of the window.open call in
	// the function MM_openBrWindow cannot have any spaces.
	// However, the report name which is passed in for this
	// parameter can.  Consequently we need replace any spaces
	// with an underscore (or an alphanumeric character)
	var szOutput = '';
	var nIndex;
	
	// Trim the space from either end of the input
	szInput = Trim(szInput);
	
	// Change any spaces within the string to '_'
	for (nIndex = 0; nIndex < szInput.length; nIndex ++)
		if (szInput.substr(nIndex,1) == ' ')
			szOutput = szOutput + '_';
		else
			szOutput = szOutput + szInput.substr(nIndex,1);
			
	// Return the properly formatted string
	return szOutput;
}

function Trim(s)
{
	// Trim the front
	while (s.substring(0,1) == ' ')
		s = s.substring(1,s.length);

	// Trim the back
	while (s.substring(s.length-1,s.length) == ' ')
		s = s.substring(0,s.length-1);

	// Return the clean string
	return s;
}

function doHourglass()
{
	try
	{
		document.body.style.cursor = 'wait';
	}
	catch(e)
	{
	}
}

function writeError(obj, errIndicatorText, errMsg)
{
	// Declare local variables
	var W3CDOM = (document.getElementsByTagName && document.createElement);
	
	if (obj.hasError) return true;
	if (W3CDOM)
	{
		// Add the error indicator text to our object and set
		// it's class to "error" so the appearance of the object
		// indicates an error.
		obj.onchange = removeError;
		var sp = document.createElement('span');
		obj.className += ' P-ERROR-TEXTBOX';
		sp.className = 'P-ERROR-MESSAGE';
		sp.appendChild(document.createTextNode(errIndicatorText));
		obj.parentNode.appendChild(sp);
		obj.hasError = sp;
		
		// We need to attach a mouseover event handler to our span
		// object so we can display the error message.  Note that IE
		// uses "attachEvent" while Mozilla browsers use "addEventListener".  
		// We can simply test for the existence of one or the other.  Note 
		// that the function AddErrorMessageToolTip function is an address,
		// not a function call.  Consequently, we can't pass it parameters.
		// See the description for the AddErrorMessageToolTip() function.
		// 
		if (document.attachEvent)
			sp.attachEvent("onmouseover", AddErrorMessageToolTip);
		else if (document.addEventListener)
			sp.addEventListener("onmouseover", AddErrorMessageToolTip, false);
		else
			alert('Cannot attach event handler');

		// This is a function with no parameters so we can use it in the
		// attachEvent/addEventListener functions.  This function then calls
		// the tooltip function to attach the error message.  Note that this
		// function must be in the writeError() function so we can attach the
		// current error message.  If it were outside of the writeError()
		// function then all the tooltips would have the same error message,
		// the last one that was added.
		function AddErrorMessageToolTip()
		{
			Tip(errMsg, 
				TITLE, 'Data Entry Error', 
				WIDTH, 150);
		}	

	}
	
	// There is an error so return true
	return true;
}

function removeError()
{
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.parentNode.removeChild(this.hasError);
	this.hasError = null;
	this.onchange = null;
}

function clearError(obj)
{
	obj.className = obj.className.substring(0,obj.className.lastIndexOf(' '));
	if (obj.hasError != undefined)
		obj.parentNode.removeChild(obj.hasError);
	obj.hasError = null;
	obj.onchange = null;
}

function IsAjax()
{
	// Note:  Code downloaded from http://www.w3schools.com/ajax/ajax_browsers.asp
	// Declare local variables
	var xmlHttpTest;
	
	// Initialize browser type
	bIsIE = true;
	
	// Different browsers use different means to create the
	// the XMLHttpRequest() object.
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttpTest = new XMLHttpRequest();
		bIsIE = false;
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttpTest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttpTest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				// Browser does not support Ajax
				return false;
			}
		}
	}	
	
	// If we're here then we successfully created the object
	// and should now delete it since we only want to know
	// if we can do it, we don't actually need to use it.
	delete xmlHttpTest;
	
	// Browser supports ajax if we're here
	return true;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   var szInput = Trim(sText);

    for (i = 0; i < szInput.length && IsNumber == true; i++) 
      { 
      Char = szInput.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function GetErrorMessage(e)
{
	// Declare local variables
	var szMsg;

	// Need to check what type of object 'e' is.  It can
	// be an exception object or a string depending upon 
	// the browser.
	try
	{
		// If it's an object then get the message property.
		// Otherwise it's a simple string.
		if (typeof(e) == 'object')
			szMsg = e.message;
		else if (typeof(e) == 'string')
			szMsg = e;
		else
			throw 'unknown exception type';
	}
	catch (ex)
	{
		szMsg = 'An unknown error occurred in GetErrorMessage()';
	}
	
	// Return the message
	return szMsg;
}

function DisplayError(exception, 
					  fName, 
					  bDisplayError,
					  bDisplayAsErrorMessage)
{
	// Declare local variables
	var szMsg = '';
	
	// The last parameter is optional so we need to fill
	// it in if it wasn't passed in.
	if (typeof bDisplayError == "undefined")
		bDisplayError = true;
		
	// Sometimes we just want to display the error message
	// and not the location or function name where it 
	// occurred.
	if (typeof bDisplayAsErrorMessage == "undefined")
		bDisplayAsErrorMessage = true;
		
	// Build the message to display
	if (bDisplayAsErrorMessage == true)
		szMsg = 'The following error occurred in function ' + fName + ': \r\n\r\n' +
				GetErrorMessage(exception);
	else
		szMsg = GetErrorMessage(exception);
			
	// Display the specified error message is so desired
	if (bDisplayError == true)
		alert(szMsg);
}
