$(function(){
	$('._qty').change(function(){UpdateQty(this);UpdateTotals();});
	$('._qty').each(function(){$(this).val('0');UpdateQty(this);UpdateTotals();});
	$('#button_action').click(function(){DoAction();});
});

function UpdateQty(obj){
	if($(obj).val()=='')$(obj).val('0');
	if(!($(obj).val()>0))$(obj).val('0');
	$(obj).val(parseInt($(obj).val()));
	row_id=$(obj).parent().parent().attr('id');
	q=parseInt($(obj).val());
	p=$('#price_'+row_id).val();
	t=((q*p*100)/100).toFixed(2);
	$('#total_'+row_id).val(t);
}
function UpdateTotals(){
	tot_deposit=0;
	$('._deposit').each(function(i){
		row_id=$(this).parent().parent().attr('id')
		thisDeposit=$(this).val();
		if (thisDeposit=='undefined' || thisDeposit==''){
			thisDeposit=0;
		}
		thisQty=$('#qty_'+row_id).val();
		if (thisQty!='undefined' && thisQty!=''){
			tot_deposit+=parseFloat(thisDeposit)*parseInt(thisQty);
		}
	});
	//``````````````````````````````````````````````
	tot_total=0;
	$('._total').each(function(i){
		row_id=$(this).parent().parent().attr('id')
		thisTotal=$(this).val();
		if (thisTotal=='undefined' || thisTotal==''){
			thisTotal=0;
		}
		tot_total+=parseFloat(thisTotal);
	});
	//``````````````````````````````````````````````
	$('#total').val((parseInt(tot_total*100)/100).toFixed(2));
	$('#deposit').val((parseInt(tot_deposit*100)/100).toFixed(2));
	$('#gtotal').val((parseInt((tot_deposit+tot_total)*100)/100).toFixed(2));
}

function DoAction(){
	if ($('#action').val()=='submit' && ValidateForm()){
		SubmitForm();
	}
}

function ValidateForm(){
	bCont=true;
	if( bCont && $('#full_name').val()=='' ){ bCont=false; alert('Please type in your name'); }
	if( bCont && $('#email').val()=='' ){ bCont=false; alert('Please type in your email address'); }
	if( bCont && $('#phone_number').val()=='' ){ bCont=false; alert('Please type in your phone number'); }
	return(bCont);
}

function SubmitForm(){
	var json='';
	var ctr=0;
	//`````````````````````````````````````````````````````
	json+='"order":[';
	$('._orderrow').each(function(i){
		wine_id=$(this).attr('id');
		if ($('#qty_'+wine_id).val()>0){
			json+='{"wine_id":"'+wine_id+'","price":"'+$('#price_'+wine_id).val()+'","qty":"'+$('#qty_'+wine_id).val()+'"},';
			ctr++;
		}
	});
	if (ctr>0){
		json=json.substr(0,json.length-1)+'],';
	}else{
		json+='],';
	}
	//`````````````````````````````````````````````````````
	json+='"full_name":"'+escape($('#full_name').val())+'",';
	json+='"email":"'+escape($('#email').val())+'",';
	json+='"address":"'+escape($('#address').val())+'",';
	json+='"city":"'+escape($('#city').val())+'",';
	json+='"province":"'+escape($('#province').val())+'",';
	json+='"postal_code":"'+escape($('#postal_code').val())+'",';
	json+='"country":"'+escape($('#country').val())+'",';
	json+='"phone_number":"'+escape($('#phone_number').val())+'",';
	json+='"special_instructions":"'+escape($('#special_instructions').val())+'",';
	//`````````````````````````````````````````````````````
	json+='"total":"'+$('#total').val()+'",';
	json+='"deposit":"'+$('#deposit').val()+'",';
	json+='"gtotal":"'+$('#gtotal').val()+'"';
	//`````````````````````````````````````````````````````
	json+='';
	json='{'+json+'}';
	//`````````````````````````````````````````````````````
	$.ajax({
		type: "POST",
		url: "ajax_funcs/process.order.php",
		data: "json="+json,
		success: function(ajax_return){
			//alert( ajax_return );
			if (ajax_return!='0'){
				$('#order_form').slideUp('slow',function(){
					$('#order_form').html('<center><p>Thank you. Your order has been successfully send to us.</p><div style="width:640px; height: 480px; background-color:#FFFFFF; border:1px solid #333333; overflow:auto;"><pre>'+ajax_return+'</pre></div></center>');
					$('#order_form').slideDown('slow');
				});
			}
		}
	});
}