var SearchDataSet;
var SearchOptions;
//var SearchOptions2;
var TypesDataSet;
var StatusDataSet;
var StylesDataSet;
var SearchTimer   = 0;
var ResultsTimer  = 0;
var FilterTimer   = "##DEFAULT##";
var SuburbFilter = "";
function save_SearchDataSet(dataset)
{
	SearchDataSet = dataset;
}
function save_SearchOptions(dataset)
{
	SearchOptions = dataset;
}

/*function save_SearchOptions2(dataset)
{
	SearchOptions2 = dataset;
}*/

function _init_Search()
{
	fetch_All_Property_Status_Types();
}
function fetch_All_Property_Status_Types()
{
	try
	{
		new Ajax.Request("data/get_Property_InfoDataSets.php",
		{
			onSuccess : function(response)
			{
//				alert(response.responseText);
				if(response.responseText != false)
				{
					try
					{
						var result    = $H(eval("(" + response.responseText + ")"));
						StatusDataSet = result.Status;
						TypesDataSet  = result.Types;
						StylesDataSet = result.Styles;
						fetch_Search_Dataset(0);
					}
					catch(e)
					{
						alert(e.message);
					}
				}
			},
			onFailure : function(response) 
			{
				alert("error loading " + url + "...");
			}
		});
	}
	catch(e)
	{
		alert(e.message);
	}
}
function fetch_Search_Dataset(month)
{
	var tomonth   = "";
	var frommonth = "";
	var AgentSelect	= "";
	if(month == 'all')
	{
		frommonth = 'all'
		tomonth   = 'all'
	}
	else
	{
		frommonth = $('fromdate').value;
		tomonth   = $('todate').value;
	}
	AgentSelect = $('agentSelect').value;
	
	if(month != 0 && tomonth != "" && frommonth != "")
	{
		_loading();
		new Ajax.Request("data/getSearchResults.php?months="+frommonth+"&tomonth="+tomonth,
		{
			onSuccess : function(response)
			{
//				alert(response.responseText);
				if(response.responseText != false)
				{
					try
					{
						var dataset = $H();
						dataset = eval("(" + response.responseText + ")");
	
						save_SearchDataSet(dataset);
						save_SearchOptions(dataset);
						
						rights             = dataset.Rights;
						officeID           = dataset.OfficeID;
						sessionassociates  = dataset.sessionassociates;
						teamID		   	   = dataset.teamleaderID;
						teamYN		       = dataset.teamleader;
						AssociateViewGroup = dataset.AssociateViewGroup;
						AssociateViewAll   = dataset.AssociateViewAll;
						//alert(rights + " " + permission + " " + sessionassociates);
						define_Search_Options();
						//popluate_Search_Options("suburbSelect",SearchDataSet.Location,0);
					}
					catch(e)
					{
						$('debugger').innerHTML += e.message;
					}
				}
				else
				{
					$("suburbSelect").options[0] = new Option("No Suburbs In Criteria",0);
					$("suburbSelect").disabled = true;
				}
			},
			onFailure : function(response) 
			{
				alert("error loading " + url + "...");
			},
			parameters : "AgentSelect=" + AgentSelect
		});
	}
	else
	{
		$("suburbSelect").options[0] = new Option("No Suburbs In Criteria",0);
		$("suburbSelect").disabled = true;	
	}
}


