(function () {
    var BASE_CLASS = 'validate',
        ALPHA = /^[a-zA-Z ]*$/,
        NUM = /^[0-9 ]*$/,
        ALPHANUM = /^[0-9a-zA-Z\., -]*$/,
        FILENAME = /^[^\\\./:\*\?\"<>\|]{1}[^\\/:\*\?\"<>\|]{0,254}$/,
        NAME = /^[0-9a-zA-Z \.,-]*$/,
        PASSWORD = /^[\w\.\?!\*@#\$%]{4,60}$/,
		DATE = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/,
        EMAIL = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,
        form = document.getElementsByTagName('form')[0],
        i,
		button;
	  if($(form.id + '_error'))
        form.error_node = $(form.id + '_error');
    form.validation_nodes = getValidationNodes(form);
	
    for(j=0; j<form.elements.length; j++)
      {
        if(form.elements[j].type=="submit" || form.elements[j].id=="submitbutton")	
	      {
		    button = form.elements[j];
            break;
          }
      }		  
	
	function validateForm(event){ 
        var passed_validation = true,
		target = form;
        for (i = 0; i < target.validation_nodes.groups.length; i++) {
            target.validation_nodes.groups[i].valid = false;
            target.validation_nodes.groups[i].invalid = false;
            target.validation_nodes.groups[i].groupnodes=0;
			target.validation_nodes.groups[i].groupvalid=0;
			}
        for (i = 0; i < target.validation_nodes.inputs.length; i++) {
            if (!target.validation_nodes.inputs[i].validate()) {
                passed_validation = false;
                if (target.validation_nodes.inputs[i].validate.error_node) {
                    //target.validation_nodes.inputs[i].validate.error_node.innerHTML = 'error';
					target.validation_nodes.inputs[i].validate.error_node.removeClassName('hide');
                }
            } else {
                if (target.validation_nodes.inputs[i].validate.error_node) {
                    //target.validation_nodes.inputs[i].validate.error_node.innerHTML = '';
					target.validation_nodes.inputs[i].validate.error_node.addClassName('hide');

                }
            }
        }
        for (i = 0; i < target.validation_nodes.groups.length; i++) {
    /*        if (target.validation_nodes.groups[i].valid === target.validation_nodes.groups[i].invalid) {
                passed_validation = false;
                if (target.validation_nodes.groups[i].error_node) {
                    //target.validation_nodes.groups[i].error_node.innerHTML = 'Group "' + target.validation_nodes.groups[i].tag + '" is incomplete.';
					target.validation_nodes.groups[i].error_node.removeClassName('hide');
                }
            } else {
                if (target.validation_nodes.groups[i].error_node) {
                    //target.validation_nodes.groups[i].error_node.innerHTML = '';
					target.validation_nodes.groups[i].error_node.addClassName('hide');
                }
            }
*/
			

	      showwarning=false;
		  switch(target.validation_nodes.groups[i].groupOpts){
            case 'notempty':
//              alert('notempty,valid:' + target.validation_nodes.groups[i].groupvalid + ',total' + target.validation_nodes.groups[i].groupnodes);
              if (target.validation_nodes.groups[i].groupvalid == 0 && target.validation_nodes.groups[i].groupnodes>0)
			    showwarning=true;
            break;
            case 'onlyone':

              if (target.validation_nodes.groups[i].groupvalid != 1 && target.validation_nodes.groups[i].groupnodes>0) 
			    showwarning=true;
			break;		
            default:
              if (target.validation_nodes.groups[i].groupvalid >0 && target.validation_nodes.groups[i].groupvalid != target.validation_nodes.groups[i].groupnodes)
			  {
                showwarning=true;			  

			  }
            break;		
          }			

            if (showwarning&&target.validation_nodes.groups[i].error_node) {
                    //target.validation_nodes.groups[i].error_node.innerHTML = 'Group "' + target.validation_nodes.groups[i].tag + '" is incomplete.';
					target.validation_nodes.groups[i].error_node.removeClassName('hide');
  				    passed_validation=false;

                }
            else if(!showwarning){
                if (target.validation_nodes.groups[i].error_node) {
                    //target.validation_nodes.groups[i].error_node.innerHTML = '';
					target.validation_nodes.groups[i].error_node.addClassName('hide');
                }
			
			}
        }
        if (!passed_validation) {

            if (target.error_node) {
                target.error_node.innerHTML = 'Please check below for errors';
				//target.error_node.removeClassName('hide');
            } else {
                //alert('There was a problem with your request.  Please correct information provided.');
				return false;
            }
        } else {

        var btn = document.createElement('input');

	btn.type= "button";
    btn.disabled = true;
    btn.value = "Please Wait..";
    btn.className=button.className;
        button.parentNode.insertBefore(btn, button);
        button.style.display = 'none';	
		
		
  		  if (target.error_node) {
                target.error_node.innerHTML = '';
            }
        }
		if(!passed_validation&&event.preventDefault)
          event.preventDefault();


        return passed_validation;
    };
    function getValidationNodes (form) {
        var all_nodes = form.getElementsByTagName('*'),
            i,
            j,
            base_length = BASE_CLASS.length + 1,
            inputs = [],
            groups = [],
            class_names,
            tag_name,
            validation_options;
        for (i = 0; i < all_nodes.length; i++) {
            tag_name = all_nodes[i].tagName.toLowerCase();
            switch (tag_name) {
                case 'input':
                case 'select':
                case 'textarea':
                    class_names = all_nodes[i].className.split(' ');
                    validation_options = [];
                    for (j = 0; j < class_names.length; j++) {
                        if (class_names[j].length > base_length && class_names[j].substring(0, base_length) == BASE_CLASS + '-') {
                            validation_options = class_names[j].substring(base_length).split('-');
                            break;
                        }
                    }
                    if (validation_options.length > 0) {
                        switch (tag_name) {
                            case 'input':
                                switch (all_nodes[i].type) {
                                    case '':
                                    case 'text':								
                                    case 'password':
                                        initText(all_nodes[i], validation_options);
                                        break;
                                    case 'hidden':
                                        initHidden(all_nodes[i], validation_options);
                                        break;											
                                    case 'checkbox':
                                        initCheckbox(all_nodes[i], validation_options);
                                        break;
                                    case 'radio':
                                        initRadio(all_nodes[i], validation_options);
                                        break;
                                }
                                break;
                            case 'select':
                                initSelect(all_nodes[i], validation_options);
                                break;
                            case 'textarea':
                                initText(all_nodes[i], validation_options);
                                break;
                        }
                        inputs.push(all_nodes[i]);
                    }
            }
        }
        function initText (element, options) {
            var i,
                j,
                group_tag,
                group_index,
                is_new_group;
            element.validate = function () {
                var i,
                    return_value,
                    group_flag;
                element.value = element.value.replace(/^\s+/, '').replace(/\s+$/, '');
                if (this.value == '') {
                    group_flag = 'invalid';
                    if (this.validate.req) {
                        return_value = false;
                    } else {
                        return_value = true;
                    }
                } else {
                    group_flag = 'valid';
                    return_value = this.validate.test();
                }
                for (i = 0; i < this.validate.groups.length; i++) {
                    this.validate._form.validation_nodes.groups[this.validate.groups[i]].groupnodes++;			
                    this.validate._form.validation_nodes.groups[this.validate.groups[i]][group_flag] = true;
					if(group_flag == 'valid')
                      this.validate._form.validation_nodes.groups[this.validate.groups[i]].groupvalid++;

                }
                return return_value;
            };
            element.validate._ele = element;
            element.validate._form = form;
            element.validate.groups = [];
            element.validate.error_node = $(element.id + '_error');
            element.validate.req = false;
            element.validate.test = function () {
                if (this.regex) {
                    return this.regex.test(this._ele.value);
                } else {
                    return true;
                }
            };
            for (i = 0; i < options.length; i++) {
                switch (options[i]) {
                    case 'req':
                        element.validate.req = true;
                        break;
                    case 'alpha':
                        element.validate.regex = ALPHA;
                        break;
                    case 'alphanum':
                        element.validate.regex = ALPHANUM;
                        break;
                    case 'filename':
                        element.validate.regex = FILENAME;
                        break;
					case 'name':
                        element.validate.regex = NAME;
                        break;
					case 'date':
                        element.validate.regex = DATE;
                        break;
					case 'num':
                        element.validate.regex = NUM;
                        break;
					case 'email':
                        element.validate.regex = EMAIL;
                        break;
                    case 'phone':
                        element.validate.test = function () {
                            var value = this._ele.value = this._ele.value.replace(/\D+/g, '');
                            return (value.length == 10);
                        };
                        break;
                    case 'ext':
                        element.validate.target = element.id.substring(0, element.id.length - 1);
                        element.validate.test = function () {
                            var value = this._ele.value = this._ele.value.replace(/\D+/g, '');
                            var main_number = document.getElementById(this.target);
                            return (value.length > 0 && main_number.value && main_number.validate());
                        };
                        break;
                    case 'zip':
                        element.validate.test = function () {
                            var value = this._ele.value = this._ele.value.replace(/\D+/g, '');
                            return (value.length == 5 || value.length == 9);
                        };
                        break;
                    case 'pw':
                        element.validate.regex = PASSWORD;
                        break;
                    case 'pw2':
                        element.validate.target = element.id.substring(0, element.id.length - 1);
                        element.validate.test = function () {
                            return (this._ele.value == document.getElementById(this.target).value);
                        };
                        break;
                    default:
                        if (options[i].substring(0, 3) == 'grp') {
                            group_tag = options[i].substring(3);
                            is_new_group = true;
                            for (j = 0; j < groups.length; j++) {
                                if (groups[j].tag == group_tag) {
                                    group_index = j;
                                    is_new_group = false;
                                    break;
                                }
                            }
                            if (is_new_group) {
                                groups.push({
                                    tag : group_tag,
                                    valid : false,
                                    invalid : false,
									groupnodes : 0,
									groupvalid : 0,
									groupOpts : '',
                                    error_node : $('grp' + group_tag + '_error')
                                });
                                group_index = j;
                            }
                            element.validate.groups.push(group_index);
                        }
                        break;
                }
            }
        }
        function initCheckbox (element, options) {
            var i,
                j,
                group_tag,
                group_index,
                is_new_group;
            element.validate = function () {
                var i,
                    return_value,
                    group_flag;
                if (!this.checked) {
                    group_flag = 'invalid';
                    if (this.validate.req) {
                        return_value = false;
                    } else {
                        return_value = true;
                    }
                } else {
                    group_flag = 'valid';
                    return_value = true;
                }
                for (i = 0; i < this.validate.groups.length; i++) {
                  this.validate._form.validation_nodes.groups[this.validate.groups[i]].groupnodes++;
  				  this.validate._form.validation_nodes.groups[this.validate.groups[i]][group_flag] = true;
					if(group_flag == 'valid')
                      this.validate._form.validation_nodes.groups[this.validate.groups[i]].groupvalid++;					
                }
                return return_value;
            };
            element.validate._ele = element;
            element.validate._form = form;
            element.validate.groups = [];
            element.validate.error_node = $(element.id + '_error');
            element.validate.req = false;
            for (i = 0; i < options.length; i++) {
                switch (options[i]) {
                    case 'req':
                        element.validate.req = true;
                        break;
                    default:
                        if (options[i].substring(0, 3) == 'grp') {
                            group_tag = options[i].substring(3);
                            is_new_group = true;
                            for (j = 0; j < groups.length; j++) {
                                if (groups[j].tag == group_tag) {
                                    group_index = j;
                                    is_new_group = false;
                                    break;
                                }
                            }
							

                            if (is_new_group) {
                                groups.push({
                                    tag : group_tag,
                                    valid : false,
                                    invalid : false,
								    groupnodes : 0,
									groupvalid : 0,
									groupOpts : '',
                                    error_node : $('grp' + group_tag + '_error')
                                });
                                group_index = j;
                            }
                            element.validate.groups.push(group_index);
                        }
                        break;
                }
            }
        }
        function initHidden (element, options) {
            var i,
                j,
                group_tag,
                group_index,
				BFonlyone=false,
				BFnotempty=false,
                is_new_group;
            element.validate = function () {
                var i,
                    return_value=true,
                    group_flag='valid';
                
           //     for (i = 0; i < this.validate.groups.length; i++) {
           //         this.validate._form.validation_nodes.groups[this.validate.groups[i]][group_flag] = true;
			//		if(group_flag == 'valid')
            //          this.validate._form.validation_nodes.groups[this.validate.groups[i]][groupvalid]++;
                
                return return_value;
            };
            element.validate._ele = element;
            element.validate._form = form;
            element.validate.groups = [];
            element.validate.error_node = $(element.id + '_error');
            element.validate.req = false;
            for (i = 0; i < options.length; i++) {
                switch (options[i]) {
                    case 'notempty':
                        BFnotempty = true;
                        break;
				    case 'onlyone':
                        BFonlyone = true;
                        break;
					
					default:
                        if (options[i].substring(0, 3) == 'grp') {
                            group_tag = options[i].substring(3);
                            is_new_group = true;
                            for (j = 0; j < groups.length; j++) {
                                if (groups[j].tag == group_tag) {
                                    group_index = j;
                                    is_new_group = false;
                                    break;
                                }
                            }
                            if (is_new_group) {
                                groups.push({
                                    tag : group_tag,
                                    valid : false,
									groupnodes : 0,
									groupvalid : 0,
									groupOpts : '',									
                                    invalid : false,
                                    error_node : $('grp' + group_tag + '_error')
                                });
                                group_index = j;
                            }
                            element.validate.groups.push(group_index);
                        }
                        break;
                }
			if(group_tag&&BFnotempty)
			  groups[group_index].groupOpts='notempty';
			else if(BFonlyone)
			  groups[group_index].groupOpts='onlyone';


            }
        }



        function initRadio (element, options) {
        }
        function initSelect (element, options) {
            var i,
                j,
                group_tag,
                group_index,
                is_new_group;
            element.validate = function () {
                var i,
                    return_value,
                    group_flag;
                element.value = element.value.replace(/^\s+/, '').replace(/\s+$/, '');
                if (this.value == '') {
                    group_flag = 'invalid';
                    if (this.validate.req) {
                        return_value = false;
                    } else {
                        return_value = true;
                    }
                } else {
                    group_flag = 'valid';
                    return_value = this.validate.test();
                }
                for (i = 0; i < this.validate.groups.length; i++) {
				    this.validate._form.validation_nodes.groups[this.validate.groups[i]].groupnodes++;
                    this.validate._form.validation_nodes.groups[this.validate.groups[i]][group_flag] = true;
					if(group_flag == 'valid')
                      this.validate._form.validation_nodes.groups[this.validate.groups[i]].groupvalid++;					
                }
                return return_value;
            };
            element.validate._ele = element;
            element.validate._form = form;
            element.validate.groups = [];
            element.validate.error_node = $(element.id + '_error');
            element.validate.req = false;
            element.validate.test = function (value) {
                if (this.regex) {
                    return this.regex.test(value);
                } else {
                    return true;
                }
            };
            for (i = 0; i < options.length; i++) {
                switch (options[i]) {
                    case 'req':
                        element.validate.req = true;
                        break;
                    default:
                        if (options[i].substring(0, 3) == 'grp') {
                            group_tag = options[i].substring(3);
                            is_new_group = true;
                            for (j = 0; j < groups.length; j++) {
                                if (groups[j].tag == group_tag) {
                                    group_index = j;
                                    is_new_group = false;
                                    break;
                                }
                            }
                            if (is_new_group) {
                                groups.push({
                                    tag : group_tag,
                                    valid : false,
                                    invalid : false,
									groupnodes : 0,
									groupvalid : 0,
									groupOpts : '',
                                    error_node : $('grp' + group_tag + '_error')
                                });
                                group_index = j;
                            }
                            element.validate.groups.push(group_index);
                        }
                        break;
                }
            }
        }
        return {
            inputs : inputs,
            groups : groups
        };
    }
if(form.id!="configure_form")	{
if (form.addEventListener){
  form.addEventListener('submit', validateForm, true); 
} else if (form.attachEvent){
  form.attachEvent('onsubmit', validateForm);
}
}
else{
  var test=validateForm();
  //alert(test);
 return test;
}
})();
