<?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 );
  function tep_get_user_survey_flags(){
     $user_flags=array();
     
     $user_flags['id']=$_COOKIE['id'];
     return $user_flags;
  }
  require('includes/application_top.php');
  // check date user setting of this survey.
  
  $survey_id=$HTTP_POST_VARS['survey_id'];
  $question_id=$HTTP_POST_VARS['question_id'];
  $question_type=$HTTP_POST_VARS['question_type'];
  $flag_id=0;
  $user_flags=tep_get_user_survey_flags();
  for($icnt=0;$icnt<sizeof($question_id);$icnt++){
    $choices=$HTTP_POST_VARS['choices_' . $question_id[$icnt]];
    if (tep_not_null($choices) && is_array($choices)){
       if ($flag_id==0){
           $sql_array=array('survey_flag_email'=>tep_db_prepare_input($user_flags['id']),
	                    'survey_flag_date' =>'now()',
			    'survey_id'=>$survey_id);
	   tep_db_perform(TABLE_EVENTS_SURVEYS_FLAG,$sql_array);
	   $flag_id=tep_db_insert_id();
       }
       switch($question_type[$icnt]){
            case 1:
	    case 2:
	    case 3:
	        for($jcnt=0;$jcnt<sizeof($choices);$jcnt++){
		    $sql_array=array('survey_flag_id'=>tep_db_prepare_input($flag_id),
		                     'question_id'=>tep_db_prepare_input($question_id[$icnt]),
				     'choice_id'=>tep_db_prepare_input($choices[$jcnt]));
		    tep_db_perform(TABLE_EVENTS_SURVEYS_ANSWER,$sql_array);
		}
		break;
	    case 4:
	    case 5:
	       $sql_array=array('survey_flag_id'=>tep_db_prepare_input($flag_id),
	                        'question_id'=>tep_db_prepare_input($question_id[$icnt]),
				'answer_text'=>tep_db_prepare_input($choices[0]));
	       tep_db_perform(TABLE_EVENTS_SURVEYS_TEXT,$sql_array);
      }
    }
  }
  tep_redirect(tep_href_link(FILENAME_EVENTS_SURVEYS,'suID=' . $survey_id));
?>
