//**********************************************************************************************************************
/**
* DOCUMENT: /core/plugins/store/front/content.front.js
* DEVELOPED BY: Ryan Stemkoski
* COMPANY: Zipline Interactive
* EMAIL: ryan@gozipline.com
* PHONE: 509-321-2849
* DATE: 4/28/2010
* DESCRIPTION: This document has all of the javascript functions required for the front-end store plugin.
*/
//***********************************************************************************************************************

/************************************************************************
/ SAME AS
/************************************************************************/

function same_as_store() {
		
	$('.cc_first_name').val($('.ship_first_name').val());	
	$('.cc_last_name').val($('.ship_last_name').val());
	$('.cc_phone').val($('.ship_phone').val());
	$('.cc_email').val($('.ship_email').val());
	$('.cc_address').val($('.ship_address').val());
	if($('input[name="address_select_"]:checked').val() == 'international') {
		$('input[name="address_select_cc"]').val("international").attr('checked', true);
		$('#cc_us').hide();
		$('#cc_international').show();
	}
	$('textarea[name="cc_address_int"]').val($('textarea[name="address_int"]').val());
	$('.cc_city').val($('.ship_city').val());
	$('.cc_state').val($('.ship_state').val());
	$('.cc_zip').val($('.ship_zip').val());
		
}

/************************************************************************
/ VALIDATE QTY
/************************************************************************/

function validate(item) {
	$(item).attr('value',$(item).val().replace(' ', ''));
	$(item).attr('value',$(item).val().replace('-', ''));
	$(item).attr('value',$(item).val().replace('+', ''));
	
	if(isNaN($(item).val()) || $(item).val() == '' || $(item).val() == ' ') {
		$(item).attr('value','0');
	}
}

/************************************************************************
/ UPDATE QTY
/************************************************************************/

function update_qty(item) {

	var name	= $(item).attr('name');
	var qty 	= $(item).attr('value');
	
	$.post("#", { action: 'update_qty', name: name, qty:qty });


}

/************************************************************************
/ PROMO CODE
/************************************************************************/

function apply_promo_store() {

	var promo	= $('#promo_store').val();

	$.post("#", { action: 'apply_promo_store', promo: promo },function(data){

		var explode = data.split('|');
		
		if(explode[0] == 'error') {
			if(promo != '') {
				$('#promo_error').html(explode[1]);
			}
			else {
				$('#promo_error').html('');
			}
			
			$('#promo_discount').html('-$'+explode[2]);
			$('#total').html('$'+explode[3]);
			$('#subtotal').html('$'+explode[4]);
			
		}
		
		else if(explode[0] == 'success') {
			$('#promo_discount').html('-$'+explode[1]);
			$('#total').html('$'+explode[2]);
			$('#subtotal').html('$'+explode[3]);
			$('#promo_error').html('');
		}
		
	});
}


/************************************************************************
/ SHIPPING
/************************************************************************/

function update_shipping() {

	var shipping_type	= $('#shipping_type').val();

	$.post("#", { action: 'update_shipping', shipping_type: shipping_type },function(data){

		var explode = data.split('|');
		
		$('#shipping').html('$'+explode[0]);
		$('#total').html('$'+explode[1]);
		
	});
}



//***********************************************************************************************************************
//ON DOCUMENT READY FUNCTIONS
//***********************************************************************************************************************
$(function() {
	
	/************************************************************************
	/ SAME AS
	/************************************************************************/
	
	$('#same_as_store').click(function () {
		if($(this).attr('checked')) {
			same_as_store();
		}
	});
	
	/************************************************************************
	/ QUANTITY
	/************************************************************************/
	
	$('.qty-box').click(function () {
		$(this).attr('value','');
	});
	
	$('.qty-box').blur(function () {
		validate($(this));
		update_qty($(this));
	});
	
	$('.qty-box').change(function () {
		validate($(this));
		update_qty($(this));
	});
	
	$('.qty-box').keyup(function () {
		validate($(this));
		update_qty($(this));
	});
	
	var item_count 	= $('.qty-box').length;
	var count		= 0;
	
	while(count < item_count) {
		if($('.qty-box').eq(count).val() == '') {			
			$('.qty-box').eq(count).val(0);
		}
		count++;
	}
	
	/************************************************************************
	/ PROMO
	/************************************************************************/
	
	if($('#promo_store').length) {
		if($('#promo_store').val() != '') {
			apply_promo_store();
		}
	}
	
	$('#promo_store').blur(function () {
		apply_promo_store();
	});
	
	$('#promo_store').change(function () {
		apply_promo_store();
	});
	
	$('#promo_store').keyup(function () {
		apply_promo_store();
	});
	
	
	/************************************************************************
	/ SHIPPING
	/************************************************************************/
	
	if($('#shipping_type').length) {
		if($('#shipping_type').val() != '') {
			update_shipping();
		}
	}
	
	$('#shipping_type').blur(function () {
		update_shipping();
	});
	
	$('#shipping_type').change(function () {
		update_shipping();
	});

	/************************************************************************
	/ INTERNATIONAL ADDRESSES
	/************************************************************************/
	$('.address_select_store').bind('click', function() {
		var item_id_orig = $(this).attr("name");
		item_arr = item_id_orig.split("_");
		item_id = item_arr[2];
		if(item_id == "cc") {
			item_id = item_id+"_";
		}
		var type = $(this).attr("value");
		
		if(type == 'us') {
			var other_type = 'international';
		} else {
			var other_type = 'us';
		}

		$('#'+item_id+other_type).hide();
		$('#'+item_id+type).show();
	});
	
});
