﻿

// Action: Pops up navLink in a new window
function WebMenu_PopupLink(navLink) {
    if (navLink == "" || navLink == null) return;
    try {
        window.open(navLink, 'popup_link');
    }
    catch (ex) {
    }
}




// grid: WebDataGrid
// Returns: Active Cell
function getWebGridActiveCell(grid) {
    try {
        if (grid == null) return;
        var behav = grid.get_behaviors();
        var activation = behav.get_activation();
        var activeCell = activation.get_activeCell();
        return activeCell;
    }
    catch (ex) {

    }
}

// grid: WebDataGrid
// Returns: Active Row
function getWebGridActiveRow(grid) {
    try {
        if (grid == null) return;
        var activeCell = getWebGridActiveCell(grid);
        var activeRow = activeCell.get_row();
        return activeRow;
    }
    catch (ex) {

    }
}

function getWebGridRow(grid, iRow) {
    try {
        if (grid == null) return;

        var rows = grid.get_rows();
        if (rows.get_length() > iRow)
            return rows.get_row(iRow);
    }
    catch (ex) {

    }
}

function getWebGridActiveOrFirstRowCellText(grid, iIndex) {
    try {
        if (grid == null) return;
        var activeRow = getWebGridActiveOrFirstRow(grid);
        if (activeRow == null) return "";

        return activeRow.get_cell(iIndex).get_text();
    }
    catch (ex) {
        return "";
    }
}


function getWebGridActiveRowCellText(grid, iIndex) {
    try {
        if (grid == null) return;
        var activeRow = getWebGridActiveRow(grid);
        if (activeRow == null) return "";

        return activeRow.get_cell(iIndex).get_text();
    }
    catch (ex) {
        return "";
    }
}

function getWebGridRowCellText(grid, iRow, iIndex) {
    try {
        if (grid == null) return;
        var activeRow = getWebGridRow(grid, iRow);
        if (activeRow == null) return "";

        return activeRow.get_cell(iIndex).get_text();
    }
    catch (ex) {
        return "";
    }
}


// grid: WebDataGrid
// Returns: Active Row
function getWebGridActiveOrFirstRow(grid) {
    try {
        if (grid == null) return;
        var activeRow = getWebGridActiveRow(grid);
        if (!activeRow)
            activeRow = getWebGridRow(grid, 0);
        return activeRow;
    }
    catch (ex) {

    }
}


// grid: WebDataGrid
// Returns: Collection of selected rows
function getWebGridSelectedRows(grid) {
    try {
        if (grid == null) return;
        var behav = grid.get_behaviors();
        var selection = behav.get_selection();
        return selection.get_selectedRows();
    }
    catch (ex) {

    }
}

function ClearWebGridSelectedRows(grid) {
    var selectedRows = getWebGridSelectedRows(grid);

    for (var i = selectedRows.get_length(); i >= 0; i--) {
        selectedRows.remove(selectedRows.getItem(i));
    }
}

function SetWebGridActiveRow(grid, row, iColIndex) {
    try {
        if (grid == null) return;
        var behav = grid.get_behaviors();
        var activation = behav.get_activation();
        var selection = behav.get_selection();
        var selectedRows = getWebGridSelectedRows(grid);

        activation.set_activeCell(row.get_cell(iColIndex), false);
        selectedRows.add(row);
    }
    catch (ex) {

    }
}


// grid: WebDataGrid
// iColIndex: Column to get values from
// Returns: "|" delimited list of values in the iColIndex column of the selected rows
function getWebGridSelectedRows_CellValues(grid, iColIndex) {
    try {
        var rows = getWebGridSelectedRows(grid);
        if (rows == null) return "";
        if (rows.get_length() == 0) return "";

        var strCellValues = "";
        for (var i = 0; i < rows.get_length(); i++) {
            if (strCellValues != "")
                strCellValues = strCellValues + ";";
            strCellValues = strCellValues + rows.getItem(i).get_cell(iColIndex).get_text();
        }
        
        return strCellValues;
    }
    catch (ex) {

    }
}


function SetGridFocus(grid, iColIndex) {
    var activeCell = getWebGridActiveCell(grid);
    if (!activeCell) {
        var firstRow = getWebGridRow(grid, 0);
        if (firstRow) {
            activeCell = firstRow.get_cell(iColIndex);
            SetWebGridActiveRow(grid, firstRow, iColIndex);
        }
    }

    if (activeCell) {
        var oElement = activeCell.get_element();
        oElement.focus();
    }
}


