var Indexes = new Array();

$(document).ready(function() {
	// indexeer alle rows
	$('table.ProductList tr').each(function() {
		// foreach line, add to the indexer
		var Price = $(this).find('span.Price');
		var Amount = $(this).find('span.Aantal');
		var Total = $(this).find('span.TotalPrice');
	
		if ( Price.length > 0 && Amount.length > 0 && Total.length > 0 ) {
			Indexes.push({Price: Price, Amount: Amount, Total: Total});
		} else {
			// geen interesse, nl geen rij met data
		}
	});
});

/**
 * This function will set the new value in ValueObject of the specific product using AJAX
 * 
 * @return boolean if the new value is succes
 */

function changeQuantity( BasePath, ProductIndex, Identifier, ValueObject ) {
	var ScriptURL = BasePath + 'ShoppingBasket/Ajax.php';
	var QueryString = '';
	var action = null;
	var result = null;
	
	// bepaal de actie
	if ( Identifier == '+' ) 
		action = 11;
	else if ( Identifier == '-' )
		action = 12;
	else
		alert('Unkown action ('+Identifier+')');
	
	QueryString += 'Action=' + action;
	QueryString += '&Product=' + ProductIndex;	

	// Doe de request. --> Synchroon (inzake direct resultaat) en POST 
	result = $.ajax({
		type: 'POST',
		url: ScriptURL,
		data: QueryString,
		async: false
	});
	
	try {
		// Verkrijg de responsetext
		result = result.responseText;

		// Parse the returned json
		result = eval('(' + result + ')');

		if ( result.Error && result.Error.ID != null ) {
			if ( result.Error.SystemError && result.Error.SystemError == true ) {
				alert('Er is een fout opgetreden; foutcode ' + result.Error.ID + "\nOmschrijving: \n\t" + result.Error.Message + "\n\nDump:\n\t" +result.Error.DUMP );
			} else {
				alert(result.Error.Message);
			}
		} else {
			var Subtotal = result.Total.Subtotal;
			var Total = result.Total.Total;
			var Sendtotal = result.Total.Sendcosts;
			var Quantity = result.Total.ProductQuantity;
			
			var LineAmount = result.Line.Amount;
			var LinePrice = result.Line.Price;
			var LineTotal = result.Line.Total;
			
			// Zet de waarde op deze rij.
			$(Indexes[ProductIndex].Total).html(LineTotal);
			$(Indexes[ProductIndex].Amount).html(LineAmount);
			$(Indexes[ProductIndex].Price).html(LinePrice);
			
			// Update de totaal rij
			$('#TotaalprijsExclusief').html(Subtotal);
			$('#Verzendkosten').html(Sendtotal);
			$('#TotaalprijsInclusief').html(Total);
			
			$('#Winkelmand_V').html(Sendtotal);
			$('#Winkelmand_TPI').html(Total);''
			$('#Winkelmand_Q').html(Quantity);
			
			var Winkelmand_BtnPay = document.getElementById('Winkelmand_BtnPay');
			if(Winkelmand_BtnPay!=undefined){				
				if(parseInt(Quantity)>0){
					Winkelmand_BtnPay.style.display = 'block';
				}else{					
					Winkelmand_BtnPay.style.display= 'none';
				}
			}
		}		

		
	} catch ( e ) {
		alert('Unexpected error while parsing the returned server values. ' + "\n\n\t" + e);
	}
}

