    
function addUpdateSchedule(src,obj){
    

    var fieldSchedule1 = new sap.m.Input({enabled:false, width: "100%" });
    var fieldSchedule2 = new sap.m.Input({width: "100%" });
    var fieldSchedule3 = new sap.m.Input({  showValueHelp: true,valueHelpOnly: true,
        placeholder: oBundle.getText("Select_vehicle_from_inventry_list"),
        valueHelpRequest: function () {
            selectVehicleDialog(fieldSchedule20,fieldSchedule3,fieldSchedule4);
        }
    });
    var fieldSchedule4 = new sap.m.Input({type:"Number", value:"0", width: "100%" , description: oBundle.getText("KM")});
    var fieldSchedule5 = new sap.m.DatePicker({width: "100%",displayFormat: "dd-MMM-yyyy",valueFormat: "dd-MMM-yyyy"});

    var fieldSchedule20 = new sap.m.Input({enabled:false,width: "100%" });

    var fieldScheduleNote = new sap.m.TextArea({width:"100%",height:"70px",rows : 2, wrapping : sap.ui.core.Wrapping.on});

    var oSimpleListBox = new sap.ui.commons.ListBox({width:"100%",height:"150px",
    select: function (e) {
       
         var oDialog1 = new sap.ui.commons.Dialog({
                width:"30%",
                height:"150px"
            });
            oDialog1.setTitle(oBundle.getText("Alert"));
            var oText = new sap.ui.commons.TextView({text: oBundle.getText("Do_you_want_to_remove_item")});
            oDialog1.addContent(oText);
            oDialog1.addButton(new sap.ui.commons.Button({ text: oBundle.getText("Remove"), press: function () {
	           oDialog1.close();
	           jQuery.each(oSimpleListBox.getSelectedItems(), function(idx,item) { oSimpleListBox.removeItem(item)});
            } 
            }));
            oDialog1.addButton(new sap.ui.commons.Button({text: oBundle.getText("Cancel"), press:function(){oDialog1.close();}}));
            oDialog1.open();
    }
});

    var btnAddTask = new sap.m.Button({ icon: "sap-icon://sys-add", text: oBundle.getText("Add"), width: "50%",
        layoutData: new sap.ui.layout.GridData({ span: "L2 M4 S6" }),
        press: function (oEvent) {
            addScheduleTaskItemDialog(oSimpleListBox);
        } 
    });

    // var btnAddTaskMaster = new sap.m.Button({ type: "Accept", icon: "sap-icon://activities", width: "50%",
    //         text:oBundle.getText("Task_List"),
    //         press: function (event) {
    //             taskMasterSelectDialog(2,oSimpleListBox);
    //         }
    //     });

    var repeatTypeSB = new sap.m.SegmentedButton({
        items: [new sap.m.SegmentedButtonItem({ key: "1", text: oBundle.getText("Repeat") }),
                new sap.m.SegmentedButtonItem({ key: "2", text: oBundle.getText("One_Time") })],
        selectionChange: function (oEvent) {
            var btnKey = oEvent.getParameter('item').getKey();

            if (btnKey == "1") {
                // repeatCB1.setText(oBundle.getText("Every"));
                repeatCB2.setText(oBundle.getText("Every"));
                intervalDuration.setVisible(true);
                intervalDurationUnitSB.setVisible(true);
                fieldScheduleOnDate.setVisible(false);
            } else {
                // repeatCB1.setText(oBundle.getText("At"));
                repeatCB2.setText(oBundle.getText("On"));
                intervalDuration.setVisible(false);
                intervalDurationUnitSB.setVisible(false);
                fieldScheduleOnDate.setVisible(true);
            }
        }
    });

     var repeatCB1 = new sap.m.CheckBox({ text: oBundle.getText("Every"), selected: false,
        select: function (oEvent) {
            if (this.getSelected()) {
                intervalMiles.setEnabled(true);
                notifyMiles.setEnabled(true);
            }else{
                intervalMiles.setEnabled(false);
                notifyMiles.setEnabled(false);
            }
        }
    });
     var intervalMiles = new sap.m.Input({enabled:false,value:"0",type:"Number", width: "100%", description:oBundle.getText("KM")});
     
     var repeatCB2 = new sap.m.CheckBox({text:oBundle.getText("Every"),selected: true,
        select: function (oEvent) {
            if (this.getSelected()) {
                intervalDuration.setEnabled(true);
                intervalDurationUnitSB.setEnabled(true);
                fieldScheduleOnDate.setEnabled(true);
                notifyDuration.setEnabled(true);
                notifyDurationUnitSB.setEnabled(true);
            }else{
                intervalDuration.setEnabled(false);
                intervalDurationUnitSB.setEnabled(false);
                fieldScheduleOnDate.setEnabled(false);
                notifyDuration.setEnabled(false);
                notifyDurationUnitSB.setEnabled(false);
            }
        }
     });
     var intervalDuration = new sap.m.Input({enabled:true,type:"Number", width: "80px"});


     var intervalDurationUnitSB = new sap.m.SegmentedButton({enabled:true,width: "90%" ,
		items: [new sap.m.SegmentedButtonItem({key:"1",width: "30%",text:oBundle.getText("Day")}), 
                new sap.m.SegmentedButtonItem({key:"2",width: "45%",text:oBundle.getText("Week")}),
                new sap.m.SegmentedButtonItem({key:"3",width: "45%",text:oBundle.getText("Month")}),
                new sap.m.SegmentedButtonItem({key:"4",width: "30%",text:oBundle.getText("Year")})
       ]});
       var fieldScheduleOnDate = new sap.m.DatePicker({width: "100%",displayFormat: "dd-MMM-yyyy",
            valueFormat: "dd-MMM-yyyy",visible:false,enabled:true,minDate:new Date()});

     var notifyMiles = new sap.m.Input({enabled:false,type:"Number", value:"0",width: "100%", description:oBundle.getText("KM")});
     var notifyDuration = new sap.m.Input({enabled:true,type:"Number",value:"0", width: "100px"});
     var notifyDurationUnitSB = new sap.m.SegmentedButton({enabled:true,width: "100%" ,
		items: [new sap.m.SegmentedButtonItem({key:"1",width: "50%",text:oBundle.getText("Day")}), 
                new sap.m.SegmentedButtonItem({key:"2",width: "50%",text:oBundle.getText("Week")}),
                new sap.m.SegmentedButtonItem({key:"3",width: "50%",text:oBundle.getText("Month")})
        ]});

    var assignTeamSchedule = new sap.m.MultiInput({ showSuggestion: true, fieldWidth: "100%",
        showValueHelp: true,
        valueHelpOnly: true,
        valueHelpRequest: function () { assignedTeamDialog(4,assignTeamSchedule); },
        suggestionItemSelected: function (oEvent) {
            
            var tokenList = this.getTokens();
            var selectedItem = oEvent.getParameter("selectedItem");
            if (selectedItem) {
                var insertFlag = true;
                for (var i = 0; i < tokenList.length; i++) {
                    if (tokenList[i].getKey() == selectedItem.getKey()) { insertFlag = false; }
                }
                if (tokenList.length == 0 || insertFlag) {
                    assignTeamSchedule.addToken(new sap.m.Token({ key: selectedItem.getKey(), text: selectedItem.getText() }));
                }
            }
        }
    }); 

    var woChkbox = new sap.m.CheckBox({text: oBundle.getText("Yes")});


	var scheduleForm1 = new sap.ui.layout.form.Form({
		editable: true,
		layout: new sap.ui.layout.form.ResponsiveGridLayout(),
		formContainers: [
			new sap.ui.layout.form.FormContainer({
				formElements: [
					new sap.ui.layout.form.FormElement({
							label: new sap.m.Label({required: true, text: oBundle.getText("Schedule_ID")}),
                            fields: [fieldSchedule1]
                    }),
                    new sap.ui.layout.form.FormElement({
							label: new sap.m.Label({required: true, text: oBundle.getText("Schedule_Title")}),
                            fields: [fieldSchedule2]
                    }),
                    new sap.ui.layout.form.FormElement({
							label: new sap.m.Label({text: " "}),
                            fields: []
                    }),
					new sap.ui.layout.form.FormElement({
							label: new sap.m.Label({required: true, text: oBundle.getText("License_Number")}),
                            fields: [fieldSchedule3]
                    }),
                    new sap.ui.layout.form.FormElement({
							label: new sap.m.Label({required: true, text: oBundle.getText("Model")}),
                            fields: [fieldSchedule20]
                    }),
                    new sap.ui.layout.form.FormElement({
							label: new sap.m.Label({required: true, text: oBundle.getText("Last_Service_Meter_Reading")}),
                            fields: [fieldSchedule4]
                    }),
                    new sap.ui.layout.form.FormElement({
							label: new sap.m.Label({required: true, text: oBundle.getText("Last_Service_Date")}),
                            fields: [fieldSchedule5]
                    }),
					]
				}),
			new sap.ui.layout.form.FormContainer({
               // title:"Task",
				formElements: [
                    new sap.ui.layout.form.FormElement({
						label: new sap.m.Label({text: oBundle.getText("Note")}),
						fields: [fieldScheduleNote]
					}),
                    new sap.ui.layout.form.FormElement({
						label: new sap.m.Label({required: true, text: oBundle.getText("Task")}),
						fields: [oSimpleListBox,
                            new sap.ui.layout.VerticalLayout({
                                content: [btnAddTask//,btnAddTaskMaster.addStyleClass("sapUiTinyMarginTop")
                           ]})
                            ]
					}),
				]
			}),

            new sap.ui.layout.form.FormContainer({
                //title:"Repeat Type",
				formElements: [
                    new sap.ui.layout.form.FormElement({
						label: new sap.m.Label({required: true, text: oBundle.getText("Repeat_Type")}),
						fields: [repeatTypeSB]
					}),
                    new sap.ui.layout.form.FormElement({
						label: new sap.m.Label({text: oBundle.getText("Schedule_Intervals")}),
						fields: [
                            new sap.ui.layout.HorizontalLayout({
                                    content: [repeatCB1,
                                    intervalMiles.addStyleClass("sapUiTinyMarginBeginEnd")
                               ]})
                        ]}),
                    new sap.ui.layout.form.FormElement({
						label: new sap.m.Label({ text: oBundle.getText("")}),
						fields: [
                            new sap.ui.layout.HorizontalLayout({
                                    content: [repeatCB2,fieldScheduleOnDate.addStyleClass("sapUiTinyMarginBeginEnd"),
                                    intervalDuration.addStyleClass("sapUiTinyMarginBeginEnd")
                               ]})
                        
                      ]}),
                    new sap.ui.layout.form.FormElement({
                    label: new sap.m.Label({ text: ""}),
                    fields: [
                        new sap.ui.layout.HorizontalLayout({
                                content: [intervalDurationUnitSB
                            ]})
                    
                    ]}),
			]}),

            new sap.ui.layout.form.FormContainer({
               // title:"Notify before",
				formElements: [
                    new sap.ui.layout.form.FormElement({
                        label: new sap.m.Label({ text: oBundle.getText("Notify_before")})
						//fields: [notifyMiles]
                    }),
                    new sap.ui.layout.form.FormElement({
                        label: new sap.m.Label({ text: ""}),
						fields: [notifyMiles]
                    }),
                    new sap.ui.layout.form.FormElement({
                        label: new sap.m.Label({ text: ""}),
                        fields: [
                            new sap.ui.layout.HorizontalLayout({
                                content: [
                                notifyDuration.addStyleClass("sapUiTinyMarginEnd"),
                            ]})
                       ]}),
                    new sap.ui.layout.form.FormElement({
                        label: new sap.m.Label({ text: ""}),
                        fields: [
                            new sap.ui.layout.HorizontalLayout({
                                content: [notifyDurationUnitSB
                            ]})
                       ]}),
				]
			}),
            new sap.ui.layout.form.FormContainer({
               // title:"Notify before",
				formElements: [
                    
                   /*  new sap.ui.layout.form.FormElement({
                        label: new sap.m.Label({ text: oBundle.getText("Assign_to") }),
                        fields: [assignTeamSchedule]
                    }),
                   new sap.ui.layout.form.FormElement({
                        label: new sap.m.Label({ text: oBundle.getText("Automatically_Generate_WO") }),
                        fields: [woChkbox]
                    }),
                    new sap.ui.layout.form.FormElement({
                        label:"",
                        fields: [new sap.m.Label({wrapping:true,text: oBundle.getText("Schedule_Automatic_Wo_title") })]
                    })*/
				]
			})
		]
	});

	var btnSchedule1 = new sap.m.Button({ visible: true, icon: "sap-icon://add",
	    text: oBundle.getText("Set_Schedule"),
	    press: function () {

	        var schedule_id = fieldSchedule1.getValue();
	        var schedule_title = fieldSchedule2.getValue();
	        var license_number = fieldSchedule3.getValue();
            var vehicle_model = fieldSchedule20.getValue();
	        var vehicle_meter_unit = fieldSchedule4.getDescription();
	        var last_service_reading = fieldSchedule4.getValue();
	        var last_service_date = fieldSchedule5.getValue();

            var noteValue = fieldScheduleNote.getValue();

	        if (validation(schedule_title, oBundle.getText("Title"))) { return; }
	        if (validation(license_number, oBundle.getText("License_Number"))) { return; }
	        if (validation(last_service_reading, oBundle.getText("Last_Service_Meter_Reading"))) { return; }
	        if (validation(last_service_date, oBundle.getText("Last_Service_Date"))) { return; }

            if (!fieldSchedule5.isValidValue()) {
			    sap.m.MessageBox.error(oBundle.getText("Invalid") + " " + oBundle.getText("Last_Service_Date"));
			    return;
		    }

	        var oLength = oSimpleListBox.getItems().length;
	        if (oLength < 1) {
	            sap.m.MessageBox.error(oBundle.getText("Add_Task"));
	            return;
	        }
	        var task_list = "";
	        for (var i = 0; i < oLength; i++) {
	            task_list = task_list + "^" + oSimpleListBox.getItems()[i].getText();
	        }
	        task_list = task_list.substring(1);

	        if (!repeatCB1.getSelected() && !repeatCB2.getSelected()) {
	            sap.m.MessageBox.error(oBundle.getText("Check_at_least_one_interval"));
	            return;
	        }

	        var repeat_type = repeatTypeSB.getSelectedKey();

	        var interval_type = "1";
	        if (repeatCB1.getSelected() && repeatCB2.getSelected()) {
	            interval_type = "3";
	        } else if (repeatCB2.getSelected()) {
	            interval_type = "2";
	        }

	        var interval_reading = intervalMiles.getValue();
	        var interval_duration = intervalDuration.getValue();
	        var duration_unit = intervalDurationUnitSB.getSelectedKey();

	        var on_date = fieldScheduleOnDate.getValue();

	        var notify_reading = notifyMiles.getValue();
	        var notify_duration = notifyDuration.getValue();
	        var notify_unit = notifyDurationUnitSB.getSelectedKey();

	        if (repeatCB1.getSelected()) {
	            if (validation(interval_reading, oBundle.getText("interval_reading"))) { return; }
	            if (validation(notify_reading, oBundle.getText("notify_reading"))) { return; }

	            if (parseFloat(notify_reading) > parseFloat(interval_reading)) {
	                sap.m.MessageBox.error(oBundle.getText("Notify_miles_must_be_less_than_interval"));
	                return;
	            }
	        }

	        if (repeatCB2.getSelected()) {
	            if (repeat_type == "1") {
	                if (validateNumber(interval_duration, oBundle.getText("interval_duration"))) { return; }
	            } else {
	                if (validation(on_date, oBundle.getText("on_date"))) { return; }
                    if (!fieldScheduleOnDate.isValidValue()) {
			            sap.m.MessageBox.error(oBundle.getText("Invalid") + " " + oBundle.getText("on_date"));
			            return;
		            }
	            }
	            if (validateNumber(notify_duration, oBundle.getText("notify_duration"))) { return; }
	        }


	        if (repeatCB2.getSelected()) {

	            var notify_days = parseInt(notify_duration);

	            if (notify_unit=="1") {
	                notify_days = parseInt(notify_duration);
	            } else if (notify_unit=="2") {
	                notify_days = parseInt(notify_duration) * 7;
	            } else if (notify_unit=="3") {
	                notify_days = parseInt(notify_duration) * 30;
	            }


	            if (repeat_type == "1") {

	                var interval_days = parseInt(interval_duration);
	                if (duration_unit=="1") {
	                    interval_days = parseInt(interval_duration);
	                } else if (duration_unit=="2") {
	                    interval_days = parseInt(interval_duration) * 7;
	                } else if (duration_unit=="3") {
	                    interval_days = parseInt(interval_duration) * 30;
	                } else if (duration_unit=="4") {
	                    interval_days = parseInt(interval_duration) * 365;
	                }

	                if (notify_days > interval_days) {
	                    sap.m.MessageBox.error(oBundle.getText("Notify_duration_must_be_less_than_interval"));
	                    return;
	                }

	            } else {

	                var date1 = new Date();
	                var date2 = new Date(on_date);

	                var res = Math.abs(date1 - date2) / 1000;
	                var daysDiff = Math.floor(res / 86400);

	                if (notify_days > daysDiff) {
	                    sap.m.MessageBox.error(oBundle.getText("Notify_duration_must_be_less_than_interval"));
	                    return;
	                }
	            }
	        }

            var woFlag = woChkbox.getSelected()?"1":"0";

            var team_id = "";
            var team_name = "";
           /* var teamTokenList = assignTeamSchedule.getTokens();
           
            for (var j = 0; j < teamTokenList.length; j++) {
                if (team_name == "") {
                    team_name = teamTokenList[j].getText();
                    team_id = teamTokenList[j].getKey();
                };
            };
            if(team_id == ""){
                sap.m.MessageBox.error(oBundle.getText("Select_Inspector"));return;
            }*/
    

	        var created_date = getIndonesianTime();
	        var created_by = operatedBy;

	        busyDialog.open();
	        Car_Schedule.insert({ master_email: emailUser, schedule_id: schedule_id, cycle_count: "1", schedule_title: schedule_title,
	            status: "1", created_date: created_date, created_by: created_by, due_soon_flag: "0", due_flag: "0",
	            license_number: license_number, vehicle_model:vehicle_model,vehicle_meter_unit: vehicle_meter_unit, last_service_reading: last_service_reading,
	            last_service_date: last_service_date, task_list: task_list,
	            repeat_type: repeat_type, interval_type: interval_type,
	            interval_reading: interval_reading, interval_duration: interval_duration, duration_unit: duration_unit, on_date: on_date,
	            notify_reading: notify_reading, notify_duration: notify_duration, notify_duration_unit: notify_unit,
                note:noteValue,location_id:hvi_location_id,team_id:team_id,team_name:team_name,wo_flag:woFlag
	        }).done(function () {
	            busyDialog.close();
	            getScheduleDetails();
                oDialog.close();

               /*  HVI_Index.where({ id: emailUser }).read().done(function (results) {
                    HVI_Index.update({ id: emailUser, schedule: (Number(results[0]['schedule']) + 1) }).done(function () { });
                });*/

	        }, failure); 
	    }
	});

    var btnSchedule2 = new sap.m.Button({visible:false, icon: "sap-icon://edit", 
        text: oBundle.getText("Update"),
        press: function () {

	        var schedule_id = fieldSchedule1.getValue();
	        var schedule_title = fieldSchedule2.getValue();
	        var license_number = fieldSchedule3.getValue();
	        var vehicle_name = fieldSchedule20.getValue();
	        var last_service_reading = fieldSchedule4.getValue();
	        var last_service_date = fieldSchedule5.getValue();

            var noteValue = fieldScheduleNote.getValue();

	        if (validation(schedule_title, oBundle.getText("Title"))) { return; }
	        if (validation(license_number, oBundle.getText("License_Number"))) { return; }
	        if (validation(last_service_reading, oBundle.getText("Last_Service_Meter_Reading"))) { return; }
	        if (validation(last_service_date, oBundle.getText("Last_Service_Date"))) { return; }

            if (!fieldSchedule5.isValidValue()) {
			    sap.m.MessageBox.error(oBundle.getText("Invalid") + " " + oBundle.getText("Last_Service_Date"));
			    return;
		    }

	        var oLength = oSimpleListBox.getItems().length;
	        if (oLength < 1) {
	            sap.m.MessageBox.error(oBundle.getText("Add_Task"));
	            return;
	        }
	        var task_list = "";
	        for (var i = 0; i < oLength; i++) {
	            task_list = task_list + "^" + oSimpleListBox.getItems()[i].getText();
	        }
	        task_list = task_list.substring(1);

	        if (!repeatCB1.getSelected() && !repeatCB2.getSelected()) {
	            sap.m.MessageBox.error(oBundle.getText("Check_at_least_one_interval"));
	            return;
	        }

	        var repeat_type = repeatTypeSB.getSelectedKey();

	        var interval_type = "1";
	        if (repeatCB1.getSelected() && repeatCB2.getSelected()) {
	            interval_type = "3";
	        } else if (repeatCB2.getSelected()) {
	            interval_type = "2";
	        }

	        var interval_reading = intervalMiles.getValue();
	        var interval_duration = intervalDuration.getValue();
	        var duration_unit = intervalDurationUnitSB.getSelectedKey();

	        var on_date = fieldScheduleOnDate.getValue();

	        var notify_reading = notifyMiles.getValue();
	        var notify_duration = notifyDuration.getValue();
	        var notify_unit = notifyDurationUnitSB.getSelectedKey();

	        if (repeatCB1.getSelected()) {
	            if (validation(interval_reading, oBundle.getText("interval_reading"))) { return; }
	            if (validation(notify_reading, oBundle.getText("notify_reading"))) { return; }

	            if (parseFloat(notify_reading) > parseFloat(interval_reading)) {
	                sap.m.MessageBox.error(oBundle.getText("Notify_miles_must_be_less_than_interval"));
	                return;
	            }
	        }

	        if (repeatCB2.getSelected()) {
	            if (repeat_type == "1") {
	                if (validateNumber(interval_duration, oBundle.getText("interval_duration"))) { return; }
	            } else {
	                if (validation(on_date, oBundle.getText("on_date"))) { return; }
                    if (!fieldScheduleOnDate.isValidValue()) {
			            sap.m.MessageBox.error(oBundle.getText("Invalid") + " " + oBundle.getText("on_date"));
			            return;
		            }
	            }
	            if (validateNumber(notify_duration, oBundle.getText("notify_duration"))) { return; }
	        }


	        if (repeatCB2.getSelected()) {
	            var notify_days = parseInt(notify_duration);
	            if (notify_unit=="1") {
	                notify_days = parseInt(notify_duration);
	            } else if (notify_unit=="2") {
	                notify_days = parseInt(notify_duration) * 7;
	            } else if (notify_unit=="3") {
	                notify_days = parseInt(notify_duration) * 30;
	            }

	            if (repeat_type == "1") {

	                var interval_days = parseInt(interval_duration);
	                if (duration_unit=="1") {
	                    interval_days = parseInt(interval_duration);
	                } else if (duration_unit=="2") {
	                    interval_days = parseInt(interval_duration) * 7;
	                } else if (duration_unit=="3") {
	                    interval_days = parseInt(interval_duration) * 30;
	                } else if (duration_unit=="4") {
	                    interval_days = parseInt(interval_duration) * 365;
	                }
	                if (notify_days > interval_days) {
	                    sap.m.MessageBox.error(oBundle.getText("Notify_duration_must_be_less_than_interval"));
	                    return;
	                }

	            } else {

	                var date1 = new Date();
	                var date2 = new Date(on_date);

	                var res = Math.abs(date1 - date2) / 1000;
	                var daysDiff = Math.floor(res / 86400);

	                if (notify_days > daysDiff) {
	                    sap.m.MessageBox.error(oBundle.getText("Notify_duration_must_be_less_than_interval"));
	                    return;
	                }
	            }
	        }

            // var woFlag = woChkbox.getSelected()?"1":"0";
            // var teamTokenList = assignTeamSchedule.getTokens();
            // var team_id = "";
            // var team_name = "";
            // for (var j = 0; j < teamTokenList.length; j++) {
            //     if (team_name == "") {
            //         team_name = teamTokenList[j].getText();
            //         team_id = teamTokenList[j].getKey();
            //     };
            // };
            // if(team_id == ""){
            //     sap.m.MessageBox.error(oBundle.getText("Select_Inspector"));return;
            // }
    

	        busyDialog.open();
	        Car_Schedule.update({id:obj['id'], schedule_title: schedule_title,task_list: task_list,
	            repeat_type: repeat_type, interval_type: interval_type,
	            interval_reading: interval_reading, interval_duration: interval_duration, duration_unit: duration_unit, on_date: on_date,
	            notify_reading: notify_reading, notify_duration: notify_duration, notify_duration_unit: notify_unit,
                note:noteValue,last_service_reading:last_service_reading
	        }).done(function () {
	            busyDialog.close();
	            getScheduleDetails();
                oDialog.close();
	        }, failure); 
	    }
        });
   

    var oDialog = new sap.m.Dialog({ contentWidth: "100%", contentHeight: "100%", draggable: true, 
     resizable: true,
        title: oBundle.getText("Add_Schedule"),
        content: [scheduleForm1],
        buttons: [btnSchedule1, btnSchedule2,
			new sap.m.Button({ text: oBundle.getText("Close"), icon: "sap-icon://decline",
			    press: function () {
			        oDialog.close();
			    }
			})
		]
    }).open();

    if(src==2){

        oDialog.setTitle(oBundle.getText("Update_Schedule"));
        btnSchedule1.setVisible(false);
        btnSchedule2.setVisible(true);

        fieldSchedule1.setValue(obj['schedule_id']);
        fieldSchedule2.setValue(obj['schedule_title']);
        fieldSchedule3.setValue(obj['license_number']);
        fieldSchedule20.setValue(obj['vehicle_model']);

        fieldSchedule4.setValue(obj['last_service_reading']);
        fieldSchedule5.setValue(obj['last_service_date']);

        fieldSchedule4.setDescription(obj['vehicle_meter_unit']);
        intervalMiles.setDescription(obj['vehicle_meter_unit']);
        notifyMiles.setDescription(obj['vehicle_meter_unit']);

        fieldScheduleNote.setValue(obj['note']);

        fieldSchedule3.setEnabled(false);
        fieldSchedule4.setEnabled(false);
        fieldSchedule5.setEnabled(false);
        
        if(obj['wo_flag']=="1"){
            woChkbox.setSelected(true); 
        }else{
            woChkbox.setSelected(false); 
        }
        if (obj['team_id'] != null && obj['team_name'] != "") {
            var team_id = obj['team_id'];//.split(",");
            var team_name = obj['team_name'];//.split(",");
            // for (var i = 0; i < assigned_participants.length; i++) {//
                assignTeamSchedule.addToken(new sap.m.Token({key:team_id, text: team_name }));
            // };
        };


        oSimpleListBox.removeAllItems();
        var woItems = obj['task_list'].split("^");

        if (woItems != "") {
            for (var i = 0; i < woItems.length; i++) {
                oSimpleListBox.addItem(new sap.ui.core.ListItem({ text: woItems[i] }));
            }
        }

        var repeat_type = obj['repeat_type'];
        repeatTypeSB.setSelectedKey(repeat_type);

        var interval_type = obj['interval_type'];

        if(interval_type=="1" || interval_type=="3"){
            repeatCB1.setSelected(true);
            intervalMiles.setValue(obj['interval_reading']);
            notifyMiles.setValue(obj['notify_reading']);
            intervalMiles.setEnabled(true);
            notifyMiles.setEnabled(true);
        }else{
             repeatCB1.setSelected(false);
             intervalMiles.setEnabled(false);
             notifyMiles.setEnabled(false);
        }

         if(interval_type=="2" || interval_type=="3"){
               repeatCB2.setSelected(true);
              if (repeat_type == "1") {
                  intervalDuration.setValue(obj['interval_duration']);
                  intervalDurationUnitSB.setSelectedKey(obj['duration_unit']);
               } else { 
                    fieldScheduleOnDate.setValue(obj['on_date']);
                    repeatCB1.setText(oBundle.getText("At"));
                    repeatCB2.setText(oBundle.getText("On"));
                    intervalDuration.setVisible(false);
                    intervalDurationUnitSB.setVisible(false);
                    fieldScheduleOnDate.setVisible(true);
               }

            notifyDuration.setValue(obj['notify_duration']);
            notifyDurationUnitSB.setSelectedKey(obj['notify_duration_unit']);

            intervalDuration.setEnabled(true);
            intervalDurationUnitSB.setEnabled(true);
            fieldScheduleOnDate.setEnabled(true);
            notifyDuration.setEnabled(true);
            notifyDurationUnitSB.setEnabled(true);
        }else{
            
            intervalDuration.setEnabled(false);
            intervalDurationUnitSB.setEnabled(false);
            fieldScheduleOnDate.setEnabled(false);
            notifyDuration.setEnabled(false);
            notifyDurationUnitSB.setEnabled(false);
        }

    }else if(src == 3){
       /* busyDialog.open();
        HVI_Index.where({ id: emailUser }).read().done(function (results) {

            var newSCHNo = Number(sessionStorage.getItem('sc_start_no')) + Number(results[0]['schedule']) + 1;
            fieldSchedule1.setValue(sessionStorage.getItem('sc_prefix') + newSCHNo);
            fieldSchedule2.focus();
            busyDialog.close();
        });*/
        
        var indexScheduel  = new Date().getTime();

        fieldSchedule1.setValue("SCH" + indexScheduel);

        btnSchedule1.setVisible(true);
        // btnSchedule2.setVisible(true);

        // fieldSchedule1.setValue(obj['schedule_id']);
        fieldSchedule2.setValue(obj['schedule_title']);
        fieldSchedule3.setValue(obj['license_number']);
        fieldSchedule20.setValue(obj['vehicle_model']);

        fieldSchedule4.setValue(obj['last_service_reading']);
        fieldSchedule5.setValue(obj['last_service_date']);

        fieldSchedule4.setDescription(obj['vehicle_meter_unit']);
        intervalMiles.setDescription(obj['vehicle_meter_unit']);
        notifyMiles.setDescription(obj['vehicle_meter_unit']);

        fieldScheduleNote.setValue(obj['note']);

        // fieldSchedule3.setEnabled(false);
        // fieldSchedule4.setEnabled(false);
        // fieldSchedule5.setEnabled(false);
        if(obj['wo_flag']=="1"){
                woChkbox.setSelected(true); 
        }else{
            woChkbox.setSelected(false); 
        }
        if (obj['team_id'] != null && obj['team_name'] != "") {
            var team_id = obj['team_id'];//.split(",");
            var team_name = obj['team_name'];//.split(",");
            // for (var i = 0; i < assigned_participants.length; i++) {//
                assignTeamSchedule.addToken(new sap.m.Token({key:team_id, text: team_name }));
            // };
        };


       oSimpleListBox.removeAllItems();
       var woItems = obj['task_list'].split("^");

       if (woItems != "") {
           for (var i = 0; i < woItems.length; i++) {
               oSimpleListBox.addItem(new sap.ui.core.ListItem({ text: woItems[i] }));
           }
       }

       var repeat_type = obj['repeat_type'];
       repeatTypeSB.setSelectedKey(repeat_type);

       var interval_type = obj['interval_type'];

       if(interval_type=="1" || interval_type=="3"){
           repeatCB1.setSelected(true);
           intervalMiles.setValue(obj['interval_reading']);
           notifyMiles.setValue(obj['notify_reading']);
           intervalMiles.setEnabled(true);
           notifyMiles.setEnabled(true);
       }else{
            repeatCB1.setSelected(false);
            intervalMiles.setEnabled(false);
            notifyMiles.setEnabled(false);
       }

        if(interval_type=="2" || interval_type=="3"){
              repeatCB2.setSelected(true);
             if (repeat_type == "1") {
                 intervalDuration.setValue(obj['interval_duration']);
                 intervalDurationUnitSB.setSelectedKey(obj['duration_unit']);
              } else { 
                   fieldScheduleOnDate.setValue(obj['on_date']);
                   repeatCB1.setText(oBundle.getText("At"));
                   repeatCB2.setText(oBundle.getText("On"));
                   intervalDuration.setVisible(false);
                   intervalDurationUnitSB.setVisible(false);
                   fieldScheduleOnDate.setVisible(true);
              }

           notifyDuration.setValue(obj['notify_duration']);
           notifyDurationUnitSB.setSelectedKey(obj['notify_duration_unit']);

           intervalDuration.setEnabled(true);
           intervalDurationUnitSB.setEnabled(true);
           fieldScheduleOnDate.setEnabled(true);
           notifyDuration.setEnabled(true);
           notifyDurationUnitSB.setEnabled(true);
       }else{
           
           intervalDuration.setEnabled(false);
           intervalDurationUnitSB.setEnabled(false);
           fieldScheduleOnDate.setEnabled(false);
           notifyDuration.setEnabled(false);
           notifyDurationUnitSB.setEnabled(false);
       }

    }else{
        
      /*  busyDialog.open();
        HVI_Index.where({ id: emailUser }).read().done(function (results) {

            var newSCHNo = Number(sessionStorage.getItem('sc_start_no')) + Number(results[0]['schedule']) + 1;
            fieldSchedule1.setValue(sessionStorage.getItem('sc_prefix') + newSCHNo);
            fieldSchedule2.focus();
            busyDialog.close();
        });*/

        var indexScheduel  = new Date().getTime();

        fieldSchedule1.setValue("SCH" + indexScheduel);

    }

}
 
