var SEARCH_CORE = $H();
//SYSTEM SESSION VARS
SEARCH_CORE.CONTROLLER_CALL_URL    	   		    = "/?page=api_speed&content=main&params=";
SEARCH_CORE.HTML_Elements     					= $H();
SEARCH_CORE.DATA               					= $H();
SEARCH_CORE.MEM_STATE    	    				= $H();
SEARCH_CORE.TMP                					= $H();
SEARCH_CORE.STYLE    	   						= $H();
SEARCH_CORE.STYLE.Selects      					= $H();
SEARCH_CORE.CONST                               = $H();
SEARCH_CORE.RELOAD                              = $H();
SEARCH_CORE.RELOAD.DATA 					    = $A();
//CONSTANTS
//STYLES
SEARCH_CORE.STYLE.Selects.Headings_BG_Color    	= "#003466";
SEARCH_CORE.STYLE.Selects.Headings_Font_Color  	= "#ffffff";

//FILTER
SEARCH_CORE.MEM_STATE.search_Suburb_filter          = $H();
SEARCH_CORE.MEM_STATE.search_Suburb_filter.TimerID  = 0;
SEARCH_CORE.MEM_STATE.search_Suburb_filter.Value    = "";
SEARCH_CORE.MEM_STATE.search_District   		    = false;

SEARCH_CORE.CONST.Price_Ranges 					    = $H();
SEARCH_CORE.CONST.Price_Ranges.Sale 				= $H();

SEARCH_CORE.CONST.Price_Ranges.Sale[0]    			= 100000;
SEARCH_CORE.CONST.Price_Ranges.Sale[1]    			= 200000;
SEARCH_CORE.CONST.Price_Ranges.Sale[2]    			= 300000;
SEARCH_CORE.CONST.Price_Ranges.Sale[3]    			= 400000;
SEARCH_CORE.CONST.Price_Ranges.Sale[4]    			= 500000;
SEARCH_CORE.CONST.Price_Ranges.Sale[5]    			= 600000;
SEARCH_CORE.CONST.Price_Ranges.Sale[6]    			= 700000;
SEARCH_CORE.CONST.Price_Ranges.Sale[7]    			= 800000;
SEARCH_CORE.CONST.Price_Ranges.Sale[8]    			= 900000;
SEARCH_CORE.CONST.Price_Ranges.Sale[9]    			= 1000000;

SEARCH_CORE.CONST.Price_Ranges.Rental 	   			= $H();

SEARCH_CORE.CONST.Price_Ranges.Rental[0]  			= 1000;
SEARCH_CORE.CONST.Price_Ranges.Rental[1]  			= 2000;
SEARCH_CORE.CONST.Price_Ranges.Rental[2]  			= 3000;
SEARCH_CORE.CONST.Price_Ranges.Rental[3]  			= 4000;
SEARCH_CORE.CONST.Price_Ranges.Rental[4]  			= 5000;
SEARCH_CORE.CONST.Price_Ranges.Rental[5]  			= 6000;
SEARCH_CORE.CONST.Price_Ranges.Rental[6]  			= 7000;
SEARCH_CORE.CONST.Price_Ranges.Rental[7]  			= 8000;
SEARCH_CORE.CONST.Price_Ranges.Rental[8]  			= 9000;
SEARCH_CORE.CONST.Price_Ranges.Rental[9]  			= 10000;

SEARCH_CORE.CONST.FormatHint = 'Example Format: $100,000';

SEARCH_CORE.RELOAD.STATE            		        = false;
SEARCH_CORE.DefaultProvince = 0;

var CurrentPaymentType = 0;

var SetpaymentID       = 2;

var FlashMapFrame = new Array();
// eg. FlashMapFrame[provinceID] = Frame Number 
FlashMapFrame[1] = 6 //Auckland;
FlashMapFrame[2] = 97 //Bay Of Plenty;
FlashMapFrame[3] = 297 //Canterbury;
FlashMapFrame[4] = 46 //Coromandel;
FlashMapFrame[5] = 116 //Gisborne;
FlashMapFrame[6] = 136 //Hawkes Bay;
FlashMapFrame[7] = 157 //Manawatu;
FlashMapFrame[8] = 237 //Marlborough;
FlashMapFrame[9] = 257 //Nelson & Bays;
FlashMapFrame[11] = 317 //North Otago;
FlashMapFrame[10] = 26 //Northland;
FlashMapFrame[25] = 1 //Offshore Islands;
FlashMapFrame[12] = 317 //Otago;
FlashMapFrame[22] = 317 //Otago Lakes District;
FlashMapFrame[23] = 1 //Pacific Islands;
FlashMapFrame[13] = 337 //Southland;
FlashMapFrame[14] = 1 //Steward Island;
FlashMapFrame[15] = 177 //Taranaki;
FlashMapFrame[16] = 76 //Waikato;
FlashMapFrame[19] = 217 //Wairarapa;
FlashMapFrame[18] = 357 //Wanganui;
FlashMapFrame[17] = 197 //Wellington;
FlashMapFrame[20] = 277 //West Coast;




function RepopulateCurrentRegion ()
{
	var oOption = $("search_Province_select").options[$("search_Province_select").selectedIndex];
	
	_search_Province_Handler(oOption.text, oOption.value);
	
}

function property_search_init(OfficeIDs,ProvinceID,ProvinceName,UserIDs,GET,PaymentID)
{
	if(PaymentID)
	{
		SetpaymentID = PaymentID;
	}
	if(OfficeIDs)
	{
		SEARCH_CORE.DATA.OfficesIDs =  unserialize(OfficeIDs).join(",");
		SEARCH_CORE.DefaultProvince = 0;
	}
	if(UserIDs)
	{
		SEARCH_CORE.DATA.UserIDs =  unserialize(UserIDs).join(",");
	}

	_init_Search_HTML_Elements();
	_init_Search_Observers();
	
	_get_Property_Types();
	_get_Property_PaymentTypes();
	
	
	if(RESULTS_CORE_In_Data)
	{
		//_console("reloading");
		SEARCH_CORE.RELOAD.STATE            		= true;
		SEARCH_CORE.RELOAD.TMP  					= unserialize(RESULTS_CORE_In_Data);
					 
		SEARCH_CORE.RELOAD.DATA['Province'] 		= SEARCH_CORE.RELOAD.TMP['Province'].split(',');

//		SEARCH_CORE.RELOAD.DATA['Regions']  		= SEARCH_CORE.RELOAD.TMP['Regions'].split(',');
		SEARCH_CORE.RELOAD.DATA['Suburbs']  		= SEARCH_CORE.RELOAD.TMP['Suburbs'].split(',');
		SEARCH_CORE.RELOAD.DATA['Types']    		= SEARCH_CORE.RELOAD.TMP['Types'].split(',');
		SEARCH_CORE.RELOAD.DATA['Suburbs']  		= SEARCH_CORE.RELOAD.TMP['Suburbs'].split(',');

		SEARCH_CORE.RELOAD.DATA['Payment']  	    = SEARCH_CORE.RELOAD.TMP['Payment'];
		if(SEARCH_CORE.RELOAD.TMP['Rental_MIN'])
		{
			SEARCH_CORE.RELOAD.DATA['Rental_MIN']  	    = SEARCH_CORE.RELOAD.TMP['Rental_MIN'];
		}
		if(SEARCH_CORE.RELOAD.TMP['Rental_MAX'])
		{
			SEARCH_CORE.RELOAD.DATA['Rental_MAX']  	    = SEARCH_CORE.RELOAD.TMP['Rental_MAX'];
		}
		if(SEARCH_CORE.RELOAD.TMP['Sale_MIN'])
		{
			SEARCH_CORE.RELOAD.DATA['Sale_MIN']  	    = SEARCH_CORE.RELOAD.TMP['Sale_MIN'];
		}
		if(SEARCH_CORE.RELOAD.TMP['Sale_MAX'])
		{
			SEARCH_CORE.RELOAD.DATA['Sale_MAX']  	    = SEARCH_CORE.RELOAD.TMP['Sale_MAX'];
		}
		SEARCH_CORE.RELOAD.TMP 						= $H();

		_repopulate_Search();	

	}
	else
	{
	
		//_console("console new search");
		setTimeout(RepopulateCurrentRegion, 100);
	}
}

