﻿//article select image
function selectImage(fileid, position, borderclass) {
    $('mainimage').src = rooturl + "contentimage.aspx?fileid=" + fileid + "&width=338&height=500";
    $('articleimage' + $('articlecurrentpos').innerHTML).className = "";
    $('articleimage' + position).className = borderclass;
    $('articlecurrentpos').innerHTML = position;
}

//removes article from basket
function removeFromBasket(id) {
    if (confirm('Ali želite odstraniti izdelek iz košarice?')) {
        $('articleid').value = id;
        $('validate').value = 1; //remove from basket
        $('baskettype').value = 1; //basket
        $('basketform').submit();
    }
}

//add to basket - whishlist
function addToBasketFromWishlist(id) {
    $('articleid').value = id;
    $('validate').value = 0; //add to basket
    $('baskettype').value = 1; //basket
    $('quantity').value = $('quantity' + id).value;
    $('myaccountwishlistform').submit();
}

//removes article from wishlist
function removeFromWishlist(id) {
    if (confirm('Ali želite odstraniti izdelek iz liste želja?')) {
        $('articleid').value = id;
        $('validate').value = 1; //remove from whishlist
        $('baskettype').value = 2; //wishlist
        $('myaccountwishlistform').submit();
    }
}

//clear basket
function emptyBasket() {
    $('validate').value = 3; //empty basket
    $('basketform').submit();
}

//to payment button
function toPaymentBasket() {
    $('validate').value = 4; //to order login
    $('basketform').submit();
}

//reload if no response from ajax
function checkResponse(responseText) {
    //no response when insert/update was ok
    if (responseText == '') {
        toggleDiv('modalWindow');

        //reload page with delivery address
        if ($('orderbusinesspartnerdeliveryaddressid')) {
            var reload = window.location.href.substring(0, window.location.href.indexOf('businesspartnerdeliveryaddressid'));

            if (reload == "") {
                window.location = window.location + "?businesspartnerdeliveryaddressid=" + $('orderbusinesspartnerdeliveryaddressid').value + "&paymenttypeid=" + $('orderpaymenttypeid').value;
            } else {
                window.location = reload + "businesspartnerdeliveryaddressid=" + $('orderbusinesspartnerdeliveryaddressid').value + "&paymenttypeid=" + $('orderpaymenttypeid').value;
            }
        }
        //reload page
        else {
            window.location.reload();
        }
    }
}

//set order comment
var lastOpenedCommentId = "";
function setComment(articleid) {
    var display = $("commentpopup" + articleid).style.display;
    if (display != "block") {
        if ($(lastOpenedCommentId)) {
            $(lastOpenedCommentId).style.display = "none";
        }
        $("commentpopup" + articleid).style.display = "block";
        lastOpenedCommentId = "commentpopup" + articleid;
    }
}

//order sale certificate
function setSaleCertificate() {
    sendAjaxRequest(rooturl + "ajaxordersalecertificate.aspx?salecertificatepasscode=" + $('salecertificatepasscode').value, "", "handleSaleCertificate");
}

//order sale certificate remove
function removeSaleCertificate() {
    sendAjaxRequest(rooturl + "ajaxordersalecertificate.aspx?salecertificateid=" + $('salecertificateremoveid').value, "", "handleSaleCertificate");
}

//handle sale certificate
function handleSaleCertificate(responseText) {

    //remove / add
    if (responseText == "3") {
        $('salecertificatetitle').style.display = "block";
        $('salecertificatepasscode').style.display = "block";
        $('salecertificatepasscode').value = "";
        $('salecertificatebutton').style.display = "block";
        $('salecertificateremove').style.display = "none";
        $('salecertificatecodeerror').style.display = "none";
        $('salecertificatecodeinfo').style.display = "none";
        $('confirmorderdiv').style.display = "block";
        $('confirmorderdiverror').style.display = "none";
    }
    else {
        //errors
        if (responseText.split("###")[0] == "1") {
            //show errors
            $('salecertificatecodeinfo').style.display = "none";
            $('salecertificatecodeerror').style.display = "block";
            $('salecertificatecodeerror').innerHTML = responseText.split("###")[1];
        }
        //info
        else {
            //show info
            $('salecertificatecodeerror').style.display = "none";
            $('salecertificatecodeinfo').style.display = "block";
            $('salecertificatecodeinfo').innerHTML = responseText.split("###")[1];
            $('salecertificateremoveid').value = responseText.split("###")[2];

            //hide form
            $('salecertificatetitle').style.display = "none";
            $('salecertificatepasscode').value = "";
            $('salecertificatepasscode').style.display = "none";
            $('salecertificatebutton').style.display = "none";

            //show remove
            $('salecertificateremove').style.display = "block";
            $('confirmorderdiv').style.display = "block";
            $('confirmorderdiverror').style.display = "none";
        }
    }
}

