function validateCheckout( step ){
	if( step == 1 ){
		if($('billing_first_name').value == ""){
			alert('Please enter your billing first name.');
			$('billing_first_name').focus();
		} else if($('billing_last_name').value == ""){
			alert('Please enter your billing last name.');
			$('billing_last_name').focus();
		} else if (!(checkEmail('billing_email'))){
			alert('Please enter a valid email address.');
			$('billing_email').focus();
		} else if($('billing_phone').value == ""){
			alert('Please enter your billing phone number.');
			$('billing_phone').focus();
		} else if($('billing_address1').value == ""){
			alert('Please enter your billing address.');
			$('billing_address1').focus();
		} else if($('billing_city').value == ""){
			alert('Please enter your billing city.');
			$('billing_city').focus();
		} else if($('billing_zip').value == ""){
			alert('Please enter your billing zip/postal code.');
			$('billing_zip').focus();
		} else if($('billing_country').value == ""){
			alert('Please select your billing country.');
		} else if($('billing_country').value == "US" && $('billing_state').value == ""){
			alert('Please select your billing state.');
			$('billing_state').focus();
		} else {
			$( 'checkout_step_1' ).style.display = 'none';
			$( 'checkout_step_2' ).style.display = 'block';
			
			//update billing display info
			$('billing_info_name').innerHTML = $('billing_first_name').value + ' ' + $('billing_last_name').value;
			$('billing_info_address').innerHTML = $('billing_address1').value + ' ' + $('billing_address2').value;
			$('billing_info_city').innerHTML = $('billing_city').value;
			if ($('billing_country').value == "US") {
				$('billing_info_state').innerHTML = $('billing_state').value;
			}
			else {
				$('billing_info_state').innerHTML = $('billing_province').value;
			}
			$('billing_info_zip').innerHTML = $('billing_zip').value;
			$('billing_info_country').innerHTML = $('billing_country').value;
			$('billing_info_phone').innerHTML = $('billing_phone').value;
			$('billing_info_email').innerHTML = $('billing_email').value;
			$('billing_info').style.display = 'block';
			
			updateHeader( 2 );
		}
	} else if(step == 2){
		if($('shipping_first_name').value == ""){
			alert('Please enter your shipping first name.');
			$('shipping_first_name').focus();
		} else if($('shipping_last_name').value == ""){
			alert('Please enter your shipping last name.');
			$('shipping_last_name').focus();
		} else if (!(checkEmail('shipping_email'))){
			alert('Please enter a valid email address.');
			$('shipping_email').focus();
		} else if($('shipping_phone').value == ""){
			alert('Please enter your shipping phone number.');
			$('shipping_phone').focus();
		} else if($('shipping_address1').value == ""){
			alert('Please enter your shipping address.');
			$('shipping_address1').focus();
		} else if($('shipping_city').value == ""){
			alert('Please enter your shipping city.');
			$('shipping_city').focus();
		} else if($('shipping_zip').value == ""){
			alert('Please enter your shipping zip/postal code.');
			$('shipping_zip').focus();
		} else if($('shipping_country').value == ""){
			alert('Please select your shipping country.');
			$('shipping_country').focus();
		} else if($('shipping_country').value == "US" && $('shipping_state').value == ""){
			alert('Please select your shipping state.');
			$('shipping_state').focus();
		} else {
			var shipRate = parseShippingRate();
			$('shipping_methods').style.display = 'block';
			
			if( shipRate == 0 ){
				updateShipping();
				alert('Please wait while your shipping rate is calculated.');
			} else {
				// display tax rate
				//var taxRate = parseFloat( $( 'display_tax_rate' ).get( 'html' ).replace( '$', '' ) );
				//var taxRate = updateTax( );
				var theTax = updateTax();
				//$('display_tax_rate').innerHTML = formatCurrency(taxRate);
				//$('display_tax_rate').style.display = 'inline';
				// display grand total
				//var grandTotal = updateGrandTotal( $( 'taxableTotal' ).innerHTML, shipRate, taxRate );
				var grandTotal = updateGrandTotal( $( 'subTotal' ).value, shipRate, theTax );
				//alert( grandTotal );
				$('display_grand_total_rate').innerHTML = grandTotal;
				$('display_grand_total_rate').style.display = 'inline';
				
				// display shipping
				$('display_shipping_rate').innerHTML = formatCurrency(shipRate);
				$('display_shipping_rate').style.display = 'inline';
				
				$('checkout_step_2').style.display = 'none';
				$('checkout_step_3').style.display = 'block';
				
				//update shipping display info
				$('shipping_info_name').innerHTML 		= $('shipping_first_name').value + ' ' + $('shipping_last_name').value;
				$('shipping_info_address').innerHTML 	= $('shipping_address1').value + ' ' + $('shipping_address2').value;
				$('shipping_info_city').innerHTML 		= $('shipping_city').value;
				if ($('shipping_country').value == "US") {
					$('shipping_info_state').innerHTML 	= $('shipping_state').value;
				}
				else {
					$('shipping_info_state').innerHTML 	= $('shipping_province').value;
				}				
				$('shipping_info_zip').innerHTML 		= $('shipping_zip').value;
				$('shipping_info_country').innerHTML 	= $('shipping_country').value;
				$('shipping_info_phone').innerHTML 	= $('shipping_phone').value;
				$('shipping_info_email').innerHTML 	= $('shipping_email').value;
				$('shipping_info').style.display 		= 'block';
				
				updateHeader( 3 );
			}				
		}
	} else if(step == 3){
		CheckCardNumber();
	}
}

