/* *********************************************************
** This is the 'validation' class.
** This class has functrions related to the validation.
** ********************************************************/

function validation()
{
    
    /* *************************************************
    ** This is the array variable that holds all the controls which need to be validate
    ** ************************************************/
    this.fields = new Array();
    
    
    
    /* ******************************************************************* 
    ** This function is used to add a form field to the 'fields' array.
    ** This function needs 3 arguments to be passed.
    ** 'ctrl' = The control object of the form.
    ** 'type' = The type of validation( For types please see the switch-case statement in checkValidation() )
    ** 'msg'  = The message to be displayed.
    ** ******************************************************************/
    this.addField = function(ctrl, type, msg)
    {
        if(typeof ctrl != 'undefined')
        {
            totalAddedFields = this.getTotalAddedFields();
            this.fields[totalAddedFields] = new Array();
            this.fields[totalAddedFields]['ctrl'] = ctrl;
            this.fields[totalAddedFields]['type'] = type;            
            this.fields[totalAddedFields]['msg']  = msg;            
        }
    
    };
    
    
    /* *************************************************************
    ** This function validate all the control in the 'fields' array.
    ** And returns the respactive result according to the validation.
    ** ************************************************************/
    this.checkValidation = function()
    {
        if(this.getTotalAddedFields() > 0)
        {
           for(i = 0; i < this.getTotalAddedFields(); i++) 
           {
              ctrl = this.fields[i]["ctrl"];
              msg  = this.fields[i]["msg"];
              switch(this.fields[i]["type"])
              {
                    case 'required_numeric':
                        if(isNaN(ctrl.value))
                        {
                            alert(msg);
                            ctrl.focus();
                            return false;
                        }
                        break;
                    case 'required_email':
                        var regEx = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
                        if(!regEx.test(ctrl.value))
                        {
                            alert(msg);    
                            ctrl.focus();
                            return false;
                        }
                        break;
                    case 'required_phone':
                        if (isNaN(ctrl.value.replace("-","")) || ctrl.value == '')
                        {
                            alert (msg);
                            ctrl.focus();
                            return false;
                        }
                        break;
                    case 'required':
                        if (ctrl.value == '')
                        {
                            alert (msg);
                            ctrl.focus();
                            return false;
                        }
                        break;
                    case 'required_accept':
                        if (!ctrl.checked)
                        {
                            alert (msg);
                            ctrl.focus();
                            return false;
                        }
                        break;
                    case 'not_required_numeric':
                        if(ctrl.value != '')
                        {
                            if(isNaN(ctrl.value))
                            {
                                alert(msg);
                                ctrl.focus();
                                return false;
                            }
                        }
                        break;
                    case 'not_required_phone':
                        if(ctrl.value != '')
                        {
                            if (isNaN(ctrl.value.replace("-","")))
                            {
                                alert (msg);
                                ctrl.focus();
                                return false;
                            }
                        }
                        break;
                    case 'not_required_string':
                        if(ctrl.value != '')
                        {
                            var regEx = /^.+([a-zA-z])$/;
                            if(!regEx.test(ctrl.value))
                            {
                                alert(msg);
                                ctrl.focus();
                                return false;
                            }
                        }
                        break;   
              }

           }
        return true;
        }  
    };
    
    
    /* **************************************************
    ** This function returns total length of 'fields'. 
    ** *************************************************/
    this.getTotalAddedFields = function()
    {
        return this.fields.length;
    };
       
}
// End Validation class


/* **********************************************************************
** This is out of class but this is a required function
** that will be called on submit to validate the form.
** This function should be changed according to the from requirement.
** **********************************************************************/
 function fnValidateQuestionForm(form)
 {
     if(!validate){var validate = new validation();}
     
     validate.addField(form.Name, 'required', 'Please enter your Name');
     validate.addField(form.Phone, 'required_phone', 'Please enter a valid Home Phone number');
	 validate.addField(form.Email, 'required_email', 'Please enter a valid Email'); 
	 validate.addField(form.Question, 'required', 'Please enter your Question');
	 validate.addField(form.captchastring, 'required', 'Please type the characters that appears in the image');	 
     return validate.checkValidation();
 }

