﻿var renditionsAddedTitle;
var renditionsSelectionAdded;

var toolTipEmbargo = false;

function EndToolTipEmbargo()
{
    tt_HideInit();
    toolTipEmbargo = false;
}

function Download(enKey)
{
    var rend = "";
    var inputs = document.getElementsByTagName("INPUT");

    var inputSelectedAssetID = GetElement("INPUT", "inputSelectedAssetID");

    var strRend = inputSelectedAssetID.value.split(";");
    for (var i = 0; i < strRend.length; i++) {
        if (strRend[i].length > 0) {
            if (rend != "") {
                rend += ";";
            }
            rend += strRend[i];
        }

    }
    
//    for (var i = 0; i < inputs.length; i++) 
//    {
//        var input = inputs[i];
//        if((input.id.indexOf("ckRend") >= 0 || input.id.indexOf("ckDoc") >= 0) && input.type == "checkbox" && input.checked)
//        {
//            if (rend != "")
//                rend += ";";

//            var rendid = input.id.substring(input.id.indexOf("ckRend") + 6);

//            //rend += input.id;
//            rend += rendid;
//        }
//    }

    //var ret = BasketService.PrepareForDownload(enKey, rend, OnDownloadPrepared, OnAddError);

    if (rend != "") 
    {
        //window.location = "Download.aspx?id=" + rend;
        window.location = "../PublicPages/Basket.aspx?id=" + rend;
    }

    return false;
}

function OnDownloadPrepared(ret) {
    // window.location = "Download.aspx";
    window.location = "Basket.aspx";
}

function AddPDFVersionToSessionBasket(enKey, productSheetID) {
    var ret = BasketService.AddPDFVersionToSessionBasket(enKey, productSheetID, OnAddPDFComplete, OnAddError);

    return false;
}

function OnAddPDFComplete(ret) {
    var _width = 300;
    var _height = 80;
    var iMyWidth;
    var iMyHeight;
    iMyWidth = (window.screen.width / 2) - (_width / 2);
    iMyHeight = (window.screen.height / 2) - (_height / 2 + 20);

    var tipText = renditionsSelectionAdded;
    var tipTitle = renditionsAddedTitle;
    Tip(tipText, TITLE, tipTitle, CLOSEBTN, true, CLICKCLOSE, true, STICKY, 1, FIX, [iMyWidth, iMyHeight], WIDTH, _width, HEIGHT, _height);
}


function AddPressDocumentToSessionBasket(enKey, pressID) {
    var ret = BasketService.AddPressDocumentToSessionBasket(enKey, pressID, OnAddPressDocumentComplete, OnAddError);
    return false;
}

function OnAddPressDocumentComplete(ret) {

    if (ret == null || ret == "")
        return;

    var _width = 300;
    var _height = 80;
    var iMyWidth;
    var iMyHeight;
    iMyWidth = (window.screen.width / 2) - (_width / 2);
    iMyHeight = (window.screen.height / 2) - (_height / 2 + 20);

    var tipText = pressreleaseDocumentAdded;
    var tipTitle = pressreleaseTitle;
    Tip(tipText, TITLE, tipTitle, CLOSEBTN, true, CLICKCLOSE, true, STICKY, 1, FIX, [iMyWidth, iMyHeight], WIDTH, _width, HEIGHT, _height);
}