// grid: WebDataGrid
// iQSIndex: Index of Permit column
// Action: Redirects to Permit/Complaint in selected row
function GotoPermitDetails(grid, iQSIndex) {
    try {
        if (grid == null) return;
        /*var behav = grid.get_behaviors();
        var activation = behav.get_activation();
        var activeCell = activation.get_activeCell();
        var activeRow = activeCell.get_row();*/
        var activeRow = getWebGridActiveRow(grid);
        if (activeRow == null) return;

        var strPermitNum = activeRow.get_cell(iQSIndex).get_text();
        if (strPermitNum == "" || strPermitNum == null) return;
        
        if (strPermitNum.startsWith("@")) {
            window.location = "Complaint_Details.aspx?Complaint=" + strPermitNum;
        }
        else {
            window.location = "Permit_Details.aspx?Permit=" + strPermitNum;
        }
    }
    catch (ex) {

    }
}


// grid: WebDataGrid
// iTypeIndex: Index of Plan type column
// iNumIndex: Index of Plan # column
// Action: Redirects to Plan in selected row
function GotoPlanDetails(grid, iTypeIndex, iNumIndex) {
    var strDetailURL = "Plan_Details.aspx?Plan="

    try {
        /*var behav = grid.get_behaviors();
        var activation = behav.get_activation();
        var activeCell = activation.get_activeCell();
        activeRow = activeCell.get_row();*/
        var activeRow = getWebGridActiveRow(grid);
        if (activeRow == null) return;

        window.location = strDetailURL + activeRow.get_cell(iTypeIndex).get_text() + activeRow.get_cell(iNumIndex).get_text();
    }
    catch (ex) {

    }
}



// grid: WebDataGrid
// iTypeIndex: Index of Contractor name column (not encoded, this function will encode)
// Action: Redirects to Contractor in selected row
function GotoContractorDetails(grid, iQSIndex) {
    try {
        var activeRow = getWebGridActiveOrFirstRow(grid);
        if (activeRow == null) return;

        window.location = "ContractorDetails.aspx?name=" + Url.encode(activeRow.get_cell(iQSIndex).get_text());
    }
    catch (ex) {

    }
}


function TrimAndTrailingSpace(strInput) {
    strInput = strInput.trim();
    if (strInput!="")
        strInput = strInput + " ";

    return strInput;
}


function GotoPermitSearchResults(strAddressNum, strStreetDir, strStreetName, strStreetType, strCity, strState, strZipcode) {
    if (strState == "")
        strState = "CO";

    var strURL = "PermitSearchResults.aspx?StLo=" + strAddressNum + "&StHi=" + strAddressNum + "&StDir=" + strStreetDir + "&StName=" + strStreetName + "&StType=" + strStreetType + "&SearchType=Permits";

    if (strURL != "")
        window.location = strURL;
}


function PopupMapWindow(strMapKey, strAddressNum, strStreetDir, strStreetName , strStreetType, strCity, strState, strZipcode, strParcel) {
    if (strState=="")
        strState="CO";

    var strFullAddress = TrimAndTrailingSpace(strAddressNum) + TrimAndTrailingSpace(strStreetDir) + (strStreetName + " " + strStreetType).trim() + ", " + strCity + ", " + TrimAndTrailingSpace(strState) + strZipcode
    var strURL;
    switch (strMapKey.toLowerCase()) {
        case "Map_Google".toLowerCase():
            strURL = "http://maps.google.com/maps?q=" + Url.encode(strFullAddress);
            break;
        case "Map_GoogleDir".toLowerCase():
            strURL = "InspectorMapSearch.aspx?type=GOOGLEDIR&address=" + Url.encode(strFullAddress);
            break;
        case "Map_GoogleFind".toLowerCase():
            strURL = "InspectorMapSearch.aspx?type=GOOGLEFIND";
            break;
        case "Map_Yahoo".toLowerCase():
            strURL = "http://maps.yahoo.com/index.php?q1=" + Url.encode(strFullAddress);
            break;
        case "Map_Bing".toLowerCase():
            strURL = "http://www.bing.com/maps/default.aspx?where1=" + Url.encode(strFullAddress);
            break;
        case "Map_MapQuest".toLowerCase():
            var AddressLine1 = TrimAndTrailingSpace(strAddressNum) + TrimAndTrailingSpace(strStreetDir) + (strStreetName + " " + strStreetType).trim();
            strURL = "http://www.mapquest.com/maps/map.adp?searchtype=address&country=US&address=" + Url.encode(AddressLine1);
            strURL = strURL + "&city=" + Url.encode(strCity) + "&state=" + Url.encode(strState) + "&zipcode=" + Url.encode(strZipcode)
            break;
        case "Map_EPC".toLowerCase():
            if (strParcel.length == 10) {
                strURL = "http://gis2.asr.elpasoco.com/?Sched=" + strParcel;
            }
            else {
                strURL = "http://land.elpasoco.com/SearchResults.aspx?from=" + strAddressNum.trim() + "&to=" + strAddressNum.trim() + "&street=" + strStreetName
            }
            break;
        default:
    }
 
 if (strURL!="")
     OpenPopupWindow(strURL);

}




