/**
 * Implementation of hook_form(). A multistep form that pulls in content from the database and renders it into the form based
 * on that locations id. Checkboxes should be populated with their defaults as stored in the database. Checkbox values are
 * converted from a string to an array for processing by default_value.
 */
function locations_locations_form(&$lid, $form_values = NULL)
{
	$this_location = $lid;
	if (isset($this_location) && strspn($this_location, '1234567890') == strlen($this_location)) {
		$sql = "SELECT sid, fid, rid, lid, name, symbol, phone, fax, tty, email, url, handicapped_access, public_note, staff_note, units, activities FROM {locations_locations} WHERE lid = ". $this_location;
		$result = db_query(db_rewrite_sql($sql));
		$location = db_fetch_object($result);
	}
	$form = array();

	$form['#multistep'] = TRUE;
	$form['#redirect'] = FALSE;
	
	$step = isset($form_values) ? (int) $form_values['step'] : 1;

	$form['locations_locations'] = array(
		'#type' => 'fieldset',
		'#title' => t('Location: Step @number', array('@number' => $step))
	);
	
	$form['locations_locations']['lid'] = array(
		'#type' => 'value',
		'#value' => $location->lid
	);
	
	$form['locations_locations']['step'] = array(
		'#type' => 'hidden',
		'#value' => $step + 1,
		);
	
	switch($step) {
		case 1:
			$form['locations_locations']['sid'] = array(
				'#type' => 'select',
				'#title' => 'Space',
				'#default_value' => $location->sid,
				'#options' => _spaces_dropdown(),
				'#required' => TRUE,
				);
		$button_text = t('Next');
		break;
		
		case 2:
		$form['locations_locations']['sid'] = array(
			'#type' => 'hidden',
			'#value' => isset($form_values) ? $form_values['sid'] : ''
			);
		
		$form['locations_locations']['fid'] = array(
				'#type' => 'select',
				'#title' => 'Level',
				'#default_value' => $location->fid,
				'#options' => _levels_dropdown($form_values['sid'])
				);
		$button_text = t('Next');
		break;
			
		case 3:
			$form['locations_locations']['sid'] = array(
				'#type' => 'hidden',
				'#value' => isset($form_values) ? $form_values['sid'] : ''
				);
		
			$form['locations_locations']['fid'] = array(
				'#type' => 'hidden',
				'#value' => isset($form_values) ? $form_values['fid'] : ''
				);

			$form['locations_locations']['rid'] = array(
					'#type' => 'select',
					'#title' => 'Room',
					'#default_value' => $location->rid,
					'#options' => _rooms_dropdown($form_values['fid'], $form_values['sid'])
					);

			$form['locations_locations']['name'] = array(
				'#type' => 'textfield',
				'#title' => t('Location Name'),
				'#default_value' => $location->name,
				'#required' => TRUE
				);

			$form['locations_locations']['symbol'] = array(
				'#type' => 'textfield',
				'#title' => t('Symbol'),
				'#default_value' => $location->symbol,
				'#size' => '10',
				'#maxlength' => '10'
				);
						
			$form['locations_locations']['phone_areacode'] = array(
				'#type' => 'textfield',
				'#title' => t('Phone'),
				'#default_value' => substr($location->phone, 0, 3),
				'#size' => '3',
				'#maxlength' => '3'
				);

			$form['locations_locations']['phone_prefix'] = array(
				'#type' => 'textfield',
				'#default_value' => substr($location->phone, 3, 3),
				'#size' => '3',
				'#maxlength' => '3'
				);

			$form['locations_locations']['phone_number'] = array(
				'#type' => 'textfield',
				'#default_value' => substr($location->phone, 6, 4),
				'#size' => '4',
				'#maxlength' => '4'
				);

			$form['locations_locations']['fax_areacode'] = array(
				'#type' => 'textfield',
				'#title' => t('Fax'),
				'#default_value' => substr($location->fax, 0, 3),
				'#size' => '3',
				'#maxlength' => '3'
				);

			$form['locations_locations']['fax_prefix'] = array(
				'#type' => 'textfield',
				'#default_value' => substr($location->fax, 3, 3),
				'#size' => '3',
				'#maxlength' => '3'
				);

			$form['locations_locations']['fax_number'] = array(
				'#type' => 'textfield',
				'#default_value' => substr($location->fax, 6, 4),
				'#size' => '4',
				'#maxlength' => '4'
				);

			$form['locations_locations']['tty_areacode'] = array(
				'#type' => 'textfield',
				'#title' => t('TTY'),
				'#default_value' => substr($location->tty, 0, 3),
				'#size' => '3',
				'#maxlength' => '3'
				);

			$form['locations_locations']['tty_prefix'] = array(
				'#type' => 'textfield',
				'#default_value' => substr($location->tty, 3, 3),
				'#size' => '3',
				'#maxlength' => '3'
				);

			$form['locations_locations']['tty_number'] = array(
				'#type' => 'textfield',
				'#default_value' => substr($location->tty, 6, 4),
				'#size' => '4',
				'#maxlength' => '4'
				);

			$form['locations_locations']['email'] = array(
				'#type' => 'textfield',
				'#title' => t('Email'),
				'#default_value' => $location->email
				);

			$form['locations_locations']['url'] = array(
				'#type' => 'textfield',
				'#title' => t('URL'),
				'#default_value' => $location->url
				);
			$form['locations_locations']['handicapped_access'] = array(
				'#type' => 'radios',
				'#title' => t('Wheelchair Access'),
				'#options' => array(
					'0' => t('Not Accessible'),
					'1' => t('Partially Accessible'),
					'2' => t('Fully Accessible')
					),
				'#default_value' => $location->handicapped_access
				);
			$form['locations_locations']['public_note'] = array(
				'#type' => 'textarea',
				'#title' => t('Public Note'),
				'#rows' => '',
				'#columns' => '',
				'#default_value' => isset($location->public_note) ? $location->public_note : NULL
				);
			$form['locations_locations']['staff_note'] = array(
				'#type' => 'textarea',
				'#title' => t('Staff Note'),
				'#rows' => '',
				'#columns' => '',
				'#default_value' =>  isset($location->staff_note) ? $location->staff_note : NULL
				);
			$unit_options = array(
				'B' => t('Branch Unit'),
				'R' => t('Research Unit'),
				'C' => t('Central Unit')
				);
			$units = explode(';', "'". $location->units ."'");
			$form['locations_locations']['units'] = array(
				'#type' => 'checkboxes',
				'#title' => 'Unit Affiliations',
				'#options' => $unit_options,
				'#default_value' => $units,
				'#required' => TRUE
				);
			$activities_options = array(
				'E' => t('Has Events'),
				'C' => t('Has Classes'),
				'B' => t('Has Book Sales')
				);
			$activities = explode(';', $location->activities);
			$form['locations_locations']['activities'] = array(
				'#type' => 'checkboxes',
				'#title' => 'Activities',
				'#options' => $activities_options,
				'#default_value' => $activities
				);

		$button_text = t('Submit');
		break;
	}
	
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => $button_text
		);

	return $form;
}