// viewMore() is used to display the second page of search results containing all centers in a state ordered
// by distance away when the user searches by zip code
function viewMore()
{
	document.myform.submit();
}

// displayZip() is called to show the text box in which the user may enter a zip code and hide the state
// dropdown menu
function displayZip()
{
	var zipBox = document.getElementById('searchByZip');
	var stateBox = document.getElementById('searchByState');
	if (zipBox.style.display != 'block')
	{
		stateBox.style.display = 'none';
		zipBox.style.display = 'block';
		document.getElementById('zipform').zipcode.focus();
	}
}

// displayState() is called to display the state dropdown menu and hide the text box for the zip search
function displayState()
{
	var zipBox = document.getElementById('searchByZip');
	var stateBox = document.getElementById('searchByState');
	if (stateBox.style.display != 'block')
	{
		zipBox.style.display = 'none';
		stateBox.style.display = 'block';
		document.getElementById('stateform').state.focus();
	}
}

// enforceNumbersOnly() makes sure that only numbers may be entered into the text box for the zip
// search
function enforceNumbersOnly(evt, form)
{
	// Determine what the field value will be.
	var result = form.zipcode.value;
	var charCode = (evt && evt.charCode) ? evt.charCode : false;
	var keyCode = (evt) ? evt.keyCode : window.event.keyCode;
	if (charCode && result.length < 5) result += String.fromCharCode(charCode);
	
	// Allow numbers/Backspace/Delete/Tab, as well as shortcuts using modifier
	// keys, no matter what. But don't allow the Shift key, because no shortcuts
	// depend on that.
	if (keyIsAlwaysAllowed(keyCode) ||
		evt.altKey || evt.ctrlKey || evt.metaKey) return true;
	
	// Enter key should be able to submit form if current ZIP code is valid.
	if (keyCode == 13 && zipIsValid(result)) return true;
	
	if (!charCode) return false;
	return !isNaN(result) && parseInt(result) >= 0 && result.indexOf(".") < 0;
	
}

function keyIsAlwaysAllowed(keyCode)
{
	return keyCode == 8 || keyCode == 9 || keyCode == 46 ||
		(keyCode > 47 && keyCode < 58) ||
		(keyCode > 36 && keyCode < 41); // (keyCode > 95 && keyCode < 106)
}

/* function enforceNumbersOnly(evt, form)
{
	// Get the keycode from IE or Mozilla
	var keyCode = (evt) ? evt.keyCode : window.event.keyCode;
	
	// Enter key should be able to submit form if 5 digits are present
	if ((keyCode == 13) && (form.zipcode.value.length == 5))
	{
		document.zipform.submit();
		return true;
	}

	// Any number or a backspace/delete/tab
	return (keyCode > 47 && keyCode < 58) || keyCode == 8 ||
	(keyCode > 36 && keyCode < 41) || keyCode == 46 ||
	(keyCode > 95 && keyCode < 106 || keyCode == 9);
} */

function zipIsValid(zip)
{
	return !isNaN(zip) && parseInt(zip) >= 0 && zip.indexOf(".") < 0 &&
			zip.length == 5;
}

// goButton() controls whether or not the "Go" button for the zip search is greyed out. The
// user may only click go if five numbers are have been entered.
function goButton(form)
{
	var go = document.getElementById('zipSearch-go');
	if (form.id == 'zipform')
	{
		go.disabled = !zipIsValid(form.zipcode.value);
	}
	else
	{
		if (form.state.value != 'Select State')
		{
			document.stateform.submit();
		}
	}
}

// setZipFormStatus() makes sure that the zip search element appears properly on page refreshes
function setZipFormStatus()
{
	var go = document.getElementById('zipSearch-go');
	var zip = document.getElementById('zipform').zipcode.value;
	go.disabled = !zipIsValid(zip);
	if (document.getElementById('searchType-state').checked)
	{
		displayState();
	}
}

function check()
{
	setZipFormStatus();
}
