jQuery.ajaxSetup({cache:false});
jQuery.noConflict();


function changeMoms(elm) {
    var newMomsValue = elm.value;

    var urlStr = '/ecommerce/jsp/changeMoms.jsp?MOMS=' + newMomsValue;
    jQuery.noConflict();
    jQuery.ajax({
        url: urlStr,
        type: 'GET',
        dataType: 'xml',
        timeout: 15000,
        error: function() {
            alert('Misslyckades att spara orderinformation.');
        },
        success: function(xml) {
            var locationHref = window.location.href;
            if (locationHref.indexOf("paymentandprocessordersvanstroms") > 0) {
                window.location.href = "/ecommerce/control/main";
            } else if ((locationHref.indexOf("additem") > 0) || (locationHref.indexOf("modifycart") > 0)) {
                window.location.href = "/ecommerce/control/showcartsvanstroms";   
            }
            else {
            }
                //Nothing in particular
                window.location.reload();
            }
    });
}

function addItemToCart(productId, minAntal) {
    document.location.href = "/ecommerce/control/additem?add_product_id=" + productId + "&product_id=" + productId + "&quantity=" + minAntal;
}

    function showHide(el) {
        var itemDiv = document.getElementById(el);
        var currState = itemDiv.style.display;
        if (currState == 'none') {
            itemDiv.style.display = 'block';
        } else {
            itemDiv.style.display = 'none';
        }
    }

