

function ss79_checkForm(fieldx) 
{ 
  /* 
  if (fieldx != "")
  { alert(fieldx.tagName); }
  
  returns false, if any required value is missing
  parameter fieldx is optional to check a certain field
  if field has:
    - required="1": so the field must not be empty
    - validationmsg="xy": the displayed msg on an error
    - param: parameter for field check - regex?    
    
    ideas: increase performance for radios (name is given back)
  */
  var tempstrResult = 1;
  var strResult = true;
  if (fieldx == "")
  { //loop all fields
    ss79_checkForm_resetExistingValidationmsgs(fieldx);
    var tags;
    var j=0;
    var i=0;
    var tags2Check = new Array("input","select","textarea");
    for (j=0; j < tags2Check.length; j++)
    {
      var tags = document.getElementsByTagName(tags2Check[j]);
      for (i=0; i < tags.length; i++)
      { 
        //alert(tags[i]);
        tempstrResult = ss79_checkForm_fieldCheck(tags[i]);
        if (tempstrResult == false)
        { strResult = tempstrResult; }
      }
    }
  }
  else
  {
    // just check whichField
    ss79_checkForm_resetExistingValidationmsgs(fieldx);
    strResult = ss79_checkForm_fieldCheck(fieldx);
  }
  return strResult;
}

function ss79_checkForm_resetExistingValidationmsgs(fieldx)
{
  // just for a single field
  if (fieldx != "")
  {
    if (fieldx.nextSibling)
    {
      if (fieldx.nextSibling.className == "ss79_demandedField_validationmsg")
      {
        fieldx.className =  fieldx.nextSibling.className.replace("ss79_demandedField", "");
        fieldx.parentNode.removeChild(fieldx.nextSibling);
        ss79_checkForm_resetLabelStyle(fieldx.name);
      }
    }
  }
  else
  {
    var tags2remove = document.getElementsByTagName("div");
    var arrTags2remove = new Array();
    var countx = 0;
    var i=0;
    
    // get all nodes
    for (i=0; i < tags2remove.length; i++)
    { 
      if (tags2remove[i].className == "ss79_demandedField_validationmsg")
      { arrTags2remove[countx] = tags2remove[i]; countx++; }
    }
    
    // now delete styles
    if (countx > 0)
    {  
      for (i=0; i < arrTags2remove.length; i++)
      { 
        arrTags2remove[i].previousSibling.className =  arrTags2remove[i].previousSibling.className.replace("ss79_demandedField", "");
        arrTags2remove[i].parentNode.removeChild(arrTags2remove[i]);
      }
    }
  }  
}

function ss79_checkForm_setLabelStyle(labelFor)
{
  // loop labels
  var labels = document.getElementsByTagName("label");
  for (i=0; i < labels.length; i++)
  { 
    if (labels[i].getAttributeNode("for").nodeValue == labelFor)
    { labels[i].className = labels[i].className + " ss79_demandedField_label "; }
  }
}
function ss79_checkForm_resetLabelStyle(labelFor)
{
  // loop labels
  var labels = document.getElementsByTagName("label");
  for (i=0; i < labels.length; i++)
  { 
    if (labels[i].getAttributeNode("for").nodeValue == labelFor)
    { labels[i].className =  labels[i].className.replace("ss79_demandedField_label", ""); }
  }
}

function ss79_checkForm_fieldCheck(fieldx) 
{
  //alert(fieldx.type + " " + fieldx.value + " " + fieldx.getAttribute("required"));
  // check if field is required and value is set  
  if ((fieldx.getAttribute("required") != null))
  {
    if (fieldx.type == "checkbox")
    { return ss79_checkForm_fieldCheck_checkbox(fieldx); }
    else if (fieldx.type == "radio")
    { return ss79_checkForm_fieldCheck_radio(fieldx); }
    else if (fieldx.value == "")
    { return ss79_checkForm_fieldCheck_validationmsg(fieldx); }
    else
    { return true; }
  }
  else // not required
  { return true; }
}

function ss79_checkForm_fieldCheck_validationmsg(fieldx)
{  
  // change border style
  fieldx.className = fieldx.className + " ss79_demandedField ";
  // show validationmsg 
  var validationmsgx = "!";
  var i = 0;
  if ((fieldx.getAttribute("validationmsg") != null) && (fieldx.getAttribute("validationmsg") != ""))
  { validationmsgx = fieldx.getAttribute("validationmsg"); }
  //alert(validationmsgx);
  var neuDiv = document.createElement("div");
  var classx = document.createAttribute("class");
  classx.nodeValue = "ss79_demandedField_validationmsg";
  neuDiv.setAttributeNode(classx);
  var neuDivText = document.createTextNode(" " + validationmsgx);
  neuDiv.appendChild(neuDivText);
  fieldx.parentNode.insertBefore(neuDiv, fieldx.nextSibling); 
  
  // add field specific, so user sees changes onblur
  
  var oneventsx = document.createAttribute("onblur");
  if (fieldx.fireEvent)
  { oneventsx.nodeValue = new Function("ss79_checkForm(this);"); } // IE
  else
  { oneventsx.nodeValue = "ss79_checkForm(this);"; } // FF
  fieldx.setAttributeNode(oneventsx);
  
  var oneventsx = document.createAttribute("onchange");
  if (fieldx.fireEvent)
  { oneventsx.nodeValue = new Function("ss79_checkForm(this);"); } // IE
  else
  { oneventsx.nodeValue = "ss79_checkForm(this);"; } // FF
  fieldx.setAttributeNode(oneventsx);
  /*
  oneventsx = document.createAttribute("onkeyup");
  oneventsx.nodeValue = "ss79_checkForm(this);";
  fieldx.setAttributeNode(oneventsx);
  oneventsx = document.createAttribute("onchange");
  oneventsx.nodeValue = "ss79_checkForm(this);";
  fieldx.setAttributeNode(oneventsx);*/
  
  // change label of that field
  if ((fieldx.name != null) && (fieldx.name !=""))
  { ss79_checkForm_setLabelStyle(fieldx.name); }
    
  return false; // when valmsgs is display, something is not correct   
}

function ss79_checkForm_fieldCheck_radio(fieldx)
{
  var arrTags2remove = new Array();
  var isChecked =0;
  var countx =0;
  var i=0;
  
  var tags = document.getElementsByTagName("input");
  for (i=0; i < tags.length; i++)
  { 
    // check all radios with same name if one is checked then return name
    if (tags[i].name == fieldx.name)
    {
      arrTags2remove[countx] = tags[i]; countx++;
      if (tags[i].checked)
      { isChecked = 1; }
    } 
  }
  
  // if any radio was checked, reset validationmsg at the others
  if (isChecked != 0)
  { 
    for (i=0; i < arrTags2remove.length; i++)
    { ss79_checkForm_resetExistingValidationmsgs(arrTags2remove[i]); }
    return arrTags2remove[0].name;  
  }  
  return ss79_checkForm_fieldCheck_validationmsg(fieldx);  
}

function ss79_checkForm_fieldCheck_checkbox(fieldx)
{
  if (!fieldx.checked)
  { return ss79_checkForm_fieldCheck_validationmsg(fieldx); }  
}
