﻿
/*
 DataSecurity is used to be for the frequent validation to Email , Password, and some others' input values.
 [Begin]
*/

var DataSecurity = (function(){

        function getPreDefinedErrorMsg(){
           var _preDefinedErrorMsg = {
                    __1 : "Please enter an email to continue.",//emailAddrEmpty
                    __2: "Please enter a valid email format to continue.",//emailAddrFormat
                    __3:"Passwords do not match, please try again.", //emailAddrBad
                    __4:"Please enter a password to continue..",
                    __5:"Please enter a valid password format to continue.",
                    __6:"Please enter a value to continue.",
                    _1:"success"
                 };
            return _preDefinedErrorMsg;
        
        }
       
        function toElement(id){
            return document.getElementById(id);
        }
        
        function trim(str){
            return str.replace(/(^\s*)|(\s*$)/g, "");  
        }
        
        function clearTextBoxValue(){
           for(var i=0;i<arguments.length;i++)
                arguments[i].value = "";
        }
        
        
       var $$ = function(args){
            return args ? Array.apply(null,args):new Array();
        }
        
        function dataSecurity(){
            if(typeof(Function.prototype._addParamsToEventHandler_dataSecurity)=="undefined"){// bind params to the EvnentHandler
                Function.prototype._addParamsToEventHandler_dataSecurity = function(objParams){
                    var objParams = objParams;
                    var fnHandler = this;
                    return function(){
                        fnHandler.call(null,objParams);
                    };
                }
            }
        
        }// constructor
        
        dataSecurity.prototype = {
            /*
            summary: check whether the Email is valid.
            visibility: public.
            parameters:
                obj1-- required. the object of an element.
                fnCallBack-- optional.it's a function pointer and will deal with what should be done afterwards when the email address is not valid.
                                      The fnCallBack will be called with argument(the object which need to be verified),But,under fnCallBack = null,the default action is to
                                      clear the Email while invalid.
            returns:true if valid.otherwise,false.
            */
            IsValidEmailFormat : function(obj){
                var reg= /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
                if(reg.test(trim(obj.value)))
                    return true;
                else{
                  //  if(fnCallBack != null)
                  //      (fnCallBack._addParamsToEventHandler_dataSecurity(obj))();
                 //   else
                  //      clearTextBoxValue(obj);        
                    return false; 
                }
                    
            },
            
            /*
            summary: check whether the Password is valid.
            visibility: public.
            parameters:
                obj-- required. the object of an element.
                fnCallBack-- optional.it's a function pointer and will deal with what should be done afterwards when the email address is not valid.
            returns:true if valid.otherwise,false.
            */
            
            IsValidPasswordFormat : function(obj){
                return true;
            },
            
            IsTheSamePassword : function(obj1,obj2){
                if(obj1.value!=obj2.value){
         /*           if(fnCallBack!=null)
                        fnCallBack();
                    else
                        clearTextBoxValue(obj2,obj1);*/
                    return false;
                  }else
                        return true;
                    
            },
            PasswordConfirmChecked : function(textBox1Id,textBox2Id,isUsingDefaultErrMsg){
                var obj1 = document.getElementById(textBox1Id);
                var obj2 = document.getElementById(textBox2Id);
                
             //   if(!this.IsTheSamePassword(obj1.value ,obj2.value,clearTextBoxValue.bind(null,obj1,obj2)))
             if(!this.IsTheSamePassword(obj1 ,obj2,null))
                      return isUsingDefaultErrMsg == true ? {statusCode:-3,value:getPreDefinedErrorMsg().__3} : -3;
                else
                    return 1;
                    
                
            
            },
               /*
                check whether an email address is validated according to some rules.
               */   
            EmailValidated : function(textBoxId,isUsingDefaultErrMsg){    
                     
                        var objEmailTxtBox = document.getElementById(textBoxId);
                        var emailLen = trim(objEmailTxtBox.value).length;
                        if(emailLen == 0){
                            objEmailTxtBox.focus();
                            return isUsingDefaultErrMsg == true ? {statusCode:-1,value:getPreDefinedErrorMsg().__1} : -1;

                        }
                        if(emailLen>0){
                            if(!this.IsValidEmailFormat(objEmailTxtBox,null/*clearTextBoxValue.bind(null,objEmailTxtBox)*/)){        
                                objEmailTxtBox.focus();
                              return isUsingDefaultErrMsg == true ? {statusCode:-2,value:getPreDefinedErrorMsg().__2} : -2;  
                            }
                            else{
                                return 1;
                            }
                        }

                },
            PasswordValidated : function(textBoxId,isUsingDefaultErrMsg){
                var objPwTxtBox = document.getElementById(textBoxId);
                var pwLen = objPwTxtBox.value.length;
                if(pwLen==0){
                    objPwTxtBox.focus();
                    
                    return isUsingDefaultErrMsg == true ? {statusCode:-4,value:getPreDefinedErrorMsg().__4} : -4;
                }
                if(pwLen>0){
                    if(!this.IsValidPasswordFormat()){
                       objPwTxtBox.focus();
                        return isUsingDefaultErrMsg == true ? {statusCode:-5,value:getPreDefinedErrorMsg().__5} : -5;
                    }else
                        return 1;
                }
                    
            },
            
            // just only check whether the textbox value is empty.
            NormalTextBoxValidated : function(textBoxId,isUsingDefaultErrMsg){
               var objPwTxtBox = document.getElementById(textBoxId);
                var pwLen = trim(objPwTxtBox.value).length;
                if(pwLen==0){
                    objPwTxtBox.focus();
                    return isUsingDefaultErrMsg == true ? {statusCode:-6,value:getPreDefinedErrorMsg().__6} : -6;
                }else
                    return true;   
            },

            
                // public method  
            isValidated : function(objIds){ // objIds = {email:{id:"",errorMsg:""},password:{id:"",errorMsg:""}}
                if(objIds == null){
                        alert("please pass the textbox value you want to validate as a Object to this function!");
                        return false;
                    }
                else{
                       for(var pName in objIds){
                            
                       }
                    }
                }  ///////////////////
 
    }
    
    var DataSecurity = new dataSecurity();
  //  if(typeof target.DataSecurity == "undefined")
  //      target.DataSecurity = DataSecurity;
  //  else
  //      alert("Object 'DataSecurtity'attached unsuccessfully!");
  
  return DataSecurity;
    
})();

/*
 DataSecurity is used to be for the frequent validation to Email , Password, and some other input values.
 [End]
*/
