var itemnr

function add_webshop()
{	
	// Add popup, does not belong here but hey! it works ;)
	/* Don't show the popup for an other year...
	if(window.document.location.pathname == '/index.php' || window.document.location.pathname == '/')
	{
		popUp('extra_popup.html', 500, 130);
	}
	*/
	
	var webwinkel = document.getElementById('winkelwagen');
	if(webwinkel != null)
	{
		moveMenuBar;
		document.body.onscroll=moveMenuBar;
		window.onscroll=moveMenuBar;
	}
	
	var url='http://'+document.domain+'/include/helperfiles/webshop.php';
	makeRequestShop_inhoud(url);
}


function moveMenuBar()
{
	var webwinkel = document.getElementById('winkelwagen');
	
	if(webwinkel != null)
	{
		var y = findScrollTop();
		if(y < 0) y = 0;
		webwinkel.style.top = y + 25 + 'px';
	}
}

function findScrollTop()
{
	if(window.pageYOffset != null)
		return window.pageYOffset;
	if(document.body.scrollWidth != null)
		return document.body.scrollTop
	return (null);
}

function add_to_cart(itemnr)
{
	var url='http://'+document.domain+'/include/helperfiles/webshop.php';
	makeRequestShop(url, itemnr)
}


function makeRequestShop(url, itemnr) {
    var httpRequest;

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType('text/xml');
            // See note below about this line
        }
    } 
    else if (window.ActiveXObject) { // IE
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (e) {
                       try {
                            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                           } 
                         catch (e) {}
                      }
                                   }

    if (!httpRequest) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }

    
    httpRequest.open('POST', url, true);    
    httpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
    httpRequest.send('action=add&articlenr='+itemnr);
}

function makeRequestShop_inhoud(url) {
    var httpRequest;

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType('text/xml');
            // See note below about this line
        }
    } 
    else if (window.ActiveXObject) { // IE
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (e) {
                       try {
                            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                           } 
                         catch (e) {}
                      }
                                   }

    if (!httpRequest) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }

    
    httpRequest.open('POST', url, true);    
    httpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    httpRequest.onreadystatechange = function() { alertContents_inhoud(httpRequest); };
    httpRequest.send('action=show');
}

function alertContents(httpRequest) {

    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
        	var url='http://'+document.domain+'/include/helperfiles/webshop.php';
			makeRequestShop_inhoud(url)
            /*alert(httpRequest.responseXML);*/
        } else {
            alert('There was a problem with the request.');
        }
    }

}

function alertContents_inhoud(httpRequest) {

    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) { 
        	/* Check for contents of webshop, if so, show, else, hide */
        	var webshopdiv = document.getElementById('winkelwagen');
        	var items = httpRequest.responseXML.getElementsByTagName('item');
			var itCnt = items.length;
			
			if(itCnt >= 1)
			{
				var webshoptable = document.getElementById('winkelwagen_items');
				var webshopprice = document.getElementById('webshopprice');
			
				var price = 0;
				removeAllChildNodes(webshoptable);
				removeAllChildNodes(webshopprice);
				
				for(i=0; i<itCnt; i++)
				{
					var title = items[i].getElementsByTagName('title');
					var count = items[i].getElementsByTagName('count');
					var number = items[i].getElementsByTagName('number');
					var price_add = items[i].getElementsByTagName('price');
					title = title[0].firstChild.nodeValue;
					count = count[0].firstChild.nodeValue;
					number = number[0].firstChild.nodeValue;
					price_add = price_add[0].firstChild.nodeValue;
					
					var thetbody = document.createElement('tbody');
					var thetr = document.createElement('tr');
					var thetd_count = document.createElement('td');
					var thetd_title = document.createElement('td');
					var thetd_number = document.createElement('td');
					
					thetd_count.appendChild(document.createTextNode(count+'x'));
					thetd_number.appendChild(document.createTextNode(number));
					thetd_title.appendChild(document.createTextNode(title));
					
					thetr.appendChild(thetd_count);
					thetr.appendChild(thetd_number);
					thetr.appendChild(thetd_title);
					thetbody.appendChild(thetr);
					webshoptable.appendChild(thetbody);
					
					/* Add price to total count */
					price_add = parseInt(price_add) * parseInt(count);
					
					price = price + parseInt(price_add);
				}
				webshopprice.appendChild(document.createTextNode(price));
        		webshopdiv.style.display='block';
			}
            /*alert(httpRequest.responseXML);*/
        } else {
            alert('There was a problem with the request.');
        }
    }
}

function removeAllChildNodes(node) 
{
	if (node && node.hasChildNodes && node.removeChild) 
	{
		while (node.hasChildNodes()) 
		{
			node.removeChild(node.firstChild);
		}
	}
}

