﻿if(typeof(Ascentium)=='undefined')Ascentium={};
Ascentium.WatermarkedTextbox=function(elt,watermark){
    var _this=this;
    var _dirty=false;
    var _valid=true;
    
    this.EmptyClass="WatermarkEmpty";
    this.ErrorClass="WatermarkError";
    this.Watermark=watermark||"Enter value here";
    this.ValidationExpression="[^\s]+";
    
    this.get_element=function(){return elt;};
    this.get_value=function(){
        return _dirty?elt.value:'';
    }
    this.set_value=function(value){
        textBoxMouseDown();
        textBoxFocus();
        elt.value=value;
        textBoxBlur();
    };
    
    this.initialize=initialize;
    this.dispose=dispose;
    this.validate=validate;
    
    function initialize(){
        if(!elt||elt.nodeName!='INPUT')return false;
        setEvents();
        textBoxBlur();
        return _this;
    }
    
    function dispose(){
        setEvents(true);        
        _this=elt=null;
    }
    
    function setEvents(disposing){
        setEvent(window,'onunload',dispose,!disposing);
        setEvent(elt,'onfocus',textBoxFocus,!disposing);
        setEvent(elt,'onmousedown',textBoxMouseDown,!disposing);
        setEvent(elt,'onblur',textBoxBlur,!disposing);
        setEvent(elt,'onkeydown',textBoxKeydown,!disposing);
    }
    
    function textBoxFocus(){
        if(!_dirty&&_valid){
            elt.value='';
            removeClass(elt,[_this.EmptyClass,_this.ErrorClass]);
        }
    }

    function textBoxMouseDown(){
        if(!_valid){
            elt.value='';
            removeClass(elt,[_this.EmptyClass,_this.ErrorClass]);
            _valid=true;
        }
    }
    
    function textBoxBlur(){
        _dirty=_valid&&!!elt.value;
        if(!_dirty){
            elt.value=_this.Watermark;
            elt.className+=' '+_this.EmptyClass;
        }
    }
    
    function textBoxKeydown(e){
        if((e.keyCode||e.which)==13)textBoxBlur();
        if(_valid)return;
        elt.value='';
        removeClass(elt,[_this.EmptyClass,_this.ErrorClass]);
        _valid=true;
    }
    
    function validate(){
        if(_this.ValidationExpression){
            try{
                _valid=new RegExp(_this.ValidationExpression).test(_dirty?elt.value:'');
                if(!_valid){
                    elt.className+=' '+_this.ErrorClass;
                    elt.value=_this.Watermark;
                    elt.focus();
                }
            }catch(e){
                _valid=false;
            }
        }else _valid=true;
        return _valid;
    }
        
    function setEvent(elt,nm,fnc,attaching){
        if(elt&&(elt==window||elt.nodeType)&&typeof(fnc)=='function'){
            if(elt.attachEvent)return elt[attaching?'attachEvent':'detachEvent'](nm,fnc);
            if(elt.addEventListener)return elt[attaching?'addEventListener':'removeEventListener'](nm.replace(/^on/,''),fnc,false);
            elt[nm]=attaching?fnc:null;
        }
    }
    
    function removeClass(elt,classNames){
        if(typeof(classNames)=="String")classNames=[classNames];
        elt.className=elt.className.replace(new RegExp("\\b("+classNames.join("|")+")\\b","g"),"");
    }
}
