// JavaScript Document

function refresh_price() {
	var selected = jQuery("input[name='ship_opt']:checked").val();
	update_shipping( selected, false, false );
}

function update_shipping( shipsel, record_ship_sel, show_alert ) {
	var optlab = jQuery("label[for='" + shipsel + "']").text();
	if( !optlab ) {
		jQuery('#ShipCostCell').text( '--.--' );
		jQuery('#OrderTotalCell').text( '--.--' );
		jQuery('#ShippingCell').text( 'Shipping:');
		if( show_alert && (jQuery('#prev_ship_sel').length > 0) ) {
			alert( 'Previously selected shipping choice no longer applicable for this zip code. Please select another shipping option.' );
		}
	} else {
		var newprice = optlab.substr( optlab.indexOf( '$' ) );
		var shipdesc = optlab.substr( 0, optlab.indexOf( '$' ) - 1 );
		jQuery('#ShipCostCell').text( newprice );
		var subtot = jQuery('#SubtotalCell').text().substr(1);
		var coup_text = jQuery('#CouponCell').text();
		var coup_disc = parseFloat( coup_text.substr( coup_text.indexOf( '$' )+1 ) );
		var gift_text = jQuery('#GiftCell').text();
		var gift_disc = parseFloat( gift_text.substr( gift_text.indexOf( '$' )+1 ) );
		var newtot = parseFloat(subtot) - coup_disc - gift_disc + parseFloat(newprice.substr( 1 ));
		if( newtot < 0 ) {
			newtot = 0.0;
		}
		jQuery('#OrderTotalCell').text( '$' + newtot.toFixed(2) );
		jQuery('#ShippingCell').text( 'Shipping (' + shipdesc + '):');
	}
	if( record_ship_sel ) {
		jQuery('#prev_ship_sel').val( shipsel );
		var ship_zip = '';
		if( jQuery('#to_zip').length > 0 ) {
			ship_zip = jQuery('#to_zip').val();
		} else {
			ship_zip = jQuery('#s_zip').val();
		}
		jQuery.get( '/lhp_ajax/ajax_shipopts.php', { to_zip: ship_zip, ship_sel: shipsel } );
		if( jQuery('#gift_code').val().length > 4 ) {
			process_gift_card('');
		}
	}

}

function zip_changed() {
	var ship_zip = jQuery('#to_zip').val();
	if( ship_zip.length == 5 ) {
		var no_prev_sel = false;
		var selected = jQuery("input[name='ship_opt']:checked").val();
		if( !selected ) {
			no_prev_sel = true;
			selected = '';
		}
		//document.body.style.cursor = 'progress';
		jQuery('#ship_update_loading').show();
		jQuery('#ShipCostCell').html( '<img src="/images/ajax-loader.gif" />' );
		jQuery('.CheckoutSectionData:eq(0)').load( '/lhp_ajax/ajax_shipopts.php', { to_zip: ship_zip, to_country: 'US', zip_field: true, ship_sel: selected },
												   function() {
													   if( (no_prev_sel == true) && (jQuery('#prev_ship_sel').length > 0) ) {
														   selected = jQuery('#prev_ship_sel').val();
													   }
													   jQuery('#'+selected).attr("checked", "checked");
													   update_shipping( selected, true, true );
													   //document.body.style.cursor = 'auto';
													   jQuery('#ship_update_loading').hide();
												   });
	} else {
		alert( "Please input a valid 5-digit US zipcode for shipping rate information." );
	}
		
	return( false );
}
 
function show_discount_inputs() {
	jQuery("#gift_row").show();
	jQuery("#coupon_row").show();
	jQuery("#discount_link").hide();
}

function process_gift_card(e){
	var gift_code = jQuery('#gift_code').val();
	jQuery('#GiftCell').html( '<img src="/images/ajax-loader.gif" />' );
	jQuery.getJSON("/lhp_ajax/ajax_discounts.php", { gift_code: gift_code },
		function(data){
			if( data.error.length > 0 ) {
				jQuery('#GiftCell').text( '$0.00' );
				jQuery('#GiftDesc').text( '' );
				alert( 'Sorry: ' + data.error );
			} else {
				var gift_amt = parseFloat( data.gift_amt );
				jQuery('#GiftCell').text( '-$' + gift_amt.toFixed(2) );
				jQuery('#GiftDesc').text( data.gift_desc );
			}
			refresh_price();
			if( data.gift_info && data.gift_info.length > 0 ) {
				alert( data.gift_info );
			}
		});
}

function process_coupon_code(e){
	var coupon_code = jQuery('#coupon_code').val();
	jQuery('#CouponCell').html( '<img src="/images/ajax-loader.gif" />' );
	jQuery.getJSON("/lhp_ajax/ajax_discounts.php", { coupon_code: coupon_code },
	function(data){
		if( data.error.length > 0 ) {
			jQuery('#CouponCell').html( '$0.00' );
			jQuery('#CouponDesc').text( '' );
			alert( 'Sorry: ' + data.error );
		} else {
			var coupon_amt = parseFloat( data.coupon_amt );
			jQuery('#CouponCell').html( '-$' + coupon_amt.toFixed(2) );
			jQuery('#CouponDesc').text( data.coupon_desc );
		}
		refresh_price();
		if( data.coupon_info && data.coupon_info.length > 0 ) {
			alert( data.coupon_info );
		}
	});
}

function copy_addr_values( show_alert ) {
	jQuery('#s_full_name').val( jQuery('#full_name').val() );
	jQuery('#s_address1').val( jQuery('#address1').val() );
	jQuery('#s_address2').val( jQuery('#s_address2').val() );
	jQuery('#s_address3').val( jQuery('#s_address3').val() );
	jQuery('#s_city').val( jQuery('#city').val() );
	jQuery('#s_state').val( jQuery('#state').val() );
	jQuery('#s_country').val( jQuery('#country').val() );
	jQuery('#s_phone').val( jQuery('#phone').val() );
	if( jQuery('#s_zip').val() != jQuery('#zip').val() ) {
		jQuery('#s_zip').val( jQuery('#zip').val() );
		jQuery('#s_zip').change();
		if( show_alert ) {
			alert('Please check shipping rates for any changes since last refresh, then press the complete order button again when you\'re ready.');
			return( false );
		}
	}
	return( true );
}

function s_same_addr_click(btn) {
	if( btn.checked ) {
		copy_addr_values(false);
		jQuery('#ShipAddrDetails').hide();
	} else {
		jQuery('#ShipAddrDetails').show();
	}
}

// This needs to be called before the quickform checks are performed
// because we need to make sure that whatever info was input into
// the billing address gets copied into the shipping fields also if
// the checkbox is checked
function check_addr_copy( show_alert ) {
	if( jQuery('.SameAddrCB').val() ) {
		return( copy_addr_values( show_alert ) );
	} else {
		return( true );
	}
}