﻿ function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
    // Internet Explorer
  try
    {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  
if (xmlHttp==null)
{
    alert ("Your browser does not support AJAX!");
    return;
} 
  
return xmlHttp;
}

_basketurl = null;     
// show the stock popup in sales mode    

function showstock(basketurl, stockurl)
{
    //showPopup("ModalPopup1")
   
    
    document.getElementById("MPText").style.textAlign = "center";
    document.getElementById("MPText").innerHTML="<br /><br/ ><br /><img src='../imgs/waitcircle.gif' valign='center'/><br /><br /><br />";

    xmlHttp=GetXmlHttpObject();
    xmlHttp.open("GET",stockurl,true);
    xmlHttp.send(null);
    xmlHttp.onreadystatechange=getStockStateChanged;
  
    _basketurl = basketurl;
    document.getElementById("MPYes").onclick = atb_stock;    
}

function atb_stock()
{
    //vanish('ModalPopup1');
    if(document.getElementById('div_basket') != null)
    {
        AjaxGet(_basketurl,'div_basket','loadResults');
        //document.getElementById("MPYes").onclick = null;
    }
    else
    {
           location.replace(_basketurl.toLowerCase().replace("async",""));
    } 
}

function loadStockResults()
{
   var sStockInfo = xmlHttp.responseText;
   //sStockInfo = sStockInfo.replace("<br>","\n");

   var blnConfirm = confirm(sStockInfo);
   if(blnConfirm)
   {
        atb_stock();
   }

   //document.getElementById("MPText").style.textAlign = "left";
   //document.getElementById("MPText").innerHTML = xmlHttp.responseText;
}

function getStockStateChanged()
{
 if (xmlHttp.readyState==4)
    {
        if (xmlHttp.status==200) {
            setTimeout('loadStockResults()',500);
        }
    }
}



function AjaxGet(pUrl, pElement, pCallBackMethod)
{

document.getElementById(pElement).style.textAlign = "center";
document.getElementById(pElement).innerHTML="<img src='../imgs/waitcircle.gif' valign='center'/>";

xmlHttp=GetXmlHttpObject();    
xmlHttp.onreadystatechange= function()
{
    if (xmlHttp.readyState==4)
    { 
        if (xmlHttp.status==200) 
        {
            setTimeout(pCallBackMethod + '()',300);
        }
    }
}
    xmlHttp.open("GET",pUrl,true);
    xmlHttp.send(null);
    
}


function loadBasket()
{
    document.getElementById("div_basket").style.textAlign = "left";
    document.getElementById("div_basket").innerHTML=xmlHttp.responseText;
}

function loadResults()
{
    document.getElementById("div_basket").style.textAlign = "left";
    document.getElementById("div_basket").innerHTML=xmlHttp.responseText;
   
    document.getElementById("FPText").innerHTML="Ihr Einkaufskorb wurde akualisiert<h1>Produkt hinzugefügt</h1>";
    showPopup("FloatingPopup1");
    showPopup("FPFloatingPopup");
    setTimeout("fade('FPFloatingPopup')",1000);
}

function setJSEnabledState()
{
    xmlHttp=GetXmlHttpObject();
    xmlHttp.open("GET","setJSEnabled.aspx",true);
    xmlHttp.send(null);
}

function SetLayerPosition() 
{
          // centre any visible divpopups even if scroll or resize happens
          var elem;
          var bws = getBrowserHeight();
          var ltop = parseInt((bws.height - 300)/2) +  parseInt(document.documentElement.scrollTop) + 'px';
          var lleft = parseInt((bws.width - 230)/2) + parseInt(document.body.scrollLeft) + 'px';
          
          elem = document.getElementById("MPModalPopup");    
          elem.style.left =lleft;
          elem.style.top = '220px';

          elem = document.getElementById("FPFloatingPopup");    
          elem.style.left = lleft;
          elem.style.top = ltop;
          elem = null;
}

