<?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.
   $services->attributes=array();
   //service attribute values
   if(is_array($HTTP_POST_VARS['id'])) $services->attributes=$HTTP_POST_VARS['id'];
   $survey_result="";
   $service_id=(isset($HTTP_GET_VARS['service_id'])?(int)$HTTP_GET_VARS['service_id']:0);
   $type=(isset($HTTP_GET_VARS['type'])?tep_db_prepare_input($HTTP_GET_VARS['type']):'');
	$survey_query=tep_db_query("SELECT * from " . TABLE_SERVICES_SURVEYS . " where service_id='" . tep_db_input($service_id) ."' and survey_type='" . tep_db_input($type) . "' and survey_visibility_flag=1 and survey_expiration_date>='".getServerDate()."'");	
	$survey_result=tep_db_fetch_array($survey_query);
	// check for previous date
	$survey_accept='empty_survey';
	$sur_cnt=tep_db_num_rows($survey_query);
	if (tep_db_num_rows($survey_query)>0)
	{
		$survey_accept='empty_question';
		$question_query = tep_db_query("SELECT * from " . TABLE_SERVICES_SURVEYS_QUESTION . " where survey_id='" . (int)$survey_result['survey_id'] . "' order by sort_order asc");
		$qcount=tep_db_num_rows($question_query);
		if ($qcount>0){
			$survey_accept='date_expires';
			$flag_query=tep_db_query("SELECT date_add(survey_flag_date, interval '" . tep_db_input($survey_result['survey_time_period']) . "' month) as check_date,survey_flag_date from " . TABLE_SERVICES_SURVEYS_FLAG . " where survey_id='" . tep_db_input($survey_result['survey_id']) . "' and survey_flag_email='" . tep_db_input($cuID) . "' order by check_date desc");
			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;
		}
	}
	if ($survey_accept!='accept' && $type=='CD')
		tep_redirect(tep_href_link(FILENAME_SHOPPING_CART,tep_get_all_get_params().'service_action=buy_service&service_id='.$services->service_id));
	else {
		if(tep_session_is_registered('customer_id')) {
			$cuID=($HTTP_SESSION_VARS['customer_id']!='')?(int)$HTTP_SESSION_VARS['customer_id']:(int)$_SESSION['customer_id'];
	   } else {
			$cuID=(isset($HTTP_COOKIE_VARS['survey_login_id'])?(int)$HTTP_COOKIE_VARS['survey_login_id']:0);
			//check the customer id is present in table
			if ($cuID>0) {
				$customer_query=tep_db_query("SELECT count(*) from ". TABLE_CUSTOMERS . " where customers_id='" . tep_db_input($cuID) ."'");
				if (tep_db_num_rows($customer_query)<=0) $cuID=0;
			}
	  }
	  if ($cuID<=0){
		$page=array();
		$page=array('page'=>FILENAME_SERVICES_SURVEYS_ANSWER,
		 		    'mode' =>((ENABLE_SSL==true)?'SSL':'NONSSL'),
                    'get' => tep_db_prepare_input($HTTP_GET_VARS),
                    'post' => tep_db_prepare_input($HTTP_POST_VARS));
		$navigation->set_snapshot($page);
		$return=tep_db_prepare_input((isset($HTTP_GET_VARS["R"])?"R=1":''));
		tep_redirect(tep_href_link(FILENAME_LOGIN, $return, 'SSL'));
	  }
	}		

	$content = CONTENT_SERVICES_SURVEYS_ANSWER;
	require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_SERVICES_SURVEYS_ANSWER);
	
	require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);
	
	require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
