$(document).ready(function(){
	$(".common_status").hide();
	//$("#bulkShippingStatus").val('');
	$("#searchProductCity").multiselectCustom({
	   noneSelectedText: "Select City",
	   selected:"cities"
	});
	$("#searchBusiness").multiselectCustom({
	   noneSelectedText: lblSelectBusiness,
	   selected:"business"   
	});
	$('[task=delete]').click(function(){
		var thehref	=	$(this).attr("rel");	
		jConfirm(msgConfirmDelete, 'Alert', function(r) {
			if(r==true) {
				 window.location.href	=	GLOBAL_BASE_PATH+thehref;
				 return true; 
			}else
				return false;
		});       
	});
	
	var onloadMainSmID = $("#sm_main").val();
		fetchServices(onloadMainSmID);
	
        $("#createLabel").click(function(){               
		if($("input[name='checkOrderIDs[]']:checked").length<=0){
			jAlert(lblSelectItem);
			return false;
		}else{
			var orderNumbers="";
			
			$("input[name='checkOrderIDs[]']:checked").each(function() {
				 var currorderNumbers= parseInt($(this).val());	
				 if(currorderNumbers>0) {
				 	orderNumbers = orderNumbers+currorderNumbers+":";
				 }
			});
										
			var thishref	=	$("a.listLabels").attr("href");
			thishref		+= "shipmentIDs/"+orderNumbers+"/";
				$("#createLabel").colorbox({iframe:true, innerWidth:800, innerHeight:460,href:thishref}).trigger('click');
		
		
		}
	});
 	// dispatch and shipped Up datetime picker	
	var todayDate = new Date;
	todayDate.setDate(todayDate.getDate());
	todayDate.setHours(todayDate.getHours());
	todayDate.setMinutes(todayDate.getMinutes());
	
	$('#dispatchDate, #dispatchExpDate').datepicker({
        minDate: todayDate,
        onSelect: function (dateText, inst) {
            $("#div_err_formChangeShippingStatus_"+$(this).attr('id')).addClass("hide");
        }
    });
	$('#deliveredDate').datetimepicker({
        onSelect: function (dateText, inst) {
            $("#div_err_formChangeShippingStatus_"+$(this).attr('id')).addClass("hide");
        }
    });
	// Pick Up datetime picker
	var currDate = new Date;
	currDate.setDate(currDate.getDate());
	currDate.setHours(currDate.getHours());
	currDate.setMinutes(currDate.getMinutes()+30); 

	$('#pickupDate').datetimepicker({
        minDate: currDate,
        onSelect: function (dateText, inst) {
            $("#div_err_formChangeShippingStatus_pickupDate").addClass("hide");
        }
    });
	
	// pickup closing time
	$('#pickupClosingTime').timepicker({
            onSelect: function (dateText, inst) {
            $("#div_err_formChangeShippingStatus_"+$(this).attr('id')).addClass("hide");
        }
    });	
	
	$(document).on("change","#bulkShippingStatus",function(){
		$(".common_status").hide();
		$('.'+$(this).val()+'_class').show();
		$("#div_err_formShipments_pickupDate,#div_err_formShipments_pickupClosingTime,#div_err_formShipments_dispatchDate,#div_err_formShipments_dispatchExpDate,#div_err_formShipments_deliveredDate").addClass('hide');
	});
	
	$(document).on("click","#submitBulkStatus",function(){
		if($(".ListAll:checked").size()==0)
		{
			jAlert(lblSelectItem);
			return false;
		}
		$('#pickupDate,#pickupClosingTime,#dispatchDate,#dispatchExpDate,#deliveredDate ').blur();
		if(!$(this).validateForm({form: "formShipments"}))
			return false;
		if($("#bulkShippingStatus").val() != '')
			$("#formShipments").submit();
		return false;
	});
});

$("#sm_main").change(function(){
	var mainSmID = $(this).val();
	fetchServices(mainSmID);
});

fetchServices = function (smID){
	if(smID != '')
	{
		var serviceId='';  
		var serviceTypeID='';  
		$('#shipping_service_li').html('');
		var urlAppend = ('') ? "s_id/"+''+"/t_id/"+'' : '';
		$.ajax({
			type: "GET",
			url: GLOBAL_BASE_PATH+"/shippingmethod/ajaxGetServices/group_id/"+smID+"/"+urlAppend,
			async: true,
			global: false,
			success: function(servicesHtml){
				$('#realtime_sm_services').html('<option value="">Select</option>'+servicesHtml);
			if($("#selectedServiceType").val() != '')
				$('#realtime_sm_services').val($("#selectedServiceType").val());
			}
		});
	}
	else{
		$('#realtime_sm_services').html('<option value="">Select</option>');
	}
}
