/**
 * Utility function for retrieving the current selected text from the HTML page
 */
function getSel() {
    var txt = '';
    var foundIn = '';
    if (window.getSelection)
    {
        txt = window.getSelection();
        foundIn = 'window.getSelection()';
    }
    else if (document.getSelection)
    {
        txt = document.getSelection();
        foundIn = 'document.getSelection()';
    }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
        foundIn = 'document.selection.createRange()';
    }
    else return;
    return txt;
}

/**
 * Deselects any selected text in the page
 */
function deselect() {
    if (document.selection) {
        document.selection.empty();
    } else if (window.getSelection) {
        window.getSelection().removeAllRanges();
    }
}

/**
 * Selects an HTML element
 * @param id a string representing the id of the HTML element
 */
function selectElement(id) {
    if (document.selection) {
        var range = document.body.createTextRange();
        range.moveToElementText(document.all[id]);
        range.select();
    } else if (window.getSelection()) {
        var range = document.createRange();
        range.selectNodeContents(document.getElementById(id));
        var selection = window.getSelection();
        selection.removeAllRanges();
        selection.addRange(range);
    }
}

/**
 * Trims a string
 * @param str a string to be trimmed
 */
function trim(str) {
	var sString = str;
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

/**
 * Adds 123dictionar as engine to the list of Firefox engines
 */
function addEngine() {
    if ((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function")) {
        window.sidebar.addSearchEngine(
            "http://www.123dictionar.ro/firefox/123dictionar.src",
            "http://www.123dictionar.ro/firefox/123dictionar.png", "123dictionar", "Language dictionary"
        );
    } else {
        alert("Din pacate, browserul folosit nu este Firefox");
    }
}

/*
function executeScripts(scripts) {
    // loop through the scripts in the order they came in
    var self = this;
    var tmp = "", code = "";
    for(var i = 0; i < scripts.length; i++){
        if(scripts[i].path){ // remotescript
            dojo.io.bind(this._cacheSetting({
                "url": 		scripts[i].path,
                "load":     function(type, scriptStr){
                        dojo.lang.hitch(self, tmp = scriptStr);
                },
                "error":    function(type, error){
                        error._text = type + " downloading remote script";
                        self._handleDefaults.call(self, error, "onExecError", true);
                },
                "mimetype": "text/plain",
                "sync":     true
            }, this.cacheContent));
            code += tmp;
        }else{
            code += scripts[i];
        }
    }

    try{
        // initialize a new anonymous container for our script, dont make it part of this widgets scope chain
        // instead send in a variable that points to this widget, usefull to connect events to onLoad, onUnLoad etc..
        delete this.scriptScope;
        this.scriptScope = new (new Function('_container_', code+'; return this;'))(self);
    }catch(e){
        e._text = "Error running scripts from content:\n"+e.description;
        this._handleDefaults(e, "onExecError", true);
    }
}
*/
