// form handling routines...

// depreciated use id('id') instead from common.js
// safely gets an element by ID or name 
//function element(id) {
//  try {
//    return document.getElementById(id);
//    }
//  catch (eID) {
//    try {
//      return document.getElementsByName(id);
//      }
//    catch (eName) {
//      return null;
//      };
//    };
//  };

// set focus to an object...
function focusID(id) {
  field = document.getElementById(id); field.focus(); field.select();
  };

// makes a concantenated list of all the elements of a group that are checked.
// checkboxes will return values of checked elements.
// radio buttom will return the value of the selected button.
function chkd(name) {
  var chk=[];
  element=document.getElementsByName(name);
  for (i=0;i<element.length;i++) {
    if (element[i].checked==true) {
      chk[chk.length]=element[i].value;
      };
    };
  return chk.join("|");
  };

// returns the selected elements of a list...
// name is the id property of select element for IE
// name is the name property of the option elements in Firefox
function choiceValue(name) {
  var id = document.getElementById(name);
  return id.value;
  };
function choiceIndex(name) {
  var id = document.getElementById(name);
  return id.selectedIndex;
  };
function choiceOption(name) {
  var id = document.getElementById(name);
  return id.options[id.selectedIndex]['text'];
  };

// generic field validation routine.
function fieldIs(id,condition,min,max,prompt) {
  condition = condition || ''; min = min || 0; max= max || 0, prompt = prompt || '';
  var obj=document.getElementById(id); str = obj.value; str = str.toString(); var field=id.toUpperCase(); var msg;
  if (min) {	// check minimum length...
    msg = prompt || (field + " is required.");
    if (str.length==0) { alert(msg); focusID(id); return false; };
    msg = prompt || (field + " does not contain the required minimum " + min + " characters.");
    if (str.length<min) { alert(msg); focusID(id); return false; };
    };
  if (max) {	// check maximum length...
    msg = prompt || (field + " contains greater than maximum " + max + " characters.");
    if (str.length>max) { alert(msg); focusID(id); return false; };
    };
  var re;
  switch (condition) {
    case 'fullText':
      re = re || /^[\w\s\d\-\.,?!\"\'@$#&$*\(\)]+/i;
    case 'username':
      re = re || /^[\w-\.@]+/i;
    case 'password':
      re = re || /^[\w-!@#$%^&*]+/i;
    case 'text':
      re = re || /^[\w\s\d\-]+/i;
      var clean=str.match(re);
      msg = prompt || (field + " contains invalid characters and has been modified.");
	  if ((str.length!=0) & (str!=clean)) {	obj.value=clean; alert(msg); focusID(id); return false; };
      break;
    case 'email':
      re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
      msg = prompt || (field + " considered an invalid address.");
	  if ((str.length!=0) & (!str.match(re))) { alert(msg); focusID(id); return false; };
      break;
    case 'phone':
      re = /^[-\d\.]*$/;
    case 'number':
      re = re || /^[-+\d\.]*$/;
	  msg = prompt || ("Number only ["+re+"] required for " + field);
	  if ((str.length!=0) & (!str.match(re))) { alert(msg); focusID(id); return false; };
      break;
    case 'notEmpty':
      return (str.length!=0);
    case 'empty':
      return (str.length==0);
    };
  return true;
  };

// checks a field for allowed characters of a specified named pattern...
function fieldCharMatch(evt, pattern) {
  evt = (evt) ? evt : ((window.event) ? event : null);
  var code;
  // IE triggers a window event and passes a keyCode, where Mozilla passes a charCode.
  if (window.event) { code=evt.keyCode; } else { code=evt.charCode };
  if (code<32) { return true; };
  var re = {
    phone:/[0-9\-]/,
    name:/[a-zA-Z\-]/,
    names:/[a-zA-Z\- ]/,
    email:/[a-zA-Z0-9_\-\.@]/,
    username:/[a-zA-Z0-9_\-\.@]/,
    word:/[a-zA-Z0-9_\-]/,
    words:/[a-zA-Z0-9_\- ]/,
    hex:/[0-9a-fA-F]/,
    digits:/[0-9]/,
    alpha:/[a-zA-Z]/,
    integer:/[0-9+\-]/,
    float:/[0-9\.,eE+\-]/,
    text:/[a-zA-Z0-9 _\-\.,\"\'@\?!#$&*\(\)]/
    };
  char = String.fromCharCode(code);
  //alert (char+":"+re[pattern]);
  return (null != char.match(re[pattern]));
  };

// swap two form elements 
function elementHideAndShow(hide,show,style) {
  style = style || 'inline';
  if (idExists(hide)) { id(hide).style.display='none'; };
  if (idExists(show)) { id(show).style.display=style; };
  };
