﻿
// ####################################
// ########## Sold Listing Object ##########
// ####################################

function SoldListing() {
    this.id = null;
    this.VEShapeLayer = _soldLayer; 
    this.VEShape = null;
    this.Address = null;
	this.City = null;
    this.State = null;
    this.PostalCode = null;
    this.CountyFipsCode = null;
    this.ParcelNumber = null;
    this.Latitude = null;
    this.Longitude = null;
    this.MatchCode = null;
    this.Distance = null;
    this.ExactMatch = null;
    this.OnBoardId = null;
    this.ListingType = null;
    this.Bedrooms = null;
    this.Bathrooms = null;
    this.Area = null;
    this.SalePrice = null;
    this.SaleDate = null;
    this.YearBuilt = null;
    this.DocumentNumber = null;
    this.AbsenteeIndicator = null;
    this.RecordingDate = null;
    this.CalendarDate = null;
    this.DisclosureFlag = null;
    this.TransactionType = null;
    this.SalesCode = null;
    this.AddedDate = null;
    this.numberOfDaysSold = null;
    this.numberOfDaysInSystem = null;
}

SoldListing.prototype.Populate = function(jsonObject) {

    this.id = jsonObject.id;
    this.Latitude = jsonObject.latitude;
    this.Longitude = jsonObject.longitude;
    this.Address = jsonObject.addressLine1;
    this.City = jsonObject.city;
    this.State = jsonObject.state;
    this.PostalCode = jsonObject.zip5;
    if ((jsonObject.zipPlus4 !== undefined) && (jsonObject.zipPlus4 !== null) && (jsonObject.zipPlus4 !== '')) {
        this.PostalCode += '-' + jsonObject.zipPlus4;
    }
    this.CountyFipsCode = jsonObject.fipsCounty;
    this.ParcelNumber = null; // Not provided by by HJ
    this.MatchCode = null; // Not provided by by HJ
    this.Distance = null; // Not provided by by HJ
    this.ExactMatch = null; // Not provided by by HJ
    this.OnBoardId = null; // Not provided by by HJ
    this.ListingType = jsonObject.propertyType;
    this.Bedrooms = jsonObject.bedroomCnt;
    this.Bathrooms = jsonObject.bathroomCnt;
    this.Area = jsonObject.homeSizeSqFt;
    this.SalePrice = jsonObject.purchasePrice;

    this.SaleDate = jsonObject.recordingDate; // Not provided by by HJ
    this.YearBuilt = jsonObject.builtYear;
    this.DocumentNumber = null; // Not provided by by HJ
    this.AbsenteeIndicator = null; // Not provided by by HJ
    this.RecordingDate = jsonObject.recordingDate;
    this.CalendarDate = null; // Not provided by HJ
    this.DisclosureFlag = null; // Not provided by by HJ
    this.TransactionType = jsonObject.transactionType;
    this.SalesCode = jsonObject.salesCode;
    this.AddedDate = null; // Not provided by by HJ
    this.numberOfDaysSold = null; // Not provided by by HJ
    this.numberOfDaysInSystem = null; // Not provided by by HJ
}
SoldListing.prototype.DisplayShapeArray = function(array) 
{
     this.VEShapeLayer.AddShape(array);
}

SoldListing.prototype.FormatCurrency = function(num) {
    num = parseFloat(num.toString().replace(/\$|\+\-\,/g, ''));
    if (isNaN(num)) { num = 0; }
    return ('$' + addCommas(num.toFixed(2))); // addCommas() from utility.js
}

SoldListing.prototype.GetShape = function() {
    this.VEShape = new VEShape(VEShapeType.Pushpin, new VELatLong(this.Latitude, this.Longitude));
    this.VEShape.SetTitle("--Sold Listing--");

    var descrip = '<div style="margin: 4px;"><p>';
    if (this.ListingType != null) {
        descrip += this.ListingType + '<br />';
    }
    if ((this.Address !== null) && (this.City !== null) && (this.State !== null) && (this.PostalCode !== null)) {
        descrip += this.Address + '<br />';
        descrip += this.City + ', ' + this.State + ' ' + this.PostalCode;
    } else {
        descrip += 'No address provided';
    }
    descrip += '</p>';

    descrip += '<p>';
    if (this.SalesCode !== null) {
        descrip += 'Closing Price: ';
        if (this.SalePrice != null) {
            descrip += this.FormatCurrency(parseFloat(this.SalePrice)).replace(/\.[0-9][0-9]/, '');
        } else {
            descrip += '-- unavailable --';
        }
        descrip += "<br />";
    }

    descrip += 'Recording Date: ';
    if (this.RecordingDate != null) {
        var lds = this.RecordingDate.split('-');
        descrip += lds[1] + '/' + lds[2] + '/' + lds[0]; 
    } else {
        descrip += 'unavailable';
    }
    descrip += "</p>";

    descrip += '<p>';
    if ((this.Bedrooms !== null) && !isNaN(parseFloat(this.Bedrooms))) {
        descrip += this.Bedrooms + ' Bedrooms';
        if ((this.Bathrooms !== null) && !isNaN(parseFloat(this.Bathrooms))) {
            descrip += ', ' + this.Bathrooms + ' Bathrooms';
        }
        descrip += '<br />';
    } else {
        if ((this.Bathrooms !== null) && !isNaN(parseFloat(this.Bathrooms))) {
            descrip += this.Bathrooms + ' Bathrooms<br />';
        }
    }


    descrip += 'Year Built: ';
    if ((this.YearBuilt !== null) && (!isNaN(parseInt(this.YearBuilt)))) {
        descrip += parseInt(this.YearBuilt);
    } else {
        descrip += 'unknown';
    }
    descrip += '<br />';

    if (this.SalesCode !== null) {
        descrip += 'Price Shown: ' + getSalesCodeDesc(this.SalesCode);
    }
    descrip += '</p>';

    descrip += '<div style="position:absolute;bottom:3px;right:3px;font-size:.7em;">Information provided by Home Junction.</div>';
    descrip += "</div>";

    this.VEShape.SetDescription(descrip);
    this.VEShape.SetCustomIcon("../App_Themes/Default/Images/Map/icon_sold.gif");

    return this.VEShape;
}
