	/**
	 *
	 *	iShop - Thinc1st - PowerFull AJAX Webshop
	 *	--------------------------------------------
	 *	version: 			1.0
	 *	author:	 			Richard Edens
	 *	description:		Javascript to use with the Webshop Functionality within iCMS.
	 *	creationdate:		10 okt. 2007.
	 *
	 */

	function rand ( n )
	{
	  return ( Math.floor ( Math.random ( ) * n + 1 ) );
	}

	// Variables.
	var discountCode = "";						// Sets the discount!
	var http_request = false; 					// Sets the HTTP request container
	var lng = "";								// Sets the language used within the webshop.
	var sess = "";								// Sets the sesion variable used!
	var pos = "";								// Current position.
	var myDate=new Date();
	var shopBag;								// For closing the site check if the shoping bag is more then one!
	var scrollX;
	var scrollY;
	var totalProducts;
	var noProductReturnUrl = "";

	// Alert Box!
	var abName;
	var abLink;

	// Product list vars
	var plColumns = "";
	var plColumnsNames = "";
	var plExtraNames = "";
	var plColors = "";
	var plClasses = "";
	var plDesigns = "";
	var plMoney = "";

	var fncSetHandlingShippingCountry = "";

	function makeRequest(myUrl,myType){
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}
		} 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('Cannot create XMLHTTP instance');
				return false;
			}
		}

		// Webshop options.
		if (myType == "wsAddProduct"){
			http_request.onreadystatechange = wsAddProduct_result;
		}
		if (myType == "wsResetProductList"){
			http_request.onreadystatechange = wsResetProductList_result;
		}
		if (myType == "wsRemoveProduct"){
			http_request.onreadystatechange = wsRemoveProduct_result;
		}
		if (myType == "wsRemoveMultipleProducts"){
			http_request.onreadystatechange = wsRemoveMultipleProducts_result;
		}
		if (myType == "wsTotalAmountProducts"){
			http_request.onreadystatechange = wsTotalAmountProducts_result;
		}
		if (myType == "wsProductList"){
			http_request.onreadystatechange = wsProductList_result;
		}
		if (myType == "wsVname"){
			http_request.onreadystatechange = wsVname_result;
		}

		// Alert Box
		if (myType == "abCreateBox"){
			http_request.onreadystatechange = abCreateBox_result;
		}

		http_request.open('GET', myUrl, true);
		http_request.send(null);
	}

	// When added a product, also update the amount of products in the shoppingbag
	function wsVname_result() {

	   if (http_request.readyState == 4) {
	       if (http_request.status == 200) {

	           if(http_request.responseText == "OK"){

	           		// Winkelmandje aanpassen
					wsTotalAmountProducts();


	           } else {
	           	    alert("We could not set the virtual product name!\n\n" + http_request.responseText);
	           }
	       } else {
	           alert('There was a problem with the request.');
	       }
	   }

	}

	// When added a product, also update the amount of products in the shoppingbag
	function wsAddProduct_result() {

	   if (http_request.readyState == 4) {
	       if (http_request.status == 200) {
	           if(http_request.responseText == "OK"){

	           		// Is alles ok??
	           		//alert(http_request.responseText);

	           		// Winkelmandje aanpassen
					wsTotalAmountProducts();

	           } else {
	           	    alert("We could not add the product!\n\n" + http_request.responseText);
	           }
	       } else {
	           alert('There was a problem with the request.');
	       }
	   }

	}

	// Resets the shopResult list, and sets the amount in the shopping bag to 0
	function wsRemoveProduct_result() {

	   if (http_request.readyState == 4) {
	       if (http_request.status == 200) {

	       		// Winkelmandje aanpassen
				wsTotalAmountProducts();

	       } else {
	           alert('There was a problem with the request.');
	       }
	   }

	}

	// Resets the shopResult list, and sets the amount in the shopping bag to 0
	function wsRemoveMultipleProducts_result() {

	   if (http_request.readyState == 4) {
	       if (http_request.status == 200) {

	       		// Winkelmandje aanpassen
				wsTotalAmountProducts();

	       } else {
	           alert('There was a problem with the request.');
	       }
	   }

	}

	// Resets the shopResult list, and sets the amount in the shopping bag to 0
	function wsResetProductList_result() {

	   if (http_request.readyState == 4) {
	       if (http_request.status == 200) {

	       		// Winkelmandje aanpassen
					wsTotalAmountProducts();

	       } else {
	           alert('There was a problem with the request.');
	       }
	   }

	}

	function wsTotalAmountProducts_result() {

	   if (http_request.readyState == 4) {
	       if (http_request.status == 200) {
	           	try{

	           		// We onthouden wat er in het winkelmandje zit in javascript.
	           		shopBag = http_request.responseText;

	           		totalProducts = shopBag;

	           		// Check if we need to exit this page!
	           		wsCheckProductAmount();

	           		// We zetten de hoeveelheid producten die in het winkelmandje zitten.
	           		document.getElementById("shopBag").innerHTML = shopBag;

	           		// We laten de product lijst zien als dat mogelijk is.
	           		//alert(plColumns);
	           		//alert(plColors);

	           		wsProductList(plColumns,plColumnsNames,plExtraNames,plColors,plClasses,plDesigns,plMoney);

	           	} catch(e){
	           		alert("We could not set the shopping bag amount.\n\n" + http_request.responseText + "\r\n" + e);
	           	}
	       } else {
	           alert('There was a problem with the request.');
	       }
	   }

	}

	function so_clearInnerHTML(obj) {
		// perform a shallow clone on obj
		nObj = obj.cloneNode(false);
		// insert the cloned object into the DOM before the original one
		obj.parentNode.insertBefore(nObj,obj);
		// remove the original object
		obj.parentNode.removeChild(obj);
	}

	function wsProductList_result() {

	   if (http_request.readyState == 4) {
	       if (http_request.status == 200) {
	           	try{
	           		so_clearInnerHTML(document.getElementById("productList"));
	           		document.getElementById("productList").innerHTML = http_request.responseText;
	           	} catch(e){
	           		//alert("We could not set the product list in HTML page. " + e);
	           	}
	       } else {
	           alert('There was a problem with the request.');
	       }
	   }

	}

   /**
	*
	* iShop functions!!
	*
	*/

	function wsAddProduct(id, vid, snelstartline, pname, amount, price, discount, tax, taxsys, weight, giftwrapcosts, shippingcosts, barcode, product_options){

		//alert("adding product:\n" + id + "\n" + vid + "\n" + snelstartline + "\n" + pname + "\n" + amount + "\n" + price + "\n" + discount + "\n" + tax + "\n" + product_options + " : session" + sess)

		// Preset productoptions.
		product_options_result = "";


			// Go trew extra options if needed!
			if(product_options != "" || product_options != "false"){

				// First lets split the options.
				opts = product_options.split(",");
				optsnr = opts.length;

				// Second lets go trew the options.
				for(i=0;i<optsnr;i++){

					// Get the new extra option to read.
					opt = opts[i];

					// Get the name and type
					opt_opts = opt.split(":");

					name = opt_opts[0];
					type = opt_opts[1];
					realname = opt_opts[2];

					try{

						val = "";

						if(type == "select"){
							var selectObj = document.getElementById(name);
							val = selectObj.options[selectObj.selectedIndex].value;
						}
						if(type == "radio"){
							var radioObj = document.getElementsByName(name);
							for(j=0; j < radioObj.length; j++) {
								if(radioObj[j].checked == true) {
									val = radioObj[j].value;
								}
							}
						}
						if(type == "line"){
							val = document.getElementById(name).value;
						}
						if(type == "password"){
							val = document.getElementById(name).value;
						}
						if(type == "hidden"){
							val = document.getElementById(name).value;
						}
						if(type == "amount"){
							val = document.getElementById(name).value;
						}

						product_options_result = product_options_result + realname + ":" + type + ":" + val + ",";

					} catch(err){
						txt="iShop - Versie 1.0.\n\n";
						txt+="Er zit een fout in uw BLOG voor de vormgeving van dit product.\n";
						txt+="U heeft welligt een virtueel product willen tonen.\nVergeet niet voor ieder aantal veld een {opt} te plaatsen.\n\n";
						txt+="Error description: " + err.description + "\n\n";
						txt+="We wilden veld: " + name + " type " + type + " realname " + realname + " uitlezen\n\n";
						txt+="Click OK to continue.\n\n";
						alert(txt);
					}


				}

			}

		url = "/webshop.php?action=TOEVOEGEN&id=" + id + "&vid=" + vid + "&snelstartline=" + snelstartline + "&name=" + pname + "&amount=" + amount + "&price=" + price + "&discount=" + discount + "&tax=" + tax + "&taxsys=" + taxsys + "&weight=" + weight + "&giftwrapcosts=" + giftwrapcosts + "&shippingcosts=" + shippingcosts + "&barcode=" + barcode + "&product_options=" + product_options_result + "&" + sess + "&lng=" + lng + "&rnd=" + myDate.getTime() + "&rndnbr=" +  rand ( 1000000 );
		//alert(url);
		makeRequest(url,"wsAddProduct");
	}

	function wsVname(vid, naam){
		url = "/webshop.php?action=VNAME&vid=" + vid + "&naam=" + naam + "&" + sess + "&lng=" + lng + "&rnd=" + myDate.getTime() + "&rndnbr=" +  rand ( 1000000 );
		makeRequest(url,"wsVname");
	}

	function wsResetProductList(){
		url = "/webshop.php?action=RESET&" + sess + "&lng=" + lng + "&rnd=" + myDate.getTime() + "&rndnbr=" +  rand ( 1000000 );
		makeRequest(url,"wsResetProductList");
	}

	function wsRemoveProduct(arg){
		url = "/webshop.php?action=VERWIJDEREN&record=" + arg + "&" + sess + "&lng=" + lng + "&rnd=" + myDate.getTime() + "&rndnbr=" +  rand ( 1000000 );
		makeRequest(url,"wsRemoveProduct");
	}

	function wsRemoveMultipleProducts(arg){
		url = "/webshop.php?action=MULTI_VERWIJDEREN&records=" + arg + "&" + sess + "&lng=" + lng + "&rnd=" + myDate.getTime() + "&rndnbr=" +  rand ( 1000000 );
		makeRequest(url,"wsRemoveMultipleProducts");
	}

	function wsTotalAmountProducts(){
		url = "/webshop.php?action=TOTAALWINKELMANDJE&" + sess + "&lng=" + lng + "&rnd=" + myDate.getTime() + "&rndnbr=" +  rand ( 1000000 );
		makeRequest(url,"wsTotalAmountProducts");
	}

	function wsProductList(columns,columnsnames,extranames,colors,classes,designs,moneysign){
		plColumns = columns;
		plColumnsNames = columnsnames;
		plExtraNames = extranames;
		plColors = colors;
		plClasses = classes;
		plDesigns = designs;
		plMoney = moneysign;
		if(fncSetHandlingShippingCountry == ""){
			url = "/webshop.php?action=PRODUCTLIJST&columns=" + columns + "&designs=" + designs + "&columnnames=" + columnsnames + "&extranames=" + extranames + "&colors=" + colors + "&classes=" + classes + "&moneysign=" + moneysign + "&" + sess + "&lng=" + lng + "&rnd=" + myDate.getTime() + "&rndnbr=" +  rand ( 1000000 );
		} else {
			url = "/webshop.php?action=PRODUCTLIJST&fncSetHandlingShippingCountry=" + fncSetHandlingShippingCountry + "&columns=" + columns + "&designs=" + designs + "&columnnames=" + columnsnames + "&extranames=" + extranames + "&colors=" + colors + "&classes=" + classes + "&moneysign=" + moneysign + "&" + sess + "&lng=" + lng + "&rnd=" + myDate.getTime() + "&rndnbr=" +  rand ( 1000000 );
		}
		makeRequest(url,"wsProductList");
	}

	function wsShowBigImage(arg){
		toMoveObject = arg;
	}

	function wsHideBigImage(){
		document.getElementById(toMoveObject).style.display = "none";
		toMoveObject = false;
	}

	function wsSetProductReturnUrl(arg){
		noProductReturnUrl = arg;
	}

	function wsCheckProductAmount(){
		if(noProductReturnUrl != ""){
			if(totalProducts == 0){
				document.location.href = noProductReturnUrl;
			}
		}
	}

   /**
	*
	* iShop Extra functions!
	*
	*/
	function wsSetLanguage(lng1,sess1){
		lng = lng1;
		sess = sess1;
	}

   /**
	*
	* iShop Go to page when no products are selected!!
	*
	*/
	function wsGoToPageWhenNoProducts(pagel,sessl){
		document.location.href = pagel + "?" + sessl;
	}

   /**
	*
	* iShop Close functions!
	*
	*/
	function wsClosePage(){
		//alert("Ok! " + shopBag);
		shopBag = parseInt(shopBag);
		if(lng == "NL"){
			message = "U heeft nog producten in uw winkelmandje staan weet u zeker dat u de website wilt verlaten?";
		}
		if(lng == "EN"){
			message = "You still have products in your shopping bag, are you sure you want to leave the site?";
		}
		if(shopBag != NaN && shopBag > 0){
			if(confirm(message) == true){
				return true;
			} else {
				return false;
			}
		}
	}

	function wsSaveScrollCoordinates() {
	  	scrollX = (document.all)?document.body.scrollLeft:window.pageXOffset;
	  	scrollY = (document.all)?document.body.scrollTop:window.pageYOffset;
	}

	/**
	 *
	 *	iShop Alert Box!
	 *	-------------------------------
	 *	Eigenlijk geprogrammeerd als los onderdeel maar verwikkeld in dit Javascript om laadtijd te voorkomen!
	 *
	 */


	// When added a product, also update the amount of products in the shoppingbag
	function abCreateBox_result() {

	   if (http_request.readyState == 4) {
	       if (http_request.status == 200) {

           		// Is alles ok??
           		if (document.layers) {
					w = window.innerWidth;
					h = window.innerHeight;
				} else if (document.all) {
					w = document.body.clientWidth;
					h = document.body.clientHeight;
				} else if (document.getElementById) {
					w = window.innerWidth;
					h = window.innerHeight;
				}

				/* Set width height of layer. */
           		document.getElementById(abName).style.width = w;
           		document.getElementById(abName).style.height = h;

				/* Set innerHTML */
           		document.getElementById(abName).innerHTML = http_request.responseText;

				/* Show the damn layer! */
           		document.getElementById(abName).style.display = "block";

	       } else {
	           alert('There was a problem with the request.');
	       }
	   }

	}

	function abCreateBox(alertBoxName,alertBoxLink,blog){
		abName = alertBoxName;
		abLink = alertBoxLink;
		url = "/alertbox.php?blog="+blog+"&rndnbr=" +  rand ( 1000000 );
		makeRequest(url,"abCreateBox");
	}