// formtoolz.js - Client side companion for formtoolz.asp
///////////////////////////////////////////////////////////////////////////////

form = 0; // Default to the first form in the page

// setFocus sets the focus of an input object.
// If the object is a collection (etc. a radio button group), the first object
// receives focus.

function setFocus(field) {
  if (document.forms[form][field].length > 1) {
    document.forms[form][field][0].focus();
  } else {
    document.forms[form][field].focus();
  }
}

// GetValue returns the (first) selected value of an radio or 
// checkbox group or null if nothing is selected. For other types of inputs,
// the value is simply returned.

function GetValue(field) {
  var val = null;
  obj = document.forms[form][field];

  if (obj.type) {
    if (obj.type == 'text' || obj.type == "textarea" || obj.type == "password" || obj.type == "file") {
      val = obj.value;
      if (val == "")
        val = null;

    } else if (obj.type == 'checkbox') {
      if (obj.checked)
        val = obj.value;
      else
        val = "";

    } else if (obj.type == 'select-one') {
      val = obj.options[obj.selectedIndex].value;
      if (val == "")
        val = null;

    } else if (obj.type == 'select-multiple') {
      for (i=0; i<obj.length; i++)
        if (obj[i].selected)
          return obj[i].value;
    }
  } else if (obj.length > 0) {
    if (obj[0].type == 'radio' || obj[0].type == "checkbox") {
      for (i=0; i<obj.length; i++)
        if (obj[i].checked)
          return obj[i].value;
    }
  }
  return val;
}

function GetNum(field) {
  val = GetValue(field);
  return val;
}

function GetString(field) {
	return GetValue(field);
}

function GetLength(val) {
  val = GetValue(val);
  if (val != null)
	return val.toString().length;
  return 0;
}

function ConsistOf(str, valid) {
  if (str == "" || str == null )
    return false;
    
  str = str.toString();

  for (i=0; i<str.length; i++)
    if (valid.indexOf(str.charAt(i)) == -1)		// If the character is NOT valid...
	  return false;

  return true
}

// IsNumeric returns true only if val can be evaluated as a real number
function IsNumeric(val) {
  return ConsistOf(val, "0123456789-,.");
}

// IsInteger returns true only if val can be evaluated as an integer
function IsInteger(val) {
  return ConsistOf(val, "0123456789-");
}

function IsUsername(val) {
  return ConsistOf(val, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.");
}

function IsPassword(val) {
  return ConsistOf(val, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.");
}

function IsEmail(val) {
  if (val == null)
	return false;
  //The email must contain a @ and a .
  if (val.indexOf('.') == -1  ||  val.indexOf('@') == -1)
    return false;
  return true
}

// IsNull returns true if val is NULL
function IsNull(val) {
	if (val == null)
		return false;
	return true;
}

