﻿function renderPaging(pagerId, searchTableString, pageSize, pageStart, propertyRows) {
    var pagingOut = new Array();
    pagingOut.push('<div style="padding: 10px 0 10px 0"><div style="float: left;">Show&nbsp;<select id="' + pagerId + '" onchange="javascript:changePageSize($get(\'' + pagerId + '\'));">');

	for (var i=10; i<=50; i+=10) {
	    pagingOut.push('<option value="' + i + '"')
		if (pageSize == i) {
		    pagingOut.push(' selected');
		}
		pagingOut.push('>' + i + ' properties per page</option>');
	}
	pagingOut.push('</select></div><div style="float: right;"><strong>Page:</strong>&nbsp;');
	
	if (pageStart <= 1) {
	    //pagingOut.push('<strong style="font-size: 20px;color: #999999;">&laquo;&nbsp;</strong>');
	} else {
		prevPage = pageStart-1;
		pagingOut.push('<a href="');
		pagingOut.push(GetRenderTableHref(pageSize, prevPage));
		pagingOut.push('"><img src="');
		pagingOut.push(ImagePath);
		pagingOut.push('/gtk-go-left16.png" width="16" height="16" align="absmiddle" alt="Previous" border="0" /></a>');
	}
	
	for (var i=1; i<=Math.ceil(propertyRows/pageSize); i++) {
		if (i==pageStart) {
		    pagingOut.push('<strong>' + i + '</strong>&nbsp;');
		} else {
		    pagingOut.push('<a href="');
		    pagingOut.push(GetRenderTableHref(pageSize, i));
		    pagingOut.push('">');
		    pagingOut.push(i);
		    pagingOut.push('</a>&nbsp;');
		}
	}
	
	if (pageStart >=propertyRows/pageSize) {
	    //pagingOut.push('<strong style="font-size: 20px;color: #999999;">&raquo;&nbsp;</strong>');
	} else {
		nextPage = pageStart+1;
		pagingOut.push('<a href="');
		pagingOut.push(GetRenderTableHref(pageSize, nextPage));
		pagingOut.push('"><img src="');
		pagingOut.push(ImagePath);
		pagingOut.push('/gtk-go-right16.png" width="16" height="16" align="absmiddle" alt="Next" border="0" /></a>');
	}
	
	pagingOut.push('</div><div style="clear: both;"></div></div>');
	return pagingOut.join('');
}

function GetRenderTableHref(pageSize, nextPage) {
    return "javascript:renderTOKTable(searchResults.PropertyInfoList, " + pageSize + ", " + nextPage + ", true);";
}

function changePageSize(selectSize)
{
	renderTOKTable(searchResults.PropertyInfoList, selectSize.value, 1, true);
}

