//**********************************************************************************************************************
/**
* DOCUMENT: /core/plugins/register/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 register plugin.
*/
//***********************************************************************************************************************

function update_total() {

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

	$.post("#", { action: 'update_total', attendees: attendees },function(data){
		
		var explode = data.split('-');

		$('#total').html('$'+explode[0]);
		$('#discount').html('($'+explode[1]+')');
		
	});
}

function apply_promo() {

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

	$.post("#", { action: 'apply_promo', 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]);
			
		}
		
		else if(explode[0] == 'success') {
			$('#promo_discount').html('($'+explode[1]+')');
			$('#total').html('$'+explode[2]);
			$('#promo_error').html('');
		}
		
	});
}

function same_as_training() {

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

		var explode = data.split('|');

		$('.cc_first_name').val(explode[0]);	
		$('.cc_last_name').val(explode[1]);
		$('.cc_title').val(explode[2]);
		$('.cc_company').val(explode[3]);
		$('.cc_phone').val(explode[4]);
		$('.cc_email').val(explode[5]);
		$('.cc_address').val(explode[6]);
		if(explode[10] == 'international') {
			$('input[name="address_select_1"]').val("international").attr('checked', true);
			$('#us_1').hide();
			$('#international_1').show();
		}
		$('.cc_address_int').val(explode[11]);
		$('.cc_city').val(explode[7]);
		$('.cc_state').val(explode[8]);
		$('.cc_zip').val(explode[9]);
		
	});
}


//***********************************************************************************************************************
//ON DOCUMENT READY FUNCTIONS
//***********************************************************************************************************************
$(function() {

	$('.address_select').bind('click', function() {
		var item_id_orig = $(this).attr("name");
		item_arr = item_id_orig.split("_");
		item_id = item_arr[2];
		var type 	= $(this).attr("value");
		
		if(type == 'us') {
			var other_type = 'international';
		} else {
			var other_type = 'us';
		}
		
		$('#'+other_type+'_'+item_id).hide();
		$('#'+type+'_'+item_id).show();
	});

	if($('#attendees').length) {
		update_total();
	}

	if($('#attendees').val() == "") {
		$('#attendees').val(0);
	}

	$('#attendees').click(function () {
		$(this).attr('value','');
	});
	

	
	$('#attendees').change(function () {
		validate($(this));
		update_total();

	});
	
	$('#attendees').keyup(function (event) {
		if(event.keyCode != '8' && event.keyCode != '46') {
			validate($(this));
		}
		update_total();

	});
	
	if($('#promo').length) {
		if($('#promo').val() != '') {
			apply_promo();
		}
	}
	
	$('#promo').blur(function () {
		apply_promo();
	});
	
	$('#promo').change(function () {
		apply_promo();
	});
	
	$('#promo').keyup(function () {
		apply_promo();
	});
	
	$('#same_as').click(function () {
		if($(this).attr('checked')) {
			same_as_training();
		}
	});
	
	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');
		}
	}

});