function _repopulate_Search()
{
	SEARCH_CORE.HTML_Elements.search_Province_select.disabled   = true;
	
	_search_ProvinceMap_Handler(SEARCH_CORE.RELOAD.DATA['Province'][0]);
}

function _init_Search_HTML_Elements()
{
	SEARCH_CORE.HTML_Elements.search_Province_select_div	 		= $('search_Province_select_div');
	SEARCH_CORE.HTML_Elements.search_Province_select	 		    = $('search_Province_select');
	
	if (SEARCH_CORE.DefaultProvince)
	{
		SEARCH_CORE.HTML_Elements.search_Province_select.selectedIndex = SEARCH_CORE.DefaultProvince;
	}
	
	SEARCH_CORE.HTML_Elements.search_Suburb_filter   	  			= $('search_Region_Suburb_filter');
	SEARCH_CORE.HTML_Elements.search_Suburb_filter.value 			= "Suburb Filter";
	
	SEARCH_CORE.HTML_Elements.search_Region_Suburb_select_div 	  	= $('search_Region_Suburb_select_div');
	SEARCH_CORE.HTML_Elements.search_Location_select 	  			= $('search_Region_Suburb_select');
	$('search_Region_Suburb_select').style.maxWidth = $('search_Region_Suburb_select').offsetWidth + 'px';
	
	SEARCH_CORE.HTML_Elements.search_Property_Type_select_div       = $('search_Property_Type_select_div');
	SEARCH_CORE.HTML_Elements.search_Type_select     	  			= $('search_Property_Type_select');
	SEARCH_CORE.HTML_Elements.search_Type_select.hideFocus 			= true;
			
	//SEARCH_CORE.HTML_Elements.search_PaymentType_select			    = $('search_PaymentType_select');
	SEARCH_CORE.HTML_Elements.search_PaymentType_select_div		    = $('search_PaymentType_select_div');
	SEARCH_CORE.HTML_Elements.search_PaymentType_check_div			= $('search_PaymentType_check_div');
	SEARCH_CORE.HTML_Elements.payment_options_radio_group			= $('remax_search_form').payment_options_radio_group;
	
	SEARCH_CORE.HTML_Elements.search_Sale_price_options_div         = $('search_Sale_price_options_div');
	SEARCH_CORE.HTML_Elements.search_Sale_Count_lable          	    = $('search_Sale_Count');
	SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select        	= $('search_Sale_Min_Price_select');
	SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select          = $('search_Sale_Max_Price_select');
	
	SEARCH_CORE.HTML_Elements.search_Rental_price_options_div       = $('search_Rental_price_options_div');
	SEARCH_CORE.HTML_Elements.search_Rental_Count_label          	= $('search_Rental_Count');
	SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select        = $('search_Rental_Min_Price_select');
	SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select        = $('search_Rental_Max_Price_select');
	
	SEARCH_CORE.HTML_Elements.search_Surrounding_Suburbs_check		= $('search_Surrounding_Suburbs_check');

	SEARCH_CORE.HTML_Elements.search_Submit_button          		= $('search_Submit_button');
}

function _init_Search_Observers()
{
	Event.observe(SEARCH_CORE.HTML_Elements.search_Province_select, 	   'change',  _search_Province_init, false);
	Event.observe(SEARCH_CORE.HTML_Elements.search_Location_select, 	   'change',  _populate_Type_List, false);
	Event.observe(SEARCH_CORE.HTML_Elements.search_Type_select, 	 	   'change',  _search_Payment_Handler,     false);
	Event.observe(SEARCH_CORE.HTML_Elements.search_Submit_button, 	  	   'click',   _search_Submit_Handler,   false);
	Event.observe(SEARCH_CORE.HTML_Elements.search_Suburb_filter,   	   'focus',   _search_Filter_Handler,   false);
	Event.observe(SEARCH_CORE.HTML_Elements.search_Suburb_filter,   	   'keyup',   _search_Filter_Handler,   false);
	Event.observe(SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select,  'change',  _search_Sale_Price_Handler,   false);
	Event.observe(SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select,  'change',  _search_Sale_Price_Handler,   false);
}
function _search_Province_init()
{
	_search_Province_Handler();
	_control_Flash_Map(SEARCH_CORE.DATA.ProvinceID);

}
function _control_Flash_Map(province_id)
{

	frame = FlashMapFrame[province_id];
	$('flashnzmap').GotoFrame(frame);
	$('flashnzmap').Play();

}

function _search_ProvinceMap_Handler(provinceID,provinceName)
{
	if(provinceID)
	{
		SEARCH_CORE.HTML_Elements.search_Province_select.selectedIndex 				     = "-1";
		for(i =0;i<=(SEARCH_CORE.HTML_Elements.search_Province_select.options.length-1);i++)
		{
			switch(true)
			{
				case(SEARCH_CORE.HTML_Elements.search_Province_select.options[i].value == provinceID):
					
					switch(true)
					{
						case(SEARCH_CORE.HTML_Elements.search_Province_select.selectedIndex !=SEARCH_CORE.HTML_Elements.search_Province_select.options[i].index):
							SEARCH_CORE.HTML_Elements.search_Province_select.selectedIndex  = SEARCH_CORE.HTML_Elements.search_Province_select.options[i].index;
							_search_Province_Handler();
						break;
					}
					
				return;
			}
		}
		SEARCH_CORE.HTML_Elements.search_Province_select.options[SEARCH_CORE.HTML_Elements.search_Province_select.options.length]          = new Option(provinceName,provinceID);
		SEARCH_CORE.HTML_Elements.search_Province_select.options[SEARCH_CORE.HTML_Elements.search_Province_select.options.length-1].selected = true;
		_search_Province_Handler();
		//setTimeout("_search_Province_Handler();",50);
	}

}

function _set_sub_region (SubRegionID, SubRegionName)
{
	SEARCH_CORE.DATA.SubRegionID = SubRegionID;
//	_search_Province_Handler();
//	$('search_Region_Suburb_filter').value = SubRegionName;
	

	_populate_District_Suburb_List();	
	//_search_Filter_Handler();
}

