
var selectedListingIDs;
var nonGeocodedListingIDs;
enNGReportControlIncluded = true;
var selectedCount = 0;

function reportCheckBoxChange(currentCheckBox)
{
	var listingID = 0;
	var index = 0;

	index = syncCheckBoxes(currentCheckBox);

	if (!selectedListingIDs)
	{
		selectedListingIDs = new Array();
	}

	listingID = eval(currentCheckBox.value);

	if (currentCheckBox.checked)
	{
		selectedListingIDs[index] = listingID;
		selectedCount++;
		
        listingData = LNJS.Page.SearchMgr.lData.items[index];
    
        if (listingData != null)
        {
            if ((typeof(listingData.lat) == 'undefined') &&
                (typeof(listingData.lon) == 'undefined'))
            {
                if (!nonGeocodedListingIDs)
                {
                    nonGeocodedListingIDs = new Array();
                }
                
                nonGeocodedListingIDs[index] = listingData.lid;
            }
            else if ((listingData.lat == 0) &&
                (listingData.lon == 0))
            {
                if (!nonGeocodedListingIDs)
                {
                    nonGeocodedListingIDs = new Array();
                }
                
                nonGeocodedListingIDs[index] = listingData.lid;
            }
        }
	}
	else
	{
		var selectAllCheckBox = document.getElementById('llRH-SelectAll');
		
		if (selectAllCheckBox)
		{
			selectAllCheckBox.checked = false;
		}
		
		selectedListingIDs[index] = null;
		delete selectedListingIDs[index];
		selectedCount--;
		
		if (nonGeocodedListingIDs)
		{
		    nonGeocodedListingIDs[index] = null;
		    delete nonGeocodedListingIDs[index];
		}
		
		if (selectedCount == 0)
		{
		    selectedListingIDs = new Array();
		}
	}
	
	updateSelectedCount();
}

function syncCheckBoxes(currentCheckBox)
{
	var pchk = null;
	var rchk = null;

	var index = currentCheckBox.id.replace(/rchk|pchk/, '');

	if (currentCheckBox.id.match('rchk'))
	{
		rchk = currentCheckBox;
	    
		pchk = document.getElementById('pchk'+index);
	    
		if (pchk)
		{
			pchk.checked = currentCheckBox.checked;
		}
	}
	else if (currentCheckBox.id.match('pchk'))
	{
		pchk = currentCheckBox;
	    
		rchk = document.getElementById(currentCheckBox.id.replace('pchk', 'rchk'));
	    
		if (rchk)
		{
			rchk.checked = currentCheckBox.checked;
		}
	}

	return index;
}

function updateSelectedCount()
{
	var listingsCount = document.getElementById("selectedListingsCount");

	if ((listingsCount) &&
	    (typeof(showSelectedCount) == 'undefined') ||
	    ((typeof(showSelectedCount) != 'undefined') &&
	        (showSelectedCount)))
	{
	    if ((typeof(showSelectedText) != 'undefined') &&
	        (showSelectedText == true))
	    {
	        listingsCount.innerHTML = (
			    "<b>" +
			    selectedCount +
			    " Selected" +
			    "</b>");
	    }
	    else
	    {
		    listingsCount.innerHTML = (
			    "<b>" +
			    selectedCount +
			    "</b>");
		}
			    
		if ((LNJS.Page.SearchMgr.lData != null) &&
		    (selectedCount == LNJS.Page.SearchMgr.lData.resultCount))
		{
			var selectAllCheckBox = document.getElementById('llRH-SelectAll');
			
			if (selectAllCheckBox != null)
			{
				selectAllCheckBox.checked = true;
			}
		}
	}
}

function selectAll(selectAllCheckBox)
{
	var startIndex = LNJS.Page.SearchMgr.pageStartIndex;
	var pageSize = LNJS.Page.SearchMgr.pageSize;
	var resultsCount = LNJS.Page.SearchMgr.lData.resultCount;
	var endIndex = Math.min((pageSize + startIndex), resultsCount);

	for (var i = startIndex; i < endIndex; i++)
	{
		var checkBox = document.getElementById('rchk'+i);
		
		if (checkBox != null)
		{
			checkBox.checked = selectAllCheckBox.checked;
		}
	}
	
	var listingsCount = document.getElementById("selectedListingsCount");
	
	selectedListingIDs = new Array();
	
	nonGeocodedListingIDs = new Array();
	
	if (selectAllCheckBox.checked)
	{
		for (var i = 0; i < resultsCount; i++)
		{
		    listingData = LNJS.Page.SearchMgr.lData.items[i];
		    
			selectedListingIDs[i] = listingData.lid;
			
			if ((typeof(listingData.lat) == 'undefined') &&
                (typeof(listingData.lon) == 'undefined'))
            {
                nonGeocodedListingIDs[i] = listingData.lid;
            }
            else if ((listingData.lat == 0) &&
                (listingData.lon == 0))
            {
		        nonGeocodedListingIDs[i] = listingData.lid;
		    }
		}
		
		selectedCount = resultsCount;
	}
	else
	{
		selectedListingIDs.length=selectedCount = 0;
	}
	
	updateSelectedCount();
	
	if (Element.visible(LNJS.Page.SearchMgr.popup))
	{
		var pinChkBox = document.getElementById('pchk' + LNJS.Page.SearchMgr.pinIndex);
		
		pinChkBox.checked = selectAllCheckBox.checked;
	}
}

