$jc = jQuery.noConflict();

function PerformScrollSetting() {
    try {
        //        alert(document.getElementById("gvData_DXMainTable").parentNode);
        SetDivPosition();

        document.getElementById("gvData_DXMainTable").parentNode.onscroll = function (event) {
            var intY = document.getElementById("gvData_DXMainTable").parentNode.scrollTop;
            document.getElementById('hdnScrollPosition').value = intY;
        };
    }
    catch (Error) { }
}

function SetDivPosition() {
    setTimeout(RestoreDivPostion, 2000);
}
function RestoreDivPostion() {
    var hdnScrollPosition = document.getElementById('hdnScrollPosition');
    if (hdnScrollPosition.value != "") {
        document.getElementById("gvData_DXMainTable").parentNode.scrollTop = hdnScrollPosition.value;
    }
}

//    Don't Remove the script as it is used to fix the carousel issue
function ScrollJCarousel() {
    //ShowAlarms()
    //    var canScroll = document.getElementById('hdnScrollPosition').value;
    //    alert(canScroll);
    //jQuery('#mycarousel').jcarousel({
    //    auto: 5,
    //    wrap: 'last',
    //    scroll: 1,
    //    animation: 3000,
    //});
    $jc(document).ready(function () {
        $jc('.jcarousel').jcarousel({
            item: '.jcarousel-item',
            animation: {
                duration: 800,
                easing: 'linear',
                complete: function () {
                }
            }
        })
        .jcarouselAutoscroll({
            interval: 5000,
            target: '+=1',
            autostart: true,
        });
        $jc('.jcarousel-prev').jcarouselControl({
            target: '-=1'
        });
        $jc('.jcarousel-next').jcarouselControl({
            target: '+=1'
        });
        var count = 1;
        var total = $jc('.jcarousel').jcarousel('items').size();
        $jc('.jcarousel').on('jcarousel:animateend', function (event, carousel) {
            count++;
            if (count == (total - 3)) {
                $jc('.jcarousel').jcarousel('scroll', 0);
                count = 1;
            }
        });
    });
}

//function ShowAlarms() {
//    var isIE = CheckBrowser();
//    if (document.getElementById("hdnShowAlarms").value == '1') {
//        document.getElementById("divBoxPanel").setAttribute(isIE ? "className" : "class", "ControlShow");
//    }
//    else {
//        document.getElementById("divBoxPanel").setAttribute(isIE ? "className" : "class", "ControlHide");
//    }
//}

function ShowAlarms() {
    var isIE = CheckBrowser();
    if (document.getElementById("hdnShowAlarms").value == '1') {
        if (isIE) {
            if (document.all && !document.querySelector) {
                document.getElementById("divBoxPanel").setAttribute("className", "ControlShow");
                document.getElementById("btnjcarouselPrev").setAttribute("className", "ControlShow jcarousel-prev");
                document.getElementById("btnjcarouselNext").setAttribute("className", "ControlShow jcarousel-next");
                //document.getElementById("divNoAlarms").setAttribute("className", "ControlHide");
            }
            else {
                document.getElementById("divBoxPanel").setAttribute("class", "ControlShow");
                document.getElementById("btnjcarouselPrev").setAttribute("class", "ControlShow jcarousel-prev");
                document.getElementById("btnjcarouselNext").setAttribute("class", "ControlShow jcarousel-next");
                //document.getElementById("divNoAlarms").setAttribute("class", "ControlHide");
            }
        }
        else {
            document.getElementById("divBoxPanel").setAttribute("class", "ControlShow");
            document.getElementById("btnjcarouselPrev").setAttribute("class", "ControlShow jcarousel-prev");
            document.getElementById("btnjcarouselNext").setAttribute("class", "ControlShow jcarousel-next");
            //document.getElementById("divNoAlarms").setAttribute("class", "ControlHide");
        }
    }
    else {
        if (isIE) {
            if (document.all && !document.querySelector) {
                document.getElementById("divBoxPanel").setAttribute("className", "ControlHide");
                document.getElementById("btnjcarouselPrev").setAttribute("className", "ControlHide");
                document.getElementById("btnjcarouselNext").setAttribute("className", "ControlHide");
                // document.getElementById("divNoAlarms").setAttribute("className", "ControlShow");
            }
            else {
                document.getElementById("divBoxPanel").setAttribute("class", "ControlHide");
                document.getElementById("btnjcarouselPrev").setAttribute("class", "ControlHide");
                document.getElementById("btnjcarouselNext").setAttribute("class", "ControlHide");
                //  document.getElementById("divNoAlarms").setAttribute("class", "ControlShow");
            }
        }
        else {
            document.getElementById("divBoxPanel").setAttribute("class", "ControlHide");
            document.getElementById("btnjcarouselPrev").setAttribute("class", "ControlHide");
            document.getElementById("btnjcarouselNext").setAttribute("class", "ControlHide");
            // document.getElementById("divNoAlarms").setAttribute("class", "ControlShow");
        }
    }
}

