<?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');
  // check date user setting of this survey.
?>
<!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">
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr> 
	<!-- body_text //-->
<?php
 $suID=(isset($HTTP_GET_VARS['suID'])?(int)$HTTP_GET_VARS['suID']:0);
$survey_query=tep_db_query("SELECT * from " . TABLE_EVENTS_SURVEYS . " where survey_id='" . (int)$suID ."' and survey_visibility_flag=1 and survey_expiration_date<=curdate()");

$survey_result=tep_db_fetch_array($survey_query);
// check for previous date
$survey_accept='empty_survey';
if (tep_db_num_rows($survey_query)>0)
{
   $survey_accept='empty_question';
   $question_query = tep_db_query("SELECT * from " . TABLE_EVENTS_SURVEYS_QUESTION . " where survey_id='" . (int)$suID . "' order by sort_order asc");
   if (tep_db_num_rows($question_query)>0){  
      $survey_accept='date_expires';
      $flag_query=tep_db_query("SELECT date_add(survey_flag_date, interval '" . $survey_result['survey_time_period'] . "' month) as check_date,survey_flag_date from " . TABLE_EVENTS_SURVEYS_FLAG . " where survey_id='" . (int)$suID . "' and survey_flag_email='" . $HTTP_COOKIE_VARS['id'] . "'");
      if (tep_db_num_rows($flag_query)<=0)
         $survey_accept="accept";
      else
      {
         $flag_result=tep_db_fetch_array($flag_query);
         if ($flag_result['check_date']<=getServerDate())
	    $survey_accept="accept";
      }
      $counter = 1;
   }
}
?>
<script language="JavaScript1.3" type="text/javascript">
<!--
function validateForm(theForm) { //form validator
  var cnt,jcnt,value="",err_result="",con_value,con_length;
  if (theForm.question_id[0]){
     for(cnt=0;cnt<theForm.question_id.length;cnt++){
        value="";
	con_value="";
        if (theForm.question_required[cnt].value==1){
	    switch(theForm.question_type[cnt].value){
	       case '1':  // radio,check
               case '3':
	           con_value=eval("theForm.choices_" + theForm.question_id[cnt].value+"[0]");
		   if (con_value){
		       con_length=eval("theForm.choices_" + theForm.question_id[cnt].value+".length");
		       for(jcnt=0;jcnt<con_length;jcnt++){
		         if (eval("theForm.choices_" + theForm.question_id[cnt].value+"["+jcnt+"].checked")==true) value="1";
		       }
		      if (value!="1")
		         err_result+="<?php echo tep_db_input(ERR_VALUE_REQUIRED); ?>"+(cnt+1)+"\n";
		   } else {
		        if (eval("theForm.choices_" + theForm.question_id[cnt].value+".checked")==false)
       		         err_result+="<?php echo tep_db_input(ERR_VALUE_REQUIRED); ?>"+(cnt+1)+"\n";
		   }
		   break;
               case '2':  // pulldown
	       case '4':
	       case '5':
	          value=eval("theForm.choices_" + theForm.question_id[cnt].value + ".value");
		  if (value=='' || value=='0'){
		     err_result+="<?php echo tep_db_input(ERR_VALUE_REQUIRED); ?>"+(cnt+1)+"\n";
		  }
	    }
	}
    }
 } else {
        if (theForm.question_required.value==1){
	    switch(theForm.question_type.value){
	       case '1':  // radio,check
               case '3':
	           con_value=eval("theForm.choices_" + theForm.question_id.value+"[0]");
		   if (con_value){
		      con_length=eval("theForm.choices_" + theForm.question_id.value+".length");
		      for(jcnt=0;jcnt<con_length;jcnt++){
		         if (eval("theForm.choices_" + theForm.question_id.value+"["+jcnt+"].checked")==true) value="1";
		      }
		      if (value!="1")
		         err_result+="<?php echo tep_db_input(ERR_VALUE_REQUIRED); ?>1\n";
		   } else {
		      if (eval("theForm.choices_" + theForm.question_id.value+".checked")==false)
       		         err_result+="<?php echo tep_db_input(ERR_VALUE_REQUIRED); ?>1\n";
		   }
		   break;
               case '2':  // pulldown
	       case '4':  // Single Line
	       case '5':  // multiple lines
	          value=eval("theForm.choices_" + theForm.question_id.value + ".value");
		  if (value=='' || value=='0'){
		     err_result+="<?php echo tep_db_input(ERR_VALUE_REQUIRED); ?>1\n";
		  }
	    }
	}
 }
 if (err_result!=''){
    alert(err_result);
    return false;
 }
return (true);
}
-->
</script>
    <td width=100% align=left valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
        <tr>
          <td class="pageHeading"><?php echo SURVEY_HEADING_TITLE; ?></td>  
        </tr>
        <tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','15'); ?></td></tr>
        <tr>
            <td class="main"><b><?php echo TEXT_SURVEY_NAME . $survey_result['survey_name']; ?></b></td>  
        </tr>
        <tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td></tr>
	<?php if ($survey_accept!='accept') { ?>
	    <tr><td><table border="0" cellspacing="2" cellpadding="2" class=formArea>
	        <?php if ($survey_accept=='date_expires') { ?>
	           <tr><td class="main"><?php echo sprintf(TEXT_SURVEY_TIME_INTRO,$survey_result['survey_time_period']); ?></td></tr>
		   <tr><td class="main"><?php echo TEXT_SURVEY_FILL_DATE . $flag_result['survey_flag_date'];?></td></tr>
		   <tr><td class="main"><?php echo TEXT_SURVEY_FUTURE_DATE . $flag_result['check_date']; ?></td></tr>
		<?php } else if ($survey_accept=='empty_question') { ?>
		   <tr><td class="main"><?php echo TEXT_EMPTY_QUESTION; ?></td></tr>
		<?php } else { ?>
		   <tr><td><?php echo TEXT_EMPTY_SURVEY;?></td></tr>
		<?php } ?>
		</table></td></tr>
	<?php } else { ?>
        <tr>
	   <td><?php echo tep_draw_form('submit_survey',FILENAME_EVENTS_SURVEYS_PROCESS,'','post','onSubmit=" return validateForm(this)"'); ?>
	      <table border="0" cellspacing="0" cellpadding="0" class="formArea">
             <tr>
	       <td valign="top"><table border="0" cellspacing="0" cellpadding="4">
                  <?php while($question_result = tep_db_fetch_array($question_query)){ 
		         $choice_query=tep_db_query("SELECT choice_id,choice_text from " . TABLE_EVENTS_SURVEYS_CHOICE . " where question_id='" . (int)$question_result['question_id'] . "' order by choice_id");
		     if ($question_result['question_type']>3 || tep_db_num_rows($choice_query)>0){
		  ?>
	          <tr>
		    <td class="main" align=right><?php if ($question_result['question_type']!=6) echo $counter . ". ";?></td>
		    <td class="main" align=left><?php echo $question_result['question_text'];
		                           echo '&nbsp;<i>' . ($question_result['question_required']==1?'(' .TEXT_REQUIRED . ')':'') . '</i>' ?>
		    </td>
		  </tr>
		  <?php 
		     
	             $choice = "";
		     $choice_array[]=array('id'=>0,text=>'');
	             while($choice_result = mysql_fetch_array ($choice_query)){
		        switch ($question_result["question_type"]){
			    case 1: //radio
            	               $choice .= "&nbsp;&nbsp;" . tep_draw_radio_field("choices_" . $question_result['question_id'] . "[]",$choice_result['choice_id'],false,'','id="choices_' . $question_result['question_id'] . '"') . "&nbsp;" . $choice_result["choice_text"] . "<br>";
			       break;
                            case 2: //pulldown
            	               $choice_array[]=array('id'=>$choice_result['choice_id'],'text'=>$choice_result['choice_text']);
				break;
                            case 3: //checkbox
            	               $choice .= "&nbsp;&nbsp;" . tep_draw_checkbox_field("choices_" . $question_result['question_id'] . "[]",$choice_result['choice_id'],false,'','id="choices_' . $question_result['question_id'] . '"') . "&nbsp;" . $choice_result["choice_text"] . "<br>";
				break;
                         }
	            }
		 ?>
		 <tr>
		    <td class="main"></td>
		    <td class="main" align=left>
		 <?php 
	             switch ($question_result["question_type"]){
			case 2:
			   $choice=tep_draw_pull_down_menu("choices_" . $question_result['question_id'] . "[]",$choice_array,'','id="choices_' . $question_result['question_id'] . '"');
			   break;
			case 4:
			   $choice="&nbsp;&nbsp;" . tep_draw_input_field("choices_" . $question_result['question_id'] . "[]",'','size=99 id="choices_' . $question_result['question_id'] . '"');
			   break;
			case 5:
			   $choice="&nbsp;&nbsp;" . tep_draw_textarea_field("choices_" . $question_result['question_id'] . "[]" ,'','99','3','','id="choices_' . $question_result['question_id'] . '"');
			   break;
                    }
	            echo $choice;
                 ?>
		 </td>
		</tr>
                <tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td></tr>
		<?php if ($question_result['question_type']!=6) {$counter++;
	                 echo tep_draw_hidden_field('question_required[]',$question_result['question_required'],'id="question_required"');
			 echo tep_draw_hidden_field('question_type[]',$question_result['question_type'],'id="question_type"');
			 echo tep_draw_hidden_field('question_id[]',$question_result['question_id'],'id="question_id"');}
		 }} ?>
               </table></td>
	    </tr></table></td></tr>
            <tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td></tr>
	    <tr>
	      <td>
      	        <?php echo tep_draw_hidden_field('survey_id',$HTTP_GET_VARS['suID']);?>
	        <?php if ($counter>1) echo tep_image_submit('button_survey_submit.gif',IMAGE_SURVEY_SUBMIT); ?>
	      </td>
	    </tr>
            <?php echo '</form>' ; ?>
	    <?php } ?>
<!-- body_eof //-->
</table></td></tr>
</table>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>