var manageOrderTable;

$(document).ready(function() {

	var divRequest = $(".div-request").text();

	// top nav bar 
	$("#navOrder").addClass('active');

	if(divRequest == 'add')  {
		// add order	
		// top nav child bar 
		$('#topNavAddOrder').addClass('active');	

		// request date picker
		//$("#requestDate").datepicker();
		
		

		// create order form function
		$("#createOrderForm").unbind('submit').bind('submit', function() {
			var form = $(this);

			$('.form-group').removeClass('has-error').removeClass('has-success');
			$('.text-danger').remove();
				
			//var requestDate = $("#requestDate").val();
			//var projectName = $("#proj_id").val();
				
            /*
			// form validation 
			if(requestDate == "") {
				$("#requestDate").after('<p class="text-danger"> Request Date field is required </p>');
				$('#requestDate').closest('.form-group').addClass('has-error');
			} else {
				$('#requestDate').closest('.form-group').addClass('has-success');
			} // /else

			if(projectName == "") {
				$("#proj_id").after('<p class="text-danger"> Project Name is required </p>');
				$('#proj_id').closest('.form-group').addClass('has-error');
			} else {
				$('#proj_id').closest('.form-group').addClass('has-success');
			} // /else
              */
			


			// array validation
			var productName = document.getElementsByName('cate_name[]');				
			var validateProduct;
			for (var x = 0; x < productName.length; x++) {       			
				var productNameId = productName[x].id;	    	
		    if(productName[x].value == ''){	    		    	
		    	$("#"+productNameId+"").after('<p class="text-danger"> Category Name is required!! </p>');
		    	$("#"+productNameId+"").closest('.form-group').addClass('has-error');	    		    	    	
	      } else {      	
		    	$("#"+productNameId+"").closest('.form-group').addClass('has-success');	    		    		    	
	      }          
	   	} // for

	   	for (var x = 0; x < productName.length; x++) {       						
		    if(productName[x].value){	    		    		    	
		    	validateProduct = true;
	      } else {      	
		    	validateProduct = false;
	      }          
	   	} // for       		   	
	   	
		
		/*
	   	var quantity = document.getElementsByName('quantity[]');		   	
	   	var validateQuantity;
	   	for (var x = 0; x < quantity.length; x++) {       
	 			var quantityId = quantity[x].id;
		    if(quantity[x].value == ''){	    	
		    	$("#"+quantityId+"").after('<p class="text-danger"> Quantity is required!! </p>');
		    	$("#"+quantityId+"").closest('.form-group').addClass('has-error');	    		    		    	
	      } else {      	
		    	$("#"+quantityId+"").closest('.form-group').addClass('has-success');	    		    		    		    	
	      } 
	   	}  // for

	   	for (var x = 0; x < quantity.length; x++) {       						
		    if(quantity[x].value){	    		    		    	
		    	validateQuantity = true;
	      } else {      	
		    	validateQuantity = false;
	      }          
	   	} // for       


         var budget = document.getElementsByName('exp_budget[]');		   	
	   	var validatebudget;
	   	for (var x = 0; x < budget.length; x++) {       
	 			var budgetId = budget[x].id;
		    if(budget[x].value == ''){	    	
		    	$("#"+budgetId+"").after('<p class="text-danger"> Expenditure Budget is required!! </p>');
		    	$("#"+budgetId+"").closest('.form-group').addClass('has-error');	    		    		    	
	      } else {      	
		    	$("#"+budgetId+"").closest('.form-group').addClass('has-success');	    		    		    		    	
	      } 
	   	}  // for

	   	for (var x = 0; x < budget.length; x++) {       						
		    if(budget[x].value){	    		    		    	
		    	validatebudget = true;
	      } else {      	
		    	validatebudget = false;
	      }          
	   	} // for    

       var procKind = document.getElementsByName('proc_kind[]');		   	
	   	var validateprocKind;
	   	for (var x = 0; x < procKind.length; x++) {       
	 			var procKindId = procKind[x].id;
		    if(procKind[x].value == ''){	    	
		    	$("#"+procKindId+"").after('<p class="text-danger"> Procurement Kind is required!! </p>');
		    	$("#"+procKindId+"").closest('.form-group').addClass('has-error');	    		    		    	
	      } else {      	
		    	$("#"+procKindId+"").closest('.form-group').addClass('has-success');	    		    		    		    	
	      } 
	   	}  // for

	   	for (var x = 0; x < procKind.length; x++) {       						
		    if(procKind[x].value){	    		    		    	
		    	validateprocKind = true;
	      } else {      	
		    	validateprocKind = false;
	      }          
	   	} // for  

           
		 // array validation
			var delivery_date = document.getElementsByName('delivery_date[]');				
			var validatedelivery_date;
			for (var x = 0; x < delivery_date.length; x++) {       			
				var delivery_dateId = delivery_date[x].id;	    	
		    if(delivery_date[x].value == ''){	    		    	
		    	$("#"+delivery_dateId+"").after('<p class="text-danger"> Delivery Date is required!! </p>');
		    	$("#"+delivery_dateId+"").closest('.form-group').addClass('has-error');	    		    	    	
	      } else {      	
		    	$("#"+delivery_dateId+"").closest('.form-group').addClass('has-success');	    		    		    	
	      }          
	   	} // for

	   	for (var x = 0; x < delivery_date.length; x++) {       						
		    if(delivery_date[x].value){	    		    		    	
		    	validatedelivery_date = true;
	      } else {      	
		    	validatedelivery_date = false;
	      }          
	   	} // for  

       */		

			//if(requestDate && projectName) {
				if(validateProduct == true) {
					// create order button
					// $("#createOrderBtn").button('loading');

					$.ajax({
						url : form.attr('action'),
						type: form.attr('method'),
						data: form.serialize(),					
						dataType: 'json',
						success:function(response) {
							console.log(response);
							// reset button
							$("#createOrderBtn").button('reset');
							
							$(".text-danger").remove();
							$('.form-group').removeClass('has-error').removeClass('has-success');

							if(response.success == true) {
								
								// create order button
								$(".success-messages").html('<div class="alert alert-success">'+
	            	'<button type="button" class="close" data-dismiss="alert">&times;</button>'+
	            	'<strong><i class="glyphicon glyphicon-ok-sign"></i></strong> '+ response.messages +
	            	//' <br /> <br /> <a type="button" onclick="printOrder('+response.order_id+')" class="btn btn-primary"> <i class="glyphicon glyphicon-print"></i> Print </a>'+
	            	'<a href="add_department.php?o=add" class="btn btn-default" style="margin-left:10px;"> <i class="glyphicon glyphicon-plus-sign"></i> Add New Category </a>'+
	            	
	   		       '</div>');
								
							$("html, body, div.panel, div.pane-body").animate({scrollTop: '0px'}, 100);

							// disabled te modal footer button
							$(".submitButtonFooter").addClass('div-hide');
							// remove the product row
							$(".removeProductRowBtn").addClass('div-hide');
								
							} else {
								alert(response.messages);								
							}
						} // /response
					}); // /ajax
				} // if array validate is true
			//} // /if field validate is true
			

			return false;
		}); // /create order form function	
	
	}  	

}); // /documernt