function _search_Filter_Handler()
{	
	if(!SEARCH_CORE.MEM_STATE.search_Suburb_filter.Activated)
	{
		SEARCH_CORE.MEM_STATE.search_Suburb_filter.Activated 	= "true";
		SEARCH_CORE.HTML_Elements.search_Suburb_filter.value 	= "";
		return;
	}

	if(SEARCH_CORE.MEM_STATE.search_Suburb_filter.TimerID!=0){clearTimeout(SEARCH_CORE.MEM_STATE.search_Suburb_filter.TimerID);}
	switch(true)
	{
		case(trim(SEARCH_CORE.HTML_Elements.search_Suburb_filter.value)!="" && 
		trim(SEARCH_CORE.HTML_Elements.search_Suburb_filter.value).toUpperCase()!=SEARCH_CORE.MEM_STATE.search_Suburb_filter.Value):
	   		 SEARCH_CORE.MEM_STATE.search_Suburb_filter.TimerID = setTimeout('_search_Filter()', 750);
		break;
		case(SEARCH_CORE.MEM_STATE.search_Suburb_filter.Activated && trim(SEARCH_CORE.HTML_Elements.search_Suburb_filter.value) == 
					"" && SEARCH_CORE.MEM_STATE.search_Suburb_filter.Value !=""):
			SEARCH_CORE.MEM_STATE.search_Suburb_filter.Value 	= "";	
			_search_Filter() 
		//	_populate_Region_Suburb_List();	
		break;
	}
}
function _search_Filter()
{
	clearTimeout(SEARCH_CORE.MEM_STATE.search_Suburb_filter.TimerID);
	SEARCH_CORE.MEM_STATE.search_Suburb_filter.Value = trim(SEARCH_CORE.HTML_Elements.search_Suburb_filter.value).toUpperCase();
	if (SEARCH_CORE.MEM_STATE.search_District ){
		_populate_District_Suburb_List();
    }else {	
    	_populate_Region_Suburb_List();
    }
    
}

function _search_Province_Handler()
{

	SEARCH_CORE.HTML_Elements.search_Suburb_filter.disabled           = true;
	SEARCH_CORE.HTML_Elements.search_Suburb_filter.value 			  = "Suburb Filter";
	SEARCH_CORE.MEM_STATE.search_Suburb_filter.Value 				  = "";	
	SEARCH_CORE.HTML_Elements.search_Location_select.disabled         = true; 
	//SEARCH_CORE.HTML_Elements.search_PaymentType_select.disabled      = true;
	SEARCH_CORE.MEM_STATE.search_Suburb_filter.Activated              = false;
	SEARCH_CORE.HTML_Elements.search_Type_select.disabled             = true; 
	SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select.disabled   = true;
	SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select.disabled   = true;
	SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select.disabled = true;
	SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select.disabled = true;
	SEARCH_CORE.HTML_Elements.search_Location_select.style.textAlign = "left";

	_search_loading_layer(SEARCH_CORE.HTML_Elements.search_Region_Suburb_select_div,SEARCH_CORE.HTML_Elements.search_Location_select,true);
	_search_loading_layer(SEARCH_CORE.HTML_Elements.search_Property_Type_select_div,SEARCH_CORE.HTML_Elements.search_Type_select,true);		
	
	SEARCH_CORE.DATA.ProvinceID = get_Selected_Options(SEARCH_CORE.HTML_Elements.search_Province_select,true);
	_get_Province_Properties(SEARCH_CORE.DATA.ProvinceID);

}
function _search_Type_Handler(e)
{
	switch(e.currentTarget.options[e.currentTarget.selectedIndex].value)
	{
	
	}
}
function _search_PaymentType_Handler()
{
	if (SEARCH_CORE.HTML_Elements.payment_options_radio_group)
	{
		var payment = get_Selected_Radio(SEARCH_CORE.HTML_Elements.payment_options_radio_group);
		
		switch(payment)
		{
			case 1:
				// rent
				CurrentPaymentType = payment;
				SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select.disabled   = true;
				SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select.disabled   = true;
				SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select.disabled = false;
				SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select.disabled = false;
			break;
			case 2:
				CurrentPaymentType = payment;
				// sale
				SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select.disabled   = false;
				SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select.disabled   = false;
				SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select.disabled = true;
				SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select.disabled = true;
			break;
			case 0:
				SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select.disabled   = true;
				SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select.disabled   = true;
				SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select.disabled = true;
				SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select.disabled = true;
			break;
		}
	}
}

function _search_Sale_Price_Handler()
{
	
}

