var http;
var browser = navigator.appName;
if ( browser == 'Microsoft Internet Explorer' )
	http = new ActiveXObject("Microsoft.XMLHTTP");
else
	http = new XMLHttpRequest();

function getFooterpics(albumid) {
	http.open('GET', base+'/footerimages.php?albumid='+albumid+''); 
	http.onreadystatechange = handleResponseGetFooterpics; 
	http.send(null);	
}

function handleResponseGetFooterpics() {	
	if(http.readyState == 4 && http.status == 200)
	{ 
		if(http.responseText) { 
			document.getElementById('wrap_footerimages').innerHTML = http.responseText;
		}		
	} else if(http.readyState == 4 && http.status == 404) { 
	  document.getElementById('wrap_footerimages').innerHTML = "HTTP ERROR 404: <br /><br />Pagina niet gevonden"; 
	} 
}

/* WEBSHOP */

function addToBasket(id, sub, am) {
	if(typeof sub == 'undefined'){
		sub = 0;
	}
	var amount;
	if(typeof am != 'undefined'){
		amount = parseInt(am);
	} else {
		if(sub == 0){
			amount = document.getElementById('inputAmount').value;
		} else {
			amount = document.getElementById('inputAmount_'+sub).value;
		}
	}
	http.open('GET', base+'/customer/plugins/webshop/basket.php?id='+id+'&amount='+amount+'&sub='+sub); 
	http.onreadystatechange = handleResponseAddToBasket; 
	http.send(null);
}
function handleResponseAddToBasket() {
	if(http.readyState == 4 && http.status == 200){ 
		if(http.responseText) {			
			if(http.responseText != ''){
				document.location=base+'/winkelmandje/2/Winkelmandje.html';
				document.getElementById('winkelmandjeLink').innerHTML = http.responseText;
				setTimeout("hide('winkelmandjeMsg')", 2000);
			}
		}		
	} else if(http.readyState == 4 && http.status == 404) { 
	  document.getElementById('winkelmandjeLink').innerHTML = "HTTP ERROR 404: <br /><br />Pagina niet gevonden"; 
	} 
}


/*
function delete(id) {
	http.open('GET', base+'/customer/plugins/webshop/basket.php?id='+id+'&amount='+amount+'&sub='+sub); 
	http.onreadystatechange = handleResponseAddToBasket; 
	http.send(null);
}
function handleResponseDelete() {
	if(http.readyState == 4 && http.status == 200){ 
		if(http.responseText) {			
			if(http.responseText != ''){
				document.location=base+'/winkelmandje/2/Winkelmandje.html';
				document.getElementById('winkelmandjeLink').innerHTML = http.responseText;
				setTimeout("hide('winkelmandjeMsg')", 2000);
			}
		}		
	} else if(http.readyState == 4 && http.status == 404) { 
	  document.getElementById('winkelmandjeLink').innerHTML = "HTTP ERROR 404: <br /><br />Pagina niet gevonden"; 
	} 
}
*/

function deleteItem(id, sub){
	http.open('GET', base+'/customer/plugins/webshop/delete.php?id='+id+'&sub='+sub); 
	http.onreadystatechange = function(){
		if(http.readyState == 4 && http.status == 200){ 
			if(http.responseText) {			
				if(http.reponseText != ''){
					var result = eval('('+http.responseText+')');
					if(result.total == 0){
						document.location=base+'/webshop/1/Webshop.html';
					} else {
						document.location=base+'/winkelmandje/1/winkelmandje.html';
					}
				}
			}
		}
	}
	http.send(null);
}

function refreshBasket(id, sub) {
	if(typeof sub == 'undefined'){
		sub = 0;
	}
	if(id != 0) {
		document.getElementById('loader'+id+'_'+sub).innerHTML = "<img src='"+base+"/customer/plugins/webshop/images/loader.gif' id='loading' alt='Bezig met laden...' />";		
	}
	setTimeout('refreshBasketFunction('+id+', '+sub+')', 800);
}
function refreshBasketFunction(id, sub) {
	if(id != 0) {
		var amount = document.getElementById('inputAmount_'+id).value;
	}else {
		var amount = 0;
	}
	var check = document.getElementById('sendmethodSelect');
	if(check) {
		var sendmethod = document.getElementById('sendmethodSelect').options[document.getElementById('sendmethodSelect').selectedIndex].value;
	}else {
		var check = document.getElementById('sendmethodInput');
//		var sendmethod = check.value;
	}

	var check = document.getElementById('paymethodSelect');
	if(check) {
		var paymethod = document.getElementById('paymethodSelect').options[document.getElementById('paymethodSelect').selectedIndex].value;
	}else {
		var check = document.getElementById('paymethodInput');
//		var paymethod = check.value;
	}
	http.open('GET', base+'/customer/plugins/webshop/basketRefresh.php?id='+id+'&amount='+amount+'&sub='+sub+'&sendmethod='+sendmethod+'&paymethod='+paymethod+''); 
	http.onreadystatechange = function(){
		handleResponseRefreshBasket(id, sub);
	}
	http.send(null);
}
function handleResponseRefreshBasket(id, sub) {
	if(http.readyState == 4 && http.status == 200){ 
		if(http.responseText) {
			if(id > 0) {
				var loading = document.getElementById('loader'+id+'_'+sub);
				loading.innerHTML = "";
			}
			var result = eval('('+http.responseText+')');
			if(result['status'] == 'hoofdpagina') {
				document.location=base+'/webshop/1/Webshop.html';
			}else {
				var link = document.getElementById('winkelmandjeLink');
				if(link){
					link.innerHTML = result['msg'];
				} else {
					alert('Kan de de link naar het winkelmandje niet vinden.');
				}
				var main = document.getElementById('webshop_right');
				if(main){
					main.innerHTML = result['overzicht'];
				} else {
					alert('Kan het overzicht niet vinden.');
				}
				setTimeout("hide('winkelmandjeMsg')", 2000);
			}			
		}		
	} else if(http.readyState == 4 && http.status == 404) { 
		document.getElementById('webshop_right').innerHTML = "HTTP ERROR 404: <br /><br />Pagina niet gevonden"; 
	} 
}

function chooseSub(el, list){
	var div = document.createElement('div');
	div.id = 'subSelection';
	div.style.zIndex = 1000;
	for(var i = 0; i < list.length; i++){
		var c = document.createElement('div');
		c.innerHTML = list[i].name;
		c.style.cursor = 'pointer';
		c.id = 'subinput_'+list[i].id+'_'+list[i].sub;
		c.className = 'subInput';
		c.onclick = function(){
			var data = this.id.split('_');
			var sub = data.pop();
			var id = data.pop();
			addToBasket(id, sub, 1);
		};
		div.appendChild(c);
	}
	el.parentNode.appendChild(div);
	var mask = document.createElement('div');
	mask.id = 'mask';
	mask.style.position = 'fixed';
	mask.style.width = '100%';
	mask.style.height = '100%';
	mask.style.top = 0;
	mask.style.left = 0;
	mask.style.zIndex = 900;
	document.body.appendChild(mask);
	mask.onclick = function(){
		var s = document.getElementById('subSelection');
		s.parentNode.removeChild(s);
		this.parentNode.removeChild(this);
	}
}