$dtval = jQuery.noConflict();

function doCheckBackSpaceOrDelete() {
    var keyCode = (event.which) ? event.which : event.keyCode;
    if (keyCode == 8 || keyCode == 46)
        event.returnValue = false;
}

var Alert_PastDate = "From/To date cannot be past date.";
var Alert_FromTo = "Please select To date greater than From date..!";
var Alert_ValidDate = "Please select a valid date range !";

function setAlert(ValidDate) {
    Alert_ValidDate = ValidDate;
}

//var Alert_Delete = "Do you want to delete this.";

//function setAlertMessageDelete(Delete) {
//    Alert_Delete = Delete;
//}

////Delete confirm
//function DeleteConfirm() {
//    return confirm(Alert_Delete);
//}

function checkDate(sender, args) {
    try {
        var alertMessage = "";
        var isError = false;
        var fromDate = new Date($find("ceStartDate")._selectedDate);
        var toDate = new Date($find("ceEndDate")._selectedDate);
        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);
        toDate.setHours(toHours);
        if (sender._selectedDate > new Date()) {
            isError = true;
            alertMessage = Alert_ValidDate;
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
        //		    CheckStartAndEnd();
        //		    if ($find("ceStartDate")._selectedDate > $find("ceEndDate")._selectedDate) {
        //		        alert("Please select a valid date range !");
        //		        return false;
        //		    }
        //		    if (sender._id == "ceEndDate") {
        //if (!) {
        //		            sender._selectedDate = new Date();
        //		            // set the date back to the current date
        //		            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        //		        }
        //		    }
    }
    catch (Error) {
        return false;
    }
}


function setAlertMessageRoute(PastDate, FromTo, ValidDate) {
    Alert_PastDate = PastDate;
    Alert_FromTo = FromTo;
    Alert_ValidDate = ValidDate;
}
//Included By vijay - Date Validation for Route management.
function checkDateRoute(sender) {
    try {
        var alertMessage = "";
        var isError = false;
        var fromDate = new Date($find("calFromdate")._selectedDate);
        var toDate = new Date($find("calTodate")._selectedDate);
        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);
        var fromMin = eval(document.getElementById("ddlFromMin").value);
        var toMin = eval(document.getElementById("ddlToMin").value);
        fromDate.setHours(fromHours)
        fromDate.setMinutes(fromMin, 0, 0);
        toDate.setHours(toHours)
        toDate.setMinutes(toMin, 0, 0);

        if (fromDate < new Date() && document.getElementById("txtToDate").value == '') {
            isError = true;
            document.getElementById("txtFromDate").value = ''
            TrinetraCustomAlerts(Alert_PastDate);
            return false
        }
        else if (toDate < new Date() && document.getElementById("txtFromDate").value == '') {
            isError = true;
            document.getElementById("txtToDate").value = '';
            TrinetraCustomAlerts(Alert_PastDate);
            return false;
        }


        if (sender._selectedDate >= new Date().getDateOnly()) {

            if (document.getElementById("txtFromDate").value != '' && document.getElementById("txtToDate").value != '') {
                if (fromDate < new Date() || toDate < new Date()) {
                    isError = true;
                    alertMessage = Alert_ValidDate;
                    sender._selectedDate = new Date();
                    // Clear the value
                    document.getElementById("txtFromDate").value = '';
                    document.getElementById("txtToDate").value = '';
                }
                else if (new Date(fromDate) >= new Date(toDate)) {
                    isError = true;
                    alertMessage = Alert_FromTo;
                    sender._selectedDate = new Date();
                    // Clear the value
                    sender._textbox.set_Value('');
                }
            }
        }
        else {
            isError = true;
            alertMessage = Alert_PastDate;
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value('');
            //alert(alertMessage);
        }

        if (alertMessage != '' || alertMessage.length != 0) {
            TrinetraCustomAlerts(alertMessage);
            alertMessage = '';
            return false;
        }
        else {
            return true;
        }
    }
    catch (Error) {
        return false;
    }
}

