﻿(function() {

    // pour simuler un namespace, on créé un objet vide et on lui affecte les méthodes ensuite
    if (typeof (this.cyim) == "undefined") {
        this.cyim = {};
    }
    // Check si une méthode existe
    if (typeof (this.cyim.globaleval) != "undefined") {
        return;
    }

    // pour créer une méthode dans le namespace cyim :
    // cyim.exempleDeMethode = function(arg1, arg2) {
    //  ... Code de la méthode ...
    // }
    //
    // L'appel de la méthode se fera de cette manière
    // cyim.exempleDeMethode(valeur1, valeur2);
    //

    // Retourne une url sans paramètre
    // ex : http://www.cyim.com/folder1/page1.html?param=value&param2=value2 --> http://www.cyim.com/folder1/page1.html
    // value:String         valeur dans laquelle rechercher
    // withProtocol:String  Si vrai l'url retournée contiendra http://, sinon il sera enlevé
    cyim.urlWithoutParams = function(value, withProtocol) {
        var ret;
        var array = value.match(/(http:\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?/);
        if (array == null)
            ret = value;
        else
            ret = array[0];
        if (typeof (withProtocol) == "undefined" || withProtocol)
            return ret;
        return ret.replace(/(http:\/)?\/?/i, '');
    }

    //Fonction remplaçant eval (multi-navigateur)
    cyim.globaleval = function(script) {
        if (window.execScript) {
            //IE
            //return dojo.eval(script);
            return window.execScript(script);
        } else if (navigator.userAgent.indexOf('KHTML') != -1) {
            //SAFARI like
            window.setTimeout(script, 0);
        } else {
            //autres (FF)
            return window.eval(script);
        }
    };

    // detruit un composant dijit via une requete dojo (dojo.query)
    cyim.destroyDijitWidgetByQuery = function(query) {
        dojo.query(query).forEach(
            function(tag) {
                if (typeof (dijit.byNode(tag)) != "undefined") {
                    dijit.byNode(tag).destroy();
                }
            });
    }

    // Returns the value of a form element (input, select)
    cyim.valueOf = function(tag) {
        if (tag.tagName == "SELECT") {
            return tag.options[tag.selectedIndex].value;
        }
        else if (tag.tagName == "INPUT") {
            return tag.value;
        }
        return "";
    }

    // Hiding label over inputs when focusing
    cyim.registerLabel = function(labelId, inputId) {
        var label = dojo.byId(labelId);
        var input = dojo.byId(inputId);
        if (label != undefined && input != undefined) {
            if (cyim.valueOf(input)) {
                if (label.storedDisplay == null) {
                    label.storedDisplay = dojo.style(label, 'display');
                    dojo.style(label, 'display', 'none');
                }
            }

            dojo.connect(label, 'onclick', function() {
                if (label.storedDisplay == null) {
                    label.storedDisplay = dojo.style(label, 'display');
                    dojo.style(label, 'display', 'none');
                }
                input.focus();
            });
            dojo.connect(input, 'onfocus', function() {
                if (label.storedDisplay == null) {
                    label.storedDisplay = dojo.style(label, 'display');
                    dojo.style(label, 'display', 'none');
                }
            });
            dojo.connect(input, 'onblur', function() {
                if (!cyim.valueOf(input)) {
                    dojo.style(label, 'display', label.storedDisplay);
                    label.storedDisplay = null;
                }
            });
        }
    }

    //Fournit une validation des champs obligatoires d'un formulaire dojo
    //donne le focus au 1er widget obligatoire non saisi
    cyim.validForm = function(formName) {
        var firstwidget = null;
        dojo.forEach(dijit.byId(formName).getDescendants(), function(widget) {
            if (widget.isValid && !widget.isValid()) {
                if (!widget.disabled) {
                    widget.validate(true);
                    widget.focus();
                    if (!firstwidget) {
                        firstwidget = widget;
                    }
                }
            }
        });
        if (firstwidget) {
            firstwidget.focus();
            return false;
        }
        return true;
    }

    //Formate une chaine de caractère
    cyim.unescapeHTML = function(str) {
        var div = document.createElement('div');
        div.innerHTML = str;
        return div.innerHTML.replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&');
    }


})();
