<?php


/**
 *		sabreQMS Module for plugin to Drupal 7 Framework
 *
 *		Discrepancy Search List functions
 *    
*/



/*
 * _get_discrepancy_results_table_header()
 * generates the headers for the search results table so it only gets called once
 *
 */

function _get_discrepancy_results_table_header($bAdmin = True) {
	$table_header = array( 
		'dr_no' => array( 'data' => 'DR No.', 'field' => 'dr_no', 'sort' => 'desc', 'class' => array('qms-dr-results-drno') ),
		'date_opened' => array( 'data' => 'Opened', 'field' => 'date_opened', 'sort' => 'desc', 'class' => array('qms-dr-results-dateopened') ),
		'date_closed' => array( 'data' => 'Closed', 'field' => 'date_closed', 'sort' => 'desc', 'class' => array('qms-dr-results-dateclosed') ),
		'customer' => array( 'data' => 'Customer', 'field' => 'customer', 'sort' => 'asc', 'class' => array('qms-dr-results-customer')  ),
		'full_name' => array( 'data' => 'Technician', 'field' => 'full_name', 'sort' => 'asc', 'class' => array('qms-dr-results-tech')  ),
		'sim_name' => array( 'data' => 'Simulator', 'field' => 'sim_name', 'sort' => 'asc', 'class' => array('qms-dr-results-simulator') ),
	);	
	
	if ($bAdmin) {
		$table_header['admin'] = array( 'data' => 'Admin', 'class' => array('qms-results-admin'));
	}
	$table_header['discrepancy'] = array( 'data' => 'DR Text', 'field' => 'discrepancy', 'sort' => 'asc', 'class' => array('qms-hidden-field') );
	
	return $table_header;
}

/*
 * _get_discrepancy_search_results()
 * display the search results
 * This function gets called by discrepancy_search_form_callback() and discrepancy_pager_callback()
 * also rebuilds the search results and cache after a delete
 *
 */

