<?php
/*
	osCommRes, Services Oline 
	http://www.oscommres.com 
	Copyright (c) 2005 osCommRes 
	
	Freeway eCommerce 
	http://www.zac-ware.com/freeway 
	Copyright (c) 2007 ZacWare
	
	Released under the GNU General Public License
*/


// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	
	require('includes/application_top.php');
	// check date user setting of this survey.
	if ($HTTP_GET_VARS['action']==''){
		$survey_id=(isset($HTTP_POST_VARS['survey_id'])?(int)$HTTP_POST_VARS['survey_id']:0);
		$question_id=(isset($HTTP_POST_VARS['question_id'])?tep_db_prepare_input($HTTP_POST_VARS['question_id']):array());
		$question_type=(isset($HTTP_POST_VARS['question_type'])?tep_db_prepare_input($HTTP_POST_VARS['question_type']):array());
		$login_id=(isset($HTTP_POST_VARS['login_id'])?tep_db_prepare_input($HTTP_POST_VARS['login_id']):0);

		if ($survey_id<=0 || sizeof($question_id)<=0 || sizeof($question_type)<=0 || $login_id<=0) 
			tep_redirect(tep_href_link(FILENAME_DEFAULT));
		$flag_id=0;
		for($icnt=0;$icnt<sizeof($question_id);$icnt++){
			$choices=tep_db_prepare_input($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($login_id),
										'survey_flag_date' =>'now()',
										'survey_id'=>$survey_id);
					tep_db_perform(TABLE_SERVICES_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_SERVICES_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_SERVICES_SURVEYS_TEXT,$sql_array);
				}
			}
		}
		tep_redirect(tep_href_link(FILENAME_SERVICES_SURVEYS_PROCESS,'action=success&login_id=' . $login_id,'SSL'));
	}

	$content = CONTENT_SERVICES_SURVEYS_PROCESS;
	
	require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_SERVICES_SURVEYS_PROCESS);
	
	require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);
	
	require(DIR_WS_INCLUDES . 'application_bottom.php');

?>
