
var checkSubmissionCueVariable = false;


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;
	}


function ResetSingleSubmission()
	{
	checkSubmissionCueVariable = false;
	}



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;
	}



function Mandatory(strDescr)
	{
	return true;
	}


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;
			}
		}
	}



function AddAlert(currentAlert, newAlert)
	{
	if (currentAlert.search(newAlert) == -1)
		currentAlert += "\n     - " + newAlert
	return currentAlert
	}

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 informations have not been completed, please correct :";
	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 = AddAlert(strFormatAlert, strDescription);
							IsValid = false;
							}
						}
					if (strOnChangeTxt.indexOf("ValidateAmbigEndDate") != -1)
						{
						if (!ValidateAmbigEndDate(elements[i],false,strDescription))
							{
							strDescription = GetLastArg("ValidateAmbigEndDate",strOnChangeTxt);
							boolFormatError = true;
							strFormatAlert = AddAlert(strFormatAlert, strDescription);
							IsValid = false;
							}
						}
					if (strOnChangeTxt.indexOf("ValidateDate") != -1)
						{
						if (!ValidateDate(elements[i],false,strDescription))
							{
							strDescription = GetLastArg("ValidateDate",strOnChangeTxt);
							boolFormatError = true;
							strFormatAlert = AddAlert(strFormatAlert, strDescription);
							IsValid = false;
							}
						}
					if (strOnChangeTxt.indexOf("ValidateTime") != -1)
						{
						if (!ValidateTime(elements[i],false,strDescription))
							{
							if (strDescription == "")
								strDescription = GetLastArg("ValidateTime",strOnChangeTxt);
							boolFormatError = true;
							strFormatAlert = AddAlert(strFormatAlert, strDescription);
							IsValid = false;
							}
						}
					if (strOnChangeTxt.indexOf("Mandatory") != -1)
						{
						if (strDescription == "")
							strDescription = GetLastArg("Mandatory",strOnChangeTxt);
						if (IsEmpty(elements[i].value))
							{
							boolMandError = true;
							strMandAlert = AddAlert(strMandAlert, 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 = AddAlert(strNumericAlert, 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 = AddAlert(strDepBlankAlert, strDescription);
								IsValid = false;
								}
							}
						else if (checkType == "notblank" || checkType == "\"notblank\"")
							{
							if (theElementValues.length != 0 && StringTrim(elements[i].value, " ") == "")
								{
								boolDepNotBlankError = true;
								strDepNotBlankAlert = AddAlert(strDepNotBlankAlert, 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 = AddAlert(strDepValueAlert, 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;
	}


function ContainsValue(string,value)
	{
	var isIn = false;
	var tmpStr = "";
	
	for (j = 0; !isIn && j < string.length; ++j)
		{
		if (string.charAt(j) != "\r" && string.charAt(j) != "\n" && j != string.length - 1)
			tmpStr += string.charAt(j);
		else
			{
			if (j == string.length - 1)
				tmpStr += string.charAt(j);
			if (value == tmpStr)
				isIn = true;
			tmpStr = "";
			}
		}
	
	return isIn;
	}


function GetFunctionArgs(strFunc,strSource)
	{
	var results = new Array();
	var result = "-1";
	var argString = "";
	
	/*	First, match the fucntion anme plus the opening (. We read into argString everything
		found after this string
	 */
	var myRegExp = eval("/" + strFunc + "\\((.*)/i");
	myRegExp.multiline = true;
	if (myRegExp.test(strSource))
		{
		results = strSource.match(myRegExp);
		argString = results[1];
		}
	

	var i;
	var strLen = argString.length;
	var inQuotes = false;
	var bracketCount = 1;
	var c;
	var resultStr = "";
	
	for (i = 0; i < strLen; ++i)
		{
		c = argString.charAt(i)
		if (c == "'")
			{
			if (inQuotes)
				inQuotes = false;
			else
				inQuotes = true;
			}
		
		if (c == "(" && !inQuotes)
			++bracketCount;
		
		if (c == ")" && !inQuotes)
			--bracketCount;
		
		if (bracketCount == 0)
			break;
		
		if (!inQuotes && c == ",")
			c = "|";
		if (inQuotes && c == "|")
			c = " ";
		
		resultStr += c;
		}
	
	if (resultStr != "")
		result = resultStr;
	
	return result
	}
	

function GetLastArg(strFunc,strSource)
	{
	strArgs = GetFunctionArgs(strFunc,strSource);
	var results = strArgs.split('|');
	result = results[results.length - 1];
	
	result = StringTrim(result," ");
	if (result.charAt(0) == "'" && result.charAt(result.length - 1) == "'")
		result = result.substring(1,result.length - 1);
	else if (result.charAt(0) == '\"' && result.charAt(result.length - 1) == '\"')
		result = result.substring(1,result.length - 1);
	return result;
	}



function IsEmpty(strValue)
	{
	var MyRegExp = /^\s*$/; //Check for entirely white space.
	return MyRegExp.test(strValue);
	}
	

function StringTrimLeft(strValue,strChar)
	{
	var result = strValue;
	while (result.charAt(0) == strChar)
		{
		result = result.substring(1,result.length);
		}
	return result;
	}
	


function StringTrimRight(strValue,strChar)
	{
	var result = strValue;
	while (result.charAt(result.length - 1) == strChar)
		{
		result = result.substring(0,result.length - 1);
		}
	return result;
	}




function StringTrim(strValue,strChar)
	{
	strValue = StringTrimLeft(strValue,strChar);
	strValue = StringTrimRight(strValue,strChar);
	return strValue;
	}
	


function StringPadLeft(strValue,strChar,intMaxLength)
	{
	if (strChar!="")
		{
		while (strValue.length < intMaxLength)
			strValue = strChar + strValue;
		}
	return strValue
	}



function StringPadRight(strValue,strChar,intMaxLength)
	{
	if (strChar!="")
		{
		while (strValue.length < intMaxLength)
			strValue = strValue + strChar;
		}
	return strValue
}