function goBack(step){
	$('checkout_step_1').setStyle( 'display', 'none' );
	$('checkout_step_2').setStyle( 'display', 'none' );
	$('checkout_step_3').setStyle( 'display', 'none' );
	$( 'international_shipping_notice' ).setStyle( 'display', 'none' );
	
	$('checkout_step_' + step).setStyle( 'display', 'block' );
	updateHeader(step);
}

function updateHeader(step){
	if(step == 1){
		$('checkout_header').innerHTML = 'CHECKOUT STEP 1 OF 3 - BILLING INFORMATION'
		$('billing_info').style.display = 'none';
		$('shipping_info').style.display = 'none';
	} else if(step == 2){
		$('checkout_header').innerHTML = 'CHECKOUT STEP 2 OF 3 - SHIPPING INFORMATION'
		$('shipping_info').style.display = 'none';
	} else if(step == 3){
		$('checkout_header').innerHTML = 'CHECKOUT STEP 3 OF 3 - PAYMENT INFORMATION'
	}
}

function sameAsBilling( sessionInfo ){
	if($('sameAs').checked){
		$('shipping_first_name').value 	= $('billing_first_name').value;
		$('shipping_last_name').value 	= $('billing_last_name').value;
		$('shipping_email').value 		= $('billing_email').value;
		$('shipping_phone').value 		= $('billing_phone').value;
		$('shipping_address1').value 		= $('billing_address1').value;
		$('shipping_address2').value 		= $('billing_address2').value;
		$('shipping_city').value 		= $('billing_city').value;
		$('shipping_state').value 		= $('billing_state').value;
		$('shipping_province').value 	= $('billing_province').value;
		$('shipping_zip').value 			= $('billing_zip').value;
		$('shipping_country').value 		= $('billing_country').value;
		
		updateShipping( sessionInfo );
		checkCountry();
	}
}

function updateShipping( sessionInfo ){
	var theTax = updateTax( );
	
	var HTMLRequest = new Request.HTML({
		method:		'get',
		url:			webroot + "calculateShipping.cfm?" + sessionInfo + "&zip=" + $( 'shipping_zip' ).value + "&country=" + $( 'shipping_country' ).value,
		update:		$( 'shipping_methods' ),
		onRequest:	function() {},
		onComplete:	function() {},
		onFailure:	function() { alert( webroot + "calculateShipping.cfm?" + sessionInfo + "&zip=" + $( 'shipping_zip' ).value + "&country=" + $( 'shipping_country' ).value + ' Shipping Retrieval Failed' ); },
		onSuccess:	function() { 
						$( 'shipping_methods' ).setStyle( 'display', 'block' );
					
						// adjust the grand total
						//var shipRate = parseShippingRate();
						//var grandTotal = updateGrandTotal( $( 'subTotal' ).value, shipRate, theTax );
						//$('display_grand_total_rate').innerHTML = grandTotal;
						//$('display_grand_total_rate').style.display = 'inline';					
					}
	}).send();
}

