﻿var gridViewCtlId = null;
var gridViewCtl = null;
var curSelRow = null;
var textBoxId = null;
var textBoxValueId = null;
var popupId = null;
var isEvent = false;
var localityTextId = null;
var localityValId = null;


function ShowLookUp(textBox, popup, gridview, isEventExe, localityText, SearchTextId) {
    popupId = popup;
    gridViewCtlId = gridview;
    if (curSelRow != null) {
        curSelRow.className = 'Row';
    }
    curSelRow = null;
    gridViewCtl = null;
    $find(popupId).show();
    textBoxId = document.getElementById(textBox);
    textBoxValueId = document.getElementById(textBoxId.id + '_Val');
    if(localityText){
        localityTextId = document.getElementById(localityText);
        localityValId = document.getElementById(localityTextId.id + '_Val');
    }
    if (SearchTextId) 
    {
        $('#' + SearchTextId).focus();
    }
    isEvent = isEventExe;
    return false;
}


function ShowLocalityLookUp(textBox, popup, gridview, isEventExe, errorLabel) {
    popupId = popup;
    gridViewCtlId = gridview;
    if (curSelRow != null) {
        curSelRow.className = 'Row';
    }
    curSelRow = null;
    gridViewCtl = null;
    textBoxId = document.getElementById(textBox);
    $('#' + textBoxId.id).fireEvent("change");
    $('#'+gridview)[0].innerText = '';
    if($('#'+errorLabel)[0]){
        $('#'+errorLabel)[0].innerText = '';
    }
    $find(popupId).show();
   
    textBoxValueId = document.getElementById(textBoxId.id + '_Val');
    isEvent = isEventExe;
    return false;
}

function getGridViewControl() {
    if (null == gridViewCtl) {
        gridViewCtl = document.getElementById(gridViewCtlId);
    }
}

function onGridViewRowSelected(rowIdx) {
    var selRow = getSelectedRow(rowIdx);
    if (curSelRow != null) {
        curSelRow.className = 'Row';
    }

    if (null != selRow) {
        curSelRow = selRow;
        curSelRow.className = 'SelectedRow';
    }
    return false;
}


function onGridViewLocalityRowSelected(rowIdx) {
    var selRow = getSelectedRow(rowIdx);
    if (curSelRow != null) {
        curSelRow.className = 'Row';
    }

    if (null != selRow) {
        curSelRow = selRow;
        curSelRow.className = 'SelectedRow';
    }
}

function getSelectedRow(rowIdx) {
    return getGridRow(rowIdx);
}

function getGridRow(rowIdx) {
    getGridViewControl();
    if (null != gridViewCtl) {
        return gridViewCtl.rows[rowIdx];
    }
    return null;
}

function getGridColumn(rowIdx, colIdx) {
    var gridRow = getGridRow(rowIdx);
    if (null != gridRow) {
        return gridRow.cells[colIdx];
    }
    return null;
}

function getCellValue(rowIdx, colIdx) {
    var gridCell = getGridColumn(rowIdx, colIdx);
    if (null != gridCell) {
        return gridCell.innerHTML.replace(/^\s*/, "").replace(/\s*$/, "");
    }
    return null;
}


$.fn.fireEvent = function(eventType) {
    return this.each(function() {
        if (document.createEvent) {
            var event = document.createEvent("HTMLEvents");
            event.initEvent(eventType, true, true);
            return !this.dispatchEvent(event);
        } else {
            var event = document.createEventObject();
            return this.fireEvent("on" + eventType, event)
        }
    });
};


function addCity() {
    if (curSelRow != null) {
        $('#' + textBoxValueId.id).val(getCellValue(curSelRow.rowIndex, 0));
        $('#' + textBoxId.id).val(getCellValue(curSelRow.rowIndex, 1));
        
        if(localityTextId){
            $('#' + localityTextId.id)[0].innerText = 'Select Locality';
            $('#' + localityValId.id)[0].innerText = '';
        }
        
        $find(popupId).hide();

    }
    else {
        alert("Please select a row.");
    }
    return false;
}


function addLocality() {
    if (curSelRow != null) {
        $('#' + textBoxValueId.id).val(getCellValue(curSelRow.rowIndex, 0));
        $('#' + textBoxId.id).val(getCellValue(curSelRow.rowIndex, 1));
        $find(popupId).hide();
        if (isEvent) {
            $('#' + textBoxId.id).fireEvent("change");
        }

    }
    else {
        alert("Please select a row.");
    }
    return false;
}


function ChangePage() {
    curSelRow = null;
    gridViewCtl = null;
}

function SetValue() {
    textBoxId.value = 'Hi';
}

function SetDefaultButton(e,controlid, buttonid) {
    if (e.keyCode == 13) {
        e.returnValue = false;
        e.cancel = true;
        $("#" + controlid).blur();
        $("#" + buttonid).click();
    }
}
