
$.validator.messages.required = "Please specify the quantity of one of the products.";
$(document).ready(function(){
				var validator = $("form#productChoice").validate({
												submitHandler: function(form) {
																   if(selectedAtLeastOne(form)){
																	   alert("Please specify the quantity of one of the products");
																   }else{
																		form.submit();
																   }
															   },
												 groups:{
													 qty:"LiteQty FullQty NetworkQty ServerQty UsersQty DirectoryQty OtherQty"
													 },
												 rules:{
													 LiteQty:{
														 number:true,
														 max:200,
														 min:0,
														 required: selectedAtLeastOne

													 },
													 FullQty:{
														 number:true,
														 min:0,
														 max:200,
														 required: selectedAtLeastOne
													 },
													 NetworkQty:{
														 number:true,
														 min:0,
														 max:200,
														 required: selectedAtLeastOne
													 },
													 ServerQty:{
														 number:true,
														 min:0,
														 max:200,
														 required: selectedAtLeastOne
													 },
													 UsersQty:{
														 number:true,
														 min:0,
														 max:200,
														 required: selectedAtLeastOne
													 },
													 DirectoryQty:{
														 number:true,
														 min:0,
														 max:200,
														 required: selectedAtLeastOne
													 },
													 OtherQty: {
														 number:true,
														 min:0,
														 max:500
													 },
													 Other:{
														 required: "input[name='OtherQty']:filled"
													 }
												},
												errorPlacement:function(error,element){
													$("div#errorContainer").prepend(error);
												}
												 
												 });
				
				var inputFields = $("table#purchaseTable tbody:first-child td:nth-child(2) input:text");
				$(inputFields).change(function(){
											   $(inputFields).each(function(){
											   			validator.element(this);
												});
				});
});

function selectedAtLeastOne(element){
	var cells = $("table#purchaseTable tbody:first-child td:nth-child(2)");
	var selectedQuantity = 0;
	$(cells).each(function(i){
						  var value = parseInt($(this).children("input:text").val());
				selectedQuantity += isNaN(value)?0:value;
			});
	var otherQty = parseInt($("input[name='OtherQty']").val() );
	otherQty = isNaN(otherQty)?0:otherQty;

	return selectedQuantity <= 0 && otherQty <=0;

}
