//Form functions
var showErrorText;

function submitForm(e, form, _showErrorText, submitted)
  {
	showErrorText = _showErrorText;
	if (!e) var e = window.event;
  if (e.target) target = e.target;
	else if (e.srcElement) target = e.srcElement;

  errorCount = 0;
	formError = '';
	validate(form)

  form.submitted.value = submitted;

  if (errorCount == 1)
  	{ 
		var div = document.getElementById("DivStatus");
	  if (div)
    	{
      div.innerHTML = formError; 
      div.style.display = 'block';
			}
    }
  else if (errorCount > 0)
  	{ 
		var div = document.getElementById("DivStatus");
		if (showErrorText)
			{ s = 'The highlighted fields are invalid.'; }
    else
			{ s = 'The highlighted fields are invalid.  Hover over an arrow for more information.'; }

	  if (div)
    	{
      div.innerHTML = s ;
      div.style.display = 'block';
			}
    }
	else
  	{
		var div = document.getElementById("DivButton1");
		if (div)
    	{
  		div.style.color='GrayText';
  		div.disabled='disabled';
			}
      
		var div = document.getElementById("DivButton2");
		if (div)
    	{
  		div.style.color='GrayText';
  		div.disabled='disabled';
			}

		cursorWait(target);
		form.submit();
  	}
  }

function cancelForm(e, form)
{
	if (!e) var e = window.event;
  if (e.target) target = e.target;
	else if (e.srcElement) target = e.srcElement;

  var div = document.getElementById("DivButton1");
  if (div)
    	{
  		div.style.color='GrayText';
  		div.disabled='disabled';
  	}
      
  var div = document.getElementById("DivButton2");
  if (div)
    	{
  		div.style.color='GrayText';
  		div.disabled='disabled';
  	}
  

  cursorWait(target);
  document.location=form.CancelURL.value;
}

function cursorWait(target)
  {
  
  	target.style.cursor='Wait';
  	document.body.style.cursor="Wait";
  }

function showError(c, msg, otherControl)
	{
	s ='<IMG ALIGN="TOP" STYLE="cursor: help;" SRC="/media/image/icon/error.gif" TITLE="' + msg + '"> ';
  if (showErrorText)
  	{ s = s + msg; }
	document.getElementById('Err' + c.name).innerHTML=s;
	document.getElementById('Err' + c.name).style.display='inline';

	//If this is the first control, then give it the focus
	if (errorCount == 0) 
  	{ 
    if (otherControl)
      { setFocus(otherControl); } 
		else
      { setFocus(c); } 
		formError=msg;
    }

	errorCount += 1;
  }

function hideError(c)
	{
	document.getElementById('Err' + c.name).style.display='none';
  }

function setFocus(c)
  {
	try
  	{ 
    c.focus(); 
    c.select(); 
    }
	catch(e)
  	{ }
  }

function showOtherValue(listBox, textBox)
	{
  if (listBox.selectedIndex == listBox.options.length - 1)
		{
    textBox.style.display='inline';
    listBox.style.width='80px';
		}
  else
		{
    textBox.style.display='none';
    listBox.style.width=(parseInt(textBox.style.width,10)+90) + 'px';
		}
  }

//Key filters
function processKey(e, form, _showErrorText)
	{
  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;
  
  if (keyCode == 13 && target.type != "textarea")
		{
		submitForm(e, form, _showErrorText);
    }
	else if (form.CancelURL.value != '' && keyCode == 27)
		{ 
    cancelForm(e, form);
    if (e.preventDefault) 
      { e.preventDefault(); }
    }
	}

function processNumberKey(e, showNegative, showDecimal)
	{
	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;

  //if (keyCode == 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)
	{
	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;

  if (keyCode == 32)
    {
    now = new Date;
    value = target.value;

    var regComplete = /^\w+(\/)\w+(\/)\w+ \w+:\w/;  
    var regMinute = /^\w+(\/)\w+(\/)\w+ \w+/;  
    var regHour = /^\w+(\/)\w+(\/)\w+/;  
    var regYear = /^\w+(\/)\w+/;  
    var regMonth = /^\w+/;
    var regNoSlash = /[^(\/)]$/;								
    var regNoSpace = /[^( )]$/;								
    var regNoColon = /[^(:)]$/;								

  	sText = value;

		if (regComplete.test(value)) {}
		else if (regMinute.test(value))
    	{
			if (showTime)
        {
      	sNewText = (now.getMinutes());
  	    if (now.getMinutes() < 10) { sNewText = '0' + sNewText; }
  	    if (regNoColon.test(value)) { sNewText = ':' + sNewText; }
        sText = value + sNewText;
        }
			}
		else if (regHour.test(value))
    	{
			if (showTime)
        {
      	sNewText = (now.getHours()) + ':';
  	    if (now.getHours() < 10) { sNewText = '0' + sNewText; }
  	    if (regNoSpace.test(value)) { sNewText = ' ' + sNewText; }
        sText = value + sNewText;
        }
			}
		else if (regYear.test(value))
    	{
      //Nothing entered yet - add the year 
    	sNewText = (now.getFullYear());
	    if (regNoSlash.test(value)) { sNewText = '/' + sNewText; }
	    if (showTime) { sNewText = sNewText + ' '; }
      sText = value + sNewText;
      }
		else if (regMonth.test(value))
    	{
      //Nothing entered yet - add the month 
    	sNewText = (now.getMonth() + 1) + '/';
	    if (now.getMonth() < 9) { sNewText = '0' + sNewText; }
	    if (regNoSlash.test(value)) { sNewText = '/' + sNewText; }
      sText = value + sNewText;
      }
		else
    	{
      //Nothing entered yet - add the day
    	sNewText = (now.getDate()) + '/';
	    if (now.getDate() < 9) { sNewText = '0' + sNewText; }
      sText = sNewText;
      }

		target.value = sText;
    if (e.preventDefault) 
      { e.preventDefault(); }
		else
    	{ e.keyCode = 0; }

    //keyCode = 0;
    }
	}

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

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

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

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

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