//show hide
function showHideCC(paymenttypeid) {
    var display = $('divcc').style.display;
    if (display != "block" && paymenttypeid == 4) {
        $('divcc').style.display = "block";
    }
    else {
        $('divcc').style.display = "none";
    }
}

//get order delivery payment delivery types
function getOrderDeliveryPaymentDeliveryTypes(businesspartnerdeliveryaddressid) {
    sendAjaxRequest(rooturl + "ajaxorderdeliverypayment.aspx?businesspartnerdeliveryaddressid=" + businesspartnerdeliveryaddressid, "deliverytypes", "");
}

//set article second property
function setArticleSecondProperty(articleid, inputarticleid, property1) {
    sendAjaxRequest(rooturl + "ajaxarticleproperty.aspx?articleid=" + articleid + "&inputarticleid=" + inputarticleid + "&property1=" + property1, "articlepropertyselect", "getArticlePricehandler");
}

//handler to call variant price ajax
function getArticlePricehandler(responseText) {
    //call function to set price
    setArticleVariantPrice($('property2').value);
    //call function to set stock
    getArticleStock($('property2').value);
    
    
}

//get article stock data
function getArticleStock(articleid) {
    sendAjaxRequest(rooturl + "ajaxarticlestock.aspx?articleid=" + articleid, "", "getArticleStockhandler");
}

//article stock handler
function getArticleStockhandler(responseText) {
    if (responseText != '') {
        //split response text
        var sTmp = responseText.split('###');

        //if stock id is 1 - on stock
        if (sTmp[0] == 1) {
            $('articleaddbutton').style.display = 'block';
            $('stockholder').style.display = 'block';            
            $('stockholder').innerHTML = '<span class="green">' + sTmp[1] + '</span>';
        }
        //if stock id is 2 - delivery in days...
        else if (sTmp[0] == 2) {
            $('articleaddbutton').style.display = 'block';
            $('stockholder').style.display = 'block';            
            $('stockholder').innerHTML = '<span class="orange">' + sTmp[1] + '</span>';
        }
        //if stock id is 3 - out of stock
        else if (sTmp[0] == 3) {
            $('articleaddbutton').style.display = 'none';
            $('stockholder').style.display = 'block';            
            $('stockholder').innerHTML = '<span class="red">' + sTmp[1] + '</span>';
        }
        else {
            $('stockholder').style.display = 'none';
            $('articleaddbutton').style.display = 'block';
        }
    }
    else {
        $('stockholder').style.display = 'none';
        $('articleaddbutton').style.display = 'block';
    }
}

//get article stock data
function getArticleStockBasket(articleid) {
    sendAjaxRequest(rooturl + "ajaxarticlestock.aspx?articleid=" + articleid, "", "getArticleStockBasketHandler");
}

//article stock handler
function getArticleStockBasketHandler(responseText) {
    //split response text
    var sTmp = responseText.split('###');

    //if stock id is 3
    if (sTmp[0] == 3) {
        $('stockholder').style.display = 'block';
    }
    else {
        $('stockholder').style.display = 'none';
        window.location = rooturl + "xt_basket.aspx?articleid=" + $('property2').value + "&amp;quantity=1";
    }
}

//set basket property
function setBasketProperty(reference, validate, property1, property2) {
    if (validate == 1) {
        sendAjaxRequest(rooturl + "ajaxbasket.aspx?reference=" + reference + "&validate=1&property1=" + escape(property1), "property1div", "");
    }
    else {
        sendAjaxRequest(rooturl + "ajaxbasket.aspx?reference=" + reference + "&property1=" + escape(property1) + "&validate=2&property2=" + escape(property2), "property2div", "");
    }
}

//set properties
function setBasketProperties() {
    if ($('property1')) {
        $('property1').disabled = true;
    }
    if ($('property2')) {
        $('property2').disabled = true;
    }
    
    sendAjaxRequest(rooturl + "ajaxbasket.aspx?reference=" + $('reference').value + "&validate=1", "property1div", "handleBasketProperties");
}

//set properties
function handleBasketProperties(responseText) {
    sendAjaxRequest(rooturl + "ajaxbasket.aspx?reference=" + $('reference').value + "&property1=" + escape($('property1').value) + "&validate=2", "property2div", "");
}

//add article to basket from article
function addToWishlist(articleid, quantity) {
    //add article to basket
    $('quantity').value = quantity;
    $('articleid').value = articleid;
    $('wishlistform').submit();
}


