//	This function teggles the visibility of given web page elements, based on the value of a given form field
//	It is meant to be called when a form field in question is changed
//	This function takes in three arguments:
//	* An array of names of elements (usually DIV elements) to be made visible/invisible
//	* The form field that has been changed
//	* The sets of values for which each element should be visible
function toggleDivVisibility(divIds, formField, visibleValues) {
//	Check the following conditions:
//	* 'divIds' argument is an Array
//	* 'formField' argument is either a select box or a radio button
//	* 'visibleValues' argument is an Array
//	* 'divIds' argument has a non-zero length
//	* 'divIds' argument has the same length as 'visibleValues' argument
	if ((divIds instanceof Array) && ((formField.tagName.toUpperCase() == 'SELECT') || ((formField.tagName.toUpperCase() == 'INPUT') && (formField.type.toUpperCase() == 'RADIO'))) && (visibleValues instanceof Array) && (divIds.length >= 0) && (divIds.length == visibleValues.length)) {

//		Loop through the 'divId' Array
		for (var i=0; i<divIds.length; i++) {

//			Make sure that the given Array value of 'visibleValues' is itself also an Array
			if (visibleValues[i] instanceof Array) {

//				Gete the page element to be checked for visibility/invisibility
				var divToToggle = document.getElementById(divIds[i]);

//				Make sure that the desired page element exists
				if (divToToggle) {

//					Initialize relevant local variables
					var makeVisible = false;
					var continueLooping = true;
					var j = 0;

//					If the corresponding Array of visibility values is empty, do not even try to loop
					if (visibleValues[i].length == 0) {
						continueLooping = false;
					}

//					Loop through the Array of visiblity values until either getting to the end of the Array or finding a value corresponding to the incoming form field's value
					while (continueLooping) {

//						Initialize the comparison value variable
						var compareValue = '';

						if (formField.tagName.toUpperCase() == 'SELECT') {
//							Incoming form field is a select box, set compareValue to the selected value
							compareValue = formField.options[formField.selectedIndex].value;
						}
						else {
//							Incoming form field is a radio button, set compareValue to the button's value
							compareValue = formField.value;
						}

						if (compareValue == visibleValues[i][j]) {
//							compareValue matches a value in the visibility values Array, set visibility flag to true and stop looping
							makeVisible = true;
							continueLooping = false;
						}
						else {
//							compareValue does not match the given value from the visibility values Array, move to next value in Array
							j++;

							if (j >= visibleValues[i].length) {
//								Loop has iterated through the entire visiblity values array, stop looping
								continueLooping = false;
							}
						}
					}

					if (makeVisible) {
//						Make (or keep) the element visible, set element's 'display' style
						divToToggle.style.display = 'block';
					}
					else {
//						Make (or keep) the element invisible, set element's 'display' style
						divToToggle.style.display = 'none';
					}
				}
			}
		}
	}
}


function showFieldGroup (classID) {
	
	
				switch(classID)
				{
				case '1': // Residential
					$('#homeGroup').show();
					$('#residentialFields').show();
					$('#homeNeighborhoods').show();
					
					$('#condoNeighborhoods').hide();
					$('#condoGroup').hide();
					$('#commercialGroup').hide();
					$('#landGroup').hide();
					$('#multiFamilyGroup').hide();
					
					resetAllFormValues();
				  	break;
				case '2': // Land
					$('#landGroup').show();
					
					$('#condoNeighborhoods').hide();
					$('#homeNeighborhoods').hide();
					$('#homeGroup').hide();
					$('#condoGroup').hide();
					$('#residentialFields').hide();
					$('#communityFeatures').hide();
					$('#commercialGroup').hide();
					$('#multiFamilyGroup').hide();
					
					resetAllFormValues();
				  	break;
				case '4': // Commercial
					$('#commercialGroup').show();
					
					$('#condoNeighborhoods').hide();
					$('#homeNeighborhoods').hide();
					$('#homeGroup').hide();
					$('#condoGroup').hide();
					$('#residentialFields').hide();
					$('#communityFeatures').hide();
					$('#landGroup').hide();
					$('#multiFamilyGroup').hide();
					
					resetAllFormValues();
					break;
				case '5': // MultiFamily
					$('#multiFamilyGroup').show();
					$('#residentialFields').show();
					$('#homeNeighborhoods').show();
					
					$('#condoNeighborhoods').hide();
					$('#homeGroup').hide();
					$('#condoGroup').hide();
					$('#landGroup').hide();
					$('#commercialGroup').hide();
					$('#communityFeatures').hide();
					
					
					resetAllFormValues();
					break;
				case '6': // Condo
					$('#condoGroup').show();
					$('#residentialFields').show();
					$('#condoNeighborhoods').show();
					
					$('#homeNeighborhoods').hide();
					$('#homeGroup').hide();
					$('#commercialGroup').hide();
					$('#landGroup').hide();
					$('#multiFamilyGroup').hide();
					
					
					resetAllFormValues();
					break;
				default:
					$('#homeGroup').show();
					$('#residentialFields').show();
					$('#homeNeighborhoods').show();
					
					$('#condoGroup').hide();
					$('#condoNeighborhoods').hide();
					$('#commercialGroup').hide();
					$('#landGroup').hide();
					$('#multiFamilyGroup').hide();
					
					
					resetAllFormValues();
				}
								
			}
	
//MLS_SearchLongForm
//neighborhoods

function resetAllFormValues () {
	$('#landTypes').val('');
	$('#commercialTypes').val('');
	$('#presentUse').val('');
	$('#neighborhoodsHome').val('');
	$('#squarefeetHome').val('');
	$('#neighborhoodsCondo').val('');
	$('#squarefeetCondo').val('');
	$('#locations').val('');
	
	$("form#MLS_SearchLongForm INPUT[@name=neighborhoods][type='checkbox']").attr('checked', false);
}

function unCheckNeighborhoods () {
	$("form#MLS_SearchLongForm INPUT[@name=neighborhoods][type='checkbox']").attr('checked', false);	
}
/*
function jqUnCheckAll( id, name, flag )
{
   if (flag == 1)
   {
      $("form#" + id + " INPUT[@name=" + name + "][type='checkbox']").attr('checked', false);
	  $("form#MLS_SearchLongForm INPUT[@name=neighborhoods][type='checkbox']").attr('checked', false);
   }
}		
*/

$(document).ready(function() {
			
			
			showFieldGroup($('#classIDselect').val());
			
			$('#classIDselect').change(function() {
				var currentValue = $('#classIDselect').val();
				showFieldGroup(currentValue);
				//alert(currentValue);
			});
			
			
			
});











