Location: PHPKode > projects > Homeless Mangement Information System > hmis/include/questions_display.inc
<?php
//*Client Data System, Copyright (C) 2000, 2001, 2002 Tedd Kelleher.  This is free software, subject to the 
//*GNU GENERAL PUBLIC LICENSE, Version 2, June 1991 (in file named gpl.txt), which should accompany 
//*any distribution of this file.  Tedd Kelleher can be contacted at hide@address.com
//A collection of functions that return HTML questions based on submitted parameters

/********************************************************************************************
 * Name: build_tabindex_html: 
 * Description: This function is intended to solve all the tabindex fud and bring order out of chaos.
 * 
 * Details and Notes: 
 * 
 * @param: $index -- if set and 'explicit' then this tabindex is used.
 * @param: $precidence -- if set and 'mixed' this allows one to decrement the precidence of some indexes 
 *         so they always come last.
 * 
 * @global: GLOBAL $tabbing_preference is set in initialize.inc
 * 
 * @return: portion of html tag pertaining to tabindex param
 * 
 * History: Updated 2/24/03 by Jeff.
 ********************************************************************************************/
function build_tabindex_html ($index,$precedence=0)
{
    GLOBAL $tabbing_preference;
    
    if ($tabbing_preference == 'explicit') //bugbug: right now the explicit taborder is fooo'd
    {
        return ' tabindex="'.$index.'" ';
    }
    else if ($tabbing_preference == 'implicit')
    {
        return ''; //return no tabindex
    }
    else //assume $tabbing_preference == mixed
    {
        return ' tabindex="'.($precedence+1).'" ';
    }
}

function add_more_clients_button ()
{
	GLOBAL $head_dynamic_ecma, $page_id;
	$head_dynamic_ecma[2] = '
		function disableform(which)
		{
		  if (document.images) 
		  {
		    for (i=0;i<which.length;i++) 
		    {
	           var tempobj=which.elements[i];
			   
			   if (tempobj.type!="hidden") 
          	   {
		           tempobj.disabled=true;
			   }
		    }
		  }
		}
	';
	$head_dynamic_ecma[1] .= "\ndisableform(formid_".$page_id.");\n";
	$head_dynamic_ecma[1] .= "\naddmoreBtn.focus();\n";
    
	$final_html .=  "\n<input type=\"button\" id=\"addmoreBtn\" name=\"addmoreBtn\" value=\"Add more...\" onclick=\"javascript:window.location.reload( window.location.href+'?report_name=client_profile&empty_form=yes' );\">";
	return $final_html;
}

function edit_button ($alternative_backout=0)
{
	GLOBAL $head_dynamic_ecma, $page_id, $question_validation_error;
	$head_dynamic_ecma[2] = '
		function enableform(which)
		{
		  if (document.images) 
		  {
		    for (i=0;i<which.length;i++) 
		    {
	           var tempobj=which.elements[i];
			   if (tempobj.type!="hidden")
               {
		           tempobj.disabled=false;
			   }
		    }
		  }
		}
		
		function disableform(which)
		{
		  if (document.images) 
		  {
		    for (i=0;i<which.length;i++) 
		    {
	           var tempobj=which.elements[i];
			   
			   if (tempobj.type=="reset")
          	   {
		           tempobj.click(); //reset form FIRST
				   tempobj.disabled=true;
			   }
			   else if (tempobj.type!="hidden")
			   {
			   	   tempobj.disabled=true;
			   }
		    }
		  }
		}
		
		function editBtnClick(myswitch,onloadcall)
		{
			if (myswitch=="Edit")
			{
				editBtn.value = "Nevermind";
				enableform(formid_'.$page_id.');
			}
			else
			{
				editBtn.value = "Edit";
			    disableform(formid_'.$page_id.');
			}
			return false;
		}
	';
	
	//if there is a validation error then allow back out or "Nevermind" which means flip from 'Edit'... 
	if ($question_validation_error)
		$head_dynamic_ecma[1] .= "\neditBtnClick(\"Edit\");\n";
	else //else flip 'Nevermind'
		$head_dynamic_ecma[1] .= "\neditBtnClick(\"Nevermind\");\n";
    
	$final_html .=  "\n<input type=\"button\" id=\"editBtn\" name=\"editBtn\" value=\"Edit\" onclick=\"editBtnClick(this.value);\">";
	return $final_html;
}


function form_start ($action='', $target='')
{
	GLOBAL $page_id;
	GLOBAL $this_page;
		
	$final_html .=  "\n<form id=\"formid_".$page_id."\" method=\"POST\" "; 
	if($action)	{$final_html .=  "action=\"".$action."\" ";}
		else {$final_html .=  "action=\"".$this_page."\" ";}
	if($target)	{$final_html .=  "target=\"".$target."\" ";}
	$final_html .=  "autocomplete=\"false\"> ";
	$final_html .=  "<input type=\"hidden\" name=\"form_submitted\" value=\"".$page_id."\">\n";
	return $final_html;
}


function submit_button ( $name, $button_message = 'Submit form' )
{
	GLOBAL $page_id;
	
	$final_html .=   "\n<input type=\"submit\" id=\"".$name."\" ";
	$final_html .=   "value=\"".$button_message."\" ".build_tabindex_html(19000).">";
	
	return $final_html;
}



