﻿// JScript File

var myTimeout = 3000;

var getConn = null;
var url = 'xml.http.asp';

var submitInputName1 = 'saveChanges1';
var submitInputName2 = 'saveChanges2';

var myScreenX = screen.width;
var myScreenY = screen.height;

var myBrowser = navigator.appName;

var extReqStatus;

function bodyOnLoad() {
    focusOnLogin ();
    setTimeout('msgDivHide()', myTimeout);
}

if (window.XMLHttpRequest) {
    getConn = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
    getConn = new ActiveXObject("Microsoft.XMLHTTP");
}

// asünkroonsete päringute jaoks eraldi objekt
if (window.XMLHttpRequest) {
    getConnA = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
    getConnA = new ActiveXObject("Microsoft.XMLHTTP");
}

function noXmlHttp() {
    alert("Your browser does not support XMLHTTP!");
}

function xmlHttpError(myText) {
 
    //alert('Error: "' + myText + '" occurred!');
    showMessage(141);
        return;
}

/* abiinfo kuvamine */
function showHelp(showText, e) {

    var myX = 0;
    var myY = 0; 

    if (myBrowser == 'Microsoft Internet Explorer')
    {
        myX = document.documentElement.scrollLeft + window.event.clientX;
        myY = document.documentElement.scrollTop + window.event.clientY;
    }
    else
    {
        myX = e.pageX;
        myY = e.pageY;
    }  

    myHelpDiv = document.getElementById('jsHelpDiv');

    if (myHelpDiv != null && showText.length > 0)
   {
        myHelpDiv.innerHTML = showText;

        showX = (myX + 10);
        showY = (myY + 25);

        scrHeight = (document.body.clientHeight);
        sTop = document.documentElement.scrollTop;
        // Chrome ja Safari ei toeta documentElement-i
        if (sTop == 0) {
            sTop = document.body.scrollTop;
        }

        if (myBrowser == 'Microsoft Internet Explorer') {
            minBottom = 120
        }
        else {
            minBottom = 230
        }

        if (((scrHeight + sTop) - showY) < minBottom) {
            showY = ((scrHeight + sTop) - minBottom);
        }

        myHelpDiv.style.left = showX + 'px';
        myHelpDiv.style.top = showY + 'px';
        myHelpDiv.style.display = 'block';
        myHelpDiv.style.visibility = 'visible';
   }  

}

function hideHelp ()
{

    myHelpDiv = document.getElementById('jsHelpDiv');
   
    if (myHelpDiv != null)
   {
        myHelpDiv.innerHTML = '';
        myHelpDiv.style.visibility = 'hidden';
        myHelpDiv.style.display = 'none';
   }  
}

/* TecComi adminile e-maili saatmine */
function sendTecComEMail(xLocalCode, xTecComCode, xName) {

    if (getConn && xLocalCode && xTecComCode && xName) {

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=TecComEmail&myLocalCode=' + encodeURI(xLocalCode) + '&myTecComCode=' + encodeURI(xTecComCode) + '&myName=' + encodeURI(xName));

        if (getConn.readyState == 4) {

            if (getConn.status == 200) {

                alert(getConn.responseText);
            }
            else {

                alert(getConn.responseText);
            }
        }
    }
}

