window.onload=function()
{
	searchprop();
}

function searchprop()
{
	 httpObject = getHTTPObject();
	 if (httpObject != null)
	 {
		httpObject.onreadystatechange = doSearch;
		httpObject.open("GET", "php/randomprop.php", true);
		httpObject.send(null);
	 }

	
}

function doSearch()
{

	if(httpObject.readyState == 4)
	{
		var html = ""
			html = html + "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
			//html = html + "<tr>";
			//html = html + "	<td class='MemberLoginTxt' style='width:200' align='left'>Search Results: </td>";
			//html = html + "</tr>";

		if(httpObject.responseText.length > 0)
		{
			var props = httpObject.responseText.split("|^|");
			
			
			for(var t=0;t<props.length;t++)
			{
					var curprop = props[t].split("|~|");
					
					var property_name = curprop[2];
					var property_id = curprop[0];
					//var property_pic = "images/ImgFeatureProp.gif";
					if(curprop[1] == "")
						curprop[1] = "../images/StandardBushveld.jpg";
					var property_pic =  "php/resize.php?fn="+curprop[1]+"&w=112&h=84";
					//alert(property_pic);
					if (curprop[4]) 
					{
						var bedrooms = "";
						var bathrooms = "";
						var garages = "";
					
						var extras = curprop[4].split("~^~");
						var attribute = new Array();
						for(var p=0;p < 12;p++)
						{
							if(extras[p]) {
								attribute[p] = extras[p];
								
								if(extras[p].indexOf("Bedrooms") != -1)
								{
									var index = extras[p].indexOf("Bedrooms");
									bedrooms = parseInt(extras[p].substr(extras[p].indexOf("Bedrooms")+11));
									
									if(isNaN(bedrooms))
										bedrooms = "";
								}
								else if(extras[p].indexOf("Bathrooms") != -1)
								{
									var index = extras[p].indexOf("Bathrooms");
									bathrooms = parseInt(extras[p].substr(extras[p].indexOf("Bathrooms")+12));
									
									if(isNaN(bathrooms))
										bathrooms = "";
								}
								else if(extras[p].indexOf("Garage") != -1)
								{
									var index = extras[p].indexOf("Garage");
									garages = parseInt(extras[p].substr(extras[p].indexOf("Garage")+9));
									
									if(isNaN(garages))
										garages = "";
								}
							}
							else {
								attribute[p] = "";
							}
						}
						
						var stats = "<table><tr>";
				
						if(bedrooms != "")
							stats += '<td><img style="margin-right: 5px" src="images/layout/bed.png" alt="Bedrooms" title="Bedrooms" /></td><td><span style="font-size:20px" >'+bedrooms+'</span></td>';
						if(bathrooms != "")
							stats += '<td><img style="margin:0px 5px" src="images/layout/bath.png" alt="Bathrooms" title="Bathrooms" /></td><td><span style="font-size:20px" >'+bathrooms+'</span></td>';
						if(garages != "")
							stats += '<td><img style="margin:0px 5px" src="images/layout/garage.png" alt="Garages" title="Garages" /></td><td><span style="font-size:20px" >'+garages+'</span></td>';
							
						stats += "</tr></table>";
					}
					else
					{
						var attribute = new Array();
						for(var p=0;p < 12;p++)
						{
							attribute[p] = "";
						}				
					}
					
					html = html + "<tr>";
					html = html + '<td class="GridHeader"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>';
					html = html + '<td colspan="2" class="gridHeaderTxt" style="cursor:pointer" onclick="moredetails(\''+property_id+'\');">'+property_name+'</td><td class="gridPropID" style="cursor:pointer" onclick="moredetails(\''+property_id+'\');">Property ID: '+property_id+'</td></tr></table></td>';

    

					//html = html + "	<td class='Header_background' style='width:100%;'><span align='left' style='text-align:left;padding-left:10px'>"+property_name+"</span><span align='right' style='text-align:right;padding-right:10px'>"+property_id+"</span></td>";
					html = html + "</tr>";
					html = html + "<tr>";
					html = html + "<td class='GridColor'><table width='100%' border='0' cellspacing='0' cellpadding='5'>";

					html = html + "<tr>";
					html = html + "<td><img src='"+property_pic+"' width='112' height='84' /></td>";

					html = html + "<td><table width='100%' border='0' cellspacing='0' cellpadding='0'>";
					
					html = html + "<tr>";
					html = html + "<td style='width:130px;padding-left:10px'>"+attribute[0]+"</td>";
					html = html + "<td style='width:130px;padding-left:10px'>"+attribute[4]+"</td>";
					html = html + "<td style='width:130px;padding-left:10px'>"+attribute[8]+"</td>";
					html = html + "</tr>";
					html = html + "<tr>";
					html = html + "<td style='width:130px;padding-left:10px'>"+attribute[1]+"</td>";
					html = html + "<td style='width:130px;padding-left:10px'>"+attribute[5]+"</td>";
					html = html + "<td style='width:130px;padding-left:10px'>"+attribute[9]+"</td>";
					html = html + "</tr>";
					html = html + "<tr>";
					html = html + "<td style='width:130px;padding-left:10px'>"+attribute[2]+"</td>";
					html = html + "<td style='width:130px;padding-left:10px'>"+attribute[6]+"</td>";
					html = html + "<td style='width:130px;padding-left:10px'>"+attribute[10]+"</td>";
					html = html + "</tr>";
					html = html + "<tr>";
					html = html + "<td style='width:130px;padding-left:10px'>"+attribute[3]+"</td>";
					html = html + "<td style='width:130px;padding-left:10px'>"+attribute[7]+"</td>";
					html = html + "<td style='width:130px;padding-left:10px'>"+attribute[11]+"</td>";
					html = html + "</tr>";
					
					html = html + "<tr>";
					html = html + "<td style='width:130px;padding-left:10px' colspan='3' >"+stats+"</td>";
					html = html + "</tr>";

					html = html + "</table><td>";

					html = html + "<td><table width='100%' border='0' cellspacing='0' cellpadding='0'>";

					html = html + "<tr>";
					html = html + "<td align='right' style='padding-right:5px'><img src='images/Arrow.gif' width='13' height='11' /></td></td>";
					html = html + "<td style='text-align:left;cursor:pointer;text-decoration:underline;width:100px' onclick=\"moredetails('"+property_id+"');\">View More Details</td>";
					html = html + "</tr>";
					html = html + "<tr>";
					html = html + "<td align='right' style='padding-right:5px'><img src='images/Arrow.gif' width='13' height='11' /></td></td>";
					html = html + "<td style='text-align:left;cursor:pointer;text-decoration:underline;width:100px' onclick=\"addtowishlist('"+property_id+"');\">Add to wishlist</td>";
					html = html + "</tr>";
					html = html + "<tr>";
					html = html + "<td align='right' style='padding-right:5px'><img src='images/Arrow.gif' width='13' height='11' /></td></td>";
					html = html + "<td style='text-align:left;cursor:pointer;text-decoration:underline;width:100px' onclick=\"emailagent('"+property_id+"');\">Email Agent</td>";
					html = html + "</tr>";
					html = html + "<tr>";
					html = html + "<td align='right' style='padding-right:5px'><!--<img src='images/Arrow.gif' width='13' height='11' />--></td></td>";
					html = html + "<td style='text-align:left;cursor:pointer;text-decoration:underline;width:100px' onclick=\"smsagent('"+property_id+"');\"><!--Sms Agent--></td>";
					html = html + "</tr>";

					html = html + "</table><td>";
					
					html = html + "</table><td>";
					html = html + "</tr>";
					html = html + "<tr>";
					html = html + "	<td style='height:5px'></td>";
					html = html + "</tr>";			
			}
			window.parent.document.getElementById('iframe').height = 800 + props.length*80;		
		}
		else
		{

					 html += '</table>';				
		}
		




		html = html + "</table>";
		document.getElementById("searchresults").innerHTML = html;					
	}
}


