<?php
/*
  $Id: shopping_cart.php,v 1.1.1.1 2003/09/18 19:05:12 wilt Exp $

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

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/
defined('_FEXEC') or die(); 
class shoppingCart {
	var $contents, $total, $weight, $cartID, $content_type,$mode;
	
	function shoppingCart() {
		$this->reset();
	}
	function restore_contents() {
		//ICW replace line
		global $customer_id, $gv_id, $REMOTE_ADDR;
		//      global $customer_id;
		$bol_insert=false;
		if (!tep_session_is_registered('customer_id')) return false;
		// insert current cart contents in database
		if (is_array($this->contents)) {
			reset($this->contents);

			while (list($products_id, ) = each($this->contents)) {

				$qty = $this->contents[$products_id]['qty'];
				// if element is event omit 'E' in products id
				$element_id=$products_id;

				if ($this->contents[$products_id]['element_type']=='E' || $this->contents[$products_id]['element_type']=='S') $element_id=substr($products_id,1);

				$product_query = tep_db_query("select products_id from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($element_id) . "' and products_type='".$this->contents[$products_id]['element_type']."'");

 				if (tep_db_num_rows($product_query)<=0) {
					tep_db_query("delete from " .TABLE_CUSTOMERS_TEMP_BASKET ." where products_id='".tep_db_input($element_id)."' and products_type='".$this->contents[$products_id]['element_type']."' and customers_id='".tep_session_id()."'"); 
					if($qty=="")$qty=0;
					tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added, products_type,old_orders_id,answer_type,answer_value,forced_id,subscription_start_date,discount_id) values ('" . (int)$customer_id . "', '" . tep_db_input($element_id) . "', '" . $qty . "',now(),'" . $this->contents[$products_id]['element_type'] . 
									"','" . (int)tep_db_input($this->contents[$products_id]['old_orders_id']) . "','" . tep_db_input($this->contents[$products_id]['answer_type']) . "','" . tep_db_input($this->contents[$products_id]['answer_value']) . "','" . tep_db_input($this->contents[$products_id]['forced_item_ids']) . "','" . tep_db_input($this->contents[$products_id]['subscription_start_date']) . "'," . ((int)$this->contents[$products_id]['discount_id']) . ")");
					if (isset($this->contents[$products_id]['attributes'])) {
						tep_db_query("delete from " .TABLE_CUSTOMERS_TEMP_BASKET_ATTRIBUTES ." where products_id='".tep_db_input($element_id)."' and customers_id='".tep_session_id()."'"); 
						reset($this->contents[$products_id]['attributes']);
						while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
							tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($element_id) . "', '" . (int)$option . "', '" . (int)$value . "')");
						}
					}
					if ($this->contents[$products_id]['element_type']=='E'){
						$sessions=&$this->contents[$products_id]['sessions'];
						tep_db_query("delete from " .TABLE_CUSTOMERS_TEMP_BASKET_SESSIONS ." where products_id='".tep_db_input($element_id)."' and customers_id='".tep_session_id()."'"); 
						reset($sessions);
						while(list($id,)=each($sessions)){
							tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_SESSIONS ." (customers_id, products_id, session_id, session_type, session_group,session_date) values('" . (int)$customer_id . "','" . $element_id . "','" . $id . "','" . $sessions[$id]['type'] . "','" . $sessions[$id]['group'] . "','" . $sessions[$id]['date'] . "')");
						}
					}
						if ($this->contents[$products_id]['element_type']=='V'){
						$others=&$this->contents[$products_id]['others'];
						tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_SERVICES . " (customers_id,products_id,start_date,end_date,start_time,end_time,resource_id) values(" . (int)$customer_id . ",'" . $element_id . "','" . tep_db_input($others["start_date"]) . "','" . tep_db_input($others["end_date"]) . "','" . tep_db_input($others["start_time"]) . "','" . tep_db_input($others["end_time"]) ."','".(int)tep_db_input($others["resource_id"])."')");
					}
				} else {
					tep_db_query("delete from " .TABLE_CUSTOMERS_TEMP_BASKET ." where products_id='".tep_db_input($element_id)."' and products_type='".$this->contents[$products_id]['element_type']."' and customers_id='".tep_session_id."'"); 
					if ($this->contents[$products_id]['element_type']=='P'){
					if($qty=="")$qty=0;
						tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $qty . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($element_id) . "'");
					//	echo "update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $qty . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($element_id) . "'".'hh';
					} else if ($this->contents[$products_id]['element_type']=='E') {
						$sessions=&$this->contents[$products_id]['sessions'];
						reset($sessions);
						while(list($id,)=each($sessions)){
							$session_query=tep_db_query("SELECT session_id from " . TABLE_CUSTOMERS_BASKET_SESSIONS . " where session_id='" . $id . "' and products_id='" . $element_id . "' and customers_id='" . (int)$customer_id . "'");
							if (!tep_db_num_rows($session_query)){
								tep_db_query("delete from ".TABLE_CUSTOMERS_TEMP_BASKET_SESSIONS ." where session_id='".$id ."' and products_id='".$element_id ."' and customers_id='".tep_session_id()."'") ;
								tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_SESSIONS ." (customers_id, products_id, session_id, session_type, session_group,session_date) values('" . (int)$customer_id . "','" . $element_id . "','" . $id . "','" . $sessions[$id]['type'] . "','" . $sessions[$id]['group'] . "','" . $sessions[$id]['date'] . "')");
							}
						}
					} else if ($this->contents[$products_id]['element_type']=='V') {
						$others=&$this->contents[$products_id]["others"];
						tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $qty . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($element_id) . "'");
						tep_db_query("update " . TABLE_CUSTOMERS_BASKET_SERVICES . " set start_date = '" . tep_db_input($others["start_date"]) . "',end_date = '" . tep_db_input($others["end_date"]) . "',start_time='" . tep_db_input($others["start_time"]) . "',end_time='" . tep_db_input($others["end_time"]) . "',resource_id='".(int)tep_db_input($others["resource_id"])."' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($element_id) . "'");
					}
				}
				if ($this->contents[$products_id]['element_type']=='E' && $this->contents[$products_id]['qty']>1 && !$bol_insert){
					$this->save_attendees();
					$bol_insert=true;
				}
        	}
			//ICW ADDDED FOR CREDIT CLASS GV - START
			if (tep_session_is_registered('gv_id')) {
				$gv_query = tep_db_query("insert into  " . TABLE_COUPON_REDEEM_TRACK . " (coupon_id, customer_id, redeem_date, redeem_ip) values ('" . $gv_id . "', '" . (int)$customer_id . "', now(),'" . $REMOTE_ADDR . "')");
				$gv_update = tep_db_query("update " . TABLE_COUPONS . " set coupon_active = 'N' where coupon_id = '" . $gv_id . "'");
				tep_gv_account_update($customer_id, $gv_id);
				tep_session_unregister('gv_id');
			}
			//ICW ADDDED FOR CREDIT CLASS GV - END
		}

		// reset per-session cart contents, but not the database contents
		$this->reset(false);
		$bol_taken=false;
		$products_query = tep_db_query("select products_id,customers_basket_quantity,products_type,old_orders_id,answer_type,answer_value,forced_id,discount_id from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'");
		while ($products = tep_db_fetch_array($products_query)) {
			if ($products['products_type']=='E'){
				$products_id='E' . $products['products_id'];
				if ($products["customers_basket_quantity"]>1) {
					if (!$bol_taken) {
						$this->get_attendees();
						$bol_taken=true;
					}
					$this->multiple_event_attendees+=1;
				}
			} else if ($products['products_type']=='S') {
				$products_id='S' . $products['products_id'];
			} else {
				$products_id=$products['products_id'];
			}
			$this->contents[$products_id] = array('qty' => $products['customers_basket_quantity'],'element_type'=>$products['products_type'],'old_orders_id'=>(int)$products['old_orders_id'],'answer_type'=>$products['answer_type'],'answer_value'=>$products['answer_value'],'sessions'=>array(),'forced_item_ids'=>$products['forced_id'],'discount_id'=>$products['discount_id']);
			if ($products['products_type']=='E'){
				$session_query=tep_db_query("SELECT session_id,session_type,session_group,session_date from " . TABLE_CUSTOMERS_BASKET_SESSIONS . " where customers_id='" . (int)$customer_id . "' and products_id='" . (int)$products["products_id"] . "' order by session_date");
				while($result=tep_db_fetch_array($session_query)){
					$this->contents[$products_id]['sessions'][$result["session_id"]]=array(	'type'=>$result["session_type"],
																							'group'=>$result["session_group"],
																							'date'=>$result["session_date"],
																							'fees'=>0
													 										);
				}
			}	if ($products['products_type']=='V'){
				$service_query=tep_db_query("SELECT start_date,end_date,start_time,end_time,resource_id from " . TABLE_CUSTOMERS_BASKET_SERVICES . " where customers_id='" . (int)$customer_id . "' and products_id='" . tep_db_input($products['products_id']) ."'");
				$service_result=tep_db_fetch_array($service_query);
				$this->contents[$products_id]['others']=$service_result;
				if($this->contents[$products_id]['others']['resource_id']){
					 $resource_price=get_details('resource_costs',TABLE_RESOURCES,'resource_id='.(int)$this->contents[$products_id]['others']['resource_id']);	
					 $this->contents[$products_id]['others']['resource_costs']=$resource_price[0]['resource_costs'];
				}
			}
			$attributes_query = tep_db_query("select products_options_id, products_options_value_id from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products['products_id']) . "'");
			while ($attributes = tep_db_fetch_array($attributes_query)) {
				$this->contents[$products['products_id']]['attributes'][$attributes['products_options_id']] = $attributes['products_options_value_id'];
			}
		}
		$this->cleanup();
    }

	function reset($reset_database = false) {
		global $customer_id;

		$this->contents = array();
		$this->total = 0;
		$this->weight = 0;
		$this->content_type = false;
	
		if (tep_session_is_registered('customer_id') && ($reset_database == true)) {
			tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'");
			tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "'");
			tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_SESSIONS . " where customers_id ='". (int)$customer_id . "'");
			tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_SERVICES . " where customers_id ='". (int)$customer_id . "'");
			tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTENDEES . " where customers_id ='". (int)$customer_id . "'");
		} else if (!tep_session_is_registered('customer_id') && ($reset_database == true)) {
			tep_db_query("delete from " . TABLE_CUSTOMERS_TEMP_BASKET . " where customers_id = '" . tep_session_id() . "'");
			tep_db_query("delete from " . TABLE_CUSTOMERS_TEMP_BASKET_ATTRIBUTES . " where customers_id = '" . tep_session_id() . "'");
			tep_db_query("delete from " . TABLE_CUSTOMERS_TEMP_BASKET_SESSIONS . " where customers_id ='". tep_session_id() . "'");
		} 

		$this->multiple_event_attendees=0;
		//unset($this->attendees);
		unset($this->cartID);
		if (tep_session_is_registered('cartID')) tep_session_unregister('cartID');
	}

  function add_cart($products_id, $qty = '1', $attributes = '', $notify = true,$sessions=array(),$old_orders_id=0,$tax=0,$unit_price=0,$price_breaks=0,$salemaker_id=0) {
  global $new_products_id_in_cart, $customer_id;

  $products_id_string = tep_get_uprid($products_id, $attributes);
  $products_id = tep_get_prid($products_id_string);

	$attributes_pass_check = true;
	if (is_array($attributes)) {  
		reset($attributes);  
		while (list($option, $value) = each($attributes)) {  
			if (!is_numeric($option) || !is_numeric($value)) {  
				$attributes_pass_check = false;  
				break;           
			}
		}  
	} 
  if (is_numeric($products_id) && is_numeric($qty) && ($attributes_pass_check == true)) {
 
    $check_product_query = tep_db_query("select products_status from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
    $check_product = tep_db_fetch_array($check_product_query);
 
    if (($check_product !== false) && ($check_product['products_status'] == '1')) {
      if ($notify == true) {
        $new_products_id_in_cart = $products_id;
        tep_session_register('new_products_id_in_cart');
      }

	  	// get the forced ids from the xsell_products
		$xsell_query=tep_db_query("select xsell_id  from " . TABLE_PRODUCTS_XSELL . " where products_id='" . $products_id . "' and item_type='P' and xsell_type='F'");
		$forced_ids="";
		if (tep_db_num_rows($xsell_query)){
			while($xsell_result=tep_db_fetch_array($xsell_query)){
				$forced_ids=$xsell_result["xsell_id"] . ",";
			}
			$forced_ids=substr($forced_ids,0,-1);
		}
 
	  // get the cross product_ids
      if ($this->in_cart($products_id_string)) {
        //$this->update_quantity($products_id_string, $qty, $attributes,array());
		 if(isset($this->contents[$products_id_string]['order_products_id']))
		  {
			  $prev_quantity=$this->get_quantity($products_id_string);
			  $update_quantity=$qty-$prev_quantity;
			  
			
		  }
		 $this->update_quantity($products_id_string, $qty, $attributes,array(),0,$forced_ids,$tax,$unit_price,$price_breaks,$salemaker_id);
      } else {
		$this->contents[$products_id_string] = array('qty' => $qty,'element_type'=>'P','sessions'=>array(),'old_orders_id'=>0,'answer_type'=>'','answer_value'=>'','forced_item_ids'=>$forced_ids,'discount_id'=>$salemaker_id);
       
// insert into database
if($qty=="")$qty=0;
		if (tep_session_is_registered('customer_id')){ 
			tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added,products_type,old_orders_id,forced_id,discount_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id_string) . "', '" . (int)$qty . "', now(),'P', '" . (int)$old_orders_id ."','". $forced_ids ."'," . $salemaker_id . ")");
		}else{
			tep_db_query("insert into " . TABLE_CUSTOMERS_TEMP_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added,products_type,old_orders_id,forced_id) values ('" . tep_session_id() . "', '" . tep_db_input($products_id_string) . "', '" . (int)$qty . "', now(),'P', '" . (int)$old_orders_id ."','". $forced_ids ."')");
		}
			
        if (is_array($attributes)) {
          reset($attributes);
          while (list($option, $value) = each($attributes)) {
            $this->contents[$products_id_string]['attributes'][$option] = $value;
// insert into database
		if (tep_session_is_registered('customer_id')) {
			tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id_string) . "', '" . (int)$option . "', '" . (int)$value . "')");
		}else {
			tep_db_query("insert into " . TABLE_CUSTOMERS_TEMP_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . tep_session_id() . "', '" . tep_db_input($products_id_string) . "', '" . (int)$option . "', '" . (int)$value . "')");
		}
          }
        }
      }
	  
	// print_r($this->contents[$products_id_string]) ;
 
      $this->cleanup();
 
// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
      $this->cartID = $this->generate_cart_id();
    }
  }
}

// function to add subscription to cart
function add_cart_subscription($subscription_id, $qty = '1', $attributes = '', $notify = true,$sessions=array(),$old_orders_id=0,$subscription_start_date='0000-00-00') {
  global $customer_id;
 
 	$products_id="S" . $subscription_id;
	
    $check_subscription_query = tep_db_query("select subscription_status from " . TABLE_SUBSCRIPTIONS . " where subscription_id = '" . (int)$subscription_id . "'");
    $check_subscription = tep_db_fetch_array($check_subscription_query);
	
    if (($check_subscription !== false) && ($check_subscription['subscription_status'] == '1')) {
 
 	  	// get the forced ids from the xsell_subscriptions
		$xsell_query=tep_db_query("select xsell_id  from " . TABLE_PRODUCTS_XSELL . " where products_id='" . $subscription_id . "' and item_type='S' and xsell_type='F'");
		$forced_ids="";
		if (tep_db_num_rows($xsell_query)){
			while($xsell_result=tep_db_fetch_array($xsell_query)){
				$forced_ids=$xsell_result["xsell_id"] . ",";
			}
			$forced_ids=substr($forced_ids,0,-1);
		}
      if (!$this->in_cart($products_id)) {
        $this->contents[$products_id] = array('qty' => $qty,'element_type'=>'S','sessions'=>array(),'old_orders_id'=>0,'answer_type'=>'','answer_value'=>'','forced_item_ids'=>$forced_ids,'subscription_start_date'=> $subscription_start_date);
// insert into database
        if (tep_session_is_registered('customer_id')) 
			tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added,products_type,old_orders_id,forced_id,subscription_start_date) values ('" . (int)$customer_id . "', '" . tep_db_input($subscription_id) . "', '" . (int)$qty . "', now(),'S', '0','" . $forced_ids . "','" . $subscription_start_date . "')");
		else
			tep_db_query("insert into " . TABLE_CUSTOMERS_TEMP_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added,products_type,old_orders_id,forced_id,subscription_start_date) values ('" . tep_session_id() . "', '" . tep_db_input($subscription_id) . "', '" . (int)$qty . "', now(),'S', '0','" . $forced_ids . "','" . $subscription_start_date . "')");
      }
 
      $this->cleanup();
 
// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
      $this->cartID = $this->generate_cart_id();
    }
}

// function to add service to cart
function add_cart_service($service_id, $qty = '1', $attributes = '', $notify = true,$others="") {
  global $new_products_id_in_cart, $customer_id;
  $res=array();
  $resource_id=(($others["resource_id"])?$res[1]=$others["resource_id"]:$res[1]=0);
  $service_id_string = tep_get_service_uprid($service_id,$res);
  if(tep_db_input($others["start_date"])) $start_date_string=strtotime(tep_db_input($others["start_date"]));
  if(tep_db_input($others["start_time"])) $start_time_string=strtotime(tep_db_input($others["start_time"]));
  if(tep_db_input($others["end_date"])) $end_date_string=strtotime(tep_db_input($others["end_date"]));
  if(tep_db_input($others["end_time"])) $end_time_string=strtotime(tep_db_input($others["end_time"]));
  $time_string=$start_date_string+$end_date_string+$start_time_string+$end_time_string+$qty;
  $service_id_string.="-".$time_string;
  $service_id = tep_get_service_prid($service_id_string);
  if (is_numeric($service_id) && is_numeric($qty)) { 
    $check_service_query = tep_db_query("select service_status from " . TABLE_SERVICES . " where service_id = '" . (int)$service_id . "'");
    $check_service = tep_db_fetch_array($check_service_query);
	if (($check_service !== false) && ($check_service['service_status'] == '1')) {
      if ($notify == true) {
        $new_products_id_in_cart = $service_id;
        tep_session_register('new_session_id_in_cart');
      }
 	  if ($this->in_cart($service_id_string)) {
	  	$this->contents[$service_id_string]['qty']=$qty;
		$this->contents[$service_id_string]['others']=$others;
		if (tep_session_is_registered('customer_id')) {
			tep_db_query("update " . TABLE_CUSTOMERS_BASKET_SERVICES . " set start_date = '" . tep_db_input($others["start_date"]) . "',end_date = '" . tep_db_input($others["end_date"]) . "',start_time='" . tep_db_input($others["start_time"]) . "',end_time='" . tep_db_input($others["end_time"]) . "',resource_id='". (int)tep_db_input($others["resource_id"])."' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($service_id_string) . "'");
		}
      } else {
        $this->contents[$service_id_string] = array('qty' => $qty,'element_type'=>'V','sessions'=>array(),'old_orders_id'=>0,'answer_type'=>'','answer_value'=>'','others'=>$others);
	  // insert into database
        if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added,products_type,old_orders_id) values ('" . (int)$customer_id . "', '" . tep_db_input($service_id_string) . "', '" . (int)$qty . "', now(),'V', '" . (int)$old_orders_id . "')");

        if (is_array($attributes)) {
          reset($attributes);
          while (list($option, $value) = each($attributes)) {
            $this->contents[$service_id_string]['attributes'][$option] = $value;
      // insert into database
            if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id,products_type) values ('" . (int)$customer_id . "', '" . tep_db_input($service_id_string) . "', '" . (int)$option . "', '" . (int)$value . "','V')");
          }
        }
		if (tep_session_is_registered('customer_id')) {
		 	tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_SERVICES . "(customers_id,products_id,start_date,end_date,start_time,end_time,resource_id) values (" . (int)$customer_id . ",'" . $service_id_string . "','" . $others["start_date"] . "','"  . $others["end_date"] . "','" . $others["start_time"] . "','" . $others["end_time"] . "','".(int)$others["resource_id"]."')");
		} 
      }
      $this->cleanup();
 
// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
      $this->cartID = $this->generate_cart_id();
    }
  }
}


	//updated function to add event to cart

	function add_cart_event($events_id, $sessions=array(),$old_orders_id,$answer_type,$answer_value,$no_attendees) {
		global $new_products_id_in_cart, $customer_id,$order_status;
		//$qty=sizeof($sessions);
		//add 'E' to id to avoid conflict with products
		$products_id='E' . $events_id;
		//print_r($sessions);
		if ($this->in_cart($products_id)){
			if(isset($this->contents[products_id]['order_products_id']) && ($order_status!=1)) return true;

			$added_event_fee=$this->check_sessions($products_id,$sessions);

			ksort($this->contents[$products_id]['sessions']);
			$this->contents[$products_id]['final_price']+=$added_event_fee;
			$this->contents[$products_id]['answer_type']=$answer_type;
			$this->contents[$products_id]['answer_value']=$answer_value;
			//update question answer value
			if (tep_session_is_registered('customer_id')){
				tep_db_query("UPDATE " . TABLE_CUSTOMERS_BASKET . " set answer_value='" . tep_db_input($answer_value) . "' where customers_id='" . (int)$customer_id . "' and products_id='" . (int)$events_id . "'");
				tep_db_query("DELETE from " . TABLE_CUSTOMERS_BASKET_SESSIONS . " where customers_id='" . (int)$customer_id . "' and products_id='" . (int)$events_id . "'");
			} else 	{
				tep_db_query("UPDATE " . TABLE_CUSTOMERS_TEMP_BASKET . " set answer_value='" . tep_db_input($answer_value) . "' where customers_id='" . tep_session_id() . "' and products_id='" . (int)$events_id . "'");
				tep_db_query("DELETE from " . TABLE_CUSTOMERS_TEMP_BASKET_SESSIONS . " where customers_id='" . tep_session_id() . "' and products_id='" . (int)$events_id . "'");
			}
		} else {
			//print_r($sessions);
			$this->contents[$products_id] = array(
													'qty' =>$no_attendees,
													'element_type'=>'E',
													'sessions'=>$sessions,
													'old_orders_id'=>(int)$old_orders_id,
													'answer_type'=>$answer_type,
													'answer_value'=>$answer_value,
													);
			if (tep_session_is_registered('customer_id')){
				//add to cart
				tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added,products_type,old_orders_id,answer_type,answer_value) values ('" . (int)$customer_id . "', '" . tep_db_input($events_id) . "', '" . $no_attendees . "', now(),'E','" . 
						(int)$old_orders_id . "','" . $answer_type . "','" . $answer_value . "')");
			}else {
				tep_db_query("insert into " . TABLE_CUSTOMERS_TEMP_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added,products_type,old_orders_id,answer_type,answer_value) values ('" . tep_session_id() . "', '" . tep_db_input($events_id) . "', '" . $no_attendees . "', now(),'E','" . 
						(int)$old_orders_id . "','" . $answer_type . "','" . $answer_value . "')");
			}
			if ($no_attendees>1){
				$this->multiple_event_attendees+=1;
			}
		}
		//add to sessios_cart
		if (tep_session_is_registered('customer_id')){
			reset($this->contents[$products_id]['sessions']);
			$temp=&$this->contents[$products_id]['sessions'];
			while(list($id,)=each($temp)){
				tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_SESSIONS . " (customers_id,products_id,session_id,session_type,session_group,session_date) values ('" . $customer_id . "','" . $events_id . "','" . $id . "','" . $temp[$id]['type'] . "','" . $temp[$id]['group'] . "','" . $temp[$id]['date'] . "')");
			}
		}
		else {
			reset($this->contents[$products_id]['sessions']);
			$temp=&$this->contents[$products_id]['sessions'];
			while(list($id,)=each($temp)){
				tep_db_query("insert into " . TABLE_CUSTOMERS_TEMP_BASKET_SESSIONS . " (customers_id,products_id,session_id,session_type,session_group,session_date) values ('" . tep_session_id() . "','" . $events_id . "','" . $id . "','" . $temp[$id]['type'] . "','" . $temp[$id]['group'] . "','" . $temp[$id]['date'] . "')");
			}
		}
		
		$this->cleanup();
		// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
		$this->cartID = $this->generate_cart_id();
	}    

	function check_sessions($products_id,$new_sessions){
		$sessions=&$this->contents[$products_id]['sessions'];
		$added_event_fee=0;

		reset($new_sessions);
		while(list($id,)=each($new_sessions)){


			if (!isset($sessions[$id]))
			{
			   $sessions[$id]=$new_sessions[$id];
			   $fee=$this->get_event_session_price($id);
			   $sessions[$id]['fees']=$fee;
			   $added_event_fee+=$fee; //$new_sessions[$id]['fees'];
			 }
		}
		return $added_event_fee;
		//$this->contents[$products_id]['qty']=sizeof($sessions);
	}

	function update_quantity($products_id, $quantity = '', $attributes = '',$sessions=array(),$old_orders_id=0,$forced_ids="",$tax=0,$unit_price=0,$price_breaks=0,$salemaker_id=0) {
		global $customer_id,$order_status,$HTTP_SESSION_VARS;

		if (empty($quantity)) return true; // nothing needs to be updated if theres no quantity, so we return true..
	
		if ($this->contents[$products_id]['element_type']=='E') return;

		//only pending payment can update the quantity
		if(isset($this->contents[$products_id]['order_products_id']) && ($order_status!=1)) return true;

		$products_id_string = tep_get_uprid($products_id, $attributes);
		$products_id = tep_get_prid($products_id_string);
		
		$attributes_pass_check = true;
		if (is_array($attributes)) {  
			reset($attributes);  
			while (list($option, $value) = each($attributes)) {  
				if (!is_numeric($option) || !is_numeric($value)) {  
					$attributes_pass_check = false;  
					break;           
				}
			}  
		} 
		if (is_numeric($products_id) && isset($this->contents[$products_id_string]) && is_numeric($quantity)  && ($attributes_pass_check == true)) {
			if($price_breaks=='0')
				$this->contents[$products_id_string] = array('qty' => $quantity,'element_type'=>'P','sessions'=>$sessions,'old_orders_id'=>(int)$old_orders_id,'answer_type'=>'','answer_value'=>'','forced_item_ids'=>$forced_ids,'tax'=>$tax,'final_price'=>$unit_price,'discount_id'=>$salemaker_id);
			else 
				$this->contents[$products_id_string] = array('qty' => $quantity,'element_type'=>'P','sessions'=>$sessions,'old_orders_id'=>(int)$old_orders_id,'answer_type'=>'','answer_value'=>'','forced_item_ids'=>$forced_ids,'discount_id'=>$salemaker_id);
			// update database
			
			if (tep_session_is_registered('customer_id')) {
				tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $quantity . "',discount_id='" . $salemaker_id . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id_string) . "'");
				}
			else{
				tep_db_query("update " . TABLE_CUSTOMERS_TEMP_BASKET . " set customers_basket_quantity = '" . $quantity . "' where customers_id = '" . tep_session_id() . "' and products_id = '" . tep_db_input($products_id_string) . "'");
				}
			if (is_array($attributes)) {
				reset($attributes);
				while (list($option, $value) = each($attributes)) {
					$this->contents[$products_id_string]['attributes'][$option] = $value;
					// update database
					if (tep_session_is_registered('customer_id')) {
						tep_db_query("update " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " set products_options_value_id = '" . (int)$value . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id_string) . "' and products_options_id = '" . (int)$option . "'");
						}
					else {
						tep_db_query("update " . TABLE_CUSTOMERS_TEMP_BASKET_ATTRIBUTES . " set products_options_value_id = '" . (int)$value . "' where customers_id = '" . tep_session_id() . "' and products_id = '" . tep_db_input($products_id_string) . "' and products_options_id = '" . (int)$option . "'");
						}
				}
			}
		}
		
	}

	function cleanup() {
		global $customer_id;
	
		reset($this->contents);
		while (list($key,) = each($this->contents)) {
			if ($this->contents[$key]['qty'] < 1) {
			unset($this->contents[$key]);
			// remove from database
			if (tep_session_is_registered('customer_id')) {
				tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'");
				tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'");
					if ($this->contents[$key]['element_type']=='E'){
						tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_SESSIONS . " where customers_id ='" . (int)$customer_id . "' and products_id= '" . tep_db_input(substr($key,1)) . "'");
						if ($this->contents[$key]['qty']>1){
							$events_fd=false;
							while(list($kkey,)=each($this->contents)){
								if($this->contents[$kkey]['element_type']=='E')
								$events_fd=true;
							}	
						 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTENDEES . " where customers_id ='" . (int)$customer_id . "'");
						}
					}
				} else {
				tep_db_query("delete from " . TABLE_CUSTOMERS_TEMP_BASKET . " where customers_id = '" . tep_session_id() . "' and products_id = '" . tep_db_input($key) . "'");
				tep_db_query("delete from " . TABLE_CUSTOMERS_TEMP_BASKET_ATTRIBUTES . " where customers_id = '" . tep_session_id() . "' and products_id = '" . tep_db_input($key) . "'");
				if ($this->contents[$key]['element_type']=='E')
					tep_db_query("delete from " . TABLE_CUSTOMERS_TEMP_BASKET_SESSIONS . " where customers_id ='" . tep_session_id() . "' and products_id= '" . tep_db_input(substr($key,1)) . "'");
				if ($this->contents[$key]['element_type']=='V')
					tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_SERVICES . " where customers_id ='" . (int)$customer_id . "' and products_id= '" . tep_db_input($key) . "'");

				}
				
			}
		}
	}

	function count_contents() {  // get total number of items in cart 
		$total_items = 0;
		if (is_array($this->contents)) {
			reset($this->contents);

			while (list($products_id, ) = each($this->contents)) {
				$total_items += $this->get_quantity($products_id);
			}
		}
		return $total_items;
	}

	function get_quantity($products_id_string) {
		if (isset($this->contents[$products_id_string])) {
			return $this->contents[$products_id_string]['qty'];
		} else {
			return 0;
		}
	}

	function in_cart($products_id_string) {
		if (isset($this->contents[$products_id_string])) {
			return true;
		} else {
			return false;
		}
	}

	function remove($products_id,$cart_sessions=array(),$type='',$subs_count=0) {
		global $customer_id,$order_id,$store_member;
		// remove from database
		if ($this->contents[$products_id]['element_type']=='E' || $this->contents[$products_id]['element_type']=='S') {
			$element_id=substr($products_id,1);
		} else {
			$element_id=$products_id;
		}
		if (tep_session_is_registered('customer_id')) {
		
			switch($this->contents[$products_id]['element_type']) {
				case 'P':
					tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($element_id) . "' and products_type='".$this->contents[$products_id]['element_type']."'");
					tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($element_id) . "'");
					unset($this->contents[$products_id]);
				break;
				case 'E':
					for($icnt=0;$icnt<count($cart_sessions);$icnt++) {
						$session_query=tep_db_query("select * from " . TABLE_CUSTOMERS_BASKET_SESSIONS . " where customers_id ='" . (int)$customer_id . "' and products_id= '" . $element_id . "' and session_id='" . $cart_sessions[$icnt] . "'");
						if(tep_db_num_rows($session_query)>0) {
							tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_SESSIONS . " where customers_id ='" . (int)$customer_id . "' and products_id= '" . $element_id . "' and session_id='" . $cart_sessions[$icnt] . "'");
							unset($this->contents[$products_id]['sessions'][$cart_sessions[$icnt]]);
						}
					}	
					if(tep_session_is_registered('order_id')) {
						if (isset($this->contents[$products_id]['order_products_id'])) {
							for($icnt=0;$icnt<count($cart_sessions);$icnt++) {
								$prd_id=$this->contents[$products_id]['sessions'][$cart_sessions[$icnt]]["order_products_id"];
								unset($this->contents[$products_id]['sessions'][$cart_sessions[$icnt]]);
							}
						}	
					}
					$this->get_events_final_price($products_id);
					if(count($this->contents[$products_id]['sessions'])<=0)	{
						$cart_element=$this->contents[$products_id];
						tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($element_id) . "' and products_type='".$this->contents[$products_id]['element_type']."'");
							$item_exist=false;
							reset($this->contents);
							while(list($key,)=each($this->contents)){
								if(($this->contents[$key]['element_type']=='E' || $this->contents[$key]['element_type']=='V') && $key!=$products_id) {
									$item_exist=true;
								}	
							}
							unset($this->contents[$products_id]);
							if(!$item_exist) {
								$get_member_subs_query=tep_db_query("select subscription_id from " . TABLE_SUBSCRIPTIONS . " where subscription_type='M'  ");
								if(tep_db_num_rows($get_member_subs_query)>0){
								   $member_subscription=tep_db_fetch_array($get_member_subs_query);
								   $subscription_id = $member_subscription['subscription_id'];
								   $this->remove("S".$subscription_id,'','',1);
								} 
							}
						
						
						if ($cart_element['qty']>1 && $this->multiple_event_attendees<=1){
							$events_fd=false;
							while(list($key,)=each($this->contents)){
								if($this->contents[$key]['element_type']=='E' && $this->contents[$key]['element_type']['qty']>1)
								  $events_fd=true;
							}
							if(!$events_fd){
								tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTENDEES . " where customers_id ='" . (int)$customer_id . "'");
								$this->multiple_event_attendees=0;
								$this->attendees=array();
							}
						} else if ($cart_element['qty']>1){
							$this->multiple_event_attendees-=1;
						}
					}	
				break;
				case 'V':
					tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($element_id) . "' and products_type='".$this->contents[$products_id]['element_type']."'");
					tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_SERVICES . " where customers_id ='" . (int)$customer_id . "' and products_id= '" . $element_id . "'");
							$item_exist=false;
							reset($this->contents);
							while(list($key,)=each($this->contents)){
								if(($this->contents[$key]['element_type']=='E' || $this->contents[$key]['element_type']=='V' ) && $key!=$products_id) {
									$item_exist=true;
								}	
							}
							unset($this->contents[$products_id]);
							if(!$item_exist) {
								$get_member_subs_query=tep_db_query("select subscription_id from " . TABLE_SUBSCRIPTIONS . " where subscription_type='M'  ");
								if(tep_db_num_rows($get_member_subs_query)>0){
								   $member_subscription=tep_db_fetch_array($get_member_subs_query);
								   $subscription_id = $member_subscription['subscription_id'];
								   $this->remove("S".$subscription_id,'','',1);
								} 
							}
					
				break;
				case 'S':
					$store_member=false;
					$subs_query=tep_db_query("select subscription_id from " . TABLE_SUBSCRIPTIONS . " where subscription_type='M'  ");
					if(tep_db_num_rows($subs_query)>0) {
						$subs_result=tep_db_fetch_array($subs_query);
						if($subs_result['subscription_id']==$element_id && $subs_count==0)  {
						$store_member=true;
						}
					} if(!$store_member) {
						tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($element_id) . "' and products_type='".$this->contents[$products_id]['element_type']."'");
						unset($this->contents[$products_id]);
					}	
				break;
			}
		}else{
			tep_db_query("delete from " . TABLE_CUSTOMERS_TEMP_BASKET . " where customers_id = '" . tep_session_id() . "' and products_id = '" . tep_db_input($element_id) . "' and products_type='".$this->contents[$products_id]['element_type']."'");
			tep_db_query("delete from " . TABLE_CUSTOMERS_TEMP_BASKET_ATTRIBUTES . " where customers_id = '" . tep_session_id() . "' and products_id = '" . tep_db_input($element_id) . "'");

			if ($this->contents[$products_id]['element_type']=='E'){
				tep_db_query("delete from " . TABLE_CUSTOMERS_TEMP_BASKET_SESSIONS . " where customers_id ='" . tep_session_id() . "' and products_id= '" . $element_id . "'");
				$this->attendees=array();
			}
         if ($this->contents[$products_id]['element_type']=='V')
				tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_SERVICES . " where customers_id ='" . tep_session_id() . "' and products_id= '" . $element_id . "'");

		}

	/*	if (tep_session_is_registered('order_id'))
		{
			if (isset($this->contents[$products_id]['order_products_id']))
			{
			
				switch($this->contents[$products_id]['element_type']) {
					case 'P':
						unset($this->contents[$products_id]);
					break;
					case 'E':
						for($icnt=0;$icnt<count($cart_sessions);$icnt++) {
							$prd_id=$this->contents[$products_id]['sessions'][$cart_sessions[$icnt]]["order_products_id"];
							unset($this->contents[$products_id]['sessions'][$cart_sessions[$icnt]]);
						}
						$this->get_events_final_price($products_id);
						if(count($this->contents[$products_id]['sessions'])<=0) {
							$cart_element=$this->contents[$products_id];
							unset($this->contents[$products_id]);
							
							if ($cart_element['qty']>1 && $this->multiple_event_attendees<=1){
								$events_fd=false;
								while(list($key,)=each($this->contents)){
									if($this->contents[$key]['element_type']=='E' && $this->contents[$key]['qty']>1)
									  $events_fd=true;
								} 
								if(!$events_fd){
									$this->multiple_event_attendees=0;
									$this->attendees=array();
								}
							} else if ($cart_element['qty']>1){
								$this->multiple_event_attendees-=1;
							}
						}	
					break;
					case 'V':
						unset($this->contents[$products_id]);
					break;
					case 'S':
						unset($this->contents[$products_id]);
					break;
				}
			}
		}*/
	//	print_r($this->contents[$products_id]['sessions']);
		//if($this->contents[$products_id]['element_type']!='E')
		//	unset($this->contents[$products_id]);
		// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
		$this->cartID = $this->generate_cart_id();
	}

	function remove_all() {
		$this->reset();
	}

	function get_product_id_list($only_product_ids=true) {
		$product_id_list = '';
		if (is_array($this->contents)) {
			reset($this->contents);
			while (list($products_id, ) = each($this->contents)) {
				if ($this->contents[$products_id]['element_type']=='E' || $this->contents[$products_id]['element_type']=='S' || $this->contents[$products_id]['element_type']=='V')
					$product_id_list .= ',' . substr($products_id,1);
				else
					$product_id_list .= ($only_product_ids?',' . tep_get_prid($products_id):',' . $products_id);
			}
		}
		return substr($product_id_list, 1);
	}

	function calculate($include_all=true,$whole_tot=false) {
		global $dates,$customer_country_id,$customer_zone_id,$sign,$modify_price_prefix,$sign,$purpose,$manual_option,$order_id;
		$this->total_virtual = 0; // ICW Gift Voucher System
		$this->modify_price_prefix=$modify_price_prefix;
		$this->sign=$sign;
		$this->purpose=$purpose;
		$this->manual_option=$manual_option;
		$this->total = 0;
		$this->weight = 0;
		$this->stored_total = 0;
		if (!is_array($this->contents)) return 0;
	
		$owd_costs=0;
		reset($this->contents);
		$this->mode="";
		while (list($products_id, ) = each($this->contents)) {
			$qty = $this->contents[$products_id]['qty'];
			if ($this->contents[$products_id]['element_type']=='P')
			{

			    $owd_costs+=tep_owd_costs('quantity',$this->contents[$products_id]['qty']);
				// products price
				$this->mode=($this->mode=="" || $this->mode=="P"?"P":"M");
				$product_sql = "select products_id, products_price, products_tax_class_id, products_weight from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'";
				$product_query = tep_db_query($product_sql);
				if ($product = tep_db_fetch_array($product_query)) {
				// ICW ORDER TOTAL CREDIT CLASS Start Amendment
					$no_count = 1;
					$gv_query = tep_db_query("select products_model from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
					$gv_result = tep_db_fetch_array($gv_query);
					if (ereg('^GIFT', $gv_result['products_model'])) {
						$no_count = 0;
					}
					$products_weight = $product['products_weight'];
				}	
				if(isset($this->contents[$products_id]['order_products_id']))
				{
					$this->weight += ($qty * $products_weight);			
			 if($include_all){
						
						$this->total += tep_add_tax($this->contents[$products_id]['final_price'], $this->contents[$products_id]['tax']) * $qty;
					}
					
				//	print_r($this->total);
				}
				else if(isset($this->contents[$products_id]['final_price']) && isset($this->contents[$products_id]['tax'])) //merge
				{
					$this->weight += ($qty * $products_weight);
					$this->total += tep_add_tax($this->contents[$products_id]['final_price'], $this->contents[$products_id]['tax']) * $qty;
				} //merge
				else{					
					$product_query = tep_db_query("select products_id, products_price, products_tax_class_id, products_weight,products_price_break from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
					if ($product = tep_db_fetch_array($product_query)) {
						$prid = $product['products_id'];
						$products_tax = tep_get_tax_rate($product['products_tax_class_id']);
						
						$products_weight = $product['products_weight'];
						$products_price=$this->get_product_price($products_id,$product["products_price"],0);
						//echo $products_price;										
						$this->weight_virtual += ($qty * $products_weight) * $no_count;// ICW CREDIT CLASS;
						$this->weight += ($qty * $products_weight);
					}else if($include_all){
						//$this->total += tep_add_tax($this->contents[$products_id]['final_price'], $this->contents[$products_id]['tax']) * $qty;
					}
					//else {
					// ICW ORDER TOTAL  CREDIT CLASS End Amendment
					//$this->weight_virtual += ($qty * $products_weight) * $no_count;// ICW CREDIT CLASS;
					//$this->weight += ($qty * $products_weight);
					$prid = $product['products_id'];
					$products_tax = tep_get_tax_rate($product['products_tax_class_id']);

					if ($product["products_price_break"]=="Y"){
						$check_product_break_sql = "select * from " . TABLE_PRODUCTS_PRICE_BREAK . " where products_id='" . $product['products_id'] . "' and quantity='" . $qty . "'";
						$check_product_break_query = tep_db_query($check_product_break_sql);
						if(tep_db_num_rows($check_product_break_query) > 0){
							$check_product_break_result = tep_db_fetch_array($check_product_break_query);
							$products_price = $product['products_price'] - $check_product_break_result['discount_per_item'];
						}
					} else {
						if ($this->contents[$products_id]['discount_id']>0) {
	
							$product_to_categories_query = tep_db_query("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$prid . "'");
							$product_to_categories = tep_db_fetch_array($product_to_categories_query);
							$category = $product_to_categories['categories_id'];
	
							$sale_option=" sale_id=" . $this->contents[$products_id]['discount_id'] .' and ' ;
							$sDate=getServerDate();
							$sale_query = tep_db_query("select sale_specials_condition, sale_deduction_value, sale_deduction_type,sale_id,sale_discount_type from " . TABLE_SALEMAKER_SALES . " where " . $sale_option . " ((sale_categories_all='' and sale_products_selected='') or sale_categories_all like '%," . $category . ",%' or sale_products_selected like '%," . (int)$prid .",%') and sale_status = '1' and (sale_date_start <= '" . $sDate. "' or sale_date_start = '0000-00-00') and (sale_date_end >= '" . $sDate . "' or sale_date_end = '0000-00-00') and (sale_pricerange_from <= '" . $product["products_price"] . "' or sale_pricerange_from = '0') and (sale_pricerange_to >= '" . $product["products_price"] . "' or sale_pricerange_to = '0')");
							if (tep_db_num_rows($sale_query)>0){
								$sale=tep_db_fetch_array($sale_query);
								$products_price=tep_get_salemaker_price($this->contents[$products_id]['special_price'],$products_price,$sale);
							}
						}
					}
					if (isset($this->contents[$products_id]['attributes'])) {
						reset($this->contents[$products_id]['attributes']);
						while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
							$attribute_price_query = tep_db_query("select options_values_price, price_prefix, products_attributes_weight
							from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$prid . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");
							$attribute_price = tep_db_fetch_array($attribute_price_query);
							if ($attribute_price['price_prefix'] == '-') {
								$products_price -= $attribute_price['options_values_price'];
							} else {
								$products_price += $attribute_price['options_values_price'];
							}
							$this->weight += ($qty*$attribute_price['products_attributes_weight']);
							$this->weight_virtual += ($qty * $attribute_price['products_attributes_weight']) * $no_count;// ICW CREDIT CLASS;
						}
					}
					//echo $products_price;
					//}
					
					$this->total_virtual += tep_add_tax($products_price, $products_tax) * $qty * $no_count;// ICW CREDIT CLASS;
					$this->total += tep_add_tax($products_price, $products_tax) * $qty;
					
					//print_r ($this);
					
				}

		  } elseif ($this->contents[$products_id]['element_type']=='S'){
  				if(isset($this->contents[$products_id]['order_products_id']))
				{
					if($include_all)
						$this->total += tep_add_tax($this->contents[$products_id]['final_price'], $this->contents[$products_id]['tax']) * $qty;
				}
				else
				{
					$this->mode=($this->mode=="" || $this->mode=="S"?"S":"M");
					// subscription price
					$subscription_query = tep_db_query("select subscription_id, subscription_costs, subscription_tax_class_id from " . TABLE_SUBSCRIPTIONS . " where subscription_id = '" . (int)substr($products_id,1) . "'");
					if ($subscription = tep_db_fetch_array($subscription_query)) {
						$suid = $subscription['subscription_id'];
						$subscription_tax = tep_get_tax_rate($subscription['subscription_tax_class_id']);
						$subscription_costs = $this->get_subscription_price($suid,$subscription['subscription_costs']);
						//subscription discount start
						$subscription_costs = $subscription_costs - subscription_discount($suid,$this->contents[$products_id]['subscription_start_date'],$subscription['subscription_costs']);
						//end
						$subscription_weight = $subscription['subscription_weight'];
	
						$this->total_virtual += tep_add_tax($subscription_costs, $subscription_tax);
						$this->total += tep_add_tax($subscription_costs, $subscription_tax) * $qty;
					}
				}
	      } else if ($this->contents[$products_id]['element_type']=='V'){
   				if(isset($this->contents[$products_id]['order_products_id']))
				{
					if($include_all)
						$this->total += tep_add_tax($this->contents[$products_id]['final_price'], $this->contents[$products_id]['tax']) * $qty;
				}
				else
				{
					// service price
					$prid=tep_get_service_prid($products_id);
					$services_query = tep_db_query("select service_id, service_costs,include_resource_price, service_tax_class_id,service_total_time,booking_type from " . TABLE_SERVICES . " where service_id = '" . $prid . "'");
					if ($services = tep_db_fetch_array($services_query)) {
						$service_tax_id=$services["service_tax_class_id"];
						
						if($this->contents[$products_id]['others']['resource_id'] && $services['include_resource_price']=='Y' && $this->contents[$products_id]['others']['resource_costs'])
							$service_tax_id=tep_get_resource('resource_tax_class_id',(int)$this->contents[$products_id]['others']['resource_id'],$languages_id);
							$service_tax=tep_get_tax_rate($service_tax_id);
						 //service discount start
						$dates['start_date']=$this->contents[$products_id]['others']['start_date'];
						$dates['end_date']=$this->contents[$products_id]['others']['end_date'];
						$dates['start_time']=$this->contents[$products_id]['others']['start_time'];
						$dates['end_time']=$this->contents[$products_id]['others']['end_time'];
						$dates['service_costs']=$this->get_service_price($products_id,$services,$service_tax);
						$service_price=$this->get_service_price($products_id,$services,$service_tax)-services_discount($prid);
						//end
						//$service_price=$this->get_service_price($products_id,$services,$service_tax);
						
						
						$this->total_virtual += tep_add_tax($service_price*$qty,$service_tax) * $no_count;// ICW CREDIT CLASS;
						$this->total += tep_add_tax($service_price*$qty,$service_tax);
					}
				}
			}elseif ($this->contents[$products_id]['element_type']=='E') {
			  	if(isset($this->contents[$products_id]['order_products_id']))
				{
					if($include_all)
						$this->total += tep_add_tax($this->contents[$products_id]['final_price'], $this->contents[$products_id]['tax']) * $qty;
				}
				else
				{
					$this->mode=($this->mode=="" || $this->mode=="E"?"E":"M");
					$result_query = tep_db_query("select events_id,events_fees,session_style,waitfee_vip,waitfee_custom,waitfee_firstcome,events_tax_class_id from " . TABLE_EVENTS . " where events_id='" . (int)substr($products_id,1) . "'");
					$result = tep_db_fetch_array($result_query);
					
					$events_tax = tep_get_tax_rate($result['events_tax_class_id']);
					$total_fees=$this->get_event_price($products_id,$result,$events_tax);
					
					$this->total_virtual += tep_add_tax($total_fees*$qty,$events_tax);
					$this->total += tep_add_tax($total_fees*$qty,$events_tax);

				}
				
			}	
    }
	//manual price adjustment
	if($whole_tot==false){
	         if($this->manual_option=='Y'){
					$this->total+=$modify_price_prefix;
			  }	
			  }
			  
			//  print_r($this);
		if ($owd_costs>0){
			$owd_costs+=tep_owd_costs('packing',$this->weight);
			$owd_costs+=tep_owd_costs('order');
			$owd_costs+=tep_owd_costs('overweight',$this->weight);
			$this->total+=$owd_costs;
			$this->total_virtual+=$owd_costs;
			$this->stored_total+=$owd_costs;
		}
   }
   function show_net_total($ww=false)
	{
	//if ww true - displays all tot  - if ww false tot will display according to manual price adjustment
      $this->calculate(true,$ww);
      return $this->total;
	}
	function attributes_price($products_id) {
		$attributes_price = 0;
		if (isset($this->contents[$products_id]['attributes'])) {
			if ($this->contents[$products_id]['element_type']=="V"){
				$prid=tep_get_service_prid($products_id);
				// get the service details
				reset($this->contents[$products_id]['attributes']);
				while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
					$attribute_price_query = tep_db_query("select options_values_price, price_prefix 
					from " . TABLE_SERVICES_ATTRIBUTES . " where service_id = '" . $prid . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");

					$attribute_price = tep_db_fetch_array($attribute_price_query);
					if ($attribute_price['price_prefix'] == '-') {
						$attributes_price -= $attribute_price['options_values_price'];
					} else {
						$attributes_price += $attribute_price['options_values_price'];
					}
				}
			} else {
				reset($this->contents[$products_id]['attributes']);
				while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
					$attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");
					$attribute_price = tep_db_fetch_array($attribute_price_query);
					//DiscountSpecials start
					  global $customer_id;
/*					  $query = tep_db_query("select g.customers_groups_discount from " . TABLE_CUSTOMERS_GROUPS . " g inner join  " . TABLE_CUSTOMERS  . " c on g.customers_groups_id = c.customers_groups_id and c.customers_id = '" . $customer_id . "'");
					  $query_result = tep_db_fetch_array($query);
					  $customers_groups_discount = $query_result['customers_groups_discount'];
					  $query = tep_db_query("select customers_discount from " . TABLE_CUSTOMERS . " where customers_id =  '" . $customer_id . "'");
					  $query_result = tep_db_fetch_array($query);
					  $customer_discount = $query_result['customers_discount'];
					  $customer_discount = $customer_discount + $customers_groups_discount;
					  if ($customer_discount >= 0) {
						 $attribute_price['options_values_price'] = $attribute_price['options_values_price'] + $attribute_price['options_values_price'] * abs($customer_discount) / 100;
					  } else {
						 $attribute_price['options_values_price'] = $attribute_price['options_values_price'] - $attribute_price['options_values_price'] * abs($customer_discount) / 100;
					  }
*/				   //DiscountSpecials end
					if ($attribute_price['price_prefix'] == '-') {
						$attributes_price -= $attribute_price['options_values_price'];
					} else {
						$attributes_price += $attribute_price['options_values_price'];
					}
				}
			} // products_type
		}
		return $attributes_price;
	}




    function get_products() {
		global $languages_id, $dates,$customer_id,$customer_country_id,$customer_zone_id,$currencies;
		if (!is_array($this->contents)) return false;
		//print_r($this->contents);
		$products_array = array();
		reset($this->contents);
		while (list($products_id, ) = each($this->contents)) {
			if ($this->contents[$products_id]['element_type']=='P')
       		{
				 $discount_text='';
				 $discount_whole_text='';
				$products_query = tep_db_query("select p.products_id, p.products_price,pd.products_name, p.products_model, p.products_image_1,p.products_title_1,p.products_price, p.products_weight, p.products_tax_class_id,p.products_sku , cd.categories_name,p.product_type,p.products_price_break,cd.categories_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES_DESCRIPTION ." cd, " . TABLE_PRODUCTS_TO_CATEGORIES . " pc where p.products_id = '" . (int)$products_id . "' and pc.products_id = p.products_id and pc.categories_id = cd.categories_id and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "' and cd.language_id='" . (int)$languages_id . "'");
				//echo "select p.products_id, pd.products_name, p.products_model, p.products_image_1,p.products_title_1,p.products_price, p.products_weight, p.products_tax_class_id,p.products_sku , cd.categories_name,p.product_type from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES_DESCRIPTION ." cd, " . TABLE_PRODUCTS_TO_CATEGORIES . " pc where p.products_id = '" . (int)$products_id . "' and pc.products_id = p.products_id and pc.categories_id = cd.categories_id and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "' and cd.language_id='" . (int)$languages_id . "'";
				//echo "select p.products_id, pd.products_name, p.products_model, p.products_image_1,p.products_title_1,p.products_price, p.products_weight, p.products_tax_class_id,p.products_sku , cd.categories_name,p.product_type from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES_DESCRIPTION ." cd, " . TABLE_PRODUCTS_TO_CATEGORIES . " pc where p.products_id = '" . (int)$products_id . "' and pc.products_id = p.products_id and pc.categories_id = cd.categories_id and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "' and cd.language_id='" . (int)$languages_id . "'";
			//	echo "select p.products_id, pd.products_name, p.products_model, p.products_image_1,p.products_title_1,p.products_price, p.products_weight, p.products_tax_class_id,p.products_sku , cd.categories_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES_DESCRIPTION ." cd, " . TABLE_PRODUCTS_TO_CATEGORIES . " pc where p.products_id = '" . (int)$products_id . "' and pc.products_id = p.products_id and pc.categories_id = cd.categories_id and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "' and cd.language_id='" . (int)$languages_id . "'";
				if ($products = tep_db_fetch_array($products_query)) {
					$prid = $products['products_id'];
					if(isset($this->contents[$products_id]['order_products_id']))
					{
						$products_price = $this->contents[$products_id]['price'];
						$original_price=$this->contents[$products_id]['price'];
				   		$products_tax=$this->contents[$products_id]['tax'];
						$products_name=$this->contents[$products_id]['name'];
						$products_model=$this->contents[$products_id]['model'];
						$products_sku=$this->contents[$products_id]['sku'];
						$final_price=$this->contents[$products_id]['final_price'];
						$support_packs_type=$this->contents[$products_id]['support_packs_type']; //Support Packs
						$order_products_id=$this->contents[$products_id]['order_products_id'];
					}else {
						
						//$original_price=$products["products_price"];
						$original_price=$products_price = $this->get_product_price($products_id,$products["products_price"],0);
						if(isset($this->contents[$products_id]['tax']) && isset($this->contents[$products_id]['final_price'])) //any updateion of products
						{
							$products_tax=$this->contents[$products_id]['tax'];
							$final_price =$this->contents[$products_id]['final_price'];
						} 
						else 
						{
							$products_tax = tep_get_tax_rate($products['products_tax_class_id']);
							$sale_discount_type=$this->contents[$products_id]['special_price']?'S':'';
							if ($products["products_price_break"]=="Y"){
								$check_product_break_sql = "select * from " . TABLE_PRODUCTS_PRICE_BREAK . " where products_id='" . $products_id . "' and quantity='" . $this->contents[$products_id]['qty'] . "'";
								$check_product_break_query = tep_db_query($check_product_break_sql);
		
								if(tep_db_num_rows($check_product_break_query) > 0){
									$check_product_break_result = tep_db_fetch_array($check_product_break_query);
									$products_price = $products['products_price'] - $check_product_break_result['discount_per_item'];
								}
							} else if ($this->contents[$products_id]['discount_id']>0){
								$sale_option=" sale_id=" . $this->contents[$products_id]['discount_id'] . " and ";
								$sDate=getServerDate();
								$sale_query = tep_db_query("select sale_specials_condition, sale_deduction_value, sale_deduction_type,sale_id,sale_discount_type,choice_text from " . TABLE_SALEMAKER_SALES . " where " . $sale_option . " ((sale_categories_all='' and sale_products_selected='') or sale_categories_all like '%," . $products["categories_id"] . ",%' or sale_products_selected like '%," . (int)$prid .",%') and sale_status = '1' and (sale_date_start <= '" . $sDate. "' or sale_date_start = '0000-00-00') and (sale_date_end >= '" . $sDate . "' or sale_date_end = '0000-00-00') and (sale_pricerange_from <= '" . $products["products_price"] . "' or sale_pricerange_from = '0') and (sale_pricerange_to >= '" . $products["products_price"] . "' or sale_pricerange_to = '0')");
								if (tep_db_num_rows($sale_query)>0){
									$sale=tep_db_fetch_array($sale_query);
									$products_price=tep_get_salemaker_price($this->contents[$products_id]['special_price'],$products_price,$sale);
									$sale_discount_type=$sale['sale_discount_type'];
	
									switch($sale['sale_deduction_type']){
										case 1:
											$discount_text=$sale["choice_text"] . " (" . number_format($sale["sale_deduction_value"],2). "%)";
											break;
										case 2:
											$discount_text=$sale["choice_text"] . " (<s><font color='red'>" . $currencies->format(tep_add_tax($original_price, $products_tax)) . "</font></s>&nbsp;" . $currencies->format($sale["sale_deduction_value"], $products_tax) . ")";
											break;
										default:
											$discount_text=$sale["choice_text"] . " (" . $currencies->format($sale["sale_deduction_value"],$products_tax). ")";
											break;
										
									}
									$discount_whole_text=TEXT_DISCOUNT_APPLIED . '&nbsp;' . $discount_text .'<br>' . TEXT_ORIGINAL_AMOUNT . '&nbsp;' . $currencies->format(tep_add_tax($original_price,$products_tax)*$this->contents[$products_id]['qty']) . '<br>' . TEXT_DISCOUNT . '&nbsp;' .$currencies->format(tep_add_tax($original_price-$products_price,$products_tax)*$this->contents[$products_id]['qty']) . '<br>';
								}
							}

							//else $products_price = $this->get_product_price($prid,$products['products_price']);

							$select_country_query= tep_db_query("select entry_country_id from " . TABLE_ADDRESS_BOOK . " where customers_id = " .(int)$customer_id);
							/*if(tep_db_num_rows($select_country_query)){
								while($customer_country_array = tep_db_fetch_array($select_country_query))
									  $customer_country_id = $customer_country_array['entry_country_id'];
							}*/
							$final_price = ($products_price + $this->attributes_price($products_id));
						}
						$products_name=$products['products_name'];
						$products_model=$products['products_model'];
						$products_sku=$products['products_sku'];
						$support_packs_type=$products['product_type'];//Support Packs
						$order_products_id=0;
					}
					$products_weight=$products['products_weight'];
					$products_taxclass=$products['products_tax_class_id'];
					$products_cat=$products['categories_name'];					
                    //DiscountSpecials end
					$products_array[] = array(	'element_type'=>$this->contents[$products_id]['element_type'],
												'old_orders_id'=>(int)$this->contents[$products_id]['old_orders_id'],
												'answer_type'=>$this->contents[$products_id]['answer_type'],
												'answer_value'=>$this->contents[$products_id]['answer_value'],
												'id' => $prid,
												'sh_id'=>$products_id,
												'sessions'=>array(),
												'forced_item_ids'=>$this->contents[$products_id]['forced_item_ids'],
												'name' => $products_name,
												'model' => $products_model,
												'image' => $products['products_image_1'],
												'price' => ($original_price + $this->attributes_price($products_id)),
												'tax' => $products_tax,
												'quantity' => $this->contents[$products_id]['qty'],
												'weight' => $products_weight,
												'final_price' => $final_price,
												'tax_class_id' => $products_taxclass,
												'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : ''),
												'sku'=>$products_sku,
												'order_products_id'=>$order_products_id,
												'category_name' => $products_cat,
												'support_packs_type' =>$support_packs_type, //Support Packs
												'discount_id'=>$this->contents[$products_id]['discount_id'],
												'discount_type'=>$sale_discount_type,
												'discount_text'=>$discount_text,
												'discount_whole_text'=>$discount_whole_text,
												);
												
				}
        	} else if ($this->contents[$products_id]['element_type']=='S'){
				$subscription_query = tep_db_query("select su.subscription_id, sud.subscription_name, su.subscription_image, su.subscription_costs, su.subscription_tax_class_id,su.subscription_type, scd.subscription_categories_name from " . TABLE_SUBSCRIPTIONS . " su, " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud," . TABLE_SUBSCRIPTION_CATEGORIES . " sc," . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " scd," . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " ssc where su.subscription_id = '" . (int)substr($products_id,1) . "' and sc.subscription_categories_id=scd.subscription_categories_id and sc.subscription_categories_id=ssc.subscription_categories_id and ssc.subscription_id='" . (int)substr($products_id,1) . "' and sud.subscription_id = su.subscription_id and sud.language_id = '" . (int)$languages_id . "' and scd.language_id='" . (int)$languages_id . "'");
				//$subscription_query = tep_db_query("select su.subscription_id, sud.subscription_name, su.subscription_image, su.subscription_costs, su.subscription_tax_class_id,su.subscription_type from " . TABLE_SUBSCRIPTIONS . " su, " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud where su.subscription_id = '" . (int)substr($products_id,1) . "' and sud.subscription_id = su.subscription_id and sud.language_id = '" . (int)$languages_id . "'");
				if ($subscription = tep_db_fetch_array($subscription_query)) {
						$prid = $subscription['subscription_id'];
						if(isset($this->contents[$products_id]['order_products_id']))
						{
							$subs_name=$this->contents[$products_id]['name'];
							$subscription_costs = $this->contents[$products_id]['price'];
							$subscription_tax=$this->contents[$products_id]['tax'];
							$order_products_id=$this->contents[$products_id]['order_products_id'];
						}
						else
						{
							$subs_name=$subscription['subscription_name'];
							$subscription_costs = $this->get_subscription_price($prid,$subscription['subscription_costs']);
							//subscription discount start
							$subscription_costs = $subscription_costs - subscription_discount($prid,$this->contents[$products_id]['subscription_start_date'],$subscription['subscription_costs']);
							//end
							$subscription_tax=tep_get_tax_rate($subscription['subscription_tax_class_id']);
							$order_products_id=0;
						}
						
						$products_array[] = array(	'element_type'=>$this->contents[$products_id]['element_type'],
													'old_orders_id'=>(int)$this->contents[$products_id]['old_orders_id'],
													'answer_type'=>$this->contents[$products_id]['answer_type'],
													'answer_value'=>$this->contents[$products_id]['answer_value'],
													'id' => substr($products_id,1),
													'sh_id'=>$products_id,
													'sessions'=>array(),
													'subscription_start_date'=>$this->contents[$products_id]['subscription_start_date'],
													'forced_item_ids'=>$this->contents[$products_id]['forced_item_ids'],
													'name' => $subs_name,
													'model' => '',
													'image' => $subscription['subscription_image'],
													'price' => $subscription_costs,
													'quantity' =>1,
													'tax' => $subscription_tax,
													'weight'=>1,
													'final_price' => $subscription_costs,
													'subscription_type'=>$subscription["subscription_type"],
													'tax_class_id' => $subscription['subscription_tax_class_id'],
													'sku'=>'',
													'support_packs_type'=>'',//Support Packs
													'order_products_id'=>$order_products_id,
													'category_name' => $subscription['subscription_categories_name'],
													'attributes' =>'');
					}
			} else if ($this->contents[$products_id]['element_type']=='V'){
				// service price
				$prid=tep_get_service_prid($products_id);
				$services_query = tep_db_query("select se.service_id, sed.service_name,se.service_image,se.service_costs, se.include_resource_price,se.service_tax_class_id,se.service_total_time,se.booking_type,scd.services_categories_name from " . TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION . " sed," . TABLE_SERVICES_CATEGORIES . " sc," . TABLE_SERVICES_CATEGORIES_DESCRIPTION . " scd," . TABLE_SERVICES_TO_SERVICES_CATEGORIES . " ssc where se.service_id = '" . $prid . "' and ssc.service_id='" . $prid . "' and se.service_id=sed.service_id and sc.services_categories_id=scd.services_categories_id and sc.services_categories_id=ssc.services_categories_id and sed.language_id='" . $languages_id ."' and scd.language_id='" . (int)$languages_id . "'");
				//$services_query = tep_db_query("select se.service_id, sed.service_name,se.service_image, se.service_costs, se.include_resource_price,se.service_tax_class_id,se.service_total_time,se.booking_type from " . TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION . " sed where se.service_id = '" . $prid . "' and se.service_id=sed.service_id and sed.language_id='" . $languages_id ."'");
				if ($services = tep_db_fetch_array($services_query)) {
					$service_tax_id=$services["service_tax_class_id"];
					if(isset($this->contents[$products_id]['order_products_id']))
					{
						$service_cost=$this->contents[$products_id]["price"];
						$service_name=$this->contents[$products_id]['name'];
						$service_price=$this->contents[$products_id]['final_price'];
						$service_tax=$this->contents[$products_id]['tax'];
						$order_products_id=$this->contents[$products_id]['order_products_id'];						
					}
					else
					{
						$service_cost=$services["service_costs"];
						$service_name=$services['service_name'];
						$service_tax=tep_get_tax_rate($service_tax_id);
						//service discount start
						$dates['start_date']=$this->contents[$products_id]['others']['start_date'];
						$dates['end_date']=$this->contents[$products_id]['others']['end_date'];
						$dates['start_time']=$this->contents[$products_id]['others']['start_time'];
						$dates['end_time']=$this->contents[$products_id]['others']['end_time'];						
						$dates['service_costs']=$this->get_service_price($products_id,$services,$service_tax);
						$service_price=$this->get_service_price($products_id,$services,$service_tax)-services_discount($prid);	
						//end
						$order_products_id=0;						
					}					
					$products_array[] = array(	'element_type'=>$this->contents[$products_id]['element_type'],
												'old_orders_id'=>(int)$this->contents[$products_id]['old_orders_id'],
												'id' =>$prid,
												'sh_id'=>$products_id,
												'name' =>$service_name ,
												'image' => $services['service_image'],
												'price' => $service_cost,
												'others'=> $this->contents[$products_id]['others'],
												'quantity' =>$this->contents[$products_id]['qty'],
												'final_price' => $service_price,
												'tax_class_id' => $service_tax_id,
												'sku'=>'',
												'support_packs_type'=>'',//Support Packs
												'tax'=>$service_tax,
												'order_products_id'=>$order_products_id,
												'category_name' => $services['services_categories_name'],
												'attributes'=>$this->contents[$products_id]['attributes']
												);
				}
				
			}elseif ($this->contents[$products_id]['element_type']=='E'){
				$result_query = tep_db_query("select e.events_id,ed.events_name,e.session_style,e.events_image,e.events_fees,e.waitfee_vip,e.waitfee_firstcome,e.waitfee_custom,e.events_tax_class_id,ecd.events_categories_name from events e, events_description ed,events_categories ec,events_categories_description ecd,events_to_events_categories eec where e.events_id='" . (int)substr($products_id,1) . "' and eec.events_id='" . (int)substr($products_id,1) . "' and ec.events_categories_id=ecd.events_categories_id and ec.events_categories_id=eec.events_categories_id and e.events_id=ed.events_id and ed.language_id='" . (int)$languages_id . "' and ecd.language_id='" . (int)$languages_id . "'");
				$result = tep_db_fetch_array($result_query);
				if(isset($this->contents[$products_id]['order_products_id']))
				{
					$events_tax = $this->contents[$products_id]['tax'];
					$total_fees=$this->contents[$products_id]['final_price'];
					$events_name=$this->contents[$products_id]['name'];
				}
				else
				{
					$events_tax = tep_get_tax_rate($result['events_tax_class_id']);
					$total_fees=$this->get_event_price($products_id,$result,$events_tax);
					$events_name=$result['events_name'];
				}
				$products_array[] = array(	'element_type'=>$this->contents[$products_id]['element_type'],
											'answer_type'=>$this->contents[$products_id]['answer_type'],
											'answer_value'=>$this->contents[$products_id]['answer_value'],
											'id' => substr($products_id,1),
											'sh_id'=>$products_id,
											'sessions'=>$this->contents[$products_id]['sessions'],
											'forced_item_ids'=>$this->contents[$products_id]['forced_item_ids'],
											'name' => $events_name,
											'model' => '',
											'order_products_id'=>isset($this->contents[$products_id]['order_products_id'])?$this->contents[$products_id]['order_products_id']:'0',
											'image' => $result['events_image'],
											'price' =>  $total_fees,
											//'price'=>$this->contents[$products_id]['price'],
											'quantity' => $this->contents[$products_id]['qty'],
											'weight' => '1',
											'final_price' => $total_fees,
											'tax'=>$events_tax,
											'tax_class_id' => $result['events_tax_class_id'],
											'old_orders_id'=>(int)$this->contents[$products_id]['old_orders_id'],
											'sku'=>'',
											'support_packs_type'=>'', //Support Packs
											'category_name' => $result['events_categories_name'],
											'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : ''));
			}
		}
	return $products_array;
	}

	function get_product_price($products_id,$products_price,$salemaker_id=0){
	  global $customer_id;
		$query = tep_db_query("select g.customers_groups_discount from " . TABLE_CUSTOMERS_GROUPS . " g inner join  " . TABLE_CUSTOMERS  . " c on g.customers_groups_id = c.customers_groups_id and c.customers_id = '" . $customer_id . "'");
		$query_result = tep_db_fetch_array($query);
		$customers_groups_discount = $query_result['customers_groups_discount'];
		$query = tep_db_query("select customers_discount from " . TABLE_CUSTOMERS . " where customers_id =  '" . $customer_id . "'");
		$query_result = tep_db_fetch_array($query);
		$customer_discount = $query_result['customers_discount'];
		$customer_discount = $customer_discount + $customers_groups_discount;
		if ($customer_discount >= 0) {
		 $products_price = $products_price + $products_price * abs($customer_discount) / 100;
		} else {
		 $products_price = $products_price - $products_price * abs($customer_discount) / 100;
		 
		}
		if ($special_price = tep_get_products_spl_price($products_id,$salemaker_id,$products_id)) {
			$this->contents[$products_id]['special_price']=$special_price;
			$products_price = $special_price;
		}
		return $products_price;
	}

	function get_subscription_price($subscription_id,$subscription_price){
	  global $customer_id;
		$query = tep_db_query("select g.customers_groups_discount from " . TABLE_CUSTOMERS_GROUPS . " g inner join  " . TABLE_CUSTOMERS  . " c on g.customers_groups_id = c.customers_groups_id and c.customers_id = '" . $customer_id . "'");
		$query_result = tep_db_fetch_array($query);
		$customers_groups_discount = $query_result['customers_groups_discount'];
		$query = tep_db_query("select customers_discount from " . TABLE_CUSTOMERS . " where customers_id =  '" . $customer_id . "'");
		$query_result = tep_db_fetch_array($query);
		$customer_discount = $query_result['customers_discount'];
		$customer_discount = $customer_discount + $customers_groups_discount;
		if ($customer_discount >= 0) {
		 $subscription_price = $subscription_price + $subscription_price * abs($customer_discount) / 100;
		} else {
		 $subscription_price = $subscription_price - $subscription_price * abs($customer_discount) / 100;
		}
		return $subscription_price;
	}

	function get_event_price($events_id,$event,&$events_tax){
		$sessions=&$this->contents[$events_id]['sessions'];

		reset($sessions);
		$total_fees=0;
		$waitlist_stored=false;
		$reminder_amount=false;
		while(list($id,)=each($sessions)){
			$events_fees=0;
			if ($sessions[$id]['type']=='R'){ 					//reserved user
				//$days_differ=tep_count_days(date('Y-m-d'),$sessions[$id]['date']);
				$events_fees=$event['events_fees']-tep_get_discount_price($event["events_id"],$event['events_fees'],$sessions[$id]['date']);
				$this->contents[$events_id]['sessions'][$id]['events_fees']=$events_fees;
			} else if ($sessions[$id]['type']=='W'){				//waitlist user
				if (!$waitlist_stored){
					switch($sessions[$id]['group']){
						case 'V':
							$events_fees=$event['waitfee_vip'];	//vip group
							break;
						case 'F':
							$events_fees=$event['waitfee_firstcome']; //firstcome group
							break;
						case 'C':
							$events_fees=$event['waitfee_custom'];	//custome group
					}
					if ($event['session_style']=='E') $waitlist_stored=true;
				}
				$this->contents[$events_id]['sessions'][$id]['events_fees']=$event['events_fees'];
			} else {
				if (!$reminder_amount){
					$events_fees=tep_get_orders_reminder_details($this->contents[$events_id]['old_orders_id'],$event["events_id"],$event["session_style"]);
					$events_tax=$amount_details['tax'];
					$events_fees=$amount_details['amount'];
					$reminder_amount=true;
				}
				$this->contents[$events_id]['sessions'][$id]['events_fees']=$event['events_fees'];
			}
			if ($sessions[$id]['type']!='U') $this->contents[$events_id]['sessions'][$id]['fees']=$events_fees;
			else $this->contents[$events_id]['sessions'][$id]['fees']=0;
			$total_fees+=$events_fees;
		}
		return $total_fees;
	}

	function get_event_session_price($session_id,$session_group=''){
		$total_fees=0;
		$waitlist_stored=false;
		$reminder_amount=false;
		$result_query = tep_db_query("select e.events_id,e.session_style,e.events_fees,e.waitfee_vip,e.waitfee_firstcome,e.waitfee_custom,e.events_tax_class_id,es.start_date,es.sessions_type from events e, events_sessions es  where e.events_id=es.events_id and es.sessions_id='" . (int)$session_id . "'");
		$event = tep_db_fetch_array($result_query);
		$events_fees=0;
		if ($event['sessions_type']=='R'){ 					//reserved user
				//$days_differ=tep_count_days(date('Y-m-d'),$sessions[$id]['date']);
				$events_fees=$event['events_fees']-tep_get_discount_price($event["events_id"],$event['events_fees'],$event['start_date']);
				$this->contents[$events_id]['sessions'][$session_id]['events_fees']=$events_fees;
		} else if ($event['sessions_type']=='W'){				//waitlist user
				if (!$waitlist_stored){
					switch($session_group){
						case 'V':
							$events_fees=$event['waitfee_vip'];	//vip group
							break;
						case 'F':
							$events_fees=$event['waitfee_firstcome']; //firstcome group
							break;
						case 'C':
							$events_fees=$event['waitfee_custom'];	//custome group
					}
					if ($event['session_style']=='E') $waitlist_stored=true;
				}
				$this->contents[$events_id]['sessions'][$session_id]['events_fees']=$event['events_fees'];
		} else {
				if (!$reminder_amount){
					$events_fees=tep_get_orders_reminder_details($this->contents[$events_id]['old_orders_id'],$event["events_id"],$event["session_style"]);
					$events_tax=$amount_details['tax'];
					$events_fees=$amount_details['amount'];
					$reminder_amount=true;
				}
				$this->contents[$events_id]['sessions'][$session_id]['events_fees']=$event['events_fees'];
		}
		if ($event['sessions_type']!='U') $this->contents[$events_id]['sessions'][$session_id]['fees']=$events_fees;
		else $this->contents[$events_id]['sessions'][$session_id]['fees']=0;
		$total_fees+=$events_fees;
		return $total_fees;
	}
	
	function get_service_price($service_id,$services,&$service_tax_id){
		global $start_date,$end_date,$customer_country_id,$customer_zone_id;
		$total=0;
		$others=$this->contents[$service_id]["others"];				
		$option_price="";
		$start_date=$others['start_date'];
		$end_date=$others['end_date'];
		$loading_price_dates=get_loading_array((int)$services["service_id"]); 
	   
	  if($this->contents[$service_id]['attributes']){
		$attrib_list=$this->contents[$service_id]['attributes'];
		while (list($option, $value) = each($attrib_list)) {
			$attribute_price_query = tep_db_query("select options_values_price
			from " . TABLE_SERVICES_ATTRIBUTES . " where service_id = '" . (int)$services["service_id"] . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");
			$attribute_price = tep_db_fetch_array($attribute_price_query);
			$option_price+=$attribute_price['options_values_price'];
		}
	  } 
	  if($services['include_resource_price']=='Y'){
			$resource_query=tep_db_query("select resource_costs,resource_tax_class_id from ".TABLE_RESOURCES ." where resource_id='".(int)$others["resource_id"]."'");
			if($resources = tep_db_fetch_array($resource_query)){
				$loading_dates=array('loading_price_dates'=>$loading_price_dates,'start_date'=>$others["start_date"],'end_date'=>$others["end_date"],'start_time'=>$others["start_time"],'end_time'=>$others["end_time"]);
				$final_price=tep_calc_service_option_cost($services,$others["start_date"] . " " . $others["start_time"],$others["end_date"] . " " . ($others["end_time"]),$option_price,$resources["resource_costs"],$loading_dates);
				$service_tax_id=$resources["resource_tax_class_id"];
				//echo $final_price;
				$total+=$final_price;
			}
		}else{
			$service_tax = tep_get_tax_rate($services['service_tax_class_id']);
			$loading_dates=array('loading_price_dates'=>$loading_price_dates,'start_date'=>$others["start_date"],'end_date'=>$others["end_date"],'start_time'=>$others["start_time"],'end_time'=>$others["end_time"]);
			$final_price=tep_calc_service_cost($services,$others["start_date"] . " " . $others["start_time"],$others["end_date"] . " " . ($others["end_time"]),$option_price,$loading_dates,$qty,$services['service_costs']);
			$total+=$final_price;
		 }
		return $total;
	}
    function show_total() {
      $this->calculate();
      return $this->total;
    }
	function is_free_checkout(){
		// calcuate total amount
		$this->calculate();
		// if the total is zero and only event is added to cart 
		if ($this->total<=0 && $this->mode=='E'){
			return true;
		} else {
			return false;
		}
	}
	
	function show_weight() {
      $this->calculate();
      return $this->weight;
    }

    function generate_cart_id($length = 5) {
      return tep_create_random_value($length, 'digits');
    }

    function get_content_type() {
      $this->content_type = false;

      if ( (DOWNLOAD_ENABLED == 'true') && ($this->count_contents() > 0) ) {
        reset($this->contents);
        while (list($products_id, ) = each($this->contents)) {
          if (isset($this->contents[$products_id]['attributes'])) {
            reset($this->contents[$products_id]['attributes']);
            while (list($key, $value) = each($this->contents[$products_id]['attributes'])) {
			 if($this->contents[$products_id]['element_type']=='V')
         	     $virtual_check_query = tep_db_query("select count(*) as total from " . TABLE_SERVICES_ATTRIBUTES . " where service_id = '" . tep_get_service_prid($products_id) . "' and options_id = '" . (int)$key . "' and options_values_id = '" . (int)$value . "'");
			  else				 
	              $virtual_check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad where pa.products_id = '" . (int)$products_id . "' and pa.options_values_id = '" . (int)$value . "' and pa.products_attributes_id = pad.products_attributes_id");
              $virtual_check = tep_db_fetch_array($virtual_check_query);

              if ($virtual_check['total'] > 0) {
                switch ($this->content_type) {
                  case 'physical':
                    $this->content_type = 'mixed';
                    return $this->content_type;
                    break;
                  default:
                    $this->content_type = 'virtual';
                    break;
                }
              } else {
                switch ($this->content_type) {
                  case 'virtual':
                    $this->content_type = 'mixed';
                    return $this->content_type;
                    break;
                  default:
                    $this->content_type = 'physical';
                    break;
                }
              }
            }
// ICW ADDED CREDIT CLASS - Begin
       } elseif ($this->show_weight() == 0) {
            reset($this->contents);
            while (list($products_id, ) = each($this->contents)) {
              $virtual_check_query = tep_db_query("select products_weight from " . TABLE_PRODUCTS . " where products_id = '" . $products_id . "'");
              $virtual_check = tep_db_fetch_array($virtual_check_query);
              if ($virtual_check['products_weight'] == 0) {
                switch ($this->content_type) {
                  case 'physical':
                    $this->content_type = 'mixed';
                    return $this->content_type;
                    break;
                  default:
		    		if ($this->contents[$products_id]['element_type']=='E' && ($this->content_type=='' || $this->content_type=='virtual')){
		       			$this->content_type='virtual';
		    		} else if ($this->contents[$products_id]['element_type']=='S' && ($this->content_type=='' || $this->content_type=='virtual')){
						$this->content_type='virtual';
					}else if ($this->contents[$products_id]['element_type']=='V' && ($this->content_type=='' || $this->content_type=='virtual')){
						$this->content_type='virtual';	
					}else {
                  $this->content_type = 'virtual_weight';
					}
                    break;
                }
              } else {
                switch ($this->content_type) {
                  case 'virtual':
                    $this->content_type = 'mixed';
                    return $this->content_type;
                    break;
                  default:
                    $this->content_type = 'physical';
                    break;
                }
              }
	  }
// ICW ADDED CREDIT CLASS - End
          } else {
            switch ($this->content_type) {
              case 'virtual':
                $this->content_type = 'mixed';
                return $this->content_type;
                break;
              default:
                $this->content_type = 'physical';
                break;
            }
          }
        }
      } else {
        $this->content_type = 'physical';
      }
      return $this->content_type;
    }

    function unserialize($broken) {
      for(reset($broken);$kv=each($broken);) {
        $key=$kv['key'];
        if (gettype($this->$key)!="user function")
        $this->$key=$kv['value'];
      }
    }
	
   // ------------------------ ICWILSON CREDIT CLASS Gift Voucher Addittion-------------------------------Start
   // amend count_contents to show nil contents for shipping
   // as we don't want to quote for 'virtual' item
   // GLOBAL CONSTANTS if NO_COUNT_ZERO_WEIGHT is true then we don't count any product with a weight
   // which is less than or equal to MINIMUM_WEIGHT
   // otherwise we just don't count gift certificates

    function count_contents_virtual() {  // get total number of items in cart disregard gift vouchers
      $total_items = 0;
      if (is_array($this->contents)) {
        reset($this->contents);
        while (list($products_id, ) = each($this->contents)) {
          $no_count = false;
          $gv_query = tep_db_query("select products_model from " . TABLE_PRODUCTS . " where products_id = '" . $products_id . "'");
          $gv_result = tep_db_fetch_array($gv_query);
          if (ereg('^GIFT', $gv_result['products_model'])) {
            $no_count=true;
          }
          if (NO_COUNT_ZERO_WEIGHT == 1) {
            $gv_query = tep_db_query("select products_weight from " . TABLE_PRODUCTS . " where products_id = '" . tep_get_prid($products_id) . "'");
            $gv_result=tep_db_fetch_array($gv_query);
            if ($gv_result['products_weight']<=MINIMUM_WEIGHT) {
              $no_count=true;
            }
          }
          if (!$no_count) $total_items += $this->get_quantity($products_id);
        }
      }
      return $total_items;
    }
// ------------------------ ICWILSON CREDIT CLASS Gift Voucher Addittion-------------------------------End

	function check_orders_id($orders_id,$events_id){
    	global $cart;
        if ($cart->count_contents()<=0) return false;
        
		reset($cart->contents);
		$found=false;
        while (list($products_id, ) = each($cart->contents)) {
			if ($products_id==$events_id && $cart->contents[$products_id]['old_orders_id']==$orders_id){
	     		$found=true;
	     		break;
	  		}
		}
		return $found;
	}
	function check_forced_items_purchased()
	{
		global $cart;
		if($cart->count_contents()<=0) return false;
		$product_id_list=$cart->get_check_id_list();
		$product_id_list=',' . $product_id_list		 . ',';
		$missed_items=false;
		reset($this->contents);
		while (list($products_id, ) = each($this->contents)) {
			$forced_ids=$this->contents[$products_id]["forced_item_ids"];
			if ($forced_ids!=""){
				$prefix=$this->contents[$products_id]["element_type"];
				$forced_item_array=array($forced_ids);
				for ($icnt=0;$icnt<count($forced_item_array);$icnt++){
					if(strpos($product_id_list, "," . $prefix . $forced_item_array[$icnt] . ",")===false){
						$missed_items=true;
						break 2;
					}
				}
			}
		}
		return $missed_items;
	}

	function get_check_id_list() {
		$product_id_list = '';
		if (is_array($this->contents)) {
			reset($this->contents);
			while (list($products_id, ) = each($this->contents)) {
				if ($this->contents[$products_id]['element_type']=='E')
					$product_id_list .= ',P' . substr($products_id,1);
				else if ( $this->contents[$products_id]['element_type']=='S')
					$product_id_list .= ',S' . substr($products_id,1);
				else if ( $this->contents[$products_id]['element_type']=='V')
					$product_id_list .= ',V' . tep_get_service_prid($products_id);
				else
					$product_id_list .= ',P' . tep_get_prid($products_id);
			}
		}
		return substr($product_id_list, 1);
	}
	// stored subscription
	
	function check_stored_subscription_purchased() //added by sobi
	{
		global $cart,$customer_id,$order_id;
		
		if($cart->count_contents()<=0) return 0;
		$also_purchased=0;
		 $subs_query=tep_db_query("select sc.subscription_categories_id as cat_id,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'");
		if(tep_db_num_rows($subs_query)>0)
		{
				$row=tep_db_fetch_array($subs_query);
				$subscription_id=$row['id'];
 				if(tep_session_is_registered('order_id'))
					 {
					 	$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='" . $customer_id . "' and  op.products_id='" . $subscription_id."' and op.products_type='S' and o.orders_id!= '" . $order_id . "'");
						if(tep_db_num_rows($order_query)>0)  //already purchase the stored subscription
							$also_purchased=1;//already purchased
					}
				
				reset($this->contents);
				$item_exists=false;
				while (list($products_id, ) = each($this->contents)) {
					if($this->contents[$products_id]['element_type']=='V' || $this->contents[$products_id]['element_type']=='E')
						$item_exists=true;
					$item_id=str_replace("S",'',$products_id);
					if($this->contents[$products_id]["element_type"]=='S' && $item_id==$subscription_id) //purchased
					{
						if($also_purchased==1)
							$also_purchased=3;//already purchased & available in cart
						else	
							$also_purchased=2;//available in cart 
					}
				}
				if($also_purchased==0 && $item_exists)
					$also_purchased=4;// not yet purchased stored subscription
		}//echo $also_purchased;
		return $also_purchased;
	}

	// multiple attendees
	function get_max_attendees(){
		if (is_array($this->contents)) {
		 reset($this->contents);
			while (list($products_id, ) = each($this->contents)) {
				if ($this->contents[$products_id]["element_type"]=="E" && $this->contents[$products_id]["qty"]>1)
					return $this->contents[$products_id]["qty"];
			}
		}
		return -1;
	}
	function set_attendees($attendees){
		$this->attendees=$attendees;
		if (tep_session_is_registered("customer_id")){
			$this->save_attendees();
		}
	}
	function save_attendees(){
		global $customer_id;
		$attendees=$this->attendees;
		tep_db_query("DELETE from " . TABLE_CUSTOMERS_BASKET_ATTENDEES . " where customers_id='" .  (int)$customer_id . "'");
		
		for ($icnt=0;$icnt<count($attendees);$icnt++){
			$attendees[$icnt]["customers_id"]=$customer_id;
			tep_db_perform(TABLE_CUSTOMERS_BASKET_ATTENDEES,$attendees[$icnt]);
		}
	}
	function get_attendees(){
		global $customer_id;
		$query=tep_db_query("SELECT * from " . TABLE_CUSTOMERS_BASKET_ATTENDEES . " where customers_id='" .  (int)$customer_id . "'");
		$attendees=array();
		if (tep_db_num_rows($query)>0){
			while($result=tep_db_fetch_array($query)){
				$attendees[]=$result;
			}
			$this->attendees=$attendees;
		}
	}
	function get_attendee_names($break='<br>'){
		$result="";
		$attendees=$this->attendees;
		for ($icnt=0;$icnt<count($attendees);$icnt++){
			$result.=$attendees[$icnt]["first_name"] . ' ' . $attendees[$icnt]["last_name"] . $break;
		}
		return $result;
	}
	// multiple attendees
	function load_products($order_id) {
	global $currencies,$modify_price_prefix,$sign,$purpose,$manual_option;
	$dis_time_format="";
	if(defined('TIME_FORMAT')) $dis_time_format=TIME_FORMAT;
	  $orders_products_query = tep_db_query("select orders_products_id, products_id, products_name, products_model, products_price, products_tax, products_quantity, final_price,products_type,events_type,events_fees,products_sku,support_packs_type from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . (int)$order_id . "' and products_type!='E' order by products_type,products_name,products_model");
	  //echo tep_db_num_rows($orders_products_query);
	  while ($orders_products = tep_db_fetch_array($orders_products_query)) {
	  $attribs=array();
	  		if($orders_products['products_type']=="P")
			{
		  			$attributes_query = tep_db_query("select products_options_id,products_options_values_id, products_options, products_options_values, options_values_price, price_prefix from " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . " where orders_id = '" . (int)$order_id . "' and orders_products_id = '" . (int)$orders_products['orders_products_id'] . "' and products_type='P'");
					if (tep_db_num_rows($attributes_query)) {
						  while ($attributes = tep_db_fetch_array($attributes_query))
								$attribs[$attributes['products_options_id']]= $attributes['products_options_values_id'];									 
					}
  					$products_id_string = tep_get_uprid($orders_products['products_id'], $attribs);
  					$products_id = tep_get_prid($products_id_string);
					$this->contents[$products_id_string] = array('qty' =>$orders_products['products_quantity'],
																  'element_type'=>'P',
																  'sessions'=>array(),
																  'old_orders_id'=>0,
																  'answer_type'=>'',
																  'answer_value'=>'',
																  'forced_item_ids'=>$forced_ids,
																  'order_products_id'=>$orders_products['orders_products_id'],
																  'name' => $orders_products['products_name'],
																  'model' => $orders_products['products_model'],
																  'tax' => $orders_products['products_tax'],
																  'price' => $orders_products['products_price'],
																  'final_price' => $orders_products['final_price'],
																  'id'=>$orders_products['products_id'],
																  'sku'=>$orders_products['products_sku'],
																  'support_packs_type'=>$orders_products['support_packs_type'] //Support Packs
																  );
				if (is_array($attribs)) {
				  reset($attribs);
				  while (list($option, $value) = each($attribs)) 
					  $this->contents[$products_id_string]['attributes'][$option] = $value;
				}
			}
			elseif($orders_products['products_type']=='V') {
				$resource_query=tep_db_query("Select resource_id,date_format(start_date,'%Y-%m-%d') as start_date, date_format(end_date,'%Y-%m-%d') as end_date,time_format(start_time,'%I:%i') as start_time,time_format(start_time,'%H:%i') as stime,time_format(end_time,'%I:%i') as end_time,time_format(end_time,'%H:%i') as etime, resource_name from " . TABLE_ORDERS_SERVICES . " where orders_products_id='" . (int)$orders_products['orders_products_id'] . "' ");
				    if($resource_result=tep_db_fetch_array($resource_query)) {
						//$this->products[$index]['resources'][$res_index]= array('start_date' => $resource_result['start_date'] ,
						/*	$others[]=array('resource_id' =>$resource_result['resource_id'] ,
										'start_date' => $resource_result['start_date'] ,
										'start_time' =>$resource_result['start_time'] ,
										'end_date' => $resource_result['end_date'] ,
										'end_time' =>$resource_result['end_time'] ,
										'resource_name' => $resource_result['resource_name']
							);*/
										$others['resource_id']=(int)$resource_result['resource_id'];
										$others['start_date']=$resource_result['start_date'];
										$others['end_date']=$resource_result['end_date'];
										$others['start_time']=$resource_result['start_time'];
										$others['end_time']=$resource_result['end_time'];
										if($dis_time_format!="") {
											if($dis_time_format=='24') {
												$others['start_time']=$resource_result['stime'];
												$others['end_time']=$resource_result['etime'];
											}
										}										
										$others['resource_name']=$resource_result['resource_name'];
					 }
					 else $others="";
					$res=array();
					$resource_id=(($others["resource_id"])?$res[1]=$others["resource_id"]:$res[1]=0);
					$service_id_string = tep_get_service_uprid($orders_products["products_id"],$res);
					if(tep_db_input($others["start_date"])) $start_date_string=strtotime(tep_db_input($others["start_date"]));
					if(tep_db_input($others["start_time"])) $start_time_string=strtotime(tep_db_input($others["start_time"]));
					if(tep_db_input($others["end_date"])) $end_date_string=strtotime(tep_db_input($others["end_date"]));
					if(tep_db_input($others["end_time"])) $end_time_string=strtotime(tep_db_input($others["end_time"]));
					$time_string=$start_date_string+$end_date_string+$start_time_string+$end_time_string+$orders_products["products_quantity"];
					$service_id_string.="-".$time_string;
					$service_id = tep_get_service_prid($service_id_string);
			        $this->contents[$service_id_string] = array('qty' => $orders_products["products_quantity"],
																'element_type'=>'V',
																'sessions'=>array(),
																'old_orders_id'=>0,
																'answer_type'=>'',
																'answer_value'=>'',
																'others'=>$others,
															    'order_products_id'=>$orders_products['orders_products_id'],
																'name' => $orders_products['products_name'],
																'model' => $orders_products['products_model'],
																'tax' => $orders_products['products_tax'],
																'price' => $orders_products['products_price'],
																'final_price' => $orders_products['final_price'],
																'id'=>$orders_products['products_id']);
       				$attributes_query = tep_db_query("select products_options_id,products_options_values_id, products_options, products_options_values, options_values_price, price_prefix from " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . " where orders_id = '" . (int)$order_id . "' and orders_products_id = '" . (int)$orders_products['orders_products_id'] . "' and products_type='V'");
					if (tep_db_num_rows($attributes_query)) {
						  while ($attributes = tep_db_fetch_array($attributes_query))
             				 $this->contents[$service_id_string]['attributes'][$attributes["products_options_id"]] = $attributes["products_options_values_id"];
					}
			}
			//service end
			elseif ($orders_products['products_type']=='S'){
			 	$products_id="S" . $orders_products["products_id"];
				$subs_query=tep_db_query("Select date_format(start_date,'%Y-%m-%d') as start_date from " . TABLE_ORDERS_SUBSCRIPTIONS . " where orders_products_id='" . (int)$orders_products['orders_products_id'] . "' ");
				$subs_row=tep_db_fetch_array($subs_query);
		        $this->contents[$products_id] = array('qty' => $orders_products['products_quantity'],
													  'element_type'=>'S',
													  'sessions'=>array(),
													  'old_orders_id'=>0,
													  'answer_type'=>'',
													  'answer_value'=>'',
													  'forced_item_ids'=>$forced_ids,
													  'subscription_start_date'=> $subs_row['start_date'],
													  'order_products_id'=>$orders_products['orders_products_id'],
													  'name' => $orders_products['products_name'],
													  'model' => $orders_products['products_model'],
													  'tax' => $orders_products['products_tax'],
													  'price' => $orders_products['products_price'],
													  'final_price' => $orders_products['final_price'],
													  'id'=>$orders_products['products_id']);
			}
	}//while end
	//=============events=======
	 /* $prev_eve_id=0;
	  $final_price=0;
	  $bol_taken=false;
	  $this->multiple_event_attendees=0;
	  $orders_products_query = tep_db_query("select e.events_id,op.orders_products_id, op.products_id, op.products_name, op.products_model, op.products_price, op.products_tax, op.products_quantity, op.final_price,op.products_type,op.events_type,op.events_fees,op.products_sku from " . TABLE_ORDERS_PRODUCTS . " op," . TABLE_EVENTS_SESSIONS . " e where op.products_id=e.sessions_id and op.orders_id = '" . (int)$order_id . "' and op.products_type='E' order by op.products_name,op.products_model");
	  while ($orders_products = tep_db_fetch_array($orders_products_query)) {	
				if ($orders_products["products_quantity"]>1) {
					if (!$bol_taken) {
						$attendee_query=tep_db_query("select distinct customers_firstname,customers_lastname from ".TABLE_CUSTOMERS ." c, ".TABLE_CUSTOMERS_TO_CUSTOMERS ." ctc where ctc.customer_id=c.customers_id and ctc.orders_id='".(int)$order_id."'");
						while($attendee_result=tep_db_fetch_array($attendee_query)){
							$attendees_name[]=array('first_name'=>$attendee_result['customers_firstname'],'last_name'=>$attendee_result['customers_lastname']);
						}
						$this->attendees=$attendees_name;
						$bol_taken=true;
					}
					$this->multiple_event_attendees+=1;
				}

  		  	  if($prev_eve_id==0)
			  {
			     $prev_eve_id=$orders_products['events_id'];
				 $prev_data_array=array('qty' =>$orders_products['products_quantity'],
									'name' => $orders_products['products_name'],
									'model' => $orders_products['products_model'],
									'tax' => $orders_products['products_tax'],
									'price' => $orders_products['products_price'],
									'final_price' => $orders_products['final_price'],
									'order_products_id'=>$orders_products['orders_products_id']
									);
			  }echo $prev_eve_id . '<br>' . $orders_products['events_id'] . '<br>';
			  if($prev_eve_id!=$orders_products['events_id'])		
			  { 
					$this->contents['E'.$prev_eve_id]=array('qty'=>$prev_data_array['qty'],
								    'element_type'=>'E',
									'sessions'=>$sessions,
									'name' => $prev_data_array['name'],
									'model' => $prev_data_array['model'],
									'tax' => $prev_data_array['tax'],
									'price' => $final_price, 
									'final_price' => $final_price,
									//'price' => $prev_data_array['price'],
									//'final_price' => $prev_data_array['final_price'],
									'id'=>$prev_eve_id,
									'order_products_id'=>$prev_data_array['order_products_id']
									);
						$sessions=array();
						$final_price=0;
						$sessions[$orders_products['products_id']]=array("type"=>$orders_products["events_type"],
								"date"=>$orders_products["products_model"],
								"fees"=>$orders_products["events_fees"],
								'order_products_id'=>$orders_products['orders_products_id'],
								"group"=>'');
						$final_price+=$orders_products["events_fees"];
						$prev_eve_id=$orders_products['events_id'];
				}
				else
				{
					$sessions[$orders_products['products_id']]=array("type"=>$orders_products["events_type"],
							"date"=>$orders_products["products_model"],
							"fees"=>$orders_products["events_fees"],
							'order_products_id'=>$orders_products['orders_products_id'],
							"group"=>'');
					$final_price+=$orders_products["events_fees"];
				}
	  }
	 if($prev_eve_id!=0)		
	  { 
			$this->contents['E'.$prev_eve_id]=array('qty' =>$prev_data_array['qty'],
							'element_type'=>'E',
							'sessions'=>$sessions,
							'name' => $prev_data_array['name'],
							'model' => $prev_data_array['model'],
							'tax' => $prev_data_array['tax'],
							'price' => $final_price,
							'final_price' => $final_price,
							//'price' => $prev_data_array['price'],
							//'final_price' => $prev_data_array['final_price'],
							'id'=>$prev_eve_id,
							'order_products_id'=>$prev_data_array['order_products_id']
							);
		}*/
		
	  $prev_eve_id=0;
	  $final_price=0;
	  $bol_taken=false;
	  $this->multiple_event_attendees=0;
	  $orders_products_query = tep_db_query("select e.events_id,op.orders_products_id, op.products_id, op.products_name, op.products_model, op.products_price, op.products_tax, op.products_quantity, op.final_price,op.products_type,op.events_type,op.events_fees,op.products_sku from " . TABLE_ORDERS_PRODUCTS . " op," . TABLE_EVENTS_SESSIONS . " e where op.products_id=e.sessions_id and op.orders_id = '" . (int)$order_id . "' and op.products_type='E' order by op.products_name,op.products_model");
	  while ($orders_products = tep_db_fetch_array($orders_products_query)) {	
				if ($orders_products["products_quantity"]>1) {
					if (!$bol_taken) {
						$attendee_query=tep_db_query("select distinct c.customers_firstname as first_name,c.customers_lastname as last_name,c.customers_id, c.customers_email_address as email_address,a.entry_street_address as street_address, a.entry_city as city,a.entry_postcode as postcode,a.entry_state as state,a.entry_country_id as country from ".TABLE_CUSTOMERS ." c, ".TABLE_CUSTOMERS_TO_CUSTOMERS ." ctc, " . TABLE_ADDRESS_BOOK . " a where a.address_book_id=c.customers_default_address_id and a.customers_id=c.customers_id and ctc.customer_id=c.customers_id and ctc.orders_id='".(int)$order_id."'");
						while($attendee_result=tep_db_fetch_array($attendee_query)){
							//$attendees_name[]=array('first_name'=>$attendee_result['customers_firstname'],'last_name'=>$attendee_result['customers_lastname']);
							$attendees_name[]=$attendee_result;
						}
						$this->attendees=$attendees_name;
						$bol_taken=true;
						$this->multiple_event_attendees+=1;
					}
					
				}

  		  	  if($prev_eve_id!=$orders_products['events_id'])
			  {
			  	if($prev_eve_id!=0)  {
					$this->contents['E'.$prev_eve_id]['sessions']=$sessions;
					$this->contents['E'.$prev_eve_id]['price']=$final_price;
					$this->contents['E'.$prev_eve_id]['final_price']=$final_price;
				}
					
			     $prev_eve_id=$orders_products['events_id'];
				$this->contents['E'.$prev_eve_id]=array('qty'=>$orders_products['products_quantity'],
													'element_type'=>'E',
													'sessions'=>$sessions,
													'name' => $orders_products['products_name'],
													'model' =>$orders_products['products_model'],
													'tax' => $orders_products['products_tax'],
													'id'=>$prev_eve_id,
													'order_products_id'=>$orders_products['orders_products_id']
													);		
					$sessions=array();
					$final_price=0;								
				}
					$session_fees=$orders_products['events_fees'];
					if($orders_products['events_fees']<=0)
						$session_fees=$orders_products['final_price'];
					$sessions[$orders_products['products_id']]=array("type"=>$orders_products["events_type"],
							"date"=>$orders_products["products_model"],
							"fees"=>$session_fees,
							'order_products_id'=>$orders_products['orders_products_id'],
							"group"=>'');
					$final_price+=$session_fees;
												 
				 
				/* $prev_data_array=array('qty' =>$orders_products['products_quantity'],
									'name' => $orders_products['products_name'],
									'model' => $orders_products['products_model'],
									'tax' => $orders_products['products_tax'],
									'price' => $orders_products['products_price'],
									'final_price' => $orders_products['final_price'],
									'order_products_id'=>$orders_products['orders_products_id']
									); 
			  echo $prev_eve_id . '<br>' . $orders_products['events_id'] . '<br>';*/
			/*  if($prev_eve_id!=$orders_products['events_id'])		
			  { 
					$this->contents['E'.$prev_eve_id]=array('qty'=>$prev_data_array['qty'],
								    'element_type'=>'E',
									'sessions'=>$sessions,
									'name' => $prev_data_array['name'],
									'model' => $prev_data_array['model'],
									'tax' => $prev_data_array['tax'],
									'price' => $final_price, 
									'final_price' => $final_price,
									//'price' => $prev_data_array['price'],
									//'final_price' => $prev_data_array['final_price'],
									'id'=>$prev_eve_id,
									'order_products_id'=>$prev_data_array['order_products_id']
									);
						$sessions=array();
						$final_price=0;
						$sessions[$orders_products['products_id']]=array("type"=>$orders_products["events_type"],
								"date"=>$orders_products["products_model"],
								"fees"=>$orders_products["events_fees"],
								'order_products_id'=>$orders_products['orders_products_id'],
								"group"=>'');
						$final_price+=$orders_products["events_fees"];
						$prev_eve_id=$eve_result['events_id'];
				}
				else
				{
					$sessions[$orders_products['products_id']]=array("type"=>$orders_products["events_type"],
							"date"=>$orders_products["products_model"],
							"fees"=>$orders_products["events_fees"],
							'order_products_id'=>$orders_products['orders_products_id'],
							"group"=>'');
					$final_price+=$orders_products["events_fees"];
				}*/
	  }
	/* if($prev_eve_id!=0)		
	  { 
			$this->contents['E'.$prev_eve_id]=array('qty' =>$prev_data_array['qty'],
							'element_type'=>'E',
							'sessions'=>$sessions,
							'name' => $prev_data_array['name'],
							'model' => $prev_data_array['model'],
							'tax' => $prev_data_array['tax'],
							'price' => $final_price,
							'final_price' => $final_price,
							//'price' => $prev_data_array['price'],
							//'final_price' => $prev_data_array['final_price'],
							'id'=>$prev_eve_id,
							'order_products_id'=>$prev_data_array['order_products_id']
							);
		}*/
		if($prev_eve_id!=0) {
			$this->contents['E'.$prev_eve_id]['sessions']=$sessions;
			$this->contents['E'.$prev_eve_id]['final_price']=$final_price;
			$this->contents['E'.$prev_eve_id]['price']=$final_price;
		}
		
		// manual price adjustment
		$sql_query=tep_db_query("select * from orders_total where class='ot_adjust' and orders_id='" . $order_id. "'");
		if(tep_db_num_rows($sql_query)>0)
		{
			$row=tep_db_fetch_array($sql_query);
			$adjust_value=$row['value'];
			tep_session_register('manual_option');
			tep_session_register('sign');
			tep_session_register('purpose');
			tep_session_register('modify_price_prefix');
			$manual_option='Y';
			$purpose=$row['title'];
			$modify_price_prefix=$row['value'];
			$sign=$adjust_value{0};                
			} 
			
			
			
			//print_r ($_SESSION);
	 }
  
  function get_events_final_price($products_id) {
  	$sessions=$this->contents[$products_id]['sessions'];
	$final_price=0;
	  while (list($id, ) = each($sessions)) {
	  	$final_price+=$this->contents[$products_id]['sessions'][$id]['fees'];
	  }
	  $this->contents[$products_id]['final_price']=$final_price;
  }
}?>
