<?php

/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd 
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	require('includes/application_top.php');
  
  // get the type of action
	$action=isset($HTTP_POST_VARS["post_action"])?$HTTP_POST_VARS["post_action"]:'';
	
	$service_id=isset($HTTP_POST_VARS["select_id"])?(int)$HTTP_POST_VARS["select_id"]:0;

	$survey_type=isset($HTTP_POST_VARS["survey_type"])?$HTTP_POST_VARS["survey_type"]:'RV';
	$only_content = (isset($HTTP_GET_VARS['only_content']) ? $HTTP_GET_VARS['only_content'] : 'N');
		
	$tx_report_from=isset($HTTP_POST_VARS["survey_report_from"])?$HTTP_POST_VARS["survey_report_from"]:'';
	$tx_report_to=isset($HTTP_POST_VARS["survey_report_to"])?$HTTP_POST_VARS["survey_report_to"]:'';
		
	// build the additional options to query
	$add_option="";
	if ($tx_report_from!="") $add_option=" and suf.survey_flag_date>='" . $tx_report_from . "' ";
	if ($tx_report_to!="") $add_option.=" and suf.survey_flag_date<='" . $tx_report_to . "' ";

	$counter = 0;
	$question1_id=isset($HTTP_POST_VARS["question1_id"])?(int)$HTTP_POST_VARS["question1_id"]:0;
	$question2_id=isset($HTTP_POST_VARS["question2_id"])?(int)$HTTP_POST_VARS["question2_id"]:0;
	$choice1_id=isset($HTTP_POST_VARS["choice1_id"])?(int)$HTTP_POST_VARS["choice1_id"]:0;
	$choice2_id=isset($HTTP_POST_VARS["choice2_id"])?(int)$HTTP_POST_VARS["choice2_id"]:0;

	if ($action=="report" && $question1_id>0) {
	
		if($question1_id!=$question2_id){
		   
			$result_query = tep_db_query("SELECT suf.survey_flag_id FROM " . TABLE_SERVICES_SURVEYS_ANSWER  . " sua, " . TABLE_SERVICES_SURVEYS_FLAG . " suf WHERE choice_id = '". $choice1_id . "' and suf.survey_flag_id = sua.survey_flag_id " . $add_option);
			
			while($result = tep_db_fetch_array ($result_query)){
			   

				$count_relation = tep_db_query("SELECT * FROM " . TABLE_SERVICES_SURVEYS_ANSWER . " WHERE survey_flag_id = ". $result["survey_flag_id"]. " and choice_id = '". $choice2_id . "'");
				
				$matches = tep_db_fetch_array($count_relation);
				
				if($matches != NULL)
					$counter += 1;	

			}
		}
	} 
	$survey_found=false;
	$question_found=false;
	
	$survey_detail_query=tep_db_query("SELECT survey_name,survey_id from " . TABLE_SERVICES_SURVEYS . " where service_id='" . $service_id . "' and survey_type='" . $survey_type . "'");
	if (tep_db_num_rows($survey_detail_query)>0) {
		$survey_found=true;
		$survey_detail_result=tep_db_fetch_array($survey_detail_query);
		
		$question_query=tep_db_query("SELECT question_id,question_text,question_type from " . TABLE_SERVICES_SURVEYS_QUESTION . " where survey_id='" . $survey_detail_result["survey_id"] . "' and (question_type<4) order by question_id");
		if (tep_db_num_rows($question_query)){
			$question_found=true;
		}
	}
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="SetFocus();">
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php if ($only_content!='Y') require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
	<tr>
		<?php if ($only_content!='Y') { ?> 
		<?php } ?>
		<!-- body_text //-->
		<td width="100%" valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
		<?php if ($action=='screen') { ?>
			<tr>
				<td class="pageHeading"><?php echo sprintf(HEADING_TITLE_RESULTS,$survey_detail_result['survey_name']) ; ?></td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '10'); ?></td></tr>
		<?php
				//get the survey names
				$question_array=array(array('id'=>0,'text'=>TEXT_SELECT_QUESTION));
				$choice_array=array(array('id'=>0,'text'=>TEXT_SELECT_CHOICE));
			if ($survey_found) {
				if ($question_found){
			?>
			<script language="JavaScript">
				var choices=new Array();
				choices[0]=Array();
				choices[0][0]=0;
				choices[0][1]="<?php echo TEXT_SELECT_CHOICE;?>";
				<?php 	$row=1;
						$counter=0;
						while ($question=tep_db_fetch_array($question_query)){
							if ( $question["question_type"] != 6 ) {
								$counter += 1;
							}
							if ($question["question_type"]<4) {
								$question_array[]=array('id'=>$question['question_id'],'text'=>$counter . ". " . $question['question_text']);
								
								
								
								$choice_query=tep_db_query("SELECT choice_id,choice_text from " . TABLE_SERVICES_SURVEYS_CHOICE . " where question_id='" . $question['question_id'] . "'");
								if (tep_db_num_rows($choice_query)>0){
								?>
									choices[<?php echo $row;?>]=new Array();
									<?php 	$row2=0;
											while($choice=tep_db_fetch_array($choice_query)) {?>
										choices[<?php echo $row;?>][<?php echo $row2;?>]="<?php echo $choice['choice_id']; $row2++?>";
										choices[<?php echo $row;?>][<?php echo $row2;?>]="<?php echo addslashes($choice['choice_text']); $row2++?>";
									<?php } ?>
								<?php } else { ?>
									choices[<?php echo $row;?>]=null;
								<?php 
								}
								$row++;
							}
						}
				?>
				function clearChoice(choice){
					while(choice.options.length>0){
						choice.options[0]=null;
					}
					choice.selectedIndex=-1;
				}
				function addChoices(question,cno,cselect){
					var icnt;
					var choice;
					if (cno==1)
						choice=document.relation_params.choice1_id;
					else
						choice=document.relation_params.choice2_id;

					clearChoice(choice);
					icnt=question.selectedIndex;
					if (icnt<0) return;
					if (choices[icnt]==null) return;
					for (var jcnt=0;jcnt<choices[icnt].length;jcnt=jcnt+2){
						choice.options[choice.options.length] = new Option(choices[icnt][jcnt+1], choices[icnt][jcnt]);
					}
				}
				function open_popup(){
					var vtop;
					var vleft;
					var wFeats;
					var width;
					var height;
					var frm=document.relation_params;
					if (frm.question1_id.selectedIndex<=0 || frm.question2_id.selectedIndex<=0 || frm.choice1_id.selectedIndex<0 || frm.choice2_id.selectedIndex<0){
						alert("<?php echo addslashes(stripslashes(ERR_SELECT_QUESTION));?>");
						return;
					}
					width=600;
					height=350;
					vtop=(screen.availHeight-height)/2;
					vleft=(screen.availWidth-width)/2;
					wFeats="height="+height+",width="+width+",location=no,menubar=no,resizable=no,scrollbars=no,toolbar=no,status=yes,left=" + vleft + ",top=" + vtop ;
					//if (window.open("about:blank",'relation_results',wFeats))
					document.relation_params.target='relation_results';
					document.relation_params.submit();
				}
			</script>
			<?php echo tep_draw_form('relation_params',FILENAME_REPORTS_SERVICES_SURVEYS_RELATION,'only_content=Y');
					echo '<input type="hidden" name="post_action" value="report">';
			?>
			<tr>
				<td valign="top">
				<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="50%">
					<tr>
						<td class="main" align="center"><?php echo '<b>' . RELATION_QUESTION1 . '</b>';?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr>
				<td valign="top">
				<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="50%">
					<tr>
						<td class="main"><?php echo RELATION_QUESTION;?></td>
						<td class="main"><?php echo tep_draw_pull_down_menu('question1_id',$question_array,$question1_id,'onChange="javascript:addChoices(this,1);"');?></td>
					</tr>
					<tr>
						<td class="main"><?php echo RELATION_CHOICE;?></td>
						<td class="main"><?php echo tep_draw_pull_down_menu('choice1_id',$choice_array);?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
			<tr>
				<td valign="top">
				<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="50%">
					<tr>
						<td class="main" align="center"><?php echo '<b>' . RELATION_QUESTION2 . '</b>';?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr>
				<td valign="top">
				<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="50%">
					<tr>
						<td class="main"><?php echo RELATION_QUESTION;?></td>
						<td class="main"><?php echo tep_draw_pull_down_menu('question2_id',$question_array,$question2_id,'onChange="javscript:addChoices(this,2);"');?></td>
					</tr>
					<tr>
						<td class="main"><?php echo RELATION_CHOICE;?></td>
						<td class="main"><?php echo tep_draw_pull_down_menu('choice2_id',$choice_array);?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
			<tr>
				<td valign="middle">
				<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="50%">
					<tr>
						<td align=right><?php echo '<a href="javascript:open_popup()">' . tep_image_button('button_submit.gif',IMAGE_SUBMIT) . '</a>&nbsp;' . '<a href="' . tep_href_link(FILENAME_REPORTS_SERVICES_SURVEYS_SUMMARY) . '">' . tep_image_button('button_cancel.gif',IMAGE_CANCEL) . '</a>'; ?></td>
					</tr>
				</table>
				</td>
			</tr>
			<?php 	reset($HTTP_POST_VARS);
					while(list($key,$value)=each($HTTP_POST_VARS)){
						if (!is_array($value) && $value!='x' && $value!='y' && $key!='post_action')
							echo '<input type="hidden" name="' . $key . '" value="' . htmlspecialchars(stripslashes($value)) . '">';
					}
			?>
			</form>
			<script language="JavaScript">
				addChoices(document.relation_params.question1_id,1);
				addChoices(document.relation_params.question2_id,2);
			</script>
				<?php } else { //if questions found ?>
				<tr>
					<td valign="top">
					<table border="0" cellspacing="0" cellpadding="4" width="50%">
						<tr>
							<td align="left" class="main"><?php echo SURVEY_NO_QUESTIONS; ?></td>
						</tr>
					</table>
					</td>
				</tr>
			<?php } // questions found
				} else { // survey found
			?>
				<tr>
					<td valign="top">
					<table border="0" cellspacing="0" cellpadding="4" width="50%">
						<tr>
							<td align="left" class="main"><b><?php echo ERR_NO_SURVEY; ?></b></td>
						</tr>
					</table>
					</td>
				</tr>
			<?php
				} // survey found
				if (!$survey_found || !$question_found) {
				?>
				<tr>
					<td valign="top">
						<?php echo '<a href="' . tep_href_link(FILENAME_REPORTS_SERVICES_SURVEYS_SUMMARY) . '">' . tep_image_button('button_cancel.gif',IMAGE_CANCEL) . '</a>';?>
					</td>
				</tr>	
				<?php
					}
				}  else { //action if ?>
			<tr>
				<td class="pageHeading" align="center"><?php echo sprintf(HEADING_TITLE_RESULTS,$survey_detail_result["survey_name"]) ; ?></td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '10'); ?></td></tr>
			<?php
				$question_query=tep_db_query("SELECT question_text from " . TABLE_SERVICES_SURVEYS_QUESTION . " where question_id='" . $question1_id . "'");
				$choice_query=tep_db_query("SELECT choice_text from " . TABLE_SERVICES_SURVEYS_CHOICE . " where choice_id='" . $choice1_id . "'");			
				$question=tep_db_fetch_array($question_query);
				$choice=tep_db_fetch_array($choice_query);
			?>
			
			<tr>
				<td valign="top">
				<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="100%">
					<tr>
						<td class="main" align="center"><?php echo '<b>' . RELATION_QUESTION1 . '</b>';?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr>
				<td valign="top">
				<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="100%">
					<tr>
						<td class="main"><?php echo RELATION_QUESTION;?></td>
						<td class="dataTableContent"><?php echo $question['question_text'];?></td>
					</tr>
					<tr>
						<td class="main"><?php echo RELATION_CHOICE;?></td>
						<td class="dataTableContent"><?php echo $choice['choice_text'];?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
			<?php
			
				$question_query=tep_db_query("SELECT question_text from " . TABLE_SERVICES_SURVEYS_QUESTION . " where question_id='" . $question2_id . "'");
				$choice_query=tep_db_query("SELECT choice_text from " . TABLE_SERVICES_SURVEYS_CHOICE . " where choice_id='" . $choice2_id . "'");			
				$question=tep_db_fetch_array($question_query);
				$choice=tep_db_fetch_array($choice_query);
			?>
			<tr>
				<td valign="top">
				<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="100%">
					<tr>
						<td class="main" align="center"><?php echo '<b>' . RELATION_QUESTION2 . '</b>';?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr>
				<td valign="top">
				<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="100%">
					<tr>
						<td class="main"><?php echo RELATION_QUESTION;?></td>
						<td class="dataTableContent"><?php echo $question['question_text'];?></td>
					</tr>
					<tr>
						<td class="main"><?php echo RELATION_CHOICE;?></td>
						<td class="dataTableContent"><?php echo $choice['choice_text']; ?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr>
				<td valign="top">
				<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="100%">
					<tr>
						<td class="main"><?php echo TEXT_ANSWERED . $counter;?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
			<script language="javascript">
				function close_window(){
					window.close();
				}
			</script>
			<tr>
				<td valign="top">
				<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="100%">
					<tr>
						<td align="center" valign="middle">
						<script language="JavaScript">
						if (window.print) {
							document.write('<a href="javascript:;" onClick="javascript:window.print()"><?php echo tep_image_button("button_print.gif",IMAGE_PRINT);?></a>');
						}
						else document.write ('<?php echo IMAGE_PRINT; ?>');
						</script>
						<?php 
						echo '<a href="javascript:;" onClick="javascript:close_window()">' . tep_image_submit('button_close.gif',IMAGE_CLOSE) . '</a>';
						?>
						</td>
					</tr>
				</table>
				</td>
			</tr>
			<?php }?>
		</table>
		</td>
<!-- body_text_eof //-->
	 </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>