function addScheduleTaskItemDialog(oSimpleListBox){
    
    var lvl1 = new sap.m.Label({ text: oBundle.getText("Item_Name")});
    var field1 = new sap.m.Input({ enabled:true, value: "", width: "100%" });
           
 var oDialogItemSelect = new sap.m.Dialog({
        modal : true,
        title: oBundle.getText("Add_Task"),
        state: sap.ui.core.ValueState.Success,
        content: [lvl1,field1],
        buttons: [  new sap.m.Button({text: oBundle.getText("Add"),
					    icon: "sap-icon://add",
					    press: function () {
					        var itemV = field1.getValue();
                            if (itemV == "") {
                               sap.m.MessageBox.error(oBundle.getText("Item_is_empty"));
                                return;
                            }
                           oSimpleListBox.addItem(new sap.ui.core.ListItem({ text: itemV }));
                           oDialogItemSelect.close();
					    }
					}),
					new sap.m.Button({text: oBundle.getText("Cancel"),
					    icon: "sap-icon://decline",
					    press: function () {
					        oDialogItemSelect.close();
					    }
					})
				]
    }).open();

}

function selectVehicleDialog(fieldSchedule20,input,fieldSchedule4) {

    var oSelectDialog = new sap.m.SelectDialog({ contentWidth: "50%", draggable: true,
        resizable: true, //rememberSelections: true, multiSelect: true,
        title: oBundle.getText("Select_Vehicle"),
        noDataText: oBundle.getText("No_Vehicle"),
        liveChange: function (oEvent) {
            sSearchValue = oEvent.getParameter("value"),
            oBinding = oEvent.getParameter("itemsBinding");
            if (oBinding) {
                var oFilters = [
                    new sap.ui.model.Filter("license_number", sap.ui.model.FilterOperator.Contains, sSearchValue),
                    new sap.ui.model.Filter("model", sap.ui.model.FilterOperator.Contains, sSearchValue),
                    new sap.ui.model.Filter("make", sap.ui.model.FilterOperator.Contains, sSearchValue)
                ];
                var filterObj = new sap.ui.model.Filter(oFilters, false);
                oBinding.filter(filterObj);
            } else {
                oBinding.filter([]);
            }
        },
        confirm: function (oEvent) {
            var selectedItem = oEvent.getParameter("selectedItem");

            input.setValue(selectedItem.getTitle());
            fieldSchedule20.setValue(selectedItem.getDescription());

            var getVehicle = vehicle4WJSONData.filter(function(obj, index){
                    return (obj.license_number === selectedItem.getTitle());
                });
           
            fieldSchedule4.setValue(getVehicle[0]['current_odometer']);
            
        }
    });

	var oItemTemplate = new sap.m.StandardListItem({
		title: "{license_number}",
		description: "{model}",
        info:"{make}",
		type: "Active"
	})

    var oModel = new sap.ui.model.json.JSONModel();
    oModel.setData({ modelData: vehicle4WJSONData });
    oSelectDialog.setModel(oModel); 
    oSelectDialog.bindAggregation("items", "/modelData", oItemTemplate);
    oSelectDialog.open();    
    oSelectDialog._getCancelButton().setText(oBundle.getText("Cancel"));
}