	/**
	 *
	 *	Time2act - PowerFull AJAX Webshop
	 *	--------------------------------------------
	 *	version: 			1.0
	 *	author:	 			Richard Edens
	 *	description:		Javascript to use with the Webshop Functionality within the Time2act Website Manager.
	 *	creationdate:		19 augst. 2006.
	 *
	 */

   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 discountCode = "";						// Sets the discount!
   var highbtw = "";
   var lowbtw = "";
   
   var artikelnr = "";
   var productnaam = "";
   var prijs = "";
   var korting = "";
   var verpakingskosten = "";
   var verzendkosten = "";
   var totaal_prijs = "";
   var opties = "";
   
   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;
			}
		}
		if (myType == "wsAddProduct"){
			http_request.onreadystatechange = wsAddProduct_result; 
		}
		if (myType == "wsShowProductAmount"){
			http_request.onreadystatechange = wsShowProductAmount_result; 
		}
		if (myType == "wsGetProductAmount"){
			http_request.onreadystatechange = wsGetProductAmount_result; 
		}
		if (myType == "wsGetProductListResult"){
			http_request.onreadystatechange = wsGetProductListResult_result; 
		}
		if (myType == "wsResetProductList"){
			http_request.onreadystatechange = wsResetProductList_result;
		}
		if (myType == "wsRemoveProduct"){
			http_request.onreadystatechange = wsRemoveProduct_result;
		}
		if (myType == "wsGetAllInfo"){
			http_request.onreadystatechange = wsGetAllInfo_result;
		}
		if (myType == "wsCalcDiscount"){
			http_request.onreadystatechange = wsCalcDiscount_result;
		}
		http_request.open('GET', myUrl, true); 
		http_request.send(null); 
   } 

   /**
    *
    *	Time2act
    *	--------------------------------------------------------
    *	The following functions are done when we get answers from the webshop system.
    *	some functions are connected to each other, and for a good reson.
    *	Otherwise the logics of a webshop wont function.
    *
    *	-------------------------------------------------------------
    *	AddProducts					calls:		GetProductAmount	calls:		GetAllInfo
    *	GetProductAmount			calls:		GetAllInfo
	*	GetProductList				calls:		GetProductAmount	calls:		GetAllInfo
	*	RemoveProduct				calls:		GetProductList		calls:		GetProductAmount	calls:		GetAllInfo
    *	ResetProductList			calls:		GetProductAmount	calls:		GetAllInfo
    *	GetAllInfo
    *
    */
   
   // 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"){
   					url = "/webshop.php?action=TOTAALWINKELMANDJE&"+sess+"&lng="+lng;
   					makeRequest(url,"wsGetProductAmount");
               } else {
               	alert(http_request.responseText);
               }
           } else { 
               alert('There was a problem with the request.'); 
           } 
       } 

   } 
   
   // Show the amount inside a special DIV called "shopBag", implemented with the string [webShopBag] inside a template
   function wsGetProductAmount_result() { 

       if (http_request.readyState == 4) { 
           if (http_request.status == 200) { 
           	   try{
               		document.getElementById('shopBag').innerHTML = http_request.responseText;
           	   } catch(e){
           	   }
               // Also show the other settings then!
               url = "/webshop.php?action=LIJST&"+sess+"&lng="+lng+"&kortingscode="+discountCode+"&highbtw="+highbtw+"&lowbtw="+lowbtw;
			   makeRequest(url,"wsGetAllInfo");
           } else { 
               alert('There was a problem with the request.'); 
           } 
       } 

   } 
   
   // Show the products result inside a special DIV called "shopResult", implemented with the string [webShopResult] inside a template
   function wsGetProductListResult_result() { 

       if (http_request.readyState == 4) { 
           if (http_request.status == 200) {
           	   try{
               		document.getElementById('shopResult').innerHTML = http_request.responseText;
               } catch(e){
           	   }
               // Set the product amount, incase of change!
               url = "/webshop.php?action=TOTAALWINKELMANDJE&"+sess+"&lng="+lng;
			   makeRequest(url,"wsGetProductAmount");
           } else { 
               alert('There was a problem with the request.'); 
           } 
       } 

   } 
   
    // Resets the shopResult list, and sets the amount in the shopping bag to 0
   function wsCalcDiscount_result() { 

       if (http_request.readyState == 4) { 
           if (http_request.status == 200) {
           		// Remove Product
		   		url = "/webshop.php?action=PRODUCTLIJST&"+sess+"&lng="+lng+"&kortingscode="+discountCode+"&highbtw="+highbtw+"&lowbtw="+lowbtw+"&VIS_artikelnr="+artikelnr+"&VIS_productnaam="+productnaam+"&VIS_prijs="+prijs+"&VIS_korting="+korting+"&VIS_verpakingskosten="+verpakingskosten+"&VIS_verzendkosten="+verzendkosten+"&VIS_totaal_prijs="+totaal_prijs+"&VIS_opties="+opties;
				makeRequest(url,"wsGetProductListResult");
           } 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) {
           		// Remove Product
		   		url = "/webshop.php?action=PRODUCTLIJST&"+sess+"&lng="+lng+"&kortingscode="+discountCode+"&highbtw="+highbtw+"&lowbtw="+lowbtw+"&VIS_artikelnr="+artikelnr+"&VIS_productnaam="+productnaam+"&VIS_prijs="+prijs+"&VIS_korting="+korting+"&VIS_verpakingskosten="+verpakingskosten+"&VIS_verzendkosten="+verzendkosten+"&VIS_totaal_prijs="+totaal_prijs+"&VIS_opties="+opties;
				makeRequest(url,"wsGetProductListResult");
           } 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) {
				url = "/webshop.php?action=PRODUCTLIJST&"+sess+"&lng="+lng+"&kortingscode="+discountCode+"&highbtw="+highbtw+"&lowbtw="+lowbtw+"&VIS_artikelnr="+artikelnr+"&VIS_productnaam="+productnaam+"&VIS_prijs="+prijs+"&VIS_korting="+korting+"&VIS_verpakingskosten="+verpakingskosten+"&VIS_verzendkosten="+verzendkosten+"&VIS_totaal_prijs="+totaal_prijs+"&VIS_opties="+opties;
				makeRequest(url,"wsGetProductListResult");
           } else { 
               alert('There was a problem with the request.'); 
           } 
       } 

   } 
   
   // Gets all other info, like price total amount, shipping costs etc..
   function wsGetAllInfo_result(){
   		if (http_request.readyState == 4) { 
           if (http_request.status == 200) {
				str = http_request.responseText;
				parts = str.split("&");
				l = parts.length;
				for(i=0;i<l;i++){
					part = parts[i];
					if(part != ""){
						secparts = part.split("=");
						fr = secparts[0];
						bk = secparts[1];
						try{
							document.getElementById(fr).innerHTML = bk;
						} catch (e) {
						}	
					}
				}
           } else { 
               alert('There was a problem with the request.'); 
           } 
       } 
   }
   
   // Show the amount in your shopping bag.
   function wsShowProductAmount_result() { 

       if (http_request.readyState == 4) { 
           if (http_request.status == 200) {
               alert(http_request.responseText); 
           } else { 
               alert('There was a problem with the request.'); 
           } 
       } 

   } 
   
   /**
    *
    * Time2act Influence the product list functions!
    *
    */
    
   function wsAddProduct(id,product,price,price_type,discount,giftwrap_costs,giftwrap,vat,sendcosts){
   		url = "/webshop.php?action=TOEVOEGEN&productid="+id+"&product="+product+"&price="+price+"&type_price="+price_type+"&discount="+discount+"&verpakkingskosten="+giftwrap_costs+"&verpakken="+giftwrap+"&verzendkosten="+sendcosts+"&btw="+vat+"&"+sess+"&lng="+lng;
   		makeRequest(url,"wsAddProduct");
   }
      
   function wsResetProductList(){
   		url = "/webshop.php?action=RESET&"+sess+"&lng="+lng;
		makeRequest(url,"wsResetProductList");
   }
   
   function wsRemoveProduct(arg){
   		url = "/webshop.php?action=VERWIJDEREN&record="+arg+"&"+sess+"&lng="+lng;
		makeRequest(url,"wsRemoveProduct");
   }
   
   /**
    *
    * Time2act Getting updates on what is within the shoppingbag.
    *
    */
   function wsGetProductAmount(){
   		url = "/webshop.php?action=TOTAALWINKELMANDJE&"+sess+"&lng="+lng;
		makeRequest(url,"wsGetProductAmount");
   }
   
   function wsGetProductListResult(artikelnr1,productnaam1,prijs1,korting1,verpakingskosten1,verzendkosten1,totaal_prijs1,opties1){
   	
	   	artikelnr = artikelnr1;
	   	productnaam = productnaam1;
	   	prijs = prijs1;
	   	korting = korting1;
	   	verpakingskosten = verpakingskosten1;
	   	verzendkosten = verzendkosten1;
	   	totaal_prijs = totaal_prijs1;
	   	opties = opties1;
   	
   		url = "/webshop.php?action=PRODUCTLIJST&"+sess+"&lng="+lng+"&kortingscode="+discountCode+"&highbtw="+highbtw+"&lowbtw="+lowbtw+"&VIS_artikelnr="+artikelnr+"&VIS_productnaam="+productnaam+"&VIS_prijs="+prijs+"&VIS_korting="+korting+"&VIS_verpakingskosten="+verpakingskosten+"&VIS_verzendkosten="+verzendkosten+"&VIS_totaal_prijs="+totaal_prijs+"&VIS_opties="+opties;
		makeRequest(url,"wsGetProductListResult");
   }

   function wsGetAllInfo(){
   		url = "/webshop.php?action=LIJST&"+sess+"&lng="+lng+"&kortingscode="+discountCode+"&highbtw="+highbtw+"&lowbtw="+lowbtw;
   		makeRequest(url,"wsGetAllInfo");
   }
   
   function wsCalcDiscount(){
   		try{
   			discountCode = document.getElementById('discountCode').value;
   		} catch(e){
   			alert(e);
   		}
   		url = "/webshop.php?action=SET_DISCOUNT&"+sess+"&lng="+lng+"&kortingscode="+discountCode;
   		makeRequest(url,"wsCalcDiscount");
   }

   /**
    *
    * Time2act Extra functions!
    *
    */
   function wsSetLanguage(lng1,sess1){
   		lng = lng1;
   		sess = sess1;
   }
   
   function wsSetAllInfo(highbtw1,lowbtw1){
   		try{
   			discountCodeObj = getObj("discountCode");
   			discountCode = discountCodeObj.value;
   		} catch(e) {
   			discountCode = "";
   		}
   		highbtw = highbtw1;
   		lowbtw = lowbtw1;
   }
   
   function wsShowProductAmount(){
   		url = "/webshop.php?action=TOTAALWINKELMANDJE&"+sess+"&lng="+lng;
   		makeRequest(url,"wsShowProductAmount");
   }