/**
 * 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.
 */ 
 
 
/**
 * 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 (!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;
	
	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);
}


Cart.prototype.RegisterEvent = function(obj, evt, fnc) {
	if (obj.addEventListener) {
		obj.addEventListener(evt, fnc, false);
	} else {
		obj.attachEvent("on" + evt, fnc);
	}
}

Cart.prototype.alertSluiten = function() {
	var alertcontainer = document.getElementById("alertcontainer");
	alertcontainer.parentNode.removeChild(alertcontainer);
	document.getElementById("hiderdiv").style.display = "none";
}

Cart.prototype.orderAlert = function() {
	var me = this;
	var alertcontainer = document.createElement("div");
	var alerttitel = document.createElement("p");
	var alertsluitlink = document.createElement("a");
	var alertsluiten = document.createElement("img");
	var alertmelding = document.createElement("p");
	var alertbutton = document.createElement("input");

	alertcontainer.className = "shop_alert";
	alertcontainer.id = "alertcontainer";
	alerttitel.className = "shop_alerttitel";
	alerttitel.innerHTML = "Melding";
	alertsluitlink.className = "shop_alert";
	alertsluitlink.href = "#";
	this.RegisterEvent(alertsluitlink, "click", function() { me.alertSluiten(); });
	alertsluiten.className = "shop_alert";
	alertsluiten.src = "/sites/268/images/sluiten.gif";
	alertsluiten.alt = "Sluiten";
	alertmelding.className = "shop_alert";
	alertmelding.innerHTML = "U heeft besteld: " + document.getElementById("ds_artikel_detailtitel").innerHTML;
	alertbutton.className = "shop_alert";
	alertbutton.type = "button";
	alertbutton.value = "OK";
	this.RegisterEvent(alertbutton, "click", function() { me.alertSluiten(); });

	document.body.appendChild(alertcontainer);
	alertcontainer.appendChild(alertsluitlink);
	alertsluitlink.appendChild(alertsluiten);
	alertcontainer.appendChild(alerttitel);
	alertcontainer.appendChild(alertmelding);
	alertcontainer.appendChild(alertbutton);

	document.getElementById("hiderdiv").style.height = document.documentElement.scrollHeight + "px";
	document.getElementById("hiderdiv").style.display = "block";
	alertcontainer.style.display = "block";

	// ScrollY positie ophalen
	var scrollY = 0;
	if (typeof (window.pageYOffset) == 'number')
		scrollY = window.pageYOffset;
	else if (document.body && document.body.scrollTop)
		scrollY = document.body.scrollTop;
	else if (document.documentElement && document.documentElement.scrollTop)
		scrollY = document.documentElement.scrollTop;

	// Hoogte van zichtbaar deel van pagina ophalen
	var winHeight = 0;
	if (typeof (window.innerWidth) == 'number')
		winHeight = window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		winHeight = document.documentElement.clientHeight;

	// De popup centreren
	var x = document.body.clientWidth / 2 - alertcontainer.clientWidth / 2;
	var y = scrollY + winHeight / 2 - alertcontainer.clientHeight / 2;

	alertcontainer.style.left = x + "px";
	alertcontainer.style.top = y + "px";
}

/**
* 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 _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=" + proId + "&wmprosid=" + proSId + "&wmamount=" + amount + "&wmvariation=" + varId + "&wmpersonalize=" + text);
	this.orderAlert();
	//highlightCart(varId);
}


/**
 * 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.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;
	
	var melding = confirm("Weet u zeker dat u dit artikel wilt verwijderen?");
	
	if(melding){
		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;

	var melding = confirm("Weet u zeker dat u alle artikelen uit uw winkelwagen wilt verwijderen?");

	if(melding){
		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;
	}
}