function additemsToShoppingListForm(id) {
    /*
    var formName = "addform" + indexItem;
    var comFormName = "commentForm" + indexItem;
    var comments = document.forms[comFormName].comments.value;
    document.forms[formName].comments.value = comments;
    document.forms[formName].action = "/ecommerce/control/additemsToShoppingList";
    if (document.forms[formName].quantity.value > 0) {
        document.forms[formName].submit();
    } */
    document.addform.action = "/ecommerce/control/additemsToShoppingList";
    document.addform.shoppingListId.value = document.getElementById("shoppingListId" + id).value;
    document.addform.submit();

}

    function activateTab(selectedTabName) {
        updateTab('description', selectedTabName);
        updateTab('specification', selectedTabName);
        updateTab('accessory', selectedTabName);
        updateTab('related', selectedTabName);
    }

    function updateTab(tabName, selectedTabName) {
        var tabElement = document.getElementById(tabName + 'Tab');
        var linkElement = document.getElementById(tabName + 'Link');
        var divElement = document.getElementById(tabName + 'Div');
        var className = "notselected";
        if (tabElement && linkElement) {
            if (tabName == selectedTabName) {
                className ="selected";
            }
        }
        tabElement.className = className;
        linkElement.className = className;
        divElement.className = className;
    }

    function displayDescription() {
        activateTab('description');
        document.location.href = '#infoTab';
    }

    function IsNumeric(sText)
{
   var ValidChars = "0123456789 ";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function checkAntal(elm, minAntal, allowBreakPackage) {
    var newQ = elm.value;
    if (!/^([0-9\s]*)$/.test(newQ)) {
        alert("Artikelnummer best\u00e5r bara av siffror !");
        newQ = minAntal;
        elm.value = newQ;
        return false;
    }
    if ((!allowBreakPackage) && (minAntal > newQ)) {
        alert("Denna artikel levereras i l\u00e5st f\u00f6rpackningsstorlek om " + minAntal + " enheter. V\u00e4nligen ange ett antal som \u00e4r en multipel av " + minAntal);
        newQ = minAntal;
        elm.value = newQ;
        return false;
    }
    if ((!allowBreakPackage) && (newQ % minAntal > 0)) {
        alert("Denna artikel levereras i l\u00e5st f\u00f6rpackningsstorlek om " + minAntal + " enheter. V\u00e4nligen ange ett antal som \u00e4r en multipel av " + minAntal);
        newQ = newQ - (newQ % minAntal);
        elm.value = newQ;
        return false;
    }
    return true;
}

    function submit_form() {
        var nbOfVariants = document.addform.nbOfVariants.value;
        var sendSubmit = 0;
        for (i=0; i<nbOfVariants; i++) {
            var quantTempElm = document.getElementById("quantity_" + i);
            var quantTemp = quantTempElm.value;
            if ((quantTemp.length > 0) && (quantTemp != 0)) sendSubmit = 1;

        }
        if (sendSubmit == 1) {
            document.addform.submit();
        }
    }

function addItem(productId, quantity, comment) {
    window.location = ofbizUrl + "additem?" +
        "add_product_id=" + productId +
        "&comments=" + comment +
        "&quantity=" + quantity;
}

function addItemToShoppingList(shoppingListId, productId, quantity, comment) {
    window.location = ofbizUrl + "additemsToShoppingList?" +
            "shoppingListId=" + shoppingListId +
            "&add_product_id=" + productId +
            "&comments=" + comment +
            "&quantity=" + quantity;
}

function additems(indexItem) {
    var comFormName = "commentForm" + indexItem;
    var comments = document.forms[comFormName].comments.value;
    var formName = "addform" + indexItem;
    document.forms[formName].comments.value = comments;
    if (document.forms[formName].quantity.value > 0) {
        document.forms[formName].submit();
    }
}

function toggle(e) {
    e.checked = !e.checked;
}

            function showPriceCached(id, variant, trOpen, trClosed, index){
                variant.style.display = 'block';
                trOpen.style.display = 'block';
                trClosed.style.display = 'none';
            }
            function showPriceAjax(id, variant, trOpen, trClosed,index){
                if(variant.getAttribute('loaded')=='true'){
                    variant.style.display = 'block';
                    trOpen.style.display = 'block';
                    trClosed.style.display = 'none';
                } else {
                    var urlStr = '/ecommerce/jsp/priceHelper.jsp?productId='+id;
                    jQuery.noConflict();
                    jQuery.ajax({
                        url: urlStr,
                        type: 'GET',
                        dataType: 'xml',
                        timeout: 15000,
                        error: function() {
                            alert('Misslyckades att ladda priser.');
                        },
                        success: function(xml) {
                            var element = xml.documentElement;
                            var productId = jQuery(element).attr('productId');

                            jQuery(xml).find('variant').each(function() {
                                var table = "<table border='0' cellpadding='0' cellspacing='0'>";
                                var variantid = jQuery(this).attr('id');
                                jQuery(this).find('price').each(function(){
                                    var amount =jQuery(this).attr('amount');
                                    var price =jQuery(this).attr('priceToDisplay');
                                    var isCampaign = jQuery(this).attr('isCampaign')=="true" ? true : false;
                                    var isPromo = jQuery(this).attr('isPromo')=="true" ? true : false;
                                    var recommendedRetailPrice=jQuery(this).attr('recommendedRetailPrice');
                                    var unitOfMeasure =jQuery(this).attr('unitOfMeasure');
                                    var recommendedRetailPriceString = "";
                                    var priceString = "";

                                    if((price<recommendedRetailPrice) && isCampaign) {
                                        recommendedRetailPriceString = " <span class=\"lineThrough\"\">" + recommendedRetailPrice + "&nbsp;" + unitOfMeasure + " </span>";
                                        priceString = "<span class=\"campaignPrice\"\">" + price + "&nbsp;" + unitOfMeasure + " </span>";
                                    }else {
                                        priceString = price + "/"+ unitOfMeasure;
                                    }



                                   // table = table + "<tr><td>"+ amount + " " + unitOfMeasure + ":&nbsp;&nbsp</td><td align='right'>" + price + "/"+ unitOfMeasure  + ${recPriceStringInclHtml?if_exists} +" </td></tr>";
                                    table = table + "<tr><td>"+ amount + " " + unitOfMeasure + ":&nbsp;&nbsp</td><td align='right'>" + priceString + recommendedRetailPriceString +"</td></tr>";
                                });
                                table = table + "</table>"
                                var element = document.getElementById('table-' + variantid);
                                if(element!=null){
                                    element.innerHTML = table + element.innerHTML;
                                }
                            });
                            variant.style.display = 'block';
                            trOpen.style.display = 'block';
                            trClosed.style.display = 'none';
                            var variant1 = document.getElementById("variant_" + productId);
                            variant1.setAttribute('loaded','true');
                        }
                    });
                }
            }
            function toggleVariant(id, index, fetchedPrices) {
                var variant = document.getElementById("variant_" + id);
                var tdOpen = document.getElementById("td_open_" + id);
                var tdClosed = document.getElementById("td_closed_" + id);
                var trOpen = document.getElementById("tr_open_" + id);
                var trClosed = document.getElementById("tr_closed_" + id);
                if (variant != null) {
                    if (variant.style.display == 'block') {
                        variant.style.display = 'none';
                        trOpen.style.display = 'none';
                        trClosed.style.display = 'block';
                    } else {
                        if(fetchedPrices)
                            showPriceCached(id, variant, trOpen, trClosed, index );
                        else
                            showPriceAjax(id, variant, trOpen, trClosed, index);
                    }
                }
            }
            function showError(t){
                alert(t);
            }
            function showPrices(xml) {
            alert(xml);
                $(xml).find('price').each(function() {
                    var price = $(this).text();
                    alert('PRICE='+price);
                    $('')
                            .html(price)
                            .appendTo('td#variant-price-'+id);
                });
            }