function removeFromPool (propID)
{
	// ajax call to remove a property from the pool
	url = "_designlibrary/featuredProperty_removeFromPool.php";
	param = "PropertyID=" + propID;
	new Ajax.Request(url + "?" +param,
    {
        onSuccess : function(response, json) {
        	//featuredPropertyBuild(response.responseText);
        },
		
        onFailure : function(response) {
            alert("Error setting featured property");
        },
        
        onException : function(response, json)
        {
        	alert("Exceptions found");
        },
        // sending through parameters        
        parameters : param
    } );
	inPoolSearch(1);
}

function featuredPropertySet (propID)
{
	// ajax call to set new featured property then call function to rebuild featured property section
	url = "_designlibrary/featuredProperty_changeFeaturedProp.php";
	param = "PropertyID=" + propID;
	new Ajax.Request(url + "?" +param,
    {
        onSuccess : function(response, json) {
        	//featuredPropertyBuild(response.responseText);
			//alert("inserted");
        },
        onFailure : function(response) {
            alert("Error setting featured property");
        },
        onException : function(response, json)
        {
        	alert("Exceptions found");
        },
        // sending through parameters        
        parameters : param
    } );
    createFeaturedProperty ();
}

function createFeaturedProperty ()
{
	// ajax call to set new featured property then call function to rebuild featured property section
	url = "_designlibrary/FeaturedProperty_current.php";
	
	new Ajax.Request(url,
    {
        onSuccess : function(response, json) {
        	featuredPropertyBuild(response.responseText);
        },
		
        onFailure : function(response) {
            alert("Error setting featured property");
        },
        
        onException : function(response, json)
        {
        	alert("Exceptions found");
        },
        // sending through parameters
        parameters : param
    } );
}

function keyCheck (e)
{
	//if enter pressed 
	if (e.keyCode == '13')
	{
		// executes search
		webrefSearch(1);
	}
}

function featuredPropertyBuild(text)
{
	// retrieving the dataset from the request
	var dataset = $H();
	
	// Setting a variable with the  featured property information
	dataset = eval("(" + text + ")");
	var imagePath = dataset.ImageURL;
	var buttonPath= "http://www.aus.whitewallweb.co.za/?page=property_Details&webref=" + dataset.Webref ;
	
	// Creating the featured property html// 
	html = "<table width='99%' height='99%' class='border'>" +
				"<tr>" +
					"<td colspan='2' class='sec_table_top' style='padding-left:20px' height='25px'>" +
						"<span> <b>Latest Featured Property</b> </span>" +
					"</td>" +
				"</tr>" +
				"<tr>" +
					"<td align='center' style='padding-left:20px'>" +
						"<img style='width:200px;' onclick='window.open(\"" + buttonPath + "\");' src='" + imagePath + "' Alt='Featured Property' />" +
					"</td>" +
					"<td>" +
						"<table>" +
							"<tr>" +
								"<td colspan=2><b>Featured Property Information</b><br /><br /></td>" +
							"</tr>" +
							"<tr>" +
								"<td colspan=2><b>Web Reference: " +  dataset.Webref + " </b></td>" +
							"</tr>" + 
							"<tr>" +
								"<td>Area  :</td>" +
								"<td>" + dataset.Area + "</td>" +
							"</tr>" +
							"<tr>" +
								"<td>Type :</td>" +
								"<td>" + dataset.Type + "</td>" +
							"</tr>" +
							"<tr>" +
								"<td>Price :</td>" +
								"<td> $" + dataset.DisplayPrice + "</td>" +
							"</tr>" +
							"<tr>" +
								"<td>Date Added:</td>" +
								"<td>" + dataset.Date + "</td>" +
							"</tr>" +
							"<tr>" +
								"<td colspan='2'>" +
									"<a style='cursor:hand' onclick='window.open(\"" + buttonPath + "\");' >View Property </a>" +
								"</td>" +
							"</tr>"
						"</table>" +
					"</td>" +
				"</tr>" +
		 	"</table>";
		 	
 	// rewriting the result stats div
	document.getElementById("featured").innerHTML = html;		 	
}

function previousSearch(resultSet)
{
	// Setting hidden Value to keep track of the page
	document.getElementById("pageNo").value = resultSet;
	
	// setting url and parameters for request
	url = "_designlibrary/featuredProperty_previousSearch.php";
	parameters = "resultSet=" + resultSet;
		
	// call request function
	performSearch (url, parameters);
}

function possibleSearch (resultSet)
{
	// Setting hidden Value to keep track of the page
	document.getElementById("pageNo").value = resultSet;

	// setting url and parameters for request
	url = "_designlibrary/featuredProperty_possibleSearch.php";
	parameters = "resultSet=" + resultSet;
	
	// call request function
	performSearch (url, parameters);
}

