var Alert_FromDate = "Please select the From Date.";
var Alert_ToDate = "Please select the To Date.";
var Alert_RouteName = "Please select the Route Name";
var Alert_validDate = "Please select a valid date range !";
var Alert_VehicleSch = "Please select vehicle to be scheduled.";
var Alert_DriverSch = "Please select driver to be scheduled.";
var Alert_OneVehicle = "You can schedule only one vehicle at a time";
var Alert_AlreadyScheduled = "Some of the Vehicles/Drivers have already been scheduled in the selected duration. Do you want to overwrite?";
var Alert_AlreadyScheduleVehicle = "Some of the Vehicles have already been scheduled in the selected duration. Do you want to overwrite?";
var Alert_AlreadyScheduleDriver = "Some of the Drivers have already been scheduled in the selected duration. Do you want to overwrite?";


function setAlertMessage(FromDate, ToDate, RouteName, validDate, VehicleSch, DriverSch, OneVehicle, AlreadyScheduled, AlreadyScheduleVehicle, AlreadyScheduleDriver) {
    Alert_FromDate = FromDate;
    Alert_ToDate = ToDate;
    Alert_RouteName = RouteName;
    Alert_validDate = validDate;
    Alert_VehicleSch = VehicleSch;
    Alert_DriverSch = DriverSch
    Alert_OneVehicle = OneVehicle;
    Alert_AlreadyScheduled = AlreadyScheduled;
    Alert_AlreadyScheduleVehicle = AlreadyScheduleVehicle;
    Alert_AlreadyScheduleDriver = AlreadyScheduleDriver;
}


function Validate(type) {
    if (document.getElementById('txtFromDate') != null && document.getElementById('txtFromDate').value == '') {
        alert(Alert_FromDate);
        document.getElementById('txtFromDate').focus();
        return false;
    }
    else if (document.getElementById('txtToDate') != null && document.getElementById('txtToDate').value == '') {
        alert(Alert_ToDate);
        document.getElementById('txtToDate').focus();
        return false;
    }
    else if (document.getElementById('ddlRouteName') != null && document.getElementById('ddlRouteName').value == '- Select -') {
        alert(Alert_RouteName);
        document.getElementById('ddlRouteName').focus();
        return false;
    }
    else if (document.getElementById('rdnMonth') != null && document.getElementById('rdnMonth').checked == true) {
        if (document.getElementById('txtFrequencyMonthlyDay') != null && document.getElementById('txtFrequencyMonthlyDay').value == '') {
        alert("Please enter the date for monthly schedule.");
        document.getElementById('txtFrequencyMonthlyDay').focus();
        return false;
    }
    }
    else if (document.getElementById('rdnWeekly') != null && document.getElementById('rdnWeekly').checked == true) {
     if (document.getElementById('hdnRecurrenceDaysDelay') != null && document.getElementById('hdnRecurrenceDaysDelay').value == '') {
        alert("Please select the week days for weekly schedule.");
        document.getElementById('rdnWeekly').focus();
        return false;
    }
    }
    var fromDate = new Date($find("calFromdate")._selectedDate);
    var toDate = new Date($find("calTodate")._selectedDate);
    var fromMin = eval(document.getElementById("ddlFromMin").value);
    var toMin = eval(document.getElementById("ddlToMin").value);
    var fromHours = (document.getElementById("ddlFromAMPM").value == "AM" ?
                 eval(document.getElementById("ddlFromHours").value) :
                 document.getElementById("ddlFromAMPM").value == "PM"
                 && eval(document.getElementById("ddlFromHours").value) == "12" ?
                 eval(document.getElementById("ddlFromHours").value) :
                 eval(document.getElementById("ddlFromHours").value) + 12);
    var toHours = (document.getElementById("ddlToAMPM").value == "AM" ?
                 eval(document.getElementById("ddlToHours").value) :
                 eval(document.getElementById("ddlToHours").value) + 12);
    fromDate.setHours(fromHours);
    fromDate.setMinutes(fromMin, 0, 0);
    toDate.setHours(toHours);
    toDate.setMinutes(toMin, 0, 0);
    var alertMessage = '';

    if (fromDate < new Date() || toDate < new Date()) {
        isError = true;
        alertMessage = Alert_validDate;
        if (fromDate < new Date()) {
            document.getElementById("txtFromDate").value = '';
            document.getElementById('txtFromDate').focus();
        }
        else if (toDate < new Date()) {
            document.getElementById("txtToDate").value = ''
            document.getElementById('txtToDate').focus();
        }
    }
    else if (new Date(fromDate) >= new Date(toDate)) {
        isError = true;
        alertMessage = Alert_validDate;
        //document.getElementById("txtFromDate").value = new Date();
        //document.getElementById("txtToDate").value = new Date();
    }
    if (alertMessage != '' || alertMessage.length != 0) {
        alert(alertMessage);
        alertMessage = '';
        return false;
    }

    // Included by vijay - to check whether the more than one vehicle is selected for one schedule.
    if (type == 1) {
        if (!CheckIsSelected(ddlVehicle.value)) {
            alert(Alert_VehicleSch);
            return false;
        }
        //if (document.getElementById("hdnSelectedVehiclesList").value != null) {
        //    if (document.getElementById("hdnSelectedVehiclesList").value == '') {
        //        alert(Alert_VehicleSch);
        //        return false;
        //    }
        //}

        //                    if (document.getElementById("hdnSelectedDriverList").value != null) {
        //                        if (document.getElementById("hdnSelectedDriverList").value == '') {
        //                            alert(Alert_DriverSch);
        //                            return false;
        //                        }
        //                    }

        //                    if (document.getElementById("hdnSelectedVehiclesList").value != null && document.getElementById("hdnSelectedVehiclesList").value != '') {
        //                        var hdn = document.getElementById("hdnSelectedVehiclesList").value;
        //                        var hdnValue = hdn.substring(0, hdn.lastIndexOf(","));
        //                        if (hdnValue.split(',').length > 1) {
        //                            alert(Alert_OneVehicle);
        //                            return false;
        //                        }
        //                    }
    }

    //if (type == 1 && (document.getElementById('hdnAssignedVehicles') != null && document.getElementById('hdnAssignedVehicles').value != '')
    //    || (document.getElementById('hdnAssignedDrivers') != null && document.getElementById('hdnAssignedDrivers').value != '')) {

    //    var assignedVehiclesChecked = 0;
    //    var assignedDriversChecked = 0;

    //    if (document.getElementById('hdnAssignedVehicles').value != '' && document.getElementById('hdnSelectedVehiclesList').value != '') {
    //        var AssVehArr = document.getElementById('hdnAssignedVehicles').value.split(",");

    //        var i = 0;
    //        for (i = 0; i < AssVehArr.length; i++) {
    //            if ((document.getElementById('hdnSelectedVehiclesList').value).match(AssVehArr[i])) {
    //                assignedVehiclesChecked = 1;
    //            }
    //        }
    //    }

    //    if (document.getElementById('hdnAssignedDrivers').value != '' && document.getElementById('hdnSelectedDriverList').value != '') {
    //        var AssDriversArr = document.getElementById('hdnAssignedDrivers').value.split(",");

    //        var i = 0;
    //        for (i = 0; i < AssDriversArr.length; i++) {
    //            if ((document.getElementById('hdnSelectedDriverList').value).match(AssDriversArr[i])) {
    //                assignedDriversChecked = 1;
    //            }
    //        }
    //    }

    //    if (assignedVehiclesChecked == 1 && assignedDriversChecked == 1) {
    //        return confirm(Alert_AlreadyScheduled);
    //    }
    //    else if (assignedVehiclesChecked == 1) {
    //        return confirm(Alert_AlreadyScheduleVehicle);
    //    }
    //    else if (assignedDriversChecked == 1) {
    //        return confirm(Alert_AlreadyScheduleDriver);
    //    }
    //    else {
    //    }
    //}
    return true;
}


