/* Merged Plone Javascript file
 * This file is dynamically assembled from separate parts.
 * Some of these parts have 3rd party licenses or copyright information attached
 * Such information is valid for that section,
 * not for the entire composite file
 * originating files are separated by ----- filename.js -----
 */

/* ----- formsubmithelpers.js ----- */
function inputSubmitOnClick(event) {
    if (!event) var event = window.event; // IE compatibility

    if (hasClassName(this, 'submitting')) {
        return confirm(window.form_resubmit_message);
    } else {
        addClassName(this, 'submitting');
    }
    return true;
}

function registerSubmitHandler() {
    var nodes = cssQuery('input[type=submit]');
    for (var i=0; i<nodes.length; i++) {
        var node = nodes[i];
        if (!node.onclick) {
            node.onclick = inputSubmitOnClick;
        }
    }
}
registerPloneFunction(registerSubmitHandler);


/* ----- linkpopper.js ----- */
linkpopperWhiteList = [
    
    new RegExp('^'+window.location.protocol+'//'+window.location.hostname)
];

function isInWhiteList(href){
    for (var i=0; i<linkpopperWhiteList.length; i++){
        if (href.match(linkpopperWhiteList[i])) {
            return true;
        }
    }
    return false;
}

function makeExternalLinksPopup() {
    // Fetch all the a elements in the document.
    var links = document.getElementsByTagName('a');

    // Loop through the a elements in reverse order
    // for speed.
    for (var i = links.length; i != 0; i--) {
        
        // Pull out the element for this iteration.
        var a = links[i-1];
        
        // If the element doesn't have an href, skip it.
        if (!a.href) continue;        
        
        // If the url is not http or https skip it
        if (!a.href.match(/https?:\/\//)) continue;
        
        // If its in the white list skip it
        if (isInWhiteList(a.href)) continue;

        // Set link target to _blank
        a.setAttribute('target', '_blank')
    }
}

registerPloneFunction(makeExternalLinksPopup);

