
/* Variable to know if the "In Den Warenkorb" button has already been clicked so that there is no double submission.  */
var alreadyClicked = false;

/* Function to Alert the User that the current Shop has changed before submitting the Form. */
function alertShopChange(formId, switchedShop)  {

    var addForm = document.forms[formId];

    if (!alreadyClicked) {
        if (switchedShop==true) {
            input_box = confirm("Sie haben den Shop gewechselt. Ihr Warenkorb wird deshalb geleert.");


            if (input_box==true)  {
                /*we only want to pop it once - So now we can put the switchShop cookie to done -- if we dont hit cancel*/
                //setCookie("ShopSwitched","false");
                /*we poped the message once --no need to do it again unless we switch shops*/
                setCookie("ShopSwitched","done");

                addForm.submit();
            }

        } else {

            addForm.submit();
        }
    }
}

/* Function to Alert the User that the current Shop has changed before viewing the cart. */
function alertShopChangeForCart(formId, switchedShop)  {
    var addForm = document.forms[formId];
    if (!alreadyClicked) {
        if (switchedShop==true) {
            input_box = confirm("Sie haben den Shop gewechsel. Ihr Warenkorb wird deshalb gelöscht.");
            if (input_box==true)  {
                /*we only want to pop it once - So now we can put the switchShop cookie to done -- if we dont hit cancel*/
                //setCookie("ShopSwitched","false");
                /*we poped the message once --no need to do it again unless we switch shops*/
                setCookie("ShopSwitched","done");
                //We want to stay in the same page
                addForm.action = location.href;
                addForm.submit();
            }

        } else {
            addForm.submit();
        }
    }
}




/* Function to refresh the page when changing BusinessTransaction through the radio buttons  */
function callRefreshThroughRadioButtonsMethod()  {

    var btForm = document.forms["businessTransactionForm"];

    if(btForm)  {
      btForm.submit();
      disableAddToCart();
    } else  {
      alert('Not working');
    }
}

/* Disables the possibility to press the "In Den Warenkorb" button */
function disableAddToCart() {
    alreadyClicked = true;
}

/* Function to refresh the page when changing Goodies through the radio buttons  */
function reloadChangeGoodie(formId) {

    var goodiesForm = document.forms[formId];

    if(goodiesForm) {
      goodiesForm.submit();
      disableAddToCart();
    }
}

/* Function to refresh the page when changing discounts through the dropdowns  */
function reloadChangeDiscount() {

    var discountsForm = document.forms["discountsForm"];

    if(discountsForm) {
      discountsForm.submit();
      disableAddToCart();
    }
}

/*Function to set a cookie */
function setCookie(name, value, expires, path, domain, secure) {

    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}



/*Functions to manage the cookies - Switch shop*/


function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/*Function to set the shop cookie to either business or the default shop - if we switch shops we set the ShopSwitched to true*/
    function setCookieForShop(shop){

     if (shop == getCookie("Shop")){
        setCookie("ShopSwitched","false");
    }else{

        setCookie("Shop",shop);
        if(getCookie("CARTCOUNT")>0){
            setCookie("ShopSwitched","true");
        }

    }

}

/*this deletes the cookie when called*/
function deleteCookie( name, path, domain ) {

if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


/*Get the value of switch cookie to verify if we switched shops*/
function getSwitchedValue(value){
    if(getCookie('ShopSwitched')=="done"){
                    switched =false;
    }
    else{
        switched = value;
    }
    return switched;
}