function _get_Province_Properties(province_array)
{
	//FETCH ALL PROPERTIES FROM PROPERTY SERACH TABLE WITH SELECTED PROVINCE IDS
	var data = $A();
	data['property_search_options'] = $A();
	data['property_search_options']['officeIDs']       = SEARCH_CORE.DATA.OfficesIDs;
	data['property_search_options']['userIDs']         = SEARCH_CORE.DATA.UserIDs;
	data['property_search_options']['provinceIDs']     = province_array.join(",");
	data['property_search_options']['paymentIDs']	   = SetpaymentID;
	
	CtrlCall(SEARCH_CORE.CONTROLLER_CALL_URL+"feed:property_search_options;filter:property_search_options;","data="+serialize(data),"_load_Province_Properties");
}
function _get_Property_Types()
{
	//FETCH ALL PROPERTY TYPES
	//CtrlCall(SEARCH_CORE.CONTROLLER_CALL_URL+"feed=function:_get_Property_Types;lang:js",'data=null',"_load_Search_Types");
	CtrlCall(SEARCH_CORE.CONTROLLER_CALL_URL+"feed:types;","","_load_Search_Types");
}
function _get_Property_PaymentTypes()
{
	//FETCH ALL PROPERTY PAYMENT TYPES
	CtrlCall(SEARCH_CORE.CONTROLLER_CALL_URL+"feed:payment_types;","data=null","_load_Payment_Types");
}
function _search_loading_layer(containerDiv,Element,show)
{
		eval("if(!SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader){SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader = document.createElement('div');}");
		eval("containerDiv.appendChild(SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader);");
		switch(show)
		{
			case true:
				Element.style.display = "none";
				eval("SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.style.backgroundColor                                     = '#f1f1f1';");
				eval("SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.style.top      		                                     = Element.style.top;");
				eval("SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.style.left      		                                     = Element.style.left;");
				eval("SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.style.width      		                                 = Element.style.width;");
				eval("SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.style.height      		                                 = Element.style.height;");
				eval("SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.style.zIndex   		 									 = '999';");
				eval("SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.innerHTML             								     = \"<table cellpadding='0' cellspacing=0' style='width:\"+SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.style.width+\";height:\"+SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.style.height+\";'><tr><td style='width:\"+SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.style.width+\";height:\"+SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.style.height+\"; vertical-align: middle' align='center' valign='middle'><img src ='"+MUFFIN_SHARED_URL+"/images/property_search/remaxloading.gif' align='middle'></td></tr></table>\";");
				eval("SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.style.display = 'block';");
			break;
			default:
				Element.style.display = "block";
				
				eval("SEARCH_CORE.HTML_Elements."+containerDiv.id+"_loader.style.display = 'none';");			
			break;
		}
		
}
function _load_Payment_Types(response)
{
	SEARCH_CORE.DATA.PaymentTypes 		  																= $H();
	SEARCH_CORE.TMP._type_reponseSet     																= $M(response);
	SEARCH_CORE.TMP._type_inc 																		    = 0;
	var tempHTML = "<table><tr>";
	switch(true)
	{
		case(SEARCH_CORE.TMP._type_reponseSet!=null):
			$H(SEARCH_CORE.TMP._type_reponseSet.PaymentTypes.PaymentType).each(function(type)
			{
				SEARCH_CORE.DATA.PaymentTypes[type.value.PaymentID] 	             						= $H();
				SEARCH_CORE.DATA.PaymentTypes[type.value.PaymentID].Name          							= type.value.MethodDescription;
				SEARCH_CORE.DATA.PaymentTypes[type.value.PaymentID].PropertyCount 							= 0;
				//alert(SEARCH_CORE.HTML_Elements.search_PaymentType_select_div);
				if(SEARCH_CORE.TMP._type_inc % 2 == 0)
				{
					tempHTML += "</tr><tr>";
				}
				tempHTML += "<tr align='center'><td>"+type.value.MethodDescription+" </td><td><div id='payment_amount_div_"+type.value.PaymentID+"'>[0]</div></td><td style='padding:0px;'><input type='radio' style='width:auto;' onClick=\"_search_PaymentType_Handler();\" name='payment_options_radio_group' value='"+type.value.PaymentID+"' id='payment_radiobutton_"+type.value.PaymentID+"' "+((type.value.MethodDescription == "Sale")? "":"")+" disabled /></td></tr>";
				// tempHTML += "asdojasoudhuo<input type='radio' value='testing' id='test' />";
				// SEARCH_CORE.HTML_Elements.search_PaymentType_select.options[SEARCH_CORE.TMP._type_inc]  = new Option(type.value.MethodDescription,type.value.PaymentID);
				SEARCH_CORE.TMP._type_inc++;
			});
		break;
		default:
			_error_message("No Payment Types found");
		break;
	}
	if(SEARCH_CORE.TMP._type_inc % 2 != 0)
	{
		//Create a placeholder if ends on uneven number
		tempHTML += "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>"
	}
	tempHTML += "</tr></table>";
	
	SEARCH_CORE.HTML_Elements.search_PaymentType_check_div.innerHTML = tempHTML;
	SEARCH_CORE.HTML_Elements.payment_options_radio_group			= $('remax_search_form').payment_options_radio_group;

	SEARCH_CORE.TMP = $H();
}
function _load_Search_Types(response)
{
	SEARCH_CORE.DATA.Types 		  																	    = $H();
	SEARCH_CORE.TMP._type_reponseSet 																	= $M(response);
	SEARCH_CORE.TMP.optioncount      																	= 0;
	switch(true)
	{
		case(SEARCH_CORE.TMP._type_reponseSet!=null):
			//SEARCH_CORE.TMP._type_reponseSet.Types.Types[0].Type.each(function(type)
			$H(SEARCH_CORE.TMP._type_reponseSet.Types.Type).each(function(type)
			{
				SEARCH_CORE.DATA.Types[type.value.PropTypeID] 	           									= $H();
				SEARCH_CORE.DATA.Types[type.value.PropTypeID].Name          								    = type.value.PropTypeName;
				SEARCH_CORE.DATA.Types[type.value.PropTypeID].PropertyCount 							        = 0;
			});
		break;
		default:
			_error_message("No Property Types found");
		break;
	}
	SEARCH_CORE.TMP 																					= $H();
	_populate_default_PropertyTypes_list();
}
function _populate_default_PropertyTypes_list()
{
	SEARCH_CORE.TMP.optioncount      																	    = 0;
	//ASSIGN OPTIONS
	SEARCH_CORE.DATA.Types.each(function(type)
	{
		SEARCH_CORE.HTML_Elements.search_Type_select.options[SEARCH_CORE.TMP.optioncount]                   = new Option(type.value.Name,type.key);
		SEARCH_CORE.HTML_Elements.search_Type_select.options[SEARCH_CORE.TMP.optioncount].disabled          = "true";
		SEARCH_CORE.HTML_Elements.search_Type_select.options[SEARCH_CORE.TMP.optioncount].style.color       = "#c1c1c1";
		SEARCH_CORE.TMP.optioncount++;
	});
	SEARCH_CORE.TMP 																						= $H();
}
function _load_Province_Properties(response)
{
	if (response.responseText[0]=='|') alert(response.responseText);
	

	SEARCH_CORE.TMP                        = $H();
	SEARCH_CORE.TMP.options_reponseSet = $M(response);
	switch(true)
	{
		case(SEARCH_CORE.TMP.options_reponseSet!=null && SEARCH_CORE.TMP.options_reponseSet.Suburbs!=null) && !(SEARCH_CORE.MEM_STATE.search_District):
			SEARCH_CORE.DATA.Locations 		        	 = $H();
			SEARCH_CORE.DATA.Locations.Suburbs     		 = $H();
			SEARCH_CORE.DATA.Locations.Regions     		 = $H();
			
			SEARCH_CORE.DATA.Locations.Regions           = SEARCH_CORE.TMP.options_reponseSet.Regions.Region;
			SEARCH_CORE.DATA.Locations.Suburbs           = SEARCH_CORE.TMP.options_reponseSet.Suburbs.Suburb;
			
			_populate_Region_Suburb_List();
			
		break;
		case (SEARCH_CORE.TMP.options_reponseSet!=null && SEARCH_CORE.TMP.options_reponseSet.Suburbs!=null) :
			SEARCH_CORE.DATA.Locations 		        	 = $H();
			SEARCH_CORE.DATA.Locations.Suburbs     		 = $H();
			SEARCH_CORE.DATA.Locations.Regions     		 = $H();
			
			SEARCH_CORE.DATA.Locations.Regions           = SEARCH_CORE.TMP.options_reponseSet.Regions.Region;
			SEARCH_CORE.DATA.Locations.Suburbs           = SEARCH_CORE.TMP.options_reponseSet.Suburbs.Suburb;

			_populate_Region_Suburb_List();
			//_reload_map();
			
		break;		
		
		default:
			SEARCH_CORE.HTML_Elements.search_Location_select.options.length = 0;
			SEARCH_CORE.HTML_Elements.search_Location_select.options[0]     = new Option('No Properties In Province','false');
			_populate_default_PropertyTypes_list();
			_search_loading_layer(SEARCH_CORE.HTML_Elements.search_Property_Type_select_div,SEARCH_CORE.HTML_Elements.search_Type_select,false);	
			_search_loading_layer(SEARCH_CORE.HTML_Elements.search_Region_Suburb_select_div,SEARCH_CORE.HTML_Elements.search_Location_select,false);
		break;
	}

}

function _reload_map()
 {
		var so = new SWFObject('/flash/remax_nz.swf', 'flashnzmap', '211', '194', '7', '#ffffff');
		so.addParam("wmode", "transparent");
		so.write('flashcontent');
		SEARCH_CORE.MEM_STATE.search_District = false;
}

function _reset_suburb() 
{
	SEARCH_CORE.MEM_STATE.search_District = false;
}

