function add_to_cart()
{

	var url = "ajax_functions.php";
	new Ajax.Updater('',url,
	{
		method: "post",
		parameters: 
		{
			mode: 'add_to_cart',
			prod_name: $('prod_name').value,
			prod_colors: $('prod_colors').value,
			prod_sizes: $('prod_sizes').value,
			prod_qty: $('prod_qty').value,
			prod_price: $('prod_price').value,
			tax_exempt: $('tax_exempt').value
			},
		onSuccess: function(transport)
		{		
			if (transport.responseText == 'error')
			{
				$('cart_status').innerHTML = "<font color=red size=1><blink>Unable to add item. Try again.</blink></font>";
			}
			else
			{
				$('cart_status').innerHTML = "<font color=blue size=1>Item added to cart</font></font>";			
			}
			
			$('cart_status').style.display = "block";
			new Effect.Fade('cart_status', {duration:5.0});
		}
	});

}// end


function update_shipping(qty)
{

		var name = $('customer_name').value;
		var email = $('email').value;

		location.href='view_cart.php?shipping='+qty+'&name='+name+'&email='+email;

}// end

function update_country(cty)
{

		var name = $('customer_name').value;
		var email = $('email').value;
		
		location.href='view_cart.php?country='+cty+'&name='+name+'&email='+email;

}// end

function update_province(prv)
{

		var name = $('customer_name').value;
		var email = $('email').value;

		location.href='view_cart.php?location='+prv+'&name='+name+'&email='+email;

}// end


function update_qty(id,qty)
{

	var url = "ajax_functions.php";
	new Ajax.Updater('',url,
	{
		method: "post",
		parameters: 
		{
			mode: 'update_qty',
			new_qty: qty,
			item_to_update: id
			},
		onSuccess: function(transport)
		{		
			location.href='view_cart.php';
		}
	});

}// end


function remove_item(id)
{

	var url = "ajax_functions.php";
	new Ajax.Updater('',url,
	{
		method: "post",
		parameters: 
		{
			mode: 'remove_item',
			item_to_remove: id
			},
		onSuccess: function(transport)
		{		
			location.href='view_cart.php';
		}
	});

}// end

/*
 * validates the given field id and outputs an error message
 */
function validateRegistrationForm(id,field) 
{

	if (!$(id).value) 
	{
		$('error_'+id).innerHTML = "<span style=\"color:crimson;\">"+field+" value missing</span>";
		return false;
	} else {
		$('error_'+id).innerHTML = "";
		return true;
	}

}// end


/*
 * validate email formatting
 */
function validateEmail(email_id,error_id) 
{

   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = $(email_id).value;
   if(reg.test(address) == false) {
      $(error_id).innerHTML = "<span style=\"color:crimson;\">Email address not valid format</span>";
      return false;
   }
   else
   {
     $(error_id).innerHTML = "";  
     return true;
   }
   
}// end

function submit_order()
{

	if (!validateRegistrationForm('customer_name','Customer Name')){ return false; }

	if (!validateEmail('email','error_email')){ return false; }

	var url = "ajax_functions.php";
	new Ajax.Updater('',url,
	{
		method: "post",
		parameters: 
		{
			mode: 'submit_order',
			customer_name: $('customer_name').value,
			email: $('email').value
		},
		onSuccess: function(transport)
		{	
			location.href='process_order.php';
		}
	});

}// end
