/**
 * SHOP.JS 
 * Created on 24 January 2008
 * Author: Maarten Janssen
 * Current version: 1.0.0
 *
 * This class contains all common functions of a shoppingcart in the Parade
 * webshop module. 
 */ 

/**
 * !!! This class is customized for the Storms webshop !!!
 */
 
/**
 * Shoppingcart class. This class initializes a shoppingcart. The constructor
 * searches for the shoppingcart placeholder and tries to create the 
 * HTTPRequest object. If one of these fails an instance of Cart cannot be
 * created. If all goes well the getHtml function is called to display the
 * shoppingcart.
 */
function Cart () {
	if (CmsEditorActive!==true){
		if (!document.getElementById ("shop_cart_placeholder")) {
			alert ("Missing shoppingcart placeholder.");
			return null;
		}
	
		this.url = document.getElementById ("shop_cart_placeholder").getElementsByTagName ("input")[0].value;
		this.req = null;
		this.reopen = false;
		
		try {
			this.req = new ActiveXObject ("Msxml2.XMLHttp");
		} catch (e) {
			try {
				this.req = new ActiveXObject ("Microsoft.XMLHttp");
			} catch(e2) {
				try {
					if (typeof XMLHttpRequest != 'undefined') this.req = new XMLHttpRequest ();
				} catch (e3) {
					alert ("The HTTP request object could not be created for your browser.");
					return null;
				}
			}
		}
		
		this.getHtml ();
	}
}


/**
 * Execute an HTTP request to retrieve the shoppingcart HTML.
 */
Cart.prototype.getHtml = function () {
	var _this = this;	
	
	this.req.open ("GET", this.url, true);
	this.req.onreadystatechange = function () { _this.stateChange () };
	this.req.send (null);
}


/**
 * Add a new product to the shoppingcart by issuing a POST with product data
 * and the required action.
 */
Cart.prototype.add  = function (proId, proSId, varId, amount, text) {	
	if (document.getElementById (amount))
		amount = document.getElementById (amount).value;
	else 
		amount = 1;
		
	if (varId.length == 0) varId = 0;
		
	if (document.getElementById (text)) {
		text = document.getElementById (text).value;
		if (text.length == 0) {
			alert ("Voer aub uw persoonlijke tekst in voor u dit product bestelt.");
			return;
		}
	} else {
		text = "";
	}
	text = text.replace (/\s/g, "%20");
	
	var urlTest = window.location.href; 
	if (urlTest.indexOf (".sph.html") != -1) {
	   alert ("Deze wijn heeft inmiddels mogelijk een andere prijs. Bekijk uw bestellijst voor de actuele prijs. Ook is deze wijn mogelijk niet meer leverbaar. In dat geval wordt contact met u opgenomen.");
    }

	var _this = this;
	
	alert ("Het product is toegevoegd aan de bestellijst.");

	this.req.open ("POST", this.url, true);
	this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.req.onreadystatechange = function () { _this.stateChange () };
	this.req.send ("wmaction=addline&wmproid=" + proId + "&wmprosid=" + proSId + "&wmamount=" + amount + "&wmvariation=" + varId + "&wmpersonalize=" + text);
}


/**
 * Add a new product to the shoppingcart by issuing a POST with product data
 * and the required action.
 */
Cart.prototype.addBatch  = function (intCode, amount) {	
	var _this = this;

	this.req.open ("POST", this.url, true);
	this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.req.onreadystatechange = function () { _this.stateChange () };
	this.req.send ("wmaction=addline&wmproid=&wmprosid=0&wmamount=" + amount + "&wmvariation=0&wmpersonalize=&wmintcode=" + intCode);
}


/**
 * Change the amount of a product in the shoppingcart by issuing a POST with 
 * the line id and the required action for the webservice.
 */
Cart.prototype.change = function (sclId, sclSId, amount) {
	if (document.getElementById (amount))
		amount = document.getElementById (amount).value;
	else
		amount = 1;

	var _this = this;
	this.reopen = true;

	this.req.open ("POST", this.url, true);
	this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.req.onreadystatechange = function () { _this.stateChange () };
	this.req.send ("wmaction=chline&wmsclid=" + sclId + "&wmsclsid=" + sclSId + "&wmamount=" + amount);
}


/**
 * Remove a product from the shoppingcart by issuing a POST with the line id
 * and the required webservice action command.
 */
Cart.prototype.remove = function (sclId, sclSId) {
	var _this = this;
	this.reopen = true;

	this.req.open ("POST", this.url, true);
	this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.req.onreadystatechange = function () { _this.stateChange () };
	this.req.send ("wmaction=delline&wmsclid=" + sclId + "&wmsclsid=" + sclSId);
}


/**
 * Clear the shoppingcart by issuing a POST with the required action by the
 * webservice.
 */
Cart.prototype.clear = function () {
	var _this = this;
	this.reopen = true;

	this.req.open ("POST", this.url, true);
	this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.req.onreadystatechange = function () { _this.stateChange () };
	this.req.send ("wmaction=clear");
}


/**
 * Replace the content of the shoppingcart placeholder if the readyState of
 * HTTPRequest changes. The content will only be updated if the readyState
 * is 4 (Ready / Done loading) and the status code is 200 (Ok).
 */
Cart.prototype.stateChange = function () {
	if (this.req.readyState == 4 && this.req.status == 200) {
		document.getElementById ("shop_cart_placeholder").innerHTML = this.req.responseText;
		
		var totalPrice = 0;
		var counter = 1;

		while (document.getElementById ("cartprice" + counter)) {
			totalPrice += parseFloat (document.getElementById ("cartprice" + counter).innerHTML.substring (2));
			document.getElementById ("cartprice" + counter).innerHTML = document.getElementById ("cartprice" + counter).innerHTML.replace (".", ","); 

			counter ++;
		}
		
		var groteLijst = document.getElementById ("shop_cart_placeholder").getElementsByTagName ("div")[0];
		document.getElementById ("shop_cart_placeholder").removeChild (groteLijst);
		var blaat = String (document.body.lastChild.id);
		if (blaat.indexOf ("shop_cart") == 0) {
			document.body.removeChild (document.body.lastChild);		
		} 
		
		document.body.appendChild (groteLijst);
		
		if (this.reopen) {
			openBestellijst ();
			this.reopen = false;
		}
	}
}