function _populate_Region_Suburb_List()
{
	SEARCH_CORE.TMP 																							  = $H();
	SEARCH_CORE.MEM_STATE.location_options                                                                        = $H();
	SEARCH_CORE.MEM_STATE.location_options.Suburbs                                                                = $H();

	SEARCH_CORE.HTML_Elements.search_Location_select.options.length 										      = 0;
	
	SEARCH_CORE.TMP.optioncount 																			      = 0;
	var allkeys = '';
	if (SEARCH_CORE.DATA.Locations.Suburbs)
	{
		$H(SEARCH_CORE.DATA.Locations.Suburbs).each(function(suburb)
		{
			if (SEARCH_CORE.MEM_STATE.search_Suburb_filter.Value=="" || 
			    suburb.value.Name.toUpperCase().indexOf(SEARCH_CORE.MEM_STATE.search_Suburb_filter.Value) == 0 ||
			    (SEARCH_CORE.MEM_STATE.search_Suburb_filter.Value,SEARCH_CORE.DATA.Locations.Regions[SEARCH_CORE.DATA.Locations.Suburbs[suburb.key].Region].Name && 
			    	suburb.value.Name.toUpperCase().indexOf(SEARCH_CORE.MEM_STATE.search_Suburb_filter.Value) == 0
			    ))
			{
				
					var oOptionList = SEARCH_CORE.HTML_Elements.search_Location_select.options;
					
					oOptionList[(SEARCH_CORE.TMP.optioncount +1)] = new Option(suburb.value.Name, suburb.key);
					allkeys = allkeys + suburb.key +',';
					if (SEARCH_CORE.RELOAD.STATE && in_array(","+suburb.key,SEARCH_CORE.RELOAD.DATA['Suburbs']))
					{
						oOptionList[SEARCH_CORE.TMP.optioncount].selected = "true";
					}
					SEARCH_CORE.TMP.optioncount++;
				
					SEARCH_CORE.MEM_STATE.location_options.Suburbs[suburb.key] = suburb.value;
			}
		});
		
		SEARCH_CORE.HTML_Elements.search_Location_select.options[0] = new Option("All Suburbs", allkeys.substring(0,(allkeys.length -1)));
		SEARCH_CORE.HTML_Elements.search_Location_select.options[0].style.backgroundColor  = SEARCH_CORE.STYLE.Selects.Headings_BG_Color;
		SEARCH_CORE.HTML_Elements.search_Location_select.options[0].style.color 		     = SEARCH_CORE.STYLE.Selects.Headings_Font_Color;
		
		if (SEARCH_CORE.TMP.optioncount == 0)
			{
					var oOptionList = SEARCH_CORE.HTML_Elements.search_Location_select.options;
								
					oOptionList[SEARCH_CORE.TMP.optioncount] = new Option("No suburbs found", -1);
			}		
		
		SEARCH_CORE.TMP 																							   = $H();
		SEARCH_CORE.HTML_Elements.search_Suburb_filter.disabled 													   = false;
		SEARCH_CORE.HTML_Elements.search_Location_select.disabled       											   = false;
		_search_loading_layer(SEARCH_CORE.HTML_Elements.search_Region_Suburb_select_div,SEARCH_CORE.HTML_Elements.search_Location_select,false);
		_populate_Type_List();
		
	}
	else
	{
		SEARCH_CORE.HTML_Elements.search_Location_select.disabled 														   = true;
		SEARCH_CORE.HTML_Elements.search_Location_select.options[SEARCH_CORE.TMP.optioncount] 
	}

}

function _populate_District_Suburb_List()
{
	SEARCH_CORE.TMP 																							  = $H();
	SEARCH_CORE.MEM_STATE.location_options                                                                        = $H();
	SEARCH_CORE.MEM_STATE.location_options.Suburbs                                                                = $H();
	
	SEARCH_CORE.HTML_Elements.search_Location_select.options.length 										      = 0;
	var allkeys = '';
	SEARCH_CORE.TMP.optioncount 																			      = 0;

	
	if (SEARCH_CORE.DATA.Locations.Suburbs)
	{
		$H(SEARCH_CORE.DATA.Locations.Suburbs).each(function(suburb)
		{
			 if (SEARCH_CORE.DATA.Locations.Suburbs[suburb.key].Region == 'region_'+SEARCH_CORE.DATA.SubRegionID) 
			{		
					if (suburb.value.Name.toUpperCase().indexOf(SEARCH_CORE.MEM_STATE.search_Suburb_filter.Value) == 0) 
					{		
						var oOptionList = SEARCH_CORE.HTML_Elements.search_Location_select.options;
						
						oOptionList[SEARCH_CORE.TMP.optioncount + 1] = new Option(suburb.value.Name, suburb.key);
						allkeys = allkeys + suburb.key +',';
						if (SEARCH_CORE.RELOAD.STATE && in_array(","+suburb.key,SEARCH_CORE.RELOAD.DATA['Suburbs']))
						{
							oOptionList[SEARCH_CORE.TMP.optioncount].selected = "true";
						}
						SEARCH_CORE.TMP.optioncount++;
					
						SEARCH_CORE.MEM_STATE.location_options.Suburbs[suburb.key] = suburb.value;
					}
			}
		});
		
		if (SEARCH_CORE.TMP.optioncount == 0)
			{
					var oOptionList = SEARCH_CORE.HTML_Elements.search_Location_select.options;
								
					oOptionList[SEARCH_CORE.TMP.optioncount] = new Option("No suburbs found", -1);
			}
		else
			{
					SEARCH_CORE.HTML_Elements.search_Location_select.options[0] = new Option("All Suburbs", allkeys.substring(0,(allkeys.length -1)));
					SEARCH_CORE.HTML_Elements.search_Location_select.options[0].style.backgroundColor  = SEARCH_CORE.STYLE.Selects.Headings_BG_Color;
					SEARCH_CORE.HTML_Elements.search_Location_select.options[0].style.color 		     = SEARCH_CORE.STYLE.Selects.Headings_Font_Color;
			}
		
		SEARCH_CORE.TMP 																							   = $H();
		SEARCH_CORE.HTML_Elements.search_Suburb_filter.disabled 													   = false;
		SEARCH_CORE.HTML_Elements.search_Location_select.disabled       											   = false;
		_search_loading_layer(SEARCH_CORE.HTML_Elements.search_Region_Suburb_select_div,SEARCH_CORE.HTML_Elements.search_Location_select,false);
		_populate_Type_List();

	}
	else
	{
		SEARCH_CORE.HTML_Elements.search_Location_select.disabled 														   = true;
		SEARCH_CORE.HTML_Elements.search_Location_select.options[SEARCH_CORE.TMP.optioncount] 
	}
			//	SEARCH_CORE.HTML_Elements.search_Suburb_filter.value = "Suburb Filter";
   	 SEARCH_CORE.MEM_STATE.search_District = "true";

}

