/* 
* Auction script
* developed by The-Di-Lab
* contact me at thedilab@gmail.com
*/

/*
 * Class AjaxBidForm
 * It binds bid actions to bidding forms
 * params:
 * formName- class name of the form element
 * 
 * It should not update any form content after bidding,
 * since Cron class is taking of this behavior.
 * This class should only update backend data and display information.
 *
 */
function AjaxBidForm(formName){
		var thisForm = $('.'+formName);
		$(thisForm).submit(function(){
		   //main		   
		   $.ajax({
				  async: true,
				  type: 'POST',
				  url: $(this).attr('action'),
				  dataType:'json',
				  data: $(this).serialize(),
				  error: function(e,text, xhr){
				    alert(text+", reload page and bid again.");
				  },
				  ajaxError:function(){
				  	alert('error loading data, refresh your page.');
				  },
				  success:function(returnDt) { 
							react(returnDt);		
				  }
			});
		
			return false;
		});			
		//handle response
		function react(data){
			var status = data.Auction.status;
			switch(status)
				{
				case 1:					  		 
				case 2:
				case 3:					  		 
				case 4:
				case 5:
				case 6:
				case 0:
					displayMsg(data.Auction.message);
				  //alert(data.Auction.message);
				  break;
				default:
				  alert(status);
				}
		}
		//display Message
		function displayMsg(msgBox){
			  $.jGrowl(msgBox);
		}
}	

