// 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	= numChecked("search_location");
	var num_amenities	= 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);

	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 toggleLoc()
{
	var oDiv = document.getElementById("search_location");
//	var parentPos = findPosition(oDiv.parentNode);
//	oDiv.style.left = (parentPos.x + 256) +"px";

	if ( oDiv.timeout_w !== undefined ) clearTimeout(oDiv.timeout_w);

	if ( oDiv.state_w === undefined )
	{
		oDiv.state_w = "out"
		oDiv.dest_w = 683;
		oDiv.style.width = "1px"
	}
	else if ( oDiv.state_w == "out" )
	{
		oDiv.state_w = "in";
		oDiv.dest_w = 1;
	}
	else if ( oDiv.state_w == "in" )
	{
		oDiv.state_w = "out";
		oDiv.dest_w = 683;
	}
	setDisplay(oDiv,"");
	oDiv.timeout_w = setTimeout("slideSearchLoc()",10); 
}

function slideSearchLoc()
{
	var oDiv	= document.getElementById("search_location");
	var state_w	= oDiv.state_w;
	var cur_w	= parseInt(oDiv.style.width,10);
	var dest_w	= oDiv.dest_w;

	if ( state_w == "out" )
	{
		var delta_w = Math.abs(Math.ceil((dest_w-cur_w)/2));		// How far to move?  Half of width left over
		if ( delta_w > 100 ) delta_w = 100;							// Limit max distance to 100
		var new_w = cur_w + delta_w;
		if ( Math.abs(new_w-dest_w) <= 1) new_w = dest_w;
		oDiv.style.width = new_w+"px";
		if ( new_w != dest_w ) oDiv.timeout_w = setTimeout("slideSearchLoc()",20);
		else
		{
			document.getElementById("search_button_1").style.display = "none";
			document.getElementById("search_button_2").style.display = "none";
		}
	}
	else if ( state_w == "in" )
	{
		var delta_w = Math.abs(Math.ceil((dest_w-cur_w)/2));		// How far to move?  Half of width left over
		if ( delta_w > 100 ) delta_w = 100;							// Limit max distance to 100
		var new_w = cur_w - delta_w;
		if ( Math.abs(new_w-dest_w) <= 1) new_w = dest_w;
		oDiv.style.width = new_w+"px";
		if ( new_w != dest_w ) oDiv.timeout_w = setTimeout("slideSearchLoc()",20);
		else
		{
			document.getElementById("search_button_1").style.display = "block";
			document.getElementById("search_button_2").style.display = "block";
			setDisplay(oDiv,"none");
		}
	}
}


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 resetLocations()
{
	var oLoc = document.getElementById("search_location");
	var oInputs = oLoc.getElementsByTagName("INPUT");

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

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"), "");
}