//add article to basket from article
function addToBasket(articleid, quantity) {
    //add article to basket
    sendAjaxRequest(rooturl + "ajaxsmallbasket.aspx?validate=3&articleid=" + articleid + "&quantity=" + quantity, 'headersmallbasket', 'addToBasketHandler');
}

function addToBasketHandler(responseText) {
    //refresh basket title
    sendAjaxRequest(rooturl + "ajaxsmallbasket.aspx?validate=1", "infobasket", "");

    //scroll to top
    window.scroll(0, 0);

    //show basket with delay
    setTimeout('scrollHeaderSmallBasketDiv()', 300);
}

function changeAdditionalArticleView() {
    toggleDiv('viewrelatedarticlesdiv');
    if ($('viewrelatedarticlesimage').src.indexOf('down') > -1) {
        $('viewrelatedarticlesimage').src = rooturl + "images/icons/up.png";
    }
    else {
        $('viewrelatedarticlesimage').src = rooturl + "images/icons/down.png";
    }
}

function changeAdditionalArticleRelated() {
    toggleDiv('relatedarticlesdiv');
    if ($('viewrelatedarticlesimage1').src.indexOf('down') > -1) {
        $('viewrelatedarticlesimage1').src = rooturl + "images/icons/up.png";
    }
    else {        
        $('viewrelatedarticlesimage1').src = rooturl + "images/icons/down.png";
    }
}

//get article variant price
function setArticleVariantPrice(articlevariantid) {
    sendAjaxRequest(rooturl + "ajaxarticleprice.aspx?articlevariantid=" + articlevariantid, "ajaxarticleprice", "articleSaleIcon");
}

//shwos/hides article sale icon
function articleSaleIcon(responseText) {
    var htmlstring = $('ajaxarticleprice').innerHTML;

    //check if article is on sale
    if (htmlstring.indexOf('###isonsale###') != -1) {
        $('articlesale').style.display = 'block'; //show sale icon
        $('ajaxarticleprice').innerHTML = htmlstring.replace('###isonsale###', '');
    }
    else {
        $('articlesale').style.display = 'none'; //hide sale icon
        $('ajaxarticleprice').innerHTML = htmlstring.replace('###isnotonsale###', '');
    }

    //get value
    var reference = $('changedarticlereference').innerHTML;
    
    //set value
    $('articlereference').innerHTML = reference;
}

//tooltip-----------------------------------------------------------------------------
var OP = (navigator.userAgent.indexOf('Opera') != -1);
var IE = (navigator.userAgent.indexOf('MSIE') != -1 && !OP);
var GK = (navigator.userAgent.indexOf('Gecko') != -1);
var SA = (navigator.userAgent.indexOf('Safari') != -1);
var DOM = document.getElementById;

var tooltip = null;

function TOOLTIP() {
    this.width = 375;                     // width (pixels)
    this.bgColor = "#FFFFFF";             // background color
    this.textFont = "Arial";      // text font family
    this.textSize = 12;                   // text font size (pixels)
    this.textColor = "#000000";           // text color
    this.border = "1px solid #e8453c";    // border (CSS spec: size style color, e.g. "1px solid #D00000")
    this.opacity = 100;                    // opacity (0 - 100); not supported by all browsers
    this.cursorDistance = 5;              // distance from mouse cursor (pixels)
    this.xPos = 'right';                  // horizontal position: "left" or "right"
    this.yPos = 'bottom';                 // vertical position: "top" or "bottom"
    this.padding = 15;                 // padding

    // don't change
    this.text = '';
    this.height = 0;
    this.obj = null;
    this.active = false;

    this.create = function() {
        if (!this.obj) this.init();

        var s = (this.textFont ? 'font-family:' + this.textFont + '; ' : '') +
            (this.textSize ? 'font-size:' + this.textSize + 'px; ' : '') +
            (this.border ? 'border:' + this.border + '; ' : '') +
            (this.textColor ? 'color:' + this.textColor + '; ' : '');
        (this.padding ? 'padding:' + this.padding + 'px; ' : '');

        var t = '<table border="0" cellspacing="0" cellpadding="4" width=' + this.width + '><tr>' +
            '<td align=left' + (s ? ' style="padding:15px;' + s + '"' : '') + '>' + this.text +
            '</td></tr></table>';

        if (DOM || IE) this.obj.innerHTML = t;
        if (DOM) this.height = this.obj.offsetHeight;
        else if (IE) this.height = this.obj.style.pixelHeight;
        if (this.bgColor) this.obj.style.backgroundColor = this.bgColor;

        this.setOpacity();
        this.move();
        this.show();
    }

    this.init = function() {
        if (DOM) this.obj = document.getElementById('ToolTip');
        else if (IE) this.obj = document.all.ToolTip;
    }

    this.move = function() {
        var winX = getWinX() - (((GK && !SA) || OP) ? 17 : 0);
        var winY = getWinY() - (((GK && !SA) || OP) ? 17 : 0);
        var x = mouseX;
        var y = mouseY;

        if (this.xPos == 'left') {
            if (x - this.width - this.cursorDistance >= getScrX())
                x -= this.width + this.cursorDistance;
            else x += this.cursorDistance;
        }
        else {
            if (x + this.width + this.cursorDistance > winX + getScrX())
                x -= this.width + this.cursorDistance;
            else x += this.cursorDistance;
        }

        if (this.yPos == 'top') {
            if (y - this.height - this.cursorDistance >= getScrY())
                y -= this.height + this.cursorDistance;
            else y += this.cursorDistance;
        }
        else {
            if (y + this.height + this.cursorDistance > winY + getScrY())
                y -= this.height;
            else y += this.cursorDistance;
        }

        this.obj.style.left = x + 'px';
        this.obj.style.top = y + 'px';
    }

    this.show = function() {
        this.obj.style.zIndex = 69;
        this.active = true;
        this.obj.style.visibility = 'visible';
    }

    this.hide = function() {
        this.obj.style.zIndex = -1;
        this.active = false;
        this.obj.style.visibility = 'hidden';
    }

    this.setOpacity = function() {
        this.obj.style.opacity = this.opacity / 100;
        this.obj.style.MozOpacity = this.opacity / 100;
        this.obj.style.KhtmlOpacity = this.opacity / 100;
        this.obj.style.filter = 'alpha(opacity=' + this.opacity + ')';
    }
}