function fetch_QuickSearch_Dataset(QuickSearch)
{
	currentSearchState = 'quicksearch';
	$('Loading2').style.display = 'block';
	$("webref_button").disabled    = true;
	$("address_button").disabled    = true;

	var value = "";
	if(QuickSearch == 'webref')
	{
		$('address_define').value = 'Address...';
		value = $('webref_define').value;
	}
	else
	{
		$('webref_define').value = 'WebRef...';
		value = $('address_define').value;
	}
	
	if(value == "")
	{
		value = "Blank";		
	}
	
	if(value != 'Address...' && value != 'WebRef...' && value != "Blank")
	{
		new Ajax.Request("data/getQuickSearchResults.php?QuickSearch="+QuickSearch+"&value="+value,
		{
			onSuccess : function(response)
			{
//				alert(response.responseText);
				if(response.responseText != 'false')
				{
					try
					{
						$("suburbSelect").options.length = 0;
						$("suburbSelect").options[0] = new Option("No Suburbs In Criteria",0);
						_loadingQuickSearch();
						if($('divAdvanceSearch').style.display != "none")
						{
							_openAdvance();
						}
						var dataset = $H();
						dataset = eval("(" + response.responseText + ")");
						save_SearchDataSet(dataset);
						save_SearchOptions(dataset);
						rights             = dataset.Rights;
						officeID           = dataset.OfficeID;
						sessionassociates  = dataset.sessionassociates;
						teamID		   	   = dataset.teamleaderID;
						teamYN		       = dataset.teamleader;
						AssociateViewGroup = dataset.AssociateViewGroup;
						AssociateViewAll   = dataset.AssociateViewAll;
						
						var Properties   = $H();
						
						$H(dataset.Properties).each
						(
							function(property)
							{	
								Properties[property.key] = $H(property.value);
							}
						);	
						SuburbFilter="##DEFAULT##";
						display_Results(Properties, 'other');
					}
					catch(e)
					{
						$('debugger').innerHTML += e.message;
					}
				}
				else
				{
					$('Loading2').style.display = 'none';
					var Properties   = $H();
					display_Results(Properties, 'other');
					alert('No results found for : '+ value);	
				}
			},
			onFailure : function(response) 
			{
				$('Loading2').style.display = 'none';
				alert("error loading " + url + "...");
			}
		});
	}
	else
	{
		$('Loading2').style.display = 'none';
		var Properties   = $H();
		display_Results(Properties, 'other');
		alert('No results found for : '+ value);
	}
	$("webref_button").disabled    = false;
	$("address_button").disabled    = false;
}

//################################################################################################################
//					Suburb Filter
//################################################################################################################
function filter_Handler(fieldName, functionToCall)
{
	var fieldObj = $(fieldName);
	if(trim(fieldObj.value)!="")
	{
		if(SuburbFilter!=trim(fieldObj.value).toUpperCase())
		{
			SuburbFilter = trim(fieldObj.value).toUpperCase();
			if(FilterTimer!=0){clearTimeout(FilterTimer);}
			FilterTimer = setTimeout(functionToCall, 1250);
		}
	}
	else
	{
		SuburbFilter="##DEFAULT##";
		if(FilterTimer!=0){clearTimeout(FilterTimer);}
		FilterTimer = setTimeout(functionToCall, 1250);
	}
}

