/**
 * 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);
}


/**
 * 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) {
			if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Engels") {
				alert("Please fill in your personal text before ordering this product.");
			}
			else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Duits") {
				alert("Bitte trage ihre pers&#246;nliche text ein bevor sie dieses produkt bestellen.");
			}
			else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Frans") {
				alert("Enregistrez votre texte personnel s'il vous plaît, avant passer le commande pour se produit.");
			}
			else {
				alert("U heeft niet alle bestelvelden ingevuld.");
			}
			return;
		}
	} else {
		text = "";
	}
	text = text.replace (/\s/g, "%20");
	
	// Check stock if it is used...
	if (document.getElementById ("prostock")) {
		var stock         = document.getElementById ("prostock").value;
		var minStock      = document.getElementById ("prominstock").value;
		var noStockAction = document.getElementById ("pronostockaction").value;
	
		if (varId != 0 && document.getElementById ("provarstock_" + varId)) {
			stock = document.getElementById ("provarstock_" + varId).value;
		}
	
		if (amount > stock - minStock) {
			if (noStockAction == 1) {
				if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Engels") {
					if (!confirm ("This product is possibly not in stock.\n\nDo you want to order this product anyway?")) return
				}
				else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Duits") {
					if (!confirm ("Dieses Produkt ist möglich nicht auf Vorrat.\n\nWollen Sie dieses Produkt doch bestellen?")) return
				}
				else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Frans") {
					if (!confirm ("Ce produit n'est pas sur le stock peut-être.\n\nVoulez-vous passer le commande pour se produit quand-même?")) return
				}
				else {
					if (!confirm ("Dit product is mogelijk niet meer op voorraad.\n\nWilt u dit product toch bestellen?")) return
				}
			} else if (noStockAction == 2) {
				if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Engels") {
					alert ("This product cannot be ordered right now due to low stock.");
				} else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Duits") {
					alert ("Dieses Produkt kann jetzt nicht bestellt werden wegen niedrigen Lager.");
				} else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Frans") {
					alert ("Ce produit ne peut être commandé dès maintenant en raison de la faiblesse des stocks.");
				} else {
					alert ("Op dit moment kan dit product niet besteld worden, omdat het uit voorraad is.");
				}
				return;
			}
		}
	}

	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.WinkelwagenLink() }; //_this.stateChange ()
	this.req.send ("wmaction=addline&wmproid=" + proId + "&wmprosid=" + proSId + "&wmamount=" + amount + "&wmvariation=" + varId + "&wmpersonalize=" + text);
	highlightCart();
}


/**
 * 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;

	if (document.getElementById ("prostock_" + sclId)) {
		var stock         = document.getElementById ("prostock_" + sclId).value;
		var minStock      = document.getElementById ("prominstock_" + sclId).value;
		var noStockAction = document.getElementById ("pronostockaction_" + sclId).value;

		if (stock - amount <= minStock) {
			if (noStockAction == 1) {
				if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Engels") {
					if (!confirm ("This product is possibly not in stock.\n\nDo you want to order this product anyway?")) return
				}
				else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Duits") {
					if (!confirm ("Dieses Produkt ist möglich nicht auf Vorrat.\n\nWollen Sie dieses Produkt doch bestellen?")) return
				}
				else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Frans") {
					if (!confirm ("Ce produit n'est pas sur le stock peut-être.\n\nVoulez-vous passer le commande pour se produit quand-même?")) return
				}
				else {
					if (!confirm ("Dit product is mogelijk niet meer op voorraad.\n\nWilt u dit product toch bestellen?")) return
				}
			} else if (noStockAction == 2) {
				if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Engels") {
					alert ("This product cannot be ordered right now due to low stock.");
				} else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Duits") {
					alert ("Dieses Produkt kann jetzt nicht bestellt werden wegen niedrigen Lager.");
				} else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Frans") {
					alert ("Ce produit ne peut être commandé dès maintenant en raison de la faiblesse des stocks.");
				} else {
					alert ("Op dit moment kan dit product niet besteld worden, omdat het uit voorraad is.");
				}
				return;
			}
		}
	}

	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;
	if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Engels") {
		var melding = confirm("Are you sure you want to delete this product?");
	}
	else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Duits") {
		var melding = confirm("Sind Sie sicher Sie wollen dieses Produkt l&#246;schen?");
	}
	else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Frans") {
		var melding = confirm("Vous &#234;tre s&#251;r que vous voulez supprimer ce produit?");
	}
	else {
		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;
	if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Engels") {
		var melding = confirm("Are you sure you want to delete all products in your shopping cart?");
	}
	else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Duits") {
		var melding = confirm("Sind Sie sicher Sie wollen alle Produkten in ihre Einkaufswagen l&#246;schen?");
	}
	else if (document.getElementById("gekozenkleur").getElementsByTagName("input")[2].value == "Frans") {
		var melding = confirm("Vous &#234;tre s&#251;r que vous voulez supprimer tout produits dans votre panier?");
	}
	else {
		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;
	}
}

Cart.prototype.WinkelwagenLink = function() {
	if (this.req.readyState == 4 && this.req.status == 200) {
		var bestellijst = document.getElementById("bigcart");

		// Is bigcart div available?
		if (bestellijst) {
			// Should it be reopened?
			if (bestellijst.style.display == "block") reopen = true;
			// Remove old bigcart
			bestellijst.parentNode.removeChild(bestellijst);
		}

		document.getElementById("shop_cart_placeholder").innerHTML = this.req.responseText;

		var bigCart = document.getElementById("bigcart2");
		bigCart.id = 'bigcart';
		//bigCart.parentNode.removeChild (bigCart);
		document.body.appendChild(bigCart);

		openPopup('bigcart');
	}
}

var opac;

function highlightCart(){
	opac = 0;
	changeOpac();
}

//change the opacity for different browsers 
function changeOpac() {
	var opacity = Math.sin(opac)*50; 
    var object = document.getElementById('carthighlight').style; 
	var placeholder = document.getElementById('carttd2').clientHeight;
	
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")";
	object.display="block";
	object.width="180px";
	object.height=placeholder+"px";
	object.marginTop="-"+placeholder+"px";
	
    opac += (Math.PI/15.7);
    if(opac<Math.PI){
    	setTimeout("changeOpac()",50);
    }
    else{
    	object.display='none';
    }
}
