﻿//Query Builder client-side stuff

function QueryBuilder_TriggerFieldChangeCallback() { 
    $get('QueryBuilder_StoredArgument').value = 'RepopulateSamples';
    __doPostBack('Search_UP', '');
}

function SearchTab_DisableButtons() {
    //Disable all search tab buttons until both update panels are done updating
    $get('Find_ExecuteButton').disabled = true;
    //Test for the existence of 1 Query Builder button...if absent, then the QB panel is not enabled for this user...
    var qbButton = $get('QB_btnRunQueryBuilder');
    if (qbButton) QueryBuilder_DisableButtons();
    //Disable the button on the Locate Lot by Number tab
    LocateLotByLotNum_DisableButtons();
}

function LocateLotByLotNum_DisableButtons() {
    $get('LotByLotNum_ExecuteButton').disabled = true;
}

function QueryBuilder_DisableButtons() {
    //Disable some buttons first
    //There is no need for code to re-enable them...they reset to enabled when the Update Panel updates.
    $get('QB_btnAnd').disabled = true;
    $get('QB_btnOr').disabled = true;
    $get('QB_btnNot').disabled = true;
    $get('QB_btnLeftParen').disabled = true;
    $get('QB_btnRightParen').disabled = true;
    $get('QB_btnAddToQueryString').disabled = true;
    $get('QB_btnRunQueryBuilder').disabled = true;
    $get('QB_btnClear').disabled = true;
}

function UpdateSearchPanel() {
    $get('QueryBuilder_StoredArgument').value = 'FillFieldList';
    __doPostBack('Search_UP', '');
}

//function LaunchQueryBuilder() {
    //    $get('QueryBuilder_StoredArgument').value = 'RunQuery';
    //    __doPostBack('Search_UP', ''); 
//}

function QueryBuilder_ChooseSampleValue() {
    var sampleValue = $get('QB_SampleValuesList').options[$get('QB_SampleValuesList').selectedIndex].value;
    $get('QB_txtValue').value = sampleValue;
}

function QueryBuilder_ConstructQueryString() {
    var queryField = $get('QB_FieldList').options[$get('QB_FieldList').selectedIndex].value;
    var queryOperator = $get('QB_OperatorList').options[$get('QB_OperatorList').selectedIndex].value;
    var queryValue = $get('QB_txtValue').value;

    var QueryStringBox = $get('QB_QueryString');
    if (QueryStringBox) {
        var existingQuery = QueryStringBox.value;
        existingQuery = existingQuery + queryField + " " + queryOperator + " " + queryValue;
        QueryStringBox.value = existingQuery;
    }
}

function QueryBuilder_AddOperatorToQuery(operatorToAdd) {
    var QueryStringBox = $get('QB_QueryString');
    if (QueryStringBox) {
        var existingQuery = QueryStringBox.value;
        if ((operatorToAdd=='And') || (operatorToAdd=='Or') || (operatorToAdd=='Not')) { 
            existingQuery = existingQuery + " " + operatorToAdd + " ";
        } else {
            existingQuery = existingQuery + operatorToAdd;
        }
        QueryStringBox.value = existingQuery;
    }
}

function QueryBuilder_ClearQueryString() {
   var QueryStringBox = $get('QB_QueryString');
   if (QueryStringBox) {
       QueryStringBox.value = ""; 
   }
}

//Zoom to Scale client-side manipulation (code that doesn't interact with web control itself). The control contains it's own embedded jscript code that is meant to work with it.
function toggleZoomControl() {
    var ZoomDiv = $get('ZoomToScalePanel');
    var imgZoom = $get('imgshowzoom');
    if (ZoomDiv) {
        if (ZoomDiv.style.display == 'inline') {
            //close the zoom control panel
            ZoomDiv.style.display = "none";
            if (imgZoom) {
                imgZoom.src = "Images/expand.gif";
                imgZoom.alt = "ShowZoom";
            }
        } else {
            //open the zoom panel
            ZoomDiv.style.display = "inline";
            if (imgZoom) {
                imgZoom.src = "Images/collapse.gif";
                imgZoom.alt = "HideZoom";
            }
        }
    }
}

