// Form Field Validation Functions:
// validForm() returns true or false depending on if there's text in given inputs

function validRequired(formField,fieldLabel)
{
	var result = true;

	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}

	return result;
}

function allDigits(formField)
{
	if( inValidCharSet(formField.value,"0123456789.") )
	{
		return true;
	}
	else
	{
		alert('Field must contain only numbers. e.g. 00.00');
		formField.focus();
		return false;
	}
}

/* returns false if str contains any chars from inValidcharset */
function inValidCharSet(str,validcharset)
{
	decimal_used = false;
	
	for (var i=0;i<str.length;i++)
	{
		if (validcharset.indexOf(str.substr(i,1)) < 0)
		{
			return false;
		}
		
		if( str.substr(i,1) == '.')
			decimal_used = true;
	}
	
	// ensure the decimal is correctly placed.
	if( decimal_used )
	{
		if( str.substr(str.length-3,1) != '.')
			return false;
	}
	
	return true;
}

function maxLength(formField,inputLength)
{
	var inputString = formField.value;
	if(inputString.length <= inputLength)
	{
		return true;
	}
	else
	{
		alert('Too many characters! Due to size limitations, the field must contain at most '+inputLength+' characters. '+inputString.length+' currently in use.');
		formField.focus();
		result = false;
	}
} 

function spaceRemaining(formField,inputLength,outputfield)
{
	var inputString = formField.value;
	var remaining = inputLength - inputString.length;
	
	if(remaining >= 0)
		outputfield.value = remaining;
	else
		outputfield.value = remaining;
}

// ****************************
// Email validation functions
// ****************************
function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function EmailValidator(theFormItem)
{

  if (theFormItem.value == "")
  {
    //alert("Please enter a value for the \"email\" field.");
    //theForm.email.focus();
    //return (false);
	return (true); //Another test tests for this (and it can be false if no email is selected)
  }

  if (!isEmailAddr(theFormItem.value))
  {
    alert("Please enter a complete email address in the form: name@domain.com");
    theFormItem.focus();
    return (false);
  }
   
  if (theFormItem.value.length < 3)
  {
    alert("Please enter at least 3 characters in the \"email\" field.");
    theFormItem.focus();
    return (false);
  }
  return (true);
}

function numbersonly(myfield, e, dec)
{
var key;
var keychar;
if (window.event)
 key = window.event.keyCode;
else if (e)
 key = e.which;
else
 return true;
keychar = String.fromCharCode(key);
// control keys
if ((key==null) || (key==0) || (key==8) || 
 (key==9) || (key==13) || (key==27) )
 return true;
// numbers
else if ((("0123456789").indexOf(keychar) > -1))
 return true;
// decimal point jump
else if (dec && (keychar == "."))
 {
 myfield.form.elements[dec].focus();
 return false;
 }
else
 return false;
}

function toggleLayer(whichLayer)
{
	var style;
	if (document.getElementById) // this is the way the standards work
		style = document.getElementById(whichLayer).style;
	else if (document.all) // this is the way old msie versions work
		style = document.all[whichLayer].style;
	else if (document.layers) // this is the way nn4 works
		style = document.layers[whichLayer].style;
	
	if(style.display == "none")
		style.display = "block";
	else
		style.display = "none";
		
		//style.visibility = style.visibility ? "":"hidden";
		//style.display = style.display ? "":"none";
}

function toggleStyle(whichLayer, style1, style2)
{
	var layer;
	if (document.getElementById) // this is the way the standards work
		layer = document.getElementById(whichLayer);
	else if (document.all) // this is the way old msie versions work
		layer = document.all[whichLayer];
	else if (document.layers) // this is the way nn4 works
		layer = document.layers[whichLayer];
	
	if(layer)
	{
		if(layer.className == style1)
			layer.className = style2;
		else
			layer.className = style1;
	}
}