/*	Copyright (c) 1996,2001 Picdar Technology Limited       All Rights Reserved
 *
 * 	This is UNPUBLISHED PROPRIETARY work of Picdar Technology Limited;
 * 	the contents of this file may not be disclosed to third parties, copied or
 * 	duplicated in any form, in whole or in part, without the prior written
 * 	permission of Picdar Technology Limited
 *
 *	Module   :	Forms001.js
 *
 *	Synopsis :	JavaScript Common Code Form Utils
 *
 *	Author   :	Many
 *
 */

/* 	REQUIRES:
	String.js
	DateTime.js (only if date and time validation is required.)
*/

/*	Used by the edit inputs common code. This procedure is used to add a value selected
	in a select list to a text area. The elemnts need to be aranged carefully :
	TEXT AREA - ADD BUTTON - SELECT LIST
	
	button	the Add button, which should be the form element DIRECTLY between the text ares
			and the list
	form	The form that contaisn the elements
 */
function AddValue(button,form)
	{
	var source;
	var target;
	
	for (var i = 0; i < form.elements.length; i++)
		{
		if (form.elements[i].name == button.name)
			{
			source = form.elements[i + 1];
			target = form.elements[i - 1];
			break;
			}
		}
	
	for (i = 0; i < source.length; ++i)
		{
		if (source.options[i].selected == true && !ContainsValue(target.value.toUpperCase(),source.options[i].value.toUpperCase()))
			{
			if (target.value == "")
				target.value = source.options[i].value;
			else
				target.value = source.options[i].value + "\r" + target.value;
			}
		}
	}
	
function AddValueFromList(theList, form)
	{
	var source = theList;
	var target;
	for (var i = 0; i < form.elements.length; i++)
		{
		if (form.elements[i].name == list.name)
			{
			target = form.elements[i - 1];
			break;
			}
		}
	var sourceValue = source.options[source.selectedIndex].value;
	if (target.value == "")
		target.value = sourceValue;
	else
		target.value = target.value + "\r" + sourceValue;
	}

/* global variable, required for next function */
var checkSubmissionCueVariable = false;

/* This function returns true if it hasn't been called from the current page
   and false if it has.  Designed to be used in an onsubmit handler, or in a
   "Go" function which submits a form.

	example: <form name="myForm" onsubmit="return CheckSingleSubmission(true);">
   
   A popup alert message is displayed if alertUser is true
*/
function CheckSingleSubmission(alertUser)
	{
	if (!checkSubmissionCueVariable)
		{
		checkSubmissionCueVariable = true;
		result = true;
		}
	else if (!alertUser)
		{
		result = false;
		}
	else
		{
		message = "You have already submitted the form\n";
		message = message + "Please wait for the next page to load.";
		alert(message);
		result = false;
		}
	return result;
	}

/* This function resets the submission handler variable
   (used by the function above). This can be used if a
   request to cancel an activity is overridden.
*/
function ResetSingleSubmission()
	{
	checkSubmissionCueVariable = false;
	}



/* This function resets the form to blank values. It should be used with
 the onReset event handler of the form 
*/

function ClearForm(objForm)
	{
	with (objForm)
		{
		for (var i = 0; i < elements.length; i++) 
			{
			switch (elements[i].type)
				{
				case "radio":
					
					var strEleName = elements[i].name;					
					var GroupLength = elements[i].form.elements[strEleName].length;
					if (GroupLength) //if it is a group of radio buttons, select first.
						elements[i].form.elements[strEleName][0].checked = true;
					else
						elements[i].checked = false;
                	break;
				case "text":
					elements[i].value = "";
					break;
				case "checkbox":
					elements[i].checked = false;
					break;
				case "textarea":
					elements[i].value = " ";
					break;
				case "select-one":
					elements[i].selectedIndex = 0;
					break;
				case "select-multiple":
					elements[i].selectedIndex = -1;
					break;
				case "password":
					elements[i].value = "";
					break;
				}
			}
		}
	return false;
	}

/* Counts the number of checked items in a form by looking for checked checkboxes.
 */
