(function() { // Do everything in this one anonymous function
    // When the document finishes loading, call init()
    if (window.addEventListener) window.addEventListener("load", init, false);
    else if (window.attachEvent) window.attachEvent("onload", init);

    // Define event handlers for any forms and form elements that need them.
    function init() {
        // Loop through all forms in the document
        for(var i = 0; i < document.forms.length; i++) {
            var f = document.forms[i];  // the form we're working on now

            // Assume, for now, that this form does not need any validation
            var needsValidation = false;

            // Now loop through the elements in our form
            for(j = 0; j < f.elements.length; j++) {
                var e = f.elements[j];  // the element we're working on

                // We're only interested in <input type="text"> textfields
                if (e.type != "text") continue;

                // See if it has attributes that require validation
                var pattern = e.getAttribute("pattern");
                // We could use e.hasAttribute() but IE doesn't support it
                var required = e.getAttribute("required") != null;

                // Required is just a shortcut for a simple pattern
                if (required && !pattern) {
                    pattern = "\\S";
                    e.setAttribute("pattern", pattern);
                }

                // If this element requires validation,
                if (pattern) {
                    // validate the element each time it changes
                    e.onchange = validateOnChange;
                    // Remember to add an onsubmit handler to this form
                    needsValidation = true;
                }
            }

            // If at least one of the form elements needed validation,
            // we also need an onsubmit event handler for the form
            if (needsValidation) f.onsubmit = validateOnSubmit;
        }
    }
          
    // This function is the onchange event handler for textfields that 
    // require validation.  Remember that we converted the required attribute
    // to a pattern attribute in init().
    function validateOnChange() {
        var textfield = this;                            // the textfield
        var pattern = textfield.getAttribute("pattern"); // the pattern
        var value = this.value;                          // the user's input

        // If the value does not match the pattern set the class to "invalid".
        if (value.search(pattern) == -1) textfield.className = "invalid";
        else textfield.className = "valid";
    }

    // This function is the onsubmit event handler for any form that 
    // requires validation.
    function validateOnSubmit() {
        // When the form is submitted, we revalidate all the fields in the
        // form and then check their classNames to see if they are invalid.
        // If any of those fields are invalid, display an alert and prevent
        // form submission.
        var invalid = false;  // Start by assuming everything is valid
        var esum = 0;
        // Loop through all form elements
        for(var i = 0; i < this.elements.length; i++) {
            var e = this.elements[i];
            if (e.type == "text" | e.checked | e.type == "select-one" | e.type == "select-multiple" | e.type == "hidden" | e.type == "textarea" ) {
                 esum += e.value.length // add a checksum for server-side compare
            }
            // If the element is a text field and has our onchange handler
            if (e.type == "text" && e.onchange == validateOnChange) {
                e.onchange(); // Invoke the handler to re-validate
                // If validation fails for the element, it fails for the form
                if (e.className == "invalid") invalid = true;
            }
        }

        // If the form is invalid, alert user and block submission
        if (invalid) {
            alert("The form is incompletely or incorrectly filled out.\n" +
                  "Please correct the  highlighted fields and try again.");
            return false;
        }
       if (document.mwaaform.esumid) {
           document.mwaaform.esumid.value = esum;
           return true;
       }
       var newInput = document.mwaaform.elements[1].cloneNode(false); //newInput is a new form element place holder
       newInput.id = 'esumid';
       newInput.name = 'esumname';
       newInput.value = esum ;
       // newInput.type = 'Hidden';
       document.mwaaform.appendChild(newInput);
       return true;
    }
})();


function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function VerifyData(){	
  if (document.frmUser.fromwho.value == "") {
    alert("Please enter a Name.");
    document.frmUser.fromwho.focus();
    return (false);
  }
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.@";
  var checkStr = document.frmUser.email.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
	if (j == checkOK.length) {
	      allValid = false;
	      break;
	}
  }
  if (!allValid) {
    alert("Please enter only letter and digit and \"@.\"characters in the \"EMail\" field.");
    document.frmUser.email.focus();
    return (false);
  }
    var i = 1;
    var sLength = checkStr.length;

    // look for @
    while ((i < sLength) && (checkStr.charAt(i) != "@")) { i++ }
    if ((i >= sLength) || (checkStr.charAt(i) != "@")) {
	alert("Please enter real Email address.");
	document.frmUser.email.focus();
	return false;
    }
    else i += 2;

    // look for .
    while ((i < sLength) && (checkStr.charAt(i) != ".")) { i++ }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (checkStr.charAt(i) != ".")) {
	alert("Please enter real domain name in the \"EMail\" field.");
	document.frmUser.email.focus();
	return false;
    }  
    if (document.frmUser.body.value == "") {
	alert("Please enter a Description.");
	document.frmUser.body.focus();
	return (false);
    }      
    else
	  return true;
}
