<?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');
  $command=isset($HTTP_GET_VARS['command'])?$HTTP_GET_VARS['command']:'';
  switch($command){
  	case 'load_details':
	echo 'load_details^';
	$question_id=isset($HTTP_GET_VARS['question_id'])?(int)$HTTP_GET_VARS['question_id']:'';
	echo $question_id.'^';
	$question_query=tep_db_query("select question_id,question_title,question_description,answer_type from " . TABLE_POP_QUESTIONS . " where question_id='$question_id'");
	$question_array=tep_db_fetch_array($question_query);
	$question_subs_array=array();
    $question_subs_array[]=array('id'=>'T','text'=>'Text');
    $question_subs_array[]=array('id'=>'N','text'=>'Number');    
    $question_subs_array[]=array('id'=>'D','text'=>'Date');	
	$question_disp_array=array('T'=>'Text','N'=>'Number','D'=>'Date');
  ?>
  <table width="100%" cellpadding="0" cellspacing="0" border="0" class="openContent">
	<tr heigth="40" bgcolor="#EDF1FE" class="openContent_top">
	<td width="10" valign="top" align="center">&nbsp;</td>
	<td align="left" valign="middle" width="350" class="inner_title"><?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;'.$question_array["question_title"]; ?></td>
	<td width="100" align="left" class="main" valign="top">
		<table border="0" id="header_operation" cellspacing="0" cellpadding="0" style="cursor:pointer;width:100;height:30px;display:inline;position:relative;">
		<tr class="img_move" id="img_ids">
		<td class="cell_bg_popm_left" width="10">&nbsp;</td>
		<?php echo '<td width="10%" onclick="javascript:do_hover_change(this,1);edit_question();" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,3);"><img alt="Edit" title="Edit" src="images/template/img_edit.gif"></td>'; ?>
		<?php echo '<td width="10%" onclick="javascript:do_hover_change(this,1);void delete_question(' . $question_id . ');" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,3);"><img alt="Delete" title="Delete" src="images/template/img_trash.gif"></td>'; ?>
		<td class="cell_bg_popm_right" width="10">&nbsp;</td>
		</tr>
		</table>
	</td>
	<td width="400" align="right"><span style="visibility:hidden;" id="main_operation"><?php echo '<a id="header_edit_operation" href="javascript: save_question('.$question_id.')">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save','','','').'</a>&nbsp;&nbsp;<a href="javascript:void close_question('.$question_id.')">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close','','','').'</a>'; ?></span></td>
	</tr>
	<tr>
	<td colspan="4" id="primary_display">
		<br>
		<table width="100%" class="main" cellpadding="0" cellspacing="0" border="0">
		<tr>
		<td width="120">&nbsp;</td>
		<td width="300" align="right"><b><?php echo TABLE_HEADING_DESCRIPTION; ?> : </b></td>
		<td>&nbsp;<?php echo $question_array['question_description']; ?></td>
		</tr>
		<tr>
		<td colspan="2" align="right"><b><?php echo TABLE_HEADING_ANSWER; ?> : </b></td>
		<td>&nbsp;<?php $question_type=$question_array['answer_type']; echo $question_disp_array[$question_type]; ?></td>
		</tr>
		<tr>	
			<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif',50,20); ?></td>
		</tr>
		</table>
	</td>
	</tr>
	<tr>
	<td colspan="4" id="edit_display" style="display:none;">
		<br>
		<table width="100%" class="main" cellpadding="2" cellspacing="2" border="0">
		<tr>
		<td colspan="2" align="right"><?php echo TABLE_HEADING_TITLE; ?></td>
		<td>&nbsp;<?php echo tep_draw_input_field('edit_question_title',$question_array['question_title']); ?></td>
		</tr>
		<tr>
		<td width="120">&nbsp;</td>
		<td width="300" align="right"><?php echo TABLE_HEADING_DESCRIPTION; ?> :</td>
		<td>&nbsp;<?php echo tep_draw_input_field('edit_question_description',$question_array['question_description']); ?></td>
		</tr>
		<tr>
		<td colspan="2" align="right"><?php echo TABLE_HEADING_ANSWER; ?> :</td>
		<td>&nbsp;<?php $question_type=$question_array['answer_type']; 
		echo tep_draw_pull_down_menu('edit_answer_type',$question_subs_array,$question_type); ?></td>
		</tr>
		<tr>	
			<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif',50,20); ?></td>
		</tr>
		</table>
	</td>
	<td colspan="4" id="delete_display" style="display:none;">
	<td>
	</tr>
	</table>
  <?php
	exit;
	break;
	case 'new_pop_question':
	echo 'new_pop_question^';
	$new_question_title=isset($HTTP_GET_VARS['new_question_title'])?$HTTP_GET_VARS['new_question_title']:'';
	$new_question_description=isset($HTTP_GET_VARS['new_question_description'])?$HTTP_GET_VARS['new_question_description']:'';
	$new_answer_type=isset($HTTP_GET_VARS['new_answer_type'])?$HTTP_GET_VARS['new_answer_type']:'';
	tep_db_query("insert into ".TABLE_POP_QUESTIONS." (question_title,question_description,answer_type) values('$new_question_title','$new_question_description','$new_answer_type') ");
	load_events_questions();
	exit;
	break;
	case 'delete_question':
	$question_id=isset($HTTP_GET_VARS['question_id'])?$HTTP_GET_VARS['question_id']:'';
	tep_db_query("delete from ".TABLE_POP_QUESTIONS." where question_id='$question_id'");
	//echo 'delete_question^'.$question_id;
	echo 'delete_question^';
	echo load_events_questions();
	exit;
	break;
	case 'save_question':
	$question_id=isset($HTTP_GET_VARS['question_id'])?$HTTP_GET_VARS['question_id']:'';
	$edit_question_title=isset($HTTP_GET_VARS['edit_question_title'])?$HTTP_GET_VARS['edit_question_title']:'';
	$edit_question_description=isset($HTTP_GET_VARS['edit_question_description'])?$HTTP_GET_VARS['edit_question_description']:'';
	$edit_answer_type=isset($HTTP_GET_VARS['edit_answer_type'])?$HTTP_GET_VARS['edit_answer_type']:'';
	tep_db_query("update ".TABLE_POP_QUESTIONS." set question_title='$edit_question_title',question_description='$edit_question_description',answer_type='$edit_answer_type' where question_id='$question_id'");
	echo 'new_pop_question^';	
	load_events_questions();
	exit;
	break;
  }
  tep_get_last_access_file();  
  // get input entries to find type of entry