function CountChecked(theForm)
    {
    var count;
    count = 0;

    var i;
    for (i = 0; i < theForm.length; ++i)
        {
        if (theForm.elements[i].type == "checkbox" && theForm.elements[i].checked)
            ++count;
        }
    return count;
    }

function DependencyCheck()
	{
	return true;
	}
	
function FindFormElement (theForm, inputName)
	{
	var input = null;
	for (var i = 0; i < theForm.elements.length; I++)
		{
		if (theForm.elements[i].name == inputName)
			input = theForm.elements[i];
		}
	return input;
	}

function Numeric(strDescr)
	{
	return true;
	}

/* This is a dummy function that is used to indicate that a field is mandatory.
	Its presence in the onChange handler of an <INPUT> tag indicates to the ValidateForm function
	that the input is mandatory.
	"strDescription" is used to describe the input field to the user if it is blank when the form is submitted.
*/

function Mandatory(strDescr)
	{
	return true;
	}

/*	This function is used to set the value shown in a text field from a popup menu selector
	name 	is the name of the target text box
	element is the popup menu selector itself
	form 	is the form that contaisn the input
 */
function SetTextFromSelector(name,element,form)
	{
	var selected = element.selectedIndex;
	var value = element.options[selected].value;
	for (var i = 0; i < form.elements.length; i++)
		{
		if (form.elements[i].name == name) 
			{
			form.elements[i].value = value;
			break;
			}
		}
	}

/* 	This function checks "objForm" to make sure that the form is valid. It checks for the presence of 
	Date fields, time fields and mandatory fields. 
	If the form is invalid an alert appears informing the user which fields were invalid and the reason.
*/