function moredetails(id)
{
	//alert('more details : ' + id);
	window.parent.document.getElementById('div_hidden_prop_id').innerHTML = id;
	window.parent.fromsearch = '"frameindex.htm","800"';
	window.location = "Moredetails.htm";
}

function addtowishlist(id)
{
	window.parent.document.getElementById('div_hidden_prop_id').innerHTML = id;
	//alert('add to wishlist : ' + id);
	if (window.parent.document.getElementById('div_hidden_profile_id'))
	{
		var profile_id = window.parent.document.getElementById('div_hidden_profile_id').innerHTML;
		if (profile_id != 3)
		{
			alert("You do not have a wishlist. (Only customers can have a wishlist)");
		}
		else
		{
			httpObject = getHTTPObject();
			 if (httpObject != null)
			 {
				httpObject.onreadystatechange = addtowishlistres;
				httpObject.open("GET", "php/addtowishlist.php?pid="+id, true);
				httpObject.send(null);
			 }			
		}
	}
	else
	{
			alert("You are not logged in. Please register and log in. \r\nOnce you are logged in , this property will be added to your wishlist.");
	}
	

	
	
}

function addtowishlistres()
{
	if(httpObject.readyState == 4)
	{
				window.parent.document.getElementById('div_hidden_prop_id').innerHTML = "";
				if (confirm("Property has been added to your wishlist , would you like to go to Wishlist maintainance now?"))
				{
					window.location = "Memberframe.htm";
				}
	}
		
}

function emailagent(id)
{
	window.parent.document.getElementById('div_hidden_prop_id').innerHTML = id;
	window.location = "EmailPropAgent.htm";
}

function smsagent(id)
{
	alert('sms : ' + id);
}

function getHTTPObject()
{
	if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) return new XMLHttpRequest();
	else{
			alert("Your browser does not support AJAX.");
			return null;
		}
}