function joinNewsletter( ){

	// remove any click events just in case
	$( 'join_newsletter' ).removeEvents( 'submit' );
	
	// look for a submit event
	$( 'join_newsletter' ).addEvent( 'submit', function( event ) {

		// validate
		if( $( 'name' ).value == '' ){
				
			alert( 'If you would like to join our newsletter, please enter your name.' );
			$( 'name' ).setStyle( 'background-color', '#FEFFBF' );
			$( 'name' ).focus( );
			
		} else if( $( 'email' ).value == '' || !( checkEmail( 'email' ) ) ){

			alert( 'If you would like to join our newsletter, please enter a valid email address.' );
			$( 'email' ).setStyle( 'background-color', '#FEFFBF' );
			$( 'email' ).focus( );
			
		} else {

			// checkajax
			var myRequest = new Request({
								   
				method:			'post',
				url:				'join_newsletter.cfm?ajax=join newsletter',
				onRequest: 		function ( ) {
					
					$( 'login_right' ).set( 'html', '<img src="images/loader.gif" style="margin: 20px 0 0 60px;">' );
					
				},
				onSuccess:	function( html ) {
					
					// insert the new html
					$( 'login_right' ).set( 'html', 'Thank you. You have successfully joined our newsletter.' );
					
				},
				onFailure:	function( ) {
					
					alert( 'Something went wrong... your request to join our newsletter failed. Please refresh the page and try agin.' );
	
				}
			}).send( $( 'join_newsletter' ) );
			
		};
		
		event.stop( );
	
	});

};

function shippingOptions( ){
	
	// check if shipping is already filled in
	if( checkZip( $( 'shipping_zip' ) ) ){
				 
		process_shipping_cost( );
				 
	};
	
	$( 'shipping_zip' ).addEvent( 'change', function( event ) {

		process_shipping_cost( );
	
	});
	
};

function process_shipping_cost( ){

	zipCode = $( 'shipping_zip' ).value;

	// call the ajax service and calculate new shipping options and prices
	var myRequest = new Request({
						   
		method:			'post',
		url:				'online-ordering.cfm?ajax=shipping_options',
		data: 			'shipping_zip=' + zipCode,
		onRequest: 		function ( ) {
			
			// show the loading icon
			$( 'display_shipping_options' ).set( 'html', '<img src="images/loader.gif" style="margin: 20px 0 0 60px;">' );
			
			// hide the grand total
			$( 'display_grand_total' ).addClass( 'hide' );
			
		},
		onSuccess:	function( html ) {
			
			// insert the new html
			$( 'display_shipping_options' ).set( 'html', html );
			
			// show the grand total
			$( 'display_grand_total' ).removeClass( 'hide' );
			
			// recalculate the grand total
			calculateGrandTotal( );
			
		},
		onFailure:	function( ) {
			
			//alert( 'Something went wrong...' );

		}
	}).send( );

};

function calculateGrandTotal( ){
	
	
	grandTotal = parseFloat( $( 'subTotal' ).get( 'html' ) ) + parseFloat( $( 'shipping_cost' ).get( 'html' ) );
	$( 'grand_total' ).set( 'html', formatCurrency( grandTotal ) );
	
};

function sameAs( ){

	$( 'sameAs' ).addEvent( 'click', function( event ){
	
		if( $( 'sameAs' ).checked ){
		
			$( 'billing_first_name' ).value 	= $( 'shipping_first_name' ).value;
			$( 'billing_last_name' ).value 	= $( 'shipping_last_name' ).value;
			$( 'billing_address' ).value 		= $( 'shipping_address' ).value;
			$( 'billing_address2' ).value 	= $( 'shipping_address2' ).value;
			$( 'billing_city' ).value 		= $( 'shipping_city' ).value;
			$( 'billing_state' ).value 		= $( 'shipping_state' ).value;
			$( 'billing_zip' ).value 		= $( 'shipping_zip' ).value;
			$( 'billing_phone' ).value 		= $( 'shipping_phone' ).value;
		
		};
	
	});

};

function complete_purchase( ){

	$( 'buy_now' ).addEvent( 'click', function( event ){
	
		var valid = form_validation( );
		
		if( valid ){
		
			// hide the buy now and show the loading icon
			$( 'buy_now' ).set( 'html', '<img src="images/loader.gif">' );
		
			// valid form, so submit
			$( 'checkout' ).submit( );
		
		};
		
		event.stop( );
	
	});

};

window.addEvent( 'domready', function(){

	if( $( 'join_newsletter' ) ){
		
		joinNewsletter( );
		
	};
	
	/*
	if( $( 'shipping_zip' ) ){
		
		shippingOptions( );
		
	};
	*/
	
	// slideshow for homepage only 
	if( $( 'show' ) ){

		var data = {
			'slide_1.jpg': { }, 
			'slide_2.jpg': { }, 
			'slide_3.jpg': { }, 
			'slide_4.jpg': { }, 
			'slide_5.jpg': { }
		};
	
		var myShow = new Slideshow.KenBurns('show', data, { captions: true, controller: true, delay: 5000, duration: 2000, height: 200, hu: 'images/slideshow/', width: 475 });
		
	};
	
	if( $( 'sameAs' ) ){
		
		sameAs( );
	
	};
	
	if( $( 'buy_now' ) ){
	
		complete_purchase( );
	
	};

});


