function showValidationError( command, input, message )
{
  if( input && input.label )
  {
    input.label.style.color="red";
  }
  
  var domHelper     = new DomHelper();
  if( message )
  {
    if( input.error )
    {
      input.error.innerHTML = validator.message;
    }
  }
  else
  {
    tags = domHelper.findTagsByAttribute("for", input.form.id, "error");
    if( tags[0] && input.form.getAttribute("message") )
    {
      tags[0].innerHTML = input.form.getAttribute("message");
    }
  }
}

function clearValidationError( command, input )
{
  if( input && input.label )
    input.label.style.color="black";
  if( input && input.error )
  {
    input.error.innerHTML = "";
  }
}

function Input(pElement, pForm)
{
  this.element      = pElement;
  this.form         = pForm;
  this.error;
  this.label;
  
  var domHelper     = new DomHelper();
  
  try
  {
    var tags = domHelper.findTagsByAttribute("for", this.element.id);
    for( var x = 0; x <= tags.length; x++ )
    {
      if( tags[x].getAttribute("label") )
      {
        this.label = tags[x];
      }
      else if( tags[x].getAttribute("error") )
      {
        this.error = tags[x];
      }
    }
  }
  catch(e){}
  
}

function DomHelper()
{
  this.findTagsByAttribute = dhFindTagsByAttribute;

  function dhFindTagsByAttribute( name, value, nameTwo  )
  {
    var toReturn = Array();
    var allTags = document.getElementsByTagName('*');
    for (var x=0; x < allTags.length; x++)
    {
      if(!value && allTags[x].getAttribute(name) || value && allTags[x].getAttribute(name) == value)
      {
        toReturn[toReturn.length] = allTags[x];
      }
    }
    if( nameTwo )
    {
      var originalArray = toReturn;
      toReturn = Array();
      for( var x = 0; x < originalArray.length; x++ )
      {
        if( originalArray[x].getAttribute(nameTwo) )
        {
          toReturn[toReturn.length] = originalArray[x];
        }
      }
    }
    
    return toReturn;
  }
}

function FakeJax()
{
  this.submit = fjSubmit;
  this.validate = fjValidate;
  this.normalSubmit = fjNormalSubmit;
  this.populateForm = fjPopulateForm;
  this.getForm = fjGetForm;
  this.getFormMZ = fjGetFormMZ;
  this.getFormIE = fjGetFormIE;
  this.getFormSF = fjGetFormSF;
  
  function fjValidate(command)
  {
    var valid = true;
    var form  = document.getElementById(command);
    var serverSideValidators = Array();
    //alert(form.elements.length);
    for( var y = 0; y < form.elements.length; y++ )
    {
      var input = new Input(form.elements[y], form);
      clearValidationError( command, input ); 
    }
    for( var y = 0; y < form.elements.length; y++ )
    {
      var input = new Input(form.elements[y], form);
      if( input.element.getAttribute("validate") )
      {
        var value     = input.element.getAttribute("validate");
        var validator;
        eval(" validator = new " + value + ";" );
        if( validator )
        {
          if( !validator.validate(form.elements[y]) )
          {
            valid     = false;
            
            showValidationError( command, input, validator.message ); 
          }
        }
        // probably a server side validator
        else
        {
          serverSideValidators[serverSideValidators.length] == value;
        }
      }
    }
    return valid;
  }
  
  function fjSubmit(command)
  {
    var form  = document.getElementById(command);
    this.getForm().elements[0].value = command;
    var maxInputField = 1;
    
    var valid = this.validate(command);
    
    for( var x = 0; x < document.forms.length; x++ )
    {
      var form = document.forms[x];
      for( var y = 0; y < form.elements.length; y++ )
      {
        this.getForm().elements[maxInputField].value = form.elements[y].value;
        this.getForm().elements[maxInputField].name  = form.elements[y].id;
        this.getForm().elements[maxInputField].id    = form.elements[y].id;
        maxInputField++;
      }
    }
    
    maxInputField = this.populateForm("output", maxInputField);
    maxInputField = this.populateForm("label", maxInputField);
    maxInputField = this.populateForm("error", maxInputField);
    // server side validators  
    if( valid )
       this.getForm().submit();
  }
  
  function fjNormalSubmit(command)
  {
    var form  = document.getElementById(command);
    var valid = this.validate(command);
    if( valid )
       form.submit();
  }
  
  function fjPopulateForm(tagType, maxInputField)
  {
  
    var domHelper = new DomHelper();
    var labels    = domHelper.findTagsByAttribute( tagType );
    for (var x=0; x < labels.length; x++)
    {
      this.getForm().elements[maxInputField+x+1].name  = labels[x].id;
      this.getForm().elements[maxInputField+x+1].id    = labels[x].id;
//      this.getForm().elements[maxInputField+x+1].value = tagType + "_" + labels[x].getAttribute("for") + "_" + labels[x].id;
      this.getForm().elements[maxInputField+x+1].value = labels[x].id;          
      maxInputField++;
    }
    return maxInputField;
  }

  function fjGetForm()
  {
    var ba = new BrowserAbstractor();
    return ba.browserCall("fakeJax.getForm")
  }
  function fjGetFormIE()
  {
    return fakeJaxIFrame.document.getElementById("blankform");
  }
  function fjGetFormMZ()
  {
    return fakeJaxIFrame.contentDocument.getElementById("blankform");
  }
  function fjGetFormSF()
  {
    return fakeJaxIFrame.document.getElementById("blankform");
  }
}



function BrowserAbstractor()
{
  this.browserType = "MZ";
  if( navigator.appName.indexOf("icrosoft") > -1 )
    this.browserType = "IE";
  else if( navigator.appName.indexOf("afari") > -1 )
    this.browserType = "SF";
    
  this.browserCall = baBrowserCall;
  
  function baBrowserCall(func)
  {
    return eval(func + this.browserType + "();")
  }
}


// validators
function required(m)
{
  this.validate = rValidate
  this.message  = m;
  function rValidate( element )
  {
    if( element.value == null || element.value == "" )
      return false;
    
    return true;
  }
}

var fakeJax = new FakeJax();

/*
<body onLoad="fakeJax = new FakeJax();">
<form id="theForm">
Welcome to the site!<br><br><br>
<div style="border:thin solid black">
<div output="true" id="loginOutput" for="login" style="color:blue;">
<div id="loginError" for="login" error="true" style="color:red;">
</div>
<span error=true for="username" id="usernameError" > Username: </span>
<span label=true for="username" > Username: </span><input type=text validate="required" id=username ><br>
<span label=true for="password" >Password:</span> <input type=text id=password validate="required"><br>
<input type=button onClick="fakeJax.submit('login')" value=Login><br>
</div>
<br><br><br><br>
<div style="border:thin solid black" width=400 height=400>
<div output="true" id="registerError" style="color:red;">
</div>
Need an account?<br><br>
Firstname: <input type=text id=firstname ><br>
Lastname: <input type=text id=lastname ><br>
<input type=button onClick="fakeJax.submit('register')" value=Register><br>
</div>

<div style="border:thin solid black" width=400 height=400>
<div persistent=false output="true" id="timeOutput" style="color:red;">What time is it
</div>

<input type=button onClick="fakeJax.submit('timer')" value=Time><br>
</div>

</form id="theForm">

</html>

<iframe id="fakeJaxIFrame" src="dispatcher.php" height=100 width=100>
*/

