var PW_SID = "18808838";
var PW_TENANT_PORTAL_URL = "https://www.propertyware.com/pw/portals/homelocators/tenant.action";
var PW_TENANT_APPLICATION_URL = 'https://www.propertyware.com/pw/portals/homelocators/tenantApplication.action?systemID='+PW_SID;
var PW_WEBSITE_ACTION = 'https://www.propertyware.com/pw/marketing/website.do';
var PW_WEBSITE_ACTION_RETURN_URL = 'http://homelocators.propertyware.com/confirmation.html';
var startItem = 1;
var endItem = 20;
var forSale = false;

function submitSearchListings() {
    document.forms['searchListings'].sid.value = PW_SID;
    document.forms['searchListings'].startItem.value = 1;
    document.forms['searchListings'].endItem.value = 20;
    document.forms['searchListings'].submit();
}
function setItemRange(){
    forSale = false;
    startItem = (getURLParam('startItem') == ""? 1: getURLParam('startItem'));
    endItem = (getURLParam('endItem') == ""? 20: getURLParam('endItem'));

}
function setForSale(){
    forSale = true;
}

function setBuildingUnitForSale(){
    forSale = getURLParam('forSale');
    if(forSale){
        var rentBack = document.getElementById('rentBack');
        if (rentBack != null) {
            rentBack.parentNode.removeChild(rentBack);
        }
    } else {
        var saleBack = document.getElementById('saleBack');
        if (saleBack != null) {
            saleBack.parentNode.removeChild(saleBack);
        }
    }
}

function getListings() {
    var queryString = "";
    var tokens = document.location.href.split("?");
    if (tokens.length > 1) {
        queryString = "?action=q&sid="+PW_SID+"&"+tokens[1];        
    }
    var url = PW_WEBSITE_ACTION+queryString;
    //document.write(url);
    document.write('<scri' + 'pt type="text/javascript" language="javascript" src="'+url+'"></scr' +'ipt>');
}

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 getPublishedSaleBuildings() {
    outputBuildings(result.buildings, 'http://www.propertyware.com');
}

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.html?bid='+buildingID+(getURLParam('forSale') == 'true'?'&forSale=true':'');
}

function contactAgent(unitID) {
    document.location.href = 'contact_agent.html?uid='+unitID+(getURLParam('forSale') == 'true'?'&forSale=Yes':'');
}

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) {
    forSale = getURLParam('forSale');
    if (building != null) {
        if (forSale || building.residential == "false" || building.rentable == "false") {
            var oRentableResidentialTBody = document.getElementById('rentableResidentialTBody');
            if (oRentableResidentialTBody != null) {
                oRentableResidentialTBody.parentNode.removeChild(oRentableResidentialTBody);
            }
        }
        if (forSale || building.rentable == "false") {
            var oRentableTBody = document.getElementById('rentableTBody');
            if (oRentableTBody != null) {
                oRentableTBody.parentNode.removeChild(oRentableTBody);
            }
        }
        if (!forSale || building.forSale == 'false' || building.publishedForSale == 'false') {
            var forSaleTBody = document.getElementById('forSaleTBody');
            if (forSaleTBody != null) {
                forSaleTBody.parentNode.removeChild(forSaleTBody);
            }
        }
        if (building.neighborhood == "") {
            var oNeighborhoodTBody = document.getElementById('neighborhoodTBody');
            if (oNeighborhoodTBody != null) {
                oNeighborhoodTBody.parentNode.removeChild(oNeighborhoodTBody);
            }
        }
    }
}

function outputUnitDetail(containerId, unit) {
    forSale = getURLParam('forSale');
    if (unit != null) {
        if (forSale) {
            var oRentableTBody = document.getElementById('rentableTBody');
            if (oRentableTBody != null) {
                oRentableTBody.parentNode.removeChild(oRentableTBody);
            }
        } else {
            var forSaleTBody = document.getElementById('forSaleTBody');
            if (forSaleTBody != null) {
                forSaleTBody.parentNode.removeChild(forSaleTBody);
            }
        }
        if (forSale || 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('+index+'))">Zoom</a>';

    }
}

function outputOtherTenantCharges(containerId, otherTenantCharges) {
    forSale = getURLParam("forSale");
    var oDiv = document.getElementById(containerId);
    if(oDiv != null && forSale){
       oDiv.parentNode.removeChild(oDiv);
    } else 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) {
    forSale = getURLParam('forSale');
    if(forSale){
        var unitListRentTHead = document.getElementById('unitListRentTHead');
        if (unitListRentTHead != null) {
            unitListRentTHead.parentNode.removeChild(unitListRentTHead);
        }
    } else {
        var unitListSaleTHead = document.getElementById('unitListSaleTHead');
        if (unitListSaleTHead != null) {
            unitListSaleTHead.parentNode.removeChild(unitListSaleTHead);
        }
    }
    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);
        }
		var unitCount = 0;
        for (i = 0; i < units.length; i++) {
            var unit = units[i];
            if(forSale && unit.forSale == 'true' && unit.publishedForSale == 'true'){
				var oRows = document.createElement("tr");
                oTBody.appendChild(oRows);

                var oCell1s = document.createElement("td");
                oRows.appendChild(oCell1s);

                oCell1s.innerHTML = '<a class="link" href="unit.html?forSale=true&uid='+unit.id+'">'+unit.marketingName+'</a>';

                var oCell2s = document.createElement("td");
                oRows.appendChild(oCell2s);
                oCell2s.appendChild(document.createTextNode(unit.type));

                var oCell3s = document.createElement("td");
                oRows.appendChild(oCell3s);
                oCell3s.appendChild(document.createTextNode(unit.totalArea));

                var oCell4s = document.createElement("td");
                oRows.appendChild(oCell4s);
                oCell4s.appendChild(document.createTextNode(unit.salePrice));

                var oCell5s = document.createElement("td");
                oRows.appendChild(oCell5s);
                oCell5s.appendChild(document.createTextNode(unit.mlsNumber));

                unitCount++;

            } else if(!forSale && unit.publishedForRent == 'true'){
				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.html?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));

				unitCount++;
			}
        }

		if(unitCount == 0){
            var unitsTable = document.getElementById('dataSection-Units');
            if (unitsTable != null) {
                unitsTable.parentNode.removeChild(unitsTable);
            }
        }

    }
}

function setCommunityInfoZip(elementId, zip) {
    var oCommunityInfoDiv = document.getElementById(elementId);
    if (oCommunityInfoDiv != null) {
        oCommunityInfoDiv.innerHTML = oCommunityInfoDiv.innerHTML.replace(/ZIP/g, zip)
    }
}

