﻿function leavingGovDomain(url) {
    $('#exitingSite').remove();
    $('body').append('<div id="exitingSite">You are now leaving the website of the Department of Climate Change and Energy Efficiency. The website you are entering is not maintained or funded by the Commonwealth of Australia.</div>');
    var buttons = {
        'Cancel': function() { $('#exitingSite').dialog('close'); },
        'OK': function() { window.location = url; }
    };

    $('#exitingSite').dialog({ title: 'Exit message', width: '500px', modal: true, buttons: buttons });
}

function leavingDCCEE(url) {
    $('#exitingSite').remove();
    $('body').append('<div id="exitingSite">You are now leaving the website of the Department of Climate Change and Energy Efficiency. The website you are entering is not maintained by the Department.</div>');
    var buttons = {
        'Cancel': function() { $('#exitingSite').dialog('close'); },
        'OK': function() { window.location = url; }
    };

    $('#exitingSite').dialog({ title: 'Exit message', width: '500px', modal: true, buttons: buttons });
}

$.expr[':'].nonGov = function(obj, index, meta, stack) {
    var $this = $(obj);
    return nonAuGov($this.attr('href'));
};

$.expr[':'].climateChange = function(obj, index, meta, stack) {
    var $this = $(obj);
    return nonDccee($this.attr('href'));
};

var govRegEx = new RegExp('[A-Za-z0-9]*.?gov.au');
var dcceeRegEx = new RegExp('climatechange.gov.au');
var dcceeRegEx2 = new RegExp('^[A-Za-z0-9\-]+/');

function nonAuGov(href) {
    return (href != undefined && !href.match(govRegEx) && href.indexOf('/') != 0 && !href.match(dcceeRegEx2) && !isIgnore(href));
}

function nonDccee(href) {
    return (href != undefined && !(href.match(dcceeRegEx) || href.indexOf('/') == 0) && !href.match(dcceeRegEx2) && !nonAuGov(href) && !isIgnore(href))
}

function isIgnore(href) {
    return (href.indexOf('javascript') == 0 || href.indexOf('#') == 0 || href.indexOf('~') == 0 || href.indexOf('mailto:') == 0); 
}
