//Form Validation Functions
//(C) 2005 Propus Software Ltd - All Rights Reserved

var sErrorMsg;		//The Error message for the form as a whole
var nErrorNumber;	//The current error number (increments with each invalid field)
var bCancelled;		//If True, then the form has been cancelled
var target;				//The control currently with the focus

//======================================================================================
//Field validation functions
function validateString(c, fieldName, bMandatory, nTab)
{
	if (bMandatory && c.value == '') { showError(c, fieldName + " must be entered", nTab); }
	else														{ hideError(c, bMandatory); }
}

function validateFile(c, fieldName, bMandatory, nTab)
{
	if (bMandatory && c.value == '') { showError(c, fieldName + " must be entered", nTab); }
	else														{ hideError(c, bMandatory); }
}

function validateNumber(c, fieldName, bMandatory, nTab, isInteger, min, max)
{
	if (bMandatory && c.value == '')   																				{ showError(c, fieldName + " must be entered", nTab); }
	else if (isInteger && c.value != parseInt(c.value, 10) && c.value != '')	{ showError(c, fieldName + " must be a whole number", nTab); } 
	else if (parseInt(c.value, 10) < min || parseInt(c.value, 10) > max)   		{ showError(c, fieldName + " must be between " + min + " and " + max, nTab); } 
	else    																																	{ hideError(c, bMandatory); }
 }

function validateDate(c, fieldName, bMandatory, nTab, showTime)
{
	if (bMandatory && c.value == '')												{ showError(c, fieldName + " must be entered", nTab); }
	else if (!(Date.parse(c.value) > 0) && c.value != '')	{ showError(c, fieldName + " must be a valid date", nTab); } 
	else    																							{ hideError(c, bMandatory); }
}

function validateList(c, fieldName, bMandatory, nTab)
{
	if (bMandatory && (c.selectedIndex == 0 || c.value == "")) { showError(c, fieldName + " must be entered", nTab); }
	else																									  	{ hideError(c, bMandatory); }
}

function validateCombo(c, textBox, fieldName, bMandatory, nTab)
{
	if (bMandatory && c.selectedIndex == 0)																							  { showError(c, fieldName + " must be entered", nTab); }
	else if (bMandatory && c.selectedIndex == c.options.length - 1 && textBox.value == '') { showError(c, fieldName + " must be entered", nTab, textBox); }
	else																																								  { hideError(c, bMandatory); }
}

function showOtherValue(sID, nWidth)
{
	//Show the 'Other Value' text box if necessary
	lst = (document.getElementById(sID));
	txt = (document.getElementById(sID + "Text"));
  if (lst.selectedIndex == lst.options.length - 1)
	{
    txt.style.display = "inline";
    lst.style.width = "80px";
	}
  else
	{
    txt.style.display = "none";
    lst.style.width = nWidth + "px";
	}
}

//======================================================================================
//Key Filters
function getKeyCode(e)
{
	//Get the ASCII key that was pressed
	if (!e) { var e = window.event; }
  if (e.target)					{ target = e.target; }
	else if (e.srcElement) { target = e.srcElement; }
  
	if (e.keyCode)		{ keyCode = e.keyCode; }
	else if (e.which)	{ keyCode = e.which; }
	else							{ keyCode = 0; }

	return keyCode;
}

function processKey(e, form)
{
	//Filter the key for the form as a whole
	nKeyCode = getKeyCode(e);

	if (form.cancelURL == null) { sCancelURL = "" }
	else												{ sCancelURL = form.cancelURL.value }

  if (nKeyCode == 13 && target.type != "textarea")
	{
		//Pressing enter on any control except a text box should always submit the form
		if (e.preventDefault) { e.preventDefault(); }
		else									{ e.keyCode = 0; }
		eval("submitForm_" + form.formName.value + "(form, 0, 1)");
  }
	else if (sCancelURL != '' && nKeyCode == 27)
	{ 
		//Pressing cancel on any control should cancel the form
		window.location = sCancelURL;
    if (e.preventDefault) { e.preventDefault(); }
  }
}

function processNumberKey(e, showNegative, showDecimal)
{
	//Filter the key for a numeric field
	nKeyCode = getKeyCode(e);

	//if (nKeyCode == 13)											{}
  //else if (showDecimal && keyCode == 46)	{}
  //else if (showNegative && keyCode == 45) {}
  //else if (keyCode < 48 || keyCode > 57)	{ window.event.keyCode = 0; }
}

