//  Validation functions for HorizonMSI
//  Date: 11/22/2005
//  Added because XHTML and struts auto javascript functionality do not work together
//  removed form name from the following functions - validateMask, validateEmail, validateRequired
//  added form name manually to functions - validateReoPropertiesForm, validateFeedBackForm
<!-- Begin 

     var bCancel = false; 

    function validateReoPropertiesForm(form) {                                                                   
        if (bCancel) 
      		return true; 
        else 
 			var formValidationResult;
       	formValidationResult = validateRequired(form,'ReoPropertiesForm'); 
   		return (formValidationResult == 1);
   	} 

    function ReoPropertiesForm_required () { 
    	this.a0 = new Array("state", "Please choose a state.", new Function ("varName", " return this[varName];"));
    } 

	function validateFeedBackForm(form) {       
    	if (bCancel) 
      		return true; 
        else 
			var formValidationResult;
		formValidationResult = validateRequired(form,'FeedBackForm') && validateMask(form,'FeedBackForm') && validateEmail(form,'FeedBackForm'); 
     	return (formValidationResult == 1);
   } 

    function FeedBackForm_required () { 
    	this.a0 = new Array("name", "Name is required.", new Function ("varName", "this.mask=/^[A-Za-z][-A-Za-z0-9_ .]{0,75}$/;  return this[varName];"));
    	this.a1 = new Array("phone", "Phone is required.", new Function ("varName", "this.mask=/^(\\d{3})[-| ]?(\\d{3})[-| ]?(\\d{4})$/;  return this[varName];"));
    } 

    function FeedBackForm_mask () { 
    	this.a0 = new Array("name", "Name is invalid. The following are valid characters: A-Z a-z 0-9 _ - (blank) .", new Function ("varName", "this.mask=/^[A-Za-z][-A-Za-z0-9_ .]{0,75}$/;  return this[varName];"));
    	this.a1 = new Array("address", "Address is invalid. The following are valid characters: A-Z a-z 0-9 _ - (blank) .", new Function ("varName", "this.mask=/^[A-Za-z|0-9][-A-Za-z0-9_ .,]{0,75}$/;  return this[varName];"));
    	this.a2 = new Array("phone", "Phone is invalid. Please try again with the following format: 999-999-9999", new Function ("varName", "this.mask=/^(\\d{3})[-| ]?(\\d{3})[-| ]?(\\d{4})$/;  return this[varName];"));
    	this.a3 = new Array("comments", "Comments is invalid. The following are valid characters: A-Z a-z 0-9 _ - (blank) . , * # ! ? : ; ' \" ( ) and the valid length is: 2048", new Function ("varName", "this.mask=/^[A-Za-z|0-9|.|;|:|!|?|,|#|#|'|\"|(|)][-A-Za-z0-9_ .;:!?,#*'\"()]{0,2048}$/;  return this[varName];"));
    } 

    function FeedBackForm_email () { 
    	this.a0 = new Array("email", "Email is an invalid.", new Function ("varName", " return this[varName];"));
    } 



    /*$RCSfile$ $Revision: 171 $ $Date: 2005-11-22 14:51:07 -0800 (Tue, 22 Nov 2005) $ */
    /**
    * Check to see if fields are a valid using a regular expression.
    * Fields are not checked if they are disabled.
    * <p>
    * @param form The form validation is taking place on.
    */
    function validateMask(form,functionName) {
        var isValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();

        oMasked = eval('new ' + functionName + '_mask()');      
        for (x in oMasked) {
            var field = form[oMasked[x][0]];

            if ((field.type == 'hidden' ||
                field.type == 'text' ||
                 field.type == 'textarea' ||
				 field.type == 'file') &&
                 (field.value.length > 0) &&
                 field.disabled == false) {

                if (!matchPattern(field.value, oMasked[x][2]("mask"))) {
                    if (i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oMasked[x][1];
                    isValid = false;
                }
            }
        }

        if (fields.length > 0) {
           focusField.focus();
           alert(fields.join('\n'));
        }
        return isValid;
    }

    function matchPattern(value, mask) {
       return mask.exec(value);
    }

   /*$RCSfile$ $Revision: 171 $ $Date: 2005-11-22 14:51:07 -0800 (Tue, 22 Nov 2005) $ */
    /**
    * Check to see if fields are a valid email address.
    * Fields are not checked if they are disabled.
    * <p>
    * @param form The form validation is taking place on.
    */
    function validateEmail(form, functionName) {
        var bValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();

        oEmail = eval('new ' + functionName + '_email()');

        for (x in oEmail) {
            var field = form[oEmail[x][0]];
            if ((field.type == 'hidden' || 
                 field.type == 'text' ||
                 field.type == 'textarea') &&
                (field.value.length > 0) &&
                field.disabled == false) {
                if (!checkEmail(field.value)) {
                    if (i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oEmail[x][1];
                    bValid = false;
                }
            }
        }
        if (fields.length > 0) {
            focusField.focus();
            alert(fields.join('\n'));
        }
        return bValid;
    }

    /**
     * Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
     * http://javascript.internet.com
     */
    function checkEmail(emailStr) {
       if (emailStr.length == 0) {
           return true;
       }
       var emailPat=/^(.+)@(.+)$/;
       var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
       var validChars="\[^\\s" + specialChars + "\]";
       var quotedUser="(\"[^\"]*\")";
       var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
       var atom=validChars + '+';
       var word="(" + atom + "|" + quotedUser + ")";
       var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
       var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
       var matchArray=emailStr.match(emailPat);
       if (matchArray == null) {
           return false;
       }
       var user=matchArray[1];
       var domain=matchArray[2];
       if (user.match(userPat) == null) {
           return false;
       }
       var IPArray = domain.match(ipDomainPat);
       if (IPArray != null) {
           for (var i = 1; i <= 4; i++) {
              if (IPArray[i] > 255) {
                 return false;
              }
           }
           return true;
       }
       var domainArray=domain.match(domainPat);
       if (domainArray == null) {
           return false;
       }
       var atomPat=new RegExp(atom,"g");
       var domArr=domain.match(atomPat);
       var len=domArr.length;
       if ((domArr[domArr.length-1].length < 2) ||
           (domArr[domArr.length-1].length > 3)) {
           return false;
       }
       if (len < 2) {
           return false;
       }
       return true;
    }

  

    /*$RCSfile$ $Revision: 171 $ $Date: 2005-11-22 14:51:07 -0800 (Tue, 22 Nov 2005) $ */

  /**
  * This is a place holder for common utilities used across the javascript validation
  *
  **/


    /*$RCSfile$ $Revision: 171 $ $Date: 2005-11-22 14:51:07 -0800 (Tue, 22 Nov 2005) $ */
    /**
    *  Check to see if fields must contain a value.
    * Fields are not checked if they are disabled.
    * <p>
    * @param form The form validation is taking place on.
    */

    function validateRequired(form, functionName) {
        var isValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
        oRequired = eval('new ' + functionName + '_required()');
        for (x in oRequired) {
            var field = form[oRequired[x][0]];

            if ((field.type == 'hidden' ||
                field.type == 'text' ||
                field.type == 'textarea' ||
                field.type == 'file' ||
                field.type == 'checkbox' ||
                field.type == 'select-one' ||
                field.type == 'password') &&
                field.disabled == false) {

                var value = '';
                // get field's value
                if (field.type == "select-one") {
                    var si = field.selectedIndex;
                    if (si >= 0) {
                        value = field.options[si].value;
                    }
                } else if (field.type == 'checkbox') {
                    if (field.checked) {
                        value = field.value;
                    }
                } else {
                    value = field.value;
                }

                if (trim(value).length == 0) {

                    if (i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oRequired[x][1];
                    isValid = false;
                }
            } else if (field.type == "select-multiple") { 
                var numOptions = field.options.length;
                lastSelected=-1;
                for(loop=numOptions-1;loop>=0;loop--) {
                    if(field.options[loop].selected) {
                        lastSelected = loop;
                        value = field.options[loop].value;
                        break;
                    }
                }
                if(lastSelected < 0 || trim(value).length == 0) {
                    if(i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oRequired[x][1];
                    isValid=false;
                }
            } else if ((field.length > 0) && (field[0].type == 'radio' || field[0].type == 'checkbox')) {
                isChecked=-1;
                for (loop=0;loop < field.length;loop++) {
                    if (field[loop].checked) {
                        isChecked=loop;
                        break; // only one needs to be checked
                    }
                }
                if (isChecked < 0) {
                    if (i == 0) {
                        focusField = field[0];
                    }
                    fields[i++] = oRequired[x][1];
                    isValid=false;
                }
            }
        }
        if (fields.length > 0) {
           focusField.focus();
           alert(fields.join('\n'));
        }
        return isValid;
    }
    
    // Trim whitespace from left and right sides of s.
    function trim(s) {
        return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
    }


//End --> 
