var noOfReportsAlert = 'Check the value for no of reports !';
var odometerReading = 'Check the value for odometer reading !';
var radiusMessage = 'Check the geo fence radius !';
var geozoneMessage = 'Select a geozone that is to be assigned !';
var noGeozoneMessage = 'No geozone that is to be assigned to the selected vehicle !'

function SetMessages(reportAlert, odometerAlert, radiusAlert, geozoneAlert, noGeozoneAlert) {
    noOfReportsAlert = reportAlert;
    odometerReading = odometerAlert;
    radiusMessage = radiusAlert;
    geozoneMessage = geozoneAlert;
    noGeozoneMessage = noGeozoneAlert;
}

function CheckAll(type) {
    var i = 0;
    var elementID;
    var checkAllCheckBox

    checkAllCheckBox = (type == "Geozones") ? document.getElementById("chkSelectAll") : document.getElementById("chkGenSelectAll");
    elementID = (type == "Geozones") ? "cblVehicleGeozones_" : "chkGenParams_";

    var assingedCheckbox = document.getElementById(elementID + i);
    while (assingedCheckbox != null) {
        assingedCheckbox.checked = checkAllCheckBox.checked;
        i++;
        assingedCheckbox = document.getElementById(elementID + i);
    }
}

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

function getDropDownListvalue() {
    var IndexValue = ddlCommands.GetSelectedIndex();  //document.getElementById('ddlCommands').selectedIndex;
    var controlToBefocused;
    if (IndexValue == -1) {
        return false;
    }
    var SelectedVal = ddlCommands.GetValueString(); // document.getElementById('ddlCommands').value;
    var canProcess = 1;
    var NoofReports;
    var errorMessage;
    if (SelectedVal == 'r') {
        NoofReports = document.getElementById('txtOdometer').value;
        if (isNaN(NoofReports) || NoofReports.length == 0) {
            canProcess = 0;
            errorMessage = 'Check the value for no of reports !';
            controlToBefocused = document.getElementById('txtOdometer')
        }
        else {
            NoofReports = document.getElementById('txtOdometer').value;
            if (eval(NoofReports) < 1 || eval(NoofReports) > 100) {
                canProcess = 0;
                errorMessage = noOfReportsAlert;
                controlToBefocused = document.getElementById('txtOdometer')
            }
        }
    }
    //else if (SelectedVal == 'K') {
    //    NoofReports = document.getElementById('txtOdometer').value;
    //    if (isNaN(NoofReports) || NoofReports.length == 0) {
    //        canProcess = 0;
    //        errorMessage = odometerReading;
    //        controlToBefocused = document.getElementById('txtOdometer');
    //    }
    //    else {
    //        if (eval(NoofReports) < 1 || eval(NoofReports) > 16777215) {
    //            canProcess = 0;
    //            errorMessage = odometerReading;
    //            controlToBefocused = document.getElementById('txtOdometer')
    //        }
    //    }
    //}
    else if (SelectedVal == 'F') {
        NoofReports = document.getElementById('txtOdometer').value;
        if (isNaN(NoofReports) || NoofReports.length == 0) {
            canProcess = 0;
            errorMessage = radiusMessage;
            controlToBefocused = document.getElementById('txtOdometer')
        }
        else {
            if (eval(NoofReports) == 0 || (eval(NoofReports) < 100 || eval(NoofReports) > 65500)) {
                canProcess = 0;
                errorMessage = radiusMessage
                controlToBefocused = document.getElementById('txtOdometer')
            }
        }
    }
    else if (SelectedVal == 'Z') {
        if (ddlGeozone.GetItemCount() > 0) {
            NoofReports = ddlGeozone.GetSelectedIndex();
            if (NoofReports == -1) {
                canProcess = 0;
                errorMessage = geozoneMessage;
                controlToBefocused = ddlGeozone;
            }
        }
        else {
            canProcess = 0;
            errorMessage = noGeozoneMessage;
            controlToBefocused = ddlGeozone;
        }
    }
    else if (SelectedVal == 'V') {
        var chkTemp1 = document.getElementById('chkTempSensor');
        var chkTemp2 = document.getElementById('chkTempSensor2');
        var chkFuel = document.getElementById('chkFuelSensor');
        var selectedSensors = 0;

        selectedSensors = selectedSensors + (chkTemp1 == null ? 0 : (chkTemp1.checked ? 1 : 0));
        selectedSensors = selectedSensors + (chkTemp2 == null ? 0 : (chkTemp2.checked ? 1 : 0));
        selectedSensors = selectedSensors + (chkFuel == null ? 0 : (chkFuel.checked ? 1 : 0));

        if (selectedSensors == 0) {
            canProcess = 0;
            errorMessage = "Please select any of the External Sensors.";
        }
        else {
            if (chkTemp1 != null && chkTemp1.checked) {
                var txtLowTempValue = document.getElementById('txtLowTempValue');
                var txtHighTempValue = document.getElementById('txtHighTempValue');

                if (RemoveSpaces(txtLowTempValue.value).length == 0) {
                    canProcess = 0;
                    errorMessage = "Please enter the Temperature-1 Low Value.";
                    controlToBefocused = txtLowTempValue;
                }
                else {
                    if (Number(txtLowTempValue.value) < -55 || Number(txtLowTempValue.value) > 125) {
                        errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Temperature-1 Low Value should be between -55°C and +125°C.';
                        controlToBefocused = canProcess != 0 ? txtLowTempValue : controlToBefocused;
                        canProcess = 0;
                    }
                }
                if (RemoveSpaces(txtHighTempValue.value).length == 0) {
                    errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter the Temperature-1 High Value.';
                    controlToBefocused = canProcess != 0 ? txtHighTempValue : controlToBefocused;
                    canProcess = 0;
                }
                else {
                    if (Number(txtHighTempValue.value) < -55 || Number(txtHighTempValue.value) > 125) {
                        errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Temperature-1 High Value should be between -55°C and +125°C.';
                        controlToBefocused = canProcess != 0 ? txtHighTempValue : controlToBefocused;
                        canProcess = 0;
                    }
                }
                if (RemoveSpaces(txtLowTempValue.value).length != 0 && RemoveSpaces(txtHighTempValue.value).length != 0) {
                    if (Number(txtLowTempValue.value) >= Number(txtHighTempValue.value)) {
                        errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Temperature-1 Low Value should be lesser than High Value.';
                        controlToBefocused = canProcess != 0 ? txtLowTempValue : controlToBefocused;
                        canProcess = 0;
                    }
                }
            }
            if (chkTemp2 != null && chkTemp2.checked) {
                var txtLowTempValue2 = document.getElementById('txtLowTempValue2');
                var txtHighTempValue2 = document.getElementById('txtHighTempValue2');

                if (RemoveSpaces(txtLowTempValue2.value).length == 0) {
                    errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter the Temperature-2 Low Value.';
                    controlToBefocused = canProcess != 0 ? txtLowTempValue2 : controlToBefocused;
                    canProcess = 0;
                }
                else {
                    if (Number(txtLowTempValue2.value) < -55 || Number(txtLowTempValue2.value) > 125) {
                        errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Temperature-2 Low Value should be between -55°C and +125°C.';
                        controlToBefocused = canProcess != 0 ? txtLowTempValue2 : controlToBefocused;
                        canProcess = 0;
                    }
                }
                if (RemoveSpaces(txtHighTempValue2.value).length == 0) {
                    errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter the Temperature-2 High Value.';
                    controlToBefocused = canProcess != 0 ? txtHighTempValue2 : controlToBefocused;
                    canProcess = 0;
                }
                else {
                    if (Number(txtHighTempValue2.value) < -55 || Number(txtHighTempValue2.value) > 125) {
                        errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Temperature-2 High Value should be between -55°C and +125°C.';
                        controlToBefocused = canProcess != 0 ? txtHighTempValue2 : controlToBefocused;
                        canProcess = 0;
                    }
                }
                if (RemoveSpaces(txtLowTempValue2.value).length != 0 && RemoveSpaces(txtHighTempValue2.value).length != 0) {
                    if (Number(txtLowTempValue2.value) >= Number(txtHighTempValue2.value)) {
                        errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Temperature-2 Low Value should be lesser than High Value.';
                        controlToBefocused = canProcess != 0 ? txtLowTempValue2 : controlToBefocused;
                        canProcess = 0;
                    }
                }
            }
            if (chkFuel != null && chkFuel.checked) {
                var txtLowFuelValue = document.getElementById('txtLowFuelValue');
                var txtFuelDrainValue = document.getElementById('txtFuelDrainValue');
                var txtFuelFillValue = document.getElementById('txtFuelFillValue');

                if (RemoveSpaces(txtLowFuelValue.value).length == 0) {
                    errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter the Fuel Low Value.';
                    controlToBefocused = canProcess != 0 ? txtLowFuelValue : controlToBefocused;
                    canProcess = 0;
                }
                if (RemoveSpaces(txtFuelDrainValue.value).length == 0) {
                    errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter the Fuel Drain Value.';
                    controlToBefocused = canProcess != 0 ? txtFuelDrainValue : controlToBefocused;
                    canProcess = 0;
                }
                if (RemoveSpaces(txtFuelFillValue.value).length == 0) {
                    errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter the Fuel Fill Value.';
                    controlToBefocused = canProcess != 0 ? txtFuelFillValue : controlToBefocused;
                    canProcess = 0;
                }
            }
        }
    }
    else if (SelectedVal == '&') {
        var txtMDTMessage = document.getElementById('txtMDTMessage');
        var rbtPrecanned = document.getElementById("rbtPrecanned");
        if (document.getElementById('rbtNormal').checked && RemoveSpaces(txtMDTMessage.value).length == 0) {
            canProcess = 0;
            errorMessage = "Please enter the Message to send to the MDT.";
            controlToBefocused = txtMDTMessage;
        }
        if (rbtPrecanned != null && rbtPrecanned.checked == true) {
            canProcess = 0;
            errorMessage = "Please select the message to send to the MDT.";
            controlToBefocused = rbtPrecanned;
        }
    }
    else if (SelectedVal == 'L') {
        if (document.getElementById("rbtAddMDTUser") != null && document.getElementById("rbtAddMDTUser").checked == true) {
            var txtMDTUserId = document.getElementById('txtMDTUserId');
            var txtMDTUserName = document.getElementById('txtMDTUserName');
            var txtMDTUserPassword = document.getElementById('txtMDTUserPassword');

            if (RemoveSpaces(txtMDTUserId.value).length == 0) {
                canProcess = 0;
                errorMessage = "Please enter the User Id.";
                controlToBefocused = txtMDTUserId;
            }
            else {
                if (RemoveSpaces(txtMDTUserId.value).length < 5) {
                    canProcess = 0;
                    errorMessage = "User Id should be of fixed length of 5 characters.";
                    controlToBefocused = txtMDTUserId;
                }
            }
            if (RemoveSpaces(txtMDTUserName.value).length == 0) {
                errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter the User Name.';
                controlToBefocused = canProcess != 0 ? txtMDTUserName : controlToBefocused;
                canProcess = 0;
            }
            if (RemoveSpaces(txtMDTUserPassword.value).length == 0) {
                errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter the User Password.';
                controlToBefocused = canProcess != 0 ? txtMDTUserPassword : controlToBefocused;
                canProcess = 0;
            }
            else {
                if (RemoveSpaces(txtMDTUserPassword.value).length < 8) {
                    errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Password should be of fixed length of 8 characters.';
                    controlToBefocused = canProcess != 0 ? txtMDTUserPassword : controlToBefocused;
                    canProcess = 0;
                }
            }
        }
        else if (document.getElementById("rbtDeleteMDTUser") != null && document.getElementById("rbtDeleteMDTUser").checked == true) {
            if (document.getElementById("ddlMDTUserName").value == "-Select-") {
                canProcess = 0;
                errorMessage = "Please select the User Name.";
                controlToBefocused = document.getElementById("ddlMDTUserName");
            }
        }
    }
    else if (SelectedVal == 'D') {
        if (!document.getElementById('rbtDeleteMDTMsgTable').checked && document.getElementById("ddlCannedMsg").value == "-Select-") {
            canProcess = 0;
            errorMessage = "Please select the Precanned Message.";
            controlToBefocused = document.getElementById("ddlCannedMsg");
        }
    }
    else if (SelectedVal == 'b') {
        var txtDialNo = document.getElementById('txtDialNo');
        if (RemoveSpaces(txtDialNo.value).length == 0) {
            canProcess = 0;
            errorMessage = "Please enter the Default Dial Number.";
            controlToBefocused = txtDialNo;
        }
        else {
            if (txtDialNo.value.length < 10) {
                canProcess = 0;
                errorMessage = "Please enter a valid Default Dial Number.";
                controlToBefocused = txtDialNo;
            }
        }
    }
    else if (SelectedVal == 'I') {
        var txtHB = document.getElementById('txtHB');
        var txtEB = document.getElementById('txtEB');
        var txtHA = document.getElementById('txtHA');
        var txtEA = document.getElementById('txtEA');
        var txtHC = document.getElementById('txtHC');
        var txtHCA = document.getElementById('txtHCA');
        if ((RemoveSpaces(txtHB.value).length == 0) && (RemoveSpaces(txtEB.value).length == 0)
            && (RemoveSpaces(txtHA.value).length == 0) && (RemoveSpaces(txtEA.value).length == 0)
                && (RemoveSpaces(txtHC.value).length == 0) && (RemoveSpaces(txtHCA.value).length == 0)) {
            canProcess = 0;
            errorMessage = 'Please enter Atleast one Thresholds value';
            controlToBefocused = txtHB;
            txtHB.style.backgroundColor = 'lightyellow';
        }
        else {
            txtHB.style.backgroundColor = 'white';
            if (RemoveSpaces(txtHB.value).length > 0) {
                if ((parseInt(txtHB.value) >= 5) && (parseInt(txtHB.value) <= 20)) {
                    txtHB.style.backgroundColor = 'White';
                }
                else {
                    canProcess = 0;
                    errorMessage = 'Please enter valid Harsh Braking value.';
                    controlToBefocused = txtHB;
                    txtHB.style.backgroundColor = 'lightyellow';
                }
            }
            if (RemoveSpaces(txtEB.value).length > 0) {
                if ((parseInt(txtEB.value) >= 5) && (parseInt(txtEB.value) <= 20)) {
                    //Code included by ranjith on 27-june-2011 for Extreme Break Validation
                    txtHB = parseInt(txtHB.value) + 2
                    if (parseInt(txtEB.value) <= parseInt(txtHB)) {
                        errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Extreme Braking value should be 3 miles greater than Harsh Braking.';
                        controlToBefocused = canProcess != 0 ? txtEB : controlToBefocused;
                        canProcess = 0;
                        txtEB.style.backgroundColor = 'lightyellow';
                    }
                    else {
                        txtEB.style.backgroundColor = 'white';
                    }
                }
                else {
                    errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter valid Extreme Braking value.';
                    controlToBefocused = canProcess != 0 ? txtEB : controlToBefocused;
                    canProcess = 0;
                    txtEB.style.backgroundColor = 'lightyellow';
                }
            }
            if (RemoveSpaces(txtHA.value).length > 0) {
                if ((parseInt(txtHA.value) >= 5) && (parseInt(txtHA.value) <= 20)) {
                    txtHA.style.backgroundColor = 'white';
                }
                else {
                    errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter valid Harsh Acceleration value.';
                    controlToBefocused = canProcess != 0 ? txtHA : controlToBefocused;
                    canProcess = 0;
                    txtHA.style.backgroundColor = 'lightyellow';
                }
            }
            if (RemoveSpaces(txtEA.value).length > 0) {
                if ((parseInt(txtEA.value) >= 5) && (parseInt(txtEA.value) <= 20)) {
                    //Code included by ranjith on 27-june-2011 for Extreme Acceleration Validation
                    txtHA = parseInt(txtHA.value) + 2
                    if (parseInt(txtEA.value) <= parseInt(txtHA)) {
                        errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Extreme Acceleration value should be 3 miles greater than Harsh Acceleration.';
                        controlToBefocused = canProcess != 0 ? txtEA : controlToBefocused;
                        canProcess = 0;
                        txtEA.style.backgroundColor = 'lightyellow';
                    }
                    else {
                        txtEA.style.backgroundColor = 'white';
                    }
                }
                else {
                    errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter valid Extreme Acceleration value.';
                    controlToBefocused = canProcess != 0 ? txtEA : controlToBefocused;
                    canProcess = 0;
                    txtEA.style.backgroundColor = 'lightyellow';
                }
            }
            if (RemoveSpaces(txtHC.value).length > 0) {
                if ((parseInt(txtHC.value) >= 5) && (parseInt(txtHC.value) <= 20)) {
                    txtHC.style.backgroundColor = 'white';
                }
                else {
                    errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter valid Harsh Cornering value.';
                    controlToBefocused = canProcess != 0 ? txtHC : controlToBefocused;
                    canProcess = 0;
                    txtHC.style.backgroundColor = 'lightyellow';
                }
            }
            if (RemoveSpaces(txtHCA.value).length > 0) {
                if ((parseInt(txtHCA.value) >= 31) && (parseInt(txtHCA.value) <= 180)) {
                    txtHCA.style.backgroundColor = 'white';
                }
                else {
                    errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter valid Harsh Cornering Angle value.';
                    controlToBefocused = canProcess != 0 ? txtHCA : controlToBefocused;
                    canProcess = 0;
                    txtHCA.style.backgroundColor = 'lightyellow';
                }
            }

        }

    }
    else if (SelectedVal == 'i') {
        var txtKeepAliveInterval = document.getElementById('txtKeepAliveInterval');
        if (RemoveSpaces(txtKeepAliveInterval).length == 0) {
            errorMessage = 'Please enter the Keep Alive Interval';
            canProcess = 0;
            txtKeepAliveInterval.style.backgroundColor = 'lightyellow';
            controlToBefocused = txtKeepAliveInterval;
        }
        else if (RemoveSpaces(txtKeepAliveInterval).length > 0) {
            if ((parseInt(RemoveSpaces(txtKeepAliveInterval)) == 0) || (parseInt(RemoveSpaces(txtKeepAliveInterval)) > 120)) {
                errorMessage = 'Please enter the valid Keep Alive Interval';
                canProcess = 0;
                txtKeepAliveInterval.style.backgroundColor = 'lightyellow';
                controlToBefocused = txtKeepAliveInterval;
            }
            else {
                txtKeepAliveInterval.style.backgroundColor = 'white';
            }
        }
        else {
            txtKeepAliveInterval.style.backgroundColor = 'white';
        }
    }
    else if (SelectedVal == 'w' || SelectedVal == 'p') {
        // DO NOT validate for APN settings
        if (document.getElementById('rbtAPNChange') != null) {
            if (document.getElementById('rbtAPNChange').checked) {
                return true;
            }
        }
        var addressField = RemoveSpaces(document.getElementById('txtAddressByte'));
        var noOfBytesfield = RemoveSpaces(document.getElementById('txtnoofBytes'));

        if (addressField.length == 0) {
            errorMessage = 'Please enter the address location';
            canProcess = 0;
            document.getElementById('txtAddressByte').style.backgroundColor = 'lightyellow';
            controlToBefocused = document.getElementById('txtAddressByte')
        }
        else { document.getElementById('txtAddressByte').style.backgroundColor = 'white'; }
        if (noOfBytesfield.length == 0) {
            errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter the no of bytes';
            controlToBefocused = canProcess == 0 ? controlToBefocused : document.getElementById('txtnoofBytes');
            canProcess = 0;
            document.getElementById('txtnoofBytes').style.backgroundColor = 'lightyellow';
        }
        else { document.getElementById('txtnoofBytes').style.backgroundColor = 'white'; }
        if (SelectedVal == 'w') {
            var dataField = RemoveSpaces(document.getElementById('txtData'));
            if (dataField.length == 0) {
                errorMessage = (canProcess == 0 ? errorMessage + '\n' : '') + 'Please enter the data to be written';
                controlToBefocused = canProcess == 0 ? controlToBefocused : document.getElementById('txtData');
                canProcess = 0;
                document.getElementById('txtData').style.backgroundColor = 'lightyellow';
            }
            else { document.getElementById('txtData').style.backgroundColor = 'white'; }
        }
    }

    if (canProcess == 1) {
        return true;
    }
    else {
        alert(errorMessage);
        try {
            controlToBefocused.focus();
        } catch (Error) { }
        return false;
    }
}
function RemoveSpaces(fieldName) {
    var s;
    if (typeof (fieldName) == 'object') {
        s = fieldName.value;
    }
    else {
        s = fieldName;
    }
    s = s.replace(/(^\s*)|(\s*$)/gi, "");
    s = s.replace(/[ ]{2,}/gi, " ");
    s = s.replace(/\n /, "\n");
    if (typeof (fieldName) == 'object') {
        fieldName.value = s;
    }
    return s;
}

