// client-side JavaScript - requires functions from Formats.js

//
// FieldValErrorMessage
//
// This function is called when there is an error in a field. It highlights
// the field and displays an error message in the window status bar.
//
function FieldValErrorMessage(field, message)
{
  var bReturn = false;    // return false to cancel submitting a form, etc.

  TagField(field);

  alert(message);             // 1) tell the user
//  if( !confirm(message +      // or, 2) tell the user and let them cancel to bypass the error
//      "\n\nClick OK to fix the problem, or Cancel to ignore it." +
//      "\nNOTE:  Ignoring the problem may leave the record in an invalid state.") )
//    bReturn = true;
//  window.status = message;    // or, 3) display unobtrusively on the status line

	if( field.length )
//		{window.alert(field.length);
//		field[0].focus();}
  		field.focus();
	else
  	field.focus();
  return bReturn;
}

function FieldValErrorMessageCheckorRadio(field, message)
{
  var bReturn = false;    // return false to cancel submitting a form, etc.

  TagField(field);

  alert(message);             // 1) tell the user
//  if( !confirm(message +      // or, 2) tell the user and let them cancel to bypass the error
//      "\n\nClick OK to fix the problem, or Cancel to ignore it." +
//      "\nNOTE:  Ignoring the problem may leave the record in an invalid state.") )
//    bReturn = true;
//  window.status = message;    // or, 3) display unobtrusively on the status line

  return bReturn;
}

function FieldValErrorMessageCheckRequired(message)
{
  var bReturn = false;    // return false to cancel submitting a form, etc.

  alert(message);             // 1) tell the user
//  if( !confirm(message +      // or, 2) tell the user and let them cancel to bypass the error
//      "\n\nClick OK to fix the problem, or Cancel to ignore it." +
//      "\nNOTE:  Ignoring the problem may leave the record in an invalid state.") )
//    bReturn = true;
//  window.status = message;    // or, 3) display unobtrusively on the status line

  return bReturn;
}

// some functions to mark required fields in the UI - for IE 4+
function TagField(field)
{
	if( navigator.appVersion.indexOf("MSIE") != -1 )
	{
		if( !field.length )
  		field.style.backgroundColor = "#F1F384";
	}
}

function UntagField(field)
{
	if( navigator.appVersion.indexOf("MSIE") != -1 )
	{
		if( !field.length )
  		field.style.backgroundColor = "";   //"WINDOW";
	}
}

// some functions to validate required fields
function ReqFieldOk(field, fieldname)
{
  if( isEmpty(field.value) )
    return FieldValErrorMessage(field, fieldname + " must be entered");
  UntagField(field);
  return true;
}

function ReqFieldOkMode(field, field1, fieldname)
{
  var fVString = field1.value.toString();
  if( isEmpty(field.value) && field1.value != 'student')
    return FieldValErrorMessage(field, fieldname + " must be entered");
  UntagField(field);
  return true;
}


function ReqFieldNullLengthOk(field, fieldname, length)
{
  var fVString = field.value.toString();
  if((field.value != null && field.value != '') && fVString.length != length)
    return FieldValErrorMessage(field, fieldname + " must be " + length + " characters long");
  UntagField(field);
  return true;
}

function ReqFieldNumNullLengthOk(field, fieldname, min, max)
{
  var fVString = field.value.toString();
  if ( ReqNumFieldOk(field, fieldname) )
   {if((field.value != null && field.value != '') && (fVString.length < min || fVString.length > max))
    return FieldValErrorMessage(field, fieldname + " must be between " + min + " and " + max + " characters long");
  UntagField(field);
  return true;}
}

function ReqFieldLengthOk(field, fieldname, length)
{
  var fVString = field.value.toString();
  if(fVString.length != length)
    return FieldValErrorMessage(field, fieldname + " must be " + length + " characters long");
  UntagField(field);
  return true;
}

function ReqIntFieldLengthOk(field, fieldname, length)
{
  var fVString = field.value.toString();
  if(fVString.length != length)
    return FieldValErrorMessage(field, fieldname + " must be " + length + " integers long");
  UntagField(field);
  return true;
}

