function checkZIP(field_id, form_id)
{
	zipfield = document.getElementById(field_id);
	re = /^\d{5}([\-]\d{4})?$/;
  	
  	if (re.test(zipfield.value)) {
  		submit_form(form_id);
  	} else {
  		alert('Please enter a five or nine-digit zip code.');
  		return false;
  	}
}

function checkZIPandLIC(zip_field_id, lic_field_id, form_id)
{
	zipfield = document.getElementById(zip_field_id);
	licfield = document.getElementById(lic_field_id);
	re = /^\d{5}([\-]\d{4})?$/;
  	
  	errors = false;
  	errorMsg = "Please fix the following errors:\n";
  	
  	if (!re.test(zipfield.value)) {
  		errorMsg += "Please enter a five or nine-digit zip code.\n";
  		errors = true;
  	}
  	
  	if (licfield.value == '') {
  		errorMsg += "Please select a LIC level";
  		errors = true;
  	}
  	
  	if (errors) {
  		alert(errorMsg);
  	} else {
  		submit_form(form_id);
  	}
}

function checkZIPandPopup(field_id)
{
	zipfield = document.getElementById(field_id);
	
	userWidth = screen.width;
	userHeight = screen.height;
	
	windowHeight = 0;
	windowWidth = 0;
	
	if (userHeight <= 600 || userWidth <= 800) {
		windowHeight = 600;
		windowWidth = 800;
	} else if (userHeight >= 768 || userWidth >= 1024) {
		windowHeight = 600;
		windowWidth = 1000;
	}
		

	window.open(root + '/Lookup/submitZip?zip=' + zipfield.value,
	'lookup',
	'toolbar=no,scrollbars=yes,titlebar=no,menubar=no,status=no,resizeable=yes,height=' + windowHeight + ',width=' + windowWidth);

}

function checkQuantity()
{
	var form = document.forms['dispensing_amounts'];
	
	if( form )
	{
		var inputs = form.getElementsByTagName('input');
		
		if (inputs) {
			
			quantitySelected = false;
			refillSelected = false;
				
			for (var i = 0; i < inputs.length; i++) {
				if (inputs[i].type == 'radio' && inputs[i].checked
					|| inputs[i].type == 'text' && inputs[i].value) {
					if (inputs[i].name == 'amount') {	
						quantitySelected = true;
					}
					
					if (inputs[i].name == 'refill') {
						refillSelected = true;
					}
				}
			}
			
			if (quantitySelected && refillSelected) {
				submit_form('dispensing_amounts');
			} else {
				alert('Please select both a quantity and a refill option, then try again.');
			}
		}
	}
}

	function validateMemberId()
	{
		var id_input = document.getElementById('member_id_value');
		
		// should match only ten-digit numeric strings
		regex = /^\d{9}/
		
		if (regex.test(id_input.value))
		{
			show_item('lookup_button', true);
		}
		else
		{
			show_item('lookup_button', false);	
		}
	}
	
	/* now used on the pharmacy/start page so that if someone clicks inside
	the member_id_value field and types in a valid member id, they won't get 
	kicked out for forgetting to tick the member id radio button. */
	function validateMemberIdAndSelect()
	{
		var id_input = document.getElementById('member_id_value');
		
		// should match only ten-digit numeric strings
		regex = /^\d{9}/
		
		if (regex.test(id_input.value))
		{
			show_item('lookup_button', true);
			document.getElementById('member_id').checked = true;
		}
		else
		{
			show_item('lookup_button', false);	
		}
	}
	
	function validatePharmacyStart(source)
	{
		// pharmacy start form is either a plan select or a member id:
		var reveal = true;
		
		if(source.value == 'member_id')
		{
			// check the input number:
			show_item('licform', false);
			validateMemberId();
		}
		else
		{			
			// reveal the lic form
			show_item('licform', reveal);
			show_item('lookup_button', reveal);
		}
	}
	
	function validatePharmacyLIC(source)
	{
		var reveal = true;
		var selected = source.selectedIndex;
		
		if (source.options[selected].value == '') {
			reveal = false;
		}
		
		show_item('lookup_button', reveal);
	}