﻿var waitForProductDownload = false;
var productVersion = -1;

//Sendet einen Request an den WebService, der den Content für das entsprechende Produkt zurückliefert
function requestProduct(productID, withDescription) {
    ALMTools_Webseite.WSProductData.GetProduct(productID, withDescription, callbackProductContent);
    currentPageID = -1;
    currentProductID = productID;
    
    //History
    setHistoryProduct(productID);
}

//result -> ProductConfiguration
function callbackProductContent(result) {
    document.getElementById("Label1").innerHTML = result.Content;

    //Alle Boxen erst ausblenden
    hideBoxes(result.allBoxes);

    //Boxen einblenden
    showBoxes(result.Boxes);

    //Den Seitentitel setzen
    setPageTitle(result.Title);

    
    //HowTo- und Anwenderbericht-Boxen füllen
    document.getElementById("boxhowto_Label3").innerHTML = result.BoxValueHowTo;
    //document.getElementById("boxanwenderberichte_Label3").innerHTML = result.BoxValueUserReviews;

    //Link-Box befüllen
    var links = "";
    //if (result.ID != 3)
    links = "<a href='#' onclick='requestVersions(" + result.ID + ")'>" + result.LblVersions + "</a><br />";

    if (result.hasScreenshots)
        links = links + "<a href='#' onclick='requestScreenshots(" + result.ID + ")'>" + result.LblScreenshots + "</a><br />";

    if (result.isAvailable) {
        if (result.ID == 0 && result.Language == "de" || result.ID == 4 && result.Language == "de")
            links = links + "<a href='#' onclick='requestDownload(" + result.ID + ")'>" + result.LblDownload + " Demoversion </a><br />";
        else if (result.ID == 0 && result.Language == "en")
            links = links + "<a href='#' onclick='requestDownload(" + result.ID + ")'>" + result.LblDownload + " demo version </a><br />";
        else if (result.ID == 3 && result.Language == "en")
            links = links + "<a href='#' onclick='requestDownload(" + result.ID + ")'>" + result.LblDownload + " Openversion </a><br />";
        else if (result.ID == 3 && result.Language == "de") {
            links = links + "<a href='#' onclick='requestDownload(" + result.ID + ")'>" + result.LblDownload + " Openversion </a><br />";
            links = links + "<a href='#' onclick='getSpecificProductSite(" + result.ID + ", 0)'> Benutzerdokumentation </a><br />";
         }
         else if (result.ID == 1 && result.Language == "de" || result.ID == 6 && result.Language == "de") {
            links = links + "<a href='#' onclick='requestDownload(" + result.ID + ")'>" + result.LblDownload + " Freeversion </a><br />";
            links = links + "<a href='#' onclick='getSpecificProductSite(" + result.ID + ", 0)'> Anwenderdokumentation </a><br />";
        }
        else if (result.ID == 1 && result.Language == "en") {

        }
        //else if (result.ID == 1 && result.Language == "en") { //Englische VErsion nicht verfügbar
        // links = links + "<a href='#' onclick='requestDownload(" + result.ID + ")'>" + result.LblDownload + " Freeversion </a><br />";
        // links = links + "<a href='#' onclick='getSpecificProductSite(" + result.ID + ", 0)'> Userdocumentation </a><br />";
        //}
        else
            links = links + "<a href='#' onclick='requestDownload(" + result.ID + ")'>" + result.LblDownload + " Freeversion</a><br />";

        if (!result.isFree) {
            links = links + "<a href='#' onclick='requestBuy(" + result.ID + ")'>" + result.LblBuy + "</a>";
        }
    }

    if (result.ID == 2 && result.Language == "de") {
        links = links + "<a href='#' onclick='requestDownload(" + result.ID + ")'>" + result.LblDownload + " Demoversion </a><br />";
        links = links + "<a href='#' onclick='requestBuy(" + result.ID + ")'>" + result.LblBuy + "</a><br />";
        links = links + "<a href='#' onclick='getSpecificProductSite(" + result.ID + ", 0)'> Softwaredokumentation </a><br />";
     }

    //Workitem Open back to the description page
//    if (result.ID == 3) {
//        var labelsToTranslate = new Array();
//        labelsToTranslate[0] = "t40";
//        ALMTools_Webseite.WSStaticContent.GetTranslation(labelsToTranslate[0], callbackTranslate); 
//        links = links + "<a href='#' onclick='requestProduct(" + result.ID + ", true)'><span id='t40'></span></a><br />";
//    }


    if (!result.hasHowTos)
        document.getElementById("howto").style.display = "none";

    document.getElementById("boxlinks_Label3").innerHTML = links;


    //    if (waitForProductBuy == true) 
    //    {
    //        waitForProductBuy = false;
    //        requestBuy(result.ProductID, getLanguage());
    //    }
    //    if (waitForProductDownload == true) 
    //    {
    //        waitForProductDownload = false;
    //        requestDownload(result.ProductID, getLanguage());
    //    }

}

function requestVersions(productID) {
    //show productpicture/info on the left side
    ALMTools_Webseite.WSProductData.GetProduct(productID, false, callbackProductContent);
    
    //Fill Versiontext
    ALMTools_Webseite.WSProductData.GetVersions(productID, callbackDisplayProductDescription);
    setHistoryVersion(productID);
}

function requestScreenshots(productID) {
    ALMTools_Webseite.WSProductData.GetProduct(productID, false, callbackProductContent);
    ALMTools_Webseite.WSProductData.GetScreenshots(productID, callbackDisplayProductDescription);
    
    setHistoryScreenshot(productID);
}


function callbackDisplayProductDescription(result) {
    if (document.getElementById("lblProductDescription") != null) {      
        if (document.getElementById("lblDownload") != null)
            document.getElementById("lblDownload").style.display = "block";
        document.getElementById("lblProductDescription").innerHTML = result;
    }
    else
        document.getElementById("Label1").innerHTML = result;
}

//Todo! 
function requestDownload(productID) {
    //Beide Funktionen werden gleichzeitig ausgeführt. Dabei kann es zu einem Konflikt führen. Indem der Download Inhalt schon geladen
    //wurde, nur das produkt nicht
    if (document.getElementById("lblProductDescription") == null) {
        waitForProductDownload = true;
        requestProduct(productID, false);
        ALMTools_Webseite.WSStaticContent.getDownload(productID, callbackDisplayProductDownload);
    }
    else {
        ALMTools_Webseite.WSStaticContent.getDownload(productID, callbackDisplayProductDownload);
        currentPageID = 80;
        currentProductID = productID;
    }

    setHistoryDownload(productID);
}

function callbackDisplayProductDownload(result) {

    if (document.getElementById("lblProductDescription") != null) {
        document.getElementById("lblDownload").style.display = "none";
        document.getElementById("lblProductDescription").innerHTML = result;
    }
    else
        document.getElementById("Label1").innerHTML = result;

}

function getSpecificProductSite(productID, specificSiteID) {
    ALMTools_Webseite.WSProductData.GetProduct(productID, false, callbackProductContent);
    ALMTools_Webseite.WSStaticContent.getSpecificProductSite(productID, specificSiteID, callbackDisplayProductDescription);

 }