function ReqFieldNullStrCheck(field, fieldname, stringcheck)
{
  var fVString = field.value.toString();
  if((field.value != null && field.value != '') && fVString.indexOf(stringcheck) < 0)
    return FieldValErrorMessage(field, fieldname + " must have an " + stringcheck);
  UntagField(field);
  return true;
}

function ReqFieldStrCheck(field, fieldname, stringcheck)
{
  var fVString = field.value.toString();
  if(fVString.indexOf(stringcheck) < 0)
    return FieldValErrorMessage(field, fieldname + " must contain an " + stringcheck);
  UntagField(field);
  return true;
}

function ReqFieldFileTypeCheck(field, fieldname, typecheck)
{
// Assumes LOWER CASE String Compare
  var fVString = field.value.toString();
  var fVLower = fVString.toLowerCase();
  if(fVLower.indexOf(typecheck) < 0)
    return FieldValErrorMessage(field, fieldname + " must be a " + typecheck + " type file");
  UntagField(field);
  return true;
}

function ReqFieldFileTypeCheck2(field, fieldname, typecheck1, typecheck2)
{
// Assumes LOWER CASE String Compare
  var fVString = field.value.toString();
  var fVLower = fVString.toLowerCase();
  if(fVLower.indexOf(typecheck1) < 0 && fVLower.indexOf(typecheck2) < 0)
    return FieldValErrorMessage(field, fieldname + " must be either a " + typecheck1 + " or a " + typecheck2 + " type file");
  UntagField(field);
  return true;
}

function ReqNumFieldOk(field, fieldname)
{
  if( !isNumeric(field.value) )
    return FieldValErrorMessage(field, fieldname + " must be numeric");
  UntagField(field);
  return true;
}

function ReqNumFieldNullOk(field, fieldname)
{
  if( !isNumeric(field.value) && (field.value != null && field.value != ''))
    return FieldValErrorMessage(field, fieldname + " must be numeric");
  UntagField(field);
  return true;
}

function ReqIntegerFieldOk(field, fieldname)
{
  if( !isInteger(field.value) )
    return FieldValErrorMessage(field, fieldname + " must be an integer value");
  UntagField(field);
  return true;
}

function ReqIntegerFieldNullOk(field, fieldname)
{
  if( !isNumeric(field.value) )
	if (field.value == null || field.value == '') {
		field.value = 0;
	} else {
		return FieldValErrorMessage(field, fieldname + " must be numeric");
	}
  UntagField(field);
  return true;
}

function ReqNumMinMaxOk(field, fieldname, min, max)
{
  if ( ReqNumFieldOk(field, fieldname) )
  {
    if( !( field.value >= min && field.value <= max ) )
      return FieldValErrorMessage(field, fieldname + " must be between " + min + " and " + max);
  } else {
    return false;
  }

  UntagField(field);
  return true;
}

function ReqSelectOk(field, fieldname)
{
  if( isEmpty(field.options[field.selectedIndex].value) )
    return FieldValErrorMessage(field, "A valid " + fieldname + " must be selected");
  UntagField(field);
  return true;
}

function ReqCompareCatYrSelectOk(field1, field2, fieldname)
{
  if( field1.options[field1.selectedIndex].value == field2.options[field2.selectedIndex].value)
    return FieldValErrorMessage(field2, "The " + fieldname + " Must be different than the Old Catalog Year");
  UntagField(field);
  return true;
}

// works with checkboxes or radio buttons, returns true if there is at least one item selected
function ReqChecksOk(field, fieldname)
{
	for( var i=0; i<field.length; i++ )
		if( field[i].checked )
			return true;
	return FieldValErrorMessageCheckorRadio(field, fieldname + " is a required option");
}

// Works with checkboxes to validate that at least one has been checked, returns true if there is at least one item checked
function ReqChecksAllOk(elements)
{
//    var form = document.forms[0];
    for (i = 0; i < elements.length; i++)
	{ if (element[i].type == "checkbox" &&
	  element.checked)
	  window.alert(element[i].name);
//	  return true;
//	  return FieldValErrorMessageCheckRequired("Please check at least one of the type of information desired");
	}
}	
	