function AddToMyBasket(enKey) {
    var rend = "";
    var nonRend = "";
    var inputs = document.getElementsByTagName("INPUT");

    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        //        if((input.id.indexOf("ckRend") >= 0 || input.id.indexOf("ckDoc") >= 0) && input.type=="checkbox")
        //        {
        //            if (input.checked)
        //            {
        //                if (rend!="")
        //                    rend += ";";   
        //                rend += input.id;
        //            }
        //            else
        //            {
        //                if (nonRend!="")
        //                    nonRend += ";";   
        //                nonRend += input.id;                
        //            }
        //        }

        var prefixID = "";
        if ((input.id.indexOf("ckRend") >= 0 || input.id.indexOf("ckDoc") >= 0) && input.type == "checkbox") {
            prefixID = input.id.substring(0, input.id.indexOf("ckRend") + 6);
            break;
        }

    }

    var inputSelectedAssetID = GetElement("INPUT", "inputSelectedAssetID");
    var inputAllAssetID = GetElement("INPUT", "inputAllAssetID");

    var strRend = inputSelectedAssetID.value.split(";");
    for (var i = 0; i < strRend.length; i++) {
        if (strRend[i].length > 0) {
            if (rend != "") {
                rend += ";";
            }
            rend += prefixID + strRend[i];
        }

    }

    var strAllRend = inputAllAssetID.value.split(";");
    for (var i = 0; i < strAllRend.length; i++) {
        if (nonRend != "") {
            nonRend += ";";
        }

        if (rend.indexOf(strAllRend[i]) < 0) {
            nonRend += prefixID + strAllRend[i];
        }
    }

    var ret = BasketService.AddToSessionBasket(enKey, rend, nonRend, OnAddRenditionsComplete, OnAddError);

    return false;
}

function UnCheckAll() {
    var inputs = document.getElementsByTagName("INPUT");

    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        if (input.type == "checkbox") {
            input.checked = false;
        }
    }
    return false;
}

function OnAddRenditionsComplete(ret) {
    if (ret == null || ret == "")
        return;

    var inputs = document.getElementsByTagName("INPUT");

    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        if ((input.id.indexOf("ckRend") >= 0 || input.id.indexOf("ckDoc") >= 0) && input.type == "checkbox") {
            if (ret.indexOf(input.id) >= 0) {
                input.checket = true;
            }
            else {
                input.checked = false;
            }
        }
    }

    var _width = 300;
    var _height = 80;
    var iMyWidth;
    var iMyHeight;
    iMyWidth = (window.screen.width / 2) - (_width / 2);
    iMyHeight = (window.screen.height / 2) - (_height / 2 + 20);

    var tipText = renditionsSelectionAdded;
    var tipTitle = renditionsAddedTitle;
    toolTipEmbargo = true;
    Tip(tipText, TITLE, tipTitle, CLICKCLOSE, true, CLOSEBTN, true, STICKY, 1, FIX, [iMyWidth, iMyHeight], WIDTH, _width, HEIGHT, _height);
}

function OnAddError(result) {
    //alert("Error: " + result.get_message());
}

function AddToBasket(enKey) {
    var rend = "";
    var inputs = document.getElementsByTagName("INPUT");

    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        if ((input.id.indexOf("ckRend") >= 0 || input.id.indexOf("ckDoc") >= 0) && input.type == "checkbox" && (input.checked && !input.disabled)) {
            if (rend != "")
                rend += ";";

            rend += input.id;
        }
    }

    if (rend != "") {
        var ret = BasketService.AddToBasket(enKey, rend, AddToBasket_OnComplete);
    }
    return true;
}


function RemoveAssets(enKey) {
    var rend = "";
    var inputs = document.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        if (input.type == "checkbox" && input.checked == false) {
            if (rend != "")
                rend += ";";
            rend += input.id;
        }
    }
    var ret = BasketService.RemoveFromBasket(enKey, rend, RemoveAssets_OnComplete);
    return false;
}

function RemoveAssets_OnComplete(ret) {
    // window.location = "AdvancedDownload.aspx";
}

function RemoveFrombasket(enKey) {
    var rend = "";
    var inputs = document.getElementsByTagName("INPUT");

    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        if (input.type == "checkbox" && input.checked) {
            if (rend != "")
                rend += ";";
            rend += input.id;
        }
    }
    if (rend != "") {
        var ret = BasketService.RemoveFromBasket(enKey, rend, RemoveFrombasket_OnComplete);
    }
    return true;
}