function processDateKey(e, showTime)
{
	//Filter the key for a date field
	nKeyCode = getKeyCode(e);

  if (nKeyCode == 32)
  {
    //Pressing Space will attempt to fill in the date automatically
		now = new Date;

		//Determine the date parts we may want to add
	  sMinute = now.getMinutes(); 
	  sHour = now.getHours() + ":"; 
	  sYear = now.getFullYear() + " "; 
	  sMonth = (now.getMonth() + 1) + "/"; 
	  sDay = (now.getDate() + 1) + "/"; 

	  //Ensure leading zeroes are added
		if (now.getMinutes() < 9) { sMinute = "0" + sMinute; }
		if (now.getHours() < 9)		{ sHour = "0" + sHour; }
		if (now.getMonth() < 9)		{ sMonth = "0" + sMonth; }
		if (now.getDate() < 9)		{ sDay = "0" + sDay; }

    value = target.value;

		//Patterns to match for
    var sMinutePattern1 = /\d+\/\d+\/\d+\s\d+:/;
    var sMinutePattern  = /\d+\/\d+\/\d+\s\d+/;
    var sHourPattern1 = /\d+\/\d+\/\d+\s/;
    var sHourPattern  = /\d+\/\d+\/\d+/;
    var sYearPattern1 = /\d+\/\d+/;
    var sYearPattern  = /\d+\/\d+\//;
    var sMonthPattern1 = /\d+/;
    var sMonthPattern  = /\d+\//;

		var sNewText = "";

		if (sMinutePattern1.test(value))			{ sNewText = value + sMinute; }
		else if (sMinutePattern.test(value))	{ sNewText = value + ":" + sMinute; }
		else if (sHourPattern1.test(value))		{ sNewText = value + sHour; }
		else if (sHourPattern.test(value))		{ sNewText = value + " " + sHour; }
		else if (sYearPattern.test(value))		{ sNewText = value + sYear; }
		else if (sYearPattern1.test(value))   { sNewText = value + "/" + sYear; }
		else if (sMonthPattern.test(value))		{ sNewText = value + sMonth; }
		else if (sMonthPattern1.test(value))	{ sNewText = value + "/" + sMonth; }
		else if (value == "")									{ sNewText = sDay; }

		if (sNewText != "")		
		{
			//Set the new text 
			target.value = sNewText;

			//Cancel the old event			
			if (e.preventDefault) { e.preventDefault(); }
			else									{ e.keyCode = 0; }
		}
  }
}

//======================================================================================
//Other Functions
function cursorWait()	{	document.body.style.cursor="Wait"; }

function disableButtons(nCount)
{
	//Disable the buttons at the bottom of the form
	for (var i = 1; i <= nCount; i++)
	{	
		var div = document.getElementById("divButton" + i);
		if (div)
		{
  		div.style.color='GrayText';
  		div.disabled='disabled';
		}
	}
    
	cursorWait(target);
}

function showError(c, msg, nTab, otherControl)
{

	//Show an error message next to the field
	nErrorNumber++;

	var div = document.getElementById('divErr' + c.name);
	if (div)
	{
		div.title = msg;
		div.innerHTML = nErrorNumber;
		div.className = "error";
		sErrorMsg += nErrorNumber + " - " + msg + "<br />";
	}

	//If this is the first control, then give it the focus
	if (nErrorNumber == 1) 
	{ 
		if (nTab >= 0)
		{		
			showTab(nTab, 10);	//Don't know how many tabs there will be
		}
		setFocus(c); 
	}
}

function hideError(c, bMandatory)
{
	//Hide the error next to the field
	var div = document.getElementById('divErr' + c.name);
	if (div)
	{
		if (bMandatory)
		{
		div.className = "mandatory";
		div.title = "This field must be entered";
		div.innerHTML = "*";
		}
		else
		{
		div.className = "";
		div.title = "";
		div.innerHTML = "&nbsp;";
		}
  }
}

function setFocus(c)
{
	//Select and highlight the given field
	try
  { 
    c.focus(); 
    c.select(); 
  }
	catch(e) { }
}

function linkedLists(lstParent, lstChild, fields)
{
  sFilter = lstParent.options[lstParent.selectedIndex].text;
	
  for(i = lstChild.options.length-1; i >= 0; i--)
  {
    lstChild.options[i] = null;
  }

	//Recreate with fields for this category
  var row = 0;
  for(i = 0; i < fields.length; i++)
  {
    if (fields[i][2] == sFilter)
    {
      lstChild.options[row] = new Option(fields[i][1]);
      lstChild.options[row].value = fields[i][0];
      row++;
    }
  }
}