opac = 100; 
fadeItemId = null;

function fade(itemtofade)
{
    opac = 100;
    fadeItemId =itemtofade;

    fadeOut();
}

function fadeOut() { 
    if(opac > 0)
    { 
        opac-=10; 
        document.getElementById(fadeItemId).style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="
+ opac + ");";
         setTimeout('fadeOut()', 20); 
    } 
    else
    {
        vanish('FloatingPopup1');
    }
} 

function vanish(id)
{
    document.getElementById(id).style.display = 'none';
    document.getElementById(id).style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity='100')";
}
function showPopup(id)
{
    document.getElementById(id).style.display = "block";
    document.getElementById(id).style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity='100')";
    SetLayerPosition();
}


function getBrowserHeight() {
    var intH = 0;
    var intW = 0;

    if(typeof window.innerWidth  == 'number') {
     intH = window.innerHeight;
     intW = window.innerWidth;
    } 
    else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
       intH = document.documentElement.clientHeight;
       intW = document.documentElement.clientWidth;
    }
    else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
    intH = document.body.clientHeight;
    intW = document.body.clientWidth;
    }

    return {width: parseInt(intW), height: parseInt(intH)};
}

$(document).ready(function() {

    $("a.showAllOptions").click(function() {
        ShowAllOptions_Click(this);
    });

    $("input.AddCompare").click(function() {
        AddCompare_Click(this);
    });
    
    $("a.cross").click(function() {
        Cross_Click(this);
    });
              
    $("input.AddCompare").each(function() {
        $(this).removeAttr("checked");
    });
    
    $("#CompareHeader").find(".CompareItem").each(function() {
        CompareItem_Init(this);
    });

    ToggleCompareButton();
   
});

function ShowAllOptions_Click(sender)
{
    var ul = sender.parentNode.parentNode;
    for (i=0;i<ul.childNodes.length;i++)
    {
        if (ul.childNodes[i].innerHTML == sender.parentNode.innerHTML)
        {
            $(ul.childNodes[i]).hide();             
        }
        else if ($(ul.childNodes[i]).hasClass("hidden"))
        {
            $(ul.childNodes[i]).fadeIn();
        }
    }
    var li = document.createElement("li");
    ul.appendChild(li);
    var anchor = document.createElement("a");
    li.appendChild(anchor);
    anchor.setAttribute("href", "javascript:;");
    anchor.innerHTML = "&laquo; Optionen ausblenden";
    anchor.className = "showAllOptions";
    $(anchor).click(function() {
        ShowLessOptions_Click(this);
    });
}

function ShowLessOptions_Click(sender)
{
    var ul = sender.parentNode.parentNode;
    for (i=ul.childNodes.length-1;i>=0;i--)
    {
        if (ul.childNodes[i].innerHTML == sender.parentNode.innerHTML)
        {
            $(ul.removeChild(ul.childNodes[i]));               
        }
        else if (ul.childNodes[i].firstChild.className == "showAllOptions")
        {
            $(ul.childNodes[i]).fadeIn();
            return;       
        }        
        else
        {
            $(ul.childNodes[i]).fadeOut();
        }
    }
}