/* pildi kuvamine */
function showImage(productId, e) {

    var myX = 0;
    var myY = 0;

    if (myBrowser == 'Microsoft Internet Explorer') {
        myX = document.documentElement.scrollLeft + window.event.clientX;
        myY = document.documentElement.scrollTop + window.event.clientY;
    }
    else {
        myX = e.pageX;
        myY = e.pageY;
    }

    // asünkroonsete pildipäringute jaoks eraldi objekt
    if (window.XMLHttpRequest) {
        getConnX = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        getConnX = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (getConnX) {

        // Asynchronous query
        getConnX.open('post', url, true);
        //getConnX.timeout = 1000;
        getConnX.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConnX.send('myRequest=showImage&myId=' + productId);

        getConnX.onreadystatechange = function() {
            showImageResponse(myX, myY);
        }
    }
}

function showImageResponse(myX, myY) {

    imageDiv = document.getElementById('jsImageDiv');

    if (imageDiv) {

        if (getConnX.readyState == 4) {

            if (getConnX.status == 200) {

                /* hakkab pihta */
                imageDiv.innerHTML = '';
                imageDiv.innerHTML = getConnX.responseText;

                getConnX = null;

                // image maxheight = 200
                showX = (myX + 10);
                showY = (myY + 25);

                scrHeight = (document.body.clientHeight);
                sTop = document.documentElement.scrollTop;
                // Chrome ja Safari ei toeta documentElement-i
                if (sTop == 0) {
                    sTop = document.body.scrollTop;
                }

                if (myBrowser == 'Microsoft Internet Explorer') {
                    minBottom = 240
                }
                else {
                    minBottom = 350
                }

                //targetDiv.innerHTML = ((scrHeight + sTop) - showY);
                if (((scrHeight + sTop) - showY) < minBottom) {
                    showY = ((scrHeight + sTop) - minBottom);
                }

                imageDiv.style.left = showX + 'px';
                imageDiv.style.top = showY + 'px';
                imageDiv.style.display = 'block';
                imageDiv.style.visibility = 'visible';

            }
        }
    }
}

function hideImage() {

    imageDiv = document.getElementById('jsImageDiv');

    if (imageDiv) {

        imageDiv.innerHTML = '';
        imageDiv.style.visibility = 'hidden';
        imageDiv.style.display = 'none';
    }
}

/* pildi kuvamine */
function showBigImage(productId, productCode) {

    targetDiv = document.getElementById('jsShowImageDiv');
    targetDivImages = document.getElementById('jsShowImageDivAll');
    
    targetDivText = document.getElementById('jsShowImageDivText');
    targetDivImage = document.getElementById('jsShowImageDivImage');

    if (productCode && targetDivText) {
        targetDivText.innerHTML = productCode;
    }

    // toote kõikide piltide kuvamine
    if (getConn && targetDiv && targetDivImages) {

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=showImages&myId=' + productId);

        if (getConn.readyState == 4) {

            if (getConn.status == 200) {

                targetDivImages.innerHTML = getConn.responseText;

            }
        }
    }

    // pildi otsimine
    if (getConn && targetDiv && targetDivImage) {

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=showImagePath&myId=' + productId);

        if (getConn.readyState == 4) {

            if (getConn.status == 200) {

                var sTop = document.documentElement.scrollTop;

                // Chrome ja Safari ei toeta documentElement-i
                if (sTop == 0) {
                    sTop = document.body.scrollTop;
                }

                targetDiv.style.top = (sTop + 5) + 'px';

                //targetDiv.style.background = "url(" + encodeURI(getConn.responseText) + ") no-repeat center #dddddd";
                targetDivImage.innerHTML = '<img src="' + encodeURI(getConn.responseText) + '" alt="" />';
                targetDiv.style.display = 'block';
                targetDiv.style.visibility = 'visible';

            }
        }
    }
}

/* väikese pildi valides suure pildi muutmine*/
function changeBigImage(imageId) {

    targetDivImage = document.getElementById('jsShowImageDivImage');

    if (targetDivImage && imageId) {

        targetDivImage.innerHTML = '<img src="product.image.asp?id=' + imageId + '" alt="" />';
    }
}

function hideBigImage() {

    targetDiv = document.getElementById('jsShowImageDiv');

    if (targetDiv) {

        targetDiv.style.visibility = 'hidden';
        targetDiv.style.display = 'none';
    }

}

/* TecComi päring */
function orderTecCom(mySupplierInput, mySupplierCodeInput, myFile, myMsg) {

    ExtReqDiv = document.getElementById('jsExtReqDiv');
    ExtReqIcoDiv = document.getElementById('jsExtReqIcoDiv');
    ExtReqTextDiv = document.getElementById('jsExtReqTextDiv');
    ExtReqAdditionalTextDiv = document.getElementById('jsExtReqAdditionalTextDiv');

    xSupplierInput = document.getElementById(mySupplierInput);
    xSupplierCodeInput = document.getElementById(mySupplierCodeInput);
    xOrderFile = document.getElementById(myFile);

    // DIVi kuvamine ja päringu käivitamine
    if (getConnA && ExtReqDiv && ExtReqIcoDiv && ExtReqTextDiv && ExtReqAdditionalTextDiv && xSupplierInput && xSupplierCodeInput && xOrderFile && myMsg) {

        ExtReqIcoDiv.innerHTML = '<img src="/gfx/ico.loading.gif" alt="" />'
        ExtReqTextDiv.innerHTML = myMsg;
        ExtReqAdditionalTextDiv.innerHTML = '';

        var sTop = document.documentElement.scrollTop;

        // Chrome ja Safari ei toeta documentElement-i
        if (sTop == 0) {
            sTop = document.body.scrollTop;
        }

        ExtReqDiv.style.top = (sTop + (myScreenY / 2) - 80) + 'px';

        ExtReqDiv.style.display = 'block';
        ExtReqDiv.style.visibility = 'visible';

        // Asynchronous query
        getConnA.open('post', url, true);
        getConnA.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConnA.send('myRequest=TecComOrder&mySupplier=' + xSupplierInput.value + '&mySupplierCode=' + xSupplierCodeInput.value + '&myFile=' + xOrderFile.value);

        getConnA.onreadystatechange = function () {
            orderTecComResponse();
        };
    }
}

/* TecComi päringu vastus */
function orderTecComResponse() {

    extReqStatus = getConnA.readyState;

    ExtReqIcoDiv = document.getElementById('jsExtReqIcoDiv');
    ExtReqTextDiv = document.getElementById('jsExtReqTextDiv');
    ExtReqAdditionalTextDiv = document.getElementById('jsExtReqAdditionalTextDiv');

    if (ExtReqIcoDiv && ExtReqTextDiv && ExtReqAdditionalTextDiv) {

        if (getConnA.readyState == 4) {

            if (getConnA.status == 200) {

                responseText = getConnA.responseText;

                if (responseText.split('|')[0] == 'yes') {
                    myIco = '<img src="/gfx/btn.accept.png" alt="" />';
                    ExtReqAdditionalTextDiv.innerHTML = responseText.split('|')[2];
                }
                else if (responseText.split('|')[0] == 'no') {
                    myIco = '<img src="/gfx/btn.noimage.png" alt="" />';
                }
                else if (responseText.split('|')[0] == 'err') {
                    myIco = '<img src="/gfx/ico.warning.png" alt="" />';
                }
                else {
                    myIco = '<img src="/gfx/ico.warning.png" alt="" />';
                    responseText = '0|Error';
                }

                ExtReqIcoDiv.innerHTML = myIco;
                ExtReqTextDiv.innerHTML = responseText.split('|')[1];
            }
            else {
                ExtReqIcoDiv.innerHTML = '<img src="/gfx/btn.noimage.png" alt="" />';
                ExtReqTextDiv.innerHTML = "Error: Status code - " + getConnA.status;
            }

            setTimeout('hideTecComRequest("/supplier.orders.asp")', 5000);
        }
    }
}

/* TecComi päring */
function checkTecCom(productId, productCode) {

    ExtReqDiv = document.getElementById('jsExtReqDiv');
    ExtReqIcoDiv = document.getElementById('jsExtReqIcoDiv');
    ExtReqTextDiv = document.getElementById('jsExtReqTextDiv');
    ExtReqAdditionalTextDiv = document.getElementById('jsExtReqAdditionalTextDiv');

    // DIVi kuvamine ja päringu käivitamine
    if (getConnA && ExtReqDiv && ExtReqIcoDiv && ExtReqTextDiv && ExtReqAdditionalTextDiv && productId && productCode) {

        ExtReqIcoDiv.innerHTML = '<img src="/gfx/ico.loading.gif" alt="" />'
        ExtReqTextDiv.innerHTML = productCode;
        ExtReqAdditionalTextDiv.innerHTML = '';

        var sTop = document.documentElement.scrollTop;

        // Chrome ja Safari ei toeta documentElement-i
        if (sTop == 0) {
            sTop = document.body.scrollTop;
        }

        ExtReqDiv.style.top = (sTop + (myScreenY / 2) - 80) + 'px';

        ExtReqDiv.style.display = 'block';
        ExtReqDiv.style.visibility = 'visible';

        // Asynchronous query
        getConnA.open('post', url, true);
        getConnA.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConnA.send('myRequest=TecComRequest&myId=' + productId);

        getConnA.onreadystatechange = function() {
            checkTecComResponse();
        };
    }
}

/* TecComi päringu vastus */
function checkTecComResponse() {

    extReqStatus = getConnA.readyState;

    ExtReqIcoDiv = document.getElementById('jsExtReqIcoDiv');
    ExtReqTextDiv = document.getElementById('jsExtReqTextDiv');
    ExtReqAdditionalTextDiv = document.getElementById('jsExtReqAdditionalTextDiv');

    if (ExtReqIcoDiv && ExtReqTextDiv && ExtReqAdditionalTextDiv) {

        if (getConnA.readyState == 4) {

            if (getConnA.status == 200) {

                responseText = getConnA.responseText;

                if (responseText.split('|')[0] == 'yes') {
                    myIco = '<img src="/gfx/btn.accept.png" alt="" />';
                    ExtReqAdditionalTextDiv.innerHTML = responseText.split('|')[2];
                }
                else if (responseText.split('|')[0] == 'no') {
                    myIco = '<img src="/gfx/btn.noimage.png" alt="" />';
                }
                else if (responseText.split('|')[0] == 'err') {
                    myIco = '<img src="/gfx/ico.warning.png" alt="" />';
                }
                else {
                    myIco = '<img src="/gfx/ico.warning.png" alt="" />';
                    responseText = '0|Error';
                }

                ExtReqIcoDiv.innerHTML = myIco;
                ExtReqTextDiv.innerHTML = responseText.split('|')[1];
            }
            else {
                ExtReqIcoDiv.innerHTML = '<img src="/gfx/btn.noimage.png" alt="" />';
                ExtReqTextDiv.innerHTML = "Error: Status code - " + getConnA.status;
            }

            setTimeout('hideTecComRequest()', 5000);
        }
    }
}

/* TecComi DIVi sulgemine*/
function hideTecComRequest(URL) {

    ExtReqDiv = document.getElementById('jsExtReqDiv');

    if (ExtReqDiv != null && extReqStatus == 4) {

        ExtReqDiv.style.visibility = 'hidden';
        ExtReqDiv.style.display = 'none';

        if (URL) {
            window.location = URL;
        }
    }
}

/* uue tarnija lisamine */
function addSupplier () {

    targetDiv = document.getElementById('suppliersDiv');

    if (getConn != null && targetDiv != null) {

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=addSupplier');

        if (getConn.readyState == 4) {

            if (getConn.status == 200) {
                targetDiv.innerHTML = getConn.responseText;
            }
            else {
                xmlHttpError(getConn.statusText);
            }
        }
    }
    else {
        noXmlHttp();
    }
}

/* uue tarnija allahindluse lisamine */
function addSupplierDiscount (myId) {

    targetDiv = document.getElementById('discountsDiv');
    mainDiv = document.getElementById('suppliersDiv');

    if (getConn != null && targetDiv != null) {

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=addSupplierDiscount&myId=' + myId);

        if (getConn.readyState == 4) {

            if (getConn.status == 200) {
                targetDiv.innerHTML = getConn.responseText;
                showSuppliers();
            }
            else {
                xmlHttpError(getConn.statusText);
            }
        }
    }
    else {
        noXmlHttp();
    }
}

/* tarnijate kuvamine */
function showSuppliers () {

    targetDiv = document.getElementById('suppliersDiv');

    if (getConn != null && targetDiv != null) {

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=showSuppliers');

        if (getConn.readyState == 4) {

            if (getConn.status == 200) {
                targetDiv.innerHTML = getConn.responseText;
            }
            else {
                xmlHttpError(getConn.statusText);
            }
        }
    }
    else {
        noXmlHttp();
    }
}
/* tarnija kustutamine */
function showSupplierDiscounts (myId) {

    targetDiv = document.getElementById('discountsDiv');

    if (getConn != null && targetDiv != null) {

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=supplierDiscounts&myId=' + myId);

        if (getConn.readyState == 4) {

            if (getConn.status == 200) {
                targetDiv.innerHTML = getConn.responseText;
            }
            else {
                xmlHttpError(getConn.statusText);
            }
        }
    }
    else {
        noXmlHttp();
    }
}

/* tarnija kustutamine */
function deleteSupplier (myId) {

    targetDiv = document.getElementById('suppliersDiv');

    if (getConn != null && targetDiv != null) {

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=delSupplier&myId=' + myId);

        if (getConn.readyState == 4) {

            if (getConn.status == 200) {
                targetDiv.innerHTML = getConn.responseText;
            }
            else {
                xmlHttpError(getConn.statusText);
            }
        }
    }
    else {
        noXmlHttp();
    }
}

/* tarnija kustutamine */
function deleteSupplierDiscount(myId) {

    targetDiv = document.getElementById('discountsDiv');

    if (getConn != null && targetDiv != null) {

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=delSupplierDiscount&myId=' + myId);

        if (getConn.readyState == 4) {

            if (getConn.status == 200) {
                targetDiv.innerHTML = getConn.responseText;
                showSuppliers();
            }
            else {
                xmlHttpError(getConn.statusText);
            }
        }
    }
    else {
        noXmlHttp();
    }
}

/* tarnija salvestamine */
function saveSupplier (myId) {

    myDiv = document.getElementById('suppliersDiv');
    myCodeLocal = document.getElementById('CodeLocal' + myId);
    myCodeTec = document.getElementById('CodeTec' + myId);
    myName = document.getElementById('Name' + myId);
    myImgPath = document.getElementById('ImgPath' + myId);
    myExt = document.getElementById('Ext' + myId);
    myTextTec = document.getElementById('TextTec' + myId);

    if (getConn && myDiv && myCodeLocal && myCodeTec && myName && myImgPath && myExt) {

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=saveSupplier&myId=' + myId + '&myCodeLocal=' + encodeURI(myCodeLocal.value) + '&myCodeTec=' + encodeURI(myCodeTec.value) + '&myName=' + encodeURI(myName.value) + '&myImgPath=' + encodeURI(myImgPath.value) + '&myExt=' + myExt.value + '&myTextTec=' + encodeURI(myTextTec.value));

        if (getConn.readyState == 4) {

            if (getConn.status == 200) {
                myDiv.innerHTML = getConn.responseText;
                setTimeout('msgDivHide()', myTimeout);
            }
            else {
                xmlHttpError(getConn.statusText);
                myDiv.innerHTML = myDiv.innerHTML;
            }
        }
    }
    else {
        noXmlHttp();
    }

}

/* tarnija salvestamine */
function saveSupplierDiscount(myId) {

    myDiv = document.getElementById('discountsDiv');
    myGroup = document.getElementById('MyGroup' + myId);
    myDiscount = document.getElementById('Discount' + myId);

    if (getConn && myDiv && myGroup && myDiscount) {

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=saveSupplierDiscount&myId=' + myId + '&myGroup=' + myGroup.value + '&myDiscount=' + encodeURI(myDiscount.value));

        if (getConn.readyState == 4) {

            if (getConn.status == 200) {
                myDiv.innerHTML = getConn.responseText;
                setTimeout('msgDivHide()', myTimeout);
            }
            else {
                xmlHttpError(getConn.statusText);
                myDiv.innerHTML = myDiv.innerHTML;
            }
        }
    }
    else {
        noXmlHttp();
    }

}

/* ostukorvi rea koguse aktiivseks muutmine */
function sCartQuantityFocus (myInput)
{

    myControl = document.getElementById(myInput);

    myControl.select();
    myControl.style.textAlign = 'left';

}

function sCartQuantityKeyUp (myUser, myParams, myId)
{

    if (event.keyCode == 13)
    {

        sCartQuantityChange (myUser, myParams, myId)
    
    } 

}

function sCartQuantityChange(myUser, myParams, myId) {

    myOldControl = document.getElementById('sCartOldQuantity' + myId);
    myNewControl = document.getElementById('sCartQuantity' + myId);

    myOldValue = myOldControl.value;
    myNewValue = myNewControl.value;
    myPostValue = 0;

    if (isFinite(myNewValue) != true || isFinite(myOldValue) != true) {

        myNewControl.className = 'quantityError';
        myNewControl.focus();

        showMessage(28);
        return;

    }
    else {

        myNewValue = parseFloat(myNewValue);
        myNewValue = myNewValue.toFixed(0);

        myNewControl.value = myNewValue;
        myNewControl.style.textAlign = 'right';
        myNewControl.className = 'quantity';

        myPostValue = myNewValue - myOldValue;

    }

    myParams = myParams.replace(/###/gi, myPostValue);

    actionInCart(myUser, myParams);
}

/* peale dropdowni väärtuse muutmist teate kuvamine */
function selectOnChange (myInput)
{

    myControl = document.getElementById(myInput);
    myCValue = myControl.value;
    myRValue = '';

    if (myCValue == 7) {
        myRValue = '101';
    }
    else if (myCValue == 46) {
        myRValue = '155';
    }

    if (getConn != null && myControl != null)
   {
   
        if (myControl.name == 'myCartDeliveryType')
        {
            myTarget = document.getElementById('shoppingCartMsg');

            if (myTarget != null)
            {

                getConn.open('post', url, false);
                getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                getConn.send('myRequest=getMessage&myId=' + myRValue);

                if (getConn.readyState == 4)
                {

                    if (getConn.status == 200)
                    {
                        if (myCValue == 7 || myCValue == 46)
                        {
                            myTarget.innerHTML = getConn.responseText;
                        }
                        else
                        {
                            myTarget.innerHTML = '&nbsp;';
                        } 
                        
                    }

                }
                
            }
        }
   
   } 

}

/* teadete kuvamine */
function showMessage(myId) {

    targetDiv = document.getElementById('jsMsgDiv');

    if (targetDiv != null) {

        if (getConn != null) {
            getConn.open('post', url, false);
            getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            getConn.send('myRequest=showMessageText&myId=' + myId);

            if (getConn.readyState == 4) {

                if (getConn.status == 200) {
                    //targetDiv.innerHTML = getConn.responseText;
                    //targetDiv.style.visibility = 'visible';

                    //setTimeout('msgDivHide()', myTimeout);
                    alert(getConn.responseText);
                }
            }
        }
    }
}

/* ekraani laiuse kontroll */
function checkScrWidth ()
{

    if (getConn != null)
    {
   
        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=setScreenWidth&width=' + myScreenX);

        if (getConn.readyState == 4)
        {

            if (getConn.status == 200)
            {
                window.location.reload();
            }
            else
            {
                xmlHttpError (getConn.statusText);
            }
        }
    }
    else
    {
        noXmlHttp ();
    }

}

/* toote kustutamine */
function deleteProduct (myId)
{

    messageDiv = document.getElementById('jsMsgDiv');
    targetDiv = document.getElementById('productsDiv'); 

    if (getConn != null && messageDiv != null && targetDiv != null)
    {

        document.body.style.cursor = 'wait';

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=detailsDeleteSubData&myId=' + myId);
        if (getConn.readyState == 4)
        {

            if (getConn.status == 200)
            {
                targetDiv.innerHTML = '';
                messageDiv.innerHTML = getConn.responseText;
                setTimeout('msgDivHide()', myTimeout);
            }
            else
            {
                xmlHttpError (getConn.statusText);
            }
        }

        document.body.style.cursor = 'default';

    }
    else
    {
        noXmlHttp ();
    }

}

/* toote andmete muutmine */
function saveProductDetails (myId) {

    myDiv = document.getElementById('productsDiv');
    myName = document.getElementById('name' + myId);
    mySupplier = document.getElementById('supplier' + myId);
    myPrice = document.getElementById('price' + myId);
    myPath = document.getElementById('imagepath' + myId);
    myCodes = document.getElementById('productAlternativeCodes' + myId);

    myCodeCell = document.getElementById('td[code][' + myId + ']');
    myNameCell = document.getElementById('td[name][' + myId + ']');
    mySupplierCell = document.getElementById('td[supplier][' + myId + ']');
    myPriceCell = document.getElementById('td[price][' + myId + ']');

    if (getConn != null && myDiv != null && myName != null && mySupplier != null && myPrice != null && myPath != null && myCodes != null)
    {

        myPriceValue = myPrice.value;
        myPriceValue = myPriceValue.replace(/,/gi, '.');

        if (isFinite(myPriceValue) != true)
        {

            myPrice.className = 'inputError';
            myPrice.focus();

                showMessage (28);

            return;

        }
        else
        {
            myPriceValue = parseFloat(myPriceValue);
            myPriceValue = myPriceValue.toFixed(0);
        }

        /* andmete salvestamine */   
        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=detailsSaveSubData&myId=' + myId + '&myName=' + encodeURIComponent(myName.value) + '&mySupplier=' + encodeURIComponent(mySupplier.value) + '&myPrice=' + encodeURI(myPriceValue) + '&myPath=' + encodeURI(myPath.value));
        if (getConn.readyState == 4)
        {

            if (getConn.status == 200) {
                myPrice.value = myPriceValue;
                myPrice.className = 'inputNoError';
            
                myNameCell.innerHTML = myName.value;
                mySupplierCell.innerHTML = mySupplier.value;
                //myPriceCell.innerHTML = myPriceValue + ' kr';
                /*
                myDiv.innerHTML = getConn.responseText;
                setTimeout('msgDivHide()', myTimeout);
                */ 
            }
            else {
                xmlHttpError (getConn.statusText);
            }
        }

        /* analoogtoodete salvestamine */
        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=saveProductAlternatives&myId=' + myId + '&productAlternativeCodes=' + encodeURI(myCodes.value));
        if (getConn.readyState == 4)
        {

            if (getConn.status == 200) {
                myDiv.innerHTML = getConn.responseText;
                setTimeout('msgDivHide()', myTimeout);
            }
            else {
                xmlHttpError (getConn.statusText);
            }
        }

        if (myCodeCell != null) {

            /* andmete ja piltide uuesti kuvamine */
            getConn.open('post', url, false);
            getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            getConn.send('myRequest=productDetailsByCode&myCode=' + myCodeCell.innerHTML);

            if (getConn.readyState == 4) {

                if (getConn.status == 200) {
                    myDiv.innerHTML = getConn.responseText;
                }
                else {
                    xmlHttpError(getConn.statusText);
                }
            }
        }

    }
    else
    {
        noXmlHttp ();
    }

}

/* analoogtoodete salvestamine */
function saveAlternatives (myId)
{

    messageDiv = document.getElementById('jsMsgDiv');
    myCodes = document.getElementById('productAlternativeCodes' + myId);

    if (getConn != null && messageDiv != null && myCodes != null)
    {
   
        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=saveProductAlternatives&myId=' + myId + '&productAlternativeCodes=' + encodeURI(myCodes.value));

        if (getConn.readyState == 4)
        {

            if (getConn.status == 200)
            {
                messageDiv.innerHTML = getConn.responseText;
                setTimeout('msgDivHide()', myTimeout);
            }
            else
            {
                xmlHttpError (getConn.statusText);
            }
        }
    }
    else
    {
        noXmlHttp ();
    }

}

/* bännerite uuendamine*/
function refreshBanners ()
{

    targetDiv = document.getElementById('bannerDiv');

    if (getConn != null && targetDiv != null)
    {

        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=refreshBanners');

        if (getConn.readyState == 4)
        {

            if (getConn.status == 200)
            {
                targetDiv.innerHTML = getConn.responseText;
            }
            else
            {
                xmlHttpError (getConn.statusText);
            }
        }
    }
    else
    {
        noXmlHttp ();
    }
}

/* bänneri järjekorra muutmine */
function changeBannerOrder (myId, myValue)
{

    targetDiv = document.getElementById('bannersListDiv');

    if (getConn != null && targetDiv != null && myValue != null)
    {
   
        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=changeBannerOrder&myId=' + myId + '&myValue=' + myValue);

        if (getConn.readyState == 4)
        {

            if (getConn.status == 200)
            {
                targetDiv.innerHTML = getConn.responseText;
                refreshBanners ();
            }
            else
            {
                xmlHttpError (getConn.statusText);
            }
        }
    }
    else
    {
        noXmlHttp ();
    }
}

/* bänneri muutmine */
function changeBannerKeyUp (myId, myInput)
{

    if (event.keyCode == 13)
    {

        changeBanner (myId, myInput)

    } 

}

function changeBanner (myId, myInput)
{

    targetDiv = document.getElementById('bannersListDiv');
    myValue = document.getElementById(myInput).value;

    if (getConn != null && targetDiv != null && myValue != null)
    {
   
        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=changeBanner&myId=' + myId + '&myValue=' + myValue);

        if (getConn.readyState == 4)
        {

            if (getConn.status == 200)
            {
                targetDiv.innerHTML = getConn.responseText;
                refreshBanners (); 
            }
            else
            {
                xmlHttpError (getConn.statusText);
            }
        }
    }
    else
    {
        noXmlHttp ();
    }
}

/* bänneri kustutamine */
function deleteBanner (myId)
{

    targetDiv = document.getElementById('bannersListDiv');

    if (getConn != null && targetDiv != null)
    {
   
        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=delBanner&myId=' + myId);

        if (getConn.readyState == 4)
        {

            if (getConn.status == 200)
            {
                targetDiv.innerHTML = getConn.responseText;
                refreshBanners (); 
            }
            else
            {
                xmlHttpError (getConn.statusText);
            }
        }
    }
    else
    {
        noXmlHttp ();
    }
}

/* nö alainfo kuvamine */
function subData (myId, myAction)
{

    targetBody = document.getElementById('subDataBody' + myId);
    targetTd = document.getElementById('subDataTd' + myId);
    targetTr = document.getElementById('dataRow' + myId);

    if (getConn != null && targetBody != null && targetTd != null) {

        if (targetBody.className == 'subDataBody') {

            getConn.open('post', url, false);
            getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            getConn.send('myRequest=' + myAction + '&myId=' + myId);

            if (getConn.readyState == 4) {

                if (getConn.status == 200) {

                    targetTd.innerHTML = '<img src="gfx/btn.zoom.out.png" alt="" />'; 
                    targetBody.className = 'subDataBodyVisible';
                    if (targetTr != null) {

                        targetTr.className = 'selectedRow';
                    }

                    if (myBrowser == 'Microsoft Internet Explorer') {

                        var myTempDiv = document.createElement('div');
                        myTempDiv.innerHTML = '<table><tbody class="subDataBodyVisible" style="border: 1px solid #990000;" id="subDataBody' + myId + '">' + getConn.responseText + '</tbody></table>';
                        targetBody.parentNode.replaceChild(myTempDiv.firstChild.firstChild, targetBody);
                    }
                    else {

                        targetBody.innerHTML = getConn.responseText;
                    }
                }
                else {

                    xmlHttpError (getConn.statusText);
                }
            }

        }
        else {

            targetTd.innerHTML = '<img src="gfx/btn.zoom.in.png" alt="" />';
            targetBody.className = 'subDataBody';
            if (targetTr != null) {

                targetTr.className = '';
            }

            if (myBrowser == 'Microsoft Internet Explorer') {

                var myTempDiv = document.createElement('div');
                myTempDiv.innerHTML = '<table><tbody class="subDataBody" id="subDataBody' + myId + '"></tbody></table>';
                targetBody.parentNode.replaceChild(myTempDiv.firstChild.firstChild, targetBody);
            }
            else {

                targetBody.innerHTML = '';
            }
        }
   } 
}

/* toote koodi järgi otsimine */
function searchProductCode (myUser, myInput) {

    fromInput = document.getElementById(myInput);
    targetDiv = document.getElementById('productsDiv');

    if (myInput == 'productSearchInputTop') {

        secondInput = document.getElementById('productSearchInput');
    }
    else {

        secondInput = document.getElementById('productSearchInputTop');
    }  

    if (secondInput != null) {

        secondInput.value = fromInput.value;
    } 

    if (targetDiv != null) { 

        if (getConn != null) {

            getConn.open('post', url, false);
            getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            getConn.send('myRequest=productsByCode&myUser=' + myUser + '&myCode=' + fromInput.value);

            if (getConn.readyState == 4)
            {

                if (getConn.status == 200) {

                    targetDiv.innerHTML = getConn.responseText;
                }
                else {

                    xmlHttpError (getConn.statusText);
                }
            }
        }
        else {

            noXmlHttp ();
        }
    }
}

/* toote koodi järgi otsimine */
function searchProductCodeOrig (myUser, myInput) {

    fromInput = document.getElementById(myInput);
    targetDiv = document.getElementById('productsDiv');

    if (targetDiv != null) {

        if (getConn != null) {

            getConn.open('post', url, false);
            getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            getConn.send('myRequest=productsByCodeOrig&myUser=' + myUser + '&myCode=' + fromInput.value);

            if (getConn.readyState == 4) {

                if (getConn.status == 200) {

                    targetDiv.innerHTML = getConn.responseText;
                }
                else {

                    xmlHttpError(getConn.statusText);
                }
            }
        }
        else {

            noXmlHttp();
        }
    }
}

/* tootehalduses koodi järgi otsimine */
function searchProductDetails (myInput)
{

    fromInput = document.getElementById(myInput);
    targetDiv = document.getElementById('productsDiv');

    if (fromInput.value.length > 3)
    { 
        if (getConn != null && targetDiv != null)
        {
        
            document.body.style.cursor = 'wait';
        
            getConn.open('post', url, false);
            getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            getConn.send('myRequest=productDetailsByCode&myCode=' + fromInput.value);

            if (getConn.readyState == 4)
            {

                if (getConn.status == 200)
                {
                    targetDiv.innerHTML = getConn.responseText;
                }
                else
                {
                    xmlHttpError (getConn.statusText);
                }
            }
        }
        else
        {
            noXmlHttp ();
        }

        document.body.style.cursor = 'default';

    }

}

/* lehe ümbersuunamine */
function redirPage (myTarget, myParams)
{

    location.href = '' + myTarget + '?' + myParams;

}

/* ostukorvi lisamine */
function addToCart (myUser, myParams)
{

    targetDiv = document.getElementById('shoppingCartDiv');

    if (getConn != null && targetDiv != null)
    {
   
        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=addToCart&myUser=' + myUser + '&' + myParams);

        if (getConn.readyState == 4)
        {

            if (getConn.status == 200)
            {
                targetDiv.innerHTML = getConn.responseText;
            }
            else
            {
                xmlHttpError (getConn.statusText);
            }
        }
    }
    else
    {
        noXmlHttp ();
    }
}

/* ostukorvi toimingud */
function actionInCart (myUser, myParams)
{

    targetDiv = document.getElementById('shoppingCartDiv');

    if (getConn != null && targetDiv != null)
    {
   
        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=actionInCart&myUser=' + myUser + '&' + myParams);

        if (getConn.readyState == 4)
        {
            if (getConn.status == 200)
            {
                targetDiv.innerHTML = getConn.responseText;
            }
            else
            {
                xmlHttpError (getConn.statusText);
            }
        }

    }
    else
    {
        noXmlHttp ();
    }

}

/* ostukorvist kustutamine */
function delFromOrders (myId)
{

    targetDiv = document.getElementById('newOrdersDiv');

    if (getConn != null && targetDiv != null)
    {
        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=delFromOrders&myId=' + myId);

        if (getConn.readyState == 4)
        {
            if (getConn.status == 200)
            {
                targetDiv.innerHTML = getConn.responseText;
            }
            else
            {
                xmlHttpError (getConn.statusText);
            }
        }

    }
    else
    {
        noXmlHttp ();
    }

}

function divHide(myDiv) { 

    myHideDiv = document.getElementById(myDiv);

    if (myHideDiv != null) {

        myMsgDiv.style.visibility = 'hidden';
    
    }

}

/* teadete kuvamise lõpetamine */
function msgDivHide() {

    myMsgDiv = document.getElementById("msgDiv");
    myJsMsgDiv = document.getElementById("jsMsgDiv");

    if (myMsgDiv != null) {
        myMsgDiv.style.visibility = 'hidden';
    }

    if (myJsMsgDiv != null) {
        myJsMsgDiv.style.visibility = 'hidden';
    }
}

/* fookus kasutajanimele */
function focusOnLogin ()
{
        userNameInput = document.getElementById('username');
        searchTopInput = document.getElementById('productSearchInputTop');
        
        if (userNameInput != null)
        { 
            userNameInput.focus();
        }

        if (searchTopInput != null)
        { 
            searchTopInput.focus();
        }

}

/* TecComi tarnija koodi tõmbamine */
function getTecComSupplierCode(myId) {

    xSelect = document.getElementById(myId);
    xInput = document.getElementById('mySupplierCode');
    xLabel = document.getElementById('mySupplierCodeLabel');
    xBtn = document.getElementById('mySupplierButton');

    if (xSelect && xInput && xLabel && xBtn) {

        xValue = xSelect.value;

        if (getConn) {

            getConn.open('post', url, false);
            getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            getConn.send('myRequest=TecComSupplierCode&myCode=' + xValue);

            if (getConn.readyState == 4) {

                if (getConn.status == 200) {
                    xInput.value = getConn.responseText;
                    xLabel.innerHTML = getConn.responseText;

                    if (getConn.responseText == '') {
                        xBtn.style.visibility = 'hidden';
                        }
                    else {
                        xBtn.style.visibility = 'visible';
                        }
                }
                else {
                    xmlHttpError(getConn.statusText);
                }
            }


        }
    }
}

/* kui on midagi muudetud */
function inputOnChange(myInput) {

    return;
    
    changedInput = document.getElementById(myInput);
    changedInputForm = changedInput.form.name;

    if (document[changedInputForm][submitInputName1] != null)
    { 
        document[changedInputForm][submitInputName1].style.visibility = 'visible';
    }
    else if (document[changedInputForm][submitInputName2] != null)
    {
        document[changedInputForm][submitInputName2].style.visibility = 'visible';
    }

}

function selectImage(myId) {

    myCheckBox = document.getElementById('selectedImage' + myId);

    if (myCheckBox != null) {

        if (myCheckBox.checked == true) {
            myCheckBox.checked = false;
        }
        else {
            myCheckBox.checked = true;
        }

    }
}

function deleteProductImages(myCode) {

    if (document.selectedImagesForm.selectedImages.length) {

        for (var i = 0; i < document.selectedImagesForm.selectedImages.length; i++) {

            if (document.selectedImagesForm.selectedImages[i].checked) {

                /* piltide kustutamine */
                myId = document.selectedImagesForm.selectedImages[i].value;

                if (getConn != null && myId != null) {

                    getConn.open('post', url, false);
                    getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    getConn.send('myRequest=deleteImage&myId=' + myId);

                }
            }
        }

    }
    else {

        if (document.selectedImagesForm.selectedImages.checked) {

            /* piltide kustutamine */
            myId = document.selectedImagesForm.selectedImages.value;

            if (getConn != null && myId != null) {

                getConn.open('post', url, false);
                getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                getConn.send('myRequest=deleteImage&myId=' + myId);

            }
        }
    }

    /* DIVi sisu uuendamine*/
    myDiv = document.getElementById('productsDiv');

    if (myCode != null && myDiv != null && getConn != null) {

        /* andmete ja piltide uuesti kuvamine */
        getConn.open('post', url, false);
        getConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        getConn.send('myRequest=productDetailsByCode&myCode=' + myCode);

        if (getConn.readyState == 4) {

            if (getConn.status == 200) {
                myDiv.innerHTML = getConn.responseText;
            }
            else {
                xmlHttpError(getConn.statusText);
            }
        }
    }
}

