<!--

/*
Copyright © Tikit Limited 2004. All rights reserved.
*/

var XVAL_formInputChecks = Array();
var XVAL_formDoInputChecks = true;

//////////////////////////////////////////////////////////////////////////

function XVAL_createTemplate()
{
	if (!confirm("Are you sure you want to save as a templete?", ""))
		{			
			
			event.returnValue = false;
		}
	else
	{
		//XVAL_formDoInputChecks = true;
		formMain.action="../admin_TemplateSave.asp";
		//formMain.method="post";			
		//formMain.submit();
			
		}
		
}

//////////////////////////////////////////////////////////////////////////





//////////////////////////////////////////////////////////////////////////

function XVAL_formSave()
{
	XVAL_formDoInputChecks = true;
}

//////////////////////////////////////////////////////////////////////////

function XVAL_formReset()
{
	XVAL_formHideErrors();
	XVAL_formDoInputChecks = true;
}

//////////////////////////////////////////////////////////////////////////

function XVAL_formDelete()
{
	if (!confirm("Are you sure you want to delete this?", ""))
		event.returnValue = false;
	else
		XVAL_formDoInputChecks = false;
}

//////////////////////////////////////////////////////////////////////////

function XVAL_formAttach(formName, formInputChecks)
{
	var form;

	//Set event handler for form submit event
	form = document.forms[formName];
	if (form == null)
	{
		alert("XVAL_formAttach Error: Form '" + formName + "' not found.")
		return false;
	}
	//form.onsubmit = XVAL_formSubmit;

	//Assign array of input checks to global array of input checks
	XVAL_formInputChecks[formName] = formInputChecks;
	
	return true;
}

//////////////////////////////////////////////////////////////////////////

function XVAL_formSubmit()
{
	var i;
	var form, inputChecks, input, inputName, inputValue;
	var errors;

	event.returnValue = false;

	if (XVAL_formDoInputChecks)
	{

		errors = "";
		form = event.srcElement;


		//Get the array of input checks for this form
		var inputChecks = XVAL_formInputChecks[form.name];
		if (inputChecks == null)
		{
			alert("XVAL_formSubmit Error: No input checks found for form with name '" + form.name + "'.")
			return false;
		}
		
		for (i = 0; i < inputChecks.length; i++)
		{
			//Get the input element value from the form
			inputName = inputChecks[i][0];
			if (inputName == null)
				inputValue = null;
			else
			{
				input = form[inputName];
				if (input == null)
				{
					alert("XVAL_formSubmit Error: Form element with name '" + inputName + "' not found.")
					return false;
				}
				inputValue = input.value;
			}

			//Check the input value
			if (!XVAL_formInputCheck(inputChecks[i][1], inputValue, inputChecks[i][2], inputChecks[i][3], inputChecks[i][4]))
			{
				errors = errors + "<li>" + inputChecks[i][5];
			}
		}

		//If there are any errors then show them
		if (errors != "")
		{
			XVAL_formShowErrors(errors);
			return false;
		}
	}

	event.returnValue = true;
	return true;
}

//////////////////////////////////////////////////////////////////////////

function XVAL_formInputCheck(type, value, isRequired, valueMin, valueMax)
{
//alert (isRequired);
//alert (eval(isRequired));

	//Check value is required
	if ((value == null || value == "") && eval(isRequired))
		return false;
	
	//Check value is valid
	if (type == null)
	{
		//nothing
	}
	else if (type == "char")
	{
		if (value != "" && !isAlpha(value))
			return false;
	}
	else if (type == "date")
	{
		if (value != "" && !isDate(value))
			return false;
	}
	else if (type == "partialdate")
	{
		if (value != "" && !isPartialDate(value))
			return false;
	}
	else if (type == "datetime")
	{
		if (value != "" && !isDateTime(value))
			return false;
	}
	else if (type == "float")
	{
		if (value != "" && !isFloatInRange(value, valueMin, valueMax))
			return false;
	}
	else if (type == "int")
	{
		if (value != "" && !isIntegerInRange(value, valueMin, valueMax))
			return false;
	}
	else if (type == "text")
	{
		if (value != "" && !isTextInRange(value, valueMin, valueMax))
			return false;
	}
	else
	{
		alert("XVAL_formInputCheck Error: Unknown input type '" + type + "'.")
		return false;
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////

function XVAL_formShowErrors(errors)
{
	var div;
	
	div = XVAL_errors;
	if (div == null)
	{
		alert("XVAL_formShowErrors Error: Error tag with name '" + divName + "' not found.")
		return false;
	}
	
	div.innerHTML = "<br>"
		+ "<table border=1 bordercolor=red cellspacing=0 cellpadding=4 width=500>\n"
		+ "<tr>\n"
		+ "<td><font color=red>This page could not be saved due to missing/invalid data.  Please edit the following:<br><br>\n"
		+ "<ul>"
		+ errors + "\n"
		+ "</ul></font></td>\n"
		+ "</tr>\n"
		+ "</table>\n";
	div.style.display = "block";
	div.scrollIntoView(true);

	return true;	
}

//////////////////////////////////////////////////////////////////////////

function XVAL_formHideErrors()
{
	var div;
	
	div = XVAL_errors;
	if (div == null)
	{
		alert("XVAL_formShowErrors Error: Error tag with name '" + divName + "' not found.")
		return false;
	}
	
	div.innerHTML = "";
	div.style.display = "none";

	return true;
}

//////////////////////////////////////////////////////////////////////////

//-->