function _populate_Type_List()
{
	_search_loading_layer(SEARCH_CORE.HTML_Elements.search_Property_Type_select_div,SEARCH_CORE.HTML_Elements.search_Type_select,true);
	
	//This checks if all suburbs option was selected
	if(SEARCH_CORE.HTML_Elements.search_Location_select.selectedIndex == 0)
	{
		SEARCH_CORE.TMP.Selected_Locations 	= SEARCH_CORE.HTML_Elements.search_Location_select.options[0].value.split(',');
	}
	else
	{
		SEARCH_CORE.TMP.Selected_Locations 	= get_Selected_Options(SEARCH_CORE.HTML_Elements.search_Location_select);
	}
	SEARCH_CORE.MEM_STATE.available_Types              															= $H();
	SEARCH_CORE.MEM_STATE.available_Types.regions_loaded                                                         = $A();
	SEARCH_CORE.MEM_STATE.available_Types.region_loaded_inc 													    = 0;

	SEARCH_CORE.TMP.post_regions																			= $H();
	SEARCH_CORE.TMP.post_regions.list																		= $A();
	SEARCH_CORE.TMP.post_regions.inc                                                                        = 0;
	
	SEARCH_CORE.MEM_STATE.PostSuburbs  = "-1";
	SEARCH_CORE.MEM_STATE.PostRegions  = "-1";
	switch(true)
	{
		case (SEARCH_CORE.TMP.Selected_Locations == -1):
			
			// do nothing	
			
		break;
		case($A(SEARCH_CORE.TMP.Selected_Locations).length>0):
		
			SEARCH_CORE.TMP.Selected_Locations.each(function(location)
			{
				SEARCH_CORE.TMP.location_key = 'Suburbs';

				//_console(location);
				$H(SEARCH_CORE.DATA.Locations[SEARCH_CORE.TMP.location_key][location].Types).each(function(type)
				{
				//	SEARCH_CORE.MEM_STATE.available_Types.regions_loaded[SEARCH_CORE.MEM_STATE.available_Types.region_loaded_inc] = location;
					SEARCH_CORE.MEM_STATE.PostRegions    															 += ","+location.substring(7,location.length);
				//	SEARCH_CORE.MEM_STATE.location_options.Suburbs.each(function(suburb)
				//	{
						var suburb = SEARCH_CORE.DATA.Locations.Suburbs[location];
						
						if(SEARCH_CORE.DATA.Locations.Suburbs[location].Types[type.key])
						{
							$H(SEARCH_CORE.DATA.Locations.Suburbs[location].Types[type.key].Payment).each(function(payment)
							{
									switch(!SEARCH_CORE.MEM_STATE.available_Types[type.key])
									{
										case true:
											SEARCH_CORE.MEM_STATE.available_Types[type.key]				               		= $H();
											SEARCH_CORE.MEM_STATE.available_Types[type.key].PropertyCount                   = 0;
											SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment               	        = $H();
										break;
									}
									if(!SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment[payment.key])
									{
										SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment[payment.key]                                  = $H();
										SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment[payment.key].PropertyCount           	      = 0;
										SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment[payment.key].Max           				      = 0;
									    SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment[payment.key].Min           				      = null;
									}
									SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment[payment.key].Max                                  = Math.max(parseInt(payment.value.Max),SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment[payment.key].Max); 
									SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment[payment.key].Min                                  = (parseInt(payment.value.Min)<SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment[payment.key].Min || SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment[payment.key].Min==null)?parseInt(payment.value.Min):SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment[payment.key].Min; 
									SEARCH_CORE.MEM_STATE.available_Types[type.key].Payment[payment.key].PropertyCount                       += payment.value.PropertyCount;
								
							});
							SEARCH_CORE.MEM_STATE.available_Types[type.key].PropertyCount 	   += SEARCH_CORE.DATA.Locations.Suburbs[location].Types[type.key].PropertyCount; 
						}
						SEARCH_CORE.MEM_STATE.PostSuburbs 	   += ","+SEARCH_CORE.DATA.Locations.Suburbs[location].SuburbID;
					//});
				});
			});
		break;

	}
	SEARCH_CORE.TMP.type_avail       = $H();
	SEARCH_CORE.TMP.type_grayd       = $H();
	SEARCH_CORE.TMP.type_avail_count = 0;
	SEARCH_CORE.TMP.type_grayd_count = 0;

	SEARCH_CORE.HTML_Elements.search_Type_select.options.length = 0;
	SEARCH_CORE.DATA.Types.each(function(type)
	{
		switch(!SEARCH_CORE.MEM_STATE.available_Types[type.key])
		{	
			case false:
				SEARCH_CORE.HTML_Elements.search_Type_select.options[SEARCH_CORE.TMP.type_avail_count] 			 = new Option(type.value.Name +" [ "+SEARCH_CORE.MEM_STATE.available_Types[type.key].PropertyCount+" ]",type.key);
				switch(SEARCH_CORE.RELOAD.STATE)
				{
					case(true):
						switch(in_array(","+type.key,SEARCH_CORE.RELOAD.DATA['Types']))
						{
							case true:
								SEARCH_CORE.HTML_Elements.search_Type_select.options[SEARCH_CORE.TMP.type_avail_count].selected 	 = true;
							break;
						}
					break;
					default:
						SEARCH_CORE.HTML_Elements.search_Type_select.options[SEARCH_CORE.TMP.type_avail_count].selected 	         = true;
					break;
				}
				SEARCH_CORE.TMP.type_avail_count++;
			break;
			default:
				SEARCH_CORE.TMP.type_grayd[SEARCH_CORE.TMP.type_grayd_count] 										 = new Option(type.value.Name +" [ 0 ]",type.key);
				SEARCH_CORE.TMP.type_grayd[SEARCH_CORE.TMP.type_grayd_count].style.color                             = "#d1d1d1";
				SEARCH_CORE.TMP.type_grayd[SEARCH_CORE.TMP.type_grayd_count].disabled 							     = true;
				SEARCH_CORE.TMP.type_grayd_count++; 
		}
	});
	SEARCH_CORE.TMP.type_grayd.each(function(option)
	{
		SEARCH_CORE.HTML_Elements.search_Type_select.options[SEARCH_CORE.TMP.type_avail_count] 					 = option.value;
		SEARCH_CORE.TMP.type_avail_count++
	});
	SEARCH_CORE.HTML_Elements.search_Type_select.disabled                                                        = false;
	_search_Payment_Handler();
	_search_loading_layer(SEARCH_CORE.HTML_Elements.search_Property_Type_select_div,SEARCH_CORE.HTML_Elements.search_Type_select,false);

}
function _search_Payment_Handler()
{
	for(i = 0;i<SEARCH_CORE.HTML_Elements.search_Type_select.options.length;i++)
	{
		switch(SEARCH_CORE.HTML_Elements.search_Type_select.options[i].disabled)
		{
			case true:
				SEARCH_CORE.HTML_Elements.search_Type_select.options[i].selected = false;
			break;
		}
	}
	SEARCH_CORE.TMP.available_Payment              														    = $H();
//		SEARCH_CORE.HTML_Elements.search_PaymentType_select.options.length = 0;
//		SEARCH_CORE.HTML_Elements.search_PaymentType_select.disabled       = true;
	var selectedTypes = get_Selected_Options(SEARCH_CORE.HTML_Elements.search_Type_select);
	if(selectedTypes.length >0)
	{
		$H(selectedTypes).each(function(type)
		{
			
			SEARCH_CORE.MEM_STATE.available_Types[type.value].Payment.each(function(payment)
			{
				if(!SEARCH_CORE.TMP.available_Payment[payment.key])
				{
					SEARCH_CORE.TMP.available_Payment[payment.key]               = $H();
					SEARCH_CORE.TMP.available_Payment[payment.key].PropertyCount = 0;
					SEARCH_CORE.TMP.available_Payment[payment.key].Max           = 1000000;
					SEARCH_CORE.TMP.available_Payment[payment.key].Min 			 = 0;
					SEARCH_CORE.TMP.available_Payment[payment.key].PropertyCount = 0;
				}
				SEARCH_CORE.TMP.available_Payment[payment.key].PropertyCount += payment.value.PropertyCount;
			});			
		});
	}
	
	//Make the previous amount for payment options 0
	var paymentdivCount = 0;
	//LOOP THROUGH DIVS WITH NUMBER AND CHECK IF IT IS A DIV
	//while()
	/*for( i = 0; i < SEARCH_CORE.HTML_Elements.payment_options_radio_group.length; i++ )
	{
	if( group[i].checked == true )
	val = group;
	}*/
	
	// Resetting values
	$('payment_amount_div_1').innerHTML = "[0]";
	$('payment_amount_div_2').innerHTML = "[0]";
	
	var inc = 0;
	var ischecked = true;
	SEARCH_CORE.TMP.available_Payment.each(function(payment)
	{
		$('payment_amount_div_'+payment.key).innerHTML 	= "["+payment.value.PropertyCount+"]";
		$('payment_radiobutton_'+ payment.key).disabled	= false;
		//SET SALES DEFAULT SELECTED
		if($('payment_radiobutton_'+ payment.key).checked)
		{
			ischecked = false;
		}
		switch(SEARCH_CORE.RELOAD.STATE)
		{
			case(true):
				switch(payment.key == SEARCH_CORE.RELOAD.DATA['Payment'])
				{
					case true:
						SEARCH_CORE.HTML_Elements.search_PaymentType_select.options[inc].selected           =true;
					break;
				}
			break;
		}
	});
	
	//SET DEFUALT
	
	if(SetpaymentID == 2)
	{
		$('payment_radiobutton_2').checked = true;
		$('payment_radiobutton_1').checked = false;
	}
	else
	{
		$('payment_radiobutton_2').checked = false;
		$('payment_radiobutton_1').checked = true;
	}
	//THIS IS THE CODE FOR WHEN USING A DROP DOWN FOR THE PAYMENT TYPE
	/*SEARCH_CORE.TMP.available_Payment.each(function(payment)
	{
		SEARCH_CORE.HTML_Elements.search_PaymentType_select.options[inc] =  new Option(SEARCH_CORE.DATA.PaymentTypes[payment.key].Name +" [ "+payment.value.PropertyCount+" ]",payment.key);	
		switch(SEARCH_CORE.RELOAD.STATE)
		{
			case(true):
				switch(payment.key == SEARCH_CORE.RELOAD.DATA['Payment'])
				{
					case true:
						SEARCH_CORE.HTML_Elements.search_PaymentType_select.options[inc].selected           =true;
					break;
				}
			break;
		}
		inc++;
	});*/
	//SEARCH_CORE.HTML_Elements.search_PaymentType_select.disabled         = false;
	
	SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select.disabled 				    					= true;
	SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select.disabled 										= true;

	SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select.disabled 				    					= true;
	SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select.disabled 										= true;  
	
	SEARCH_CORE.TMP.SaleRange 																				= $H();
	SEARCH_CORE.TMP.SaleRange.from_inc 																	    = 0;
	SEARCH_CORE.TMP.SaleRange.to_inc   																	    = 0;
	
	SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select.options.length 									= 0;
	SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select.options.length 									= 0;
	SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select.options[SEARCH_CORE.TMP.SaleRange.from_inc]  			= new Option("From Any",0);
	SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select.options[SEARCH_CORE.TMP.SaleRange.to_inc]    			= new Option("To Any",0);
	
	SEARCH_CORE.TMP.SaleRange.from_inc++;
	SEARCH_CORE.TMP.SaleRange.to_inc++;
	
	SEARCH_CORE.TMP.RentRange 																				    = $H();
	SEARCH_CORE.TMP.RentRange.from_inc 																	        = 0;
	SEARCH_CORE.TMP.RentRange.to_inc   																	        = 0;
	
	SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select.options.length 								    = 0;
	SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select.options.length 								    = 0;
	SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select.options[SEARCH_CORE.TMP.RentRange.from_inc]  		= new Option("From Any",0);
	SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select.options[SEARCH_CORE.TMP.RentRange.to_inc]    		= new Option("To Any",0);
	
	SEARCH_CORE.TMP.RentRange.from_inc++;
	SEARCH_CORE.TMP.RentRange.to_inc++;
	
	SEARCH_CORE.TMP.available_Payment.each(function(payment)
	{
		switch(payment.key)
		{
			case "1":
				SEARCH_CORE.CONST.Price_Ranges.Rental.each(function(range)
				{
					
					switch(true)
					{
						case (payment.value.Max>=range.value):
							SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select.options[SEARCH_CORE.TMP.RentRange.from_inc] = new Option(addCommas(range.value.toFixed(2)),range.value);
							if(SEARCH_CORE.RELOAD.DATA['Rental_MIN'])
							{
								switch(SEARCH_CORE.RELOAD.STATE)
								{
									case(true):
										switch(range.value == SEARCH_CORE.RELOAD.DATA['Rental_MIN'])
										{
											case true:
												SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select.options[SEARCH_CORE.TMP.RentRange.from_inc].selected           =true;
											break;
										}
									break;
								}
							}
							SEARCH_CORE.TMP.RentRange.from_inc++;										
						break;
					}
					switch(true)
					{
						case (payment.value.Min<=range.value):
							SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select.options[SEARCH_CORE.TMP.RentRange.to_inc] = new Option(addCommas(range.value.toFixed(2)),range.value);
							if(SEARCH_CORE.RELOAD.DATA['Rental_MAX'])
							{
								switch(SEARCH_CORE.RELOAD.STATE)
								{
									case(true):
										switch(range.value == SEARCH_CORE.RELOAD.DATA['Rental_MAX'])
										{
											case true:
												SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select.options[SEARCH_CORE.TMP.RentRange.to_inc].selected           =true;
											break;
										}
									break;
								}
							}
							SEARCH_CORE.TMP.RentRange.to_inc++;										
						break;
					}
				});
			break;
			case "2":
				SEARCH_CORE.CONST.Price_Ranges.Sale.each(function(range)
				{
					switch(true)
					{
						case (payment.value.Max>=range.value):
							SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select.options[SEARCH_CORE.TMP.SaleRange.from_inc] = new Option(addCommas(range.value.toFixed(2)),range.value);
							if(SEARCH_CORE.RELOAD.DATA['Sale_MIN'])
							{
								switch(SEARCH_CORE.RELOAD.STATE)
								{
									case(true):
										switch(range.value == SEARCH_CORE.RELOAD.DATA['Sale_MIN'])
										{
											case true:
												SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select.options[SEARCH_CORE.TMP.SaleRange.from_inc].selected           =true;
											break;
										}
									break;
								}
							}
							SEARCH_CORE.TMP.SaleRange.from_inc++;										
						break;
					}
					switch(true)
					{
						case (payment.value.Min<=range.value):
							SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select.options[SEARCH_CORE.TMP.SaleRange.to_inc] = new Option(addCommas(range.value.toFixed(2)),range.value);
							if(SEARCH_CORE.RELOAD.DATA['Sale_MAX'])
							{
								switch(SEARCH_CORE.RELOAD.STATE)
								{
									case(true):
										switch(range.value == SEARCH_CORE.RELOAD.DATA['Sale_MAX'])
										{
											case true:
												SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select.options[SEARCH_CORE.TMP.SaleRange.to_inc].selected           =true;
											break;
										}
									break;
								}
							}
							SEARCH_CORE.TMP.SaleRange.to_inc++;										
						break;
					}
				});
			break;
		}
	});
	switch(SEARCH_CORE.RELOAD.STATE)
	{
		case(true):
			SEARCH_CORE.HTML_Elements.search_Province_select.disabled   = false;
			SEARCH_CORE.RELOAD.STATE 							        = false;
		break;
	}
	
	// Custom options	
	_AddCustomOption(SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select);
	_AddCustomOption(SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select);
	_AddCustomOption(SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select);
	_AddCustomOption(SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select);

	_search_PaymentType_Handler();
}


