
// Updates the quantity of an item in a shopping basket.

function updateItemQty( absolutePath, webRoot, sessionId, awardSizeId )
{
	qty = document.getElementById( awardSizeId + "qty" ).value;
	
	url = webRoot + '/shop/UpdateItemQty.php?session_id=' + sessionId + '&' + 'webRoot=' + webRoot + '&' + 
		'absolutePath=' + absolutePath + '&' + 'award_size_id=' + awardSizeId + '&' + 'qty=' + qty;
  	
	callback = 
	{
     	success: function(o) 
		{
			array = o.responseText.split( '~' );
			
			$( "#" + awardSizeId + "linePrice" ).fadeOut("slow");
			document.getElementById( awardSizeId + "linePrice" ).innerHTML = "&pound;" + array[0];
			$( "#" + awardSizeId + "linePrice" ).fadeIn("slow");
			
			innerHTML = document.getElementById( 'shoppingBasketInfo' ).innerHTML;
			
			innerHTML = "Your cart total is &pound;"+array[2]+" ("+array[1]+" items)";
			
			$( "#shoppingBasketInfo" ).fadeOut("slow");
			document.getElementById( "shoppingBasketInfo" ).innerHTML = innerHTML;
			$( "#shoppingBasketInfo" ).fadeIn("slow");
			
			document.getElementById( 'quantity_' + awardSizeId ).value = qty;
			
			$( "#basketTotal" ).fadeOut("slow");
			document.getElementById( "basketTotal" ).innerHTML =  array[2];
			$( "#basketTotal" ).fadeIn("slow");
			
			$( "#orderTotal" ).fadeOut("slow");
			document.getElementById( "orderTotal" ).innerHTML =  array[3];
			$( "#orderTotal" ).fadeIn("slow");
        }
  	} 
		
	transaction = YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
}

// Updates delivery option selected.

function updateDeliveryOption( absolutePath, webRoot, sessionId )
{
	deliveryOptionId = document.getElementById( 'delivery_option_id' ).options[document.getElementById( 'delivery_option_id' ).selectedIndex].value;
	
	url = webRoot + '/shop/UpdateDeliveryOption.php?webRoot=' + webRoot + '&' + 'absolutePath=' + absolutePath + '&' + 
		'deliveryOptionId=' + deliveryOptionId + '&' + 'sessionId=' + sessionId;
  	
	callback = 
	{
     	success: function(o) 
		{
			array = o.responseText.split( '~' );
			
			$( "#deliveryTotal" ).fadeOut("slow");
			document.getElementById( "deliveryTotal" ).innerHTML = array[0];
			$( "#deliveryTotal" ).fadeIn("slow");
			
			$( "#orderTotal" ).fadeOut("slow");
			document.getElementById( "orderTotal" ).innerHTML = array[1];
			$( "#orderTotal" ).fadeIn("slow");
			
			if( deliveryOptionId == "-----" )
				document.getElementById( 'checkoutButton' ).className = "submitGreyedOut";
			else
				document.getElementById( 'checkoutButton' ).className = "submit";
        }
  	} 
		
	transaction = YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
}

function removeBasketItem( absolutePath, webRoot, awardSizeId )
{
	awardsizeRow = 'pr'+awardSizeId;
	document.getElementById( awardsizeRow ).parentNode.removeChild( document.getElementById( awardsizeRow ) );
	
	url = webRoot + '/shop/RemoveBasketItem.php?webRoot=' + webRoot + '&' + 'absolutePath=' + absolutePath + '&' + 
		'awardSizeId=' + awardSizeId;
  	
	callback = 
	{
     	success: function(o) 
		{
			array = o.responseText.split( '~' );
			
			innerHTML = document.getElementById( 'shoppingBasketInfo' ).innerHTML;
			
			innerHTML = innerHTML = "Your cart total is &pound;"+array[2]+" ("+array[1]+" items)";;
			
			$( "#shoppingBasketInfo" ).fadeOut("slow");
			document.getElementById( "shoppingBasketInfo" ).innerHTML = innerHTML;
			$( "#shoppingBasketInfo" ).fadeIn("slow");
			
			$( "#basketTotal" ).fadeOut("slow");
			document.getElementById( "basketTotal" ).innerHTML = array[1];
			$( "#basketTotal" ).fadeIn("slow");
			
			$( "#orderTotal" ).fadeOut("slow");
			document.getElementById( "orderTotal" ).innerHTML =  array[2];
			$( "#orderTotal" ).fadeIn("slow");
        }
  	} 
		
	transaction = YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
}

