﻿ 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()
{
    //CallShopAction5(productid, productname, productquantity, productprice, categoryid, attributes) 
    var RespText = xmlHttp.responseText;
    var Tag = RespText.split("-__-")[1]; 
    var ProductType = RespText.split("-__-")[2];
    var TagPrams = Tag.split("','");
    
    if(ProductType == 'partnerproduct')
    {
        var ParentProduct = RespText.split("-__-")[1];     var PartnerProduct = RespText.split("-__-")[3];
        var TagParentParams = ParentProduct.split("','");   var TagPartnerParams = PartnerProduct.split("','"); 
        CallShopAction5(TagParentParams[0], TagParentParams[1], TagParentParams[2], TagParentParams[3], TagParentParams[4], TagParentParams[5]);
        CallShopAction5(TagPartnerParams[0], TagPartnerParams[1], TagPartnerParams[2], TagPartnerParams[3], TagPartnerParams[4], TagPartnerParams[5]);
    }
    if(ProductType == 'normalproduct')
    {
        CallShopAction5(TagPrams[0], TagPrams[1], TagPrams[2], TagPrams[3], TagPrams[4], TagPrams[5]);
    }       
    document.getElementById("div_basket").style.textAlign = "left";
    document.getElementById("div_basket").innerHTML = RespText.split("-__-")[0];
    document.getElementById("FPText").innerHTML="your basket has been updated<span class='added'>Product Added</span>";
    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)};
}