/*
	These functions use the prototype javascript library.
	You must make sure that the libray is included in the page
*/

onload = load_form_options;


function save_form_options()
{
	if($('frmOptions'))
	{
		// Turn the form options into a JSON string
		var strOpts = $('frmOptions').serialize(true);
		
		// URL for request
		var strURL = 'black/ajax/ajax_WebsiteFormValues.asp';
		
		// Object to hold request values
		var params = {
						FormVals: Object.toJSON(strOpts),
						Mode: 1
					};
		
		// Save the values
		new Ajax.Request(strURL, {method:'get', parameters: params});
	}
}


function load_form_options()
{
	if($('frmOptions'))
	{
		// URL for request
		var strURL = 'black/ajax/ajax_WebsiteFormValues.asp';
		
		// Object to hold request values
		var params = {
						FormVals: '',
						Mode: 2
					};
		
		// Save the values
		new Ajax.Request(strURL, {method:'get', parameters: params, onComplete: set_form_options});
	}
}


function set_form_options(transport)
{
	var strJSON = transport.responseText;
	
	if(strJSON.length > 0)
	{
		var formOpts = strJSON.evalJSON();
		
		if($F('hidHome') == 1)
		{
			for(var props in formOpts)
			{
				if($(props))
				{
					if(props.indexOf('btn') == -1 && props.indexOf('localities') == -1)
					{
						if(props == 'hidJob')
						{					
							// URL for request
							var strURL = 'black/ajax/ajax_WebsiteFormValues.asp';
									
							// Object to hold request values
							var params = {
											FormVals: '',
											Mode: 5,
											Min: formOpts['Current_Asking_PriceMin'],
											Max: formOpts['Current_Asking_PriceMax']
										};
							
							new Ajax.Request
							(
								strURL, 
								{
									method:'get', 
									parameters: params, 
									onComplete: function(transport)
									{
										if(parseInt(transport.responseText) > 0)
										{
											price_range_radios(transport.responseText, 'trPriceDateRange', 1);
											set_price(params.Min, params.Max)
										
										}
									}
								}
							);
							
							if(parseInt(formOpts[props]) == 1)
							{
								$('radSale').checked = true;
							}
							else if(parseInt(formOpts[props]) == 2)
							{
								$('radRent').checked = true;	
							}
										
						}
						else
						{	
							if(props != 'Current_Asking_PriceMin' && props != 'Current_Asking_PriceMax')
							{
								// Found the element - set the value
								$(props).value = formOpts[props];
							}							
						}
					}
				}
			}
		}
		else
		{
		
			if($('job_type_code') == null || $F('job_type_code') == formOpts['job_type_code'] || $F('job_type_code') == null)
			{
				for(var props in formOpts)
				{
					if($(props))
					{
						if(props.indexOf('btn') == -1 && props.indexOf('localities') == -1)
						{
							// Found the element - set the value
							$(props).value = formOpts[props];
						}
					}
				}
			}
		}
	}
}


function set_price(min, max)
{
	var strMin = '';
	var strMax = '';

	if(min.indexOf('--') == -1)
	{
		strMin = '$(\'Current_Asking_PriceMin\').value = '+ min;
		setTimeout(strMin, 150);
	}
	if(max.indexOf('--') == -1)
	{
		strMax = '$(\'Current_Asking_PriceMax\').value = '+ max;
		setTimeout(strMax, 150);
	}	
}

function metric_imperial(intMetImp, intLandFloor, intWidth)
{
	// URL for request
	var strURL = '';
	
	// Object to hold request values
	var params = {
					MetOrImp: 0,
					Width: intWidth
				};
				
	if(intLandFloor == 1)
	{
		// Floor area
		strURL = 'black/ajax/ajax_FloorAreaDropdown.asp';
		
		params.MetOrImp = intMetImp;
	
		// Change price dropdowns
		// Select is in a span because IE crashes when changing select content. Typical!
		new Ajax.Updater('spnFloorArea', strURL, {method:'get', parameters: params});
	}
	else if(intLandFloor == 2)
	{
		// Land area
		strURL = 'black/ajax/ajax_LandAreaDropdown.asp';
		
		params.MetOrImp = intMetImp;
		
		// Change price dropdowns
		// Select is in a span because IE crashes when changing select content. Typical!
		new Ajax.Updater('spnLandArea', strURL, {method:'get', parameters: params});
	}
}

