/* CLIENT-SIDE VALIDATORS
---------------------------------------------------------------- */  

var strValidationSummary = "";
var strInvalidColor = "#cc3333";

function validate_date(_strObjectID, _bolSetFocus) {
  var objTemp = document.getElementById(_strObjectID);
  if (objTemp) {
    var objLabel = document.getElementById("lbl" + _strObjectID.substr(3));
    var strDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var aryMatches = objTemp.value.match(strDatePattern);
  
    if (aryMatches == null) {
      if (_bolSetFocus) {
        objTemp.focus();
        }
      update_label(objLabel, true);
      strValidationSummary += "<li>" + get_label_text(objLabel) + "</li>";
      return false; // invalid
      }
      
    var intMonth = aryMatches[1] - 1; // JS months are 0-11, so -1
    var intDay = aryMatches[3];
    var intYear = aryMatches[5];
  
    dteDate = new Date(intYear, intMonth, intDay);
    
    if (!(intDay == dteDate.getDate() && intMonth == dteDate.getMonth() && intYear == dteDate.getFullYear())) {
      if (_bolSetFocus) {
        objTemp.focus();
        }
      update_label(objLabel, true);
      strValidationSummary += "<li>" + get_label_text(objLabel) + "</li>";
      return false; // invalid
      }
    else {
      update_label(objLabel, false);
      }
    }
  return true; // valid
  }

function validate_zip(_strObjectID, _bolSetFocus) {
  var objTemp = document.getElementById(_strObjectID);
  if (objTemp) {
    var objLabel = document.getElementById("lbl" + _strObjectID.substr(3));
    var regZIP = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
    if (regZIP.test(objTemp.value) != true) {
      if (_bolSetFocus) {
        objTemp.focus();
        }
      update_label(objLabel, true);
      strValidationSummary += "<li>" + get_label_text(objLabel) + "</li>";
      return false; // invalid
      }
    else {
      update_label(objLabel, false);
      }
    }
  return true; // valid
  }

function validate_email(_strObjectID, _bolSetFocus) {
  var objTemp = document.getElementById(_strObjectID);
  if (objTemp) {
    var objLabel = document.getElementById("lbl" + _strObjectID.substr(3));
    var regEmail = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
    if (regEmail.test(objTemp.value) != true) {
      if (_bolSetFocus) {
        objTemp.focus();
        }
      update_label(objLabel, true);
      strValidationSummary += "<li>" + get_label_text(objLabel) + "</li>";
      return false; // invalid
      }
    else {
      update_label(objLabel, false);
      }
    }
  return true; // valid
  }

function validate_radio(_strObjectID, _bolSetFocus) {
  var bolResult = false;
  var objFirstRadio = null;
  var objForm = document.getElementById("frmForm1");
  
  if (objForm) {
    var objLabel = document.getElementById("lbl" + _strObjectID.substr(3));
    for (x = 0; x < objForm.elements.length; x++) {
      var objTemp = objForm.elements[x];
      if (objTemp.type == "radio" && objTemp.id.match(_strObjectID) != null) {
        if (objFirstRadio == null) {
          objFirstRadio = objTemp;
          }
        if (objTemp.checked) {
          bolResult = true;
          }
        }
      }
    if (!bolResult) {
      if (_bolSetFocus) {
        objFirstRadio.focus();
        }
      update_label(objLabel, true);
      strValidationSummary += "<li>" + get_label_text(objLabel) + "</li>";
      }
    else {
      update_label(objLabel, false);
      }
    }

  return bolResult;
  }


  function validate_checkboxes(_objForm, _bolSetFocus) {
    var bolResult = false;
    var objFirstCheckbox = null;
    if (_objForm) {
      var objLabel = document.getElementById("lblSubscriptions");
      for (x = 0; x < _objForm.elements.length; x++) {
        var objTemp = _objForm.elements[x];
        if (objTemp.type == "checkbox") {
          if (objFirstCheckbox == null) {
            objFirstCheckbox = objTemp;
            }
          if (objTemp.checked) {
            bolResult = true;
            }
          }
        }
      if (!bolResult) {
        if (_bolSetFocus) {
          objFirstCheckbox.focus();
          }
        update_label(objLabel, true);
        strValidationSummary += "<li>Subscriptions</li>";
        }
      else {
        update_label(objLabel, false);
        }
      }
      
    return bolResult;
    }



function validate_required(_strObjectID, _bolSetFocus) {
  if (_strObjectID.substr(0, 3) == "rdo") {
    return validate_radio(_strObjectID, _bolSetFocus)
    }
  else {
    var objTemp = document.getElementById(_strObjectID);
    if (objTemp) {
      var objLabel = document.getElementById("lbl" + _strObjectID.substr(3));
      
      switch(objTemp.type) {
        case "select-multiple":
        case "select-one":
        case "textarea":
        case "text":
          if (objTemp.value.length < 1) {
            if (_bolSetFocus) {
              objTemp.focus();
              }
            update_label(objLabel, true);
            strValidationSummary += "<li>" + get_label_text(objLabel) + "</li>";
            return false; // invalid
            }
          else {
            update_label(objLabel, false);
            }
          break;
  
        case "checkbox":
          if (!objTemp.checked) {
            if (_bolSetFocus) {
              objTemp.focus();
              }
            update_label(objLabel, true);
            strValidationSummary += "<li>" + get_label_text(objLabel) + "</li>";
            return false; // invalid
            }
          else {
            update_label(objLabel, false);
            }
          break;
        }
      }
    }
    
  return true; // valid
  }


function display_validation_summary(_bolVisible) {
  var objValidationSummary = document.getElementById("divValidationSummary");
  if (objValidationSummary) {
    if (_bolVisible && strValidationSummary != "") {
      var strMessage = "<p style=\"color: " + strInvalidColor + "\"><strong>Diese faengt sind fehlend oder falsch auf:</strong></p><ul>";
      strMessage += strValidationSummary + "</ul>";
      objValidationSummary.innerHTML = strMessage;
      objValidationSummary.style.visibility = "visible";
      objValidationSummary.style.display = "block";
      }
    else {
      objValidationSummary.style.visibility = "hidden";
      objValidationSummary.style.display = "none";
      }
    }
  }



/* HELPER FUNCTIONS */

function update_label(_objLabel, _bolInvalid) {
  if (_objLabel) {
    if (_bolInvalid) {
      _objLabel.style.color = strInvalidColor;
      _objLabel.style.fontWeight = "bold";
      }
    else {
      _objLabel.style.color = "";
      _objLabel.style.fontWeight = "";
      }
    }
  }
  
function get_label_text(_objLabel) {
  if (_objLabel) {
    return _objLabel.innerHTML.replace(/(\* )|:/g, "");
    }
  else {
    return "";
    }
  }