//Report Generator code
function LaunchPipeReport(ReportType) {
    var newwin;
    if(ReportType == 'Pipe') {
        var SubdivCode = $get('rptPipe_SubdivisionList').options[$get('rptPipe_SubdivisionList').selectedIndex].value;
        var SectionCode = $get('rptPipe_SectionList').options[$get('rptPipe_SectionList').selectedIndex].value;
        
        if (SubdivCode != '---' && SectionCode != '---') newwin = open("PipeReport.aspx?SUB=" + SubdivCode + "&SECT=" + SectionCode, "ReportPopup", "resizable=yes, scrollbars=yes, width=800, height=600, toolbar=yes, menubar=yes");
    } else if(ReportType == 'PipeQuan') {
        var SubdivCode = $get('rptPipeQuan_SubdivisionList').options[$get('rptPipeQuan_SubdivisionList').selectedIndex].value;
        var SectionCode = $get('rptPipeQuan_SectionList').options[$get('rptPipeQuan_SectionList').selectedIndex].value;

        if (SubdivCode != '---' && SectionCode != '---') newwin = open("PipeQuantityReport.aspx?SUB=" + SubdivCode + "&SECT=" + SectionCode, "ReportPopup", "resizable=yes, scrollbars=yes, width=800, height=600, toolbar=yes, menubar=yes");
    } else if (ReportType == 'MeasureDown') {
    var SubdivCode = $get('rptMeasureDown_SubdivisionList').options[$get('rptMeasureDown_SubdivisionList').selectedIndex].value;
    var SectionCode = $get('rptMeasureDown_SectionList').options[$get('rptMeasureDown_SectionList').selectedIndex].value;

    if (SubdivCode != '---' && SectionCode != '---') newwin = open("MeasureDownReport.aspx?SUB=" + SubdivCode + "&SECT=" + SectionCode, "ReportPopup", "resizable=yes, scrollbars=yes, width=800, height=600, toolbar=yes, menubar=yes");
    } else if (ReportType == 'Lateral') {
    var SubdivCode = $get('rptLateral_SubdivisionList').options[$get('rptLateral_SubdivisionList').selectedIndex].value;
    var SectionCode = $get('rptLateral_SectionList').options[$get('rptLateral_SectionList').selectedIndex].value;

    if (SubdivCode != '---' && SectionCode != '---') newwin = open("LateralReport.aspx?SUB=" + SubdivCode + "&SECT=" + SectionCode, "ReportPopup", "resizable=yes, scrollbars=yes, width=800, height=600, toolbar=yes, menubar=yes");
    }

}

//Link to the main PR editing page listing all PRs
function LaunchPRSummary() {
    var PDwin;
    PDwin = open("PRSummary.asp?PID=217&REFRESH=TRUE", "HSEComPopup", "resizable=yes, scrollbars=yes, width=800, height=600, toolbar=yes, menubar=yes");
    return false;
}

//link from the map definition for the "Edit PR" link of the HSE Com layer to the UpdatePR page to deal with the HSE Com in question.
function EditHSECom(HseComCode) {
    var PDwin;
    PDwin = open("UpdateForm.asp?ID=" + HseComCode, "HSEComPopup", "resizable=yes, scrollbars=yes, width=800, height=600, toolbar=yes, menubar=yes");
}

//"Loading" icon manipulation: requires Ajax
function pleasewait() {
    document.getElementById("imgAjaxSpinner").style.display = '';
    setTimeout("AnimateGif('imgAjaxSpinner','images/loading.gif');", 50);

    return true;
}

function AnimateGif(ctrl, imgsrc) {
    var img = document.getElementById(ctrl);
    img.src = imgsrc;
}

function waitingover() {
    document.getElementById("imgAjaxSpinner").style.display = 'none';
}

//Code to attach the Loading... image to the progress event of the map...displays the icon whenever the map is refreshing (zoom in, zoom out, pan, etc)...
function mapInit() {
    var map1 = $find('Map1');
    map1.add_onProgress(customProgress);
}

function MapMouseUpHandler() {
    var map1 = $find('Map1');
    if (map1.__activeToolMode) {
        //Most tools do not register as the map's activeTool...but the select tools do
        //The other tools (buffer) will be have to be handled differently
        if (map1.__activeToolMode == 'SelectPoint') pleasewait();
        if (map1.__activeToolMode == 'SelectLine') pleasewait();
        if (map1.__activeToolMode == 'SelectRectangle') pleasewait();
        if (map1.__activeToolMode == 'SelectPoly') pleasewait();
    }
}

function customProgress(sender, pendingTiles) {
    if (sender != null) {
        //window.status = "Pending tiles: " + pendingTiles;
        if (pendingTiles > 0) {
            // showLayer and hideLayer are part of the Web ADF JS library
            // in the display_common.js	            
            pleasewait();
        } else {
            waitingover();
        }
    }
}