function ValidateForm(objForm)
	{
	var IsValid = true;
	var boolFormatError = false;
	var boolMandError = false;
	var boolNumericError = false;
	var boolDepBlankError = false;
	var boolDepNotBlankError = false;
	var boolDepValueError = false;
	var strDescription = "";
	var strOnChangeTxt = "";
	var strFormatAlert = "The following items are not in the valid format:";
	var strMandAlert = "The following mandatory items have not been completed:";
	var strNumericAlert = "The following items must only contain numeric characters:";
	var strDepBlankAlert = "The following items must not be blank:";
	var strDepNotBlankAlert = "The following items must be filled in:"; 
	var strDepValueAlert = "The following items must be filled in:";
	
	with (objForm)
		{
		for (var i = 0; i < elements.length; i++) 
			{
			strDescription = "";
			strOnChangeTxt = "";
			if (elements[i].type == "text" || elements[i].type == "textarea" || elements[i].type == "password")
				{
				if (elements[i].onchange)
					{
					strOnChangeTxt = elements[i].onchange.toString();
					if (strOnChangeTxt.indexOf("ValidateAmbigStartDate") != -1)
						{
						if (!ValidateAmbigStartDate(elements[i],false,strDescription))
							{
							strDescription = GetLastArg("ValidateAmbigStartDate",strOnChangeTxt);
							boolFormatError = true;
							strFormatAlert += "\n     - " + strDescription;
							IsValid = false;
							}
						}
					if (strOnChangeTxt.indexOf("ValidateAmbigEndDate") != -1)
						{
						if (!ValidateAmbigEndDate(elements[i],false,strDescription))
							{
							strDescription = GetLastArg("ValidateAmbigEndDate",strOnChangeTxt);
							boolFormatError = true;
							strFormatAlert += "\n     - " + strDescription;
							IsValid = false;
							}
						}
					if (strOnChangeTxt.indexOf("ValidateDate") != -1)
						{
						if (!ValidateDate(elements[i],false,strDescription))
							{
							strDescription = GetLastArg("ValidateDate",strOnChangeTxt);
							boolFormatError = true;
							strFormatAlert += "\n     - " + strDescription;
							IsValid = false;
							}
						}
					if (strOnChangeTxt.indexOf("ValidateTime") != -1)
						{
						if (!ValidateTime(elements[i],false,strDescription))
							{
							if (strDescription == "")
								strDescription = GetLastArg("ValidateTime",strOnChangeTxt);
							boolFormatError = true;
							strFormatAlert += "\n     - " + strDescription;
							IsValid = false;
							}
						}
					if (strOnChangeTxt.indexOf("Mandatory") != -1)
						{
						if (strDescription == "")
							strDescription = GetLastArg("Mandatory",strOnChangeTxt);
						if (IsEmpty(elements[i].value))
							{
							boolMandError = true;
							strMandAlert += "\n     - " + strDescription;
							IsValid = false;
							}
						}
					if (strOnChangeTxt.indexOf("Numeric") != -1 && strOnChangeTxt.indexOf("ValidateTime") == -1 && strOnChangeTxt.indexOf("ValidateDate") == -1)
						{
						if (strDescription == "")
							strDescription = GetLastArg("Numeric",strOnChangeTxt);
						var inputValue = elements[i].value;
						var theRegExp = /[^\d]/i;
						if (StringTrim(inputValue, " ") != "")
							{
							if (theRegExp.test(inputValue))
								{
								boolNumericError = true;
								strNumericAlert += "\n     - " + strDescription;
								IsValid = false;
								}
							}
						}
					if (strOnChangeTxt.indexOf("DependencyCheck") != -1)
						{
						var arguments = GetFunctionArgs("DependencyCheck", strOnChangeTxt).split('|');
						var checkType = StringTrim(StringTrim(arguments[0], " "), "'");
						checkType = checkType.toLowerCase();
						var formElement = StringTrim(StringTrim(arguments[1], " "), "'");
						var depValue = StringTrim(StringTrim(arguments[2], " "), "'");
						var strDescription = StringTrim(StringTrim(arguments[3], " "), "'");
						var theElement = GetFirstFormElement(objForm, formElement);
						var theElementValues = GetElementValue(theElement);
						if (checkType == "blank" || checkType == "\"blank\"")
							{
							if (theElementValues.length == 0 && StringTrim(elements[i].value, " ") == "")
								{
								boolDepBlankError = true;
								strDepBlankAlert += "\n     - " + strDescription;
								IsValid = false;
								}
							}
						else if (checkType == "notblank" || checkType == "\"notblank\"")
							{
							if (theElementValues.length != 0 && StringTrim(elements[i].value, " ") == "")
								{
								boolDepNotBlankError = true;
								strDepNotBlankAlert += "\n     - " + strDescription;
								IsValid = false;
								}
							}
						else if (checkType == "othervalue" || checkType == "\"othervalue\"")
							{
							var otherValueSet = false;
							for (var i = 0; i < theElementValues.length; i++)
								{
								depValue = StringTrim(depValue, "\"");
								if (theElementValues[i] == depValue)
									{
									otherValueSet = true;
									break;
									}
								}
							if (otherValueSet && StringTrim(elements[i].value, " ") == "")
								{
								boolDepValueError = true;
								strDepValueAlert += "\n     - " + strDescription;
								IsValid = false;
								}
							}
						}
					}
				}
			}
		}
		
		if (!IsValid)
			{
			var theAlert = "";
			if (boolNumericError)
				theAlert += "\n\n" + strNumericAlert;
			if (boolMandError)
				theAlert += "\n\n" + strMandAlert;
			if (boolFormatError)
				theAlert += "\n\n" + strFormatAlert;
			if (boolDepBlankError)
				theAlert += "\n\n" + strDepBlankAlert;
			if (boolDepNotBlankError)
				theAlert += "\n\n" + strDepNotBlankAlert;
			if (boolDepValueError)
				theAlert += "\n\n" + strDepValueAlert;
			alert(StringTrim(theAlert, "\n"));
			}
			
		return IsValid
	}

function GetFirstFormElement(objForm, formElement)
	{
	var theElement = "undefined"
	with (objForm)
		{
		for (var i = 0; i < elements.length; i++) 
			{
			if (elements[i].name == formElement)
				{
				theElement = elements[i];
				}
			}
		}
		return theElement;
	}
	
