// JavaScript Document

function aspHtmlDecode(s) {
	// Functie op een string gecodeerd in asp met server.htmlencode te decoderen.
	// Versie 1.0 (07-03-2007)
	s = s.replace(/&lt;/g,"<");
	s = s.replace(/&gt;/g,">");
	s = s.replace(/&quot;/g,"\"");
	s = s.replace(/&amp;/g,"&");
	return s;
}

function Loading(id){
	document.getElementById(id).innerHTML = '<div class="loading"><img src="'+PROJECT_URL+'img/icon_spinner.gif" alt="" /></div>'
}

function GetHttpRequest() { // Functie die http_request vult met een XmlHttp object, geeft false terug als er een fout optreedt.
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	return http_request
}

//functies voor toevoegen, wijzigen en verwijderen artikelen in shop
function addArticle(articleId){
	var http_request = GetHttpRequest();
	if(!http_request) return false; // Kan geen ajax component aanmaken.

	//specificaties
	var specs = ''
	var tmpid = ''
	if(document.getElementById('cont_specs')) {
		var inputSpecs = document.getElementById('cont_specs').getElementsByTagName('INPUT')
		for(var i=0;i<inputSpecs.length;i++){
			if(specs!='') specs = specs + ','
			specs = specs + inputSpecs[i].value + '='
			tmpid = inputSpecs[i].id.replace('specName','specValue')
			specs = specs + document.getElementById(tmpid).options[document.getElementById(tmpid).selectedIndex].value
		}
	}

	var url = ""
	url = PROJECT_URL + "xml/updateShoppingcart.xml.asp?container=container_shoppingcart&type=add&id=" + articleId + "&specs=" + specs
	//window.open(url)
	http_request.onreadystatechange = function() { GetShoppingcartResult(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function addFreeArticle(articleId){
	var http_request = GetHttpRequest();
	if(!http_request) return false; // Kan geen ajax component aanmaken.

	//specificaties
	var specs = ''
	var tmpid = ''
	if(document.getElementById('cont_specs')) {
		var inputSpecs = document.getElementById('cont_specs').getElementsByTagName('INPUT')
		for(var i=0;i<inputSpecs.length;i++){
			if(specs!='') specs = specs + ','
			specs = specs + inputSpecs[i].value + '='
			tmpid = inputSpecs[i].id.replace('specName','specValue')
			specs = specs + document.getElementById(tmpid).options[document.getElementById(tmpid).selectedIndex].value
		}
	}

	var url = ""
	url = PROJECT_URL + "xml/updateShoppingcart.xml.asp?container=container_shoppingcart&type=addFree&id=" + articleId + "&specs=" + specs
	//window.open(url)
	http_request.onreadystatechange = function() { GetShoppingcartResult(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}


function editArticle(line,qty) {
	var http_request = GetHttpRequest();
	if(!http_request) return false; // Kan geen ajax component aanmaken.

	var qty
	if(!qty){
		if(document.getElementById('qty'+ line)) qty = document.getElementById('qty'+ line).value
	}

	var specs = ''
	var tmpid = ''
	if(document.getElementById('cont_specs_'+ line)) {
		var inputSpecs = document.getElementById('cont_specs_'+ line).getElementsByTagName('INPUT')
		for(var i=0;i<inputSpecs.length;i++){
			if(specs!='') specs = specs + ','
			specs = specs + inputSpecs[i].value + '='
			tmpid = inputSpecs[i].id.replace('specName','specValue')
			specs = specs + document.getElementById(tmpid).options[document.getElementById(tmpid).selectedIndex].value
		}
	}

	var url = ""
	url = PROJECT_URL + "xml/updateShoppingcart.xml.asp?container=container_shoppingcart&type=edit&lineno=" + line + "&aantal=" + qty + "&specs=" + specs
	//window.open(url)
	http_request.onreadystatechange = function() { GetShoppingcartResult(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function delArticle(line) {
	var http_request = GetHttpRequest();
	if(!http_request) return false; // Kan geen ajax component aanmaken.

	var url = ""
	url = PROJECT_URL + "xml/updateShoppingcart.xml.asp?container=container_shoppingcart&type=del&lineno=" + line
	//window.open(url)
	http_request.onreadystatechange = function() { GetShoppingcartResult(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function initShoppingCart(container){
	var http_request = GetHttpRequest();
	if(!http_request) return false; // Kan geen ajax component aanmaken.

	var url = ""
	url = PROJECT_URL + "xml/updateShoppingcart.xml.asp?container=" + container
	//window.open(url)
	http_request.onreadystatechange = function() { GetShoppingcartResult(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function GetShoppingcartResult(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//afvangen te lange nodes
			if(!IE) http_request.responseXML.normalize() 

			var xmldoc = http_request.responseXML;
			var cmd_node = xmldoc.getElementsByTagName('cmd').item(0);
			var container_node = xmldoc.getElementsByTagName('container').item(0);
			var content_node = xmldoc.getElementsByTagName('content').item(0);
			var type_node = xmldoc.getElementsByTagName('type').item(0);

			if(content_node.firstChild){
				if(!IE){
					top.document.getElementById(container_node.firstChild.data).innerHTML = xmldoc.getElementsByTagName("content")[0].textContent
				}else{
					top.document.getElementById(container_node.firstChild.data).innerHTML = content_node.firstChild.data
				}
			}

			if(cmd_node.firstChild){
				if(cmd_node.firstChild.data != 'checkout') {

					if(type_node.firstChild){
						if(type_node.firstChild.data == 'add') {
							var l
							var t
							if(document.getElementById('greybox')){
								l = (640-200) / 2;
								t = ((640-100) / 2) - 50;
							}else{
								l = (document.body.clientWidth-200) / 2;
								t = ((document.body.clientHeight-100) / 2) - 50;
							}
							document.getElementById('container_addshop').style.display = 'block'
							document.getElementById('container_addshop').style.top = t + 'px'
							document.getElementById('container_addshop').style.left = l + 'px'
							setTimeout("document.getElementById('container_addshop').style.display = 'none'",2000)
						}
					}

				}
			}

		}
	}
}

//functies voor toevoegen, wijzigen en verwijderen artikelen in checkout2
function addArticleCheckout2(articleId){
	var http_request = GetHttpRequest();
	if(!http_request) return false; // Kan geen ajax component aanmaken.

	Loading('container_checkout2')
	
	var url = ""
	url = PROJECT_URL + "xml/updateShoppingcartCheckout2.xml.asp?container=container_checkout2&type=add&id=" + articleId
	//window.open(url)
	http_request.onreadystatechange = function() { GetShoppingcartCheckout2Result(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function editArticleCheckout2(line,qty) {
	var http_request = GetHttpRequest();
	if(!http_request) return false; // Kan geen ajax component aanmaken.

	Loading('container_checkout2')

	var qty
	if(!qty){
		if(document.getElementById('qty'+ line)) qty = document.getElementById('qty'+ line).value
	}

	var specs = ''
	var tmpid = ''
	if(document.getElementById('cont_specs_'+ line)) {
		var inputSpecs = document.getElementById('cont_specs_'+ line).getElementsByTagName('INPUT')
		for(var i=0;i<inputSpecs.length;i++){
			if(specs!='') specs = specs + ','
			specs = specs + inputSpecs[i].value + '='
			tmpid = inputSpecs[i].id.replace('specName','specValue')
			specs = specs + document.getElementById(tmpid).options[document.getElementById(tmpid).selectedIndex].value
		}
	}

	var url = ""
	url = PROJECT_URL + "xml/updateShoppingcartCheckout2.xml.asp?container=container_checkout2&type=edit&lineno=" + line + "&aantal=" + qty + "&specs=" + specs
	//window.open(url)
	http_request.onreadystatechange = function() { GetShoppingcartCheckout2Result(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function delArticleCheckout2(line) {
	var http_request = GetHttpRequest();
	if(!http_request) return false; // Kan geen ajax component aanmaken.

	Loading('container_checkout2')

	var url = ""
	url = PROJECT_URL + "xml/updateShoppingcartCheckout2.xml.asp?container=container_checkout2&type=del&lineno=" + line
	//window.open(url)
	http_request.onreadystatechange = function() { GetShoppingcartCheckout2Result(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function initShoppingCartCheckout2(container){
	var http_request = GetHttpRequest();
	if(!http_request) return false; // Kan geen ajax component aanmaken.

	Loading('container_checkout2')

	var url = ""
	url = PROJECT_URL + "xml/updateShoppingcartCheckout2.xml.asp?container=" + container
	//window.open(url)
	http_request.onreadystatechange = function() { GetShoppingcartCheckout2Result(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function GetShoppingcartCheckout2Result(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//afvangen te lange nodes
			if(!IE) http_request.responseXML.normalize() 

			var xmldoc = http_request.responseXML;
			var container_node = xmldoc.getElementsByTagName('container').item(0);
			var content_node = xmldoc.getElementsByTagName('content').item(0);
			var type_node = xmldoc.getElementsByTagName('type').item(0);

			if(content_node.firstChild){
				if(!IE){
					document.getElementById(container_node.firstChild.data).innerHTML = xmldoc.getElementsByTagName("content")[0].textContent
				}else{
					document.getElementById(container_node.firstChild.data).innerHTML = content_node.firstChild.data
				}
			}
			
			initShoppingCart('container_shoppingcart')
			
		}
	}
}



var xmlDoc
function checkUsername(obj,edit){
	var http_request = GetHttpRequest();
	if(!http_request) return false; // Kan geen ajax component aanmaken.

	var url = PROJECT_URL + "xml/validateUser.xml.asp?str="+obj.value+"&edit="+edit
	document.getElementById('validateUserError').innerHTML =''
	if(obj.value.length != 0){
		//window.open(url)
		http_request.onreadystatechange = function() { ResultValidationUsername(http_request); };
		http_request.open('GET', url, true);
		http_request.send(null);
	}
}
function ResultValidationUsername(http_request){
	if (http_request.readyState == 4)	{
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
			var action_node = xmldoc.getElementsByTagName('actionResult').item(0);

			if(action_node.firstChild.data != '0'){
				document.getElementById('validateUserError').innerHTML = action_node.firstChild.data
				UserOK = false
			}else{
				document.getElementById('validateUserError').innerHTML = ""
				UserOK = true
			}
		}	
	}else{
		document.getElementById('validateUserError').innerHTML = ""
		UserOK = true
	}
}

//Haal aan de hand van de code een gratis artikel op
function validateCoupon(code,msgCont,msgCodeFailed){
    var objMsgCont = document.getElementById(msgCont)
    if (code.length = 0){
        objMsgCont.innerHTML = msgCodeFailed
    }else{
        var http_request = GetHttpRequest();
	    if(!http_request) return false; // Kan geen ajax component aanmaken.
        
        var url = PROJECT_URL + "xml/getCouponArticle.xml.asp?code="+code
        //window.open(url)
        http_request.onreadystatechange = function() { ResultvalidateCoupon(http_request,msgCont);};
        http_request.open('GET', url, true);
        http_request.send(null);
    }        
}   

//Lees het resultaat van de xml uit
function ResultvalidateCoupon(http_request, msgCont){
    var objMsgCont = document.getElementById(msgCont)
    if(http_request.readyState == 4){
        if(http_request.status == 200){
            var xmldoc = http_request.responseXML;           
            var articleId = xmldoc.getElementsByTagName('articleId').item(0);            
            var msg = xmldoc.getElementsByTagName('msg').item(0);
                        
            
            if(articleId.firstChild.data != '0'){ //Er is een artikel terug gegeven
                //Voeg het artikel toe aan de shoppingcart                
                addFreeArticle(articleId.firstChild.data);
                if(msg.firstChild.data != '0'){ //Er is een melding meegeven
                    objMsgCont.innerHTML = msg.firstChild.data
                }                
            }else{
                if(msg.firstChild.data != '0'){ //Er is een melding meegeven
                    objMsgCont.innerHTML = msg.firstChild.data
                }                
            }
        }
    }
}