﻿

function CheckSupported() {
    var tst = Silverlight.supportedUserAgent();
    if (tst) {
        // Do nothing
    }
    else {
        //document.getElementById("content").innerHTML = PromptNotSupported;
    }
    
}
safariEzimerchant();
//this code is dependant on the variable CartURL which is the location of the ezimerchant cart
function safariEzimerchant() {
    if (/safari/i.test(navigator.userAgent)) {
        window.addEventListener("DOMContentLoaded", function () {

            var locatCartURL;
            if (CartURL != null) {
                localCartURL = CartURL;
            } else {
                localCartURL = "https://cfptad.ezimerchant.com/cart/";
            }
            var iframeloadcount = 2;
            var iframe = document.createElement("iframe");
            var frm = document.createElement("form");

            iframe.name = "pleasespareme";
            iframe.setAttribute("style", "display: none;");
            iframe.onload = function () {
                iframeloadcount--;
                if (iframeloadcount == 0) {
                    iframe.parentNode.removeChild(iframe);
                    frm.parentNode.removeChild(frm);
                }
            }
            document.body.appendChild(iframe);

            frm.action = localCartURL;
            frm.method = "POST";
            frm.target = "pleasespareme";

            document.body.appendChild(frm);
            frm.submit();

        }, false);
    }
}
function getSilverlightVersion() {
    var version = 'No Silverlight';
    var container = null;

    try {
        var control = null;
        var product = 'Silverlight';

        if (navigator.userAgent.indexOf("Linux") != -1) {
            product = "Silverlight (Moonlight)";
        }

        if (window.ActiveXObject) {
            control = new ActiveXObject('AgControl.AgControl');
        }
        else {
            if (navigator.plugins['Silverlight Plug-In']) {
                container = document.createElement('div');
                document.body.appendChild(container);
                container.innerHTML = '<embed type="application/x-silverlight" src="data:," />';
                control = container.childNodes[0];
            }
        }
        if (control) {
            if (control.isVersionSupported('4.0')) {
                version = product + '/4.0';
            }
            else if (control.isVersionSupported('3.0')) {
                version = product + '/3.0';
            }
            else if (control.isVersionSupported('2.0')) {
                version = product + '/2.0';
            }
            else if (control.isVersionSupported('1.0')) {
                version = product + '/1.0';
            }
        }
    }
    catch (e) { }
    if (container) {
        document.body.removeChild(container);
    }
    return version;
}

// on serious errors we reload the page via ajax call
function reloadPage() {
    try {
        PageMethods.forceServerSideRefresh(onRefreshSuccess, onRefreshFail, onRefreshTimeout);
        trackPageEvent("Reload Error", "", "", 0);
    }
    catch (err) { }
}

function onRefreshSuccess(response) {
    // prevent an endless loop. - so only reload 3 times or maybe 4 if this logic is wrong
    if (response <= 2) {
        window.location.reload(true);
    }
    else if (response == 3) {

        window.location = getBasePath() + "Default.aspx";
    }
    else {
        window.location = "./DataError.aspx";
    }
}

function getBasePath() {
    var url = window.location.protocol + "//" + window.location.host + "/"; 
    
    if (window.location.path)
        url = url + window.location.path + "/"
        
    return url;
}

function onRefreshFail(exception, ctx, methodName) {
    window.location.reload(false);
}

function onRefreshTimeout() {
    // this is probably not a good idea - might create an endless loop
    //window.location.reload(false);
}

// this tracks which version of silverlight (if any) each user has on hitting the page
try {
    if (!isDebugging) {
        trackPageEvent("Silverlight", "Version Check", getSilverlightVersion(), 0);
    }
} catch (err) { }

// this calculates the user's country using the google jsapi
// hence http://www.google.com/jsapi?key=OURKEYVALUE" must be loaded on this page prior to this script       
try {
    if (!isDebugging) {
        if (google.loader.ClientLocation) {
            userLocation = google.loader.ClientLocation.address.country_code;
        }
    }

} catch (err) {
    try {
    } catch (err) { }
}