function CheckLiveTrack() {
    var ddlLiveTrackFrequency = document.getElementById('ddlLiveTrackFrequency').value;
    if ((parseInt(ddlLiveTrackFrequency) >= 1) && (parseInt(ddlLiveTrackFrequency) < 4)) {
        clearDropdown(document.getElementById('ddlFrequencyPeriod'));
        addOption(document.getElementById('ddlFrequencyPeriod'), '1', '1');
        addOption(document.getElementById('ddlFrequencyPeriod'), '2', '2');
        document.form1.ddlFrequencyPeriod.disabled = false;
    }
    else if ((parseInt(ddlLiveTrackFrequency) >= 4) && (parseInt(ddlLiveTrackFrequency) < 11)) {
        clearDropdown(document.getElementById('ddlFrequencyPeriod'));
        addOption(document.getElementById('ddlFrequencyPeriod'), '1', '1');
        addOption(document.getElementById('ddlFrequencyPeriod'), '2', '2');
        addOption(document.getElementById('ddlFrequencyPeriod'), '3', '3');
        addOption(document.getElementById('ddlFrequencyPeriod'), '5', '5');
        document.form1.ddlFrequencyPeriod.disabled = false;

    }
    else if ((parseInt(ddlLiveTrackFrequency) >= 11)) {
        clearDropdown(document.getElementById('ddlFrequencyPeriod'));
        addOption(document.getElementById('ddlFrequencyPeriod'), '1', '1');
        addOption(document.getElementById('ddlFrequencyPeriod'), '2', '2');
        addOption(document.getElementById('ddlFrequencyPeriod'), '3', '3');
        addOption(document.getElementById('ddlFrequencyPeriod'), '5', '5');
        addOption(document.getElementById('ddlFrequencyPeriod'), '8', '8');
        addOption(document.getElementById('ddlFrequencyPeriod'), '10', '10');
        document.form1.ddlFrequencyPeriod.disabled = false;

    }
    else {
        document.form1.ddlFrequencyPeriod.disabled = true;

    }
}
function ShowOrHideMDTMsgType() {
    var isIE = CheckBrowser();
    var rbtPrecanned;
    rbtPrecanned = document.getElementById("rbtPrecanned");
    if (rbtPrecanned != null && rbtPrecanned.checked == true) {
        document.getElementById("trPreCannedMessages").className = (isIE ? "ControlShow" : "ControlShowFF");
        document.getElementById("trServerMessages").className = "ControlHide";
    }
    else {
        document.getElementById("trServerMessages").className = (isIE ? "ControlShow" : "ControlShowFF");
        document.getElementById("trPreCannedMessages").className = "ControlHide";
    }
}
function ShowOrHideMDTUserTableFields() {
    var isIE = CheckBrowser();
    document.getElementById("trMDTUserId").className = "ControlHide";
    document.getElementById("trMDTUserName").className = "ControlHide";
    document.getElementById("trMDTUserPassword").className = "ControlHide";

    if (document.getElementById("rbtAddMDTUser") != null && document.getElementById("rbtAddMDTUser").checked == true) {
        document.getElementById("trMDTUserId").className = (isIE ? "ControlShow" : "ControlShowFF");
        document.getElementById("trMDTUserName").className = (isIE ? "ControlShow" : "ControlShowFF");
        document.getElementById("txtMDTUserName").className = "ControlShow";
        document.getElementById("trMDTUserPassword").className = (isIE ? "ControlShow" : "ControlShowFF");
        document.getElementById("ddlMDTUserName").className = "ControlHide";

        document.getElementById("txtMDTUserId").value = "";
        document.getElementById("txtMDTUserName").value = "";
        document.getElementById("txtMDTUserPassword").value = "";
    }
    else if (document.getElementById("rbtDeleteMDTUser") != null && document.getElementById("rbtDeleteMDTUser").checked == true) {
        document.getElementById("trMDTUserName").className = (isIE ? "ControlShow" : "ControlShowFF");
        document.getElementById("txtMDTUserName").className = "ControlHide";
        document.getElementById("ddlMDTUserName").className = "ControlShow";

        document.getElementById("ddlMDTUserName").selectedIndex = 0;
    }
    else {
        // Nothing
    }
}
function ShowOrHideMDTMsgTableFields() {
    var isIE = CheckBrowser();
    if (document.getElementById("rbtDeleteMDTMsgTable") != null && document.getElementById("rbtDeleteMDTMsgTable").checked == true) {
        document.getElementById("trMDTCannedMsg").className = "ControlHide";
    }
    else {
        document.getElementById("trMDTCannedMsg").className = (isIE ? "ControlShow" : "ControlShowFF");
    }
}
function ManipulateVisibility(canShowFleet, canShowVehicle, canShowDeviceId, canShowLicencePlate,
        canShowLastStatus, canShowCommnDate, canShowSpeed, canShowStatus, canShowAddress, canShowSensor) {
    var isIE = CheckBrowser();
    document.getElementById('trFleetDetails').style.display = canShowFleet ? "block" : "none";          //(isIE ? "block" : "table-row") : 'none';
    if (document.getElementById('trVehicleDetails') != null) {
        document.getElementById('trVehicleDetails').style.display = canShowVehicle ? "block" : "none";
    }//(isIE ? "block" : "table-row") : 'none';
    document.getElementById('trDeviceID').style.display = canShowDeviceId ? "block" : "none";           //(isIE ? "block" : "table-row") : 'none';
    document.getElementById('trLicPlate').style.display = canShowLicencePlate ? "block" : "none";       //(isIE ? "block" : "table-row") : 'none';
    //    document.getElementById('trLastStatus').style.display = canShowLastStatus ? (isIE ? "block" : "table-row") : 'none';
    //    document.getElementById('trLastStatusDetails').style.display = canShowLastStatus ? (isIE ? "block" : "table-row") : 'none';
    if (document.getElementById('trCommndate') != null) {
        document.getElementById('trCommndate').style.display = canShowCommnDate ? "block" : "none";       //(isIE ? "block" : "table-row") : 'none';
    }
    //if (document.getElementById('trSpeed') != null)
    //    document.getElementById('trSpeed').style.display = canShowSpeed ? "block" : "none";       //(isIE ? "block" : "table-row") : 'none';
    if (document.getElementById('trStatus') != null)
        document.getElementById('trStatus').style.display = canShowStatus ? "block" : "none";       //(isIE ? "block" : "table-row") : 'none';
    if (document.getElementById('trAddress') != null)
        document.getElementById('trAddress').style.display = canShowAddress ? "block" : "none";       //(isIE ? "block" : "table-row") : 'none';
  //  if (document.getElementById('trSensorDetails') != null)
    //    document.getElementById('trSensorDetails').style.display = canShowSensor ? "block" : "none";       //(isIE ? "block" : "table-row") : 'none';
}