function GetElementValue(theElement)
	{
	var value = new Array();
	switch (theElement.type)
		{
		case "radio":
			var strEleName = theElement.name;					
			eval("var group = theElement.form." + strEleName);
      		if (group.length)
      			{
	      		for (var i = 0; i < group.length; i++)
					{
					if (group[i].checked)
						{
						if (StringTrim(group[i].value, " ") != "")
							value[value.length] = group[i].value;
						}
					}
				}
			else
				{
				if (StringTrim(theElement.value, " ") != "")
					value[value.length] = theElement.value;
				}
      		break;
		case "text":
			if (StringTrim(theElement.value, " ") != "")
				value[value.length] = theElement.value;
			break;
		case "checkbox":
			if (theElement.checked)
				{
				value[value.length] = theElement.value;
				}
			break;
		case "textarea":
			if (StringTrim(theElement.value, " ") != "")
				value[value.length] = theElement.value;
			break;
		case "select-one":
			if (StringTrim(theElement.value, " ") != "")
				value[value.length] = theElement.options[theElement.selectedIndex].value;
			break;
		case "select-multiple":
			for (var i = 0; i < theElement.options.length; i++)
				{
				if (theElement.options[i].selected)
					{
					if (StringTrim(theElement.value, " ") != "")
						value[value.length] = theElement.options[i].value;
					}
				}
			break;
		case "password":
			if (StringTrim(theElement.value, " ") != "")
				value[value.length] = theElement.value;
			break;
		}
		return value;
	}

/*
This function updates the counter with ID "strID" so it displays the correct number (as images)
*/

function UpdateCounter(intQuantity,strID,strScriptName)
    {
    var units;
    var tens;
    var hundreds;
    var thousands;
    
    var tmp;
    
    var counterA = eval("self.document." + strID + "A");
    var counterB = eval("self.document." + strID + "B");
    var counterC = eval("self.document." + strID + "C");
    var counterD = eval("self.document." + strID + "D");


    tmp = intQuantity;
    
    units = tmp % 10;
    tmp = (tmp - units) / 10;
    tens = tmp % 10;
    tmp = (tmp - tens) / 10;
    hundreds = tmp % 10;
    tmp = (tmp - hundreds) / 10;
    thousands = tmp;

    if (intQuantity >= 0 && intQuantity < 10)
        {
        counterD.src='/' + strScriptName + '/images/spacer.gif';
        counterC.src='/' + strScriptName + '/images/spacer.gif';
        counterB.src='/' + strScriptName + '/images/spacer.gif';
        counterA.src='/' + strScriptName + '/images/' + units + '.gif';
        }

    if (intQuantity >= 10 && intQuantity < 100)
        {
        counterD.src='/' + strScriptName + '/images/spacer.gif';
        counterC.src='/' + strScriptName + '/images/spacer.gif';
        counterB.src='/' + strScriptName + '/images/' + units + '.gif';
        counterA.src='/' + strScriptName + '/images/' + tens + '.gif';
        }

    if (intQuantity >= 100 && intQuantity < 1000)
        {
        counterD.src='/' + strScriptName + '/images/spacer.gif';
        counterC.src='/' + strScriptName + '/images/' + units + '.gif';
        counterB.src='/' + strScriptName + '/images/' + tens + '.gif';
        counterA.src='/' + strScriptName + '/images/' + hundreds + '.gif';
        }
    
    if (intQuantity >= 1000 && intQuantity < 10000)
        {
        counterD.src='/' + strScriptName + '/images/' + units + '.gif';
        counterC.src='/' + strScriptName + '/images/' + tens + '.gif';
        counterB.src='/' + strScriptName + '/images/' + hundreds + '.gif';
        counterA.src='/' + strScriptName + '/images/' + thousands + '.gif';
        }
    
    if (intQuantity >= 10000)
        {
        counterD.src='/' + strScriptName + '/images/x.gif';
        counterC.src='/' + strScriptName + '/images/x.gif';
        counterB.src='/' + strScriptName + '/images/x.gif';
        counterA.src='/' + strScriptName + '/images/x.gif';
        }
    }