function search_Criteria_Handler(event) 
{
	if($('webref_define').value != 'WebRef...' && $('address_define').value == 'Address...')
	{
		$('webref_define').value = 'WebRef...';
		$('address_define').value = 'Address...';
	}
	if(SearchTimer!=0){clearTimeout(SearchTimer);}
	_loading();
	SearchTimer = setTimeout('define_Search_Options()', 500);
}
function define_search_OptionsByWebRef()
{
	clearTimeout(SearchTimer);
	$('address_define').value = 'Address...';
	define_search_Results('WebRef',$('webref_define').value);
}
function define_search_OptionsByAddress()
{
	clearTimeout(SearchTimer);
	$('webref_define').value = 'WebRef...';
	define_search_Results('Address',$('address_define').value);
}
function define_Search_Options()
{
	clearTimeout(SearchTimer);
	$("suburbSelect").disabled = false;
	$("suburbSelect").options.length = 0;
	var PropertyStatus = new Array();
	for(status_id=1;status_id<=4;status_id++)
	{
		switch($("cb_status_"+status_id).checked)
		{
			case true:PropertyStatus[status_id] = status_id;break;
		}
	}
	var PropertyType = new Array();
	for(type_id=1;type_id<=8;type_id++)
	{
		switch($("cb_type_"+type_id).checked)
		{
			case true:PropertyType[type_id] = type_id;break;
		}
	}
	
	var agentSearchValue = $('agentSelect').options[$('agentSelect').selectedIndex].value;
	var Agent = (agentSearchValue!="loading Agents..."&&agentSearchValue!="")?agentSearchValue:"View All";
	var defineset = $H();
	defineset["Location"] = $H();
	defineset["Properties"] = $H();

	$H(SearchDataSet.Properties).each(function(property)
	{		
		var SuburbName = SearchDataSet.Location[property.value.RegionID].Suburbs[property.value.RegionID+"_"+property.value.SuburbID].Name;

		switch(define_Search_Options_check_property_criteria(property,Agent,PropertyStatus,PropertyType))
		{
			case true:
				var AddProp = false;
				if(SuburbFilter!="##DEFAULT##" && SuburbName.substring(0,SuburbFilter.length).toUpperCase() == SuburbFilter)
				{
					AddProp = true;
				}
				else if(SuburbFilter=="##DEFAULT##")
				{
					AddProp = true;
				}
				
				if(AddProp)
				{
					defineset.Properties[property.key] = $H(property.value);
					if(!defineset.Location[property.value.RegionID])
					{
						defineset.Location[property.value.RegionID]          = $H();
						defineset.Location[property.value.RegionID].Name 	 = SearchDataSet.Location[property.value.RegionID].Name
						defineset.Location[property.value.RegionID].Suburbs  = $H();
					}
					if(!defineset.Location[property.value.RegionID].Suburbs[property.value.RegionID+"_"+property.value.SuburbID])
					{
						defineset.Location[property.value.RegionID].Suburbs[property.value.RegionID+"_"+property.value.SuburbID] = $H();
						defineset.Location[property.value.RegionID].Suburbs[property.value.RegionID+"_"+property.value.SuburbID].Name = SearchDataSet.Location[property.value.RegionID].Suburbs[property.value.RegionID+"_"+property.value.SuburbID].Name;
					}
				}
				
			break;
			default:break;
		}
	});
	save_SearchOptions(defineset);
	popluate_Search_Options("suburbSelect",defineset.Location,0);
}
function define_Search_Options_check_property_criteria(property,Agent,StatusArray,TypeArray)
{
	var inCriteria     = new Array();
	//&& property.value.PropTypeID!=0
	if(!$H(StatusArray)[property.value.PropStatusID])
	{
		return false;
	}
	if(!$H(TypeArray)[property.value.PropTypeID])
	{
		return false;
	}

	if(property.value.AssociatesIDTwo != "false")
	{
		if(Agent !="View All" && Agent !="View Office" && property.value.AssociatesID != Agent && property.value.AssociatesIDTwo != Agent)
		{
			return false;
		}
	}
	else
	{
		if(Agent !="View All" && Agent !="View Office" && property.value.AssociatesID != Agent)
		{
			return false;
		}
	}
	if(Agent == "View Office" && property.value.OfficeID != officeID)
	{
		return false;
	}
	return true;
}

function popluate_suburbsForListings(idToPopulate,dataset,selectedIndex)
{
	var Indexing  = 0;
	var selectObj = $(idToPopulate);
	selectObj.options.length = 0;	
	$H(dataset).each(function(region)
	{
		selectObj.options[Indexing] = new Option(region.value.Name,region.key + "*");
		selectObj.options[Indexing].style.backgroundColor = '#666';
		selectObj.options[Indexing].style.color = '#fff';
		Indexing++;
		$H(region.value.Suburbs).each(function(suburb)
		{
			selectObj.options[Indexing] = new Option(suburb.value.Name,suburb.key);			
			if (selectObj.options[Indexing].value.substring(selectObj.options[Indexing].value.indexOf('_') + 1, selectObj.options[Indexing].value.length) == selectedIndex) 
			{
				selectObj.options[Indexing].selected = true;				
			}
			Indexing++;			
		});		
	});
}

function popluate_Search_Options(idToPopulate,dataset,selectedIndex)
{
	var Indexing  = 0;
	var selectObj = $(idToPopulate);
	selectObj.options.length = 0;	
	$H(dataset).each(function(region)
	{
		//_console(region.value.Name + "----" + region.key + "<br>")
		selectObj.options[Indexing] = new Option(region.value.Name,region.key);
		selectObj.options[Indexing].style.backgroundColor = '#666';
		selectObj.options[Indexing].style.color = '#fff';
		Indexing++;
		$H(region.value.Suburbs).each(function(suburb)
		{
			selectObj.options[Indexing] = new Option(suburb.value.Name,suburb.key);			
			if (selectObj.options[Indexing].value.substring(selectObj.options[Indexing].value.indexOf('_') + 1, selectObj.options[Indexing].value.length) == selectedIndex) 
			{
				selectObj.options[Indexing].selected = true;				
			}
			Indexing++;			
		});		
	});
	
	
	if(Indexing==0)
	{
		selectObj.options[0] = new Option("No Suburbs In Criteria",0);
		selectObj.disabled = true;
	}
	_loaded();
}
//################################################################################################################
//					GET SELECTED OPTIONS FROM SEARCH RESULTS
//################################################################################################################	
function results_Criteria_Handler()
{
	if(ResultsTimer!=0){clearTimeout(ResultsTimer);}
	ResultsTimer = setTimeout('define_Results()', 750);
}
//	searchObject.search(Filter) >= 0
	//property.value[type].toUpperCase().search(value.toUpperCase()) >= 0)//!= '-1') 