?>

<?php 
	function load_events_questions(){
	$row=0; $class_name="";
	$events_questions_query=tep_db_query("select question_id,question_title,question_description,answer_type from " . TABLE_POP_QUESTIONS . ' order by question_title');
	echo '<table id="load_question_table" class="main" width="100%" cellpadding="2" cellspacing="2" border="0">';
	while($events_questions_array=tep_db_fetch_array($events_questions_query)){
	//if($row%2 ==0) $class_name="dataTableRowEven"; else $class_name="dataTableRowEven";$row++;
				if(($row % 2) == 0) $class_name="dataTableRowEven";
				else $class_name="dataTableRowOdd";
?>
	<tr id="q_id_<?php echo $events_questions_array['question_id']; ?>">
	<td>
	<div onMouseOver="rowOverEffect(this);" onMouseOut="rowOutEffect(this);" class="<?php echo $class_name ?>"  id="events_questions_<?php echo $events_questions_array["question_id"]; ?>"  onClick="javascript: events_question_focus('<?php echo $events_questions_array["question_id"]; ?>');">
	<?php echo tep_draw_separator('pixel_trans.gif','10','10').tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;'.$events_questions_array["question_title"]; ?>
	</div>
	<div id="ajax_events_questions_<?php echo $events_questions_array["question_id"]; ?>" style="display:none;">
	</div>
	</td>
	</tr>
<?php $row++;
	}if(!tep_db_num_rows($events_questions_query)){?>
	<tr class="main"><td width="100%" align="center" id="result_found"><?php echo "No Results Found" ; ?></td></tr>
	<?php } echo '</table>';
	} // function