function getScrX() {
    var offset = 0;
    if (window.pageXOffset)
        offset = window.pageXOffset;
    else if (document.documentElement && document.documentElement.scrollLeft)
        offset = document.documentElement.scrollLeft;
    else if (document.body && document.body.scrollLeft)
        offset = document.body.scrollLeft;
    return offset;
}

function getScrY() {
    var offset = 0;
    if (window.pageYOffset)
        offset = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
        offset = document.documentElement.scrollTop;
    else if (document.body && document.body.scrollTop)
        offset = document.body.scrollTop;
    return offset;
}

function getWinX() {
    var size = 0;
    if (window.innerWidth)
        size = window.innerWidth;
    else if (document.documentElement && document.documentElement.clientWidth)
        size = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth)
        size = document.body.clientWidth;
    else size = screen.width;
    return size;
}

function getWinY() {
    var size = 0;
    if (window.innerHeight)
        size = window.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight)
        size = document.documentElement.clientHeight;
    else if (document.body && document.body.clientHeight)
        size = document.body.clientHeight;
    else size = screen.height;
    return size;
}

function getMouseXY(e) {
    if (e && e.pageX != null) {
        mouseX = e.pageX;
        mouseY = e.pageY;
    }
    else if (event && event.clientX != null) {
        mouseX = event.clientX + getScrX();
        mouseY = event.clientY + getScrY();
    }
    if (mouseX < 0) mouseX = 0;
    if (mouseY < 0) mouseY = 0;
    if (tooltip && tooltip.active) tooltip.move();
}

function toolTip(text, width, opacity) {
    if (text) {
        tooltip = new TOOLTIP();
        tooltip.text = text;
        if (width) tooltip.width = width;
        if (opacity) tooltip.opacity = opacity;
        tooltip.create();
    }
    else if (tooltip) tooltip.hide();
}

document.write('<div id="ToolTip" style="position:absolute; visibility:hidden"></div>');

var mouseX = mouseY = 0;
document.onmousemove = getMouseXY;
//----------------------------------------------------------------------------------------

//firstpage check if reference ok
function isReferenceValid() {
    sendAjaxRequest(rooturl + "ajaxbasket.aspx?reference=" + $('reference').value + "&validate=1", "", "handleReferenceValid");
}

//handle reference valid
function handleReferenceValid(responseText) {
    //no error
    if (responseText.indexOf('<option') > 0) {
        $("directordererror").style.display = "none";
        $('defaultquickorderform').submit();
    }
    //write error
    else {
        $("directordererror").style.display = "block";
    }
}

