$(document).ready(function()
{
	$("#print_slip").click(function()
	{
		var shipmentIDs = $('#shipmentID').val(); 
		var thishref 	= GLOBAL_BASE_PATH+'/shipment/label/shipmentIDs/'+shipmentIDs+':/';
		$("#print_slip").colorbox({iframe:true, innerWidth:"70%", innerHeight:"60%",href: thishref});
	});	
	
	var onloadMainSmID = $("#sm_main").val();
	if(onloadMainSmID != '')
		fetchServices(onloadMainSmID);
	enablePickUpDate();
	$("#sm_main").change(function(){
		var mainSmID = $(this).val();
		fetchServices(mainSmID);
		$('#realtime_sm_services').removeAttr('disabled');
	});
	$("#changeShippingStatus").click(function(){
		$('#sm_main').removeAttr('disabled');
		$('#realtime_sm_services').removeAttr('disabled');
		$('#packageType').removeAttr('disabled');
		$('#dropOffType').removeAttr('disabled');
	});
	// 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) {
			//alert($(this).attr('id'));
            $("#div_err_formChangeShippingStatus_"+$(this).attr('id')).addClass("hide");
        }
    });
	if($('#deliveredDate').length>0){
		$('#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); 
	if($('#pickupDate').length>0){
		$('#pickupDate').datetimepicker({
			minDate: currDate,
			onSelect: function (dateText, inst) {
				$("#div_err_formChangeShippingStatus_pickupDate").addClass("hide");
			}
		});
     }
	// pickup closing time
	if($('#pickupClosingTime').length>0){
		$('#pickupClosingTime').timepicker({
				onSelect: function (dateText, inst) {
				$("#div_err_formChangeShippingStatus_"+$(this).attr('id')).addClass("hide");
			}
		});
	}
});
$(document).on('click', "#createPickup", function () {
	enablePickUpDate();
});
function enablePickUpDate()
{
	if($("#createPickup").prop("checked") == true)
	{
		$("#pickupDate").removeAttr("disabled");
		$("#pickupClosingTime").removeAttr("disabled");
		$("#pickupDate,#pickupClosingTime").removeClass("textbox").addClass("date_picker_field");
	}
	else
	{
		$("#pickupDate").attr("disabled","disabled");
		$("#pickupClosingTime").attr("disabled","disabled");
		$("#pickupDate,#pickupClosingTime").removeClass("date_picker_field").addClass("textbox");
	}
}

 function checkRealTimeCarrier()
 {
	var carrierID = $("#sm_main").val();
	if(carrierID > 0){
		$.ajax({
			type: "POST",
			url: GLOBAL_BASE_PATH+"/order/checkrealtimecarrier/",
			async: true,
			global: false,
			data: {
				"carrierID": carrierID,
			},
			success: function(isRealTime){
				if(isRealTime !='Yes'){
					$(".pick_up").hide();
				}else{
					if($("#dropOffType").val() == 'REQUEST_COURIER')
						$(".pick_up").show();
					else
						$(".pick_up").hide();
					$("#shipmentStatus").val('Shipment');
				}
				$("#realtimeCarrier").val(isRealTime);
			}
		});
	}
 }
 
 $(document).on('change', "#dropOffType", function () {
        if($("#dropOffType").val() == 'REQUEST_COURIER')
			$(".pick_up").show();
		else
			$(".pick_up").hide();
});

fetchServices = function (smID){
	var serviceId		='';  
	var serviceTypeID	='';
	$('.other').hide();
	$('.service_type').show();
	if(smID == 'other')
	{
		$('.other').show();
		$('.service_type').hide();
		$("#realtimeCarrier").val('No');
		$(".pick_up").hide();
		$(".fedex_options").hide();
		$("#afterPickUp").show();
		return false;
	}
	$('#shipping_service_li').html('');
	var urlAppend = ('') ? "s_id/"+''+"/t_id/"+'' : '';
	$.ajax({
    	type: "GET",
        url: GLOBAL_BASE_PATH+"/shipment/ajaxGetServices/group_id/"+smID+"/"+urlAppend,
		async: true,
		global: false,
        success: function(servicesHtml){
        	$('#realtime_sm_services').html('<option value="">Select</option>'+servicesHtml);
			if(smID == 5){
				$(".fedex_options").show();
			}else{
				$(".fedex_options").hide();
			}
			checkRealTimeCarrier();
			if($('#hid_realtime_sm_services').val() != '')
			{
				$('#realtime_sm_services').val($('#hid_realtime_sm_services').val());
			}
		}
		
	});
}
function checkTrackingNumberRequired()
{
	if( $("#dispatchInfo").prop("checked") == true || $("#deliveredInfo").prop("checked") == true )
	{
		if($("#trackingNumber").val() == '')
			return false;
		else
			return true;
	}
	else
	{
		return true;
	}
}