﻿// JScript File
var TimerMS = 500; // value in milliseconds
var TimerObj = null;
var SelectedRow = null;
var oldBackgroundColor;
var rowCount = 0;
var CurrentProperty = '0';
var PropertyIDHighlighted = '0';
var varCheckedPropsID = 'ctl00_h_varCheckedProps';

function ToggleCheckBox(chk, propId) {
    var varCheckedProps = document.getElementById(varCheckedPropsID);
    varCheckedProps.value = varCheckedProps.value.replace(propId + ',', '');
    if (chk.checked == true) {
        varCheckedProps.value = varCheckedProps.value + propId + ',';
    }
    return false;
}
function RowClicked(event) {
    if (event.target.tagName != "INPUT") {
        var row = $(this);
        var propertyid = this.attributes['propertyid'].value
        RowHightlightProperty(propertyid, true, false);
    }
}
function RowHightlightProperty(propertyID, showProperty, scrollToProp) {
    var tr = $("tr[propertyid=" + propertyID + "]")[0];
    if (tr) {
        RowHighlight(tr, showProperty, scrollToProp)
    }
}

// set a timer to process matches when there's a pause in keystrokes
function RowHighlight(ob, showProperty, scrollToProp) {
    //Unhighlight Currently Selected Row

    // TODO make this function name a parameter the host page sets, since the host page defines it

    var arr = document.getElementById('grid_container').getElementsByTagName('tr'); // ctl00_ContentPlaceHolder1_CustomGridView1_GridView1
    for (var n = 0; arr && n < arr.length; n++) {
        var attr = arr[n].getAttribute('propertynumber');
        if (attr && attr == CurrentProperty) {
            arr[n].style.backgroundColor = 'white';
        }
    }

    oldBackgroundColor = ob.style.backgroundColor;
    
    CurrentProperty = ob.getAttribute('propertynumber')
    PropertyIDHighlighted = ob.getAttribute('propertyid');
    SetPushpinHighlight(ob.getAttribute('propertynumber'), showProperty);
    ob.style.backgroundColor = '#CCCCCC';
    //       $('.PropertyContextDetails').remove();
    //         req2 = GetRequestObject();
    //        if (req2) {
    //            req2.onreadystatechange = function() {
    //                if (req2.readyState == 4) {
    //                    //$('#HighlightRowDetailsMain').html(req2.responseText);
    //                    $(ob).after(req2.responseText);
    //       
    //                }
    //            }

    //            req2.open("get", 'PropertyContext.aspx?propertyid=' + PropertyIDHighlighted, true);
    //            req2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    //            req2.send(null);

    //        } else {
    //            alert('Cannot load XMLHTTP object');
    //        }

    if (scrollToProp) {
        var propertyStartPixel = parseInt(CurrentProperty) * 20;
        if (CurrentProperty > 50)
            propertyStartPixel = propertyStartPixel + 20;
        if (CurrentProperty > 100)
            propertyStartPixel = propertyStartPixel + 120;
        if (CurrentProperty > 150)
            propertyStartPixel = propertyStartPixel + 120;
        document.getElementById('scrollDiv').scrollTop = propertyStartPixel - 40;


        /*
        if ( CurrentProperty == 1 ) 
        document.getElementById('scrollDiv').scrollTop = 1;
        else if ( CurrentProperty < 50 )
        document.getElementById('scrollDiv').scrollTop = parseInt(CurrentProperty) * 10;
        else if ( CurrentProperty < 75 )
        document.getElementById('scrollDiv').scrollTop = parseInt(CurrentProperty) * 15;
        else if ( CurrentProperty < 100 )
        document.getElementById('scrollDiv').scrollTop = parseInt(CurrentProperty) * 17;
        else if ( CurrentProperty < 175 )
        document.getElementById('scrollDiv').scrollTop = parseInt(CurrentProperty) * 18;
        else if ( CurrentProperty < 200 )
        document.getElementById('scrollDiv').scrollTop = parseInt(CurrentProperty) * 24;
        else
        document.getElementById('scrollDiv').scrollTop = parseInt(CurrentProperty) * 32;*/
    }


    // if there's a timer, clear it
    if (TimerObj != null) {
        window.clearTimeout(TimerObj);
    }
    // set the timer
    // var fnct = 'intInDiv(' + ob.propertynumber + ');intInDiv2(' + ob.propertynumber + ')';
    // TimerObj = window.setTimeout(fnct, TimerMS);
}

function RowLeave(ob) {

    return;
    // change row style back to original no matter what
    //ob.style.backgroundColor=oldBackgroundColor;
    ob.style.backgroundColor = 'white';



    // TODO make this function name a parameter the host page sets, since the host page defines it
    SetPushpinHighlight(ob.getAttribute('propertynumber'), false);

    // if there's a timer, clear it
    if (TimerObj != null) {
        window.clearTimeout(TimerObj);
    }
    //remove the selected property from page
    //PropertyContext_NoMsg();
}

////////////////////////////////////////////////////////////////////////////////////
// Opens a print preview page in a seperate window from which the user can print the grid
function PrintButton_onclick() {

    window.print();
    return false;

}

function ExportButton_OnClick() {
    //Get element by name

    //If properties is greater than max in app.config, prompt user to email
    if (rowCount > MaxPropertySearchGridExport) {
        try {
            var answer = confirm('Export Properties is limited to ' + MaxPropertySearchGridExport + ' records from the website.  Would you like to email a request for Export?');
            if (answer)
                window.open(PadZingMailTo + '?subject=Request for PadZing Export Greater Than 300 Properties');

            return false;
        }
        catch (err) {
            alert(err);
            return false;
        }
    }
    else {
        window.open('ExportResults.aspx', 'mywindow', 'width=600,height=600,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
        return false;
    }
}

function hideSummary(objDiv) {
    var summary = $('#spnSearchSummary');
    var imgSearchSummary = $('#imgSearchSummary');
    var tblSearchSummary = $('#tblSearchSummary');
    if (objDiv.title == 'Collapse') {
        objDiv.title = 'Expand';
        summary.text('Expand');
        imgSearchSummary.attr("src", "Images/rpcIconDownArrow.gif");
        tblSearchSummary.hide();
        ResizeTabPanel();
    } else if (objDiv.title == 'Expand') {
        objDiv.title = 'Collapse';
        summary.text('Collapse');
        imgSearchSummary.attr("src", "Images/rpcIconUpArrow.gif");
        tblSearchSummary.show();
        ResizeTabPanel();
    }
}

// Todo: Don't think that this is needed anymore. Delete. C. Golob
//function hideResults(objDiv) {
//    if (objDiv.title == 'Collapse') {
//        objDiv.title = 'Expand';
//        document.getElementById('spnSearchResults').innerText = 'Expand';
//        document.getElementById('imgSearchResults').src = 'Images/rpcIconDownArrow.gif';
//        iterateThroughTableAndHideRows('ctl00_ContentPlaceHolder1_CustomGridView1_GridView1', false);
//    } else if (objDiv.title == 'Expand') {
//        objDiv.title = 'Collapse';
//        document.getElementById('spnSearchResults').innerText = 'Collapse';
//        document.getElementById('imgSearchResults').src = 'Images/rpcIconUpArrow.gif';
//        iterateThroughTableAndHideRows('ctl00_ContentPlaceHolder1_CustomGridView1_GridView1', true);
//    }
//}