// For the XUL integration we need a reference to the last input element
// having the focus. There does not seem to be an obvious way to obtain
// a reference to the current input element within a <browser> element.
// Therefore we attach event listeners for the focus/blur events that store
// the latest element as a global variable ELEMENT_CURRENT_FOCUS that can be
// access from XUL.

var ELEMENT_CURRENT_FOCUS = null;

// attach event handler for input and textarea elements

function setupEventHandlers() {
    _attachEventHandlerFor('input');
    _attachEventHandlerFor('textarea');
}

function _attachEventHandlerFor(tagname) {
    var arr = document.getElementsByTagName(tagname);

    for (var i=0; i<arr.length; i++) {
        var el = arr[i];
        registerEventListener(el, 'focus', function() { ELEMENT_CURRENT_FOCUS = this;});
        registerEventListener(el, 'blur', function() { ELEMENT_CURRENT_FOCUS = null; });
    }
}


// a portable(!) solution for adding event handler to elements
function registerEventListener(elem, event, func) {
    if (elem.addEventListener) {
        elem.addEventListener(event, func, false);
    } else if (elem.attachEvent) {
        elem.attachEvent("on"+event, func);
    }
}


// used by portlet for installed products
function startProduct(pid) {
    var product_url = HR_URL + "/" + pid;

    if (window.xulCall) {
        window.xulCall("xulStartProduct('" + pid + "','" + product_url + "')");
    }
    else{            
        document.location = product_url;
    }
}


