var ShopInterface = {};

ShopInterface.req_interface_URL = "shoppingcart/JSInterface.cfm";

ShopInterface.req_addproduct = null;
ShopInterface.req_removeproduct = null;
ShopInterface.req_refresh = null;

//TRANSLATION
ShopInterface.translation_addtocart = 'Product werd toegevoegd aan het winkelmandje';

/* ADD PRODUCT */
ShopInterface.AddProduct=function(productid, qty){
	//alert("TODO: add item " + productid + " " + qty);
	
	
	
	if(ShopInterface.req_addproduct != null){			
		//ShopInterface.req_addproduct.onreadystatechange = null;		
		//ShopInterface.detachEvent('onreadystatechange', ShopInterface.req_addproduct_response);
		//ShopInterface.req_addproduct = null;		
	}	
	
		
		if (window.XMLHttpRequest) {
			ShopInterface.req_addproduct = new XMLHttpRequest();
		// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			ShopInterface.req_addproduct = new ActiveXObject("MSXML2.XMLHTTP.3.0");
		}		
		
	//alert(ShopInterface.req_addproduct);
		
	if(ShopInterface.req_addproduct){			
		ShopInterface.req_addproduct.onreadystatechange = ShopInterface.req_addproduct_response ;			
		ShopInterface.req_addproduct.open("POST", ShopInterface.req_interface_URL,true);
		ShopInterface.req_addproduct.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ShopInterface.req_addproduct.send("action=AddProduct&pid="+productid+"&qty="+qty);	
	}		
}

ShopInterface.req_addproduct_response = function(){

    if (ShopInterface.req_addproduct.readyState == 4) {
        // only if "OK"
        if (ShopInterface.req_addproduct.status == 200) {
			//alert(ShopInterface.req_addproduct.responseText);

			/*var ul = document.getElementById("shoppingcart_content_list");
			var li = document.createElement("li");
			li.innerHTML="nieuwe lijn";
			ul.appendChild(li);			*/
			
			document.getElementById("shoppingcart_content_container").innerHTML = ShopInterface.req_addproduct.responseText;
			notifyUser(ShopInterface.translation_addtocart);	
			
         } else {
            alert("There was a problem retrieving the XML data:\n"          +       ShopInterface.req_addproduct.statusText);
         }
    }
}

/* REMOVE PRODUCT */
ShopInterface.RemoveProduct=function(productid){
	//alert("TODO: add item " + productid + " " + qty);
	
	if(ShopInterface.req_removeproduct){			
		//ShopInterface.req_removeproduct.onreadystatechange = null;		
		//ShopInterface.req_removeproduct = null;		
	}	

	if (window.XMLHttpRequest) {
		ShopInterface.req_removeproduct = new XMLHttpRequest();
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		ShopInterface.req_removeproduct = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	}	

	//alert(ShopInterface.req_removeproduct);
	
	if(ShopInterface.req_removeproduct){			
		ShopInterface.req_removeproduct.onreadystatechange = ShopInterface.req_removeproduct_response ;			
		ShopInterface.req_removeproduct.open("POST", ShopInterface.req_interface_URL,true);
		ShopInterface.req_removeproduct.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ShopInterface.req_removeproduct.send("action=RemoveProduct&pid="+productid);	
	}	
}

ShopInterface.req_removeproduct_response = function(){

    if (ShopInterface.req_removeproduct.readyState == 4) {
        // only if "OK"
        if (ShopInterface.req_removeproduct.status == 200) {
			//alert(ShopInterface.req_removeproduct.responseText);

			/*var ul = document.getElementById("shoppingcart_content_list");
			var li = document.createElement("li");
			li.innerHTML="nieuwe lijn";
			ul.appendChild(li);			*/
			
			//document.getElementById("shoppingcart_content_container").innerHTML = ShopInterface.req_removeproduct.responseText;
			//notifyUser('Product werd toegevoegd aan het winkelmandje');	
			
			window.location.reload();
			
         } else {
            alert("There was a problem retrieving the XML data:\n"          +       ShopInterface.req_removeproduct.statusText);
			window.location.reload();
         }
    }
}