//Included by Sashma on 17-Mar-2010
function UserPreferenceVisibility(canShowTimezone, canShowDistance, canShowDefaultFleet, canShowScreenRefreshFrequency) {
    var isIE = CheckBrowser();
    //document.getElementById('trTimezone').setAttribute((isIE ? 'className' : 'class'), (canShowTimezone ? "form-group ControlShow" : "ControlHide"));       //(isIE ? "ControlShow" : "ControlShowFF") : 'ControlHide')); //  .style.display = canShowTimezone ? 'block' : 'none';
    ////document.getElementById('trDistance').setAttribute((isIE ? 'className' : 'class'), (canShowDistance ? "form-group ControlShow" : "ControlHide"));       //(isIE ? "ControlShow" : "ControlShowFF") : 'ControlHide')); //.style.display = canShowDistance ? 'block' : 'none';
    //document.getElementById('trDefaultFleet').setAttribute((isIE ? 'className' : 'class'), (canShowDefaultFleet ? "form-group ControlShow" : "ControlHide"));       //(isIE ? "ControlShow" : "ControlShowFF") : 'ControlHide')); //.style.display = canShowDefaultFleet ? 'block' : 'none';
    //document.getElementById('trScreenRefreshFrequency').setAttribute((isIE ? 'className' : 'class'), (canShowScreenRefreshFrequency ? "form-group ControlShow" : "ControlHide"));       //(isIE ? "ControlShow" : "ControlShowFF") : 'ControlHide')); //.style.display = canShowScreenRefreshFrequency ? 'block' : 'none';

}