function _get_discrepancy_search_results($search,	$dr_search_results = Null) {
	
	if ( user_access('search view reports') == FALSE ) {
		drupal_set_message(t('Unauthorized:  User authentication required'));
    return;
  }

	$bAdmin = user_access('administer sabreQMS');
	
	
	
	$bEditDR = ( 	user_access('edit discrepancy') 				||
						 		user_access('set discrepancy flags') 		||
						 		user_access('close discrepancy') 				||
						 		user_access('add discrepancy comments') ||
								user_access('edit discrepancy comments') ||
								user_access('add discrepancy parts') 		||
								user_access('edit discrepancy parts') 	||
								user_access('add discrepancy files') 		||
								user_access('delete discrepancy files') );
						 		
						 			
	$num_per_page =  QMS_RECORDS_PER_PAGE;  
	$table_header;
	
	
	if ( $dr_search_results == Null ) {
		$dr_search_results = new SearchResultsTable('DR');
	}

	if ( !$dr_search_results->hasHeader() ) {
		$table_header = _get_discrepancy_results_table_header(($bEditDR || $bAdmin));
		$dr_search_results->setHeader($table_header);
	}
	else {
		$table_header = $dr_search_results->getHeader();
	}
	
	// get the selected dr cache, if anything has been stored
	$saved_selected_text = '';
	$selected_cache_name = _get_qms_cache_name(QMS_DISCREPANCY_SELECTED);

	$selected_cache = cache_get($selected_cache_name);
	if ( isset($selected_cache->data) && ($selected_cache->data <> '') ) {
		$saved_selected_text = $selected_cache->data;
	}
	
	$search_init = array(
		'table_name' => 'qms_discrepancy_log',
		'table_alias' => 'd',
		'table_header' => $table_header,
		'pager_path' => 'search/discrepancy/pager',
		'order' => QMS_DISCREPANCY_DEFAULT_SORT_COLUMN,
		'order_default' => 'DR No.',
		'sort' => QMS_DISCREPANCY_DEFAULT_SORT,
		'sort_default' => 'desc',
		'variable' => True,
	);
	
	$query = _st_setup_paged_search($search_init);
	
	
	//--------- Date filtering -----------
	
	//  Date opened
	if ( $search->open ) {
		if ( (strlen($search->from_date) > 0) && (strlen($search->to_date) > 0)) {
			$query->condition('date_opened', _st_format_timestamp($search->from_date . " 00:00:00"), '>=');
			$query->condition('date_opened', _st_format_timestamp($search->to_date . " 23:59:59"), '<=');
		}
		else if (strlen($search->from_date) > 0) {
			$query->condition('date_opened', _st_format_timestamp($search->from_date . " 00:00:00"), '>=');
		}
		else if (strlen($search->to_date) > 0) {
			$query->condition('date_opened', _st_format_timestamp($search->to_date . " 23:59:59"), '<=');
		}
		else {
			$query->condition('date_closed', 0, '=');
		}
	}
	
	//  Date closed
	if ( $search->closed ) {
		if ( (strlen($search->from_date) > 0) && (strlen($search->to_date) > 0)) {
			//$date_range[] = _st_format_timestamp($search->from_date . " 00:00:00");
			//$date_range[] = _st_format_timestamp($search->to_date . " 23:59:59");
			//$query->condition('date_closed', $date_range, 'BETWEEN');
			$query->condition('date_closed', _st_format_timestamp($search->from_date . " 00:00:00"), '>=');
			$query->condition('date_closed', _st_format_timestamp($search->to_date . " 23:59:59"), '<=');
		}
		else if (strlen($search->from_date) > 0) {
			$query->condition('date_closed', _st_format_timestamp($search->from_date . " 00:00:00"), '>=');
		}
		else if (strlen($search->to_date) > 0) {
			$query->condition('date_closed', _st_format_timestamp($search->to_date . " 23:59:59"), '<=');
		}
		else {
			$query->condition('date_closed', 0, '>');	
		}
	}
	
	// DR No
	if ( ($search->dr_no <> '') && ( $search->dr_no <> 'DR') ) {  // if only DR prefix, don't bother
		$search->dr_no .= '%';  // append wildcard for fuzzy match
		$query->condition('dr_no', $search->dr_no, 'LIKE');
	}
	
	// Discrepancy Text
	if ( $search->discrep_text <> '' ) {
		//---------------------- Keyword Seaching:  Discrepancy Text --------------
		// Combo Keyword and Exact Phrase Matching ( ex:  keyword "exact phrase" )
		_st_add_keyword_search_condition($query, 'discrepancy', $search->discrep_text);
	}
	
	// MMI
	if ( $search->mmi ) {
		$query->condition('mmi', 1 );
	}
	
	// FAA
	if ( $search->faa ) {
		$query->condition('faa', 1 );
	}
	
	// Engineering
	if ( $search->engineering ) {
		$query->condition('engineering', 1);
	}
  
  // From E-Log
  if ( $search->from_elog ) {
    $query->condition('from_sch_assignment_id', 0, '>');
  }
  
  // Preflight or Exclude Preflight
	if ( $search->preflight ) {
		$query->condition('preflight', 1);
	}
  else if ( $search->excl_preflight ) {
    $query->condition('preflight', 0);
	}
  
  // Prev Maint
	if ( isset($search->prev_maint) ) {
		$query->condition('is_pm', 1);
	}

	
	// simulator -- this has to come before customer since customer is linked to DR through simulator record
  $query->innerJoin('qms_simulators', 's', 's.simulator_id = d.simulator_id');
	if ( $search->simulator_id ) {
		$query->condition('d.simulator_id', $search->simulator_id);
	}
  else {
    // ALL SIMS SEARCH - ignore the sims that have been turned off for ALL searches
    $query->condition('s.include_all_searches', 1);
  }
	
	
	// customer -- simulator gets joined first.
	if ( $search->customer_id ) {
		/*
		$query->innerJoin('qms_customers', 'c', 'c.customer_id = d.customer_id AND c.customer_id = :cid',
											array(':cid' => $search->customer_id) );	
		*/
		$query->innerJoin('qms_customers', 'c', 'c.customer_id = :cid AND c.customer_id = s.customer_id',
											array(':cid' => $search->customer_id) );
	}
	else {
		$query->innerJoin('qms_customers', 'c', 'c.customer_id = s.customer_id');
	}
	
	// tech 
	// if tech search parm > 0, search for assigned techs
	// if tech search parm == 0, do not include the tech_user_id search condition
	// if tech search parm == -1, search for tech_user_id == 0 (flag to allow for unassigned search)
	if ( $search->tech_id > 0 ) {
		// possible for the tech assigned not to be in the employee table... rare but possible
		//$query->leftJoin('qms_employees', 'e', 'e.user_id = d.tech_user_id AND d.tech_user_id = :uid',
			//								array(':uid' => $search->tech_id) );	
		$query->condition('tech_user_id', $search->tech_id);
	}
	else if ( $search->tech_id < 0 )  {  // flag for unassigned tech condition
		$query->condition('tech_user_id', 0, '=');
	}
	// always leftjoin employees as the d.tech_user_id could be zero.
	$query->leftJoin('qms_employees', 'e', 'e.user_id = d.tech_user_id');
	
	// it's possible in some cases, have a tech_user_id == 0 if tech has not yet been assigned (feature setting)
	$query->leftJoin('users', 'u', 'u.uid = d.tech_user_id');
	
	
	// corrective_action		
	if ( $search->action_id > 0 ) {
		$query->innerJoin('qms_corrective_actions', 'a', 
											'a.action_id = d.corrective_action_id AND a.action_id = :aid',
											array(':aid' => $search->action_id) );	
	}
	
	// commment text		
	if ( $search->comment_text <> '' ) {

		$query->innerJoin('qms_discrepancy_comments', 'm', 'm.discrepancy_id = d.discrepancy_id');
		
		//---------------------- Keyword Seaching:  Comment Text --------------
		// Combo Keyword and Exact Phrase Matching ( ex:  keyword "exact phrase" )
		_st_add_keyword_search_condition($query, 'm.comment', $search->comment_text);
	}	

	
	$query->fields('d', array('discrepancy_id', 'dr_no', 'date_opened', 'date_closed', 'tech_user_id', 
																	'discrepancy', 'files_attached') )
							->fields('c', array('customer'))
							->fields('s', array('sim_name', 'active', 'device_id_internal'))
							->fields('e', array('full_name'))
							->fields('u', array('name'));
	
	// determine if items were selected or not		
	if ( strlen($saved_selected_text) ) {
		$list = explode(",", $saved_selected_text);
		$query->addExpression("IF( d.discrepancy_id IN(:sel_list), :y, :n )", "selected", 
																array(':sel_list' => $list, ':y' => 1, ':n' => 0));	
	}		
	$max_count = 0;
	$results = _st_execute_paged_search($query, $max_count);
			
	
	$i = 0;
	$table_rows = array();
	$selected_list = array();
  $maxlen_popup = 400;
	
	global $base_url;
	$has_files = '';
	$show_file_icon = '<span><br /><img class="qms-files-attached" src="' . 
												$base_url . QMS_IMAGES_DIR . 'files_attached.png' . '" /></span>';
	
	foreach ($results as $row) {
		
		// the DR has been closed, only admin edit allowed despite user's edit permissions
		$bAllowEdit = ($row->date_closed ? $bAdmin : $bEditDR );
		
		$tech_name = 
			( (0 == $row->tech_user_id) ? '[unassigned]' : 
					( strlen($row->full_name) ? $row->full_name : $row->name . "*"));
				
		
		// set the id into the array index which is linked to the tableselect's checkbox
		$table_rows[$row->discrepancy_id] = array(
			'dr_no' => array(
				'data' => array(
					'#type' => 'link',
					'#title' => check_plain($row->dr_no),
					'#href' => ('discrepancy/view/' . $row->discrepancy_id),
					'#attributes' => array('qms-url' => url('discrepancy/text/' . $row->discrepancy_id),
																 'class' => array('qms-dr-no')),
					'#options' => array('query' => array('destination' => 'search/discrepancy')),
					'#suffix' => ( $row->files_attached ? $show_file_icon : '' ),
				),
			),
			'date_opened' => _st_format_date($row->date_opened, 'short'),
			'date_closed' => _st_format_date($row->date_closed, 'short', array('fail_empty' => True)),
			'customer' => check_plain($row->customer),  
			'full_name' => $tech_name,
			'sim_name' => _st_format_sim_name($row->sim_name, $row->device_id_internal, $row->active),
		);
		
		// don't allow for the column to exist if user is not an admin, otherwise we get an empty column
		$table_rows[$row->discrepancy_id]['admin'] = array(
			'data' => _get_results_table_options('discrepancy', $row->discrepancy_id, 
																					$bAllowEdit, 'search/discrepancy'), 
			'class' => array('qms-results-admin'),
		);
		
		$table_rows[$row->discrepancy_id]['discrepancy'] = array(
			'data' => check_plain(( strlen($row->discrepancy) > $maxlen_popup ) ? (_st_convert_symbols(substr($row->discrepancy, 0, $maxlen_popup)) . '...') : _st_convert_symbols($row->discrepancy)),
			'class' => 'qms-hidden-popup-text',
		);
		
		if ( strlen($saved_selected_text) ) { $selected_list[$row->discrepancy_id] = $row->selected; }

		$i++;
	}
	
	$pager = theme('pager', array('tags' => array(), 'quantity' => $num_per_page));
	$dr_search_results->setPager($pager);
	$dr_search_results->setResults($table_rows);
	$dr_search_results->setSelected($selected_list);
	
	//Save to cache
	$results_cache_name = _get_qms_cache_name(QMS_DISCREPANCY_SEARCH_RESULTS);
	
	// cache the nested table array structure without rendering
	// rendering has to be done each time the results are displayed or the checkbox selections won't work
	cache_set($results_cache_name, $dr_search_results, 'cache', time() + QMS_CACHE_TIMEOUT);
	
	return $table_rows;
	
		
}