function define_search_Results(type, value)
{
	var Properties   = $H();
	//SearchOptions SearchDataSet
	$H(SearchDataSet.Properties).each
	(
		function(property)
		{	
			var searchObject = property.value[type].toUpperCase();	
		    if (searchObject.search(value.toUpperCase()) >= 0)
			{			
				Properties[property.key] = $H(property.value);
			}		
		}
	);	
	display_Results(Properties, '');
	SuburbFilter="##DEFAULT##";
}

function define_Results()
{
	var region_set   = $H();
	var suburbs_set  = $H();
	var Location     = $H(); 
	var Properties   = $H(); 
	var region_count = 0;
	var suburb_count = 0;
	var prop_count   = 0;
	
	getSelectedOptions($("suburbSelect")).each(function(item)
	{
		switch(isNaN(item))
		{
			case true:
				var SuburbID 	   = item.substring(strpos("_",item)+1,item.length)
				Location[SuburbID] = SuburbID;
			break;
			default:
				region_set[item]=item;
			break;
		}	
	});
	region_set.each(function(region)
	{
		$H(SearchOptions.Location[region.key].Suburbs).each(function(suburb)
		{
			var SuburbID = suburb.key.substring(strpos("_", suburb.key)+1, suburb.key.length)
			if(!Location[SuburbID])
			{
				Location[SuburbID] = SuburbID;
			}
		});
	});
	$H(SearchOptions.Properties).each(function(property)
	{
		switch(!Location[property.value.SuburbID])
		{
			case true:break;
			default:Properties[property.key] = $H(property.value);break;
		}
	});
	display_Results(Properties, '');
}

