//######################################################################################################################
// HANDLES THE 5 SEARCH FORMS.
//######################################################################################################################
function searchForm() {
	// declaring class instances.
	var mySlide = new Fx.Slide('advanced',{transition:Fx.Transitions.Cubic.easeOut}).hide();
		
	// handles the load event so the checkbox is checked/unchecked when the form is loaded.  This
	// is needed when form is submitted but validation fails.
	if ($('chkAdvancedOptions')) {
		if ($('chkAdvancedOptions').checked == true) {
			mySlide.slideIn();
		} else {
			mySlide.hide();		
		}	
	}
	
	// handles the region click events. either hides or shows the sub region div.
	var region = $$('input[id^=fldRegionID]'); 
	// holds all of the input elements that start with fldRegionID
	region.each(function(element) {
		// setting default checked property to true
		$(element).setProperty('checked', true);
		// adding click event to each checkbox.
		$(element).addEvent('click', function(){
			if ($(element).getProperty('checked') == true) {
				// showing subregion div and checking all sub regions.
				if ($('subregion' + $(element).getProperty('value')) != null) {
					$('subregion' + $(element).getProperty('value')).setStyle('display', '');
					subRegionSelect(true);
				}
			} else {
				// unchecking any checkboxes in subregion div and hiding subregion div.
				if ($('subregion' + $(element).getProperty('value')) != null) {
					subRegionSelect(false);
					$('subregion' + $(element).getProperty('value')).setStyle('display', 'none');
				}
			}
		});
	});
	
	// checking all sub regions.
	subRegionSelect(true);
	// checking all property types.
	propTypeSelect(true);
	
	// handles the chkRegionSelectAll click event. will either check or uncheck all regions.
	if ($('chkRegionSelectAll')) {
		$('chkRegionSelectAll').addEvent('click', function(e) {	
			if ($('chkRegionSelectAll').getProperty('checked') == true) { 
				regionSelect(true);
				subRegionSelect(true);
				var subregion = $$('div[id^=subregion]'); 
				subregion.each(function(element) {
					$(element).setStyle('display', '');
				});
			} else { 
				// all regions have been unchecked so we need to uncheck an subregions and hide.
				regionSelect(false); 
				subRegionSelect(false);
				var subregion = $$('div[id^=subregion]'); 
				subregion.each(function(element) {
					$(element).setStyle('display', 'none');
				});
			}
		});
	}
	
	// handles the chkSubRegionSelectAll click event. will either check or uncheck all sub regions.
	if ($('chkSubRegionSelectAll')) {
		$('chkSubRegionSelectAll').addEvent('click', function(e) {	
			if ($('chkSubRegionSelectAll').getProperty('checked') == true) { subRegionSelect(true); } else { subRegionSelect(false); }
		});
	}
	
	// function to uncheck chkSubRegionSelectAll if any sub regions are unchecked.
	var subregion = $$('input[id^=fldSubRegionID]'); 
	subregion.each(function(element) {
		$(element).addEvent('click', function(){
			if ($(element).getProperty('checked') == false) {
				$('chkSubRegionSelectAll').setProperty('checked', false)
			}
		});
	});
	
	// handles the chkPropTypeSelectAll click event. will either check or uncheck all property types.
	if ($('chkPropTypeSelectAll')) {
		$('chkPropTypeSelectAll').addEvent('click', function(e) {	
			if ($('chkPropTypeSelectAll').getProperty('checked') == true) { propTypeSelect(true); } else { propTypeSelect(false); }
		});
	}
	
	// function to uncheck chkPropTypeSelectAll if any prop types are unchecked.
	var propertytype = $$('input[id^=fldPropertyTypeID]'); 
	propertytype.each(function(element) {
		$(element).addEvent('click', function(){
			if ($(element).getProperty('checked') == false) {
				$('chkPropTypeSelectAll').setProperty('checked', false)
			}
		});
	});
	
	// handles the chkStatusSelectAll click event. will either check or uncheck all statuses.
	if ($('chkStatusSelectAll')) {
		$('chkStatusSelectAll').addEvent('click', function(e) {	
			if ($('chkStatusSelectAll').getProperty('checked') == true) { statusSelect(true); } else { statusSelect(false); }
		});
	}
	
	// function to uncheck chkStatusSelectAll if any statuses are unchecked.
	var status = $$('input[id^=fldStatus]'); 
	status.each(function(element) {
		$(element).addEvent('click', function(){
			if ($(element).getProperty('checked') == false) {
				$('chkStatusSelectAll').setProperty('checked', false)
			}
		});
	});
	
	// checking the "ACTIVE" status.
	var status = $$('input[id^=fldStatus]'); 
	// holds all of the input elements that start with fldStatus
	status.each(function(element) {
		if ($(element).getProperty('value') == "1") {
			$(element).setProperty('checked', true);						 
		}
	});
	
	// checks or unchecks all of the regions based on the passed in variable.
	function regionSelect(selected) {
		// holds all of the input elements that start with fldPropertyTypeID
		var region = $$('input[id^=fldRegionID]'); 
		region.each(function(element) {
			$(element).setProperty('checked', selected);						 
		});
	}
	
	// checks or unchecks all of the sub regions based on the passed in variable.
	function subRegionSelect(selected) {
		// holds all of the input elements that start with fldPropertyTypeID
		var subregion = $$('input[id^=fldSubRegionID]'); 
		subregion.each(function(element) {
			$(element).setProperty('checked', selected);						 
		});
		$('chkSubRegionSelectAll').setProperty('checked', selected)
	}
	
	// checks or unchecks all of the property types based on the passed in variable.
	function propTypeSelect(selected) {
		// holds all of the input elements that start with fldPropertyTypeID
		var propertytype = $$('input[id^=fldPropertyTypeID]'); 
		propertytype.each(function(element) {
			$(element).setProperty('checked', selected);
		});
	}
	
	// checks or unchecks all of the statuses based on the passed in variable.
	function statusSelect(selected) {
		// holds all of the input elements that start with fldStatus
		var status = $$('input[id^=fldStatus]'); 
		status.each(function(element) {
			$(element).setProperty('checked', selected);
		});
	}
	
	// hiding the login div
	$('modal-login').setStyle('visibility', 'hidden');
	
	// adds the click event to the control passed in.
	if ($('chkAdvancedOptions')) {
		$('chkAdvancedOptions').addEvent('click', function(e){	
			var newAjax = new Request({
					url: '/modal/login.asp?action=check',
					method: 'get',
					onComplete: getLoginResult
			});
			newAjax.send();
						
			function getLoginResult(data) {
				if ($('chkAdvancedOptions').checked == true) {
					if (data=='invalid') {			
						$('chkAdvancedOptions').setProperty('checked', false);
						var loginfade = new Fx.Tween('modal-login', {duration:1000}).start('opacity', 0,1);
					} else {
						mySlide.slideIn();
					}	
				} else {
					mySlide.slideOut();
				}
			}
		});
	}
											
	// handles the form submit event. executes the page. if no error is returned, redirect to results. if error is returned, then we
	// update the alert div with these errors.
	$('frmSearch').addEvent('submit', function(e) {	   
		new Event(e).stop();									 
		var log = $('alert').empty();
		
		this.set('send', {
			onComplete: function(response) {
				if (response == '1' || response == '2' || response == '3' || response == '4' || response == '5') {
					window.location = "results.asp?page=1&results=20&layout=tabs&proptype=" + response;
				} else {
					log.set('html', response);
					var scroll = new Fx.Scroll(window);
					scroll.toElement('alert');
				}
			}
		}).send();
	});
	
	// handles the login form post event.  if no data is returned, valid user so check advanced options and slide them in.
	// else set the error.
	$('frmLoginModal').addEvent('submit', function(e) {
		new Event(e).stop();
		this.set('send', {
			onComplete: function Login(data) {
				if (data != '') {
					$('modal-login-message').innerHTML = '<h3 class="alert">' + data + '</h3>';
				}
				else {
					$('modal-login').setStyle('visibility', 'hidden');
					$('chkAdvancedOptions').setProperty('checked', true);
					mySlide.slideIn();
					
					var newAjax = new Request({
						url: '/includes/header.asp?loginmodal=true',
						method: 'get',
						update: $('Sidebar'),
						onComplete: function() {
							if ($('submitNav')) {
								var navSlide = new Fx.Slide('submitNav').hide();
							}
							
							if ($('submitClick')) {
								$('submitClick').addEvent('click', function(e){
									e = new Event(e);
									navSlide.toggle();
									e.stop();
								});
							}
						}
					});
					newAjax.send();
				}			
			}		
		}).send();
			

	});
	
	// closes the login modal.
	$('modal-login-close').addEvent('click', function(e) {
		var loginfade = new Fx.Tween('modal-login', {duration:1000}).start('opacity', 1,0);
	});
}