var menuHeight    = 95;
var contentWidth  = 751;
var language;

// Removes a item from the cart
function removeItem(shopperID, cartID) {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }

    var url='include/cart_remove.asp';
    url=url+'?shopper_id='+shopperID;
    url=url+'&cart_id='+cartID;
    url=url+'&sid=' + Math.random();
    xmlHttp.onreadystatechange = removeCallback;
    xmlHttp.open('GET',url,true);
    xmlHttp.send(null);
}//removeItem(shopperID, cartID)

// Callback for the XMLHTTP.
function removeCallback() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        var sTmp = xmlHttp.responseText;
        xmlHttp = null;
        var sArr = new Array();
        sArr = sTmp.split(":",4);
        
        // Get the objects
        var objX = document.getElementById('product' + sArr[1]);
        var objY = document.getElementById('totalSum');
        var objZ = document.getElementById('sumRow');
        
        // If the sum is 0 then remove the "total"-row
        if (parseInt(sArr[3]) == 0) {
            // objZ.style.display = 'none';
            // objX.style.display  = 'none';
            
            /* If the cart it empty, what's the point displaying the cart?! */
            /* Kick them back to the shop /chrise */
            document.location.href = '/shop.asp?Lang=' + language;
            
        } 
        else {
            objY.innerHTML      = '<strong>Totalt:&nbsp\;' + sArr[2] + '&nbsp;SEK exkl. frakt och avgifter<' + '/strong>';
            objX.style.display  = 'none';
        }

        // Destroy objects                
        objY    = null;
        objX    = null;
        objZ    = null;
    }
}//removeCallback()


// Updates the quantity of a order-row
function updateItem(shopperID, cartID) {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    
    // Get the count.
    var count = document.getElementById('qty' + cartID).value;
    
    var url='include/cart_update.asp';
    url=url+'?shopper_id='+shopperID;
    url=url+'&cart_id='+cartID;
    url=url+'&qty='+count;
    url=url+'&sid=' + Math.random();
    xmlHttp.onreadystatechange = updateCallback;
    xmlHttp.open('GET',url,true);
    xmlHttp.send(null);
}//updateItem(shopperID, cartID)

// Callback for the XMLHTTP.
function updateCallback() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        var sTmp = xmlHttp.responseText;
        xmlHttp = null;
        var sArr = new Array();
        sArr = sTmp.split(":",5);
        
        // Get the objects
        var objX = document.getElementById('sum' + sArr[0]);
        var objY = document.getElementById('totalSum');
        
        objX.innerHTML      = sArr[3] + '&nbsp;SEK';
        objY.innerHTML      = '<strong>Totalt:&nbsp\;' + sArr[1] + '&nbsp;SEK exkl. frakt och avgifter<' + '/strong>';
        
        if (parseInt(sArr[2]) == 0) {
            document.getElementById('product' + sArr[0]).style.display = 'none';
            //console.log('CartID: ' + sArr[0]);
            //console.log('shopperID: ' + sArr[4]);
            removeItem(sArr[4], sArr[0]);
        }
        
        // Destroy objects
        objY    = null;
        objX    = null;
    }
}//updateCallback()


function wsChangeFontSize(size) {
  var listSpan = self.document.getElementsByTagName('span');
  var iSize;
  var iLH;

  if (listSpan.length != 0) {
      switch(size) {
        case 'S':
          iSize = '65%';
          iLH = '100%';
          break;
        case 'M':
          iSize = '90%';
          iLH = '120%';
          break;
        case 'L':
          iSize = '100%';
          iLH = '130%';
          break;
      }

    for (i=0; i < listSpan.length; i++){
      listSpan[i].style.fontSize = iSize;
      listSpan[i].style.lineHeight = iLH;
    }
  }
}

function init() {
  resizeFrame();
  addEventListener(window, 'resize', resizeFrame, false);
  // Select the last link in the menu
  var oLinks = document.getElementsByTagName('a');
  oldNav = oLinks[oLinks.length-1];  
}

// This function attaches an event.
// X-browser compatible
function addEventListener(target,type,func,bubbles) {
	if (document.attachEvent) {
		target.attachEvent("on"+type,func, bubbles);
	} else {
		target["on"+type] = func;
	}
}//addEventListener

function resizeFrame() {
  var f = document.getElementById('fContent');

  // Size the iFrame to fit the table-cell.
  if (!window.innerHeight) {    // Internet Gay Explorer
    f.style.width = contentWidth + 'px';
    f.style.height = (document.body.offsetHeight - parseInt(menuHeight)) + 'px';
    f.scrolling = 'auto';
  }
  else {                        // EOMB (Every other modern browser).
    f.style.height = (document.body.offsetHeight - parseInt(menuHeight)) + 'px';
    f.scrolling = 'auto';
  }

  // Remove Reference.
  f = null;

}

// Function for creating the HTTPRequest-object
// By trial and error method based on browser.
function GetXmlHttpObject() {
    var req = null;

    if(window.XMLHttpRequest) {
        try {
            req = new XMLHttpRequest();
        } 
        catch(e) {
            req = null;
        }
    }
    else if(window.ActiveXObject)
    {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP"); 
        }
        catch(e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP"); 
            } 
            catch(e) {
                req = null;
            }
        }
    }
    return req;
}//GetXmlHttpObject()