function renderTOKTable(propertyArray, pageSize, pageStart, IsSearchTable)
{
	var propertyRows = propertyArray.length;
	var st = new Array();
	var searchResultsDiv = $get("propertySearchListTable");

	if (IsSearchTable == true) {
	  st.push(renderPaging("propertyPageTop", st, pageSize, pageStart, propertyRows));
	}
	st.push('<table class="propertySearchListTable" border="1" cellpadding="0" cellspacing="0"><tr><th style="text-align:center">#</th><th>Picture</th><th>Listing summary</th><th>Agents</th><th>Select</th></tr>');
	
	for (var propertyItem = (pageStart-1) * pageSize; propertyItem < (pageStart-1) * pageSize + pageSize; propertyItem++)
	{
		if (propertyItem < propertyArray.length)
		{
			st.push('<tr><td valign="top" align="center" style="width: 30px" style="text-align:center"><div class="results-hiticon">');
			st.push(propertyItem + 1);
			st.push('</div></td><td valign="top" align="left" style="width: 130px">');
			if (propertyArray[propertyItem].PropertyFlyer) {
			    st.push('<a href="');
			    st.push(WebFlyerHandlerPage);
			    st.push('?visitor=');
			    st.push(VisitorGUID);
			    st.push('&property=');
			    st.push(propertyArray[propertyItem].WebPropertyId);
			    st.push('" target="_blank" class="results-propertyname">');
			    st.push(propertyArray[propertyItem].PropertyName);
			    st.push('</a>');
			} else {
			    st.push('<span class="results-propertyname">');
			    st.push(propertyArray[propertyItem].PropertyName);
			    st.push('</span>');
			}
			if (propertyArray[propertyItem].PropertyImage) {
			    st.push('<br /><a onclick="javascript: showLightbox(this); return false;" href="');
			    st.push(PropertyResourcesFolder);
			    st.push('/TOKproperties/');
			    st.push(propertyArray[propertyItem].PropertyImage);			
			    st.push('" rel="lightbox" title="');
			    st.push(propertyArray[propertyItem].PropertyName);
			    st.push('"><img width="120" height="90" alt="');
			    st.push(propertyArray[propertyItem].PropertyName);
			    st.push('" src="');
			    st.push(PropertyResourcesFolder);
			    st.push('/TOKproperties/thumbs/');
			    st.push(propertyArray[propertyItem].PropertyImage);
			    st.push('" style="border: solid 1px #000000"></a>');
			}
			st.push('</td><td valign="top" style="width: 295px">');
			
			for (listingItem=0; listingItem < propertyArray[propertyItem].ListingSummary.length; listingItem++)
			{
				st.push(propertyArray[propertyItem].ListingSummary[listingItem] + "<br/>");
			}
			
			st.push('</td><td valign="top" style="width: 125px">');
			
			for (agentItem=0; agentItem < propertyArray[propertyItem].Agents.length; agentItem++)
			{
				st.push("<a href='mailto:" + propertyArray[propertyItem].Agents[agentItem].Agent_email + "'>" + propertyArray[propertyItem].Agents[agentItem].Agent_name + "</a><br/>");
				st.push(propertyArray[propertyItem].Agents[agentItem].Agent_phone);
				if (agentItem + 1 < propertyArray[propertyItem].Agents.length)
				{
				    st.push('<br/><br/>');
				}
			}
			
			st.push("</td>");
			
			if (IsSearchTable == true)
			{
			  //st.push('<td valign="top" style="width: 115px" id="propertyRow' + propertyArray[propertyItem].PropertyID +'">');
			  st.push('<td valign="top" style="width: 145px; text-align: center">');
			  st.push('<div id="propertyRow');
			  st.push(propertyArray[propertyItem].PropertyID);
			  st.push('" style="text-align:center">');
			  
			  if (propertyArray[propertyItem].IsSaved != true)
			    st.push('<a href="javascript:AddFavorite(this,' + propertyArray[propertyItem].PropertyID + ');"><img src="' + PropertyResourcesFolder + '/savetofavoritesbtn.png" width="143" height="25" alt="Save to favorites" border="0" style="margin-top: 5px"/></a>');
			  else
			    st.push('<em>Property Saved</em>');
			    
			  st.push('</div>');
			  if (propertyArray[propertyItem].PropertyFlyer) {
			      st.push('<a href="');
                  st.push(WebFlyerHandlerPage);
		          st.push('?visitor=');
		          st.push(VisitorGUID);
		          st.push('&property=');
		          st.push(propertyArray[propertyItem].WebPropertyId);
			      st.push('" target="_blank"><img src="');
			      st.push(PropertyResourcesFolder);
			      st.push('/downloadbrochurebtn.png" width="143" height="25" alt="Download brochure" border="0" style="margin-top: 5px" /></a>');
			  }
			  st.push('</td>');
            }
            else
			{
			  st.push("<td valign='top'><a href='javascript:RemoveFavorite(" + propertyArray[propertyItem].PropertyID + ");'>Remove</a></td>");
			}
			st.push("</tr>");
		}
	}
	st.push('</table>');
	if (IsSearchTable == true)
        st.push(renderPaging("propertyPageBottom", st, pageSize, pageStart, propertyRows));
	
    if (searchResultsDiv)
    {
	    searchResultsDiv.innerHTML = st.join('');
	}
	else
	{
	    alert("No element has been specified to render the search results to.");
	}
}

function renderTOKSearchTable(searchArray)
{
	var searchRows = searchArray.length;
	var searchTableString = new Array();
	var searchResultsDiv = $get("savedSearchListTable");

	searchTableString.push('<table class="propertySearchListTable" border="1" cellpadding="0" cellspacing="0" width="100%"><tr><th style="text-align:center">#</th><th></th><th>Search Criteria</th><th>&nbsp;</th></tr>');
	
	for (var searchCounter = 0; searchCounter < searchRows; searchCounter++)
	{
			searchTableString.push('<tr><td valign="top" width="30" style="text-align:center"><div class="results-hiticon">');
			searchTableString.push(searchCounter + 1);
			searchTableString.push('</div></td><td valign="top" align="left" width="170px"><a href="');
			searchTableString.push(PropertySearchUrl);
			searchTableString.push('/');
			searchTableString.push(searchArray[searchCounter].SearchGUID);
			searchTableString.push('/Default.aspx" target="_blank">');
			//searchTableString.push(searchArray[searchCounter].DateUpdated.getMonth() + '/' + searchArray[searchCounter].DateUpdated.getDate() + '/' + searchArray[searchCounter].DateUpdated.getFullYear() + ' ' + TimeFormat(searchArray[searchCounter].DateUpdated) );
			searchTableString.push('Run Search');
			searchTableString.push('</a></td><td valign="top">');
			
			searchTableString.push(SearchCriteriaFormat(searchArray[searchCounter]));
			
			searchTableString.push('</td>');
	  	    searchTableString.push('<td valign="top" width="100px"><a href="javascript:RemoveSavedSearch(\'');
	  	    searchTableString.push(searchArray[searchCounter].SearchGUID);
	  	    searchTableString.push('\');">Remove</a></td>');
			searchTableString.push('</tr>');
	}
	searchTableString.push('</table>');
	
    
    if (searchResultsDiv)
    {
	    searchResultsDiv.innerHTML = searchTableString.join('');
	}
	else
	{
	    alert("No element has been specified to render the search results to.");
	}
}