//Included by Sakthi Gopinath S on 08-Jul-2011
//Get confirmation for particular commands
function getConfirmation() {

    var controlToBefocused;   
    var canProcess = 1;    
    var errorMessage = "";
    var alertMessage = '';
    //Clear Memory,Reset Box,Write EEPROM
    var SelectedVal = ddlCommands.GetValueString();
    if (SelectedVal == "M" || SelectedVal == "S" || SelectedVal == "w") {
        if (confirm('Are you sure want to send the command?')) { return true; } else { return false; }
    }
    //Firmware Upgrade
    else if (SelectedVal == "OTA" || SelectedVal == "1") {
        if (confirm('Are you sure want to upgrade the firmware?')) { return true; } else { return false; }
        //if(validateOTAFilename()){return true;}else{return false;}
    }
    else if (SelectedVal == "W2") {
        var res = EnableDisable(-1, null, null, null);
        return res;
    }
    else if (SelectedVal == "W37") {
        var res = fnRFIDSelection();
        return res;
    }
    else if (SelectedVal =="T")
    {
        
        var txt_Temp1Mn = document.getElementById('txt_Temp1Mn');
        var txt_Temp1Mx = document.getElementById('txt_Temp1Mx');
        var txt_Temp2Mn = document.getElementById('txt_Temp2Mn');
        var txt_Temp2Mx = document.getElementById('txt_Temp2Mx');
        var txt_Temp3Mn = document.getElementById('txt_Temp3Mn');
        var txt_Temp3Mx = document.getElementById('txt_Temp3Mx');
        var txt_Temp4Mn = document.getElementById('txt_Temp4Mn');
        var txt_Temp4Mx = document.getElementById('txt_Temp4Mx');
        var lblLicensePlate = document.getElementById('lblLicensePlate');
        
        if (RemoveSpaces(txt_Temp1Mn.value).length == 0 || RemoveSpaces(txt_Temp1Mn.value).length <5) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + "Please enter the valid Temperature-1 Min Value.";
            controlToBefocused = txt_Temp1Mn;
        }
        if (RemoveSpaces(txt_Temp1Mx.value).length == 0 || RemoveSpaces(txt_Temp1Mx.value).length < 5) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>')   + 'Please enter the valid Temperature-1 Max Value.';
            controlToBefocused = txt_Temp1Mx;
        }
        if (RemoveSpaces(txt_Temp2Mn.value).length == 0 || RemoveSpaces(txt_Temp2Mn.value).length < 5) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the valid Temperature-2 Min Value.';
            controlToBefocused = txt_Temp2Mn;
        }
        if (RemoveSpaces(txt_Temp2Mx.value).length == 0 || RemoveSpaces(txt_Temp2Mx.value).length < 5) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the valid Temperature-2 Max Value.';
            controlToBefocused = txt_Temp2Mx;
        }
        if ((RemoveSpaces(txt_Temp3Mn.value).length == 0 || RemoveSpaces(txt_Temp3Mn.value).length < 5) && lblLicensePlate.innerHTML.includes("Double")) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the valid Temperature-3 Min Value.';
            controlToBefocused = txt_Temp3Mn;
        }
        if ((RemoveSpaces(txt_Temp3Mx.value).length == 0 || RemoveSpaces(txt_Temp3Mx.value).length < 5) && lblLicensePlate.innerHTML.includes("Double")) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the valid Temperature-3 Max Value.';
            controlToBefocused = txt_Temp3Mx;
        }
        if ((RemoveSpaces(txt_Temp4Mn.value).length == 0 || RemoveSpaces(txt_Temp4Mn.value).length < 5) && lblLicensePlate.innerHTML.includes("Double")) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the valid Temperature-4 Min Value.';
            controlToBefocused = txt_Temp4Mn;
        }
        if ((RemoveSpaces(txt_Temp4Mx.value).length == 0 || RemoveSpaces(txt_Temp4Mx.value).length < 5) && lblLicensePlate.innerHTML.includes("Double")) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the valid Temperature-4 Max Value.';
            controlToBefocused = txt_Temp4Mx;
        }
        if (canProcess == 1) {
            return true;
        }
        else {
            TrinetraCustomAlerts(alertMessage);
            try {
                controlToBefocused.focus();
            } catch (Error) { }
            return false;
        }
    }
    else if (SelectedVal == "P") {

        var txt_P3Mn = document.getElementById('txt_P3Mn');
        var txt_P3Mx = document.getElementById('txt_P3Mx');
        var txt_DPsMn = document.getElementById('txt_DPsMn');
        var txt_DPsMx = document.getElementById('txt_DPsMx');
        var txt_DPpMn = document.getElementById('txt_DPpMn');
        var txt_DPpMx = document.getElementById('txt_DPpMx');
        var txt_P4Mn = document.getElementById('txt_P4Mn');
        var txt_P4Mx = document.getElementById('txt_P4Mx');
        var txt_DPp1Mn = document.getElementById('txt_DPp1Mn');
        var txt_DPp1Mx = document.getElementById('txt_DPp1Mx');
        var lblLicensePlate = document.getElementById('lblLicensePlate');

        if (RemoveSpaces(txt_P3Mn.value).length == 0) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + "Please enter the Pressure-3 Min Value.";
            controlToBefocused = txt_P3Mn;
        }
        if (RemoveSpaces(txt_P3Mx.value).length == 0) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the Pressure-3 Max Value.';
            controlToBefocused = txt_P3Mx;
        }
        if (RemoveSpaces(txt_DPsMn.value).length == 0) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the DPs Min Value.';
            controlToBefocused = txt_DPsMn;
        }
        if (RemoveSpaces(txt_DPsMx.value).length == 0) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the DPs Max Value.';
            controlToBefocused = txt_DPsMx;
        }
        if (RemoveSpaces(txt_DPpMn.value).length == 0) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the DPp Min Value.';
            controlToBefocused = txt_DPpMn;
        }
        if (RemoveSpaces(txt_DPpMx.value).length == 0) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the DPp Max Value.';
            controlToBefocused = txt_DPpMx;
        }
        if (RemoveSpaces(txt_P4Mn.value).length == 0 && lblLicensePlate.innerHTML.includes("Double")) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the Pressure-4 Min Value.';
            controlToBefocused = txt_P4Mn;
        }
        if (RemoveSpaces(txt_P4Mx.value).length == 0 && lblLicensePlate.innerHTML.includes("Double")) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the Pressure-4 Max Value.';
            controlToBefocused = txt_P4Mx;
        }
        if (RemoveSpaces(txt_DPp1Mn.value).length == 0 && lblLicensePlate.innerHTML.includes("Double")) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the DPp1 Min Value.';
            controlToBefocused = txt_DPp1Mn;
        }
        if (RemoveSpaces(txt_DPp1Mx.value).length == 0 && lblLicensePlate.innerHTML.includes("Double")) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the DPp1 Max Value.';
            controlToBefocused = txt_DPp1Mx;
        }
        if (canProcess == 1) {
            return true;
        }
        else {
            TrinetraCustomAlerts(alertMessage);
            try {
                controlToBefocused.focus();
            } catch (Error) { }
            return false;
        }
    }
    else if (SelectedVal == "E") {

        var txt_V1Mn = document.getElementById('txt_V1Mn');
        var txt_V1Mx = document.getElementById('txt_V1Mx');
        var txt_C1Mn = document.getElementById('txt_C1Mn');
        var txt_C1Mx = document.getElementById('txt_C1Mx');
        var txt_RC1 = document.getElementById('txt_RC1');
        var txt_V2Mn = document.getElementById('txt_V2Mn');
        var txt_V2Mx = document.getElementById('txt_V2Mx');
        var txt_C2Mn = document.getElementById('txt_C2Mn');
        var txt_C2Mx = document.getElementById('txt_C2Mx');
        var txt_RC2 = document.getElementById('txt_RC2');
        var lblLicensePlate = document.getElementById('lblLicensePlate');

        if (RemoveSpaces(txt_V1Mn.value).length == 0) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + "Please enter the Voltage-1 Min Value.";
            controlToBefocused = txt_V1Mn;
        }
        if (RemoveSpaces(txt_V1Mx.value).length == 0) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the Voltage-1 Max Value.';
            controlToBefocused = txt_V1Mx;
        }
        if ((RemoveSpaces(txt_C1Mn.value).length == 0 || RemoveSpaces(txt_Temp2Mx.value).length < 5)) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the valid Current-1 Min Value.';
            controlToBefocused = txt_C1Mn;
        }
        if ((RemoveSpaces(txt_C1Mx.value).length == 0 || RemoveSpaces(txt_Temp2Mx.value).length < 5)) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the valid Current-1 Max Value.';
            controlToBefocused = txt_C1Mx;
        }
        if ((RemoveSpaces(txt_RC1.value).length == 0 || RemoveSpaces(txt_Temp2Mx.value).length < 5)) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the valid RC1 Value.';
            controlToBefocused = txt_RC1;
        }
        if (RemoveSpaces(txt_V2Mn.value).length == 0) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the Voltage-2 Min Value.';
            controlToBefocused = txt_V2Mn;
        }
        if (RemoveSpaces(txt_V2Mx.value).length == 0 && lblLicensePlate.innerHTML.includes("Double")) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the Voltage-2 Max Value.';
            controlToBefocused = txt_V2Mx;
        }
        if ((RemoveSpaces(txt_C2Mn.value).length == 0 || RemoveSpaces(txt_Temp2Mx.value).length < 5)&& lblLicensePlate.innerHTML.includes("Double")) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the valid Current-2 Min Value.';
            controlToBefocused = txt_C2Mn;
        }
        if ((RemoveSpaces(txt_C2Mx.value).length == 0 || RemoveSpaces(txt_Temp2Mx.value).length < 5) && lblLicensePlate.innerHTML.includes("Double")) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the valid Current-2 Max Value.';
            controlToBefocused = txt_C2Mx;
        }
        if ((RemoveSpaces(txt_RC2.value).length == 0 || RemoveSpaces(txt_Temp2Mx.value).length < 5) && lblLicensePlate.innerHTML.includes("Double")) {
            canProcess = 0;
            alertMessage += (alertMessage.length == 0 ? '' : '</br>') + 'Please enter the valid RC1 Value.';
            controlToBefocused = txt_RC2;
        }
        if (canProcess == 1) {
            return true;
        }
        else {
            TrinetraCustomAlerts(alertMessage);
            try {
                controlToBefocused.focus();
            } catch (Error) { }
            return false;
        }
    }
    else { return true; }
}

