/***************************************************************************
* Currency symbol for the site *
***************************************************************************/
var currencySymbol = '&pound;';

/***************************************************************************
* Write the table of items stored in the array *
***************************************************************************/
function writeCartTable(array){
	if(array.length){
						document.write('<form id="cartForm" style="margin:0px;border:0px;" name="cartForm" target="_blank" action="https://www.paypal.com/cgi-bin/webscr" method="post">');
				document.write('<table id="cartTable" cellpadding="0" cellspacing="0">');
		document.write('<tr>');
		document.write('<th>Photo</th>');
		document.write('<th>Caption</th>');
		document.write('<th>Option</th>');
		document.write('<th>Price</th>');
		document.write('<th>Quantity</th>');
		document.write('<th>Total price</th>');
		document.write('<td><input type="Button" value="Clear cart" class="button" onmouseover="this.className=\'buttonHi\';" onmouseout="this.className=\'button\';" onclick="if(confirm(\'Clear items?\')){deleteCookie(\'JACKDYLANPRESTON\'); window.location.reload(false);}"></td>');
				document.write('</tr>');
				for(var i=0; i<array.length; i++){
			id = getArrayIndex(photos, array[i].id);
			if(array[i].payment_id != 0){
				payment_id = getArrayIndex(paymentOptions, array[i].payment_id);
				option = paymentOptions[payment_id].payment_option;
				price = paymentOptions[payment_id].price;
			}
			else{
				option = photos[id].purchase_instruction;
				price = photos[id].item_price;
			}
			caption = photos[id].caption;
			src = photos[id].thumbnail;
			if(option.length == 0){
				option = '&nbsp;';
			}
			if(caption.length == 0){
				caption = '&nbsp;';
			}
			writeCartTableRow(array, src, i, photos[id].id, caption, option, price, array[i].quantity);
		}
				document.write('<tr>');
		document.write('<td>&nbsp;</td>');
		document.write('<td>&nbsp;</td>');
		document.write('<td>&nbsp;</td>');
		document.write('<td>&nbsp;</td>');
		document.write('<td>Total</td>');
		document.write('<td id="orderTotal">'+ currencySymbol + orderTotal()+'</td>');
							document.write('<input type="hidden" name="business" value="el_prezidente@msn.com">');
		document.write('<input type="hidden" name="currency_code" value="GBP">');
		document.write('<input type="hidden" name="cmd" value="_cart">');
		document.write('<input type="hidden" name="upload" value="1">');
		document.write('<td><input type="Submit" value="Place order" id="payPalAddButton" class="button" onmouseover="this.className=\'buttonHi\';" onmouseout="this.className=\'button\';"></td>');
								document.write('</tr>');
						document.write('<input type="hidden" name="site" value="JACKDYLANPRESTON">');
			document.write('</table>');
		document.write('</form>');
					}
	else{
		document.write('<p id="cartEmpty">Cart empty</p>');
	}
}

/***************************************************************************
* Write an individual row in the table. *
***************************************************************************/
function writeCartTableRow(array, src, row_no, photos_id, caption, option, price, quantity){
	var num = parseInt(row_no, 10)+1;
		id = getArrayIndex(photos, photos_id);
	ref = '';
	if(photos[id].photo_ref != ''){
		ref = photos[id].photo_ref;
	}
	else{
		ref = photos_id+' - '+caption;
	}
					document.write('<input type="hidden" name="item_number_'+num+'" value="'+ref+'">');
	document.write('<input type="hidden" name="item_name_'+num+'" value="'+option+'">');
			document.write('<input type="hidden" name="quantity_'+num+'" value="'+quantity+'">');
	document.write('<input type="hidden" name="item_ref_'+num+'" value="'+ref+'">');
	document.write('<input type="hidden" name="item_title_'+num+'" value="'+photos[id].caption+'">');
	document.write('<input type="hidden" name="item_description_'+num+'" value="'+option+'">');
	document.write('<input type="hidden" name="item_price_'+num+'" value="'+price+'">');
		document.write('<input type="hidden" name="id_'+num+'" value="'+photos_id+'">');
		document.write('<input type="hidden" name="amount_'+num+'" value="'+price+'">');
		document.write('<tr>');
	document.write('<td class="image"><img src="'+src+'"></td>');
	document.write('<td>'+caption+'</td>');
	document.write('<td>'+option+'</td>');
	document.write('<td>'+ currencySymbol + price+'</td>');
	document.write('<td><input type="Text" size="5" class="cartInputField" value="'+quantity+'" onkeyup="quantityOnKeyUp(arrayOfItems, this.value, cartForm, '+num+', '+photos_id+');"></td>');
	document.write('<td id="totalPrice_'+num+'">'+ currencySymbol + totalPrice(price, quantity)+'</td>');
	document.write('<td id="remove_'+num+'"><input type="Button" value="Remove" class="button" onmouseover="this.className=\'buttonHi\';" onmouseout="this.className=\'button\';" onclick="confirmRemove(\'Remove item?\', cartForm, '+num+', '+photos_id+');"></td>');
	document.write('</tr>');
}

/***************************************************************************
* Runs with the onkeyup attribute of the text input field for quantity. *
* Updates the HTML form value *
* the arrayOfItems and the cookie. *
***************************************************************************/
function quantityOnKeyUp(array, value, form, row_num, photos_id){

	if(isInteger(value) && isPositive(value)){
		
		updateQuantity(form, photos_id, array[parseInt(row_num)-1].payment_id, value);
		
				form['quantity_'+row_num].value = value;
					form['quantity_'+row_num].value = value;
		
		setHTML('totalPrice_'+row_num, currencySymbol + totalPrice(form['amount_'+row_num].value, value));
		
		setHTML('orderTotal', currencySymbol + orderTotal());
	}
	
	if(value == 0){
		try{
			clearTimeout(removeAlert);
		}
		catch(e){/* Do nothing */}
		removeAlert = setTimeout('confirmRemove(\'Remove item?\', \''+form.name+'\', '+row_num+', '+photos_id+')', 3000);
			}
	else{
		try{
			clearTimeout(removeAlert);
		}
		catch(e){/* Do nothing */}
			}
}

				
