/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * @author: uzzal
 * @class: validator
 * @web: http://uzzal.wordpress.com
 */

var validator={
    show_alert: false,
    setAlert: function(status){
        this.show_alert=status;      
    },
    /*
     *@example: sets the alert message if msg=true.
     */
    showAlert:function(msg){
        if(this.show_alert){
            alert(msg);
            this.setAlert(false);
        }
    },

    /* 
     * @example validator.isNull("uzzal"); //returns false;
     * @return if str is null returns true, false otherwise
     */    
    isNull:function(str){
        var re = /^[ ]*$/;
        if(str == 'null' || str.length == 0 || str == 'undefined' || re.test(str)){
            this.showAlert("A Rquired Field is Null");
            return true;
        }
        else{return false;}
    },
    
    /*     
     * @example validator.isNumber(123); //returns true;
     * @return if str is a valid number then returns true, false otherwise
     */
    isNumber: function(str){
      if(isNaN(str)){return true;}
      else{
          this.showAlert("A Rquired Field is Not a Number");
          return false;
      }
    },
    
    /*     
     * @example validator.isEmail("myemail@example.com"); //returns true;
     * @return if str is a valid email then returns true, false otherwise
     */
    isEmail:function(str){        
        var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;        
        if (!re.test(str)) {
            this.showAlert("Email Field is Not a Valid Email");
            return false;
        } else {
            return true;
        }

    },
    
    /*     
     * @example validator.isUrl("www.example.com"); //returns true;
     * @return if str is a valid url then returns true, false otherwise
     */
    isUrl:function(str){        
        var re = /^https?:\/\/[^ ]+\.[^ ]+$/;
        
        if (!re.test(str)) {     
            this.showAlert("Url Field is Not a Valid Url");
            return false;
        } else {
            return true;
        }

    },
    
    /*     
     * @example matched two password fields for the same value.
     * @return if password matched then returns true, false otherwise
     */
    isPasswordMatch:function(password,confirm){
        if(this.isNull(password)){            
            return false;
        }
        if(this.isNull(confirm)){            
            return false;
        }
        if(password==confirm){return true;}
        else{
            this.showAlert("Password and Confirm Password Field Did Not Matched");
            return false;
        }
    },

    /*
     * @example Checks for maximum character limit in string
     * @return if str is less then max_limit returns true and false otherwise
     */
    isInMax:function(str,max_limit){
        if(str.length<=max_limit ){
            return true;
        }else{
            this.showAlert("A Field Exceeds The Maximum Character Limit");
            return false;
        }
    },

    /*
     * @example Checks for minimum character limit in string
     * @return if str is over then min_limit returns true and false otherwise
     */
    isInMin:function(str,min_limit){
        if(str.length>=min_limit ){
            return true;
        }else{
            this.showAlert("A Field Below The Mainimum Character Limit");
            return false;
        }
    }
    
}; //end of class validator