function fnRFIDSelection() {
    var checkBoxList = document.getElementById("cblRFIDDrivers");
    var numCheckBoxItems = checkBoxList.cells.length;
    var numItemsChecked = 0;

    for (i = 0; i < numCheckBoxItems; i++) {
        //Get the checkboxlist item
        var checkBox = document.getElementById(checkBoxList.id + '_' + [i]);
        //Check if the checkboxlist item exists, and if it is checked
        if (checkBox != null && checkBox.checked) { numItemsChecked = numItemsChecked + 1; }
    }
    if (numCheckBoxItems == 0) { alert('Please select atlease one RFID'); return false; }
    else if (numItemsChecked > 5) {
        alert('Maximum limit exceeded. 5 RFID only assign or unassign at a time.');
        return false;
    }
    else {
        return true;
    }
}

//Included by Sakthi Gopinath S on 08-Jul-2011
//For OTA Filename validation
function validateOTAFilename() {
    var currOTAFilename = document.getElementById('lblCurrVersion').firstChild.data;
    //var currOTACtrlChild = currOTACtrl.firstChild;
    //var currOTAFilename = currOTACtrlChild.data;
    var selOTAFilename = ddlGeozone.GetSelectedItem().text;
    var result = false;
    if (currOTAFilename.indexOf("(") != -1) {
        result = validateFileMatching(currOTAFilename, selOTAFilename, 1);
    }
    else {
        result = validateFileMatching(currOTAFilename, selOTAFilename, 0);
    }
    if (result == true) { if (window.confirm('Are you sure want to upgrade the firmware?') == false) { result = false; } }
    else { alert('You cannot upgrade this firmware version'); }
    return result;
}
//Validate whether OTA Filename is old or new version and send to corresponding process.
function validateFileMatching(currFilename, selFilename, ver) {
    var curArr = currFilename.replace("QA_", "").replace(".hex", "").split('_');
    var selArr = selFilename.replace("QA_", "").replace(".hex", "").split('_');
    var val1 = 0;
    var val2 = 0;
    var result = false;
    if (ver == 1) {
        if (curArr[0].indexOf("T") != -1 || curArr[0].indexOf("t") != -1) {
            val1 = parseInt(curArr[0].replace("T", ""), 10);
        }
        if (selArr[0].indexOf("T") != -1 || selArr[0].indexOf("t") != -1) {
            val2 = parseInt(selArr[0].replace("T", ""), 10);
        }
        result = validateVal(val1, val2, 1);
        if (result) {
            val1 = curArr[1];
            val2 = selArr[1];
            result = validateVal(val1, val2, 0);
            if (result) {
                var splArr1 = curArr[2].split("(");
                var splArr2 = selArr[2].split("(");
                if (splArr1.length == splArr2.length) {
                    val1 = parseInt(splArr1[0], 10);
                    val2 = parseInt(splArr2[0], 10);
                    result = validateVal(val1, val2, 2);
                    if (result) {
                        var splArr3 = currFilename.replace(".hex", "").split(")");
                        var splArr4 = selFilename.replace(".hex", "").split(")");
                        if (splArr3.length = splArr4.length) {
                            result = (splArr3[1] == splArr4[1]) ? true : false;
                        }
                        else {
                            result = false;
                        }
                    }
                }
                else {
                    result = validateFileMatching(currFilename, selFilename, 0);
                }
            }
        }
    }
    else if (ver == 0) {
        if (curArr[0].indexOf("T") != -1 || curArr[0].indexOf("t") != -1) {
            val1 = parseInt(curArr[0].replace("T", ""), 10);
        }
        if (selArr[0].indexOf("T") != -1 || selArr[0].indexOf("t") != -1) {
            val2 = parseInt(selArr[0].replace("T", ""), 10);
        }
        result = validateVal(val1, val2, 1);
    }
    return result;
}

