///////////////////////////////////////////////////////////////////////////
//
//  Function-pack: form verification
//  Copyright (c) 2006 I.W. 380 (http://iw380.com.ua)
//
//      realGVM   (Valeriy Galyant)   (Valeriy.Galyant@gmail.com)
//      Tramp     (Alexis Nabok)      (Alex.Nabok@gmail.com) 
//      Kiev, Ukraine
//
//  Version 1.0
//  This copyright notice MUST stay intact for use
//
///////////////////////////////////////////////////////////////////////////


var formVer = new Array();
function formConfig()
{
  if (formVer.length > 0) return true; else return false;
}
function fetchElements(name)
{
  arr = new Array(); // Elements collection
  el = document.frm.elements[name];
  if (!el) el = document.frm.elements[name + '[]'];
  if (!el) return;
  if (el.length && !(el.type && (el.type == 'select-one' || el.type == 'select-multiple')))
  {
    for(i=0; i<el.length; i++)
    {
    	arr[arr.length] = el[i];
    }
  }
  else
  {
    arr[arr.length] = el;
  }
  el = document.frm.elements[name + '_'];
  if (!el) el = document.frm.elements[name + '_[]'];
  if (el) 
  {
    if (el.length && !(el.type && (el.type == 'select-one' || el.type == 'select-multiple')))
    {
      for(i=0; i<el.length; i++)
      {
        arr[arr.length] = el[i];
      }
    }
    else
    {
      arr[arr.length] = el;
    }
  }
  return arr;
}
function createFunction(func)
{
  if (!func)
  {
    return function(value) { return !/^\s*$/.test(value); }
  }
  if (func && func.test && typeof func.test == 'function')
  {
    return func.test;
  }
  if (func && typeof func == 'function')
  {
    return func;
  }
  return function(value) { return true; } // Who knows - how to check :-)
}
function fvAddField(name, func, text)
{
  formVer[formVer.length] = {'elements' : fetchElements(name), 'function' : createFunction(func), 'text' : text}
}
function fvAddFunc(func, text)
{
  formVer[formVer.length] = {'elements' : null, 'function' : createFunction(func), 'text' : text}
}
function _fetchElementTitle(el)
{
  html = new String(el.innerHTML);
  return html.replace(/\[[^\]].*\]/g, '').replace(/<[^>]*>/g, ' ').replace(/\s\s+/g, ' ').replace(/^\s+/g, '').replace(/\s+$/g, '');
}
function fetchElementTitle(el)
{
  el = el.parentNode;
  title = null;
  while ((el.previousSibling || el.parentNode) && !/^th/i.test(el.tagName))
  {
    if (el.previousSibling)
    {
  	  el = el.previousSibling;
    }
    else
    {
  	  el = el.parentNode;
    }
  }
  title = _fetchElementTitle(el);
  return title;
}
function formVerification(frm, msg)
{
  errf  = null;
  errm  = msg +"\n";
  errm += "------------------------------------------------\n";
  for(i=0; i<formVer.length; i++)
  {
    errs = false;
    els  = formVer[i]['elements'];
    func = formVer[i]['function'];
    text = formVer[i]['text'];
    if (!els || els.length == 0) continue;
    if (!text) text = fetchElementTitle(els[0]);
    if (!text) text = els[0].name.replace('[]', '');
    for(j=0; j<els.length; j++)
    {
      el = els[j];
    	if ((el.type == 'checkbox' || el.type == 'radio') && el.checked)
      {
        errs = true;
        break;
      }
      if (el.type == 'select-one' && (el.options[el.selectedIndex].value != 0 && el.options[el.selectedIndex].value != ''))
      {
        errs = true;
        break;
      }
      if (el.type == 'select-multiple' && el.selectedIndex != -1)
      {
        errs = true;
        break;
      }
    	if ((el.type == 'text' || el.type == 'password' || el.type == 'file' || el.type == 'textarea') && func(el.value))
      {
        errs = true;
        break;
      }
    }
    if (!errs)
    {
      errm += text + "\n";
      if (!errf) errf = els[0];
    }
  }
  if (errf)
  {
    errm += "------------------------------------------------\n";
    errf.focus();
    alert(errm);
    return false;
  }
  return true;
}