function quickSave()
{
	var Properties   = $H();
	$H(SearchOptions.Properties).each(function(property)
	{
			Properties[property.key] = $H(property.value);
	});
	display_Results(Properties, '');
}
//################################################################################################################
//					BUILD RESULT DISPLAY HTML
//################################################################################################################
function display_Results(Properties, othertest)
{	
	printhtmlcode = $A();
	html = "";
	//$("search_results_table").innerHTML="";
	$('divResultSet').style.height = "0px";
	
	html = "<table class='sortable' id='search_results_table' width='98%' bgcolor='#ffffff' cellpadding='1' cellspacing='0' border='1' bordercolor='#999999' style='cursor:hand;border-collapse:collapse;'>"+
						"<tr>" +
							"<th align='center'>"+
								"<b>WebRef</b>" +
							"</th>"+
							"<th align='center'>"+
								"<b>Suburb</b>" +
							"</th>"+
							"<th align='center'>"+
								"<b>Agent</b>" +
							"</th>"+
							"<th align='center'>"+
								"<b>Address</b>" +
							"</th>"+
							"<th align='center' id='testme'>"+
								"<b>Price</b>" +
							"</th>"+
							"<th align='center'>"+
								"<b>DateListed</b>" +
							"</th>"+
							"<th align='center'>"+
								"<b>Sale/Rental</b>" +
							"</th>"+
							"<th align='center'>"+
								"<b>Status</b>" +
							"</th>"+
						"</tr>";
	var incremneter = 0;
	var TypeDisplay = "";
	var addressView = "";
	var types		= "";
	var FullName	= "";
	var statusVal	= "";
	var EditGroup   = "";
	var agentSearchValue = $('agentSelect').options[$('agentSelect').selectedIndex].value;
	var Agent = (agentSearchValue!="loading Agents..."&&agentSearchValue!="")?agentSearchValue:"View All";
	$H(Properties).each(function(property)
	{
//		alert(property.value.WebRef);
		printhtmlcode[incremneter] = property.value.WebRef;
		incremneter++;

		/*if(property.value.PaymentID == 3)
		{
			TypeDisplay = "Auction";
		}
		else
		{
			if(property.value.TypeName == null || property.value.TitleDeedID == 1 || property.value.TitleDeedID == 2)
			{
				TypeDisplay = (property.value.TitleDeedID == 1)?"Share Block":"Business";
			}
			else
			{
				TypeDisplay = property.value.TypeName;
			}
		}*/
		if(rights == 'Y' && officeID == property.value.OfficeID || sessionassociates == property.value.AssociatesID && rights == 'E' || teamID == property.value.teamid && teamYN == 'Y' && rights == 'E' || currentProcessState == 'add' || AssociateViewGroup == 1 || AssociateViewAll == 1)
		{
			addressView = property.value.Address;
		}
		else
		{
			addressView = "n/a";
		}
		if(property.value.AssociatesIDTwo != "false")
		{
			if(property.value.AssociatesIDTwo == Agent)
			{
				FullName = property.value.FullNameTwo;
			}
			else
			{
				FullName = property.value.FullName;
			}
		}
		else
		{
			FullName = property.value.FullName;
		}
//		if(property.value.PropTypeID == 8)
//		{
//			types = TypesDataSet[property.value.PropTypeID];	
//		}
//		else
//		{
//			types = TypesDataSet[property.value.PropTypeID]+" ("+StylesDataSet[property.value.StyleID]+")";
//		}
//		alert(property.value.teamid);
		if(property.value.PaymentID == 1)
		{
			types = "Rent";
		}
		else if(property.value.PaymentID == 2)
		{
			types = "Sale";
		}
		else
		{
			types = "N/A";
		}
		
		if(property.value.PropStatusID == 3 && property.value.PaymentID == 1)
		{
			statusVal = "Leased";
		}
		else if(property.value.PropStatusID == 3 && property.value.PaymentID == 2)
		{
			statusVal = "Sold";
		}
		else
		{
			statusVal = StatusDataSet[property.value.PropStatusID];
		}
		EditGroup = property.value.EditGroup;
		html += "<tr id='"+property.value.WebRef+"' onMouseOver=\"MouseOver('"+property.value.WebRef+"');\" onMouseOut=\"MouseOut('"+property.value.WebRef+"');\" onclick=\"getRowDetails(" + property.key.substring(0,property.key.length) + ",'" + property.value.PropTypeID + "','" + property.value.TitleDeedID + "','" + property.value.PaymentID + "','" + property.value.TransactionID + "','" + property.value.AssociatesID + "','" + property.value.OfficeID + "','" + property.value.teamid + "','"+property.value.WebRef+"', '"+EditGroup+"')\">" +
					"<td>"+
						property.value.WebRef +
					"</td>"+
					"<td>"+
						SearchOptions.Location[property.value.RegionID].Suburbs[property.value.RegionID+"_"+property.value.SuburbID].Name +
					"</td>"+
					"<td>"+
						FullName +
					"</td>"+
					"<td>"+
						addressView +
					"</td>"+
					"<td>"+
						property.value.MandatePrice +
					"</td>"+
					"<td>"+
						property.value.DateListed +
					"</td>"+
					"<td>"+
						types + 
					"</td>"+
					"<td>"+
						statusVal+
					"</td>"+
				"</tr>";
	});
	
	html += "</table>";
	$('divResultSet').style.overflow="auto";
	$('divResultSet').style.height = ($A(Properties).length>10)?"180px":(($A(Properties).length*18)+18)+"px";
	
	$('divPrintResultSet').innerHTML = "<img src='../_images/printtoolbar/reloadbutton.JPG' width='24' height='24' onClick='loadSearchForm();'>&nbsp;<img src='../_images/printtoolbar/excel.PNG' width='24' height='24' onClick='printResults();'>";
	
	$('divResultSet').innerHTML = html;
	sortables_init();

	//_console("PROPERTIES FOUND :::"+$A(Properties).length+ " ~~~~~ HEIGHT ::::"+$('divResultSet').style.height);
	Effect.BlindDown($('divResultSet'),{queue: 'end', scope: 'togglebar', limit:1})

	//Sorts the result by Price.
	var clid = 4;
	var td = $('testme').parentNode;
    var column = clid || td.cellIndex;
    var table = getParent(td,'TABLE');
    
    if (table.rows.length <= 1) return;
    var itm = ts_getInnerText(table.rows[1].cells[column]);
    sortfn = ts_sort_caseinsensitive;
    if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
    if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
    if (itm.match(/^[£$]/)) sortfn = ts_sort_currency;
    if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric;
    SORT_COLUMN_INDEX = column;
    var firstRow = new Array();
    var newRows = new Array();
    for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; }
    for (j=1;j<table.rows.length;j++) { newRows[j-1] = table.rows[j]; }
    
    newRows.sort(sortfn);
   // newRows.reverse();
    
    // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
    // don't do sortbottom rows
    for (i=0;i<newRows.length;i++) { if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) table.tBodies[0].appendChild(newRows[i]);}
    // do sortbottom rows only
    for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);}
    
    if(othertest == 'other')
    {
    	$('Loading2').style.display = 'none';
    }
}