function validateVal(val1, val2, opr) {
    var valid = false;

    //Validate Alphabet
    if (opr == 0) {
        if (val1.charCodeAt(0) <= val2.charCodeAt(0)) { valid = true; }
    }
    //validate number
    else if (opr == 1) {
        if (val2 > val1 || val2 == val1 - 1) { valid = true; }
    }
    else if (opr == 2) {
        if (val2 == val1) { valid = true; }
    }
    return valid;
}

function validationSpeedBand(field, key) {
    var spLow = document.getElementById('txtSpeed' + field + 'Low');
    var spHigh = document.getElementById('txtSpeed' + field + 'High');
    var spDur = document.getElementById('txtSpeed' + field + 'Dur');
    var spLowPrev = document.getElementById('txtSpeed' + (field - 1) + 'Low');
    var spHighPrev = document.getElementById('txtSpeed' + (field - 1) + 'High');
    var spDurPrev = document.getElementById('txtSpeed' + (field - 1) + 'Dur');
    var spLowNext = document.getElementById('txtSpeed' + (field + 1) + 'Low');
    var spHighNext = document.getElementById('txtSpeed' + (field + 1) + 'High');
    var spDurNext = document.getElementById('txtSpeed' + (field + 1) + 'Dur');

    if (key == 'l') { if (!(spLow.value.length != 0 && parseInt(spLow.value, 10) >= 0 && parseInt(spLow.value, 10) <= 190)) { alert('Please enter low speed band between 0 - 190 kms'); spLow.value = ''; spLow.focus(); return false; } }
    if (key == 'h') { if (!(spHigh.value.length != 0 && parseInt(spHigh.value, 10) >= 10 && parseInt(spHigh.value, 10) <= 200)) { alert('Please enter high speed band between 10 - 200 kms'); spHigh.value = ''; spHigh.focus(); return false; } }
    if (key == 'd') { if (!(spDur.value.length != 0 && parseInt(spDur.value, 10) >= 3 && parseInt(spDur.value, 10) <= 60)) { alert('Please enter duration between 3 - 60 secs'); spDur.value = ''; spDur.focus(); return false; } }


    if (spLow.value.length != 0 && spHigh.value.length != 0) {
        if (parseInt(spLow.value, 10) > parseInt(spHigh.value, 10)) {
            alert("Please enter a valid speed band [L - H]");
            spLow.value = '';
            spHigh.value = '';
            return false;
        }
    }
    if (field == 1) {
        if (spLowNext.value.length != 0 || spHighNext.value.length != 0) {
            if (parseInt(spLow.value, 10) > parseInt(spLowNext.value, 10)) {
                alert("Please enter a valid speed band low 1");
                spLow.value = '';
                spLow.focus();
                return false;
            }
            else if (parseInt(spHigh.value, 10) > parseInt(spHighNext.value, 10)) {
                alert("Please enter a valid speed band High 1");
                spHigh.value = '';
                spHigh.focus();
                return false;
            }
            else { return true; }
        } else { return true; }
    }
    else if (field == 6) {
        if (spLowPrev.value.length != 0 || spHighPrev.value.length != 0) {
            if (parseInt(spLow.value, 10) < parseInt(spLowPrev.value, 10)) {
                alert("Please enter a valid speed band low 6");
                spLow.value = '';
                spLow.focus();
                return false;
            }
            else if (parseInt(spHigh.value, 10) < parseInt(spHighPrev.value, 10)) {
                alert("Please enter a valid speed band High 6");
                spHigh.value = '';
                spHigh.focus();
                return false;
            }
            else { return true; }
        } else { return true; }
    }
    else {
        if ((spLowPrev.value.length != 0 && spHighPrev.value.length != 0) || (spLowNext.value.length != 0 && spHighNext.value.length != 0)) {
            if ((parseInt(spLow.value, 10) < parseInt(spLowPrev.value, 10) || parseInt(spLow.value, 10) < parseInt(spHighPrev.value, 10)) || parseInt(spLow.value, 10) > parseInt(spLowNext.value, 10) || parseInt(spLow.value, 10) > parseInt(spHighNext.value, 10)) {
                alert("Please enter a valid speed band low " + field);
                spLow.value = '';
                spLow.focus();
                return false;
            }
            else if (parseInt(spHigh.value, 10) < parseInt(spHighPrev.value, 10) || parseInt(spHigh.value, 10) > parseInt(spLowNext.value, 10) || parseInt(spHigh.value, 10) > parseInt(spHighNext.value, 10)) {
                alert("Please enter a valid speed band High " + field);
                spHigh.value = '';
                spHigh.focus();
                return false;
            }
            else { return true; }
        } else { return true; }
    }
    //}
    //else { return true; }
}

