/*

	This is an update / handler for the Jispro Shop Client
	
	Developed by: 	Marcel Jepma
					Jepma Internet Services.

*/

	lastWindowID = null;
	windowHeight = 0;
	modifyInProgress = '0';
	function contentHeight(){
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
			$('layoutHolder').style.height = (myHeight -18) + 'px;';
			$('innerContent').style.height = (myHeight - 242) + 'px;';
			$('innerContent').style.overflow = 'auto;';
			windowHeight = (myHeight - 242);
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
			$('layoutHolder').style.height = (myHeight -18);
			$('innerContent').style.height = (myHeight - 252);
			$('innerContent').style.overflow = "auto";
			windowHeight = (myHeight - 252);
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
			$('layoutHolder').style.height = (myHeight -18);
			$('innerContent').style.height = (myHeight - 252);
			$('innerContent').style.overflow = "auto";
			windowHeight = (myHeight - 252);
		}	
	}
	
	function showShoppingcart(){
		loadPage('content.php?page=shoppingcartView&view=Full');
	}
	
	function closeShoppingcart(){
		Windows.closeAll();
	}
	addProduct = function(productID, amount,fullList){
		if(fullList != '1')
		{
			if(amount == '-1')	amount = '0';
			new Ajax.Request('index.php?page=shoppingcartAdd&productID=' + productID + '&productAmount=' + amount,
				{
					method:'get',
					// Handle successful response
					
					onSuccess: function(t) {
						// Indien t.responeText != '' dan hebben we te maken met een error. Deze moeten we netjes afhandelen.
						$('shoppingcart').innerHTML = t.responseText;
					},
					// Handle 404
					on404: function(t) {
						alert('Error 404: location "' + t.statusText + '" was not found.');
					},
					// Handle other errors
					onFailure: function(t) {
						alert('Error ' + t.status + ' -- ' + t.statusText);
					}
				}
			);
		}else{
			var PostBody;
			// We gaan de postBody genereren.
			var inputs = document.getElementsByTagName('input');
			var tmpVar;
			var identifier = 0;
			PostBody = '';
			box = '1';
			for(var i=0, inp; inp = inputs[i]; i++) {
				splitValue = inp.id.split('_');
				if(splitValue[0] == 'amount2'){
					if(chkObject(false,'AMOUNT_H' + splitValue[1]) == true){
						box = '0';
					}
					PostBody = PostBody + '&item' + identifier + '=' + splitValue[1] + '_' + escape($(inp.id).value);
					identifier++;
				}
			}
			
			new Ajax.Request('index.php?page=shoppingcartAdd' + PostBody,
				{
					method:'get',
					// Handle successful response
					
					onSuccess: function(t) {
						// Indien t.responeText != '' dan hebben we te maken met een error. Deze moeten we netjes afhandelen.
						$('shoppingcart').innerHTML = t.responseText;
						//alert(t.responseText);
						message = 'Uw winkelwagen is ververst.<br/>';
						message = message + '<a onClick="showShoppingcart();" style="cursor:pointer;"><img src="images/icon_shoppingcart.gif" border="0" align="top">&nbsp;Klik hier om de bestelling af te ronden.</a><br/><br/>';
						message = message + 'Dit venster sluit automatisch over een aantal seconden.<br/>';
						if(box == '1')	showMessage('Toegevoegd aan Winkelwagen',message);
					},
					// Handle 404
					on404: function(t) {
						alert('Error 404: location "' + t.statusText + '" was not found.');
					},
					// Handle other errors
					onFailure: function(t) {
						alert('Error ' + t.status + ' -- ' + t.statusText);
					}
				}
			);
		}
	}
	addProductList = function(){
		var PostBody;
		// We gaan de postBody genereren.
		var inputs = document.getElementsByTagName('input');
		var tmpVar;
		var identifier = 0;
		PostBody = '';
		
		for(var i=0, inp; inp = inputs[i]; i++) {
			splitValue = inp.id.split('_');
			if(splitValue[0] == 'amount2'){
				
				PostBody = PostBody + '&item' + identifier + '=' + splitValue[1] + '_' + escape($(inp.id).value);
				identifier++;
			}
		}
		
		new Ajax.Request('index.php?page=shoppingcartAdd' + PostBody,
			{
				method:'get',
				// Handle successful response
				
				onSuccess: function(t) {
					// Indien t.responeText != '' dan hebben we te maken met een error. Deze moeten we netjes afhandelen.
					$('shoppingcart').innerHTML = t.responseText;
					//alert(t.responseText);
					message = 'De producten uit uw productlijst zijn toegevoegd aan uw winkelwagen.<br/>';
					message = message + '<a onClick="showShoppingcart();" style="cursor:pointer;"><img src="images/icon_shoppingcart.gif" border="0" align="top">&nbsp;Klik hier om de bestelling af te ronden.</a><br/><br/>';
					message = message + 'Dit venster sluit automatisch over een aantal seconden.<br/>';

					showMessage('Toegevoegd aan Winkelwagen',message);
				},
				// Handle 404
				on404: function(t) {
					alert('Error 404: location "' + t.statusText + '" was not found.');
				},
				// Handle other errors
				onFailure: function(t) {
					alert('Error ' + t.status + ' -- ' + t.statusText);
				}
			}
		);

	}
	modifyProductOnChange = function(eventH,productID,amount){
		if (eventH.keyCode == 13) {
			addProduct(productID,$('amount2_' + productID).value,'0');
			if(chkObject(false,'amount2_' + productID) == true){
				reloadShoppingcart();
			}
		}else{
			if(chkObject(false,'AMOUNT_H' + productID) == true){
				if($('AMOUNT_H' + productID).value != $('amount2_' + productID).value){
					if($('AMOUNT_B' + productID).style.display == 'none'){
						$('AMOUNT_B' + productID + '').style.display = 'block';
					}
				}
			}
		}
	}
	function reloadShoppingcart(){
		loadPage('content.php?page=shoppingcartView&view=Full');
	}
	function loadOrderForm(){
		// We gaan het formulier laden welke moet worden ingevuld indien de bestelling kan worden voltooid.
		loadPage('content.php?page=loadShopORDER');

		if($('innerContent').innerHTML == ''){
			reloadShoppingcart();
		}
	}
	
	function placeOrder(){
			var PostBody;
			var deliveryDate;
			
			var currentTime = new Date()
			var month = currentTime.getMonth() + 1
			var day = currentTime.getDate()
			var year = currentTime.getFullYear()
			currentDate  = day + "-" + month + "-" + year;
//			deliveryDate = $('day').value + '/' + $('month').value + '/' + $('year').value;
			deliveryDate = $('deliverydate').value;
						// We gaan de postBody genereren.
	
			var inputs = document.getElementsByTagName('input');
			var tmpVar;
			PostBody = '';
			PostBody = PostBody + '&company' + '='+ escape($('company').value) + '&';
			PostBody = PostBody + '&firstname' + '='+ escape($('firstname').value) + '&';
			PostBody = PostBody + '&lastname' + '='+ escape($('lastname').value) + '&';
			PostBody = PostBody + '&street' + '='+ escape($('street').value) + '&';
			PostBody = PostBody + '&number' + '='+ escape($('number').value) + '&';
			PostBody = PostBody + '&city' + '='+ escape($('city').value) + '&';
			PostBody = PostBody + '&postalcode' + '='+ escape($('postalcode').value) + '&';
			PostBody = PostBody + '&phone' + '='+ escape($('phone').value) + '&';
			PostBody = PostBody + '&email' + '='+ escape($('email').value) + '&';
			PostBody = PostBody + '&fax' + '='+ escape($('fax').value) + '&';
			
			if($('delivery').checked == true){
				PostBody = PostBody + '&delivery' + '=true&';
			}else{
				PostBody = PostBody + '&delivery' + '=false&';
			}

			PostBody = PostBody + '&deliverydate' + '='+ escape(deliveryDate) + '&';
			PostBody = PostBody + '&deliverytime' + '='+ escape($('deliverytime').value) + '&';
			PostBody = PostBody + '&comments' + '='+ escape($('comments').value) + '&';
			
			var opt = {
				// Use POST
				method: 'post',
				postBody: PostBody,
				evalScripts: true,
				// Handle successful response
				onSuccess: function(t) {
					// Als we geen response krijgen, succesvol bericht weergeven:
					if(t.responseText == ''){
						
						message = 'Succesvol besteld!<br>U krijgt de bevesteging in uw mailbox.<br/>';
						if(currentDate == deliveryDate){
							message = message + 'Aangezien de bestelling vandaag nog moet worden afgehandeld, dient u dit telefonisch aan ons te melden.<br/>U kunt ons bereiken op: 0592 - 316087';
						}
						
						showMessage('Besteld',message);
						reloadShoppingcart();
						loadPage('content.php?page=loadShop');
					}else{
						// Anders, alle fouten weergeven en teruggaan naar het formulier:
						showMessage('Foutmelding',t.responseText);
					}
					
					
				},
				// Handle 404
				on404: function(t) {
					alert('Error 404: location "' + t.statusText + '" was not found.');
				},
				// Handle other errors
				onFailure: function(t) {
					alert('Error ' + t.status + ' -- ' + t.statusText);
				}
			}
			new Ajax.Request('index.php?page=loadShopORDER', opt);
	}
	
	function showConditions(){
		if (lastWindowID == null) {
			Element.hide('topMenu');
			lastWindowID = Dialog.alert(
				{
					url: 'index.php?page=loadPage&id=13', 
					options: 
					{
						method: 'get',
						evalScripts: true
					}
				}, 
				{
					windowParameters: 
					{
						className: "darkX", 
						width:'760',
						height:'400',
						title:"Voorwaarden online winkel",
						minimizable: false,
						maximizable: false,
						draggable: false,
						closable: true,
						okLabel: 'Sluit Venster'
					}
				}
			);
			
			windowsHandler = Windows;
			
			myObserver = { 
				onDestroy: function(eventName, win) {
					lastWindowID = null;
					Windows.removeObserver(this); 
					Element.show('topMenu');
				} 
			} 
			Windows.addObserver(myObserver);
		}
	}
	
	function showMessage(title,message){
		Element.hide('topMenu');
		Dialog.info("<br/><br/><div style=\"padding:5px;\">" + message + "</div>", 
			{
				windowParameters: 
				{
					width:450, 
					height:200,
					className: "darkX",
					title:title
				}, 
				showProgress: true
			}
		); 
		function hideConfirm(){
			timeout--; 
				if (timeout >0) { 
					Dialog.setInfoMessage("<br/><br/><div style=\"padding:5px;\">" + message + "" + "<br/><br/>Dit venster sluit in " + timeout + " seconden ...</div>");
					setTimeout(hideConfirm, 1000);
				} else {
					Dialog.closeInfo();
					Element.show('topMenu');
				}
		}
		timeout=3; 
		setTimeout(hideConfirm, 1000) 
	}
	
	addFavorite = function(productID,action){
		/*
			We gaan hier een URL aanroepen wat dit product + Aantal toevoegd aan onze sessie. En dus ook aan de shoppingCart.
			Hierna gaan we onze winkelwagen herladen.
			
			De winkelwagen bevind zich onderin de pagina en is altijd zichtbaar. Indien iemand op de winkelwagen klikt, vouwt deze zichzelf uit!
		*/
		new Ajax.Request('index.php?page=customerFavoriteModify&productID=' + productID,
			{
				method:'get',
				// Handle successful response
				
				onSuccess: function(t) {
					// Indien t.responeText != '' dan hebben we te maken met een error. Deze moeten we netjes afhandelen.
					$('favorites').innerHTML = t.responseText;
					showMessage('Favorieten','Uw favorieten lijst is aangepast.');
					if(action == 'delete'){
						$('favorite_' + productID).style.display = 'block';
					}
				},
				// Handle 404
				on404: function(t) {
					alert('Error 404: location "' + t.statusText + '" was not found.');
				},
				// Handle other errors
				onFailure: function(t) {
					alert('Error ' + t.status + ' -- ' + t.statusText);
				}
			}
		);
		
	}