/*
 *  _get_dew_table_select_header()
 * 
 */
function _get_dew_table_select_header($bEditDR) {
  $table_header = array( 
		'dr_no' => array( 
      'data' => 'DR No.', 
      'field' => 'dr_no', 
      'sort' => 'desc', 
      'class' => array('qms-dr-results-drno') 
    ),
		'date_opened' => array( 
      'data' => 'Opened', 
      'field' => 'date_opened', 
      'sort' => 'desc', 
      'class' => array('qms-dr-results-dateopened') 
     ),
		'clear_datetime' => array( 
      'data' => 'Clear By', 
      'field' => 'clear_datetime', 
      'sort' => 'asc', 
      'class' => array('qms-dr-results-clearby')  
    ),
		'mmi_faa' => array( 
      'data' => 'MMI/ FAA', 
      'field' => 'mmi_faa', 
      'sort' => 'asc', 
      'class' => array('qms-dr-results-mmifaa')  
    ),
		'customer' => array( 
      'data' => 'Customer', 
      'field' => 'customer', 
      'sort' => 'asc', 
      'class' => array('qms-dr-results-customer')   
    ),
		'full_name' => array( 
      'data' => 'Technician', 
      'field' => 'full_name', 
      'sort' => 'asc' , 
      'class' => array('qms-dr-results-tech')  
    ),
		'sim_name' => array( 
      'data' => 'Simulator', 
      'field' => 'sim_name', 
      'sort' => 'asc', 
      'class' => array('qms-dr-results-simulator')  
     ),
		//array( 'data' => 'Admin'),
	);
	if ( $bEditDR ) {
		$table_header['admin'] = array( 'data' => 'Admin', 
                             'field' => 'admin', 
                             'class' => array('qms-results-admin'));
	}
  return $table_header;
}