// Included by Kumuthini. L on 04-Mar-10
function checkDateLessThanCurrentDate(sender, args) {
    try {
        var alertMessage = "";
        var isError = false;
        var effectiveFromDate = new Date($find("ceEffectiveFrom")._selectedDate);
        var effectiveToDate = new Date($find("ceEffectiveTo")._selectedDate);
        var frequencyOption = document.getElementById('ddlFrequency').value;
        if (sender._selectedDate < new Date() && frequencyOption != 2) {
            isError = true;
            alertMessage = "Please select a valid date range !";
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
    }
    catch (Error) {
        return false;
    }
}

function checkRescheduleDueOnDate(dueOnDate) {
    try {
        var alertMessage = "valid";
        var isError = false;
        var dueOn = new Date(dueOnDate.value);

        if (dueOn <= new Date()) {
            isError = true;
            alertMessage = "Please select a valid date range !";
            dueOnDate.value = "";
        }

        return alertMessage;
    }
    catch (Error) {
        return "error";
    }
}

function checkDateGreaterThanCurrentDate(sender, args, startDate) {
    try {
        var alertMessage = "";
        var isError = false;
        var date = new Date();
        var startServiceDate = new Date(startDate);

        // Subtract 1 month from the current date
        date.setMonth(date.getMonth() - 1);

        if (sender._selectedDate > new Date() || sender._selectedDate < date || (startDate != '' && sender._selectedDate < startServiceDate)) {
            isError = true;
            alertMessage = "Please select a valid date range !";
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }

        return alertMessage;
    }
    catch (Error) {
        return "Error";
    }
}

function checkStartEndDate(sender, type) {
    try {
        var isError = false;
        var fromDate = new Date($find("ceStartDate")._selectedDate);
        var toDate = new Date($find("ceEndDate")._selectedDate);

        if (sender._selectedDate > new Date() && (type == 0 || type == 1)) {
            isError = true;
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }

        if (type == 2 && fromDate > toDate) {
            isError = true;
        }

        return isError;
    }
    catch (Error) {
        return false;
    }
}

// Included by Kumuthini. L on 04-Mar-10
function CheckEffectiveFromTo() {
    try {
        var fromDate = new Date($find("ceEffectiveFrom")._selectedDate);
        var toDate = new Date($find("ceEffectiveTo")._selectedDate);
        fromDate = new Date(fromDate.getYear(), fromDate.getMonth(), fromDate.getDate());
        toDate = new Date(toDate.getYear(), toDate.getMonth(), toDate.getDate());
        var differenceBetweenDate = (toDate.getTime() - fromDate.getTime()) / (86400 * 1000);
        var allowedDifference = eval(document.getElementById("ddlFrequency").value) == 0 ? 0 :
		        eval(document.getElementById("ddlFrequency").value) == 1 ? 2 : 8;
        eval(document.getElementById("ddlFrequency").value)
        if (fromDate > toDate) {
            return 1;
        }
        else if (document.getElementById("ddlFrequency").value == "1" ||
		         document.getElementById("ddlFrequency").value == "2") {
            if (differenceBetweenDate < allowedDifference) {
                return 3;
            }
        }
        else if (document.getElementById("ddlFrequency").value == "0" && fromDate.toDateString() == toDate.toDateString()) {
            var fromHours = (document.getElementById("ddlReportFromAMPM").value == "AM"
		                || eval(document.getElementById("ddlReportFromHrs").value) == "12" ?
		             eval(document.getElementById("ddlReportFromHrs").value) :
		             eval(document.getElementById("ddlReportFromHrs").value) + 12);
            var toHours = (document.getElementById("ddlReportToAMPM").value == "AM"
		                || eval(document.getElementById("ddlReportToHrs").value) == "12" ?
		                 eval(document.getElementById("ddlReportToHrs").value) :
		                 eval(document.getElementById("ddlReportToHrs").value) + 12);

            if (fromHours > toHours) {
                isError = true;
                alertMessage = "Please select a valid time range !";
                return 2;
            }
        }
        //		        else if (fromDate.toDateString() == toDate.toDateString()) {
        //		            if (document.getElementById("rbtWeekly").checked == true || document.getElementById("rbtMonthly").checked == true) {
        //		                alert("The opted Frequency does not suit the selected date range !");
        //		                return false;    
        //		            }
        //		        }

        return 0;
    }
    catch (Error) {
        return 0;
    }
}

function CheckStartAndEnd() {
    try {
        var fromDate = $dtval('input[id$=txtStartDate]').datepicker('getDate');
        var toDate = $dtval('input[id$=txtEndDate]').datepicker('getDate');
        var fromHours = (document.getElementById("ddlFromAMPM").value == "AM"
		        || 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) == "12" ?
		     eval(document.getElementById("ddlToHours").value) :
		     eval(document.getElementById("ddlToHours").value) + 12);
        fromDate.setHours(fromHours);
        toDate.setHours(toHours);

        if (fromDate > toDate) {
            TrinetraCustomAlerts("Please select a valid date range !");
            return false;
        }
        return true;
    }
    catch (Error) {
        return false;
    }
}
/************* Report Validations ***************/
function CheckAll() {
    var i = 0;
    var elementID;
    var checkAllCheckBox
    checkAllCheckBox = document.getElementById("chkSelectAll")
    elementID = "cblViolationType_";
    var assingedCheckbox = document.getElementById(elementID + i);
    while (assingedCheckbox != null) {
        assingedCheckbox.checked = checkAllCheckBox.checked;
        i++;
        assingedCheckbox = document.getElementById(elementID + i);
    }
}

function CheckSelectOrUnSelect() {
    var checkedItems = 0;
    var i = 0;
    var elementID;
    var checkAllCheckBox
    var assingedCheckbox;
    checkAllCheckBox = document.getElementById("chkSelectAll")
    elementID = "cblViolationType_";
    assingedCheckbox = document.getElementById(elementID + i);
    while (assingedCheckbox != null) {
        if (assingedCheckbox.checked) {
            checkedItems++;
        }
        i++;
        assingedCheckbox = document.getElementById(elementID + i);
    }
    if (checkedItems == i) {
        checkAllCheckBox.checked = true;
    }
    else {
        checkAllCheckBox.checked = false;
    }
}

function CheckShowOption() {
    var summaryCheckbox = document.getElementById('chkShowSummary');
    var detailCheckbox = document.getElementById('chkShowDetail');
    if (summaryCheckbox != null && detailCheckbox != null) {
        if (!summaryCheckbox.checked && !detailCheckbox.checked) {
            if (summaryCheckbox.parentNode.className == 'ControlHide') {
                TrinetraCustomAlerts('Cannot export the report without Details !');
            }
            else {
                TrinetraCustomAlerts('Either Summary or Details or both has to be selected to export the report!');
            }
            return false;
        }
    }
    return true;
}
/******************************************************/