function addRow() {
	$("#addRowBtn").button("loading");

	var tableLength = $("#productTable tbody tr").length;

	var tableRow;
	var arrayNumber;
	var count;

	if(tableLength > 0) {		
		tableRow = $("#productTable tbody tr:last").attr('id');
		arrayNumber = $("#productTable tbody tr:last").attr('class');
		count = tableRow.substring(3);	
		count = Number(count) + 1;
		arrayNumber = Number(arrayNumber) + 1;					
	} else {
		// no table row
		count = 1;
		arrayNumber = 0;
	}

	$.ajax({
		url: 'php_action/fetchCate.php',
		type: 'post',
		dataType: 'json',
		success:function(response) {
			$("#addRowBtn").button("reset");			

			var tr = '<tr id="row'+count+'" class="'+arrayNumber+'">'+			  				
				 '<td style="padding-left:20px;">'+
					'<div class="form-group">'+
					''+count+''+
					'</div>'+
				'</td>'+

                '<td style="padding-left:20px;"">'+
					'<input type="text" name="cate_name[]" class="form-control" id="cate_name'+count+'"   placeholder="Category Name" autocomplete="off"/>'+
				'</td style="padding-left:20px;">'+
			
			   
			
				'<td>'+
					'<button class="btn btn-default removeProductRowBtn" type="button" onclick="removeProductRow('+count+')"><i class="glyphicon glyphicon-trash"></i></button>'+
				'</td>'+
			'</tr>';
			if(tableLength > 0) {							
				$("#productTable tbody tr:last").after(tr);
			} else {				
				$("#productTable tbody").append(tr);
			}		

		} // /success
	});	// get the product data

} // /add row

function removeProductRow(row = null) {
	if(row) {
		$("#row"+row).remove();


		subAmount();
	} else {
		alert('error! Refresh the page again');
	}
}