function webrefSearch (resultSet)
{
	// Setting hidden Value to keep track of the page
	document.getElementById("pageNo").value = resultSet;
	var WRef = document.getElementById('Webref');
	if (WRef.value != "")
	{
		// setting url and parameters for request
		url = "_designlibrary/featuredProperty_webrefSearch.php";
		parameters = "WRef=" + WRef.value + "&resultSet=" + resultSet;
		
		// call request function
		performSearch (url, parameters);
	}
	else
	{
		alert("Please enter a webref to search for");
	}
}

function inPoolSearch (resultSet)
{
	// Setting hidden Value to keep track of the page
	document.getElementById("pageNo").value = resultSet;
	
	// setting url and parameters for request
	url = "_designlibrary/featuredProperty_inPoolSearch.php";
	parameters = "resultSet=" + resultSet;
		
	// call request function
	performSearch (url, parameters);
}

function performSearch (url, param) 
{
    $("loadingDiv").style.display = "block";
	// Creating ajax request using prototype
	new Ajax.Request(url + "?" +param,
    {
        onSuccess : function(response, json) {
        	resultBuild(response.responseText);
            $("loadingDiv").style.display = "none";
        },

        onFailure : function(response) {

            alert("Error Searching");
        },
        
        onException : function(response, json)
        {
        	alert("Exceptions found");
        },
        // sending through parameters
        parameters : param
    } );
}

