// BROWSER CHECK
function checkBrowser() {
    var version = parseFloat(navigator.appVersion);
    var minVIE = 4.0;
    var minVNS = 4.0;
    var suitableBrowser = false;

    if (navigator.appName.indexOf('Netscape') != -1) {
        if (version >= minVNS) {
            suitableBrowser = true;
        }
    }
    else if ((navigator.appName.indexOf('Microsoft') != -1) && (navigator.appVersion.indexOf('Macintosh') == -1)) {
        if (version >= minVIE) {
            suitableBrowser = true;
        }
    }
    return(suitableBrowser);
}

//	NEW POP-UP WINDOWS: CHECKS FOR MAC IE FOR SIZE INCONTINUITY
function new_openWindow(URL, StWidth, StHeight, scroll, resize, name) {
    //var agent = navigator.userAgent.toLowerCase();
    var dimensionsSTAN = 'width=' + StWidth + ',height=' + StHeight;
    var dimensionsMCIE = 'width=' + (StWidth - 15) + ',height=' + (StHeight - 14);
    var winProperties = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=' + scroll + ',resizable=' + resize;

    if (checkBrowser()) {
        window.open(URL, 'name', winProperties + ',' + dimensionsSTAN).focus();
    }
    else {
        window.open(URL, 'name', winProperties + ',' + dimensionsMCIE).focus();
    }
}

/**
 * Takes a uri and prefixes it with the value contained in the href attribute of the "base" element.
 * Will ensure that the correct amount of fwd slashes are inserted between the two parts of the url. If there
 * is no base element in the source code or the href attribute is empty, the return value will be a uri relative
 * to the root to the domain, e.g. /my/uri.html
 * @param uri String e.g. /path/to/resource.html
 */
function prefixURIWithBaseRef(uri) {

    if (uri.length > 4) {

        var firstFourChars = uri.substring(0, 4).toLowerCase();

        if (firstFourChars == "http") {
            return uri;
        }
    }

    var baseElements = document.getElementsByTagName("base");
    var baseRef = "";
    if (baseElements !== null && baseElements.length > 0) {
        baseRef = baseElements[0].href;
        if(baseRef !== null) {
          baseRef = baseRef.replace(/https?:\/\//, document.location.protocol + "//");
        }
    }

    return generateURL(baseRef, uri);
}


/**
 * Takes a base ref and a uri and creates an absolute url by concatenating them. Will ensure that the
 * correct amount of fwd slashes are inserted between the two parts of the url. If the base ref provided is empty,
 * the function will return a uri relative to the root to the domain, e.g. /my/uri.html
 * @param baseRef String e.g. http://www.airmiles.co.uk/
 * @param uri String e.g. /path/to/resource.html
 */
function generateURL(baseRef, uri) {

    //remove fwd slashes from rhs of baseRef
    var url = baseRef.replace(/\/*$/, '');

    //remove fwd slashes from lhs of uri, then concat the two with a fwd slash in the middle
    return url + "/" + uri.replace(/^\/*/, '');
}