function RemoveFrombasket_OnComplete(ret) {
    if (ret != null || ret != "")
        return;
    document.getElementById("<%= divBut.ClientID %>").style.visibility = 'hidden';
}

function AddToBasket_OnComplete(ret) {
    var splitResults = ret.split(";");

    for (var i = 0; i < splitResults.length; i++) {
        var input = document.getElementById(splitResults[i]);
        if (input != null) {
            input.disabled = true;
            input.checked = true;
        }
        else {
            input.disabled = false;
        }
    }
    var _width = 300;
    var _height = 80;
    var iMyWidth;
    var iMyHeight;
    iMyWidth = (window.screen.width / 2) - (_width / 2);
    iMyHeight = (window.screen.height / 2) - (_height / 2 + 20);
    var tipText = renditionsSelectionAdded;
    var tipTitle = renditionsAddedTitle;
    toolTipEmbargo = true;
    Tip(tipText, TITLE, tipTitle, CLOSEBTN, true, CLICKCLOSE, true, STICKY, 1, FIX, [iMyWidth, iMyHeight], WIDTH, _width, HEIGHT, _height);
}

function GetElement(tagName, id) {
    var inputs = document.getElementsByTagName(tagName);
    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        if (input.id.indexOf(id) >= 0) {
            return input;
        }
    }
}

function ClickSelectAllAssetsAllPages(objChekAll) {
    var inputSelectedAssetID = GetElement("INPUT", "inputSelectedAssetID");
    var inputAllAssetID = GetElement("INPUT", "inputAllAssetID");


    if (objChekAll.checked) {
        inputSelectedAssetID.value = inputAllAssetID.value;
    } else {
        inputSelectedAssetID.value = "";
    }
    RenderCheckBoxStatus();
}

function ClickSelectAllAssetsCurrentPage(objChekAll) {
    var inputSelectedAssetID = GetElement("INPUT", "inputSelectedAssetID");

    var inputs = document.getElementsByTagName("INPUT");

    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];

        if (input.id.indexOf("ckRend") >= 0 && input.type == "checkbox" && !input.disabled) {
            if (input.getAttribute("isoriginal") == "1") {
                var rendID = input.id.substring(input.id.indexOf("ckRend") + 6);
                if (objChekAll.checked) {
                    input.checked = true;
                    if (inputSelectedAssetID.value.indexOf(rendID + ";") < 0) {
                        inputSelectedAssetID.value += rendID + ";"
                    }
                }
                else {
                    input.checked = false;
                    inputSelectedAssetID.value = inputSelectedAssetID.value.replace(rendID + ";", "");
                }
            }
        }
    }
}

function RenderCheckBoxStatus() {
    var inputSelectedAssetID = GetElement("INPUT", "inputSelectedAssetID");

    var inputs = document.getElementsByTagName("INPUT");

    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        if (input.id.indexOf("ckRend") >= 0 && input.type == "checkbox" && !input.disabled) {
            var rendID = input.id.substring(input.id.indexOf("ckRend") + 6);
            if (inputSelectedAssetID.value.indexOf(rendID + ";") >= 0) {
                input.checked = true;
            } else {
                input.checked = false;
            }
        }
    }
}

function ClickCheckRend(objCheckRend) {
    var inputSelectedAssetID = GetElement("INPUT", "inputSelectedAssetID");

    var rendID = objCheckRend.id.substring(objCheckRend.id.indexOf("ckRend") + 6);
    if (objCheckRend.checked) {
        if (inputSelectedAssetID.value.indexOf(rendID + ";") < 0) {
            inputSelectedAssetID.value += rendID + ";";
        }
    } else {
        var checkAllAsset = GetElement("INPUT", "ckAllAssetAllPages");
        checkAllAsset.checked = false;
        inputSelectedAssetID.value = inputSelectedAssetID.value.replace(rendID + ";", "");
    }

}

