function processForms()
{
  debug('processForms');
  
  var oForms = document.forms;
  for (var iFormIndex=0,iFormLength=oForms.length; iFormIndex < iFormLength; iFormIndex++)
  {
    if (oForms[iFormIndex])
    {
      var oInputElements = oForms[iFormIndex].getElementsByTagName('INPUT');
      debug("got "+oInputElements.length+" input elements");
      for (var iIndex = 0, iLength = oInputElements.length; iIndex < iLength; iIndex++)
      {
        if (oForms[iFormIndex].className.indexOf("inline-labels") != -1)
        {
          var oLabel = document.createElement('label');
          oLabel.innerHTML = oInputElements[iIndex].title;
          oLabel.className = "input-inner-label";
          
          debug("element value:" +oInputElements[iIndex].value);
          if (oInputElements[iIndex].value != "")
          {
            oLabel.style.display = 'none';
          }
          
          oInputElements[iIndex].parentNode.appendChild(oLabel);
          
          var oElement = oInputElements[iIndex];
          addEvent(oLabel, 'click', function(oElement) {
                    return function(oEvnt) { oElement.focus(); }}(oElement));
          
          addEvent(oInputElements[iIndex], 'keydown', function() 
                        {
                          if (this.parentNode.childNodes[2].tagName == "LABEL")
                          {
                            this.parentNode.childNodes[2].style.display = 'none'; 
                          }
                          else
                          {
                            this.parentNode.childNodes[3].style.display = 'none'; 
                          }
                        });
          addEvent(oInputElements[iIndex], 'blur', function() 
                        {
                          if (this.value == '') 
                          {
                            if (this.parentNode.childNodes[2].tagName == "LABEL")
                            {
                              this.parentNode.childNodes[2].style.display = 'block'; 
                            }
                            else
                            {
                              this.parentNode.childNodes[3].style.display = 'block'; 
                            }
                          }
                        });
        }
        
        addEvent(oInputElements[iIndex], 'keyup', validateFormElement);
        
        checkValidValue(oInputElements[iIndex]);
      }
    }
    else
    {
      debug("error with form "+iFormIndex);
    }
  }
}
addLoadEvent(processForms);


function validateFormElement(oEvent)
{
  if (this.className.indexOf('valid') != -1 || this.className.indexOf('required') != -1)
  {
    if (this.value != "") 
    {
      this.className = "valid";
    } 
    else 
    {
      this.className = "required";
    }
  }
}

function checkValidValue(oFormItem)
{
  if (oFormItem.className.indexOf('valid') != -1 || oFormItem.className.indexOf('required') != -1)
  {
    if (oFormItem.value != "") 
    {
      oFormItem.className = "valid";
    } 
    else 
    {
      oFormItem.className = "required";
    }
  }
  else
  {
  }
}

function checkValidx(formItem) 
{
  if (document.getElementById(formItem))
  {
    if (document.getElementById(formItem).value != "") 
    {
      document.getElementById(formItem).className = "valid";
    } 
    else 
    {
      document.getElementById(formItem).className = "required";
    }
  }
  else
  {
  
  }
}

/* Quick and dirty checking! */
function checkrequired_normal(form)
{
  debug("checking form");
  var bError = false;
  var sErrorElement = "";
  
  aoFormInput = form.getElementsByTagName('input');
  for (var iCount = 0; iCount < aoFormInput.length; iCount ++)
  {
    if (aoFormInput[iCount].className.indexOf('required') != -1)
    {
      debug(aoFormInput[iCount].name+" is requred"); 
      if (aoFormInput[iCount].value == "")
      {
        debug(aoFormInput[iCount].name+" is invalid ("+aoFormInput[iCount].className+")");
        bError = true;
        if (sErrorElement == "")
        {
          sErrorElement = aoFormInput[iCount].name;
        }
      }
      else
      {
        debug(aoFormInput[iCount].name+" is valid");
      }
    }
    else
    {
      debug(aoFormInput[iCount].name+" is not required: "+aoFormInput[iCount].className+" ("+aoFormInput[iCount].className.indexOf('required')+")");
    }
  }
  
  debug("form error element: "+sErrorElement+"");
  aoFormSelect = form.getElementsByTagName('select')
  debug(aoFormSelect.length);
  for (var iCount = 0; iCount < aoFormSelect.length; iCount ++)
  {
    debug(aoFormSelect[iCount].className.indexOf('required'));
    if (aoFormSelect[iCount].className.indexOf('required') != -1)
    {
      debug("value:" +aoFormSelect[iCount].value);
      if (aoFormSelect[iCount].value == "" || aoFormSelect[iCount].value == "-- Select A Country --")
      {
        debug(aoFormInput[iCount].name+" is invalid");
        bError = true;
        sErrorElement = aoFormSelect[iCount].name;
      }
      else
      {
        debug(aoFormInput[iCount].name+" is valid");
      }
    }
  }
  
  
  if (bError) 
  {
    alert("Error:\nRequired information is missing; please ensure all required fields contain information ("+sErrorElement+")");
    return false
  } 
  else 
  {
    return true;
  }
}

function checkRequired_password(form)
{
  var error = "";
  
  var sCurrentPassword = "";
  var sNewpassword1 = document.getElementById('_newpassword1').value;
  var sNewpassword2 = document.getElementById('_newpassword2').value;
  
  if (document.getElementById('_password'))
  {
    sCurrentPassword = document.getElementById('_password').value;
    sCurrentPassword == "" ? error = "Your current password is required to change the password" : null;
  }
  else
  {
    sCurrentPassword = "admin_237846";
  }
  
  sNewpassword1 == "" ? error = "You must provide the new password twice for confirmation" : null;
  document.getElementById('_newpassword2').value == "" ? error = "You must provide the new password twice for confirmation" : null;
  
  if (sCurrentPassword == sNewpassword1)
  {
    error = "The old password and new password are the same";
  }
  
  if (sNewpassword1.length < 3)
  {
    error = "The new password must be greater then 3 characters";
  }
  
  if (sNewpassword1 != sNewpassword2)
  {
    error = "The new password and the password confirmation do not match";
  }
  
  if (error) 
  {
    alert("Error:\n" + error);
    return false
  } 
  else 
  {
    return true;
  }
}

function checkrequired_setting(form) 
{
  var error = "";
  
  document.getElementById('_firstName').value == "" ? error = "A first name must be provided" : null;
  document.getElementById('_lastName').value == "" ? error = "A last name must be provided" : null;
  document.getElementById('_company').value == "" ? error = "A company must be provided" : null;
  document.getElementById('_phone').value == "" ? error = "A phone number must be provided" : null;
  document.getElementById('_country').value == "-- Select A Country --" ? error = "A country must be provided" : null;
  document.getElementById('_address1').value == "" ? error = "An address must be provided" : null;
  document.getElementById('_postcode').value == "" ? error = "A post code or zip code must be provided" : null;
  
  if (error) {
    alert("Error:\n" + error);
    return false
  } else {
    return true;
  }
}
