﻿function FormulaResultPopup(elm) {
    if (elm.value.length>0 && elm.value.substring(0,1)=='=') {
    
        // Alleen tonen als er een B2B-account geselecteerd is
        if (bid=='') {
            return false;
        }
    
        _blnFormulaIsValid=false;
        FormulaPopupResult_Parse(elm.value.substring(1,1000));
    } else {
        _blnFormulaIsValid=false;
        resetFormulaPopupView();
    }
}

function resetFormulaPopupView() {
    if (_btnIsFormulaPopupVisible) {
        $get('divFade').style.display='none';
        $get('divFormulaResultPopup').style.display='none';
        //$get('divSearchArea').style.background="url('images/main/SearchBackground_Plain.png')";
        $get('txtSearch').style.backgroundColor='white';
        $get('divSearchArea').style.backgroundRepeat='no-repeat';
        _btnIsFormulaPopupVisible=false;
    }
}

function closeFormulaResultPopup() {
    resetFormulaPopupView();
}

function FormulaPopupResult_Parse(result) {
    var strHTML='';
    var blnIsOdd=true;
    var strTRStyle='';

    var blnInNumber=true;
    var strNumber='';
    var blnInArtCode=false;
    var strArtCode='';
    
    var strCharType='';
    var strChar='';
    
    for (i=0;i<result.length;i++) {
            strChar = result.substr(i,1);
            switch (strChar) {
                case "0":
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                    strCharType='Number';
                    break;
                case "-":
                    strCharType='Separator';
                    break;
                case "x":
                case "*":
                case "X":
                    strCharType='Multiply';
                    break;
                case " ":
                    strCharType='Skip';
                    break;
                default:
                    strCharType='Other';
                    break;
            }
            
            if (blnInArtCode) {
                if (strCharType=='Number') {
                    strArtCode += strChar;
                }
                if (strCharType=='Separator') {
                    strArtCode += strChar;
                }
            }

            if (blnInNumber) {
                if (strCharType=='Number') {
                    strNumber += strChar;
                }
                if (strCharType=='Multiply') {
                    blnInNumber = false;
                    blnInArtCode = true;
                }
            }
    }
    
    _strFormulaNumber=strNumber;
    _strFormulaArtCode=strArtCode;

    strHTML += '<div style="background-color:white;padding:0px;"><table cellspacing="0" cellpadding="0" style="width:584px;margin-left:8px;margin-right:8px;background-color:white;" border="0">';

    strHTML += '<tr><td colspan="3"><b>' + _ML("Zoeken.Versneld invoeren") + '</b></td></tr>';
    strHTML += '<tr><td colspan="3">&nbsp;</td></tr>';
    strHTML += '<tr>';
    strHTML += '<td colspan="3" style="text-align:left;"><span style="color:red;font-size:20pt;">' + strNumber + '</span>';
    strHTML += '<span style="color:black;font-size:12pt;margin-left:10px;margin-right:10px;">x</span>';
    strHTML += '<span style="color:red;font-size:20pt;">' + strArtCode + '</span></td>';
    strHTML += '</tr>';
    strHTML += '<tr><td colspan="3" id="tdFormulaDescription">&nbsp;</td></tr>';

    strHTML += '</table></div>';
    
    $get('divFormulaResultContent').innerHTML=strHTML;
    //IE
    if (document.body.clientHeight) {
        $get('divFade').style.height=document.body.clientHeight.toString() + 'px';
    }
    //FF
    if (document.height) {
        $get('divFade').style.height=document.height.toString() + 'px';
    }
    $get('divFormulaResultPopup').style.display='block';
    $get('divSearchArea').style.background='';
    $get('txtSearch').style.backgroundColor='#f0f0f0';
    $get('divFade').style.display='block';

    _btnIsFormulaPopupVisible=true;
    
    ShopWebService.GetItemInfo(sid, strArtCode, AsyncDescriptionFetch_callback, generic_callbackerror);
}

function AsyncDescriptionFetch_callback(result) {
    var strHTML='';
    if (result==null) {
        $get('tdFormulaDescription').innerHTML='&nbsp;';
    } else {
        strHTML += '<hr>';
        strHTML += '<div style="text-align:right;width:576px;"><img style="border:1px solid #c0c0c0;" src="skins/" + _shopid + "/stock_' + result.VoorraadIndicatie + '.png" /></div>';
        strHTML += '<div style="font-weight:bold;font-size:14pt;">' + result.ArtikelGroep + '</div>';
        strHTML += '<div style="font-size:12pt;">' + result.Description + '</div>';
        strHTML += '<div><img src="' + _thumbnailStub + result.ImageName + '.jpg" alt="" /></div>';
        strHTML += '<hr>';
        strHTML += '<div style="font-size:9pt;color:#808080;">' + result.BigDescription + '</div>';

        $get('tdFormulaDescription').innerHTML=strHTML;

        _blnFormulaIsValid=true;
    }
}
