/***************************************************************************************************************************
JS File:		DivAlert.js
Author:			Adam Schweitzer
Date:			7/24/01
Description:	This js include file displays pop up error messages using a div in lieu of alert boxes.  It
				allows for up to two buttons, which can be named.  A function is called when a button is clicked
				on, which allows the prorammer to either a) use the default one (which re-enables buttons 
				disabled on the form) or b)create their own.


Useage notes:	Using this function is probably more difficult than it needs to be, but I have not found a
				way to make it easier to use.  The following steps must be taken before using the function:

					1) Create a div with an ID property "errorMsg1", within the form of the page
					2)(opt.) Create a style tag to set the colour/ size / position of the div.  Here is
					  a good sample:
								<style>
									#errorMsg1{
									border: 1px solid #666666;
									background-color: #ffcc00;
									padding: 13px;
									font-size: 13px;
									font-family: Verdana, Arial;
									position: absolute;
									height: auto;
									visibility: hidden;
									z-index: 5;
									left: 20px;
									top: 240px;
									width: 400px;
									}
								</style>
								
				Once these have been inserted onto the page, you are ready to use the function.  To use it,
				follow these steps:

					1) Create the error message:  errorMsg = new errorMessage ("This is an error message")
					   errorMessage is an object with the following parameters:
							message			-	The text to be displayed
							trueButtonText	-	The text to be displayed for the 'yes' button.  The default 
												value for this is: OK if there is no false button and Yes 
												if there is.
							falseButtonText	-	The text to be displayed for the 'no' button.  The default
												value is null.
							fieldName		-	This is generally the name of the field that caused the 
												error message to be called, although it does not need to be.
												The importance of this field is that - if provided - the 
												user/programmer defined functions are used when the buttons
												are clicked on in lieu of the default.  The functions the
												programmer uses must be named the following:
												AcceptClick_<fieldName> and DeclineClick_<fieldname> (if the
												false button exists).  ex: if fieldName is test then 
												AcceptClick_test and DeclineClick_test are to be used.
							type			-	The 'type' of error message (this is displayed in the div in 
												red text).  If no false button, then the default type is 
												error.  If there is a false button, the default is confirm.
							
						All of these properties can be overwritten manually by using the dot syntax (ie.
						errorMsg.message = "I changed the message" (nb: true/false button text are referred 
						to by trueText and falseText in this manner).  There are a few other properties that
						may be overwritten that are by default:
							image			-	The image shown.  Default is the red circle with a white X in
												it that indicates an error (except for when type is confirm, 
												in which case it is a question mark)
							showFalse		-	Whether the false button is shown.  It is shown if the false
												button text has a value (this property should only be set
												if the falseText propery has been set manually)

					2)	Show the div:  showErrorDiv()
						This function disables all of the form elements and shows the div.  The programmer
						is responsible for disabling any input buttons (these are not disabled by the function)
						
					3)	NB:  Note that showing the div does not halt the execution of the rest of the program,
						and the programmer will have to do this manually if necessary (ie. if in a validation
						function, return false after showing the div).
****************************************************************************************************************************/



var FormElementState = new Array()
var Disabled = false  // for when DisableFormElements is called.
var errorMsg
var focusField

/**********************************************************************************************
Function:		showErrorDiv
Author:			Adam Schweitzer
Date:			7/24/01													(last modified: 7/26/01)
Description:	This function shows the error DIV statement, and assigns the correct HTML code 
				to display the error message.
Input:			None
Output:			The DIV is shown
Return value:	None
Dependencies:	errorMsg1 DIV in form calling this function
**********************************************************************************************/
function showErrorDiv()
	{
	DisableFormElements()
	//val = '<img src = "' + errorMsg.image + '"> <font size="1" face="verdana" color="#FF0000"><B><U>' + errorMsg.type + ': </B></U></font> <br>'
	val = '<font size="1" face="verdana" color="#FF0000"><B><U>' + errorMsg.type + ': </B></U></font> <br>'
	val += '<font size="1" face="verdana" color="#000000">' + errorMsg.message + '</font><BR><center>'
	val += " <a href=\"JavaScript:document.getElementById('errorMsg1').style.visibility='hidden';EnableFormElements(); AcceptClick_" + errorMsg.fieldName + "() \">  <span> <font size=\"1\" face=\"verdana\" color=\"#000000\"><B><U>" + errorMsg.trueText + '</U></font></span> </a>'
	
	if (errorMsg.showFalse == true)
		{
		val += '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <B> <U>'
		val += '<a href="JavaScript:document.getElementById(\'errorMsg1\').style.visibility=\'hidden\';EnableFormElements() ;DeclineClick_' + errorMsg.fieldName +'()">  <span><font size="1" face="verdana" color="#000000">'
		val += errorMsg.falseText
		}
		
	val += '</B></U></font></span></a></center>'
	document.getElementById ("errorMsg1").innerHTML = val
	document.getElementById('errorMsg1').style.visibility='visible';
	
	}

