<?php
/*

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce
  
  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
*/
// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
//services
   if (isset($HTTP_GET_VARS['service_action'])) {
	   $element_type=isset($HTTP_GET_VARS['element_type'])?tep_db_prepare_input($HTTP_GET_VARS['element_type']):'P';
	   $goto =  FILENAME_SHOPPING_CART;
       $parameters = array('service_action', 'secPath', 'service_id','qty');
   }
  switch ($HTTP_GET_VARS['service_action']) {
	case 'buy_service':
		$service_id=isset($HTTP_GET_VARS['service_id'])?tep_db_prepare_input($HTTP_GET_VARS['service_id']):'';
		$store_membership_option=isset($HTTP_POST_VARS['store_membership_option'])?tep_db_prepare_input($HTTP_POST_VARS['store_membership_option']):"";	

		if ($service_id){
			 if(isset($HTTP_POST_VARS['start_date']))
			 {
			   $start_date=tep_db_prepare_input($HTTP_POST_VARS['start_date']);
			   $start_time=isset($HTTP_POST_VARS['start_time'])?tep_db_prepare_input($HTTP_POST_VARS['start_time']):'';
			 }elseif(isset($HTTP_POST_VARS['start_times'])){
				$start_times=tep_db_prepare_input($HTTP_POST_VARS['start_times']);
				$splt_date=split(" ",$start_times);
				$start_date=$splt_date[0];
				$start_time=$splt_date[1];
			 }
			$end_times=isset($HTTP_POST_VARS['end_times'])?tep_db_prepare_input($HTTP_POST_VARS['end_times']):'';
			$splt_date=split(" ",$end_times);
			$end_date=$splt_date[0];
			$end_time=$splt_date[1];
			$others["start_date"]=$start_date;
			$others["end_date"]=$end_date;
			$others["start_time"]=$start_time;
			$others["end_time"]=$end_time;
			$others["resource_id"]=isset($HTTP_POST_VARS['resource_ids'])?tep_db_prepare_input($HTTP_POST_VARS['resource_ids']):"0";
			$others["resource_costs"]=isset($HTTP_POST_VARS['resource_costs'])?tep_db_prepare_input($HTTP_POST_VARS['resource_costs']):0;
			$quantity=isset($HTTP_POST_VARS['ser_quantity'])?(int)$HTTP_POST_VARS['ser_quantity']:1;
			$service=get_services_info((int)$service_id,(int)$languages_id);
			if($service['include_resource']=='N' && $service['multiple_purchase']=='Y') {
				$service_info=$service;
				$er="";
				$er=tep_check_service_found($service_id,$start_date,$end_date,$start_time,$end_time,"error");
				if($er!='') 
				{
					echo "service_error||" . $er;
					$HTTP_GET_VARS['command']='';
					exit;
				}
			}
			if(isset($HTTP_POST_VARS['option_ids'])){
			  $option_array=split(",",$HTTP_POST_VARS['option_ids']); 
			  while(list($key,$value)=each($option_array)){
				 $attribute_split=split("-",$value);
				 if($attribute_split[0])
				 	$attribute_array[$attribute_split[0]]=$attribute_split[1];
				}
			}
			$cart->add_cart_service($service_id,$quantity,$attribute_array,true,$others);
			
			if($store_membership_option=='Y'){
		 	$subs_query=tep_db_query("select s.subscription_id as id from " .TABLE_SUBSCRIPTIONS ." s," . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " sc where s.subscription_id=sc.subscription_id and  s.subscription_type='M'");
			$subs_result=tep_db_fetch_array($subs_query);
			$order_query=tep_db_query("select o.orders_id from " .TABLE_ORDERS_PRODUCTS ." op," . TABLE_ORDERS . " o where op.orders_id=o.orders_id and o.customers_id='" . (int)$customer_id . "' and  op.products_id='" . $subs_result['id']."' and op.products_type='S'");
			$order_result=tep_db_fetch_array($order_query);
			 $item_exists=false;
			 $check_subs=false;
			if(tep_db_num_rows($order_query)<=0) {
				$check_subs=true;
			}	else  {
				
				if(tep_session_is_registered('order_id') && $order_id==$order_result['orders_id']) {
					$check_subs=true;
				}
			}
			//store membership
			if($order_result['orders_id']>0){
				while (list($products_id, ) = each($cart->contents)) {
					if($cart->contents[$products_id]['element_type']=='S' && $products_id=$subs_result['id'])
						$item_exists=true;
				}
				if(!$item_exists) {
					$subs_start_date=tep_convert_date_raw(date(EVENTS_DATE_FORMAT));
					$cart->add_cart_subscription($subs_result['id'], 1, '','','','',$subs_start_date);
				}			
			} else{
			if($check_subs) {
				while (list($products_id, ) = each($cart->contents)) {
					if($cart->contents[$products_id]['element_type']=='S' && $products_id=$subs_result['id'])
						$item_exists=true;
				}
				if(!$item_exists) {
					$subs_start_date=tep_convert_date_raw(date(EVENTS_DATE_FORMAT));
					$cart->add_cart_subscription($subs_result['id'], 1, '','','','',$subs_start_date);
				}
				}	
			
			}	
			}
		}
		//tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters)));
		break;
  }?>
