(function(){

    var Dom = YAHOO.util.Dom;
    var Event = YAHOO.util.Event;
    var Element = YAHOO.util.Element;
    var Lang = YAHOO.lang;
    /**
     * @description Ene class-d validation formtoi holbootoi erunhii oilgoltuudiig tusgana.
     * @namespace SODON.widget
     * @class ValidateForm
     */
    SODON.widget.ValidateForm = function(){
        this.init.apply(this);
    };

    
    SODON.widget.ValidateForm.prototype = {

        /**
         * @description Ene property-d buh shalgah objectuudiig huulah buguud CheckForm class-d null utgatai baina.
         * CheckForm-oos udamshsan classuudad utgiig ugdug.
         * @property {Array} items
         */
        
        items: null,

        /**
         * @description uurchlult oroogui gesen tuluv
         * @property {String} NOTCHANGE
         * @static
         */
        
        NOTCHANGE: "NOT-CHANGE",
        
        /**
         * @description zuv uurchlult orson gesen tuluv
         * @property {String} RIGHTCHANGE
         * @static
         */
        
        RIGHTCHANGE: "RIGHT-CHANGE",

        /**
         * @description buruu uurchlult orson gesen tuluv
         * @property {String} WRONGCHANGE
         * @static
         */
        
        WRONGCHANGE: "WRONG-CHANGE",

        /**
         * @description hooson oruulsan gesen tuluv
         * @property {String} EMPTYCHANGE
         * @static
         */
        
        EMPTYCHANGE: "EMPTY-CHANGE",

        /**
         * @description tailbar gesen tulhuur ug
         * @property {String} DESC
         * @static
         */
        
        DESC: "desc",

        /**
         * @description buruu gesen tulhuur ug
         * @property {String} RIGHT
         * @static
         */
        
        RIGHT: "right",

        /**
         * @description buruu gesen tulhuur ug
         * @property {String} WRONG
         * @static
         */
        
        WRONG: "wrong",

        /**
         * @description hooson gesen tulhuur ug
         * @property {String} EMPTY
         * @static
         */
        
        EMPTY: "empty",

        /**
         * @description
         * @method init
         */
        
        init: function() {},

        /**
         * @description Zuvhun Boolean utga butsaana. items bolgonii check function aas utga huleen avdag.
         * @method check
         * @param Integer i
         * @type Boolean
         */
        
        check: function(i) {
            alert("check");
            var result = this.items[i].check(i);
            if(result == this.RIGHTCHANGE)
                return true;
            else
                return false;
        },

        /**
         * @description Zuvhun Boolean utga butsaana. Buh check function aas utga huleen avdag.
         * @method checkAll
         * @type Boolean
         */
        
        checkAll: function() {

            for ( var i=0; i < this.items.length; i++ ) {
                if ( !this.check(i) ) {
                    return false;
                }
            }
            return true;
        },

         /**
         * @description
         * @method showMsg after onFocus
         * @param {Integer} i
         */
        showMsgFocus: function(i) {
              
            // ug duugui uussen msg-uudiig ustgadag function.
            this._removeMsgs(i);

            
            // establishing new message board with wrong or empty or desc
            this._createMsg(i);
        },

        /**
         * @description
         * @method showMsg after onblur
         * @param i
         */

        showMsgBlur: function(i) {

            // ug duugui uussen msg-uudiig ustgadag function.
            this._removeMsgs(i);

            // for check
            var result = this.items[i].check(i);

            // for setstatus
            if ( result == this.RIGHTCHANGE ) {
//                alert("right");
                this.items[i].status = this.RIGHTCHANGE;
            }
            else if ( result == this.WRONGCHANGE ) {
//                alert("wrong");
                this.items[i].status = this.WRONGCHANGE;
            }
            else if ( result == this.EMPTYCHANGE ) {
//                alert("empty");
                this.items[i].status = this.EMPTYCHANGE;
            }
            // end of setstatus
            
            this._createMsg(i);
        },

        /**
         * @description Ene function ni register button deer darsan tohioldold checkAll function false utga butsaasan
         * tohioldold ajilladag function yum aa.
         * @method showMsgAll
         */
        _showMsgAll: function() {

             for(var i=0; i<this.items.length; i++){
                 this._removeMsgs(i)
                 this._createMsg(i);
             }
        },

        /**
         * @description bukh elementuudiig shalgaad aldaa garval msg uzuulj
          * aldaa garakhgui bol submit khiine dee 
         * @method register
         */
        submitRegister: function() {
           alert(" enter submit register ");
           if ( !this.checkAll() ) {
                this._showMsgAll();
            }
            else {
                document.getElementById('submit').submit();
            }
        },

        /**
         * @description
         * @method _createMsg
         * @param {Integer} i
         */
        _createMsg: function(i) {
                 
            if ( this.items[i].status != this.RIGHTCHANGE) {
                
                var el = this.items[i]["element"];
                
                var newEl = new Element(document.createElement('div'));
                
                var dget = Dom.getRegion(el);
                
                Dom.setStyle(newEl,'position','absolute');
                
                Dom.setStyle(newEl, 'opacity',0.5);
                Dom.setStyle(newEl, 'color','red');
                Dom.setStyle(newEl, 'font-weight','bold');

                if ( this.items[i].status == this.NOTCHANGE) {
                    
                    newEl.set("id", el + "-msg-" + this.DESC);
                    newEl.set("innerHTML", this.items[i]["msg"].desc);
                }
                else if ( this.items[i].status == this.WRONGCHANGE ) {
                                                                         
                    newEl.set("id", el + "-msg-" + this.WRONG);
                    newEl.set("innerHTML", this.items[i]["msg"].wrong);
                }
                else if ( this.items[i].status == this.EMPTYCHANGE ) {

                    newEl.set("id", el + "-msg-" + this.EMPTY);
                    newEl.set("innerHTML", this.items[i]["msg"].empty);
                }

                Dom.insertBefore(newEl, Dom.getFirstChild(Dom.get("body")));

                Dom.setY(newEl, dget.top);
                Dom.setX(newEl, dget.right+10);
            }
        },
        
        /**
         * @description Ene method ni tuhain element-d hargalzaj baigaa buyu delgetsend haragdaj baigaa msg-uudiig
         * ustgah uuregtei
         * @method _removeMsgs
         * @param {Integer} i
         */
        _removeMsgs: function(i) {
               
            var el = this.items[i]["element"];
            if ( this._isCreateMsgDesc(i) ) Dom.get("body").removeChild(Dom.get(el + "-msg-" + this.DESC));
            if ( this._isCreateMsgWrong(i) ) Dom.get("body").removeChild(Dom.get(el + "-msg-" + this.WRONG));
            if ( this._isCreateMsgEmpty(i) ) Dom.get("body").removeChild(Dom.get(el + "-msg-" + this.EMPTY));
        },

        /**
         * @description Desc msg uussen esehiig shalgaj ugdug function.
         * @method _isCreateMsgDesc
         * @param {Integer} i
         * @return Boolean
         */
        _isCreateMsgDesc: function(i) {

            return Dom.get(this.items[i]["element"] + "-msg-" + this.DESC) != null;
        },

        /**
         * @description Wrong msg uussen esehiig shalgaj ugdug function.
         * @method _isCreateMsgWrong
         * @param {Integer} i
         * @return Boolean
         */
        _isCreateMsgWrong: function(i) {

            return Dom.get(this.items[i]["element"] + "-msg-" + this.WRONG) != null;
        },

        /**
         * @description Empty msg uussen esehiig shalgaj ugdug function.
         * @method _isCreateMsgEmpty
         * @param {Integer} i
         * @return Boolean
         */
        _isCreateMsgEmpty: function(i) {

            return Dom.get(this.items[i]["element"] + "-msg-" + this.EMPTY) != null;
        }

    };
})();