var PW_SID = "11337728";
var PW_TENANT_PORTAL_URL = "https://www.propertyware.com/pw/portals/access/tenant.action";
var PW_TENANT_APPLICATION_URL = 'https://www.propertyware.com/pw/portals/access/tenantApplication.action?systemID='+PW_SID;
var PW_WEBSITE_ACTION = 'https://www.propertyware.com/pw/marketing/website.do';
var PW_WEBSITE_ACTION_RETURN_URL = 'http://access.propertyware.com/confirmation.html';


function getPublishedBuildings() {
    presetSearchForm();
    outputBuildings(result.buildings, 'http://www.propertyware.com');
}

function presetSearchForm() {
    document.forms['searchListings'].city.value = getURLParam('city');
    document.forms['searchListings'].zip.value = getURLParam('zip');
    document.forms['searchListings'].state.value = getURLParam('state');
    document.forms['searchListings'].neighborhood.value = getURLParam('neighborhood');
    document.forms['searchListings'].min.value = getURLParam('min');
    document.forms['searchListings'].max.value = getURLParam('max');
    document.forms['searchListings'].bedrooms.value = getURLParam('bedrooms');
    document.forms['searchListings'].bathrooms.value = getURLParam('bathrooms');
}

function gotoTenantPortal() {
    document.location.href = PW_TENANT_PORTAL_URL;
}

function gotoTenantApplication() {
    document.location.href = PW_TENANT_APPLICATION_URL;
}

function gotoBuildingPage(buildingID) {
    document.location.href = 'building.aspx?bid='+buildingID;
}

function contactAgent(unitID) {
    document.location.href = 'contactAgent.aspx?uid='+unitID;
}

function apply(unitID) {
    document.location.href = PW_TENANT_APPLICATION_URL + '&unitID='+unitID;
}

function outputValue( value ) {
    document.write(value?value:"");
}

function outputFeaturedImage(imgElementId, image) {
    var oImage = document.getElementById(imgElementId);
    if (oImage != null && image != null) {
        oImage.src = 'http://www.propertyware.com/pw/marketing/imageService.do?iid='+image;
    }
}

function outputBuildingDetail(containerId, building) {
    if (building != null) {
        if (building.residential == "false" || building.rentable == "false") {
            var oRentableResidentialTBody = document.getElementById('rentableResidentialTBody');
            if (oRentableResidentialTBody != null) {
                oRentableResidentialTBody.parentNode.removeChild(oRentableResidentialTBody);
            }
        }
        if (building.rentable == "false") {
            var oRentableTBody = document.getElementById('rentableTBody');
            if (oRentableTBody != null) {
                oRentableTBody.parentNode.removeChild(oRentableTBody);
            }
        }
        if (building.neighborhood == "") {
            var oNeighborhoodTBody = document.getElementById('neighborhoodTBody');
            if (oNeighborhoodTBody != null) {
                oNeighborhoodTBody.parentNode.removeChild(oRentableTBody);
            }
        }
    }
}

function outputUnitDetail(containerId, unit) {
    if (unit != null) {
        if (unit.residential == "false") {
            var oRentableResidentialTBody = document.getElementById('rentableResidentialTBody');
            if (oRentableResidentialTBody != null) {
                oRentableResidentialTBody.parentNode.removeChild(oRentableResidentialTBody);
            }
        }
    }
}

var viewer = new PhotoViewer();
function outputGallery(containerId, images) {
    var oDiv = document.getElementById(containerId);
    if (images != null && images.length > 0 ) {
        for (i = 0; i < images.length; i++) {
            var image = images[i];
            outputImage(oDiv, image, i);
            viewer.add("http://www.propertyware.com/pw/marketing/imageService.do?iid="+image.originalID);
        }
        
    } else if (oDiv != null && oDiv.parentNode != null) {
        oDiv.parentNode.removeChild(oDiv);
    }
}