/* old *
function changeQuantity1( BasePath, ProductIndex, Indentifier, ValueObject ) {
	
	var ScriptURL = BasePath + 'ShoppingBasket/Ajax.php';
	
	var httpObject = getHTTPObject();
		
	if (httpObject != null) {
		//Getting the new value for the product
		var NewValue = document.getElementById( "HoeveelheidProduct" + ProductIndex ).innerHTML;
		
		NewValue = parseInt( NewValue );
		
		if ( Indentifier == "-" ) {
			if( ( NewValue - 1 ) > 0 ) {
				NewValue = NewValue - 1;
			}
		} else if ( Indentifier == "+" ) {
			NewValue = NewValue + 1;
		} else {
			alert( "Ik kan alleen een + en een - berekening, helaas geen " + Indentifier );
		}
		
		//Opening the connection
		httpObject.open( "POST", ScriptURL, false );
		httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		
		//Setting up params and sending them
		var Params = "Action=1&ProductIndex=" + ProductIndex + '&NewValue=' + NewValue;
		httpObject.send( Params );
		
		//Based on the ajax response we will determin if it went ok or not.
		var response = httpObject.responseText;
		
		if ( response == 'true' ) {
			setOutput( httpObject, ValueObject, NewValue );
			
			/**
			 * Calculate the total price of the productline
			 *
			//Opening the connection
			httpObject.open( "POST", ScriptURL, false );
			httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
			
			//Setting up params and sending them
			var Params = "Action=2&ProductIndex=" + ProductIndex + '&HowMany=' + NewValue;
			httpObject.send( Params );
			
			//Based on the ajax response we will determin if it went ok or not.
			var PriceHolder = document.getElementById( 'TotaalPrijsProduct' + ProductIndex );
			
			response = httpObject.responseText;
			
			if ( PriceHolder && response != 'false' ) {	
				var NewPrice = new Number( httpObject.responseText );
				NewPrice = NewPrice.toFixed( 2 );
				var NewPriceFormatted = NewPrice.replace( ".", ",");
				PriceHolder.innerHTML = NewPriceFormatted;
			} else {
				alert( 'Priceholder of product ' + ProductIndex + ' is removed' );
			}
			
			/**
			 * Setting the total price (exclusive)
			 *
			//Opening the connection
			httpObject.open( "POST", ScriptURL, false );
			httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
			
			//Setting up params and sending them
			var Params = 'Action=3';
			httpObject.send( Params );
			
			//Based on the ajax response we will determin if it went ok or not.
			var TotalPriceExclusiveHolder = document.getElementById( 'TotaalprijsExclusief' );
			
			if ( TotalPriceExclusiveHolder && response != 'false' ) {
				var NewTotalPriceEx = new Number( httpObject.responseText );
				NewTotalPriceEx = NewTotalPriceEx.toFixed( 2 )
				var NewTotalPriceExFormatted = NewTotalPriceEx.replace( ".", ",");
				TotalPriceExclusiveHolder.innerHTML = NewTotalPriceExFormatted;
						
			} else {
				alert( 'Total price (ex) could not be set.' );
			}

			/**
			 * Setting the total price (inclusive)
			 *
			//Opening the connection
			httpObject.open( "POST", ScriptURL, false );
			httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
			
			//Setting up params and sending them
			var Params = 'Action=4';
			httpObject.send( Params );
			
			//Based on the ajax response we will determin if it went ok or not.
			var TotalPriceInclusiveHolder = document.getElementById( 'TotaalprijsInclusief' );
			
			if ( TotalPriceInclusiveHolder && response != 'false' ) {
				var NewTotalPriceIncl = new Number( httpObject.responseText );
				NewTotalPriceIncl = NewTotalPriceIncl.toFixed( 2 );
				var NewTotalPriceInclFormatted = NewTotalPriceIncl.replace( ".", ",");
				TotalPriceInclusiveHolder.innerHTML = NewTotalPriceInclFormatted;
			} else {
				alert( 'Totalprice could not be set.' );
			}
			
		} else {
			return false;
			alert( response );
		}
	}
}
*/

// Change the value of the outputText field
function setOutput( httpObject, ValueObject, NewValue ){
	
	/**
	 * 0	The request is not initialized
	 * 1	The request has been set up
	 * 2	The request has been send
	 * 3	The request is in process
	 * 4	The request is complete
	 */
	if( httpObject.readyState == 4 ){
		var Object = document.getElementById( ValueObject );
		
		if( Object ) {
			Object.innerHTML = NewValue;
		} else {
			alert( 'Object not found' );
		}
	} else {
		//I don't want to alert here, but this is not good!
		alert( 'Document still handling process' );
	}
}

// Get the HTTP Object
function getHTTPObject(){
	if( window.ActiveXObject ) { 
		// Object in IE7+, Firefox, Chrome, Opera, Safari
		return new ActiveXObject( "Microsoft.XMLHTTP" );
	} else if( window.XMLHttpRequest ) { 
		// Object in IE6, IE5
		return new XMLHttpRequest();
	} else {
		//No AJAX in this browser :-(
		alert( "Your browser does not support AJAX." );
		return null;
	}
}

