
    /**
     * Check ProfileName Entry
     *
     * Slavisa Miljanovic <slavisa.miljanovic@softserbia.com>
     * @param  objectId
     * @param  messages array
     * @return null - call show/hide helpBalloon box
     */ 
    function checkProfileNameEntry(helpBalloonIcon, objectId, e, url, messages)
    {
        /********** if not allowed key is pressed return from function *************/
        notAllowedKeys = new Array('16', '17', '18', '19', '20', '37', '38', '39', '40', '44', '144', '145',
                                   '45', '33', '34', '35', '36', '113', '115', '119', '120', '122', '123');
        
        var pressedKey = e.keyCode;
        
        for (var i=0; i<notAllowedKeys.length; i++) {
            if (pressedKey == notAllowedKeys[i]) {
                return;
            }
        }
        /********************************* end *************************************/

        // call hide helpBalloon box
        hideBalloonHelper();

        var profileName = $F(objectId);
        var profileNameRegExp = new RegExp(/^([A-Za-z0-9]{6,10})$/);
        testProfileName = profileNameRegExp.test(profileName);
        var ajaxCheckCalled = false;
        var showFromAjax = false;
        
        helpBalloonTitle = messages[0];

        if (profileName.length == 0) {
            // profileName contains no characters
            helpBalloonContent = messages[1];
        } else if (profileName.length < 6) {
            // profileName contains less than 6 characters
            helpBalloonContent = messages[2];
        } else if (testProfileName) {
            // profileName is valid, ajax check for availability
            helpBalloonContent = messages[3];
            
            // url
            url += '/index/check_profilename_availability';
            
            // ajax post params
            var params = '';
            params  = 'profileName=' + profileName;
            params += '&check=true';
            params += '&valid=true'; 

            ajaxCheckCalled = true;
            showFromAjax = true;
            var ajax = new Ajax.Request(url, {method: 'post', parameters: params, onSuccess: function(transport) { afterAjaxProfileNameCheck(transport.responseText, helpBalloonIcon, helpBalloonTitle, messages, showFromAjax, objectId, profileName); }});
            
        } else if (!testProfileName && (profileName.length >= 6) && (profileName.length <= 10)) {
            // profileName isn't valid, but contains appropriate number of characters
            helpBalloonContent = messages[5];
        } else if (profileName.length > 10) {
            // profileName contains more than 10 characters
            helpBalloonContent = messages[6];
        }
        
        if (!ajaxCheckCalled) {
            showBalloonHelper(helpBalloonIcon, helpBalloonTitle, helpBalloonContent, showFromAjax);
        }
    }
    
    functionTimer = null;

    /**
     * Check ProfileName Entry
     *
     * Slavisa Miljanovic <slavisa.miljanovic@softserbia.com>
     * @param  helpBalloon title
     * @param  helpBalloon content
     * @return null - call show helpBalloon box
    */
    function showBalloonHelper(helpBalloonIcon, title, content, showFromAjax, helpBalloonPosition, helpBalloonSize)
    {
        var timerInterval = 500;

        // ********************************************************************* 
        // set position value for HelpBalloon
        // 
        var hbPosition = null;
        if (typeof(helpBalloonPosition) != 'undefined') {
            hbPosition = helpBalloonPosition;
        }

        if (hbPosition == null) {
            // set default value
            hbPosition = 2; // HelpBalloon.POS_BOTTOM_LEFT
        }
        // ********************************************************************* 

        // ********************************************************************* 
        // set size for HelpBalloon
        // 
        if (typeof(helpBalloonSize) != 'undefined') {
            HelpBalloon.Options.prototype = Object.extend(HelpBalloon.Options.prototype,{
                balloonPrefix: helpBalloonPath + helpBalloonSize
		        });
		    }
        // ********************************************************************* 

        helpBalloonObject = new HelpBalloon({
            title: title,
            content: content,
            //autoHideTimeout: 1000,
            icon: $(helpBalloonIcon),
            fixedPosition: hbPosition,
            anchorPosition: 'left 0',
            useEvent: ['mouseover'],
            showEffect: false,
            hideEffect: false
        });
        
        
        
        if (showFromAjax) {
            timerInterval = 0;
        }
        
        
        functionTimer = setTimeout('helpBalloonObject.show();', timerInterval);
    }

    /**
     * Hide Balloon Helper
     *
     * Slavisa Miljanovic <slavisa.miljanovic@softserbia.com>
     * @param  null
     * @return null - call hide helpBalloon box
     */ 
    function hideBalloonHelper()
    {
        if (helpBalloonObject) {
            //helpBalloonObject.hide.bind(helpBalloonObject);
            helpBalloonObject.hide();

            HelpBalloon.Options.prototype = Object.extend(HelpBalloon.Options.prototype,{
                balloonPrefix: helpBalloonPath + helpBalloonDefaultSize
		        });
        }
    }

    /**
     * After Ajax ProfileName Check
     *
     * Slavisa Miljanovic <slavisa.miljanovic@softserbia.com>
     * @param  response (ajax.transport)
     * @param  balloonTitle (helpBalloon title)
     * @param  messages array
     * @return null - call show helpBalloon box
     */ 
    function afterAjaxProfileNameCheck(response, helpBalloonIcon, balloonTitle, messages, showFromAjax, objectId, profileName)
    {
        var profileNameValue = $F(objectId);
        
        if (profileNameValue != profileName) {
            return;
        }

        if (response == '1') {
            // entered profilename is available
            showBalloonHelper(helpBalloonIcon, balloonTitle, messages[3], showFromAjax);
        } else {
            // entered profilename isn't available
            showBalloonHelper(helpBalloonIcon, balloonTitle, messages[4], showFromAjax);
        }
    }

    var oldFunctions = new Array();
    oldFunctions['onfocus'] = new Array();
    oldFunctions['onblur'] = new Array();
    currentFocus = null;

    /**
     * Expand Focus Blur Event
     *
     * Slavisa Miljanovic <slavisa.miljanovic@softserbia.com>
     * @param  array
     * @return null - call hide helpBalloon box
    */ 
    function expandFocusBlurEvent()
    {
        var selects = $$('select');
        var elements = new Array('day', 'month', 'year');

        selects.each(function(element){
            for (var i=0; i<elements.length; i++) {
                if (element.id.indexOf(elements[i]) > -1) {

                    if (!element.onfocus) {
                        // case when onfocus event is empty, only add new code
                        element.onfocus = function() {
                            currentFocus = this.id;
                        }
                    } else {
                        // case when onfocus event isn't empty, append new code
                        oldFunctions['onfocus'][element.id] = element.onfocus; 
                        element.onfocus = function() {
                            currentFocus = this.id;

                            var func = oldFunctions['onfocus'][element.id];
                            str = new String(func);
                            str = str.substring(str.indexOf('{') + 1);
                            str = str.substring(0, str.length - 1);
                            eval(str);
                        }
                    }
                    
                    if (!element.onblur) {
                        // case when onblur event is empty, only add new code
                        element.onblur = function() {
                            currentFocus = null;
                        }
                    } else {
                        // case when onblur event isn't empty, append new code
                        oldFunctions['onblur'][element.id] = element.onblur; 
                        element.onblur = function() {
                            currentFocus = null;
                            var func = oldFunctions['onblur'][element.id];
                            str = new String(func);
                            str = str.substring(str.indexOf('{') + 1);
                            str = str.substring(0, str.length - 1);
                            eval(str);
                        }
                    }
                }
            }
        });
    }
    
    /**
     * Check Focused Select
     *
     * Slavisa Miljanovic <slavisa.miljanovic@softserbia.com>
     * @param  object - form element (this)
     * @return null - call hide helpBalloon box
    */ 
    function checkFocusedSelect(object)
    {
        if (!currentFocus) {
            hideBalloonHelper();
        }
    }


