/*=================================================
  checkPageForms()
  
  Version 0.5
  Updated: 02.13.04
  
  -------------------------------------------------
  This script causes all forms to pass the
  "checkPageForms" function before being submitted.
  
  How to use:
  
  Be sure your forms have both the name attribute
  and the id attribute.
  
  All required fields must have a label
  with the class "required" for this to work. Also,
  the label must point to the ID of the input
  element in the for attribute. For example:
  
  <label for="example" class="required">Example</label>
  <input type="text" name="example" id="example" />
  
  And that should be it.
  
  NOTE: THIS ONLY VALIDATES THE COMPLETION OF
  THE INPUT FIELDS, NOT THE VALUE OF THOSE FIELDS.
===================================================*/

var cancelForm=false; // buttons like "cancel" set
                      // this to true so the form
                      // is not validated when it
                      // does not need to be

/*-------------------------------------------------
  checkPageForms()
  -------------------------------------------------
  Cycles through the elements of a form. The
  function actually checks for labels with the
  class "required". When a "required" label is
  found, the script checks that a value has been
  filled out for that input.
---------------------------------------------------*/
function checkPageForms(frm, formName){
  // Check if the form element exists
  if(!frm){ return false; }
  // Browser must pass this basic test to proceed
  if(document.getElementById && !cancelForm){
    var id = frm.getAttribute('id');
    if(id == null){ id = frm.attributes['id'].value; }
    // If a form name is passed to this function, then
    // it must equal the value of the ID of this form
    // (this is so multiple forms can be checked on a
    // single page)
    if(id == formName || formName == ""){
      var labels = frm.getElementsByTagName('label');
      var error  = false;
      var errorMsg = "";
      for(var i=0; i<labels.length;i++){
        var l = labels[i];
        if(l.className == "required" || l.className == "error"){
          //if(typeof(elemID.value) != "undefined"){ elemID = elemID.value; }
          var elemID = l.getAttribute('for');
          if(elemID == null){ elemID = l.attributes['for'].value; }
          var e = document.getElementById(elemID);
          if(e){
            // Process the different input types
            var nodeName = e.nodeName;
            if(nodeName.toLowerCase() == "input"){
              // Input elements
              var eType = e.getAttribute('type');
              if(eType == null){ eType = e.attributes['type'].value; }
              if(eType == "text"){
                // Text type inputs
                if(qTrim(e.value) == ""){ l.className = "error"; error = true; }
                else { l.className = "required"; }
              }
              else if(eType == "radio"){
								// Radio elements
                if(e.checked !== true){ l.className = "error"; error = true; }
              }
							else if(eType == "checkbox"){
								// Checkbox elements
								if(e.checked !== true) { l.className = "error"; error = true; }
							}
								if($('input[name="lectures[]"]').length){ 
									window.console.log($('input[name="lectures[]"]:checked').length);
									if(!$('input[name="lectures[]"]:checked').length){ l.className = "error"; error = true;}
								}
            }
            else if(nodeName.toLowerCase() == "select"){
              // Select elements
              var ix = e.selectedIndex;
              var o = qTrim(e.options[ix].value);
              if(o == "#" || o == "-" || o == "" || o == "--" || o == "##" || o == "---" || o == " "){ l.className = "error"; error = true; }
              else { l.className = "required"; }
            }
            else if(nodeName.toLowerCase() == "textarea"){
              // Textarea elements
              if(qTrim(e.value) == ""){ l.className = "error"; error = true; }
              else { l.className = "required"; }
            }
          }
        }
      }
      if(error){
        var m = "There was an error with the form. Please enter a value for all the fields marked in red and select at least one lecture";
        if(errorMsg != ""){ m += "\n\n"+ errorMsg; }
        alert(m);
        error = false;
        if(document.getElementById){
          var emsg = document.getElementById("errorMsg");
          if(emsg){
            emsg.innerHTML="";
            h="<p class=\"error\">There was an error with the form. Please review the following issues:</p>"
            h+="<ul><li class=\"error\">You may have left one or more fields blank. These fields are marked in red.</li>";
            if(errorMsg != ""){ h += "<li class=\"error\">"+ errorMsg +"</li>"; }
            h+="</ul>";
            emsg.innerHTML=h;
            window.scrollTo(0,0);
            emsg.style.display="block";
          }
        }
        return false;
      }
      else {
        if(document.getElementById){
          var emsg = document.getElementById("errorMsg");
          if(emsg){
            emsg.innerHTML="";
            emsg.innerHTML="&nbsp;"
            emsg.style.display="none";
          }
        }
      }
    }
    else { return true; }
  }
  return true;
}
function qTrim(value) { return value.replace(/\n/,"").replace(/\r/,"").replace(" ",""); }