function outputImage(oParent, image, index) {
    if (oParent != null && image != null) {
        var oImgDiv = document.createElement("div");
        oImgDiv.setAttribute("class", "Image");
        oImgDiv.setAttribute("id", "thumbnail_" + index);
        oParent.appendChild(oImgDiv);

        var oThumbDiv = document.createElement("div");
        oThumbDiv.setAttribute("class", "Thumbnail");
        oImgDiv.appendChild(oThumbDiv)

        var oImg = document.createElement("img");
        oImg.setAttribute("src", "http://www.propertyware.com/pw/marketing/imageService.do?iid="+image.smallID);
        oImg.setAttribute("id", "thumbnailIMG_" + index);
        oThumbDiv.appendChild(oImg);

        var oImgZoomDiv = document.createElement("div");
        oImgZoomDiv.setAttribute("class", "ImageLink");
        oImgDiv.appendChild(oImgZoomDiv)
        oImgZoomDiv.innerHTML = '<a class="ImageLink" href="javascript:void(viewer.show(0))">Zoom</a>';

    }
}

function outputOtherTenantCharges(containerId, otherTenantCharges) {
    var oDiv = document.getElementById(containerId);
    if (oDiv != null && otherTenantCharges != null && otherTenantCharges != '' ) {
        var oPara = document.createElement("p");
        oPara.appendChild(document.createTextNode(otherTenantCharges));
        oDiv.appendChild(oPara);
        oDiv.appendChild(document.createElement("br"))
    }   else if (oDiv != null && oDiv.parentNode != null) {
        oDiv.parentNode.removeChild(oDiv);
    }
}

function outputLeasingContact(containerId, leasingContact) {
    var oDiv = document.getElementById(containerId);
    if (oDiv != null && leasingContact != null && leasingContact != '' ) {

    }   else if (oDiv != null && oDiv.parentNode != null) {
        oDiv.parentNode.removeChild(oDiv);
    }
}

function outputAmenities(containerId, amenities) {
    var oDiv = document.getElementById(containerId);
    if (amenities != null && amenities.length ) {
        var oUL = document.createElement('ul');
        oUL.setAttribute("class","AmenityList");
        for (i = 0; i < amenities.length; i++) {
            var amenity = amenities[i];
            var oLI = document.createElement("li");
            oLI.appendChild(document.createTextNode(amenity.name));
            oUL.appendChild(oLI);
        }
        oDiv.appendChild(oUL);
    }  else if (oDiv != null && oDiv.parentNode != null) {
        oDiv.parentNode.removeChild(oDiv);
    }
}

function outputDescription(containerId, description) {
    var oDiv = document.getElementById(containerId);
    if (description != null && description != '') {
        var oPara = document.createElement("p");
        oPara.appendChild(document.createTextNode(description));
        oDiv.appendChild(oPara);
        oDiv.appendChild(document.createElement("br"))
    } else if (oDiv != null && oDiv.parentNode != null) {
        oDiv.parentNode.removeChild(oDiv);
    }
}

function outputBuildingUnits(unitListTableId, units) {
    var oTable = document.getElementById(unitListTableId);
    if (oTable != null && units != null && units.length > 0) {
        var oTBody = oTable.tBodies[0];
        if (oTBody == null) {
            oTBody = document.createElement("tbody");
            oTable.appendChild(oTBody);
        }
        for (i = 0; i < units.length; i++) {
            var unit = units[i];

            var oRow = document.createElement("tr");
            oTBody.appendChild(oRow);

            var oCell1 = document.createElement("td");
            oRow.appendChild(oCell1);
            //oCell1.appendChild(document.createTextNode(unit.marketingName));
            oCell1.innerHTML = '<a class="link" href="unit.aspx?uid='+unit.id+'">'+unit.marketingName+'</a>';

            var oCell2 = document.createElement("td");
            oRow.appendChild(oCell2);
            oCell2.appendChild(document.createTextNode(unit.type));

            var oCell3 = document.createElement("td");
            oRow.appendChild(oCell3);
            oCell3.appendChild(document.createTextNode(unit.totalArea));

            var oCell4 = document.createElement("td");
            oRow.appendChild(oCell4);
            oCell4.appendChild(document.createTextNode(unit.targetRentDescription));

            var oCell5 = document.createElement("td");
            oRow.appendChild(oCell5);
            oCell5.appendChild(document.createTextNode(unit.targetDeposit));

        }

    }
}

function setCommunityInfoZip(elementId, zip) {
    var oCommunityInfoDiv = document.getElementById(elementId);
    if (oCommunityInfoDiv != null) {
        oCommunityInfoDiv.innerHTML = oCommunityInfoDiv.innerHTML.replace(/ZIP/g, zip)
    }
}