function ShowMsgPanel(errorMsg, isPanelVisible) {
    document.getElementById('pnlcmdMessage').style.display = isPanelVisible ? 'block' : 'none';
    document.getElementById('divFailedMsg').innerHTML = errorMsg;
}

/*window.onresize = function(event) {
load();
document.getElementById('btnResizeEventHandling').click();
}*/

function DispalyLocationFromFullScreen(mapItValue, mainCaption, isMessageVisbile, msgCaption, isDateTimeVisible,
    datetimeCaption, isAddressVisible, addressCaption, isLandmarkVisible, landmarkCaption, isSpeedVisible,
    speedCaption, addLandmarkCaption, isVehicleVisbile, vehicleCaption, isPolylineVisible, driverNameCaption, isDriverNameVisible) {
    DisplayMapIt(mapItValue, 'M', mainCaption, isMessageVisbile, msgCaption, isDateTimeVisible,
    datetimeCaption, isAddressVisible, addressCaption, isLandmarkVisible, landmarkCaption, isSpeedVisible,
    speedCaption, addLandmarkCaption, isVehicleVisbile, vehicleCaption, isPolylineVisible, driverNameCaption, isDriverNameVisible);
}

function mycarousel_initCallback(carousel) {

    // Code included by Kumuthini. L
    // Hide the Previous & Next Button if the alarms count is less than 5 
    if (document.getElementById('hdnScrollingAlarmsCount').value != '' && document.getElementById('hdnScrollingAlarmsCount').value <= 4) {
        carousel.buttonPrev.hide();
        carousel.buttonNext.hide();
        return false;
    }

    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function () {
        //        document.getElementById('hdnScrollPosition').value = 1;
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function () {
        //        document.getElementById('hdnScrollPosition').value = 1;
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function () {
        //        document.getElementById('hdnScrollPosition').value = 0;
        carousel.stopAuto(0);
        //        carousel.auto = 0;
    }, function () {
        //        document.getElementById('hdnScrollPosition').value = 1;
        //        carousel.auto = 7;
        carousel.startAuto();
    });
}

function StartScrolling() {
    document.getElementById('hdnScrollPosition').value = 1;
    //        carousel.auto = 7;
    carousel.startAuto();
}
//var jquery_1_11 = $.noConflict(true);
//$(document).ready(function () {
//ShowAlarms();
//jQuery('#mycarousel').jcarousel({
//    auto: 5,
//    scroll: 1,
//    wrap: 'last',
//    animation: 3000,
//    initCallback: mycarousel_initCallback
//});

//    $('.jcarousel').jcarousel({
//        item: '.jcarousel-item'
//    })
//    .jcarouselAutoscroll({
//        interval: 3000,
//        target: '+=1',
//        autostart: true
//    })
//    .jcarousel({
//        animation: {
//            duration: 800,
//            easing: 'linear',
//            complete: function () {
//            }
//        }
//    });
//    $('.jcarousel-prev').jcarouselControl({
//        target: '-=1'
//    }).jcarouselControl({
//        event: 'mouseover'
//    }).jcarouselControl({
//        method: 'scrollIntoView'
//    });
//    $('.jcarousel-next').jcarouselControl({
//        target: '+=1'
//    }).jcarouselControl({
//        event: 'mouseover'
//    }).jcarouselControl({
//        method: 'scrollIntoView'
//    });
//    //$('.jcarousel').on('jcarousel:createend', function () {
//    //    $(this).jcarousel('scroll', 0, false);
//    //});
//});


// Grid Functions
function OnGridSelectionChanged1() {
    //            if (counter != null) setInnerText(counter, grid.GetSelectedRowCount().toString());
    ShowPopUp();
    gvData.GetSelectedFieldValues('DeviceID;Mapitvalues;Notcurposveh;LandMarkvalues', onGridSelectionCompleteVal);
}

function onGridSelectionCompleteVal(values) {
    var hdnAlarmIDs = document.getElementById("hdnAlarmIDs");
    var hdnMapItValues = document.getElementById("hdnMapItValues");
    var hdnNotCurPosVeh = document.getElementById("hdnNotCurPosVeh");
    var hdnLandmarkvalues = document.getElementById("hdnLandmarkvalues");
    var j = 1;
    hdnAlarmIDs.value = '';
    hdnMapItValues.value = '';
    hdnNotCurPosVeh.value = '';
    hdnLandmarkvalues.value = '';

    for (var l = 0; l < values.length; l++) {

        hdnAlarmIDs.value += values[l][0] + ',';
        if (values[l][1] != null && values[l][1] != '') {
            hdnMapItValues.value += values[l][1];
        }
        if (values[l][2] != null && values[l][2] != '') {
            hdnNotCurPosVeh.value += j + '. ' + values[l][2];
            j++;
        }
        hdnLandmarkvalues.value += values[l][3] + '~&~';
    }
    HidePopUp();
}

//function onGridSelectionComplete(values) {
//    var counter = document.getElementById("hdnMapItValues");
//    counter.value = '';
//    for (var i = 0; i < values.length; i++) {
//        counter.value = counter.value + values[i] + ',';
//    }
//}

//function OnGridSelectionCompleteNotCurPosVeh(values) {
//    var counter = document.getElementById("hdnNotCurPosVeh");
//    counter.value = '';
//    var j = 1;
//    for (var i = 0; i < values.length; i++) {
//        if (values[i] != '') {
//            counter.value = counter.value + j + '. ' + values[i];
//            j++;
//        }
//    }
//}
//function OnGridSelectionCompleteLandMark(values) {
//    var counter = document.getElementById("hdnLandmarkvalues");
//    counter.value = '';
//    for (var i = 0; i < values.length; i++) {
//        counter.value = counter.value + values[i] + '~&~';
//    }
//    HidePopUp();
//}

function showProgress() {
    $find('ModalProcess').show();
    //processIndicator.style.display = 'block';
}
function hideProgress() {
    //$find('ModalProcess').show();
    processIndicator.style.display = 'none';
}

function popupShowHide(type, isShow) {
    if (type == 'AdvSearch') {
        document.getElementById('pnlSearch').style.display = (isShow == 1) ? "block" : "none";
    }
    else if (type == 'CmdMsg') {
        document.getElementById('pnlcmdMessage').style.display = (isShow == 1) ? "block" : "none";
    }
    else if (type == 'Alarm') {
        document.getElementById('pnlAlarm').style.display = (isShow == 1) ? "block" : "none";
    }
    else if (type == 'popupAlarm') {
        document.getElementById('pnlPopupAlarms').style.display = (isShow == 1) ? "block" : "none";
    }
    else if (type == 'popupHistory') {
        document.getElementById('pnlPopupHistory').style.display = (isShow == 1) ? "block" : "none";
    }
    else if (type == 'RptInterval') {
        document.getElementById('pnlRptInterval').style.display = (isShow == 1) ? "block" : "none";
    }

    else if (type == 'RptDuration') {
        document.getElementById('pnlRptDuration').style.display = (isShow == 1) ? "block" : "none";
    }
    else if (type == 'Live') {
        document.getElementById('pnlLiveTracking').style.display = (isShow == 1) ? "block" : "none";
    }
}

//15-Dec-2011 - Sakthi
function popUpAdvancedSearch() {
    popupShowHide('AdvSearch', 0);
    if (document.getElementById('tblAdvSearchGrid') != null) { document.getElementById('tblAdvSearchGrid').style.display = "none"; }
    if (document.getElementById('pnlSearchGrid') != null) { document.getElementById('pnlSearchGrid').style.display = "none"; }
    popupShowHide('AdvSearch', 1);
}

function setValue(boxID) {
}

//To hide the PopupExtender when ESC key is Pressed
function onKeyDown(e) {
    if (e && e.keyCode == Sys.UI.Key.esc) {
        // if the key pressed is the escape key, dismiss the dialog                    
        $find('ModalProcess').hide();
        $find('ModalAlarm').hide();
        $find('modalSearch').hide();
    }
}

function hide(modal) {
    $find(modal).hide();
}

function CheckGrid(alertMessage) {

    if (gvData.GetSelectedRowCount() == 0) {
        alert(alertMessage);
        load();
        return false;
    }
    else {
        return true;
    }
}

function AddMoveToTopItems(deviceID, checkboxName) {
    var holderControl = document.getElementById("hdnMoveToTop");
    var checkBoxitem = document.getElementById(checkboxName);
    var selectedItems = holderControl.value.split(",");
    if (!checkBoxitem.checked) {
        if (Array.contains(selectedItems, deviceID)) {
            Array.remove(selectedItems, deviceID);
        }
    }
    else {
        if (!Array.contains(selectedItems, deviceID)) {
            Array.add(selectedItems, deviceID);
        }
    }
    holderControl.value = selectedItems.join(",");
}
var fsgWindow;
function OpenFSG(fsgValWindow) {
    var fleetid = null;
    if (ddlFleet != null) {
        fleetid = ddlFleet.GetValueString();
    }
    if (fsgValWindow != null) {
        if (!fsgValWindow.closed) {
            fsgValWindow.focus();
            return fsgValWindow;
        }
    }
    fsgWindow = window.open(((fleetid != null && fleetid.length != 0) ? "ShowVehicles.aspx?fleetid=" + fleetid : "ShowVehicles.aspx"), 'Vehicles', 'scrollbars=yes,resizable=no,width=' +
            screen.width + ',height=' + screen.height, false);
    fsgWindow.moveTo(0, 0);
    return fsgWindow;
}

//To openMap in Fullscreen Mode
var FullMapWindow;
function FullscreenMap() {
    FullMapWindow = window.open("FullscreenMap.aspx?7JUeVv9wzyvaH1FI7OLcHA==", 'FullscreenMap', 'scrollbars=yes,resizable=no,width=' +
            screen.width + ',height=' + screen.height, false);
    FullMapWindow.moveTo(0, 0);
    FullMapWindow.focus();
}
function FullscreenMapAdv() {
    FullMapWindow = window.open("FullscreenMap_Advanced.aspx?7JUeVv9wzyvaH1FI7OLcHA==", 'FullscreenMap', 'scrollbars=yes,resizable=no,width=' +
            screen.width + ',height=' + screen.height, false);
    FullMapWindow.moveTo(0, 0);
    FullMapWindow.focus();
}


function fnCallDialog(urlString) {
    showModelessDialog(urlString, window, "status:false;dialogWidth:300px;dialogHeight:300px;");
}

function ShowAlarm(alarmid, vehName, DriverName, MobileNo, BoxID, AlarmDesc, DateTime, AlarmLevel, AlarmStatus, isAccept, isAnglerAdmin) {
    try {
        MobileNo = MobileNo == "" ? "-" : MobileNo;
        var isIE = CheckBrowser();
        popupShowHide('Alarm', 1);
        document.getElementById("txtAlertComments").value = '';
        document.getElementById("hdnAlarmID").value = alarmid;
        if (isIE) {
            document.getElementById("lblVehicleName").innerText = vehName;
            document.getElementById("lblBoxID").innerText = BoxID;
            document.getElementById("lblDate").innerText = DateTime;
            document.getElementById("lblAlarmSeveritySettings").innerText = AlarmLevel;
            document.getElementById("lblAlarmSatusDesc").innerText = AlarmStatus;
            document.getElementById("lblAlarmDesc").innerText = AlarmDesc;
            if (document.getElementById("lblMobileNo") != null)
                document.getElementById("lblMobileNo").innerText = MobileNo;
            if (document.getElementById("lblDriverName") != null)
                document.getElementById("lblDriverName").innerText = DriverName;
        } else {
            document.getElementById("lblVehicleName").textContent = vehName;
            document.getElementById("lblBoxID").textContent = BoxID;
            document.getElementById("lblDate").textContent = DateTime;
            document.getElementById("lblAlarmSeveritySettings").textContent = AlarmLevel;
            document.getElementById("lblAlarmSatusDesc").textContent = AlarmStatus;
            document.getElementById("lblAlarmDesc").textContent = AlarmDesc;
            if (document.getElementById("lblMobileNo") != null)
                document.getElementById("lblMobileNo").textContent = MobileNo;
            if (document.getElementById("lblDriverName") != null)
                document.getElementById("lblDriverName").textContent = DriverName;
        }
        document.getElementById('trDeviceIDHolder').style.display = (isAnglerAdmin == 0) ? 'none' : 'block'; //(isIE ? 'block' : 'table-row');

        if (isAccept == '1') {
            document.getElementById('btnAccept').style.display = 'block';
            document.getElementById('btnAccept').parentNode.style.display = 'inline';
            document.getElementById('btnClose').parentNode.style.display = 'block';
            document.getElementById('btnClose').parentNode.style.display = 'inline';
        }
        else if (isAccept == '2') {
            document.getElementById('btnAccept').style.display = 'block';
            document.getElementById('btnAccept').parentNode.style.display = 'inline';
            document.getElementById('btnAccept').focus();
            document.getElementById('btnClose').parentNode.style.display = 'none';
            document.getElementById('btnClose').parentNode.style.display = 'none';

        }
        else if (isAccept == '3') {
            document.getElementById('btnAccept').style.display = 'none';
            document.getElementById('btnAccept').parentNode.style.display = 'none';
            document.getElementById('btnClose').parentNode.style.display = 'block';
            document.getElementById('btnClose').parentNode.style.display = 'inline';
            document.getElementById('btnClose').focus();
        }
        //        if (isAccept == '1') {
        //            document.getElementById('btnAccept').style.display = 'none';
        //            document.getElementById('btnAccept').parentNode.style.display = 'none';
        //        }
        //        else {
        //            document.getElementById('btnAccept').style.display = 'block';
        //            document.getElementById('btnAccept').parentNode.style.display = 'inline';
        //            document.getElementById('btnAccept').focus();
        //        }
    }
    catch (Error) {
        alert(Error);
    }
}

//To clear landmarks
function ClearLandmarkContents() {
    document.getElementById('pnlLandmark').style.display = 'none';
    document.getElementById('txtLocationName').value = '';
    document.getElementById('txtLocationDesc').value = '';
    document.getElementById('txtRadius').value = '';
    document.getElementById('txtstreet').value = '';
    document.getElementById('ddlCategory').selectedIndex = 0;
    return false;
}

function ClearGeozoneContents() {
    document.getElementById('pnlGeozone').style.display = 'none';
    document.getElementById('txtGeozoneName').value = '';
    document.getElementById('txtGeozoneDescription').value = '';
    document.getElementById('txtGeozoneRadius').value = '';
    document.getElementById('ddlGeozoneSeverity').selectedIndex = 0;
    document.getElementById('ddlGeozoneDirection').selectedIndex = 0;
    document.getElementById('ddlGeozoneCategory').selectedIndex = 0;
    return false;
}

function ClearLiveTrackingContents() {
    document.getElementById('pnlLiveTracking').style.display = 'none';
    document.getElementById('txtRptInterval').value = '';
    document.getElementById('txtRptDuration').value = '';
    document.getElementById('ddlRptInterval').selectedIndex = 0;
    document.getElementById('ddlRptDuration').selectedIndex = 0;
    document.getElementById('ddlRptOperation').selectedIndex = 0;
    return false;
}


//F5 Refresh Detection
function onPostBack() {
    var y = generateRandomSequence();
    var hdnGuid = document.getElementById("hdnGuid");
    hdnGuid.value = y;
}
function generateRandomSequence() {
    var g = "";
    for (var i = 0; i < 32; i++)
        g += Math.floor(Math.random() * 0xF).toString(0xF)
    return g;
}