function updateTax(){
	var tmpTax = 0;
//	var shipRate = parseShippingRate();
	
	// only NY residents pay tax - don't bother trying to work around this... it gets recalculated server-side anyway
	if($('shipping_state').value == 'NY'){
		var HTMLRequest = new Request.HTML({
			method:		'get',
			url:		webroot + "calculateTax.cfm?&zip=" + $('shipping_zip').value,
			update:		$( 'display_tax' ),
			async: 		false,
			onRequest:	function() {},
			onComplete:	function() {},
			onFailure:	function() { alert( webroot + "calculateTax.cfm?zip=" + $( 'shipping_zip' ).value + ' Tax Retrieval Failed' ); },
			onSuccess:	function() { 
							$( 'display_tax' ).setStyle( 'display', 'block' ); 

							tmpTax = $('display_tax').innerText;
							// remove the dollar sign and then parse
							if (tmpTax.length > 1)
							{
								tmpTax = tmpTax.substr(1,tmpTax.length - 1);
							}
							tmpTax = parseFloat(tmpTax);
							
							if (isNaN(tmpTax)) 
							{
								tmpTax = 0;
							}
							// adjust the grand total
							//var grandTotal = updateGrandTotal( $( 'subTotal' ).value, shipRate, tmpTax );
							//$('display_grand_total_rate').innerHTML = grandTotal;
							//$('display_grand_total_rate').style.display = 'inline';						
						}
		}).send();

//		taxRate = 0.0845;
//		$('display_tax_rate').style.display = 'block';
	} else {
		$('display_tax').style.display = 'none';
		// adjust the grand total
		//var grandTotal = updateGrandTotal( $( 'subTotal' ).value, shipRate, tmpTax );
		//$('display_grand_total_rate').innerHTML = grandTotal;
		//$('display_grand_total_rate').style.display = 'inline';
	}

	return tmpTax;
}

function updateGrandTotal( subtotal, shipRate, tax ){
	
	var subTotal1	= parseFloat( subtotal );
	var total		= subTotal1 + shipRate + tax;

	return formatCurrency(total);
	
}
/*
function updateGrandTotal( totalTaxable, shipRate, taxRate ){
	
	var subTotal1	= parseFloat( $( 'subTotal' ).value );
	var total		= subTotal1 + shipRate + ( totalTaxable * taxRate );

	return formatCurrency(total);
	
};
*/

function parseShippingRate() {
	var theValue    = '';
	var x           = $('shipping_type');

	for(i=0; i<=x.value.length; i++) {
		// get variables parsed
		if(x.value.substring(i, i+1) == '_'){
			if(theValue == ''){
				theValue = parseFloat(x.value.substring(i + 1, x.value.length));
			}
		}
	}
	return theValue;
}

// check the shipping country and update the state/province input
function checkCountry( ){
	if( $( 'shipping_country' ).value == 'US' ){		
		$( 'shipping_province_div' ).setStyle( 'display', 'none' );		
		$( 'shipping_state_div' ).setStyle( 'display', 'block' );
	}
	else {
		$( 'shipping_state_div' ).setStyle( 'display', 'none' );
		$( 'shipping_province_div' ).setStyle( 'display', 'block' );
	}
	
}
//	var valid = true;
//	
//	if( $( 'shipping_country' ).value != 'US' ){		
//		$( 'checkout_step_2' ).setStyle( 'display', 'none' );
//		$( 'international_shipping_notice' ).setStyle( 'display', 'block' );
//		$( 'shipping_country' ).value = 'US';
//		valid = 'false';
//	}
//	
//	return valid;
	
//}

// check the billing country and update the state/province input
function checkBillingCountry( ){
	if( $( 'billing_country' ).value == 'US' ){		
		$( 'billing_province_div' ).setStyle( 'display', 'none' );		
		$( 'billing_state_div' ).setStyle( 'display', 'block' );
	}
	else {
		$( 'billing_state_div' ).setStyle( 'display', 'none' );
		$( 'billing_province_div' ).setStyle( 'display', 'block' );
	}
}