//################################################################################################################
//					UTILITIES
//################################################################################################################
function strpos(needle,haystack)
{
	for(i=0;i<=haystack.length;i++)
	{
		switch(haystack.charAt(i))
		{
			case needle:return i;
		}
	}
	return false;
}
function set_delay_height(element,height)
{
	var element = $(element); 
	element.style.height = height;
}
function link(linklocation)
{
	window.opener.document.location.href=linklocation;
}
function _simulate_ctrl_press(event)
{
	/*if( event.preventDefault ) { event.preventDefault(); }
	event.returnValue = false;
	var fireOnThis = event.target;
	_console("CONTROL :: "+fireOnThis.id);
	var evObj = document.createEvent('MouseEvents');
	Event.stopObserving(event.target.parentNode,'click',_simulate_ctrl_press);
	evObj.initMouseEvent( 'click', false, false, window, event.detail, event.screenX, event.screenY, event.clientX, event.clientY, true, false, false, false, 1, fireOnThis);
	fireOnThis.dispatchEvent(evObj);
	Event.observe(event.target.parentNode,"click",_simulate_ctrl_press,false);
	_console("TYPE :: "+evObj.type);
	_console("CTRL :: "+evObj.ctrlKey);
	_console("<br>");*/
	
}

function test_click(event)
{
	//alert("clicked");
}
function _console(input)
{
	$("debugger").innerHTML +="<br>"+input;
}

function element_clearonce(element,defaultvalue)
{
	switch(element.value == defaultvalue)
	{
		case true:element.value = "";
		break;
	}
}
function trim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function _loading()
{
	//_console("loading");
	$("suburbloader").style.display = "block";
	$("suburbloaded").style.display = "none";
	$("suburbSelect").disabled 	    = true;
	$("suburb_define").disabled     = true;
//	$("webref_define").disabled     = true;
//	$("address_define").disabled    = true;
	$("agentSelect").disabled       = false;
//	$("expire").disabled			= true;
//	$("inactive").disabled			= true;
	$("incomplete").disabled		= true;
//	$("webref_button").disabled     = true;
//	$("address_button").disabled    = true;
	$("View_Date_Range").disabled   = true;
	$("view_all").disabled  		= true;
//	$("listing_date_months_back_select").disabled = true;
	for(id=1;id<=8;id++)
	{
			if(id < 5)
			{
				$("cb_status_"+id).disabled = true;
			}
//		if(id != 6)
//		{
			$("cb_type_"+id).disabled   = true;
//		}
	}
}

function _loadingQuickSearch()
{
//	$("suburbloader").style.display = "block";
//	$("suburbloaded").style.display = "none";
	$("suburb_define").disabled     = true;
	$("suburbSelect").disabled 	    = true;
//	$("agentSelect").disabled       = false;
	$("incomplete").disabled		= true;
	for(id=1;id<=8;id++)
	{
			if(id < 5)
			{
				$("cb_status_"+id).disabled = true;
			}
//		if(id != 6)
//		{
			$("cb_type_"+id).disabled   = true;
//		}
	}
}

function _loaded()
{
	$("suburbloader").style.display = "none";
	$("suburbloaded").style.display = "block";
	$("suburbSelect").disabled 	    = false;
	$("suburb_define").disabled     = false;
//	$("webref_define").disabled     = false;
//	$("address_define").disabled    = false;
//	$("agentSelect").disabled     	= false;
//	$("expire").disabled			= false;
//	$("inactive").disabled			= false;
	$("incomplete").disabled		= false;
//	$("webref_button").disabled     = false;
//	$("address_button").disabled    = false;
	$("View_Date_Range").disabled   = false;
	$("view_all").disabled  		= false;
//	$("listing_date_months_back_select").disabled = false;
	for(id=1;id<=8;id++)
	{
			if(id < 5)
			{
				$("cb_status_"+id).disabled = false;
			}
//		if(id != 6)
//		{
			$("cb_type_"+id).disabled   = false;
//		}
	}
	//_console("loaded");
}