?>


<!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>
<script language="javascript" src="includes/http.js"></script>
<?php  // WebMakers.com Added: Java Scripts   
      include(DIR_WS_INCLUDES . 'javascript/' . 'webmakers_added_js.php')
?>
</head>

<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="SetFocus();">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- Ajax Work Starts -->
<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr>
		<td style="padding:7px;"><table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr  id="img_new" class="dataTableHeadingRow"><td><?php echo '<a id="new_button" onMouseOver="do_hover_change1(this,2);" onMouseOut="do_hover_change1(this,1);" href="javascript:void new_question();"  >'.tep_image(DIR_WS_IMAGES.'template/img_new.gif','New Question','22','22','align=absmiddle').'</a>'; ?></td></tr>
			<tr><td class="dataTableHeadingRow"><?php echo HEADING_TITLE; ?></td></tr>
		</table>
		</td></tr>	
<tr>
<td  id="load_question_content" width="100%">
	<?php load_events_questions(); ?>
</td>
</tr>
<tr>
<td>	
	<table  id="new_question_content"  style="display:none;" class="openContent" width="100%" cellpadding="2" cellspacing="2" border="0">
	<tr class="openContent_top">
	<td><table width="100%"  cellpadding="0" cellspacing="0" border="0">
		<tr><td class="main">
		<?php echo tep_draw_separator('pixel_trans.gif','10','10').tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;New Question'; ?>
		</td>
		<td align="right"><?php echo '<a href="javascript: new_question_save();">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save').'</a>&nbsp;&nbsp;<a href="javascript: new_question_close();">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close').'</a>'; ?></td>
		</tr>
		</table>
	</td>
	</tr>
	<tr id="new_question_form_data" style="display:none;">
	<td>
	<?php echo tep_draw_form('new_pop_question',FILENAME_POP_QUESTIONS,'action=new_pop_question','post'); ?>
	<?php
	$answer_type_array=array();
    $answer_type_array[]=array('id'=>'T','text'=>'Text');
    $answer_type_array[]=array('id'=>'N','text'=>'Number');    
    $answer_type_array[]=array('id'=>'D','text'=>'Date');	
	?>
		<table class="main" width="100%" border="0" cellpadding="2" cellspacing="2">
		<tr>
			<td width="50%" align="right"><?php echo TABLE_HEADING_TITLE; ?></td>
			<td width="50%"><?php echo tep_draw_input_field('new_question_title'); ?></td>
		</tr>
		<tr>
			<td align="right"><?php echo TABLE_HEADING_DESCRIPTION; ?> :</td>
			<td><?php echo tep_draw_input_field('new_question_description'); ?></td>
		</tr>
		<tr>
			<td align="right"><?php echo TABLE_HEADING_ANSWER; ?> :</td>
			<td><?php echo tep_draw_pull_down_menu('new_answer_type',$answer_type_array); ?></td>
		</tr>
		<tr><td colspan="2">&nbsp;</td></tr>
		</table>
	</form>
	</td>
	</tr>
	</table>
</td>
</tr>
</table>
<div id="ajax_image_load" style="display:none;">
	<?php echo tep_image(DIR_WS_IMAGES.'24-1.gif'); ?>
</div>
<!-- Ajax Work Ends -->

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