function isSelectedListing(listingID)
{
	if ((selectedListingIDs != null) && 
	    (selectedListingIDs.length > 0))
	{
		for (var i = 0; i < selectedListingIDs.length; i++)
		{
			if (selectedListingIDs[i] == listingID)
			{
				return true;
			}
		}
	}
	
	return false;
}

function preserveSelections()
{
	if (selectedListingIDs)
	{
		var resultCount = LNJS.Page.SearchMgr.lData.resultCount;
		var sSelectedListingIDs = selectedListingIDs.join(',');
	    
		selectedListingIDs = new Array();
		selectedCount = 0;
	    
		for (var i = 0; i < resultCount; i++)
		{
			var lid = LNJS.Page.SearchMgr.lData.items[i].lid;
			var slid = lid.toString();
			if (sSelectedListingIDs.match(slid))
			{
				selectedListingIDs[i] = lid;
				selectedCount++;

				var currentCheckBox = document.getElementById('rchk' + i);

				if (currentCheckBox)
				{
					currentCheckBox.checked = true;
					syncCheckBoxes(currentCheckBox);
				}
			}
		}
		updateSelectedCount();
	}
}

function createReport(
    reportType, 
    selectedListingIDs, 
    nonGeocodedListingIDs)
{
    if ((selectedListingIDs == null) ||
        (selectedListingIDs.length < 1))
    {
        alert('Please select at least one property to create a report.');
        return;
    }
            
    if ((reportType == null) || 
        (reportType == ''))
    {
        return;
    }
    
    var sNewSelectedListings = '';
    
    for (i = 0; i < selectedListingIDs.length; i++)
    {
        var isNonGeocodedListing = false;
        
        if ((nonGeocodedListingIDs != null) &&
            (nonGeocodedListingIDs.length > 0) &&
            (reportType == 'MAP'))
        {
            for (var j = 0; j < nonGeocodedListingIDs.length; j++)
            {
                if (nonGeocodedListingIDs[j] == selectedListingIDs[i])
                {
                    isNonGeocodedListing = true;
                    break;
                }
            }
        }
        
        if (!isNonGeocodedListing)
        {
            if (sNewSelectedListings + '' == '')
            {
                if ((selectedListingIDs[i]) &&
                    (selectedListingIDs[i] != ''))
                {
                    sNewSelectedListings += selectedListingIDs[i];
                }
            }
            else
            {
                if ((selectedListingIDs[i]) &&
                    (selectedListingIDs[i] != ''))
                {
                    sNewSelectedListings += ',' + selectedListingIDs[i];
                }
            }
        }
    }
    
    sNewSelArray = sNewSelectedListings.split(',');
    
    if ((sNewSelArray.length < 1) ||
        (sNewSelectedListings == ''))
    {
        if ((reportType == 'MAP') &&
            (nonGeocodedListingIDs != null) &&
            (nonGeocodedListingIDs.length > 0))
        {
            alert('Please select at least one property to create a report.  Some listings may not be mappable.');
        }
        else
        {
            alert('Please select at least one property to create a report.');
        }
        
        return;
    }
    
    if ((sNewSelArray.length > 200) && 
        (reportType == 'LI'))
    {
        alert('You have exceeded the maximum number of properties that can be included in a Line Item report. Please select no more than 200 properties at one time.');
        return;
    }
    
    if ((sNewSelArray.length > 200) && 
        (reportType == 'SU'))
    {
        alert('You have exceeded the maximum number of properties that can be included in a Summary report. Please select no more than 200 properties at one time.');
        return;
    }
    
    if ((sNewSelArray.length > 50) && 
        (reportType == 'DE'))
    {
        alert('You have exceeded the maximum number of properties that can be included in a Detailed report. Please select no more than 50 properties at one time.');
        return;
    }
    
    if ((sNewSelArray.length > 25) && 
        (reportType == 'MAP'))
    {
        alert('You have exceeded the maximum number of properties that can be included in a Property Mapping report. Please select no more than 25 properties at one time.');
        return;
    }
    
    makeReportPopup(
        '/xNet/LoopLink/reports/reports.aspx?ListingType=' + 
        LNJS.Page.SearchMgr.searchType + 
        '&ll=true&listings=' + 
        sNewSelectedListings + 
        '&ReportType=' + 
        reportType + 
        '&STID=' +
        document.getElementById('name').value +
        '&SearchID=' + 
        searchID +
        '&CurDisp=' + UOMMoneyCurrency + '&UOMDisp=' + UOMListing + '&RentPer=' + sCurRentShowAs + '&SearchResultID=' + searchID);
}