function price_range_radios(intRadio, strID, intTitleAlign)
{
	if(intRadio > 0)
	{
		$('hidJob').value = intRadio;

		// URL for request
		var strURL = 'black/ajax/ajax_PriceDropdown.asp';
		
		// Object to hold request values
		var params = {
						Mode: 1, 
						PriceMode: 0,
						TitleAlign: 0
					};
					
		if(intRadio == 1)
		{
			// Buy
			$(strID).style.visibility = 'hidden';
			
			// Set prices to sales
			params.PriceMode = 1;
			params.TitleAlign = intTitleAlign;
		}
		else if(intRadio == 2)
		{
			// Rent
			$(strID).style.visibility = 'visible';
			
			// Set prices to lettings
			params.PriceMode = 2;
			params.TitleAlign = intTitleAlign;
		}
		
		// Change price dropdowns
		// Select is in a span because IE crashes when changing select content. Typical!
		new Ajax.Updater('spnPrice', strURL, {method:'get', parameters: params});
		
		//Save the selection
		save_price_range_radio_selection(intRadio);
	}
}

function save_price_range_radio_selection(intPriceMode)
{
	// URL for request
	var strURL = 'black/ajax/ajax_WebsiteFormValues.asp';
	
	// Object to hold request values
	var params = {
					FormVals: '',
					Mode: 4,
					PriceMode: intPriceMode
				};
	
	// Save the values
	new Ajax.Request(strURL, {method:'get', parameters: params});
}

function check_all(intMainID)
{
	var sID = '';
	var checkboxes = Form.getInputs('thisForm', 'checkbox');
	
	var chkMain = $('chkMain'+ intMainID);
	
	for(var intIndex = 0, len = checkboxes.length; intIndex < len; intIndex++)
	{
		sID = checkboxes[intIndex].id.substring(checkboxes[intIndex].id.indexOf('_') + 1, checkboxes[intIndex].id.length);
		
		if(sID == intMainID)
		{
			if(chkMain.checked == true)
			{
				checkboxes[intIndex].checked = true;
			}
			else
			{
				checkboxes[intIndex].checked = false;
			}	
		}
	}
}


function checkboxes_done()
{
	var strIDs = '';
	var checkboxes = Form.getInputs('thisForm', 'checkbox');
	
	for(var intIndex = 0, len = checkboxes.length; intIndex < len; intIndex++)
	{
		if(checkboxes[intIndex].id.indexOf('chkMain') == -1)
		{
			if(checkboxes[intIndex].checked == true)
			{
				strIDs += checkboxes[intIndex].value +',';
			}
		}
	}
	$('localities').value = strIDs.substring(0, strIDs.length - 1);
	$('thisForm').submit();
}


function selLocalities_change()
{
	$('localities').value = $F('selLocalities');
}

function search_options()
{
	var blnSubmit = true;
	var blnChecked = false;
	
	if($F('localities') == 0)
	{
		alert('Please choose a location')
		blnSubmit = false;
	}
	
	
	if(blnSubmit == true)
	{
		$('frmOptions').submit();
	}
}


function search()
{
	var blnSubmit = true;
	var blnChecked = false;
	
	if($F('localities') == 0)
	{
		alert('Please choose a location')
		$('localities').focus()
		blnSubmit = false;
	}
	
	var oBS = document.frmOptions.job_type_code
	
	for(var intIndex = 0, len = oBS.length; intIndex < len; intIndex++)
	{
		if(oBS[intIndex].checked == true)
		{
			blnChecked = true;
		}
	}
	
	if(blnChecked == false)
	{
		alert('Please select either Buy or Rent')
	}
	
	if(blnSubmit == true && blnChecked == true)
	{
		save_form_options();
		$('frmOptions').submit();
	}
}

function reset_saved_form_values()
{
	// URL for request
	var strURL = 'black/ajax/ajax_WebsiteFormValues.asp';
	
	// Object to hold request values
	var params = {
					FormVals: '',
					Mode: 3
				};
	
	// Reset session variable holding form values
	new Ajax.Request(strURL, {method:'get', parameters: params});
}


function reset_options(intMenuID)
{	
	reset_saved_form_values();
	
	if(location.href.indexOf('?') > 0)
	{
		location.href = location.href +'&Reset=1';
	}
	else
	{
		location.href = location.href +'?Reset=1';
	}
}