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

var validator={
    show_alert: false,
    setAlert: function(status){
        this.show_alert=status;      
    },
    
    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){
        if(str == null || str.length == 0){
            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 = /^[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;
        }
    }
    
}; //end of class validator