function resultBuild(text)
{
	// retrieving the dataset from the request
	var dataset = $H();
	
	// #Results
	dataset = eval("(" + text + ")");
	
	// creating html output
	var html = "<table width='98%' cellspacing=0>";
	
	if (dataset.Valid == "true") // If a valid/non-empty result set was returned
	{
		var alt = false; // used to set alternating style for each row
		var sameline = 0; // used to determine if 1st or 2nd element on tr
		
		
		for (i = dataset.StartingPoint; i < dataset.EndingPoint; i++)
		{
			// alternating style template						
			if (sameline == 0)
			{
				if (alt == true)
				{
					style = " class='alt_bg'";
					alt = false;
				}
				else
				{
					style = "";
					alt = true;
				}
				html += "<tr" + style + ">";
			}


			// Search results html
			var buttonPath= "http://www.aus.whitewallweb.co.za/?page=property_Details&webref=" + dataset.PropertyID[i].WebRef;
			var imagePath = dataset.PropertyID[i].ImageURL;			
			var PropertyButtons = " | &nbsp; <a style='cursor:hand' onclick=\"featuredPropertySet('" + dataset.PropertyID[i].PropertyID + "');\" > Set as featured property </a>" + " &nbsp; | &nbsp; <a style='cursor:hand' onclick='window.open(\"" + buttonPath + "\");' > View Property </a> &nbsp; | ";
			
			var ViewProp = "onclick='window.open(\"" + buttonPath + "\");";
			
			var statusImage = "";
			if (dataset.searchType == "webref")
			{
				switch(parseInt(dataset.PropertyID[i].InPool))
				{
					// Checking Featured pool status
					case 0: 
							statusImage = "<td align='right'> <img width='50' height='50' src='../_images/icons/previous.gif' alt='Previously Featured' /> </td>";
					break;
					
					case 1:
							statusImage = "<td align='right'> <img width='50' height='50'src='../_images/icons/featured.gif' alt='Current Featured Property' /> </td>";
							PropertyButtons = " | &nbsp; <a style='cursor:hand' onclick=\"removeFromPool('" + dataset.PropertyID[i].PropertyID + "');\" > Remove from pool </a>" + " &nbsp; | &nbsp; <a style='cursor:hand' onclick='window.open(\"" + buttonPath + "\");' > View Property </a> &nbsp; | ";
					break;
				}	
			}
			else if (dataset.searchType == "previous")
			{
				// Checking Featured pool status
				statusImage = "<td align='right'> <img width='50' height='50' src='../_images/icons/previous.gif' alt='Previously Featured' /> </td>";

				// Checking show on web status
				if (parseInt(dataset.PropertyID[i].ShowOnWeb) == 0)
				{
					PropertyButtons = "";
					ViewProp = "";
				}
			}
			else if (dataset.searchType == "inPool")
			{	
				PropertyButtons = " | &nbsp; <a style='cursor:hand' onclick=\"removeFromPool('" + dataset.PropertyID[i].PropertyID + "');\" > Remove from pool </a>" + " &nbsp; | &nbsp; <a style='cursor:hand' onclick='window.open(\"" + buttonPath + "\");' > View Property </a> &nbsp; | ";
				statusImage = "<td align='right'> <img width='50' height='50' src='../_images/icons/featured.gif' alt='Current Featured Property' /> </td>";

			}
			
			html +=	"	<td width='155'> " +
							"<img style='width:150px;' " + ViewProp + "' src='" + imagePath + "' alt='Main Property Image'> " +
						"</td> " +
						"<td style='padding-left:25px'> " +
							"<table> " +
								"<tr> " +
									"<td> <b> Web Reference: " +  dataset.PropertyID[i].WebRef + " </b> </td> " +
								"</tr>" +
								"<tr>" +
									"<td> $" + dataset.PropertyID[i].DisplayPrice + " </td>" +
								"</tr>" +
								"<tr>" +
									"<td> " + dataset.PropertyID[i].Area + " </td>" +
								"</tr>" +
								"<tr>" +
									"<td> " + dataset.PropertyID[i].PropertyType + " </td>" +
								"</tr>" +
								"<tr>" +
									"<td> " + dataset.PropertyID[i].Description + " </td>" +
								"</tr>" +
								"<tr>" +
									"<td style='padding-top:10px;'> " + PropertyButtons + " </td>" + statusImage +
								"</tr>" +
							"</table>" +
						"</td>";
			if (sameline == 1)
			{
				html +=	"</tr>";
				sameline = 0;
			}
			else 
			{
				sameline++;
			}
		}
		
		html += "</table>";
		
		// rewriting the 'results' div
		document.getElementById("result").innerHTML = html;
		new Effect.BlindDown(document.getElementById("result"));  // slide down div
		
		// rewriting the total results div
		document.getElementById("resultCount").innerHTML = dataset.Size + " results found";
		
		// rewriting the result navigation div
		// html for navigation div
		var noOfPages = Math.ceil( dataset.Size / 10 );
		var current = document.getElementById("pageNo").value;
		
		// creating navigation Html
		var navHtml = 	"<div style='background-color:#D4D4D4;width:98%;'>" +
							"<table align='center' cellspacing='0'>" +
								"<tr>";
		// setting link range
		var first = parseInt(current) - 5;
		var last = parseInt(current) + 5;
		if (first > (noOfPages - 11))
		{
			first = noOfPages - 11;
		}
		if (last < 11)
		{
			last = 11;
		}
		if (last > noOfPages)
		{
			last = noOfPages;
		}
		if (first < 1)
		{
			first = 1;
		}
		//*
		// rewriting the result stats div
		document.getElementById("resultStats").innerHTML = " Viewing page " + current + " of " + noOfPages;
		
		// link building
		var linkType = "";
		href = "href='javascript: ;'";
		
		// navigation search type building
		if (dataset.searchType == "previous")
		{
			linkType = "onclick='previousSearch(";
		}
		else if (dataset.searchType == "possible")
		{
			linkType = "onclick='possibleSearch(";
		}
		else if (dataset.searchType == "webref")
		{
			linkType = "onclick='webrefSearch(";
		}
		else if (dataset.searchType == "inPool")
		{
			linkType = "onclick='inPoolSearch(";
		}
		
		var link = "";
		// link creation
		if (current == first)
		{
			navHtml +=	"<td> <b style='padding-left:30px;padding-right:30px'> Previous &nbsp; < </b> </td>";
		}
		else
		{
			var pageNo = current - 0 - 1;
			link = linkType + pageNo + ");'";
			navHtml +=	"<td> <a style='padding-left:30px;padding-right:30px' " + href + " " + link + " > <b> Previous &nbsp; < </b> </a> </td>";
		}
		
		for (var x = first; x <= last; x++)
		{
			if (x != current)
			{
				// setting link value
				link = linkType + x + ");'";
				navHtml += 	"<td width='15' style='padding-left:4px;padding-right:4px'>" +
						   		"<a " + href + " " + link + "><b>" + x + "</b></a>" +
							"</td>";
			}
			else
			{
				navHtml += 	"<td width='15' style='padding-left:4px;padding-right:4px'> <b>" + x + "</b></td>";				
			}
		}
		if (current == last)
		{
			navHtml +=	"<td style='padding-left:30px;padding-right:30px'> <b> > &nbsp; Next </b> </td></tr>";
		}
		else
		{
			var pageNo = current - 0 + 1;
			link = linkType + pageNo + ");'";
			navHtml +=	"<td style='padding-left:30px;padding-right:30px'> <a " + href + " " + link + " > <b> > &nbsp; Next </b> </a> </td></tr>";
		}
		navHtml +=	"</table></div>";
		// rewriting the 'navigator' div
		document.getElementById("Navigator").innerHTML = navHtml;
	}
	else
	{
		// if no results found
		html += "</div><tr>" +
					"<td align='center'><b>No search Results Found</b></td>" +
				"</tr>" +
			"</table>";
		
		// rewriting the results stats div
		document.getElementById("resultStats").innerHTML = "";
		
		// rewriting the total results div
		document.getElementById("resultCount").innerHTML = "0 results found";
		
		// rewriting the 'navigator' div
		document.getElementById("Navigator").innerHTML = "";
		
		// rewriting the 'results' div
		document.getElementById("result").innerHTML = html;
		new Effect.BlindDown(document.getElementById("result")); // slide down div
	}
}
