/***************************************************************************
* Currency symbol for the site                                             *
***************************************************************************/
var currencySymbol = '&pound;';

/***************************************************************************
* Write the table of items stored in the array                             *
***************************************************************************/
function writeCartTable(array){
	html = '';
	html += '<div id="backLink"><a href="#" onclick="history.back();return false;">Back</a></div>';
	if(array.length){
				html += '<form id="cartForm" style="margin:0px;border:0px;" name="cartForm" target="_blank" action="https://www.paypal.com/cgi-bin/webscr" method="post">';
				html += '<table id="cartTable" cellpadding="0" cellspacing="0">';
		html += '<tr>';
		html += '<th>Photo</th>';
		html += '<th>Caption</th>';
		html += '<th>Option</th>';
		html += '<th>Price</th>';
		html += '<th>Quantity</th>';
		html += '<th>Total price</th>';
		html += '<td><input type="Button" value="Clear cart" class="button" onmouseover="this.className=\'buttonHi\';" onmouseout="this.className=\'button\';" onclick="if(confirm(\'Clear items?\')){deleteCookie(\'SHIPPAS\'); window.location.reload(false);}"></td>';
				html += '</tr>';
				for(var i=0; i<array.length; i++){
			//id = getArrayIndex(photos, array[i].id);
			id = array[i].id;
			ref = array[i].ref;
			option = array[i].option; 
			price =array[i].price 
			name = array[i].name;
			if(option.length == 0){
				option = name || ref;
			}
			if(name.length == 0){
				name = '&nbsp;';
			}
			if(thumbnails[id]){
				src = thumbnails[id].src;
			}
			else {
				src = "";
			}
			html += writeCartTableRow(array, src, i, id, ref, name, option, price, array[i].quantity);
					}
		html += '<tr>';
		html += '<td>&nbsp;</td>';
		html += '<td>&nbsp;</td>';
		html += '<td>&nbsp;</td>';
		html += '<td>&nbsp;</td>';
		html += '<td>Total</td>';
		html += '<td id="orderTotal">'+ currencySymbol + orderTotal()+'</td>';
							html += '<input type="hidden" name="business" value="shippas@hotmail.co.uk">';
		html += '<input type="hidden" name="currency_code" value="GBP">';
		html += '<input type="hidden" name="cmd" value="_cart">';
		html += '<input type="hidden" name="upload" value="1">';
		html += '<input type="hidden" name="charset" value="utf-8">';
		html += '<input type="hidden" name="shipping" value="0">';
		html += '<td><input type="Submit" value="Place order" id="payPalAddButton" class="button" onmouseover="this.className=\'buttonHi\';" onmouseout="this.className=\'button\';"></td>';
								html += '</tr>';
						html += '<input type="hidden" name="site" value="SHIPPAS">';
			html += '</table>';
		html += '</form>';
					}
	else{
		html += '<p id="cartEmpty">Cart empty</p>';
	}
	$('#cartDiv').html(html).find('img').bind('load', function(){		clik.doColumnResize();	});}

/***************************************************************************
* Write an individual row in the table.                                    *
***************************************************************************/
function writeCartTableRow(array, src, row_no, photos_id, ref, name, option, price, quantity){
	var num = row_no+1;
	//id = getArrayIndex(photos, photos_id);
	id = photos_id;
	photo_ref = ref + ' - '+ name;
	html = '';
				html += '<input type="hidden" name="item_number_'+num+'" value="'+photo_ref+'">';
	html += '<input type="hidden" name="item_name_'+num+'" value="'+option+'">';
			html += '<input type="hidden" name="quantity_'+num+'" value="'+quantity+'">';
	html += '<input type="hidden" name="item_ref_'+num+'" value="'+photo_ref+'">';
	html += '<input type="hidden" name="item_title_'+num+'" value="'+name+'">';
	html += '<input type="hidden" name="item_description_'+num+'" value="'+option+'">';
	html += '<input type="hidden" name="item_price_'+num+'" value="'+price+'">';
		html += '<input type="hidden" name="id_'+num+'" value="'+photos_id+'">';
		html += '<input type="hidden" name="amount_'+num+'" value="'+price+'">';
		html += '<tr>';
	html += '<td class="image"><img src="'+src+'"></td>';
	html += '<td>'+name+'</td>';
	html += '<td>'+option+'</td>';
	html += '<td>'+ currencySymbol + price+'</td>';
	html += '<td><input type="Text" size="5" class="cartInputField" value="'+quantity+'" onkeyup="quantityOnKeyUp(arrayOfItems, this.value, \'cartForm\', '+num+', '+id+');"></td>';
	html += '<td id="totalPrice_'+num+'">'+ currencySymbol + totalPrice(price, quantity)+'</td>';
	html += '<td id="remove_'+num+'"><input type="Button" value="Remove" class="button" onmouseover="this.className=\'buttonHi\';" onmouseout="this.className=\'button\';" onclick="confirmRemove(\'Remove item?\',\'SHIPPAS\', '+row_no+');"></td>';
	html += '</tr>';
	return html;
}


/***************************************************************************
* 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_name, row_num, photos_id){

	if(isInteger(value) && isPositive(value)){
		
		updateQuantity('SHIPPAS', photos_id, array[parseInt(row_num)-1].option, value);
		
				document.forms[form_name]['quantity_'+row_num].value = value;
					document.forms[form_name]['quantity_'+row_num].value = value;
		
		setHTML('totalPrice_'+row_num, currencySymbol + totalPrice(document.forms[form_name]['amount_'+row_num].value, value));
		
		setHTML('orderTotal', currencySymbol + orderTotal());
	}
	
	if(value == 0){
		try{
			clearTimeout(removeAlert);
		}
		catch(e){/* Do nothing */}
				removeAlert = setTimeout('confirmRemove(\'Remove item?\',\'SHIPPAS\', '+(row_num-1)+')', 3000);
			}
	else{
		try{
			clearTimeout(removeAlert);
		}
		catch(e){/* Do nothing */}
			}
}

				