function FormatNumberWithNoLimit(num, format) {
    var NoLimit = 2147483647;
    var NoLimitAcres = 4356000;
    var numObject = new Number(num);

    if (numObject == NoLimit || numObject == NoLimitAcres) {
       return "NO LIMIT";
    } else {
       return numObject.localeFormat(format);
    }
}

function SearchCriteriaFormat(objSC)
{
  
  var string = '';

  if (objSC.OfferStatus == "Lease")
  {
  string += '<span style="background-color: #CCC; display:block;"><strong>Search type:</strong> Lease</span>';
    string += '<span style="background-color: #CCC; display:block;"><strong>Property Type:</strong> ' + ((objSC.Industrial == true) ? 'Industrial ' : '') + ((objSC.Office == true) ? 'Office ' : '') + ((objSC.Industrial == true) ? 'Retail' : '') + '</span>';
    string += '<span style="background-color: #CCC; display:block;"><strong>Size Range (square feet):</strong> '
    string += FormatNumberWithNoLimit(objSC.SizeLow,"N0");
    string += ' - ';
    string += FormatNumberWithNoLimit(objSC.SizeHigh,"N0");
    string += '</span>';
    
    string += '<span style="background-color: #CCC; display:block;"><strong>Monthly Range:</strong> '
    string += FormatNumberWithNoLimit(objSC.PriceLow,"C0");
    string += ' - ';
    string += FormatNumberWithNoLimit(objSC.PriceHigh,"C0");
    string += '</span>';
  }
  else
  {
  
    if (objSC.PropertyCategory == "Building")
    {
    string += '<span style="background-color: #CCC; display:block;"><strong>Search type:</strong> Building</span>';
    string += '<span style="background-color: #CCC; display:block;"><strong>Offered for:</strong> ' + objSC.OfferStatus + '</span>';
    string += '<span style="background-color: #CCC; display:block;"><strong>Property Type:</strong> ' + ((objSC.Industrial == true) ? 'Industrial ' : '') + ((objSC.Office == true) ? 'Office ' : '') + ((objSC.Industrial == true) ? 'Retail ' : '') + ((objSC.MultiFamily == true) ? 'Multi family ' : '') + '</span>';
    string += '<span style="background-color: #CCC; display:block;"><strong>Size Range (square feet):</strong> '
    string += FormatNumberWithNoLimit(objSC.SizeLow,"N0");
    string += ' - ';
    string += FormatNumberWithNoLimit(objSC.SizeHigh,"N0");
    string += '</span>';
    
    string += '<span style="background-color: #CCC; display:block;"><strong>Monthly Range:</strong> '
    string += FormatNumberWithNoLimit(objSC.PriceLow,"C0");
    string += ' - ';
    string += FormatNumberWithNoLimit(objSC.PriceHigh,"C0");
    string += '</span>';    
    }
    else if (objSC.PropertyCategory == "Land")
    {
    string += '<span style="background-color: #CCC; display:block;"><strong>Search type:</strong> Land for sale</span>';
    string += '<span style="background-color: #CCC; display:block;"><strong>Property Type:</strong> ' + ((objSC.Industrial == true) ? 'Industrial ' : '') + ((objSC.Office == true) ? 'Office ' : '') + ((objSC.Industrial == true) ? 'Retail ' : '') + ((objSC.MultiFamily == true) ? 'Multi family ' : '') + ((objSC.Residential == true) ? 'Residential ' : '') + '</span>';
    string += '<span style="background-color: #CCC; display:block;"><strong>Size Range (square feet):</strong> '
    string += FormatNumberWithNoLimit(objSC.SizeLow,"N0");
    string += ' - ';
    string += FormatNumberWithNoLimit(objSC.SizeHigh,"N0");
    string += '</span>';
    
    string += '<span style="background-color: #CCC; display:block;"><strong>Monthly Range:</strong> '
    string += FormatNumberWithNoLimit(objSC.PriceLow,"C0");
    string += ' - ';
    string += FormatNumberWithNoLimit(objSC.PriceHigh,"C0");
    string += '</span>';        
    }
    
  }
  
  return string;

}

function TimeFormat(datetime) {
   var time = new Date(datetime)
   var hour = time.getHours()
   var minute = time.getMinutes()
   var second = time.getSeconds()
   var temp = "" + ((hour > 12) ? hour - 12 : hour)
   if (hour == 0)
      temp = "12";
   temp += ((minute < 10) ? ":0" : ":") + minute
   temp += ((second < 10) ? ":0" : ":") + second
   temp += (hour >= 12) ? " P.M." : " A.M."
   return temp
}

