/*
 * @author: uzzal
 * @class: validator
 * @web: http://uzzal.wordpress.com
 */

var validator={
    show_alert: false,
    object:null,
    setAlert: function(status){
        this.show_alert=status;      
    },
    setTarget: function(obj){
        this.object=obj;
    },
    /*
     *@example: sets the alert message if msg=true.
     */
    showAlert:function(msg){
        if(this.show_alert){
            alert(msg);            
            if(this.object!=null){
                try{
                this.object.select();
                }catch(e){
                this.object.focus();
                }
            }
            this.setAlert(false);
        }
    },

    /* 
     * @example validator.isNull("uzzal"); //returns false;
     * @return if str is null returns true, false otherwise
     */    
    isNull:function(str){
        if(str == null || str.length == 0){
            this.showAlert("A Required 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)){
          this.showAlert("A Required Field is Not a Number");
          return false;
      }else{
          return true;
      }
    },
    
    /*     
     * @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 = /^[a-zA-Z0-9]+:\/\/[^ ]+$/;
        
        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