// These functions are common to any page with the search box

function verifySearch()
{
	// Lock the form to prevent double-click saves
	if ( gForm_lock ) return;
	else gForm_lock = true;

	var errors = new ErrorList();
	var oForm = document.getElementById("search"); 

	clearInputErrors(oForm);

	var q_quick			= getInputValue(oForm["q_quick"]);
	var q_num_rooms		= getInputValue(oForm["q_num_rooms"]);
	var q_location		= getInputValue(oForm["q_location"]);
	var q_largest_room	= getInputValue(oForm["q_largest_room"]);
	var q_meeting_space	= getInputValue(oForm["q_meeting_space"]);
	var q_num_dbl		= getInputValue(oForm["q_num_dbl"]);
	var num_locations	= 0; // was numChecked("search_location");
	var num_amenities	= 0; // was numChecked("amenities");

	// Remove anything non-numeric from all number fields
	q_num_rooms		= q_num_rooms.replace(/[^0-9]/g, "");
	q_largest_room	= q_largest_room.replace(/[^0-9]/g, "");
	q_meeting_space	= q_meeting_space.replace(/[^0-9]/g, "");
	q_num_dbl		= q_num_dbl.replace(/[^0-9]/g, "");
	setInputValue(oForm["q_num_rooms"], q_num_rooms);
	setInputValue(oForm["q_largest_room"], q_largest_room);
	setInputValue(oForm["q_meeting_space"], q_meeting_space);
	setInputValue(oForm["q_num_dbl"], q_num_dbl);

	/* don't verify anything, allow empty searches
	if ( !q_quick && !q_num_rooms && !q_location && !q_largest_room && !q_meeting_space && !q_num_dbl
		 && !q_num_rooms && !q_largest_room && !q_meeting_space && !q_num_dbl && !num_locations && !num_amenities)
	{
		 	setError(null, errors, "You must use at least one search criteria");
	}
	*/

	if ( errors.getNumErrors() )
	{
		errors.showErrors();
		gForm_lock = false;
	}
	else
	{
		// Clear default values for search boxes
		if ( q_quick == 'Enter Location or Hotel Name' ) setInputValue(oForm["q_quick"], '');
		oForm.submit();
	}
}


function resetSearch()
{
	var oForm = document.getElementById("search");
	resetLocations();
	resetAmenities();
	oForm.reset();
}


function toggleRegion(id)
{
	var checkbox = document.getElementById(id);
	checkbox.checked = ( checkbox.checked ) ? false : true;
}


function numChecked(id)
{
	var count	= 0;
	var oLoc	= document.getElementById(id);
	var oInputs	= oLoc.getElementsByTagName("INPUT");

	for (i=0; i<oInputs.length; i++)
	{
		if ( oInputs[i].checked ) count++;
	}
	return count;
}


function resetAmenities()
{
	var oDiv = document.getElementById("amenities");
	var oInputs = oDiv.getElementsByTagName("INPUT");

	for (i=0; i<oInputs.length; i++)
	{
		document.getElementById("cbox_"+oInputs[i].id).className = "cbox_off";
		oInputs[i].checked = false;
	}
}


function changeOrder(new_val)
{
	if ( oForm = document.getElementById("search") )
	{
		setInputValue(oForm["q_orderBy"], new_val);
		verifySearch();
	}
	else if ( oForm = document.getElementById("search_refine") )
	{
		setInputValue(oForm["q_orderBy"], new_val);
		oForm.submit();
	}
}


// Used to toggle a checkbox's state and sync the image used to represent the checkbox.
// The checkbox of id "foo" must have a corresponding image of id "cbox_foo".  The image will
// have it's class toggled between "cbox" and "cbox_on"
function check(obj)
{
	// if name of checkbox was passed, find the checkbox
	if ( typeof(obj) == "string" )
	obj = document.getElementById(obj);

	if ( obj.type == "checkbox" )
	{
		image_name = "cbox_"+obj.id;
		var img = document.getElementById(image_name);
		if ( img )
		{
			if ( obj.checked )
			{
				obj.checked = false;
				img.className = "cbox_off";
			}
			else
			{
				obj.checked = true;
				img.className = "cbox_on";
			}
		}
	}
}


function expandSearch()
{
	setDisplay(document.getElementById("search_simple"), "none");
	setDisplay(document.getElementById("search_advanced"), "");
}