function form_end ( $button_message = 'Submit form', $reset_button = '' )
{
	GLOBAL $page_id, $unix_date;
	
	$final_html .=   "\n<input type=\"submit\" id=\"submitid_".$page_id."\" ";
	$final_html .=   "value=\"".$button_message."\" ".build_tabindex_html(19001).">";
	//Note: reset_button style 1 is the typical reset button.  
	//reset_button style 2 is a special case needed to force a reload of the client lookup form if previous post results are present.
	if ($reset_button==2) {$final_html .= "&nbsp;<input type=\"button\" name=\"resetid_".$page_id."\" id=\"resetid_".$page_id."\" value=\"Reset form\"
     ".build_tabindex_html(19002)." onclick=\"javascript:window.location.reload( window.location.href+'?clear_results=1' );\">";}
	elseif($reset_button==1) {$final_html .= "&nbsp;<input type=\"reset\" id=\"resetid_".$page_id."\" value=\"Reset form\"
     ".build_tabindex_html(19002).">";}
	elseif ($reset_button==3) {$final_html .= "&nbsp;<input type=\"button\" name=\"resetid_".$page_id."\" id=\"resetid_".$page_id."\" value=\"Reset form\"
     ".build_tabindex_html(19002)." onclick=\"javascript:window.location.reload( window.location.href+'?report_name=client_profile&empty_form=yes' );\">";}
     
    $final_html .= "<input type=\"hidden\" name=\"fsig\" value=\"".$unix_date."\">\n";   
	
	$final_html .=  "\n</form>\n";
	return $final_html;
}



function display_text_question ( $question_attributes_array, $form_answer, $include_td = '', $password = FALSE, $display_for_edit_forms_admin = '' )
{
	GLOBAL $question_validation_error;
    
    $question_id = $question_attributes_array['question_id'];
	
	if ( $include_td )
	{
        $final_html .= "<td class=\"tdq\" id=\"QUEST_".$question_id."\"> ";
    }
	$final_html .= "\n&nbsp;".$question_attributes_array["question_title"]."&nbsp; ";
    
	if($password)
	{
        $final_html .=  "<input type=\"password\" ";
    }
    else
	{
        $final_html .=  "<input type=\"text\" ";
    }
	
    $final_html .=  "name=\"form_answer[".$question_id."]\" ";
    
	if ( $question_attributes_array['question_display_size'] )
	{
		$final_html .=  "size=\"".$question_attributes_array['question_display_size']."\" ";
	}
	else
	{
		$final_html .=  "size=\"".$question_attributes_array['question_field_size']."\" ";
	}
	
	if ( $question_attributes_array['question_ecma']  )
    {
		$final_html .=  $question_attributes_array['question_ecma']."=\"go('".$question_id."')\" ";
	}
	$final_html .=  "maxlength=\"".$question_attributes_array['question_field_size']."\" ";
    
	//If an answer to the question has been submitted, use the submitted value.  Otherwise default
	if ( !$form_answer[$question_id] )
    {
        //The following two conditionals simply adds disabled attribute to <input so that when in EDIT FORMS the uneditable noan elements are disabled.
        /* this does not work as it caused problems when the form is submitted...
        if ( $display_for_edit_forms_admin )
        { 
          if ( strstr($question_attributes_array['question_default_value'],'#') ||
             $question_attributes_array['question_default_value']=='Don\'t know' || 
             $question_attributes_array['question_default_value']=='Refused' || 
             $question_attributes_array['question_default_value']=='Not applicable')
          {
            $final_html .= "disabled ";
          }
        }*///end of hack
		$final_html .=  "value=\"".$question_attributes_array['question_default_value']."\" ";
	}
	elseif ( !$display_for_edit_forms_admin && ($form_answer[$question_id]=='#99#' || $form_answer[$question_id]=='#98#' || $form_answer[$question_id]=='#88#' || $form_answer[$question_id]=='#77#')  ) 
	{
            //echo ' YO:qid'.$question_id;
		$final_html .= "value=\"\" ";
		//load noan array entry for use by no_answer_options() call below...
		$form_answer['noan'][$question_id] = $form_answer[$question_id];
	}
    //if want to display noan codes you are likely on the edit questions page in which case you don't want these values to be editable
	elseif ( $display_for_edit_forms_admin && ($form_answer[$question_id]=='Refused' || $form_answer[$question_id]=='Not applicable' || $form_answer[$question_id]=='Don\'t know' 
             || $form_answer[$question_id]=='#99#' || $form_answer[$question_id]=='#98#' || $form_answer[$question_id]=='#88#' || $form_answer[$question_id]=='#77#')  )
	{
        //echo ' qid'.$question_id;  wanted to add "disabled" but this caused problems when the form was submitted...
        $final_html .= "value=\"".stripslashes($form_answer[$question_id])."\" ";
	}
	else
	{
        $final_html .= "value=\"".stripslashes($form_answer[$question_id])."\" ";
    }
	$final_html .= build_tabindex_html($question_attributes_array['question_display_order']).">";
	
    $final_html .= no_answer_options ( $question_attributes_array, $form_answer['noan'][$question_id]);
    
	if ($question_validation_error[$question_id])
	{
		//echo "<p id=\"qid_2\">".$question_validation_error[$question_id]."</p>";
		$final_html .=  "<p id=\"error_style_".$question_id."\">".htmlentities($question_validation_error[$question_id]).'</p>';
	}
	if ( $include_td )
	{
        $final_html .= '</td>';
    }
	return $final_html;
}



function display_number_question ($question_attributes_array, $form_answer, $include_td = "")
{
	GLOBAL $question_validation_error;
	//GLOBAL $form_answer;
	//echo "Num Answer is: ".$form_answer[2]."<br/>";
	$question_id = $question_attributes_array["question_id"];
	if($include_td)	{$final_html .= "\n<td class=\"tdq\" id=\"QUEST_".$question_id."\"> ";}
	$final_html .= "\n&nbsp;".$question_attributes_array["question_title"]."&nbsp; ";
	$final_html .=  "\n<input type=\"text\" "; 
	$final_html .=  "name=\"form_answer[".$question_id."]\" ";
	if($question_attributes_array["question_display_size"])	{
		$final_html .=  "size=\"".$question_attributes_array["question_display_size"]."\" ";
	}
	else
	{
		$final_html .=  "size=\"".$question_attributes_array["question_field_size"]."\" ";	
	}
	
	if($question_attributes_array["question_ecma"])
	{
		$final_html .=  $question_attributes_array["question_ecma"]."=\"go('".$question_id."')\" ";
	}
	$final_html .=  "maxlength=\"".$question_attributes_array["question_field_size"]."\" ";
	//If an answer to the question has been submitted, use the submitted value.  Otherwise default
	if(!$form_answer[$question_id] && strlen($form_answer[$question_id]) < 1)	
	{
		$final_html .=  "value=\"".$question_attributes_array["question_default_value"]."\" ";
    }
	elseif ( $form_answer[$question_id]=='#99#' || $form_answer[$question_id]=='#98#' || $form_answer[$question_id]=='#88#' || $form_answer[$question_id]=='#77#' ) 
	{
		$final_html .= "value=\"\" ";
		//load noan array entry for use by no_answer_options() call below...
		$form_answer['noan'][$question_id] = $form_answer[$question_id];
	}
    else
    {
        $final_html .= "value=\"".$form_answer[$question_id]."\" ";
    }
	$final_html .= build_tabindex_html($question_attributes_array['question_display_order']).">";
    
    $final_html .= no_answer_options ( $question_attributes_array, $form_answer['noan'][$question_id]);
	
	if ($question_validation_error[$question_id])
	{
		//echo "<p id=\"qid_2\">".$question_validation_error[$question_id]."</p>";
		$final_html .=  "<p id=\"error_style_".$question_id."\">".htmlentities($question_validation_error[$question_id])."</p>";
	}
	if($include_td)	{$final_html .= "</td>\n";}
	return $final_html;
}






function display_radio_buttons_question ( $question_attributes_array, $question_elements_array, $form_answer, $include_td = '' )
{
	GLOBAL $question_validation_error;
	GLOBAL $page_id; 
	GLOBAL $form_submitted;
	
    $question_id = $question_attributes_array['question_id'];
	if($include_td)	{$final_htmla .= "\n<td class=\"tdq\" id=\"QUEST_".$question_id."\" > ";}
	$final_htmla .= "<table class=\"tablerad\">";
    $final_htmla .= "\n<tr><td colspan=\"4\">".$question_attributes_array['question_title'];
	
    $array_count = 0;
    
	if (is_array($question_elements_array))	{
		$array_count = 0;
        $even_count = 1;
        $len_count = 0;
        
		foreach ($question_elements_array AS $key => $current_element)	{
			
            $array_count++;
            $len_count = $len_count + strlen ( $current_element['question_element_title'] );
            
            if ( $even_count == 1 )  {
                //$final_htmla .= $final_html."CC</span></td></tr><tr><td class=\"nobreak\"><span class=\"smaller\">";
                $final_htmla .= $final_html."</span></td></tr><tr><td class=\"tdradbut\">";
                $even_count = 0;
                $final_html = "";
                $len_count = 0;
            
            }
            elseif ( $even_count == 0 )  {
                //$final_htmla .= $final_html ."DD</span></td><td class=\"nobreak\"><span class=\"smaller\">";
                $final_htmla .= $final_html ."</span></td><td class=\"tdradbut\">";
                $even_count = 1;
                $final_html = "";
                //$len_count = 0;  
            
            }
			//if ($array_count > 0 )   {
                //$final_html .= "&nbsp;&nbsp;";                 
            //}
            //$array_count++;
            //$final_html .= "<span class=\"nobreak\">";
            //$final_html .= str_replace (" ", "&nbsp;", $current_element["question_element_title"]);
			//$final_html .= "<td>";
            $final_html .= "<input type=\"radio\" name=\"form_answer[".$question_id."]\" value=\"".$key."\" "; 
			
            //echo "Key is: ".$key." ; Form answer is: ".$form_answer[$question_id]."Quest element value is: ".$current_element["question_element_value"]."<br/>";
            
			if(!$form_answer[$question_id])	{
				if($question_attributes_array["question_default_value"] == $key )	{
					$final_html .= "checked=\"checked\" ";
			    }
			}
			
            if($form_answer[$question_id] == $key || $form_answer[$question_id] == $current_element["question_element_value"])	{
			            
            	$final_html .= "checked=\"checked\" ";
			}
			
			if($question_attributes_array["question_ecma"])	{	
			    $final_html .=  $question_attributes_array["question_ecma"]."=\"go('[".$question_id."][".$key."]')\" ";
			}

			$final_html .= build_tabindex_html($question_attributes_array['question_display_order']).">";
            $final_html .= "</td>";
            //$final_html .= str_replace (" ", "&nbsp;", $current_element["question_element_title"]);
            if ( $even_count == 0 ) {
            $z = "tdradtxtl";
            }
            else {
                $z = "tdradtxt";
            }
            $final_html .= "<td class=\"".$z."\"><span class=\"radiotext\">";
            $final_html .= wordwrap( $current_element["question_element_title"], 26, "<br/>");
            
            //$final_html .= "</span>";
            //$final_html .= "adadgggg";	
        }
			//$final_html .= "</span>";
            
	}
	//If not an array of checkbox elements, add error message to HTML
	else	{
        $final_htmla .= "No Radio Buttons Defined for Question #".$question_id."&nbsp;</span> ";
    }
	$final_htmla .= $final_html;
    
    if ( $even_count == 1 && $array_count > 0 ) {
            $final_htmla .= "&nbsp;</td></tr>";        
    }
    else {
        $final_htmla .= "</td></tr>";
    }
    
    //$final_htmla .= no_answer_options ( $question_attributes_array, $form_answer['noan'][$question_id], 'radio_or_checkbox' );
    	
	if ($question_validation_error[$question_id])	{
		$final_htmla .=  "<tr><td colspan=\"4\"><p
        id=\"error_style_".$question_id."\">".htmlentities($question_validation_error[$question_id])."</p></td></tr> ";
													}
	$final_htmla .= "</table>";
	if($include_td)	{$final_htmla .= "</td>\n ";}
	$final_html = $final_htmla;
    return $final_html;
}



function display_dropdown_list_question ($question_attributes_array, $question_elements_array, $form_answer, $include_td = "", $search_form = "")
{
	GLOBAL $question_validation_error;
	GLOBAL $page_id;
	GLOBAL $form_submitted;
	
	$question_id = $question_attributes_array["question_id"];
	if($include_td)	{$final_html .= "\n<td class=\"tdq\" id=\"QUEST_".$question_id."\"> ";}
	$final_html .= "\n".$question_attributes_array["question_title"]."<br>";
	$final_html .= "\n<select name=\"form_answer[".$question_id."]\" ";
	if($question_attributes_array["question_ecma"])
    {
			$final_html .=  $question_attributes_array["question_ecma"]."=\"go('[".$question_id."][".$key."]')\" ";
	}
	$final_html .= build_tabindex_html($question_attributes_array['question_display_order']);
	$final_html .= "> ";
	
	if (is_array($question_elements_array))
    {
		// if SEARCH add element...  set and 0 then remove from list of params to search on...
		if ($search_form && $question_elements_array["1"]["question_id"] == 'org_client_status0')
		{
			$new_question_elements["0"] = array ("question_element_id" => "element_org_client_status0", "question_id" => "org_client_status0", "question_element_title" => "[SELECT STATUS]", "question_element_value" => "all");
            
			$question_elements_array = array_merge($new_question_elements, $question_elements_array);
			
			$question_attributes_array["question_default_value"] = 0;
			//$question_elements_array["0"]["question_element_value"] = "99";
		    //echo "<p><font color=red>Search_form=";
            //echo pretty_var_dump($question_elements_array)."<br>".$form_answer[$question_id];
	        //echo "</font></p>";
		}
		
		//echo "<p>question_elements_array[0][question_id]:";
	 	//echo pretty_var_dump($question_elements_array);
	    
		$already_selected = FALSE;
		
		foreach ($question_elements_array AS $key => $current_element)
		{
			$final_html .= "\n<option ";
			
			if(!$form_answer[$question_id])	{
				if(!$already_selected && $question_attributes_array["question_default_value"] == $key)
                {
				    //echo $current_element["question_element_title"]." is SELECTED A<br>";
					$final_html .= "selected ";
					$already_selected = TRUE;
			    }
			}
			
			//unfortunately $form_answer[$question_id] == $key could be 0 == "" which resolves to TRUE so first check that question_id is not an empty string.  Also ensure we are only selecting one option by default, another issue caused by status...
			if(!$already_selected && $form_answer[$question_id] != "" && ($form_answer[$question_id] == $key || $form_answer[$question_id] == $current_element["question_element_value"]) )
			{
				$final_html .= "selected ";
				//echo $current_element["question_element_title"]." is SELECTED B<br>";
				$already_selected = TRUE;
			}
            
			if ($search_form)
				$final_html .= "value=\"".$current_element["question_element_value"]."\">".$current_element["question_element_title"]."</option>"; //bugbug - why was option value set to $key instead of $current_element["question_element_value"]...???  Seems odd but I coded status dropdown this way to be consistant...
			else
				$final_html .= "value=\"".$key."\">".$current_element["question_element_title"]."</option>"; //bugbug - why was option value set to $key instead of $current_element["question_element_value"]...???  Seems odd but I coded status dropdown this way to be consistant...
			//}
        
	    }
	    $final_html .= "</select>\n";
	}
    
	//If not an array of checkbox elements, add error message to HTML
	else	{$final_html .= "No Checkboxes Defined for Question #".$question_id."&nbsp;";}
    
    //$final_html .= no_answer_options ( $question_attributes_array, $form_answer['noan'][$question_id] );
	
	
	if ($question_validation_error[$question_id])
    {
		//echo "<p id=\"qid_2\">".$question_validation_error[$question_id]."</p>";
		$final_html .=  "\n<p id=\"error_style_".$question_id."\">".htmlentities($question_validation_error[$question_id])."</p>";
    }
	
	
	if($include_td)	{$final_html .= "</td>\n";}
	return $final_html;
}



function display_checkboxes_question ($question_attributes_array, $question_elements_array, $form_answer, $include_td = "")
{
	GLOBAL $question_validation_error;
	GLOBAL $page_id; 
	GLOBAL $form_submitted;
	
	$question_id = $question_attributes_array["question_id"];
	
    if($include_td)	{$final_htmla .= "\n<td class=\"tdq\" id=\"QUEST_".$question_id."\"> ";}
	$final_htmla .= "<table class=\"tablerad\">";
    $final_htmla .= "<tr><td colspan=\"4\">".$question_attributes_array["question_title"]."<br/>\n";
	
	if (is_array($question_elements_array))	{
		
        $array_count = 0;
        $even_count = 2;//$even_count = 1;
        $len_count = 0;
        
		foreach ($question_elements_array AS $key => $current_element)	{
			
            $array_count++;
            $len_count = $len_count + strlen ( $current_element["question_element_title"] );
            
            if ( $even_count == 1 )
            {
                //$final_htmla .= $final_html."CC</span></td></tr><tr><td class=\"nobreak\"><span class=\"smaller\">";
                $final_htmla .= $final_html."</span></td></tr><tr><td class=\"tdradbut\">";
                $even_count = 0;
                $final_html = '';
                $len_count = 0;
            
            }
            elseif ( $even_count == 0 )
            {
                //$final_htmla .= $final_html ."DD</span></td><td class=\"nobreak\"><span class=\"smaller\">";
                $final_htmla .= $final_html ."</span></td><td class=\"tdradbut\">";
                $even_count = 1;
                $final_html = '';
                //$len_count = 0;  
            
            }
            elseif ( $even_count == 2 )
            {
                //$final_htmla .= $final_html."CC</span></td></tr><tr><td class=\"nobreak\"><span class=\"smaller\">";
                $final_htmla .= $final_html."</td></tr><tr><td class=\"tdradbut\">";
                $even_count = 0;
                $final_html = '';
                $len_count = 0;
            }
            
            //$final_html .= "<span class=\"nobreak\">";
            
			$final_html .= "\n";
			//$final_html .= str_replace (" ", "&nbsp;", $current_element["question_element_title"]);
			$final_html .= "\n<input type=\"checkbox\" name=\"form_answer[".$question_id."][".$key."]\" value=\"yes\" "; 
			
			if ( !$form_submitted )
            {
				if ( $current_element['question_element_checked'] == 'yes' )
                {
					$final_html .= "checked=\"checked\" ";
				}
			}
			
            if ( $form_answer[$question_id][$key] == 'yes' || $form_answer[$question_id][$key] ==
                $current_element['question_element_value'] )
            {
				$final_html .= "checked=\"checked\" ";
			}
			
			if($question_attributes_array['question_ecma'])
            {
			    $final_html .=  $question_attributes_array["question_ecma"]."=\"go('[".$question_id."][".$key."]')\" ";
			}
            
			$final_html .= build_tabindex_html($question_attributes_array['question_display_order']).">";
			$final_html .= '</td>';
            //$final_html .= str_replace (" ", "&nbsp;", $current_element["question_element_title"]);
            if ( $even_count == 0 )
            {
                $z = 'tdradtxtl';
            }
            else
            {
                $z = 'tdradtxt';
            }
            $final_html .= "<td class=\"".$z."\"><span class=\"radiotext\">";
            $final_html .= wordwrap( $current_element['question_element_title'], 25, "<br/>");
		}
	    //$final_html .= "\n</span>";
        //$final_htmla .= "&nbsp;</td></tr>";
        $final_htmla .= $final_html;
        if ( $even_count == 1 && $array_count > 0 )
        {
            $final_htmla .= '&nbsp;</td></tr>';
        }
        else {
            $final_htmla .= '</td></tr>';
        }
	}
	//If not an array of checkbox elements, add error message to HTML
	else
    {
        $final_htmla .= 'No Checkboxes Defined for Question #'.$question_id.'&nbsp;</td></tr>';
    }
    
    //$final_htmla .= no_answer_options ( $question_attributes_array, $form_answer['noan'][$question_id], 'radio_or_checkbox' );
      	
	if ($question_validation_error[$question_id])
    {
		$final_htmla .=  "<tr><td colspan=\"4\"><p
        id=\"error_style_".$question_id."\">".htmlentities($question_validation_error[$question_id])."</p></td></tr>";
	}
    $final_htmla .= "</table>";
	
	if ($include_td) {$final_htmla .= "</td>\n";}
    
	$final_html = $final_htmla;
    return $final_html;
}



function display_textarea_question ($question_attributes_array, $form_answer, $include_td = "")
{
	GLOBAL $question_validation_error, $textarea_ecma_added, $head_dynamic_ecma;
	//GLOBAL $form_answer;
	
	$question_id = $question_attributes_array["question_id"];
	if($include_td)	{$final_html .= "\n<td class=\"tdq\" id=\"QUEST_".$question_id."\"> ";}
	$final_html .= "\n&nbsp;".$question_attributes_array["question_title"]."&nbsp;&nbsp;
    <span class=\"tiny\" id=\"msg_".$question_id."\"></span><br/>";
	$final_html .=  "\n&nbsp;<textarea "; 
	$final_html .=  "name=\"form_answer[".$question_id."]\" id=\"form_answer[".$question_id."]\"";
	//$adjusted_size = $question_attributes_array["question_field_size"] * $question_attributes_array["question_display_size"];
	$final_html .=  "rows=\"".$question_attributes_array["question_field_size"]."\" ";
	$final_html .=  "cols=\"".$question_attributes_array["question_display_size"]."\" ";
	//$final_html .=  "maxlength=\"".$question_attributes_array["question_field_size"]."\" ";
	$final_html .=  "wrap=\"virtual\""; 
	$final_html .= build_tabindex_html($question_attributes_array['question_display_order']);
	
	//if($question_attributes_array["question_ecma"])	{	
	//	$final_html .=  $question_attributes_array["question_ecma"]."=\"go('".$question_id."')\" ";
	//}
	if ( $question_attributes_array["question_special_attribute"] )
    {
        $limit = $question_attributes_array["question_special_attribute"] + 0;
    }
    else
    {
        $limit = $question_attributes_array["question_field_size"] * $question_attributes_array["question_display_size"];
    }
    $final_html .=  "onkeyup=\"textarea_ck('".$question_id."', ".$limit.")\">";
    
    //If an answer to the question has been submitted, use the submitted value.  Otherwise default
	if(!$form_answer[$question_id])	{ 
		$final_html .=  $question_attributes_array["question_default_value"];
	}
		else {$final_html .= $form_answer[$question_id];}
	
	$final_html .=  "</textarea>\n";
    
    $final_html .= no_answer_options ( $question_attributes_array, $form_answer['noan'][$question_id] );
	
	if ($question_validation_error[$question_id])	{
		//echo "<p id=\"qid_2\">".$question_validation_error[$question_id]."</p>";
		$final_html .=  "<p id=\"error_style_".$question_id."\">
        <a name=\"bookmark\">".htmlentities($question_validation_error[$question_id])."</a></p>";
	}
	if($include_td)	{$final_html .= "</td>\n";}
	
    //If we have not added the generic formula that does client-side entry checking, add it now
    if ( !$textarea_ecma_added )
    {
        $head_dynamic_ecma[0] .= "
            function textarea_ck (question_id, char_limit)
            {
                quest_id = \"QUEST_\" + question_id;
                messagecell = \"msg_\" + question_id;
                frm_id = \"form_answer[\" + question_id + \"]\";
                count = document.getElementById(frm_id).value.length;
                
                if (count > char_limit)
                {
                      document.getElementById(quest_id).style.background =
                    \"#FF6666\";
                      document.getElementById(messagecell).style.color =
                    \"#FFFFCC\";
                      document.getElementById(frm_id).style.background =
                    \"#FFCCCC\";
                      overage = count - char_limit;
                      document.getElementById(messagecell).innerHTML =
                    \"Entry has reached size limit.\";
                	trimmed_entry = document.getElementById(frm_id).value.substring(0, char_limit);    
                	document.getElementById(frm_id).value = trimmed_entry;
                }
                if (count <= char_limit)
                {
                      document.getElementById(quest_id).style.background =
                    \"transparent\";
                      document.getElementById(messagecell).style.fontSize = \"80%\"
                    
                      document.getElementById(messagecell).style.color =
                    \"#000000\";
                      document.getElementById(frm_id).style.background =
                    \"#FFFFFF\";
                      underage = char_limit - count;
                      document.getElementById(messagecell).innerHTML =
                    \"Entry is \" + underage + \" characters below size limit.\";
                }
            }
       ";
        $textarea_ecma_added = 'yes';
    }
    
    return $final_html;
}



class Display_date_question
{
	var $question_validation_error;
	var $page_id; 
	//var $form_submitted;
    var $question_attributes_array;
    var $question_id;
    var $F; //month spelled out
    var $j; //date with not padding 0's, i.e., 9
    var $Y; //Year in four digits; 1994
    var $questions;
    var $question_elements;
    var $f_html;
    
    function Display_date_question ($question_attributes_array, $form_answer, $include_td = "")
	{
        GLOBAL $form_submitted;
        
        $this->question_id = $question_attributes_array["question_id"];
        $this->question_attributes_array = $question_attributes_array;
        $this->question_id = $question_attributes_array["question_id"];
		
        //echo "<p>Dispaly_date_question: qid:".$question_attributes_array["question_id"].'<br>val:'. $form_answer[$question_attributes_array["question_id"]] . "</p>";
        
		////Determine starting values
        //If there is a form answer, put it in
        if ( $form_answer[$this->question_id] )
        {
          //echo "first case".$form_answer[$this->question_id]."<p>";
          $unix_date_def = $form_answer[$this->question_id];
          //echo "fixed 1970error DATE:$unix_date_def";
          
          $this->F = safe_date("m", $unix_date_def);
          $this->m = safe_date("m", $unix_date_def);
          $this->j = safe_date("j", $unix_date_def);
          $this->Y = safe_date("Y", $unix_date_def);
        }
        elseif ( $form_submitted )  //bugbug: I don't know that this is what you want to use to determine if values persist, might want to always persist them...  $form_submitted is not a true/false but rather the type_id of the form and is not always set.
		{
          //echo "second case: ".$form_answer[$this->question_id]."<p>";
          //display_value ($form_answer);
          $this->F = $form_answer[$this->question_id."_month"];
          $this->m = $form_answer[$this->question_id."_month"];
          $this->j = $form_answer[$this->question_id."_day_of_month"];
          $this->Y = $form_answer[$this->question_id."_year"];
        }
        //If no form answer, look for a default answer (in unix time format), and convert it for use
        elseif ( $this->question_attributes_array["question_default_value"] )
        {
          //echo "third case".$form_answer[$this->question_id]."<p>";
          $unix_date_def = $this->question_attributes_array["question_default_value"];
          $this->F = safe_date("m", $unix_date_def);
          $this->m = safe_date("m", $unix_date_def);
          $this->j = safe_date("j", $unix_date_def); 
          $this->Y = safe_date("Y", $unix_date_def);
        }
        
        if ( $this->question_attributes_array["question_special_attribute"] )
        {
            $this->assemble_date_question($this->question_attributes_array["question_special_attribute"]);
        }
        else
        {
            $this->assemble_date_question ( 'FjY' );
        }
    
    
    }



    function assemble_date_question ($elements)
    {
        $length = strlen($elements);
        $this->f_html .= "<td>".$this->question_attributes_array["question_title"]."<br/>";
        $this->f_html .= "<table><tr>";
        for ($y = 0; $y < $length; $y++)
        {
            switch ($elements[$y])
            {
                case "F":
                    $this->load_month_question ();
                    $this->f_html .= display_dropdown_list_question ($this->questions["month"], $this->question_elements["month"], $this->F, "yes");
                    break;
                
                case "m":
                    $this->load_month_of_year_question ();
                    $this->f_html .= display_number_question ($this->questions["month"], "", "yes");
                    break;
                
                case "j":
                    $this->load_day_of_month_question ();
                    $this->f_html .= display_number_question ($this->questions["day_of_month"], "", "yes");
                    break;
                
                case "Y":
                    $this->load_year_question ();
                    $this->f_html .= display_number_question ($this->questions["year"], "", "yes");
                    break;    
                    
                    
                default:
                    echo "Illegal date definition";
            
            }
        }
        
        $this->f_html .= "</tr></table></td>";
            
    }
   
    
     
    function load_month_question  ()
    {
        $this->questions["month"] = array ("question_id" => $this->question_id."_month", "question_title" => "Month:", "question_type" => "dropdown", "question_owner_group_id" => "1", "question_owner_org_id" => "1",
		"question_field_size" => "0", "question_range_bottom" => "0", "question_range_top" => "0",
		"question_display_order" => $this->question_attributes_array["question_display_order"], "question_required" => $this->question_attributes_array["question_required"], "report_type_id" =>
		"client_identifier_entry", "question_default_value" => $this->F, "question_visible" => "yes", "question_ecma" => "");
        
		//$this->question_elements["month"]["select_month"] = array ("question_element_id" =>
		//"element_birth_month_begin", "question_id" => "month", "question_element_title" => date("F",
		//begin_unix), "question_element_value" => date("m", $begin_unix));
        $this->question_elements["month"]["BLANK"] = array ("question_element_id" => "element_birth_month_begin", "question_id" =>
        "month", "question_element_title" => "[SELECT MONTH]", "question_element_value" => "BLANK");
		$this->question_elements["month"]["1"] = array ("question_element_id" => "element_birth_month_begin", "question_id" => "month", "question_element_title" => "January", "question_element_value" => "01");
		$this->question_elements["month"]["2"] = array ("question_element_id" => "element_birth_month_begin", "question_id" => "month", "question_element_title" => "February", "question_element_value" => "02");
		$this->question_elements["month"]["3"] = array ("question_element_id" => "element_birth_month_begin", "question_id" => "month", "question_element_title" => "March", "question_element_value" => "03");
		$this->question_elements["month"]["4"] = array ("question_element_id" => "element_birth_month_begin", "question_id" => "month", "question_element_title" => "April", "question_element_value" => "04");
		$this->question_elements["month"]["5"] = array ("question_element_id" => "element_birth_month_begin", "question_id" => "month", "question_element_title" => "May", "question_element_value" => "05");
		$this->question_elements["month"]["6"] = array ("question_element_id" => "element_birth_month_begin", "question_id" => "month", "question_element_title" => "June", "question_element_value" => "06");
		$this->question_elements["month"]["7"] = array ("question_element_id" => "element_birth_month_begin", "question_id" => "month", "question_element_title" => "July", "question_element_value" => "07");
		$this->question_elements["month"]["8"] = array ("question_element_id" => "element_birth_month_begin", "question_id" => "month", "question_element_title" => "August", "question_element_value" => "08");
		$this->question_elements["month"]["9"] = array ("question_element_id" => "element_birth_month_begin", "question_id" => "month", "question_element_title" => "September", "question_element_value" => "09");
		$this->question_elements["month"]["10"] = array ("question_element_id" => "element_birth_month_begin", "question_id" => "month", "question_element_title" => "October", "question_element_value" => "10");
		$this->question_elements["month"]["11"] = array ("question_element_id" => "element_birth_month_begin", "question_id" => "month", "question_element_title" => "November", "question_element_value" => "11");
		$this->question_elements["month"]["12"] = array ("question_element_id" => "element_birth_month_begin", "question_id" => "month", "question_element_title" => "December", "question_element_value" => "12");    
    }


    function load_month_of_year_question  ()
    {
        $this->questions["month"] = array ("question_id" => $this->question_id."_month", "question_title" => "Month:",
		"question_type" => "number", "question_field_size" => "2", "question_range_bottom" => "1", "question_range_top" =>
		"12", "question_display_order" => $this->question_attributes_array["question_display_order"], 
        "question_required" => $this->question_attributes_array["question_required"], "question_default_value" => $this->m, 
        "question_visible" => "yes", "question_ecma" => "");
    }


    function load_day_of_month_question  ()
    {
        $this->questions["day_of_month"] = array ("question_id" => $this->question_id."_day_of_month", "question_title" => "Day:",
		        "question_type" => "number", "question_field_size" => "2", "question_range_bottom" => "1", "question_range_top" =>
		        "31", "question_display_order" => $this->question_attributes_array["question_display_order"], 
                "question_required" => $this->question_attributes_array["question_required"], "question_default_value" => $this->j, 
                "question_visible" => "yes", "question_ecma" => "");
    }


    function load_year_question  ()
    {
		$this->questions["year"] = array ("question_id" => $this->question_id."_year", "question_title" => "Year:",
		"question_type" => "number", "question_field_size" => "4", "question_range_bottom" => "1915", "question_range_top"
		=> "2038", "question_display_order" => $this->question_attributes_array["question_display_order"], "question_required" => $this->question_attributes_array["question_required"], "question_default_value" =>
		$this->Y, "question_visible" => "yes", "question_ecma" => "", "question_field_size" => "4");
    }


    
}



class Display_table {
        var $question_attributes_array;
        var $question_elements_array;
        var $form_answer;
        var $count_of_x_elements;
        var $count_of_y_elements;
        //var $y;
        var $question_id;
        var $final_html;
        var $x_total; //Consists of $x_total[axis_id]["subtotal"] and $x_total[axis_id]["total"]
        var $y_total;
        var $ecma_array_x;
        var $ecma_array_y;
        var $flag;
        var $sql;
    
    
    function Display_table ($question_attributes_array, $question_elements_array, 
                            $form_answer, $include_td = "", $query_display = "", $sql="")
    {
        GLOBAL $question_validation_error, $head_dynamic_ecma, $table_ecma_functions_inserted;
        
        $this->question_attributes_array = $question_attributes_array;
        $this->question_elements_array = $question_elements_array;
        $this->form_answer = $form_answer;
        $this->question_id = $question_attributes_array["question_id"];
        $this->query_display = $query_display;
		$this->sql = $sql;
               
        //How many columns (+1 for the axis label)
        $this->count_of_x_elements = count ( $question_elements_array["x"]) + 1;
        if($this->question_attributes_array["y_axis_title"])
        {
            $this->count_of_x_elements++;   
        }
		
        //How many rows 
        $this->count_of_y_elements = count ( $question_elements_array["y"]) + 1;
        
        if($include_td)	{$this->final_html .= "\n<td id=\"QUEST_".$question_id."\"> ";}
        
        //Create the x axis labels html
        $this->final_html .= "<table border=\"1\"><tr>
        <td colspan=\"".$this->count_of_x_elements."\">".$this->question_attributes_array["question_title"]."</td></tr>";
        
        //If there is a x axis title, output it
        if ( $this->question_attributes_array["x_axis_title"] )   {
               
                $skip_cols = $this->count_of_x_elements - 1;
                $this->final_html .= "<tr><td>&nbsp;</td>
                <td colspan=\"".$skip_cols."\">".$this->question_attributes_array["x_axis_title"]."</td></tr>";   
            
        }
        
        //display_value($this->question_attributes_array, "X values");
        
        $this->final_html .= "<tr>";
        
        //Make an empty cell if there is a Y axis title
        if ( $this->question_attributes_array["y_axis_title"] )   {
            $this->final_html .= "<td>&nbsp;</td>";
        }
        
        //Make the empty cell that is above the y axis labels
        $this->final_html .= "<td>&nbsp</td>";
        
        //Loop through the X axis labels
        if (is_array($this->question_elements_array["x"]) ){
            foreach($this->question_elements_array["x"] AS $current_x)    {

                if ($current_x["axis_special_type"] == "text_axis")    {
                    $x_axis_text = str_replace (" ", "<br/>", $current_x["axis_label"] );
                    $skip_rows = $this->count_of_y_elements;
                    $this->final_html .= "<td class=\"yaxistext\" rowspan=\"".$skip_rows."\">".$x_axis_text."</td>";

                } 

                //..else make the standard axis label, and appropriate ecma arrays           
                else {
                    $this->final_html .= "<td>".$current_x["axis_label"]."</td>";

                    //If it is a total or subtotal type x_axis, put in a label in array preceding the axis id number in the array        
                    if($current_x["axis_special_type"] == "total" || $current_x["axis_special_type"] == "subtotal") {
                        if($this->ecma_array_y) {$this->ecma_array_y .= ", ";}  

                        $this->ecma_array_y .= "\"".$current_x["axis_special_type"]."\"";    
                    }

                    //..adds the axis id number to the array
                    if($this->ecma_array_y) {$this->ecma_array_y .= ", ";}
                    $this->ecma_array_y .=  "\"".$current_x["axis_id"]."\"";
                }   

            }
        }
        $this->final_html .= "</tr>";
        
        //Loop through the y axis, creating html
        if ( is_array($this->question_elements_array["y"]) )    {
            foreach($this->question_elements_array["y"] AS $current_y)    {
                $this->final_html .= "<tr>";   

                //If there is a y axis label, create it
                if ( $this->question_attributes_array["y_axis_title"] )   {
                    if($this->flag["y_axis_label_already_created"] != "yes")    {
                        $skip_rows = $this->count_of_y_elements - 1;
                        $y_label_verticle = str_replace (" ", "<br/>", $this->question_attributes_array["y_axis_title"] );
                        $this->final_html .= "
                         <td class=\"yaxistext\" rowspan=\"".$skip_rows."\">".$y_label_verticle."</td>";
                         $this->flag["y_axis_label_already_created"] = "yes";
                    }   

                }


                //If a text axis questions, place the text label spanning the width of the table
                if ( $current_y["axis_special_type"] == "text_axis" )   {
                    $skip_cols = $this->count_of_x_elements;
                    $this->final_html .= "<td colspan=\"".$skip_cols."\">".$current_y["axis_label"]."</td>";   

                }
                else    {
                    $this->final_html .= "<td>".$current_y["axis_label"]."</td>";
                    $this->final_html .= $this->display_table_x_axis($current_y["axis_id"]);

                    //Build the ecma arrays used in the browser ecma script ORIGNALLY BELOW        
                    if($current_y["axis_special_type"] == "total" || $current_y["axis_special_type"] == "subtotal") {
                        if($this->ecma_array_x) {$this->ecma_array_x .= ", ";}  

                        $this->ecma_array_x .= "\"".$current_y["axis_special_type"]."\"";    
                    }

                    if($this->ecma_array_x) {$this->ecma_array_x .= ", ";}
                    $this->ecma_array_x .=  "\"".$current_y["axis_id"]."\""; 


                }

                $this->final_html .= "</tr>";

                /*
                //Build the ecma arrays used in the browser ecma script        
                if($current_y["axis_special_type"] == "total" || $current_y["axis_special_type"] == "subtotal") {
                    if($this->ecma_array_x) {$this->ecma_array_x .= ", ";}  

                    $this->ecma_array_x .= "\"".$current_y["axis_special_type"]."\"";    
                }

                if($this->ecma_array_x) {$this->ecma_array_x .= ", ";}
                $this->ecma_array_x .=  "\"".$current_y["axis_id"]."\"";
                */        



            }
        }
        
        if ($question_validation_error[$this->question_id])	{
		    $this->final_html .=  
            "<tr><td id=\"QUEST_".$this->question_id."\" colspan=\"".
            $this->count_of_y_elements."\">
            <span id=\"error_style_".$this->question_id."\">".
            htmlentities($question_validation_error[$this->question_id])."</span></td></tr>";
		}
    	
		//Instructions for people querying a table question
		if ( $query_display )	{
			$this->final_html .=  
            "<tr><td colspan=\"".
            $this->count_of_y_elements."\">
			Each axis intersection above contains two query options.  In the first box, enter the \"Greater Than\" value, and in the
			second enter the \"Less Than\" value.";
            
			
			$this->final_html .= "</td></tr>";
		}
	
        $this->final_html .= "</table>";
        
        //If this is not a query
        if ( !$query_display )   {
            //Insert the ecma table functions if they have not already been inserted,
            if($table_ecma_functions_inserted != "yes") {
                $head_dynamic_ecma["0"] .= $this->table_function_ecma_text();
                $head_dynamic_ecma["0"] .= "
                var x_array = new Array();
                var y_array = new Array();";

                $table_ecma_functions_inserted = "yes";
            }
            $head_dynamic_ecma["0"] .= "x_array['".$this->question_id."'] = new Array (".$this->ecma_array_x.");";
            $head_dynamic_ecma["0"] .= "y_array['".$this->question_id."'] = new Array (".$this->ecma_array_y.");";
        }
        //echo $head_dynamic_ecma["0"]."<br/>";
        //echo "ECMA X is: ".$this->ecma_array_x."<br/>";
        //echo "ECMA Y is: ".$this->ecma_array_y."<br/>";
        if($include_td)	{$this->final_html .= "</td>\n";}
        return $this->final_html;
    }                                                                                        
    
    function display_table_x_axis ($y_axis_id){

        
        foreach($this->question_elements_array["x"] AS $current_x)    {
        
            
            if ($current_x["axis_special_type"] == "text_axis")    {
                //Do nothing, since this cell was rowspanned while contructing x axis labels above           
            } 
          
            elseif ($current_x["axis_special_type"] == "total" || $current_x["axis_special_type"] == "subtotal")  {
                //Test if this is a query
				if ( $this->query_display ) {
					$this->display_table_query_cell ($current_x["axis_id"], $y_axis_id );
				}
                else {
					$this->display_table_total_cell ($current_x["question_id"], $current_x["axis_id"], 
                 	 $y_axis_id, "x", $current_x["axis_special_type"]);
				}
            }    
            
            elseif ($this->question_elements_array["y"][$y_axis_id]["axis_special_type"] == "total" 
            || $this->question_elements_array["y"][$y_axis_id]["axis_special_type"] == "subtotal")   {
                
                //Test if this is a query
				if ( $this->query_display ) {
					$this->display_table_query_cell ($current_x["axis_id"], $y_axis_id);
				}
				else {
				    //$this->ecma_array_x .= "\"".$this->question_elements_array["y"][$y_axis_id]["axis_special_type"]."\", ";
                    $this->display_table_total_cell ($current_x["question_id"], $current_x["axis_id"], $y_axis_id, "y",
                     $this->question_elements_array["y"][$y_axis_id]["axis_special_type"]);
				 
				}     
            }
            
			//If we are querying the table question...
			elseif ( $this->query_display )	{
				$this->display_table_query_cell ($current_x["axis_id"], $y_axis_id);
			    //$this->display_table_average_cell ($current_x["axis_id"], $y_axis_id, $this->sql );
			
			}
            elseif ( $this->sql )	{
				$this->display_table_average_cell ($current_x["axis_id"], $y_axis_id, $this->sql );
			
			}
			                        
            else    {
                $this->display_table_number_cell ($current_x["axis_id"], $y_axis_id);   
            
            }
        }

    }


    function display_table_number_cell ($x_axis_id, $y_axis_id)    {
    
        $this->final_html .=  "<td class=\"tblnum\">";
	    $this->final_html .=  "\n<input class=\"tblnum\" type=\"text\" "; 
	    $this->final_html .=  "name=\"form_answer[".$this->question_id."][".$x_axis_id."][".$y_axis_id."]\"";
	    $this->final_html .=  "id=\"form_answer['".$this->question_id."'][".$x_axis_id."][".$y_axis_id."]\"";
        $this->final_html .=  "onChange=\"table_total('".$this->question_id."', ".$x_axis_id.", ".$y_axis_id.")\"";
        if($this->question_attributes_array["question_display_size"])	{
		    $this->final_html .=  "size=\"".$this->question_attributes_array["question_display_size"]."\" ";
		}
	    elseif ($this->question_attributes_array["question_field_size"])	{
		    $this->final_html .=  "size=\"".$this->question_attributes_array["question_field_size"]."\" ";	
		}
        else {
            $this->final_html .=  "size=\"3\" "; 
        }	

	    if($this->question_attributes_array["question_ecma"])	{	
		    $this->final_html .=  $this->question_attributes_array["question_ecma"]."=\"go('".
            $this->question_id."')\" ";
		}
	    if($this->question_attributes_array["question_field_size"]) {
            $this->final_html .=  "maxlength=\"".$this->question_attributes_array["question_field_size"]."\" ";
        }
        else {
            $this->final_html .=  "maxlength=\"3\"";
        }
	    $this->final_html .= "value=\"".$this->form_answer[$this->question_id][$x_axis_id][$y_axis_id]."\" ";
	    $this->final_html .= build_tabindex_html($question_attributes_array['question_display_order']).">";
        $this->final_html .= "</td>";

        //Add to the totals
        $this->x_total[$x_axis_id]["subtotal"] += $this->form_answer[$this->question_id][$x_axis_id][$y_axis_id];
        //echo "x:".$x_axis_id." subtotal is: ".$this->x_total[$x_axis_id]["subtotal"]."<br/>"; 
        $this->x_total[$x_axis_id]["total"] += $this->form_answer[$this->question_id][$x_axis_id][$y_axis_id];
        //echo "x:".$x_axis_id." total is: ".$this->x_total[$x_axis_id]["total"]."<br/>";
        $this->y_total[$y_axis_id]["subtotal"] += $this->form_answer[$this->question_id][$x_axis_id][$y_axis_id]; 
        $this->y_total[$y_axis_id]["total"] += $this->form_answer[$this->question_id][$x_axis_id][$y_axis_id];
        //echo "y:".$y_axis_id." total is: ".$this->y_total[$y_axis_id]["total"]."<br/>";
    
    }


    function display_table_total_cell ($question_id, $x_axis_id, $y_axis_id, $display_x_or_y_total, $subtotal_or_total)   {
        $this->final_html .= "<td class=\"tblnum\" id=\"total['".$this->question_id."'][".$x_axis_id."][".$y_axis_id."]\">";
        
        //echo $display_x_or_y_total;
        
        if ($display_x_or_y_total == "x")   {
            
          //$this->final_html .= "xHere";
            
            if ($subtotal_or_total == "subtotal")    {
                $cell_num = $this->y_total[$y_axis_id]["subtotal"];
                            
            }
            
            else {
                //echo "In the X else<br/>";
                
                //echo "Total of x_axis_id: ".$y_axis_id." is: ".$this->y_total[$y_axis_id]["total"]."<br/>";
                $cell_num = $this->y_total[$y_axis_id]["total"];
                
            }
                        
        }
        
        else {
            //$this->final_html .= "YHere";
            
            if ($subtotal_or_total == "subtotal")    {
                $cell_num = $this->x_total[$x_axis_id]["subtotal"];
                //$x_total[$x_axis_id]["subtotal"]; Add the total line??   
                $this->x_total[$x_axis_id]["subtotal"] = 0;
            
            }
            
            else {
                $cell_num = $this->x_total[$x_axis_id]["total"];
                $this->y_total[$y_axis_id]["total"] += $this->x_total[$x_axis_id]["total"];
            }
     
        }
        $cell_num += 0;
        $this->final_html .= $cell_num;
        $this->final_html .= "</td>"; 

    }




	function display_table_query_cell ($x_axis_id, $y_axis_id)    {

		//Range bottom
		$this->final_html .=  "<td class=\"tblnum\">";
		$this->final_html .=  "\n<input class=\"tblnum\" type=\"text\" "; 
	    $this->final_html .=  "name=\"form_answer[".$this->question_id."][".$x_axis_id."][".$y_axis_id."][range_bottom]\"";
	    $this->final_html .=  "id=\"form_answer['".$this->question_id."'][".$x_axis_id."][".$y_axis_id."]\"";
        //$this->final_html .=  "onChange=\"table_total(".$this->question_id.", ".$x_axis_id.", ".$y_axis_id.")\"";
        if($this->question_attributes_array["question_display_size"])	{
		    $this->final_html .=  "size=\"".$this->question_attributes_array["question_display_size"]."\" ";
		}
	    elseif ($this->question_attributes_array["question_field_size"])	{
		    $this->final_html .=  "size=\"".$this->question_attributes_array["question_field_size"]."\" ";	
		}
        else {
            $this->final_html .=  "size=\"3\" "; 
        }	

	    if($this->question_attributes_array["question_field_size"]) {
            $this->final_html .=  "maxlength=\"".$this->question_attributes_array["question_field_size"]."\" ";
        }
        else {
            $this->final_html .=  "maxlength=\"3\"";
        }
	    //echo $this->question_id."-".$x_axis_id."-".$y_axis_id ." = ".
		//$this->form_answer[$this->question_id][$x_axis_id][$y_axis_id]["range_bottom"]."<br>";
		$this->final_html .= "value=\"".$this->form_answer[$this->question_id][$x_axis_id][$y_axis_id]["range_bottom"]."\" ";
	    $this->final_html .= build_tabindex_html($question_attributes_array['question_display_order']).">";
		
		
		//Range top
		$this->final_html .=  "\n<input class=\"tblnum\" type=\"text\" "; 
	    $this->final_html .=  "name=\"form_answer[".$this->question_id."][".$x_axis_id."][".$y_axis_id."][range_top]\"";
	    $this->final_html .=  "id=\"form_answer['".$this->question_id."'][".$x_axis_id."][".$y_axis_id."]\"";
        //$this->final_html .=  "onChange=\"table_total(".$this->question_id.", ".$x_axis_id.", ".$y_axis_id.")\"";
        if($this->question_attributes_array["question_display_size"])	{
		    $this->final_html .=  "size=\"".$this->question_attributes_array["question_display_size"]."\" ";
		}
	    elseif ($this->question_attributes_array["question_field_size"])	{
		    $this->final_html .=  "size=\"".$this->question_attributes_array["question_field_size"]."\" ";	
		}
        else {
            $this->final_html .=  "size=\"3\" "; 
        }	

	    //if($this->question_attributes_array["question_ecma"])	{	
		//    $this->final_html .=  $this->question_attributes_array["question_ecma"]."=\"go('".
        //    $this->question_id."')\" ";
		//}
	    if($this->question_attributes_array["question_field_size"]) {
            $this->final_html .=  "maxlength=\"".$this->question_attributes_array["question_field_size"]."\" ";
        }
        else {
            $this->final_html .=  "maxlength=\"3\"";
        }
	    $this->final_html .= "value=\"".$this->form_answer[$this->question_id][$x_axis_id][$y_axis_id]["range_top"]."\" ";
	    $this->final_html .= build_tabindex_html($question_attributes_array['question_display_order']).">";
		
		
		
        $this->final_html .= "</td>";


	}



	function display_table_average_cell ($x_axis_id, $y_axis_id, $query_sql)    {

		//Range bottom
		$this->final_html .=  "<td class=\"tblnum\">";
		//$this->final_html .=  "\n<input class=\"tblnum\" type=\"text\" "; 
	    //$this->final_html .=  "name=\"form_answer[".$this->question_id."][".$x_axis_id."][".$y_axis_id."][range_bottom]\"";
	    //$this->final_html .=  "id=\"form_answer[".$this->question_id."][".$x_axis_id."][".$y_axis_id."]\"";
        //$this->final_html .=  "onChange=\"table_total(".$this->question_id.", ".$x_axis_id.", ".$y_axis_id.")\"";
        //if($this->question_attributes_array["question_display_size"])	{
		//    $this->final_html .=  "size=\"".$this->question_attributes_array["question_display_size"]."\" ";
		//}
	    //elseif ($this->question_attributes_array["question_field_size"])	{
		//    $this->final_html .=  "size=\"".$this->question_attributes_array["question_field_size"]."\" ";	
		//}
        //else {
        //    $this->final_html .=  "size=\"3\" "; 
       // }

	    //if($this->question_attributes_array["question_field_size"]) {
        //    $this->final_html .=  "maxlength=\"".$this->question_attributes_array["question_field_size"]."\" ";
        //}
        //else {
        //    $this->final_html .=  "maxlength=\"3\"";
       // }
	    //echo $this->question_id."-".$x_axis_id."-".$y_axis_id ." = ".
		//$this->form_answer[$this->question_id][$x_axis_id][$y_axis_id]["range_bottom"]."<br>";
		//$this->final_html .= "value=\"".$this->form_answer[$this->question_id][$x_axis_id][$y_axis_id]["range_bottom"]."\" ";
	    //$this->final_html .= "tabindex=\"".$this->question_attributes_array["question_display_order"]."\">";
		
		/*
		//Range top
		$this->final_html .=  "\n<input class=\"tblnum\" type=\"text\" "; 
	    $this->final_html .=  "name=\"form_answer[".$this->question_id."][".$x_axis_id."][".$y_axis_id."][range_top]\"";
	    $this->final_html .=  "id=\"form_answer[".$this->question_id."][".$x_axis_id."][".$y_axis_id."]\"";
        //$this->final_html .=  "onChange=\"table_total(".$this->question_id.", ".$x_axis_id.", ".$y_axis_id.")\"";
        if($this->question_attributes_array["question_display_size"])	{
		    $this->final_html .=  "size=\"".$this->question_attributes_array["question_display_size"]."\" ";
		}
	    elseif ($this->question_attributes_array["question_field_size"])	{
		    $this->final_html .=  "size=\"".$this->question_attributes_array["question_field_size"]."\" ";	
		}
        else {
            $this->final_html .=  "size=\"3\" "; 
        }	

	    //if($this->question_attributes_array["question_ecma"])	{	
		//    $this->final_html .=  $this->question_attributes_array["question_ecma"]."=\"go('".
        //    $this->question_id."')\" ";
		//}
	    if($this->question_attributes_array["question_field_size"]) {
            $this->final_html .=  "maxlength=\"".$this->question_attributes_array["question_field_size"]."\" ";
        }
        else {
            $this->final_html .=  "maxlength=\"3\"";
        }
	    $this->final_html .= "value=\"".$this->form_answer[$this->question_id][$x_axis_id][$y_axis_id]["range_top"]."\" ";
	    $this->final_html .= "tabindex=\"".$this->question_attributes_array["question_display_order"]."\">";
		*/
		//$begin_sql = "SELECT AVG(client_rpt_answer_int) FROM client_prt_answers ";
        $quest_specify_sql = "AND client_rpt_answers.axis_x = '".$x_axis_id."' AND client_rpt_answers.axis_y = '".$y_axis_id."' ";
        $sql = $begin_sql.$query_sql.$quest_specify_sql;
		$result = run_query ($sql, "Run query to find average");
        $num = fetch_result ( $result, "Pulling average in table" );
        if ( !$num )    {
            $num = "&nbsp";
        }
        else {
            $num = number_format ($num, 1);
        }
        $this->final_html .= $num."</td>";


	}


    
    function table_function_ecma_text ()    {
    
        return "
        
function table_total(table_id, x, y) {
	
	var total_cell;
	var xsubtotal = 0;
	var xtotal = 0;
	
	var subtotal = 0;
	var total = 0;
	var array_increment = 0;
	var examine_cell;
	
	while (x_array[table_id][array_increment]){
	
		switch (x_array[table_id][array_increment]) {

			case \"subtotal\":
				total_cell = \"total['\" + table_id + \"'][\" + x + \"][\" + x_array[table_id][array_increment + 1 ] + \"]\";
				document.getElementById(total_cell).innerHTML = xsubtotal;
				
				xsubtotal = 0;

				row_totaler (table_id, x_array[table_id][array_increment + 1 ]);
				array_increment++;
				break;
		
			case \"total\":

				total_cell = \"total['\" + table_id + \"'][\" + x + \"][\" + x_array[table_id][array_increment + 1 ] + \"]\";
				document.getElementById(total_cell).innerHTML = xtotal;
				row_totaler (table_id, x_array[table_id][array_increment + 1 ]);
				array_increment++;
				break;
	
			default:	
				examine_cell = \"form_answer['\" + table_id + \"'][\" + x + \"][\" + x_array[table_id][array_increment] + \"]\";
				//alert(examine_cell);				
				var w = document.getElementById(examine_cell).value;
				if (document.getElementById(examine_cell).value)	{
					xsubtotal += parseFloat(w);
					xtotal += parseFloat(w);
				}
				break;
		}
		array_increment++;		
	}

	array_increment = 0;
	
	while (y_array[table_id][array_increment]){
		
		switch (y_array[table_id][array_increment]) {

			case \"subtotal\":
				total_cell = \"total['\" + table_id + \"'][\" + y_array[table_id][array_increment + 1 ] + \"][\" + y + \"]\";
				document.getElementById(total_cell).innerHTML = subtotal;
				
				subtotal = 0;
				array_increment++;
				break;
			
			case \"total\":

				total_cell = \"total['\" + table_id + \"'][\" + y_array[table_id][array_increment + 1 ] + \"][\" + y + \"]\";
				//alert(total_cell);
				document.getElementById(total_cell).innerHTML = total;
				array_increment++;
				break;

			default:	
				examine_cell = \"form_answer['\" + table_id + \"'][\" + y_array[table_id][array_increment] + \"][\" + y + \"]\";

				
				if (document.getElementById(examine_cell).value)	{
				subtotal = subtotal + parseFloat(document.getElementById(examine_cell).value);
				total = total + parseFloat(document.getElementById(examine_cell).value);
				
				}
				break;
		}
		array_increment++;	
	}

}


function row_totaler (table_id, x)	{
	var row_increment = 0;
	var row_total = 0;
	var row_subtotal = 0;	

	while (y_array[table_id][row_increment])	{

		switch (y_array[table_id][row_increment])	{

			case \"subtotal\":
				total_cell = \"total['\" + table_id + \"'][\" + y_array[table_id][row_increment + 1 ] + \"][\" + x + \"]\";
				//alert(\"SUBTOTAL: \" + total_cell);
				document.getElementById(total_cell).innerHTML = row_subtotal;
				
				row_subtotal = 0;
				row_increment++;
				break;
			
			case \"total\":

				total_cell = \"total['\" + table_id + \"'][\" + y_array[table_id][row_increment + 1 ] + \"][\" + x + \"]\";
				//alert(\"Total\" + total_cell);
				document.getElementById(total_cell).innerHTML = row_total;
				row_increment++;
				break;

			default:	
				
				examine_cell = \"total['\" + table_id + \"'][\" + y_array[table_id][row_increment] + \"][\" + x + \"]\";
				//alert(\"Add to grand:\" + examine_cell + \"row_subtotal\");				
				if (document.getElementById(examine_cell).innerHTML)	{
					row_subtotal = row_subtotal + parseFloat(document.getElementById(examine_cell).innerHTML);
					row_total = row_total + parseFloat(document.getElementById(examine_cell).innerHTML);
				}
				break;	
		}
		row_increment++;	
	}
}
        
        ";
    
    
    }
    
}



function display_hidden_form_values	($variable_name, $value)
{	
	$value = htmlentities($value);
	$value = addslashes($value);

	$final_html .= "\n<input type=\"hidden\" name=\"".$variable_name."\" value=\"".$value."\">\n";

	return $final_html;
}



//no_answer_options ( $question_attributes_array, $form_answer['noan'][$question_id] )
function no_answer_options ( $question_attributes, $no_answer_options_answer, $radio_or_checkboxes='' )
{
    if ( $radio_or_checkboxes ) {
        $ht .= "<tr><td colspan=\"4\">";
    }
    
    $ht .= "<br/><span class=\"noans\">";

    //Bitwise check to figure out question_no_answer_options
    if ( $question_attributes['question_no_answer_options'] & 1 ) {
        $ht .= no_answer_options_button ( $question_attributes, "Don't know", '#88#', $no_answer_options_answer );
    }
    if ( $question_attributes['question_no_answer_options'] & 2 ) {
        $ht .= no_answer_options_button ( $question_attributes, 'Not applicable', '#98#', $no_answer_options_answer );
    }
    if ( $question_attributes['question_no_answer_options'] & 4 ) {
        $ht .= no_answer_options_button ( $question_attributes, 'Refused', '#99#', $no_answer_options_answer );
    }
    if ( $question_attributes['question_no_answer_options'] & 8 ) {
        $ht .= no_answer_options_button ( $question_attributes, "Don't have", '#77#', $no_answer_options_answer );
    }

    $ht .= '</span>';
    
    if ( $radio_or_checkboxes )
	{
        $ht .= '</td></tr>';
    }
    return $ht;

}



function no_answer_options_button ( $question_attributes, $title, $value, $answer )  {

    //if ( $question_attributes['question_type'] == 'radio' )  {
    //    $q_type = "\"radio\"";
    //    $q_name =  "\"form_answer[".$question_attributes['question_id']."]\"";   
    //}
    //else {
        $q_type = "\"checkbox\"";
        $q_name =  "\"form_answer[noan][".$question_attributes['question_id']."]\"";    
    //}
    
    $final_html .= '&nbsp;';
    //$final_html .= "<span class=\"noans\">".$title.'</span><input type='.$q_type.' name='.$q_name." value=\"".$value."\" ";
    $final_html .= $title.'<input type='.$q_type.' name='.$q_name." value=\"".$value."\" ";  
			
    //echo "Key is: ".$key." ; Form answer is: ".$form_answer[$question_id]."Quest element value is: ".$current_element["question_element_value"]."<br/>";

    if ( $answer == $value )
    {
        $final_html .= "checked=\"checked\" ";
    }
    
    //Test asked we change the tabindex order so user tabs thru no answer checkboxes...  Tedd prefers this be last.  Both options are now possible.
    $final_html .= build_tabindex_html(19100,1).">";
    
    return $final_html;
}


///////////////////////////////////////////////////////////////////////////////////
// FUNCTION: question_error_marking()                                            //
///////////////////////////////////////////////////////////////////////////////////
function question_error_marking ( $question_id, $error_message_question, $error_message_head='' )
{
	GLOBAL $question_validation_error, $head_dynamic_style, $message, $message_type, $just_adding_empty_client_id_entry_forms_so_no_errors_please;
    
		//$question_id += 0;
        //echo $error_message_question."  ".$question_id."<br/>";
    
		if (!$just_adding_empty_client_id_entry_forms_so_no_errors_please)
        {
			$message .= $error_message_head;
		    $question_validation_error[$question_id] .= $error_message_question;
			$head_dynamic_style .= "\n#error_style_".$question_id." {font-style: normal; color: #FFCC99} #QUEST_".$question_id." {background-color: #FF6666} ";
		}
}



/********************************************************************************************
 * Name: secondary_form_answer_names_validation: 
 * Description: Do a little extra validation on name fields.
 *
 * Details and Notes: There could be a need for further validation on name fields but 
 * currently we just test for double dash, space, or apostraphe.
 * 
 * @param: $element_title - string - Part of possible err message displayed back in form.
 * @param: $question_id - string - index into questions array used by form.
 * @param: $form_answer_edit - string - data posted to form
 * @return: Nothing returned
 * 
 * History: Created 12/17/03 by Jeff.
 ********************************************************************************************/
function secondary_form_answer_names_validation($element_title, $question_id, $form_answer_edit)
{
		//Special validation for names, i.e. name_first$i, name_middle$i, and name_last$i.
		if(ereg("(--)|(  )|('')", $form_answer_edit))
		{
		     question_error_marking ($question_id, "Illegal characters in ".$element_title.".  Note that two dashes, apostraphes, or spaces in a row are not allowed in a name.");
		}
}



/********************************************************************************************
 * Name: secondary_form_answer_validation: 
 * Description: Validate data field posted back to form.
 *
 * Details and Notes: Makes calls to do additional validation based on $question_class.
 * Todo: Question class should be an enumeration and member of a questions or validation
 * class.
 * 
 * @param: $element_title - string - Part of possible err message displayed back in form.
 * @param: $question_id - string - index into questions array used by form.
 * @param: $form_answer_edit - string - data posted to form
 * @param: $min_field_size - int - min expected length of string
 * @param: $max_field_size - int - max expected length of string
 * @param: $range_bottom - int
 * @param: $range_top - int
 * @param: $question_class - string - calls out to additional validation if 'name'.
 * @return: Nothing returned
 * 
 * History: Edited 12/17/03 by Jeff.  Added additional validation for names.
 * 
 *******************************************************************************************/
function secondary_form_answer_validation ($element_title, $question_id, $form_answer_edit, $min_field_size="", $ereg="", $max_field_size="", $range_bottom="", $range_top="", $question_class="")
{
	GLOBAL $question_validation_error;
    //echo 'Q id is: '.$question_id.' -- Form answer is: '.$form_answer_edit.'<p>';
    //echo "ereg: ".$ereg."<br>";
	if($ereg)
    {
		if(ereg("[^".$ereg."]", $form_answer_edit))
        {
			question_error_marking ($question_id, "Illegal characters in ".$element_title.". ");
			$form_answer_edit = addslashes($form_answer_edit);
		}
	}
	//perform further validation on names.
	if($question_class == "name")
	{
		secondary_form_answer_names_validation($element_title, $question_id, $form_answer_edit);
	}
	
	if($min_field_size)
    {
		if(strlen ($form_answer_edit) < $min_field_size)
        {
			question_error_marking ($question_id, $element_title." must be at least ".$min_field_size." characters long. ");
			$form_answer_edit = "";
		}
	}
	
	$form_answer_edit = trim($form_answer_edit);
	$form_answer_edit = htmlentities($form_answer_edit);
	
	if($max_field_size)
    {
		if(strlen ($form_answer_edit) > $max_field_size)
        {
			$adjusted_length = $max_field_size - 1;
			
			question_error_marking ($question_id, $element_title." cannot be longer than ".$max_field_size."Answer too Long. Overflow cut off. ");
			
			$form_answer_edit = substr ($form_answer_edit, 0, $adjusted_length);
		}
	}
	
	if($range_bottom)
    {
		if($form_answer_edit < $range_bottom)
        {
			question_error_marking ($question_id, $element_title." cannot be less than ".$range_bottom." . ");
		}
	}
    
	if($range_top)
    {
		if($form_answer_edit > $range_top)
        {
			question_error_marking ($question_id, $element_title." cannot be greater than ".$range_top." . ");
		}
	}
	
	
	return 	$form_answer_edit;
}


///////////////////////////////////////////////////////////////////////////////////
// FUNCTION: dynamic_field_marking()                                             //
///////////////////////////////////////////////////////////////////////////////////
function dynamic_field_marking ($dynamic_field_id, $dynamic_field_message="", $message_dynamic_head="", $dynamic_message_type, $a)
{
	GLOBAL $dynamic_field, $head_dynamic_style, $message, $message_type, $tag_values;
	
    //$a is vetted_form_answers
    
    //Make sure the field id is a number
    $dynamic_field_id += 0;
    //$dynamic_field_message[$dynamic_field_id] .= $dynamic_field_message;
    //$message .= htmlentities($message_dynamic_head);
	
	switch($dynamic_message_type)
    {
		case "question_error":
			question_error_marking ($dynamic_field_id, $dynamic_field_message, $message_dynamic_head);
			break;
	    
		case "message_math_error":
			$head_dynamic_style .= " #dynamic_field_message_style".$dynamic_field_id." {font-style: normal; color: #FFCC99} #dynamic_field_td_style_".$dynamic_field_id." {background-color: #FF6666} ";
			
			if ( preg_match ( "/[^\]\[\/\+\-\)\(\$\ 0-9a]/", $dynamic_field_message ) )	{
				echo "Illegal Characters in dymanic math message ".$dynamic_field_id."<br/>";
			}
			else
            {
				$field_name = "{DYNAMIC_".$dynamic_field_id."}";
				$dynamic_field_message = "\$temp_val = ".$dynamic_field_message.";";
				eval ($dynamic_field_message);
				$tag_values[$field_name] =  $temp_val;
			}
			break;
        
		default:
			echo "Dynamic message type not defined ".$dynamic_field_id."<br/>";
    }
}



function question_display ($current_question, $question_elements, $form_answer, $display_for_edit_forms_admin = '')
{
    $final_html = '';
    
    if ($current_question)
    {
		//Route the question to an HTML creation function based on what type it is, i.e., "text"
		switch ($current_question["question_type"])
        {
			case 'text':
				$final_html = display_text_question ( $current_question, $form_answer, 'yes_td', false, $display_for_edit_forms_admin );
				break;

			case 'number':

				$final_html = display_number_question ( $current_question, $form_answer, 'yes_td' );

				break;

			case 'radio':

				$final_html = display_radio_buttons_question ( $current_question, $question_elements, $form_answer, 'yes_td' );

				break;

			case 'checkboxes':

				$final_html = display_checkboxes_question ( $current_question, $question_elements, $form_answer, 'yes_td' );

				break;

			case 'dropdown':
                //echo 'question_display(current_question)'.pretty_var_dump($current_question);
				$final_html = display_dropdown_list_question ( $current_question, $question_elements, $form_answer, 'yes_td' );

				break;

			case 'textarea':

				$final_html = display_textarea_question ( $current_question, $form_answer, 'yes_td' );

				break;

            case 'table':

				$make_html = new Display_table ( $current_question, $question_elements, $form_answer, 'yes_td' );
				$final_html = $make_html->final_html;

				break;

            case 'date':

				$make_html = new Display_date_question ( $current_question, $form_answer, 'yes_td' );
				$final_html = $make_html->f_html;

				break;

			default:
				echo "Question ##".$current_question['question_id'].'Not Found In Question Database, or question has not type
                defined<br/>';
                //var_dump($current_question);
	    }
    }
    
	return $final_html;
}


?>
Return current item: Homeless Mangement Information System