$hst = jQuery.noConflict();

// Alerts
var selectVehicle = "No Equipment selected !";
var waitForLoading = "Please wait it is loading !";

// Set the alert messages from the Resource File
function SetAlertMessages(selectVehicleAlert, waitForLoadingAlert) {
    selectVehicle = selectVehicleAlert;
    waitForLoading = waitForLoadingAlert;
}

function PerformScrollSetting() {
    SetDivPosition();
    document.getElementById("gvHistory_DXMainTable").parentNode.onscroll = function (event) {
        var intY = document.getElementById("gvHistory_DXMainTable").parentNode.scrollTop;
        document.getElementById('hdnScrollPosition').value = intY;
    };

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

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

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 SetExportHiddenValue() {
    document.getElementById("hdnExport").value = "1";
}

function OnGridSelectionChanged() {
    gvHistory.GetSelectedFieldValues('MessageInID', OnGridSelectionComplete);
}

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

function Validate(type) {
    try {
     
        if (document.getElementById("ddlVehicle").value == '- Select -') {
            TrinetraCustomAlerts(selectVehicle);
            ddlVehicle.SetFocus()//ddlVehicle.focus();
            return false;
        }
        //if (ddlVehicle.GetSelectedItem().text == '- Select -') {

        //}

        if (type == 'V') {
            //if (CheckStartAndEnd()) { return true; } else { return false; }

        }
        //ShowPopUp();
        return true;
    }
    catch (Error) {
        TrinetraCustomAlerts(waitForLoading);
        return false;
    }
}
//function ShowPopUp() {
//    if ($find('mpeProgressIndicator') != undefined) {
//        $find('mpeProgressIndicator').show();
//    }
//}

function CheckOrUnCheck(isChecked, chkboxName) {
    var checkboxtobeProcessed = document.getElementById(chkboxName);
    if (isChecked == '0') {
        checkboxtobeProcessed.checked = false;
    }
    else {
        checkboxtobeProcessed.checked = true;
    }
}

function CheckMapItBoxes() {
    try {
        var checkBoxElements = document.getElementsByTagName('input');
        var checkBoxArray = document.getElementById("hdnMapItCheckBoxes").value.split(',');
        var mapItItemst = document.getElementById('hdnAlarmIDs').value.split(',');
        Array.remove(mapItItemst, "");
        var isIE = CheckBrowser();
        for (var iterator = 0; iterator < checkBoxElements.length; iterator++) {
            if (checkBoxElements[iterator].type == 'checkbox') {
                //        alert( checkBoxElements[iterator].attributes["onclick"] + "    "  + checkBoxElements[iterator].getAttribute("onclick"));
                var attributeValue = isIE ?
             checkBoxElements[iterator].attributes["onclick"].value :
              checkBoxElements[iterator].getAttribute("onclick")
                if (attributeValue != null) {
                    if (Array.contains(mapItItemst, attributeValue.toString().substring(9, 47))) {
                        //alert('hey');
                        checkBoxElements[iterator].checked = true;
                        checkBoxElements[iterator].parentNode.parentNode.parentNode.className = 'dxgvSelectedRow_Office2003_Blue'
                    }
                    else {
                        checkBoxElements[iterator].checked = false;
                    }
                }
                //            if (Array.contains(checkBoxArray, checkBoxElements[iterator].id)) {
                //                checkBoxElements[iterator].checked = true;
                //            }
                //            else {
                //                checkBoxElements[iterator].checked = false;
                //            }
            }
        }
    }
    catch (ex) {
    }
}

function ResetValue() {
    document.getElementById("hdnAlarmIDs").value = '';
    document.getElementById("hdnMapItCheckBoxes").value = '';
    document.getElementById('hdnMapItValue').value = '';
}
// To get the selected checkbox values in Hidden Field
function setValue(checkedValue, chk) {
    var counter = document.getElementById("hdnAlarmIDs");
    var mapItCheckBoxes = document.getElementById("hdnMapItCheckBoxes");
    var arrCounter = counter.value.split(',');
    var checkBoxArray = mapItCheckBoxes.value.split(',');
    if (chk.checked == true) {
        chk.parentNode.parentNode.parentNode.className = 'dxgvSelectedRow_Office2003_Blue'
        if (!Array.contains(arrCounter, "'" + checkedValue + "'")) {
            Array.add(arrCounter, "'" + checkedValue + "'")
        }
        if (!Array.contains(checkBoxArray, chk.id)) {
            Array.add(checkBoxArray, chk.id)
        }
    }
    else {
        chk.parentNode.parentNode.parentNode.className = "dxgvDataRow_Office2003_Blue " +
        (eval(chk.parentNode.parentNode.id.toString().substring(chk.parentNode.parentNode.id.toString().length - 1)) % 2 ?
        "dxgvDataRowAlt_Office2003_Blue" : "dxgvDataRow_Office2003_Blue");
        if (Array.contains(arrCounter, "'" + checkedValue + "'")) {
            Array.remove(arrCounter, "'" + checkedValue + "'");
        }
        if (Array.contains(checkBoxArray, chk.id)) {
            Array.remove(checkBoxArray, chk.id);
        }
    }
    mapItCheckBoxes.value = checkBoxArray.join(',');
    //            alert(document.getElementById("hdnMapItCheckBoxes").value);
    counter.value = arrCounter.join(',')
    //            alert(document.getElementById("hdnAlarmIDs").value);
    document.getElementById("hdnAlarmIDs").value = counter.value;
}

function loadjscssfile(filename, filetype) {
    if (filetype == "js") { //if filename is a external JavaScript file
        var fileref = document.createElement('script')
        fileref.setAttribute("type", "text/javascript")
        fileref.setAttribute("src", filename)
    }
    else if (filetype == "css") { //if filename is an external CSS file
        var fileref = document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    if (typeof fileref != "undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref)
}

function TrendValidate() {
    var ParamList = document.getElementById("lstParams");
    var ddlvehicle = document.getElementById("ddlVehicle");
    var isError = false;
    var alertMessage = '';
    try {
        if (ddlvehicle.value == "") {
            alertMessage += (alertMessage.length > 0 ? '</br>' : '') + 'Please select the Device.';
            isError = true;
        }
        if (ParamList.value == "") {
            alertMessage += (alertMessage.length > 0 ? '</br>' : '') + 'Must select at least one Parameter.';
            isError = true;
        }
        if (isError) {
            //HidePopUp();
            TrinetraCustomAlerts(alertMessage);
            return false;
        }
        else {
            ShowPopUp();
        }
        return true;

    } catch (e) {

    }
}