﻿$(document).ready(function() {

    if (!$.browser.win()) {
        $('#eventClientDownload').hide();
        $('#nonSupportedOS').show();
        $('#BootstrapperSection').hide();
    }
    else // Windows user, check browser:
    {
        //        if (!($.browser.firefox() || $.browser.msie())) {
        //            $('#eventClientDownload').html('<div style="color:red;font-weight:bold;">Your browser is not supported, please use either Internet Explorer or Firefox to install the application</div>');
        //            $('#BootstrapperSection').hide();
        //        }
        //        else {
        Initialize();
        //        }
    }

});

// THE FOLLOWING IS COPIED FROM THE CLICKONCE DEFAULT PAGE AT http://testdrive.nsquaredsolutions.com/EventClient/
runtimeVersion = "3.5.21022";  //"3.5.30729";
checkClient = true;
directLink = "EventClient.application";

function Initialize() {

    if (HasRuntimeVersion(runtimeVersion, false) || (checkClient && HasRuntimeVersion(runtimeVersion, checkClient))) {
        //InstallButton.href = directLink;
        $('#eventClientDownload').show();
        $('#nonSupportedOS').hide();
        $('#BootstrapperSection').hide();
    }
    else {
        $('#eventClientDownload').hide();
        $('#nonSupportedOS').hide();
        $('#BootstrapperSection').show();
    }
}

function HasRuntimeVersion(v, c) {
    var va = GetVersion(v);
    var i;
    var a = navigator.userAgent.match(/\.NET CLR [0-9.]+/g);
    if (c)
        a = navigator.userAgent.match(/\.NET Client [0-9.]+/g);
    if (a != null)
        for (i = 0; i < a.length; ++i)
        if (CompareVersions(va, GetVersion(a[i])) <= 0)
        return true;
    return false;
}

function GetVersion(v) {
    var a = v.match(/([0-9]+)\.([0-9]+)\.([0-9]+)/i);
    return a.slice(1);
}

function CompareVersions(v1, v2) {
    for (i = 0; i < v1.length; ++i) {
        var n1 = new Number(v1[i]);
        var n2 = new Number(v2[i]);
        if (n1 < n2)
            return -1;
        if (n1 > n2)
            return 1;
    }
    return 0;
}