function define_Search_OptionsAfterTypeChanged()
{
	$('webref_define').value = currentWebRef;
	define_search_OptionsByWebRef();	
}

function define_search_incomplete()
{	
	var Properties   = $H();
	$H(SearchOptions.Properties).each(function(property)
	{		
		if ((parseInt( property.value['Complete']) == 0))
		{
			Properties[property.key] = $H(property.value);
		}
		
	});	
	display_Results(Properties, '');
}

function define_search_expire(type, today, enddate, currentsessionOfficeID)
{	
	var Properties   = $H();
	$H(SearchOptions.Properties).each(function(property)
	{		
	    if (property.value[type] >= today && property.value[type] <= enddate && property.value['Complete'] == 1 && StatusDataSet[property.value.PropStatusID] == 'Active' && property.value['OfficeID'] == currentsessionOfficeID) 
		{			
			Properties[property.key] = $H(property.value);
		}		
	});	
	display_Results(Properties, '');
}

function _inactive_users_listings()
{
	_multiuse('?type=inactive');
}

function _expiring_this_month()
{
	new Ajax.Request("data/getExpireDate.php",
	{
			onSuccess : function(response) 
			{
				var dataset = $H();
				dataset = eval("(" + response.responseText + ")");
				_multiuse("?todaydate="+dataset.todaydate + "&thirdaysLater=" + dataset.thirtydayslater+"");
			},
			onFailure : function(request) {
				alert("error loading AgentsUpdateSection form ...");
			},
			onComplete : function(request) 
			{
				
			}
	});
}

function _multiuse(Extension)
{
//	alert(Extension);
	currentSearchState = 'quicksearch';
	$('Loading2').style.display = 'block';
	new Ajax.Request("data/getInactiveUsersListings.php" + Extension,
		{
			onSuccess : function(response)
			{
//				alert("-"+ response.responseText+"-");
				if(response.responseText !== false && response.responseText !== 'false')
				{
					try
					{
						$("suburbSelect").options.length = 0;
						$("suburbSelect").options[0] = new Option("No Suburbs In Criteria",0);
						_loadingQuickSearch();
						if($('divAdvanceSearch').style.display != "none")
						{
							_openAdvance();
						}
						var dataset = $H();
						dataset = eval("(" + response.responseText + ")");
						save_SearchDataSet(dataset);
						save_SearchOptions(dataset);
						rights             = dataset.Rights;
						officeID           = dataset.OfficeID;
						sessionassociates  = dataset.sessionassociates;
						teamID		   	   = dataset.teamleaderID;
						teamYN		       = dataset.teamleader;
						AssociateViewGroup = dataset.AssociateViewGroup;
						AssociateViewAll   = dataset.AssociateViewAll;
						
						var Properties   = $H();
						
						$H(dataset.Properties).each
						(
							function(property)
							{	
								Properties[property.key] = $H(property.value);
							}
						);	
						SuburbFilter="##DEFAULT##";
						display_Results(Properties, 'other');
					}
					catch(e)
					{
						$('debugger').innerHTML += e.message;
					}
				}
				else
				{
					$('Loading2').style.display = 'none';
					var Properties   = $H();
					display_Results(Properties, 'other');
					alert('No results found');	
				}
			},
			onFailure : function(response) 
			{
				$('Loading2').style.display = 'none';
				alert("error loading " + url + "...");
			}
		});
}

function _openAdvance()
{
	if($('divAdvanceSearch').style.display == "none")
	{
		$('divAdvanceSearch').style.display = "block";
		$('changeThisImage').innerHTML = "<a href='javascript:;' onClick='_openAdvance();' style='text-decoration:none;'><font color='#822100'>Advanced Search</font>&nbsp;<img border='0' src='../_images/advancedArrows/arrow_up.png' align='absmiddle'/>&nbsp;</a>";
	}
	else
	{
		$('divAdvanceSearch').style.display = "none";
		$('changeThisImage').innerHTML = "<a href='javascript:;' onClick='_openAdvance();' style='text-decoration:none;'><font color='#822100'>Advanced Search</font>&nbsp;<img border='0' src='../_images/advancedArrows/arrow_dn.png' align='absmiddle'/>&nbsp;</a>";
	}
}

function Enterize (e, pfnCall, Param)
{
      var cKey = ( e )?e.keyCode:window.event.keyCode;

      if (cKey == 10 || cKey == 13)
      {
          pfnCall(Param);
      }
}