function getProducts( absolutePath, webRoot, next )
{	
	url = webRoot + '/shop/Products.php?next=' + next + '&' + 'webRoot=' + webRoot + '&' + 
		'absolutePath=' + absolutePath;
  	
	callback = 
	{
     	success: function(o) 
		{
			document.getElementById( 'mainContent' ).innerHTML = o.responseText;
        }
  	} 
		
	transaction = YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
}

function getProductDetail( absolutePath, webRoot, next )
{	
	url = webRoot + '/shop/ProductDetail.php?next=' + next + '&' + 'webRoot=' + webRoot + '&' + 
		'absolutePath=' + absolutePath;
  	
	callback = 
	{
     	success: function(o) 
		{
			document.getElementById( 'mainContent' ).innerHTML = o.responseText;
        }
  	} 
		
	transaction = YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
}

function displayImage( webRoot, filename )
{
	openShadowbox( 'img', webRoot + '/downloads/award_photos/' + filename, 285, 492 );
}

// Uses Ajax to go to the appropriate advert script - Car.php, Motorbike.php, Van.php or Caravan.php - to write
// the contents of the appropriate session variable to the database. The id of the newly created entry
// (if database successfully updated) is returned so that the PayPal confirmation script can be executed upon
// successful payment.

function submitToPayPal( absolutePath, webRoot )
{	
	amount = document.getElementById( 'basketTotal' ).innerHTML;
	amount = amount.substring( 1, amount.length );
	
	shipping = document.getElementById( 'deliveryTotal' ).innerHTML;
	shipping = shipping.replace( /&nbsp;/g, '' );
	shipping = shipping.substring( 1, shipping.length );
	
	// Store the selected shipping in a hidden variable for use by PayPal.
	document.getElementsByName( 'handling_cart' )[0].value = shipping;
	
	document.paymentForm.action = "https://www.paypal.com/uk/cgi-bin/webscr";
	document.paymentForm.submit();
}

function updateReferenceNo( absolutePath, webRoot, dropDownIdName )
{
	awardSizeId = document.getElementById( dropDownIdName ).options[document.getElementById( dropDownIdName ).selectedIndex].value;

	url = webRoot + '/shop/GetReferenceNo.php?award_size_id=' + awardSizeId + '&' + 'webRoot=' + webRoot + '&' + 
		'absolutePath=' + absolutePath;
  	
	callback = 
	{
     	success: function(o) 
		{
			array = o.responseText.split( '|' );
			
			document.getElementById( 'referenceNo' ).innerHTML = "Reference No: " + array[0];
			document.getElementById( 'referenceNo' ).style.display = "";
			
			document.getElementById( 'productDescription' ).innerHTML = array[1];
			document.getElementById( 'price' ).innerHTML = array[2];
			
			document.getElementById( 'stockStatus' ).innerHTML = "Availability: " + array[3];
			document.getElementById( 'stockStatus' ).style.display = "";
			
			if( array[1] == "Out of Stock" )
				document.getElementById( 'addToBasket' ).style.display = "none";
			else
				document.getElementById( 'addToBasket' ).style.display = "";
			
			selectElement = document.getElementById( 'colour' );
			selectElement.innerHTML = "";

			var optn = document.createElement( "option" );
			optn.text = array[4];
			optn.value = awardSizeId;

			selectElement.options.add(optn);
        }
  	} 
		
	transaction = YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
}