//search advanced ajax with parameters
function getSearchAdvancedFilter() {
    //get values
    var nodeid = $('searchadvanced_nodeid').value;
    var searchstring = $('searchadvanced_searchstring').value;
    var pricewotaxfrom = $('searchadvanced_pricewotaxfrom').value;
    var pricewotaxto = $('searchadvanced_pricewotaxto').value;
    var sale = ($('searchadvanced_sale').checked ? 1 : 0);
    var articlebrandid = $('searchadvanced_articlebrandid').value;
    var property1 = $('searchadvanced_property1').value;
    var property2 = $('searchadvanced_property2').value;

    //disable entry
    $('searchadvanced_nodeid').disabled = true;
    $('searchadvanced_searchstring').disabled = true;
    $('searchadvanced_pricewotaxfrom').disabled = true;
    $('searchadvanced_pricewotaxto').disabled = true;
    $('searchadvanced_sale').disabled = true;
    $('searchadvanced_articlebrandid').disabled = true;
    $('searchadvanced_property1').disabled = true;
    $('searchadvanced_property2').disabled = true;

    //show ajax loader
    $('ajaxsearchadvancedinfo').style.display = 'block';

    //send ajax request
    sendAjaxRequest(rooturl + "ajaxsearchadvanced.aspx?nodeid=" + nodeid + "&searchstring=" + escape(searchstring) + "&pricewotaxfrom=" + pricewotaxfrom + "&pricewotaxto=" + pricewotaxto + "&sale=" + sale + "&articlebrandid=" + articlebrandid + "&property1=" + property1 + "&property2=" + property2, "ajaxsearchadvanced", "handlerSearchAdvancedFilter");
}

//ajax handler for search advanced filter
function handlerSearchAdvancedFilter(responseText) {
    //hide ajax loader
    setTimeout("$('ajaxsearchadvancedinfo').style.display = 'none'", 500);
    
    //enable entry
    $('searchadvanced_nodeid').disabled = false;
    $('searchadvanced_searchstring').disabled = false;
    $('searchadvanced_pricewotaxfrom').disabled = false;
    $('searchadvanced_pricewotaxto').disabled = false;
    $('searchadvanced_sale').disabled = false;
    $('searchadvanced_articlebrandid').disabled = false;
    $('searchadvanced_property1').disabled = false;
    $('searchadvanced_property2').disabled = false;
}

function SearchAdvanceCleaner() {   
    //enable entry
    $('searchadvanced_nodeid').disabled = false;
    $('searchadvanced_searchstring').disabled = false;
    $('searchadvanced_pricewotaxfrom').disabled = false;
    $('searchadvanced_pricewotaxto').disabled = false;
    $('searchadvanced_sale').disabled = false;
    $('searchadvanced_articlebrandid').disabled = false;
    $('searchadvanced_property1').disabled = false;
    $('searchadvanced_property2').disabled = false;

    clearTimeout(SearchAdvanceTimer);
}

//timer to wait before submitin search advanced filter
var SearchAdvanceTimer = 0;
function getSearchAdvancedTimer() {
    //if timer exists then clear
    if (SearchAdvanceTimer > 0) {
        //clear timer
        clearTimeout(SearchAdvanceTimer);
    }
    //set timer
    SearchAdvanceTimer = setTimeout('getSearchAdvancedFilter()', 1100);
}

//submit search form filter
function submitSearchFromMenu(nodeid) {
    document.getElementById('filter').nodeid.value = nodeid;
    document.getElementById('filter').page.value = 1;
    document.getElementById('filter').submit();

    //do not execute href link
    return false;
}

//submit search form filter
function submitPagination(page) {
    document.getElementById('filter').page.value = page;
    document.getElementById('filter').submit();
    
    //do not execute href link
    return false;
}

//sale certificate onkeypress
function saleCertificatePress(e) {
    if (e && e.which) {
        e = e
        characterCode = e.which
    }
    else {
        e = event
        characterCode = e.keyCode
    }
    
    if (characterCode == 13) {
        setSaleCertificate();
        return false;
    }
    else {
        $('confirmorderdiv').style.display = "none";
        $('confirmorderdiverror').style.display = "block";
        return true;
    }
}

//key up
function saleCertificateKeyUp(value) {
    if (value == '') {
        $('confirmorderdiv').style.display = "block";
        $('confirmorderdiverror').style.display = "none";
    }
}

//subscribe user to newsletter
function submitNewsletter() {
    sendAjaxRequest(rooturl + "ajaxnewsletter.aspx?email=" + $('newsletteremail').value, "", "handleNewsletter");
}

//handle newsletter subscribtion
function handleNewsletter(responseText) {
    //no error
    if (responseText == "ok") {
        $("newslettererror").style.display = 'none';
        $('newsletterinfo').style.display = 'block';
    }
    //write error
    else {
        $("newslettererror").style.display = 'block';
        $('newsletterinfo').style.display = 'none';
    }
}