<script language="javascript">
var img_src="<?php echo HTTP_SERVER.DIR_WS_ADMIN.'images/';?>";
var previous_question_id='';
	function events_question_focus(question_id){
		if(previous_question_id!=''){
			if(document.getElementById('ajax_events_questions_'+previous_question_id) && document.getElementById('events_questions_'+previous_question_id)){
			document.getElementById('ajax_events_questions_'+previous_question_id).innerHTML='';
			document.getElementById('ajax_events_questions_'+previous_question_id).style.display='none';
			document.getElementById('events_questions_'+previous_question_id).style.display='';
			}
		}
	document.getElementById('ajax_events_questions_'+question_id).style.display='';
	document.getElementById('ajax_events_questions_'+question_id).innerHTML=document.getElementById('ajax_image_load').innerHTML;
	var command="<?php echo tep_href_link(FILENAME_POP_QUESTIONS,'command=load_details&question_id='); ?>"+question_id;
	do_get_command(command);
	previous_question_id=question_id;
	new_question_close();
	}
	function do_result(result){ 
	var token=result.split('^');
		switch(token[0]){
		case 'load_details':
		var question_id=token[1];
		document.getElementById('ajax_events_questions_'+question_id).innerHTML=token[2];
		document.getElementById('events_questions_'+question_id).style.display='none';
		break;
		case 'new_pop_question':
		document.getElementById('load_question_content').innerHTML=token[1];
		new_question_close();
		break;
		case 'delete_question':
		/*var question_id=token[1];
		var row_index=document.getElementById('q_id_'+question_id).rowIndex;
		document.getElementById('load_question_table').deleteRow(row_index);*/
		document.getElementById('load_question_content').innerHTML=token[1];
		break;
		}
	}
	function edit_question(){
	document.getElementById('edit_display').style.display='';
	document.getElementById('delete_display').style.display='none';
	document.getElementById('primary_display').style.display='none';
	document.getElementById('main_operation').style.visibility='visible';
	}
	function new_question_save(){
	document.getElementById('new_button').firstChild.src=document.getElementById('new_button').firstChild.src.replace('_hover.gif','.gif');
	display_flag=1;
	var error=false; var error_message="";
	var new_question_title=document.getElementById('new_question_title').value;
	var new_question_description=document.getElementById('new_question_description').value;
	var new_answer_type=document.getElementById('new_answer_type').value;
	obj=document.getElementById('new_question_title').value;
	obj1=document.getElementById('new_question_description').value;
		if(obj=='' || str_trim(obj)==''){
		error=true;
		error_message="<?php echo TEXT_ERR_NULL_QUESTION_TITLE; ?>";
		}
		if(obj1=='' || str_trim(obj1)=='' ){
		error=true;
		error_message+="\n<?php echo TEXT_ERR_NULL_QUESTION_DESC; ?>";
		}
		if(error==true)
			alert(error_message);
		else{
		var command="<?php echo tep_href_link(FILENAME_POP_QUESTIONS,'command=new_pop_question&new_question_title='); ?>"+new_question_title+'&new_question_description='+new_question_description+'&new_answer_type='+new_answer_type;
		do_get_command(command);		
		//do_post_command('new_pop_question',command);
		}
	}
	function new_question_close(){
	document.getElementById('new_button').firstChild.src=document.getElementById('new_button').firstChild.src.replace('_hover.gif','.gif');
	display_flag=1;
	if(document.getElementById('new_question_content')) document.getElementById('new_question_content').style.display='none';
	if(document.getElementById('new_question_form_data')) document.getElementById('new_question_form_data').style.display='none';
	if(document.getElementById('new_question_title')) document.getElementById('new_question_title').value='';
	if(document.getElementById('new_question_description')) document.getElementById('new_question_description').value='';
	if(document.getElementById('result_found'))document.getElementById('result_found').style.display='';
	}
	function save_question(question_id){
	var edit_question_title=document.getElementById('edit_question_title').value;
	var edit_question_description=document.getElementById('edit_question_description').value;
	var edit_answer_type=document.getElementById('edit_answer_type').value; var error=false,error_message="";
		if(edit_question_title=='' || str_trim(edit_question_title)==''){ error=true;
		error_message="<?php echo TEXT_ERR_NULL_QUESTION_TITLE; ?>";
		}
		if(edit_question_description=='' || str_trim(edit_question_description)=='' ){ error=true;
		error_message+="\n<?php echo TEXT_ERR_NULL_QUESTION_DESC; ?>";
		}
		if(error==true)
			alert(error_message);
		else{
		var command="<?php echo tep_href_link(FILENAME_POP_QUESTIONS,'command=save_question&question_id='); ?>"+question_id+'&edit_question_title='+edit_question_title+'&edit_question_description='+edit_question_description+'&edit_answer_type='+edit_answer_type;
		do_get_command(command);
		}
	}
	function close_question(question_id){
		if(document.getElementById('events_questions_'+question_id) && document.getElementById('ajax_events_questions_'+question_id)){
		document.getElementById('ajax_events_questions_'+question_id).innerHTML='';
		document.getElementById('ajax_events_questions_'+question_id).style.display='none';
		document.getElementById('events_questions_'+question_id).style.display='';
		}
	}
	function delete_question(question_id){
		document.getElementById('delete_display').style.display='';
		document.getElementById('edit_display').style.display='none';
		if(confirm("<?php echo DELETE_ALERT; ?>")){
		var command="<?php echo tep_href_link(FILENAME_POP_QUESTIONS,'command=delete_question&question_id='); ?>"+question_id;
		do_get_command(command);
		}else{
		close_question(question_id);
		}
	}
	function new_question(){
	display_flag=0;
		if(previous_question_id!=''){ question_id=previous_question_id;
		if(document.getElementById('events_questions_'+question_id) && document.getElementById('ajax_events_questions_'+question_id)){
		document.getElementById('ajax_events_questions_'+question_id).innerHTML='';
		document.getElementById('ajax_events_questions_'+question_id).style.display='none';
		document.getElementById('events_questions_'+question_id).style.display='';
		}
		}
	document.getElementById('new_question_content').style.display='';
	document.getElementById('new_question_form_data').style.display='';
	if(document.getElementById('result_found'))document.getElementById('result_found').style.display='none';
	}
	function do_hover_change(element,eve){
		 var edit_display=document.getElementById('edit_display');
		 var delete_display=document.getElementById('delete_display');
	  	 if(element){		   		    		    
		    var img_get_src=element.childNodes[0];
			if(img_get_src){
			    if(img_get_src.src==img_src+'template/img_edit.gif') {
					if(edit_display && edit_display.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_edit_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_edit_hover.gif' && edit_display && edit_display.style.display=="none") img_get_src.src=img_src+'template/img_edit.gif';
			    if(img_get_src.src==img_src+'template/img_trash.gif') {
					if(delete_display && delete_display.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_trash_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_trash_hover.gif' && delete_display && delete_display.style.display=="none") img_get_src.src=img_src+'template/img_trash.gif';
		  }
		}		 
	}
	var display_flag=1;	
	function do_hover_change1(element,mode){ 
	if(display_flag==1){
		if (element.mode && mode==element.mode) return;
		if (mode==2)
			element.firstChild.src=element.firstChild.src.replace(".gif","_hover.gif");
		else
			element.firstChild.src=element.firstChild.src.replace("_hover.gif",".gif");
	}
	element.mode=mode;
	} 
	
</script>
<?php
$questions_chk_query=tep_db_query("select count(*) as total from " . TABLE_POP_QUESTIONS);
$question_chk_array=tep_db_fetch_array($questions_chk_query); 
if($question_chk_array['total']<=0){
?>
	<script language="javascript">
	//document.getElementById('new_question_content').style.display='';
	//document.getElementById('new_question_form_data').style.display='';
	</script>
<?php } ?>
