/* Ajax / call Handler for MeritBuilder */
$(document).ready(function(){ 




/*Shipto Ajax Function for product shipping checkout */
	$('#shipto').bind('change', function(){
		var addressId = $(this).val();
		$.ajax({
			type: 'POST',
			url: '/ajax/getShippingInfo',
			dataType: 'json',
			data: {addressId:addressId}, 
		 	success: function(data){
				/* 	lets prepopulate the form with addys */
				$('#label').val(data.label);
				$('#address').val(data.address);
				$('#city').val(data.city);
				$('#state').val(data.state);
				$('#shipZip').val(data.zip);
				$(this).unbind();
			}
		});
	});



/*Calculate Total for credit card charge and update */
	
	$('#numOfBucks').bind('change', function(){
		var numOfBucks = $(this).val();
		var orderTotal = $('#orderTotal').html();
		$.ajax({
			type: 'POST',
			url: '/ajax/creditCardTotal',
			dataType: 'json',
			data: {numOfBucks:numOfBucks,orderTotal:orderTotal}, 
		 	success: function(data){
				$('#bucksPurchaseTotal').html(data.total);
				$('#remainingBucks').html(data.remainingBucks);
				$('#purchasedBucks').html(numOfBucks);
				$(this).unbind();
			}
		});
	});




/* Skill Update of last used */
	
	$('.lastUsed').bind('change', function(){
		var skillId = $(this).parent().find(".lastUsed").attr("id"); 
		var lastUsed = $(this).val();


		$.ajax({
			type: 'POST',
			url: '/ajax/updateSkill',
			dataType: 'json',
			data: {skillId:skillId,lastUsed:lastUsed}, 
		 	success: function(data){
				$(this).unbind();
			}
		});
/*
		$(this).parents().find('.skillWrapper').effect("highlight", {color:'green'}, 3000);
*/

			$(this).parents(".skillWrapper:first").animate({
		        backgroundColor: "#8fd422",
		        opacity: ".45"
		      }, 250 );
		      
		      
				$(this).parents(".skillWrapper:first").animate({
		        // $(this).closest('.skillWrapper').animate({ 
		        backgroundColor: "#f5f5f5",
		        opacity: "1.0"
		      }, 250 );
		      
	});







/* Skill Update of level*/
	
	$('.level').bind('change', function(){
		var skillId = $(this).parent().find(".level").attr("id"); 
		var level = $(this).val();


		$.ajax({
			type: 'POST',
			url: '/ajax/updateSkill',
			dataType: 'json',
			data: {skillId:skillId,level:level}, 
		 	success: function(data){
				$(this).unbind();
			}
		});
/*
		$(this).parents().find('.skillWrapper').effect("highlight", {color:'green'}, 3000);
*/

			$(this).parents(".skillWrapper:first").animate({
		        backgroundColor: "#8fd422",
		        opacity: ".45"
		      }, 250 );
		      
		      
			$(this).parents(".skillWrapper:first").animate({
		        backgroundColor: "#f5f5f5",
		        opacity: "1.0"
		      }, 250 );
		      
	});





/* Skill Update of class of skill */
	
	$('.skillClass').bind('change', function(){
		var skillId = $(this).parent().find(".skillClass").attr("id"); 
		var skillClass = $(this).val();
		$.ajax({
			type: 'POST',
			url: '/ajax/updateSkill',
			dataType: 'json',
			data: {skillId:skillId,skillClass:skillClass}, 
		 	success: function(data){
				$(this).unbind();
			}
		});


			$(this).parents(".skillWrapper:first").animate({
		        backgroundColor: "#8fd422",
		        opacity: ".45"
		      }, 250 );
		      
		      
			$(this).parents(".skillWrapper:first").animate({
		        backgroundColor: "#f5f5f5",
		        opacity: "1.0"
		      }, 250 );
		      
	});























});