﻿function CheckSession(redirectURL, isDialog, productName) {
    // Perform the ajax post
    try {
        var loc = GetAppUrl();
    }
    catch(Error_)
    {
    if (typeof console != "undefined") { 
            console.log("Error in state" + Error_); 
            }
    }
    var z = false;

    $.ajax({
        type: "POST",
        url: loc + "/Home/CheckSession",
        async: false,
        data: { "redirectURL": redirectURL },
        success: function (msg) {
            z = true;
        },
        error: function (msg) {
            document.location.href = loc + "/Account/LogOff";
        }
    });

    if (z) {

        if (isDialog) {


            document.getElementById('onlineCourse').src = redirectURL;
            $('#dialog_online_course').dialog('open');
            $("#dialog_online_course").dialog('option', 'title', productName);
        }
        else {

            if (productName == "Breakthrough Course Online") {

                // no toolbar
                window.open(redirectURL, "_blank", "left=0,top=0,menubar=0,status=1,toolbar=0,resizable=0,location=0,scrollbars=1,height=" + screen.availHeight + ",width=" + screen.width);
            }
            else {

                //  window.open(redirectURL, "_blank"); // "left=0,top=0,menubar=1,status=1,toolbar=1,resizable=1,location=1,scrollbars=1,height=" + $(window).height() + ",width=" + $(window).width());
                window.open(redirectURL, "_blank", "toolbar=0,left=0,top=0,resizable=1,height=" + screen.availHeight + ",width=" + screen.width);
            }
        }
    }
    //    $.post(loc + "/Home/CheckSession", { "redirectURL": redirectURL },
    //                    function (data) {
    //                        //                        window.open(data.redirectURL, "");
    //                    })
    //    .success(function () { z = true; })
    //    .error(function () {
    //        document.location.href = location + "/Account/LogOff";
    //    })            
}

function CreateLink(redirectURL, shouldCheckSilverlight, isDialog, productName) {

    if (shouldCheckSilverlight) {
        if (CheckSilverlight()) {
            CheckSession(redirectURL, isDialog, productName);
        }
    }
    else {
        CheckSession(redirectURL, isDialog, productName);
    }
}


function GetAppUrl() {
    try {

        var loc = window.location.protocol + "//" + window.location.host;

        var pathArray = window.location.pathname.split('/');

        var path = "";
        for (var i = 1; i <= pathArray.length - 3; i++) {

            if (pathArray[i].length > 0)
                path = path + "/" + pathArray[i];
        }

        return loc + path;
    }
    catch(Error_)
    {
    if (typeof console != "undefined") { 
            console.log("Error in state" + Error_); 
            }
}
}

function FixProtocol(url) {
    
    var pageProtocol = window.location.protocol;  
    var urlPorotocol = url.substring(0, 6);

    if (urlPorotocol == "https:") {       
    if (urlPorotocol != pageProtocol) {           
    return "http" + url.substring(5);
    }
    }
    else {
    if (urlPorotocol != pageProtocol) {         
    return "https" + url.substring(4);
    }
    }
    
    return url;


}

function UpdateProducts() {
    try {

        var loc = GetAppUrl();

        window.location = loc + "/Home/UpdateProducts?returnUrl=" + window.location;
    }
    catch (Error_) {
        if (typeof console != "undefined") {
            console.log("Error in state" + Error_);
        }
    }
}