/**********************************************************************************************
Function:		DisableFormElements
Author:			Adam Schweitzer
Date:			7/19/01													(last modified: 7/24/01)
Description:	This function disables all of the elements of the form and stores their previous
				state in the FormElementState array
Input:			None
Output:			None
Return value:	None
Dependencies:	FormElementState array
**********************************************************************************************/
function DisableFormElements()
	{
	var i
	var j

	if (Disabled == false)						// do not populate the array if already disabled - else can't be enabled correctly
		{
		for (j=0; j < document.forms.length; j++)
			{
			FormElementState[j] = new Array (document.forms[j].length)
			
			with (document.forms[j])
				{
				for (i=0; i < length; i++)
					{
					FormElementState[j] [i] = elements[i].disabled
					elements[i].disabled = true
					}
				}
			}
		Disabled = true
		}
	}
	
/**********************************************************************************************
Function:		EnableFormElements
Author:			Adam Schweitzer
Date:			7/19/01													(last modified: 7/24/01)
Description:	This function re-enables all of the elements of the forms disabled from 
				DisableFormElements
Input:			None
Output:			None
Return value:	None
Dependencies:	FormElementState array
**********************************************************************************************/
function EnableFormElements ()
	{
	var i
	var j
	
	if (Disabled == true)				// array is not populated if not disabled first
		for (j=0; j < FormElementState.length; j++)
			with (document.forms[j])
				for (i=0;i < FormElementState [j].length; i++)
						elements[i].disabled = FormElementState [j] [i]

	Disabled = false
	}
	
/**********************************************************************************************
Function:		errorMessage
Author:			Adam Schweitzer
Date:			7/20/01
Description:	Creates errorMessage object
Input:			The error message, the value for the true button, the value of the false button 
				(if it exists)
Output:			None
Return value:	None
Dependencies:	None
**********************************************************************************************/
function errorMessage (message, trueButtonText, falseButtonText, fieldName, type)
	{	
	this.message = message
	if (trueButtonText == null)
	{
		this.trueText = "OK"
	}
	else
	{
		this.trueText = trueButtonText
	}
	
	this.falseText = falseButtonText
	
	if (falseButtonText == null)
		{
		this.showFalse = false
		this.type = "Error"
		this.image = "images/error.jpg"
		//this.image = "images/info.jpg"
		}
	else
		{
		this.showFalse = true
		this.type = "Confirm"
		this.image = "images/info.jpg"
		}
	
	if (fieldName != null)
	{
		this.fieldName = fieldName
	}
	else
	{
		this.fieldName = ''	
	}	

	if (type != null)
	{		
		this.type = type
		this.image = "images/info.jpg"

		//this.image = "images/error.jpg"
	}
	//else
	//{	this.image = "images/error.jpg"
	//}

	}
	
/**********************************************************************************************
Function:		AcceptClick_
Author:			Adam Schweitzer
Date:			7/25/01													(last modified: 7/31/01)
Description:	General function for accept click if function is not provided in the 
				errorMessage object.
Input:			None
Output:			The buttons hidden earlier are visible, and the field in question has focus
Return value:	None
Dependencies:	focusField
**********************************************************************************************/
function AcceptClick_()
	{
	if (document.getElementById ("submission"))
		document.getElementById ("submission").style.visibility = 'visible'
	
	if (document.getElementById ("back"))
		document.getElementById ("back").style.visibility = 'visible'
	
	if (document.getElementById ("reset"))
		document.getElementById ("reset").style.visibility = 'visible'
	
	if (document.getElementById ("update"))
		document.getElementById ("update").style.visibility = 'visible'
		
	if (document.getElementById('calendar'))
		document.getElementById('calendar').style.visibility='visible';
		
	if (document.getElementById('OwnerType'))
		document.getElementById('OwnerType').style.visibility='visible';
		
	if (document.getElementById('delete'))
		document.getElementById('delete').style.visibility='visible';

	if (document.getElementById('title'))
			document.getElementById('title').style.visibility='visible';
		
	if (focusField)
		focusField.focus()
	}

