// JavaScript Document 
var d 	= new Date();
var nd 	= new Date(d.getFullYear(),d.getMonth(),d.getDate()+1,d.getHours()-1,d.getMinutes(),d.getSeconds());
$(function() {

    function cb(start, end) {
        $('#orderStatusDate').html(start.format(datePickerDateFormatJs) + ' - ' + end.format(datePickerDateFormatJs));
    }
    cb(moment().subtract(29, 'days'), moment());
    $('#orderStatusDate').daterangepicker({
        ranges: {
           [lblToday]: [moment(), moment()],
           [lblYesterday]: [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
           [lblLast7Days]: [moment().subtract(6, 'days'), moment()],
           [lblLast30Days]: [moment().subtract(29, 'days'), moment()],
           [lblThisMonth]: [moment().startOf('month'), moment().endOf('month')],
           [lblLastMonth]: [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
		 locale: {
          cancelLabel: cancelLabel,
          format: datePickerDateFormatJs,
          applyLabel: applyLabel,
		  customRangeLabel: customRangeLabel
      }
    }, cb);


});
	
$('.ui-daterangepicker-dateRange').addClass('ui-state-active');	
 $(function(){
     $("#submitSearch").click(function(){
    if($.trim($('#orderStatusDate').val())!=""){
      date = $.trim($('#orderStatusDate').val());
      string = date.split(' - ');
      if(string[1]!='' && (new Date(string[0]).getTime() > new Date(string[1]).getTime())) {
      jAlert($("#searchAlert").val());
                           return false;
       } else {
                            return true;  
       }
    }
         });
	 });
$(document).ready(function(){
    $(".common_status").hide();
$('.main_menu li a[rel=Coupons]').addClass('active');
	$("#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.createLabelLink").attr("href");
			thishref		+= "shipmentIDs/"+orderNumbers+"/";
				$(".createLabelLink").colorbox({iframe:true, innerWidth:800, innerHeight:460,href:thishref}).trigger('click');
		
		
		}
	});
	
	
	$("#ListStatusLinkCheckbox").click(function(){
		if($('#ListStatusLinkCheckbox').prop('checked')){
			$('.ListAll').prop('checked',true);
		}else{
			$('.ListAll').prop('checked',false);
		}
	 });
	//  $('.main_menu li a[rel=coupons]').addClass('active');	
	$('[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;
		});       
	});
// 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;
	});
});