function AddCompare_Click(sender)
{
    if ($(sender).attr("checked") == "true" || sender.checked)
    {
        if (GetCompareListLength() < 5)
        {
            var ListItem = $('.CompareItem:first').clone(true);
            $('.CompareItem:first').parent().append(ListItem);     
            PopulateListItem(ListItem, sender);
            $('#CompareHeader').fadeIn();
            ShowMsgItem();
           
        }        
        else
        {
            alert(youCannotCompare);
            sender.checked = false;
        }
    }
    else
    {
        $("a[productid=" + $(sender).attr("productid") + "]").parent().parent().remove();
         
         //alert(GetCompareListLength());
         ShowMsgItem();
        if (GetCompareListLength() == 1)
        {
            $('#CompareHeader').fadeOut();         
        }
    }
    ToggleCompareButton();
    AddProductId();
}
function ShowMsgItem()
{
    if (GetCompareListLength() == 2)
    {                    
            var ListItem = $("a[productid=0]").parent().parent()
            $('.CompareItem:first').parent().append(ListItem);                         
            PopulateListItem(ListItem, ListItem);                    
             $("a[productid=0]").parent().parent().fadeIn();  
    }
    else
    {     
     
        $("a[productid=0]").parent().parent().fadeOut();  
        $("a[productid=0]").parent().parent().attr("visibility",'hidden');
        if (GetCompareListLength() == 1)
        {
            $('#CompareHeader').fadeOut();         
        }
      }


}
function PopulateListItem(listItem, sender)
{
    $(listItem).children(":first").text($(sender).attr("producttitle"));
    var baseUrl = "/webcontent/product_images/thumbnailsSmall/"; 
    var url = baseUrl + $(sender).attr("manufacturerid") + "/" + $(sender).attr("sku") + ".gif";
    
    if($(sender).attr("productid")==0)
        $(listItem).children(".thumb").children("img").attr("src", url);
    else
    $(listItem).children(".thumb").children("img").attr("src", url);
    $(listItem).find("a.cross").attr("productid", $(sender).attr("productid"));
    $(listItem).find("a.cross").click(function() {
        Cross_Click(this);
    });
    $(listItem).children(".thumb").children("img").error(function() {
       Thumb_Error(this);
    });    
   if( $(listItem).find("a.cross").attr("productid")==0)
   {
    $(listItem).children(".thumb").hide();  
    $(listItem).children("#title").removeAttr("class");
    }
    else
    {
       $(listItem).children(".thumb").show();  
       $(listItem).children("#title").attr("class", "title");
       }
    listItem.removeAttr("style");
}

function Cross_Click(sender)
{
if($(sender).attr("productId") >0)
{
    var checkbox = $("input[productid=" + $(sender).attr("productId") + "]");
    if (checkbox.attr("productid") != null)
    {
        checkbox.removeAttr("checked");
    }
    else
    {
        checkbox = document.createElement("input");
        checkbox.setAttribute("productid", $(sender).attr("productid"));
    }    
    AddCompare_Click(checkbox);
    }
}

function GetCompareListLength()
{
    return $("#CompareList").children().length;
}

function AddProductId()
{
        var ProductIdList = "0";    
        $("ul#CompareList").children().each(function() {
        var attributeValue = $(this).find("a.cross").attr("productid");
        if (attributeValue != undefined)
        {
            if (ProductIdList.length > 0)
            {
                ProductIdList += ","
            }
            ProductIdList += attributeValue;
        }
        });
        $.get("/compare/service.aspx", { productid: ProductIdList });
}

function CompareItem_Init(sender)
{
    $("input[productid=" + $(sender).find(".cross").attr("productid") + "]").attr("checked", "true");
}

function ToggleCompareButton()
{
    if (GetCompareListLength() > 2)
    {
        $("#CompareHeader").find(".button").addClass("buttonOn");
        $("#CompareHeader").find(".button").attr("href", "/compare/default.aspx");
    }
    else
    {
        $("#CompareHeader").find(".button").removeClass("buttonOn");     
        $("#CompareHeader").find(".button").attr("href", "javascript:;");
    }
    if (GetCompareListLength() > 1)
    {
        $('#CompareHeader').fadeIn();
    }
    ShowMsgItem();
}

function Thumb_Error(sender)
{
    if ($(sender).attr("src").indexOf("no_image.gif") == -1)
    {
        $(sender).attr("src", "/webcontent/product_images/thumbnailsSmall/0/no_image.gif")
    }
}
function CompareProductsAlert(sender)
{

    if(sender.className=='button' )
    alert('You must select at least 2 products to compare.');
}