function _AddCustomOption (oOptionElement)
{
	// Remove custom element if we already have one
	for (var i=0; i<oOptionElement.length; i++)
	{
		if (oOptionElement.options[i].value==='CUSTOM')
		{
			oOptionElement.options[i] = null;
		}
	}
	oOptionElement.options[oOptionElement.options.length] = new Option('Let me specify...', 'CUSTOM');
	_CustomPriceHandler(oOptionElement.id.replace(/_select/, ''));
	
}

function _CustomPriceHandler (szElement)
{
	if ($(szElement+'_select').value === 'CUSTOM')
	{
		$(szElement+'_Custom_Container').style.display = 'block';
	}
	else
	{
		$(szElement+'_Custom_Container').style.display = 'none';
		$(szElement+'_Custom').value = '';
	}
	
}

String.prototype.blank = function ()
{
	for (var i=0; i<this.length; i++)
	{
		if (this.charCodeAt(i)>32) return false;
	}
	return true;
}

function SanitizeNumeric (str)
{
	// Blank is quite common, so check this first
	if (str.blank()) return NaN;

	// Remove all but the last comma
	var idx = str.lastIndexOf(',');
		
	if (str.indexOf(',')===idx && (str.length-idx)<=3)
	{
		str = str.replace(/,/, '.');
	}
	str = str.replace(/,/g, ''); // Formatted commas
	str = str.replace(/[^0-9.]/, ''); // Non-numerics
	str = str.replace(/\s/, ''); // Whitespace
	
	if (str.blank()) return NaN;

	return Number(str);
}

