﻿// Name: ArticleInfoDialog.js
// Version: 1.0.0.0
// FileVersion: 1.0.0.0
// -----------------------------------------------------------------------
//  Copyright (C) Peter Bredenberg System. All rights reserved.
// -----------------------------------------------------------------------

// Key press handler
function articleInfoOkButtonKeyPress(e, btn) {
    var kC = (window.event != null) ? event.keyCode : e.keyCode;
    if (kC == 13) {
        stopEventBubbling(e);
        btn.click();
    }
}

// Key press handler
function articleInfoCancelButtonKeyPress(e, btn) {
    var kC = (window.event != null) ? event.keyCode : e.keyCode;
    if (kC == 13) {
        stopEventBubbling(e);
        btn.click();
    }
}

// Show the article info dialog
function showArticleInfoDialog(e, artno) {
    closeAllDialogs();

    if (isLoggedIn()) {
        var indicator = $get('indicator');
        indicator.style.display = 'block';

        positionModalBackground(true);
        centerArticleInfoDialog(true, e.clientY);
        Onyx.Web.Services.OnyxWebService.GetArticleInfo(artno, OnLoadArticlInfoComplete, OnArticlInfoError, OnArticlInfoTimeOut);
    }
    else {
        alert('Du måste vara inloggad för att kunna beställa. Välj \'Logga in\' från menyn ovan.\n\nOm du inte har något konto, är du välkommen att göra en kontoansökan. Välj \'Ansök om konto\' från menyn.');
    }
}

// Center the article info dialog
function centerArticleInfoDialog(show, yPos) {
    var clientWidth = 0, bodyWidth = 0, clientHeight = 0, bodyHeight = 0;
    var dialogWidth = 400, dialogHeight = 420, dialogOffset = 300;

    if (navigator.appName.indexOf("Microsoft") != -1) {
        clientWidth = document.documentElement.clientWidth;
        clientHeight = document.documentElement.clientHeight;
    }

    bodyWidth = document.body.offsetWidth;
    bodyHeight = document.body.offsetHeight;

    var width = clientWidth > bodyWidth ? clientWidth : bodyWidth;

    if (yPos == 0)
        yPos = ((clientHeight > bodyHeight ? clientHeight : bodyHeight) - dialogHeight) / 2;
    else {
        var scrollTop = document.body.scrollTop;
        if (scrollTop == 0) {
            if (window.pageYOffset)
                scrollTop = window.pageYOffset;
            else
                scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
        }
        yPos += scrollTop - dialogOffset;
        if (yPos < 0) yPos = 0;
    }

    var articleInfoDialog = $get('ArticleInfoDialog');
    if (articleInfoDialog != null) {
        articleInfoDialog.style.left = parseInt((width - dialogWidth) / 2) + 'px';
        articleInfoDialog.style.top = parseInt(yPos) + 'px';
    }

    if (show) {
        articleInfoDialog.style.display = 'block';
    }
}

function closeArticleInfoDialog() {
    enableArticleInfoDialog(false);
    clearArticleInfoDialog();

    var dlg = $get('ArticleInfoDialog');
    dlg.style.display = 'none';

    hideModalBackground();
}

function OnArticlInfoTimeOut(result) {
    alert('Tidgränsen för åtgärden har överskridits.');
    closeArticleInfoDialog();
}

function OnArticlInfoError(result) {
    alert(result.get_message());
    closeArticleInfoDialog();
}

function OnLoadArticlInfoComplete(result) {
    if (result != null) {
        var articleInfoArticleNo = $get('ArticleInfoArticleNo');
        articleInfoArticleNo.innerHTML = result.ArticleNo;

        var articleInfoDescription = $get('ArticleInfoDescription');
        articleInfoDescription.innerHTML = result.Description;

        var articleInfoPrice = $get('ArticleInfoPrice');
        articleInfoPrice.innerHTML = result.Price.toFixed(2).toString().replace('.', ',') + ' kr';

        //        var articleInfoContractPrice = $get('ArticleInfoContractPrice');
        //        articleInfoContractPrice.innerHTML = result.Price.toFixed(2).toString().replace('.', ',') + ' kr';

        var articleInfoUnit = $get('ArticleInfoUnit');
        if (result.Unit == null)
            articleInfoUnit.innerHTML = '';
        else
            articleInfoUnit.innerHTML = result.Unit;

        var articleInfoPriceUnit = $get('ArticleInfoPriceUnit');
        articleInfoPriceUnit.innerHTML = result.PriceUnit;

        var articleInfoStock = $get('ArticleInfoStock');
        if (result.Stock.toString().indexOf('.') > 0)
            articleInfoStock.innerHTML = result.Stock.toFixed(1).toString().replace('.', ',');
        else
            articleInfoStock.innerHTML = result.Stock;

        var minQuantityHidden = $get('MinQuantityHidden');
        if (result.MinQuantity.toString().indexOf('.') > 0)
            minQuantityHidden.value = result.MinQuantity.toFixed(1);
        else {
            if (result.MinQuantity == 0)
                minQuantityHidden.value = 1;
            else
                minQuantityHidden.value = result.MinQuantity;
        }

    }
    else
        showArticleMissingError();

    var indicator = $get('indicator');
    indicator.style.display = 'none';

    enableArticleInfoDialog(true);
    setFocusToItemsTextBox();
}

// Notify ScriptManager that this is the end of the script
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

