function removeDiscountCode() {
	$.post("process/cart.php",
	{ 
		action: 'removeDiscountCode'
 	}, 
 	function (data){
		window.location = '/?page=my_cart';
 	},"json");
}

function addDiscountCode() {
	if($('#discount_code').val()) {
		$.post("process/cart.php",
		{ 
			action: 'addDiscountCode',
			code: $('#discount_code').val()
	 	}, 
	 	function (data){
			window.location = '/?page=my_cart';
	 	},"json");
 	}
 	else {
 		alert('No discount code entered.');
 	}
}

function addToCart(item,qty) {
	if(!qty) {
		qty = $('#qty').val();
	}
	if(!isNaN(parseFloat(qty)) && qty>0) { 
		$.post("process/cart.php",
		{ 
			action: 'addToCart',
			qty: qty,
			item: item
	 	}, 
	 	function (data){
	 		$('.cart').css('display','block');
	 		$('.cart').html('<a href="/?page=my_cart">My Cart('+data.item_count+')</a>');
			window.location = '/?page=my_cart';
	 	},"json");
 	}
 	else {
 		alert('Invalid quantity');
 	}
}

function removeFromCart(item) {
	$.post("process/cart.php",
	{ 
		action: 'removeFromCart',
		item: item
 	}, 
 	function (data){
 		if(data.item_count!=0) {
 			$('.cart').html('<a href="/?page=my_cart">My Cart('+data.item_count+')</a>');
 			window.location = '/?page=my_cart';
		}
		else {
			$('.cart').css('display','none');
			window.location = '/';
		}
 	},"json");
}

function editCart(item,id) {
	if(!isNaN(parseFloat($('#qty_'+id).val()))) {
		if($('#qty_'+id).val()>0) {
			$.post("process/cart.php",
			{ 
				action: 'editCart',
				qty: $('#qty_'+id).val(),
				item: item
		 	}, 
		 	function (data){
				window.location = '/?page=my_cart';
		 	},"json");
	 	}
	 	else {
	 		removeFromCart(item);
	 	}
 	}
 	else {
 		alert('Invalid quantity');
 	}
}

function checkOut() {
	$('#checkout_button').css('display','none');
	$('#checkout_dialog').slideDown();
}

function completeCheckOut() {
	$.post("process/cart.php",
	{ 
		action: 'completeCheckOut',
		first_name: $('#first_name').val(),
		last_name: $('#last_name').val(),
		title: $('#title').val(),
		org: $('#org').val(),
		address: $('#address').val(),
		city: $('#city').val(),
		state: $('#state').val(),
		zip: $('#zip').val(),
		phone: $('#phone').val(),
		email: $('#email').val(),
		transaction_id: $('#trans_id').val()
 	}, 
 	function (data){
 		if(data.completed!=1) {
			$('.hint').remove();
			for (var i=0;i<data.form_errors.length;i++) {
				switch(data.form_errors[i]) {
					case 'first_name':
						$('#'+data.form_errors[i]).after('<div class=\'hint\'>Please enter your first name.</div>');
						break;
					case 'last_name':
						$('#'+data.form_errors[i]).after('<div class=\'hint\'>Please enter your last name.</div>');
						break;
					case 'address':
						$('#'+data.form_errors[i]).after('<div class=\'hint\'>Please enter your billing address.</div>');
						break;
					case 'city':
						$('#'+data.form_errors[i]).after('<div class=\'hint\'>Please enter your billing city.</div>');
						break;
					case 'state':
						$('#'+data.form_errors[i]).after('<div class=\'hint\'>Please enter your billing state.</div>');
						break;
					case 'zip':
						$('#'+data.form_errors[i]).after('<div class=\'hint\'>Please enter your billing zip.</div>');
						break;
					case 'phone':
						$('#'+data.form_errors[i]).after('<div class=\'hint\'>Please enter your billing phone number.</div>');
						break;
					case 'email':
						$('#'+data.form_errors[i]).after('<div class=\'hint\'>Please enter your email.</div>');
						break;
				}
			}
 		}
 		else {
			window.location = '/?page=order_complete&i='+data.invoice_id;
		}
 	},"json");
}

function confRegistration(id) {
	volunteer = $('input[name=volunteer_'+id+']') || [].join('|');
	$.post("process/cart.php",
	{ 
		action: 'registerConference',
		email: $('#email_'+id).val(),
		badge: $('#badge_'+id).val(),
		volunteer: $('input[name=volunteer_'+id+']:checked').val(),
		first_time: $('#first_time_'+id).val(),
		transaction_id: $('#trans_id').val(),
		type: $('#type_'+id).val(),
		id: id
 	}, 
 	function (data){
 		if(data.completed!=1) {
			$('.hint').remove();
			for (var i=0;i<data.form_errors.length;i++) {
				switch(data.form_errors[i]) {
					case 'badge':
						$('#'+data.form_errors[i]+'_'+id).after('<div class=\'hint\'>Please enter the name you would like on your badge.</div>');
						break;
					case 'title':
						$('#'+data.form_errors[i]+'_'+id).after('<div class=\'hint\'>Please enter your title of the organization.</div>');
						break;
					case 'organization':
						$('#'+data.form_errors[i]+'_'+id).after('<div class=\'hint\'>Please enter in your organization.</div>');
						break;
					case 'conf_found':
						$('#conf_form_'+id).html('<img src="/bsi_package/bsiadmin/images/icons/25/cancel.png" />Our records show you already registered for this conference.  Please remove this item from your cart.  If this is incorrect please contact CHART for help.');
				}
			}
 		}
 		else {
 			$('#conf_form_'+id).html(data.output);
		}
 	},"json");
}