function EnableDisable(field, low, high, dur) {
    var speedLow = document.getElementById(low);
    var speedHigh = document.getElementById(high);
    var speedDur = document.getElementById(dur);

    if (field == -1) {
        var result = true;
        speedLow = document.getElementById('txtSpeed1Low');
        speedHigh = document.getElementById('txtSpeed1High');
        speedDur = document.getElementById('txtSpeed1Dur');
        if (speedLow.disabled == false || speedHigh.disabled == false || speedDur.disabled == false) {
            if (speedLow.value.length == 0 || speedHigh.value.length == 0 || speedDur.value.length == 0) {
                result = false;
            }
        }
        if (result == true) {
            for (i = 2; i <= 6; i++) {
                speedLow = document.getElementById('txtSpeed' + i + 'Low');
                speedHigh = document.getElementById('txtSpeed' + i + 'High');
                speedDur = document.getElementById('txtSpeed' + i + 'Dur');
                if (speedLow.disabled == false || speedHigh.disabled == false || speedDur.disabled == false) {
                    if (speedLow.value.length == 0 && speedHigh.value.length == 0 && speedDur.value.length == 0) {
                        if (result != false) { result = true; }
                    }
                    else if (speedLow.value.length == 0 || speedHigh.value.length == 0 || speedDur.value.length == 0) {
                        result = false;
                    }
                }
            }
            if (result == false) { alert('Please fill the speed band parameter'); return false; }
            else { return true; }
        }
        else {
            alert('Please fill the speed band parameter');
            return false;
        }
    }
    else if (field == 0) {
        setEmptyValue(2, 'load');
    }
    else if (field == 1) {
        if (speedLow != null && speedHigh != null && speedDur != null && (speedLow.value.length == 0 || speedHigh.value.length == 0 || speedDur.value.length == 0)) {
            setEmptyValue(2, 'unload');
        }
        else {
            document.getElementById('txtSpeed2Low').disabled = false;
            document.getElementById('txtSpeed2High').disabled = false;
            document.getElementById('txtSpeed2Dur').disabled = false;
            document.getElementById('txtSpeed2Low').focus();
        }
    }
    else if (field == 2) {
        if (speedLow != null && speedHigh != null && speedDur != null && (speedLow.value.length == 0 || speedHigh.value.length == 0 || speedDur.value.length == 0)) {
            setEmptyValue(3, 'unload');
        }
        else {
            document.getElementById('txtSpeed3Low').disabled = false;
            document.getElementById('txtSpeed3High').disabled = false;
            document.getElementById('txtSpeed3Dur').disabled = false;
            document.getElementById('txtSpeed3Low').focus();
        }
    }
    else if (field == 3) {
        if (speedLow != null && speedHigh != null && speedDur != null && (speedLow.value.length == 0 || speedHigh.value.length == 0 || speedDur.value.length == 0)) {
            setEmptyValue(4, 'unload');
        }
        else {
            document.getElementById('txtSpeed4Low').disabled = false;
            document.getElementById('txtSpeed4High').disabled = false;
            document.getElementById('txtSpeed4Dur').disabled = false;
            document.getElementById('txtSpeed4Low').focus();
        }
    }
    else if (field == 4) {
        if (speedLow != null && speedHigh != null && speedDur != null && (speedLow.value.length == 0 || speedHigh.value.length == 0 || speedDur.value.length == 0)) {
            setEmptyValue(5, 'unload');
        }
        else {
            document.getElementById('txtSpeed5Low').disabled = false;
            document.getElementById('txtSpeed5High').disabled = false;
            document.getElementById('txtSpeed5Dur').disabled = false;
            document.getElementById('txtSpeed5Low').focus();
        }
    }
    else if (field == 5) {
        if (speedLow != null && speedHigh != null && speedDur != null && (speedLow.value.length == 0 || speedHigh.value.length == 0 || speedDur.value.length == 0)) {
            setEmptyValue(6, 'unload');
        }
        else {
            document.getElementById('txtSpeed6Low').disabled = false;
            document.getElementById('txtSpeed6High').disabled = false;
            document.getElementById('txtSpeed6Dur').disabled = false;
            document.getElementById('txtSpeed6Low').focus();
        }
    }
}
function setEmptyValue(band, condition) {
    for (i = band; i <= 6; i++) {
        if (condition == 'load') {
            if (document.getElementById('txtSpeed' + i + 'Low') != null) {
                if (document.getElementById('txtSpeed' + i + 'Low').value.length == 0) {
                    document.getElementById('txtSpeed' + i + 'Low').value = ''; document.getElementById('txtSpeed' + i + 'Low').disabled = true;
                } else { document.getElementById('txtSpeed' + i + 'Low').disabled = false; }
            }
            if (document.getElementById('txtSpeed' + i + 'High') != null) {
                if (document.getElementById('txtSpeed' + i + 'High').value.length == 0) {
                    document.getElementById('txtSpeed' + i + 'High').value = ''; document.getElementById('txtSpeed' + i + 'High').disabled = true;
                } else { document.getElementById('txtSpeed' + i + 'High').disabled = false; }
            }
            if (document.getElementById('txtSpeed' + i + 'Dur') != null) {
                if (document.getElementById('txtSpeed' + i + 'Dur').value.length == 0) {
                    document.getElementById('txtSpeed' + i + 'Dur').value = ''; document.getElementById('txtSpeed' + i + 'Dur').disabled = true;
                } else { document.getElementById('txtSpeed' + i + 'Dur').disabled = false; }
            }
        }
        else {
            document.getElementById('txtSpeed' + i + 'Low').value = ''; document.getElementById('txtSpeed' + i + 'Low').disabled = true;
            document.getElementById('txtSpeed' + i + 'High').value = ''; document.getElementById('txtSpeed' + i + 'High').disabled = true;
            document.getElementById('txtSpeed' + i + 'Dur').value = ''; document.getElementById('txtSpeed' + i + 'Dur').disabled = true;
        }
    }
}

