﻿function ExtendDataPoint(containerName, dataPoints) {
    initLength = dataPoints.LineItems.length
    
    //If there is only one record eliminate the 2nd set of columns
    if (initLength == 1) {
        dataPoints.ColumnDefs.splice(2,2)
    } else {
        //more than 1 record splice the dataset
        for (i = initLength - 1; i > -1; i--) {
            if (i % 2 != 0) {
                var LineItemToSaveTo = dataPoints.LineItems[i - 1];
                var LineItemToRemove = dataPoints.LineItems[i];

                LineItemToSaveTo.CommunityNameRight = LineItemToRemove.CommunityName
                LineItemToSaveTo.linkRight = LineItemToRemove.link
                LineItemToSaveTo.LowSquareFeetRight = LineItemToRemove.LowSquareFeet
                LineItemToSaveTo.HighSquareFeetRight = LineItemToRemove.HighSquareFeet
                LineItemToSaveTo.LowPriceRangeRight = LineItemToRemove.LowPriceRange
                LineItemToSaveTo.FeaturesRight = LineItemToRemove.Features
                LineItemToSaveTo.CommunityIDRight = LineItemToRemove.CommunityID
                LineItemToSaveTo.ImgURLRight = LineItemToRemove.ImgURL
                LineItemToSaveTo.PhotoHeightRight = LineItemToRemove.PhotoHeight
                LineItemToSaveTo.PhotoWidthRight = LineItemToRemove.PhotoWidth
                LineItemToSaveTo.PhotoTitleRight = LineItemToRemove.PhotoTitle
                LineItemToSaveTo.CommunityLocationRight = LineItemToRemove.CommunityLocation
                dataPoints.LineItems.splice(i, 1)
            }
        }
    }
    
    return dataPoints
}

function CommunityCell(elCell, oRecord, oColumn, sData) {

    if (oRecord._nCount == 0) {
        CountToDisplay = 1;
    } else {
        CountToDisplay = (oRecord._nCount * 2) + 1;
    }

    var DisplayCounter = "<h6>" + CountToDisplay + ") </h6>";
    var CommunityURL = createURL(oRecord, oColumn, sData);

    var Features = oRecord.getData("Features")
    var CommunityLocation = oRecord.getData("CommunityLocation")

    if (Features == null) { Features = ""; }

    var LowSqFt = oRecord.getData("LowSquareFeet")
    var HighSqFt = oRecord.getData("HighSquareFeet")
    var LowPrRange = oRecord.getData("LowPriceRange")
    var SquareFeet = "";
    SquareFeet = GetSQFTMarkup(LowSqFt, HighSqFt)

    var PriceRange = parsePriceRange(LowPrRange)

    AllLIs = CombineIntoLIs(CommunityLocation, Features, SquareFeet, PriceRange)

    var CommunityURLSquareFeet = "<div>" + CommunityURL + AllLIs + "</div>";

    elCell.innerHTML = DisplayCounter + CommunityURLSquareFeet
}

function CombineIntoLIs(ActCityName, Features, Sqft, PriceRange) {
    var markupUL = "<ul>"
    markupUL += "<li>" + PriceRange + "</li>"
    markupUL += "<li>" + ActCityName + "</li>"

    if (Features != '') {
        myRegExp = /<ul>|<\/ul>/g 
        Features = Features.replace(myRegExp, "")
        markupUL += Features
    }

    myRegExp = /<p>|<\/p>/g
    markupUL += "<li>" + Sqft.replace(myRegExp, "") + "</li>"
    
    
    markupUL += "</ul>"
    
    return markupUL
}


function CommunityCellRight(elCell, oRecord, oColumn, sData) {
    if (oRecord.getData("CommunityNameRight") != null) {

        if (oRecord._nCount == 0) {
            CountToDisplay = 2;
        } else {
            CountToDisplay = (oRecord._nCount * 2) + 2;
        }

        var DisplayCounter = "<h6>" + CountToDisplay + ") </h6>";
        var CommunityURL = createURLString(oRecord.getData("linkRight"), oRecord.getData("CommunityNameRight"));
        var Features = oRecord.getData("FeaturesRight")
        var CommunityLocation = oRecord.getData("CommunityLocationRight")
        
        if (Features == null) { Features = ""; }
        var LowPrRange = oRecord.getData("LowPriceRangeRight")
        var SquareFeet = "";
        SquareFeet = GetSQFTMarkup(oRecord.getData("LowSquareFeetRight"), oRecord.getData("HighSquareFeetRight"))

        var PriceRange = parsePriceRange(LowPrRange)

        AllLIs = CombineIntoLIs(CommunityLocation, Features, SquareFeet, PriceRange)

        var CommunityURLSquareFeet = "<div>" + CommunityURL + AllLIs + "</div>";

        elCell.innerHTML = DisplayCounter + CommunityURLSquareFeet
    }

}

function CommunityCellImgURLRight(elCell, oRecord, oColumn, sData) {
    if (oRecord.getData("ImgURLRight") != null) {

        var PictureURL = "";

        PictureURL = GetImgTag(oRecord.getData("ImgURLRight"), oRecord.getData("PhotoTitleRight"), oRecord.getData("PhotoHeightRight"), oRecord.getData("PhotoWidthRight"))

        var CommunityURLWithPicture = createURLString(oRecord.getData("linkRight"), PictureURL);

        elCell.innerHTML = CommunityURLWithPicture;
    }
}

function CommunityCellImgURL(elCell, oRecord, oColumn, sData) {
    var PictureURL = "";

    PictureURL = MakeImgTag(oRecord)

    var CommunityURLWithPicture = createURL(oRecord, oColumn, PictureURL);

    elCell.innerHTML = CommunityURLWithPicture;
}