function _search_Submit_Handler()
{	
	SEARCH_CORE.TMP.Post = $A();
	
	switch(true)
	{
		case(get_Selected_Options(SEARCH_CORE.HTML_Elements.search_Province_select).length==0 || SEARCH_CORE.HTML_Elements.search_Province_select.options[SEARCH_CORE.HTML_Elements.search_Province_select.selectedIndex].value=='-1'):
			_Ciriteria_error("Please Select a Property Province (Step 1)",SEARCH_CORE.HTML_Elements.search_Province_select_div.id);	
		return;
		default:
			
			SEARCH_CORE.TMP.Post['Province'] 					= SEARCH_CORE.HTML_Elements.search_Province_select.options[SEARCH_CORE.HTML_Elements.search_Province_select.selectedIndex].value;
	}
	SEARCH_CORE.TMP.Post['SuburbFilter'] = SEARCH_CORE.MEM_STATE.search_Suburb_filter.Value.replace(" ","_");
	switch(true)
	{
		case(get_Selected_Options(SEARCH_CORE.HTML_Elements.search_Location_select).length==0):
			_Ciriteria_error("Please Select a Property Location (Step 1)",SEARCH_CORE.HTML_Elements.search_Region_Suburb_select_div.id);	
		return;
		default:
			SEARCH_CORE.TMP.Post['Suburbs'] 					= SEARCH_CORE.MEM_STATE.PostSuburbs;
		//	SEARCH_CORE.TMP.Post['Regions']                     = SEARCH_CORE.MEM_STATE.PostRegions;
	}
	switch(true)
	{
		case(get_Selected_Options(SEARCH_CORE.HTML_Elements.search_Type_select).length==0):
			_Ciriteria_error("Please Select a Property Type (Step 2)");
		return;
		default:
			SEARCH_CORE.TMP.Post['Types'] = "-1";
			SEARCH_CORE.TMP.selectedTypes	= $H(get_Selected_Options(SEARCH_CORE.HTML_Elements.search_Type_select));
			SEARCH_CORE.TMP.selectedTypes.each(function(type)
			{
				SEARCH_CORE.TMP.Post['Types'] += ","+type.value;
			});
	}
	switch(true)
	{
		case (CurrentPaymentType == 0):
			_Ciriteria_error("Please Select a Payment Type (Step 2)");
		return;
		default:
			SEARCH_CORE.TMP.Post['Payment']   = CurrentPaymentType/*SEARCH_CORE.HTML_Elements.search_PaymentType_select.options[SEARCH_CORE.HTML_Elements.search_PaymentType_select.selectedIndex].value*/;
	}
	switch(CurrentPaymentType)
	{
		case 1:
			if ($('search_Rental_Min_Price_select').value==='CUSTOM')
			{
				var val = SanitizeNumeric($('search_Rental_Min_Price_Custom').value);
				
				if (isNaN(val) || val<0)
				{
					_Ciriteria_error("Please enter a valid minimum rental price<br />"+SEARCH_CORE.CONST.FormatHint,'search_Rental_Min_Price_Custom');	
					return false;
				}
				SEARCH_CORE.TMP.Post['Rental_MIN'] = val;
			}
			else SEARCH_CORE.TMP.Post['Rental_MIN']	= SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select.options[SEARCH_CORE.HTML_Elements.search_Rental_Min_Price_select.selectedIndex].value;
			
			if ($('search_Rental_Max_Price_select').value==='CUSTOM')
			{
				var val = SanitizeNumeric($('search_Rental_Max_Price_Custom').value);
				
				if (isNaN(val) || val<0)
				{
					_Ciriteria_error("Please enter a valid maximum rental price<br />"+SEARCH_CORE.CONST.FormatHint,'search_Rental_Max_Price_Custom');	
					return false;
				}
				SEARCH_CORE.TMP.Post['Rental_MAX'] = val;
			}
			else SEARCH_CORE.TMP.Post['Rental_MAX']	= SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select.options[SEARCH_CORE.HTML_Elements.search_Rental_Max_Price_select.selectedIndex].value;
		break;
		case 2:
			if ($('search_Sale_Min_Price_select').value==='CUSTOM')
			{
				var val = SanitizeNumeric($('search_Sale_Min_Price_Custom').value);
				
				if (isNaN(val) || val<0)
				{
					_Ciriteria_error("Please enter a valid minimum sale price<br />"+SEARCH_CORE.CONST.FormatHint,'search_Sale_Min_Price_Custom');	
					return false;
				}
				SEARCH_CORE.TMP.Post['Sale_MIN'] = val;
			}
			else SEARCH_CORE.TMP.Post['Sale_MIN']	= SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select.options[SEARCH_CORE.HTML_Elements.search_Sale_Min_Price_select.selectedIndex].value;
			
			if ($('search_Sale_Max_Price_select').value==='CUSTOM')
			{
				var val = SanitizeNumeric($('search_Sale_Max_Price_Custom').value);
				
				if (isNaN(val) || val<0)
				{
					_Ciriteria_error("Please enter a valid maximum sale price<br />"+SEARCH_CORE.CONST.FormatHint,'search_Sale_Max_Price_Custom');	
					return false;
				}
				SEARCH_CORE.TMP.Post['Sale_MAX'] = val;
			}
			else SEARCH_CORE.TMP.Post['Sale_MAX']	= SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select.options[SEARCH_CORE.HTML_Elements.search_Sale_Max_Price_select.selectedIndex].value;
			break;
	}
	
	//include surrounding suburbs in post data
	SEARCH_CORE.TMP.Post['incSub'] = '0';
	
	if(SEARCH_CORE.HTML_Elements.search_Surrounding_Suburbs_check.checked)
	{
		SEARCH_CORE.TMP.Post['incSub'] = '1';
	}
	
//	CtrlCall(SEARCH_CORE.CONTROLLER_CALL_URL+"&param=function:_search_stats_criteria_update;lang:js;","data="+serialize(SEARCH_CORE.TMP.Post),"_search_Submit");
	
	CtrlCall("/?page=property_search_response&content=main","data="+serialize(SEARCH_CORE.TMP.Post),"_search_Submit");
	
}
function _search_Submit(response)
{
	window.location = "/Property-One-Liners/Search/"+response.responseText+"/";
//	setTimeout("window.location = '?page=property_one_liners&data="+serialize(SEARCH_CORE.TMP.Post)+"';",10);
}
var muffin_box;
function _Ciriteria_error(msg,id)
{
	muffin_box = new MaJiG_BOX("<table class='section_grey' style='width:300px;font-size:7pt;'><tr><td align='center'><strong>Search Criteria Missing</strong></td></tr><tr><td align='center'>"+msg+"</td></tr><tr><td align='center'><input style='font-size: 8pt' type='button' value='Ok' onclick='muffin_box._close();Effect.Shake(\""+id+"\");'></td></tr></table>");
}