/*
 * _get_discrepany_early_warning_list()
 *
 * display the discrepancy early warning report
 * This function gets called by discrepancy_early_warning_report()
 * function is located here because it shares paging features with _get_discrepancy_search_results()
 */

function discrepancy_early_warning_form($form, &$form_state, 
                                        $vars = array()) {
	
	if ( user_access('search view reports') == FALSE ) {
		drupal_set_message(t('Unauthorized:  User authentication required'));
    return;
  }
  
  // destination gets overridden by drupal_goto() and the
  // sort/paging path so we cannot use here as we
  // in some cases go 3 levels deep and need to back up
  // return based on SESSION['called_from'] url
 

	$bAdmin = user_access('administer sabreQMS');
	$bEditDR = user_access('create edit discrepancy');
  $bFilter = False;
  
  global $base_url;
	global $user;
	$user_is_customer_id = _user_is_customer($user->uid);
	
	
	$table_header = _get_dew_table_select_header($bEditDR);
	
	$search_init = array(
		'table_name' => 'qms_discrepancy_log',
		'table_alias' => 'd',
		'table_header' => $table_header,
		'pager_path' => 'reports/discrepancy_early_warning/pager',
		'order' => QMS_DISCREPANCY_DEFAULT_SORT_COLUMN,
		'order_default' => 'DR No.',
		'sort' => QMS_DISCREPANCY_DEFAULT_SORT,
		'sort_default' => 'desc',
		'variable' => True,
	);
	
	$query = _st_setup_paged_search($search_init);
	
	
	
	//--------- Date Condition -----------
	$query->condition('date_closed', 0, '=');	
	$or = db_or();
  $or->condition('mmi', 1, '=');
  $or->condition('faa', 1, '=');
	$query->condition($or);	
	
	$query->where("DATE_ADD(FROM_UNIXTIME(d.date_opened), INTERVAL CAST((d.clear_time * 24) AS UNSIGNED) HOUR) <= DATE_ADD(NOW(), INTERVAL (5*24) HOUR)");	
	
	// join simulators before customers since customer is linked to DR through the simulator record
	$query->innerJoin('qms_simulators', 's', 's.simulator_id = d.simulator_id');
	$query->leftJoin('qms_customers', 'c', 's.customer_id = c.customer_id');
	// do a left join for both of these...
	// the tech may not be listed as an employee
	// the tech may be unassigned (set to 0)
	$query->leftJoin('qms_employees', 'e', 'e.user_id = d.tech_user_id');
	$query->leftJoin('users', 'u', 'u.uid = d.tech_user_id');
	
	// customer-user?
	if ( $user_is_customer_id ) {
		// add filter to limit results to those related to this customer
		$query->condition('s.customer_id', $user_is_customer_id);
	}	
  
  
  // Check for selected DRs from DEW select form
  $dr_list = array();
  
  if ( isset($_SESSION['dew_selected'])) {
    
    $dew_selected = $_SESSION['dew_selected'];
    //unset($_SESSION['dew_selected']);
    $dr_list = explode(",", $dew_selected);
    $query->where('d.discrepancy_id IN (:sel_list)', array(':sel_list' => $dr_list));
    $bFilter = True;
    $vars['pager'] = False;
  }
  
  
	
	$query->fields('d', array('discrepancy_id', 'dr_no', 'date_opened', 
                            'tech_user_id', 'files_attached') )
							->fields('c', array('customer'))
							->fields('s', array('sim_name', 'device_id_internal', 'active'))
							->fields('e', array('full_name'))
							->fields('u', array('name'));
	$query->addExpression("DATE_ADD(FROM_UNIXTIME(d.date_opened), INTERVAL CAST((d.clear_time * 24) AS UNSIGNED) HOUR)", "clear_datetime");
	$query->addExpression("IF( DATE_ADD(FROM_UNIXTIME(d.date_opened), INTERVAL CAST((d.clear_time * 24) AS UNSIGNED) HOUR) <= DATE_ADD(NOW(), INTERVAL 24 HOUR), :r, :y )", "class", 
															array(':r' => 'dew_red', ':y' => 'dew_yellow'));
	$query->addExpression('IF( d.mmi = 1, :mmi, :faa )', 'mmi_faa', array(':mmi' => 'MMI', ':faa' => 'FAA'));
						
	$max_count = 0;	
	$result = _st_execute_paged_search($query, $max_count);
	
	$i = 0;
	$table_rows = array();
  $show_file_icon = '<span><br /><img class="qms-files-attached" src="' . 
												$base_url . QMS_IMAGES_DIR . 'files_attached.png' . 
                    '" /></span>';
	
	foreach ($result as $row) {
		
		$tech_name = '';
		if ( $row->tech_user_id ) {  // its possble the tech user id will be zero
			$tech_name = ( strlen($row->full_name) ? $row->full_name : $row->name );
		}
		else {
			$tech_name = '[unassigned]';
		}
		
    $table_rows[$row->discrepancy_id] = array(
      'dr_no' => array(
        'data' => array(
          '#type' => 'link',
          '#title' => check_plain($row->dr_no),
          '#href' => ('discrepancy/view/' . $row->discrepancy_id),
          '#attributes' => array('qms-url' => 
                        url('discrepancy/text/' . $row->discrepancy_id),
                                 'class' => array('qms-dr-no')),
          '#options' => array('query' => array('destination' => 
                                  'reports/discrepancy_early_warning')),
          '#suffix' => ( $row->files_attached ? $show_file_icon : '' ),
        ),
      ),
      'date_opened' => array('data' => _st_format_date($row->date_opened, 'short')),
      // trim the seconds from the clear_datetime display to 
      // match 'short' format in _st_format_date()
      // append class as designated in the SQL stmt to set color alerts 
      // (Red:  should be closed in 24 hours, Yellow: to be closed in 5 days)
      'clear_datetime' => array('data' => substr($row->clear_datetime, 0,                                              strlen($row->clear_datetime)-3), 
                                'class' => array($row->class)),
      'mmi_faa' => array('data' => $row->mmi_faa),
      'customer' => array('data' => $row->customer),  
      'full_name' => array('data' => $tech_name),
      'sim_name' => array('data' => _st_format_sim_name($row->sim_name, 
                                        $row->device_id_internal, 
                                        $row->active)),

      );
    
		
		if ( $bEditDR ) {
			// don't allow for the column to exist if user is not an admin, otherwise we get an empty column
        $table_rows[$row->discrepancy_id]['admin'] = 
            array('data' => _get_dew_table_options($row->discrepancy_id, 
                                                   $bEditDR), 
                  'class' => array('qms-results-admin'));
		}
		
		
		//$table_rows[] = array('data' => $row_data );
		$i++;
	}
  
//    $form['#after_build'][] = 'discrepancy_early_warning_form_after_build';
  
  
  
  $pager = theme('pager', array('tags' => array(), 
                       'quantity' => QMS_RECORDS_PER_PAGE));
  
  $form['dew_div'] = array(
		'#markup' => '<div id="qms-dew-div">',
	);
  
  // do not display the filter link if there is nothing to filter
  if ( !$bFilter ) {
    
    if ($i > 1) {
      $form['filter_link'] = array(
        '#markup' => 
          l(t('Filter Report'), 
            'reports/discrepancy_early_warning/filter',
            array('attributes' => 
                      array('class' => array('qms-btn-filter-report')) )),
                  //'query' => array('destination' => $goto_url) ) ),

      );
    }
    else {
      $bFilter = True; // fake it -- hide the checkboxes if only 1 displaying
    }
  }
  
  $form['pager_top'] = array(
		'#markup' => $pager,	
	);
	
  
  if ( !$bFilter ) {
    
    // regular DEW report, display table select to allow for filtering
    $form['dew_table'] = array(
      //'#name' => 'qms-dew-table',
      '#type' => 'tableselect',
      '#header' => $table_header,
      '#options' => $table_rows,
      '#js_select' => True,
      '#multiple' => True,
      '#advanced_select' => True,
      '#empty' => t('None'),
      '#sticky' => True,
      '#attributes' => array('id' => 'qms-dew-table'),
    );
  }
  else {
    // if this is a filtered report from the original DEW report,
    // display an ordinary table
    $form['dew_table'] = array(
      '#markup' => 
        theme('table', array(
          'header' => $table_header,
          'rows' => $table_rows,
          'empty' => t('None'),
          'attributes' => array('class' => array('dew_table')),
        )),
    );
  }
  
  
  $form['pager_bottom'] = array(
		'#markup' => $pager . '</div>',	
	);
  
  
  
  //if ( !isset($vars['pager']) || !$vars['pager'] ) {
    
    $form['record_count'] = array(
      '#markup' => (($max_count > 0) ? 
                    '<br/><p class="qms-text">Total Records: ' . 
                     $max_count . 
                    '</p>' : ''),
    );
    
    if ( !$bFilter ) {
      //hidden textarea for storing table_select clicks
      $form['dew_selected'] = array(
        '#type' => 'textarea',
        '#resizable' => False,
        '#default_value' => '',
        '#attributes' => array('id' => 'qms-dew-selected',
                               'class' => array('qms-hidden')),
        '#prefix' => '<div class="qms-hidden">',
        '#suffix' => '</div>',
      );

      //hidden submit button triggered by 'Filter Report' link above table
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
        '#submit' => array('discrepancy_early_warning_form_submit'),
        '#attributes' => array('class' => array('qms-hidden'),
                               'id' => 'qms-dew-submit'),  
      );
      
    }
  //}
  
  return $form;
}

/*
 *  discrepancy_early_warning_form_after_build
 * 
 */
/*
function discrepancy_early_warning_form_after_build($form, &$form_state) {
  drupal_add_js('/misc/tableselect.js');
  drupal_add_js(drupal_get_path('module', 'sabreTools') . '/js/sabreTools.lib.js');  
  drupal_add_js(drupal_get_path('module', 'sabreQMS') . '/js/sabreQMS.dew.js');  
			// addt'l JS for DEW report ajax call
  
  // sabreTools common js is loaded with the generic report_form
  return $form;
}*/


/*
 *  discrepancy_early_warning_form_submit
 *  Gets the list of selected DRs and routes to the filtered report
 */
function discrepancy_early_warning_form_submit($form, $form_state) {
  $dew_selected = $form_state['values']['dew_selected'];
  
  
  if (strlen($dew_selected)) {
    $_SESSION['dew_selected'] = $dew_selected;
  }
  else { 
    unset($_SESSION['dew_selected']); 
    unset($_GET['destination']);
  }
  
  drupal_goto('reports/discrepancy_early_warning/filter'); 
}