function FrequencyChange() {
    var isIE = CheckBrowser();
    if (document.getElementById('ddlFrequency') != null) {
        if (document.getElementById('ddlFrequency').value == '1') {
            document.getElementById('pnlFrequencyWeekly').style.display = "block";
            document.getElementById('pnlFrequencyMonthly').style.display = 'block';
        }
        else if (document.getElementById('ddlFrequency').value == '2') {
            document.getElementById('pnlFrequencyWeekly').style.display = 'block';
            document.getElementById('pnlFrequencyMonthly').style.display = "block"; Freq();
        }
        else {
            document.getElementById('pnlFrequencyWeekly').style.display = 'none';
            document.getElementById('pnlFrequencyMonthly').style.display = 'none';
        }
    }
}

function selectAllDelay(object) {
    var checkboxCollection = document.getElementById('chkRecurringDelay').getElementsByTagName('input');
    for (var i = 0; i < checkboxCollection.length; i++) {
        if (checkboxCollection[i].type.toString().toLowerCase() == "checkbox") {
            checkboxCollection[i].checked = object;
        }
    }
    document.getElementById("hdnRecurrenceDaysDelay").value = object == true ? "1,2,3,4,5,6,7" : "";
}

function CheckItemDelay(checkBoxList) {
    var options = checkBoxList.getElementsByTagName('input');
    var arrayOfCheckBoxLabels = checkBoxList.getElementsByTagName("label");
    var s = "";
    var len = '';
    for (i = 0; i < options.length; i++) {
        var opt = options[i];
        if (opt.checked) {

            var value = arrayOfCheckBoxLabels[i].innerHTML;  //innerText;
            var chkvalue = value == "Sun" ? "1" : value == "Mon" ? "2" : value == "Tue" ? "3" : value == "Wed" ? "4" : value == "Thu" ? "5" : value == "Fri" ? "6" : value == "Sat" ? "7" : "";
            s = s + "," + chkvalue;
        }
    }
    if (s.length > 0) {
        s = s.substring(1, s.length); //sacar la primer 'coma'
        document.getElementById("hdnRecurrenceDaysDelay").value = s;
    }
    len = s.split(',');

    //if (len.length == 7) {
    //    document.getElementById('chkSelectAllDelay').checked = true;
    //}
    //else {
    //    document.getElementById('chkSelectAllDelay').checked = false;
    //}
}
