function getElementById(id)
{
   var object = null;

   if (document.layers) 
   {
      object = document.layers[id];
   }
   else if (document.all) 
   {
      object = document.all[id];
   }
   else if (document.getElementById) 
   {
      object = document.getElementById(id);
   }

   return object;
}

function showElement(id)
{
	var divBox = getElementById(id);

	divBox.style.display = "block";
}

function hideElement(id)
{
	var divBox = getElementById(id);

	divBox.style.display = "none";
}

function loadCart()
{
   var name = "cartSummary";
   //alert("get " + name + "...");

   var strCookies = document.cookie;
   var cookies = strCookies.split(";");

   // alert(cookies);

   for (var i = 0; i < cookies.length; i++) 
   {
      var strCookie = cookies[i];

      var delimiter = strCookie.indexOf("=");

      if (delimiter > -1) 
      {
         var cookieName = strCookie.substring(0, delimiter);
         var cookieValue = strCookie.substring(delimiter + 1, strCookie.length);

	 cookieName = cookieName.replace(/^\s+|\s+$/g, '');

         if (cookieName === name)
         {
            return cookieValue;
         }
      }
   }

   return "";
}

function saveCart(value)
{
   var name = "cartSummary";
   var ablauf = new Date();
   var infuenfTagen = ablauf.getTime() + (24 * 60 * 1000);

   ablauf.setTime(infuenfTagen);
   
   document.cookie = name + "=" + value + "; expires=" + ablauf.toGMTString();
}

function showPopupDialog(shopUrl, lang, id)
{
	showPopupDialog(shopUrl, lang, id, '');
}
function showPopupDialog(shopUrl, lang, id, shopLink)
{
   var url= document.URL;
   var pos = url.indexOf("cartState=changed");

   if (pos > -1) 
   {
      var newUrl = url.substr(0, pos - 1);
      var closeUrl = newUrl;
	  
	  if (shopLink)
      {  
	     if (shopUrl === shopLink.substr(0,shopUrl.length))
		 {
		    shopLink = shopLink.substr(shopUrl.length + 1);
		 }

	     if (lang === shopLink.substr(0,2))
		 {
		    shopLink = shopLink.substr(3);
		 }
 
		 if (shopLink.indexOf("/") > -1)
		 {
			shopLink = shopLink.substr(0, shopLink.indexOf("/"));
		 }
 
	     newUrl = shopUrl + "/" + lang + "/" + shopLink;
      }
	  
      var dlg = getElementById(id);
      
      if(dlg)
      {
          var productImage = getUrlParamter('image');
          var productName = getUrlParamter('name');
          var productPrice = getUrlParamter('price');
          var productEan = getUrlParamter('ean');
          var cartCount = getUrlParamter('count');
          var cartPrice = getUrlParamter('total');
          
          var dlgHtml = dlg.innerHTML;
          
          dlgHtml = replaceAll(dlgHtml, "$product-image", shopUrl + "/media/images/info/" + decodeURI(productImage));
          dlgHtml = replaceAll(dlgHtml, "$product-name", decodeURI(productName));
          dlgHtml = replaceAll(dlgHtml, "$product-price", decodeURI(productPrice));
          dlgHtml = replaceAll(dlgHtml, "$product-ean", decodeURI(productEan));
          dlgHtml = replaceAll(dlgHtml, "$cart-count", decodeURI(cartCount));
          dlgHtml = replaceAll(dlgHtml, "$cart-total", decodeURI(cartPrice));
          dlgHtml = replaceAll(dlgHtml, "$href-close", closeUrl);
          dlgHtml = replaceAll(dlgHtml, "$href-shop", newUrl);

          if (lang)
          {
             dlgHtml = replaceAll(dlgHtml, "$href-cart", shopUrl + "/" + lang + "/index.php?page=cart");
             dlgHtml = replaceAll(dlgHtml, "$href-checkout", shopUrl + "/" + lang + "/checkout/shipping");
          }
          else
          {
             dlgHtml = replaceAll(dlgHtml, "$href-cart", shopUrl + "/index.php?page=cart");
             dlgHtml = replaceAll(dlgHtml, "$href-checkout", shopUrl + "/checkout/shipping");
          }

          dlg.innerHTML = dlgHtml;
      }

      document.body.style.overflow = "hidden";

      showElement(id);
   }
   else
   {
      document.body.style.overflow = "auto";

      hideElement(id);
   }
}

function getElementParameter(name, defValue)
{
	if (getElementById(name))
	{
		return getElementById(name).innerHTML;
	}
	
	return defValue;
}

function getUrlParamter( name )
{
  url = window.location.href;
  url = url.replace('&amp;','&');
  
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( url );

  if( results == null )
    return "";
  else
    return results[1];
}

//private method for UTF-8 decoding
function decode (utftext) {
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	while ( i < utftext.length ) {

		c = utftext.charCodeAt(i);

		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		}
		else if((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		}
		else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}

	}

	return string;
}

function replaceAll(Source,stringToFind,stringToReplace)
{
  var temp = Source;
    var index = temp.indexOf(stringToFind);
        while(index != -1){
            temp = temp.replace(stringToFind,stringToReplace);
            index = temp.indexOf(stringToFind);
        }
        return temp;
}

function getNewProductInCart(oldCart, newCart)
{
	if (oldCart === newCart)
	{
		return "";
	}
	
	var oldProducts = oldCart.split("~");
	var newProducts = newCart.split("~");
	
	if (oldProducts.length < newProducts.length)
	{
		var info = newProducts[newProducts.length- 2];
		
		var parts = info.split(":");
		var product = parts[0];

		return product;
	}
	
	for (i = 0; i < newProducts.length; i++)
	{
		var oldInfo = oldProducts[i];
		var oldParts = oldInfo.split(":");
		var oldProduct = oldParts[0];
		var oldQuantity = oldParts[1];

		var newInfo = newProducts[i];
		var newParts = newInfo.split(":");		
		var newProduct = newParts[0];
		var newQuantity = newParts[1];
		
		if (oldQuantity != newQuantity)
		{
			return newProduct;
		}
	}
	
	return "";
}

function getProductCountInCart(cart)
{
	var count = 0;
	var products = cart.split("~");
	
	for (i = 0; i < products.length; i++)
	{
		var newInfo = products[i];
		var newParts = newInfo.split(":");		
		var newProduct = newParts[0];
		var newQuantity = newParts[1];
		
		if(newQuantity)
			count = count + parseInt(newQuantity);
	}
	
	return count;
}
