<?php
/*

 osCommRes, Services Oline 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare 

Released under the GNU General Public License
*/
// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();

class shoppingCart {
	var $contents, $total, $weight, $cartID, $content_type;

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

	function restore_contents() {
		//ICW replace line
		global $customer_id, $gv_id, $REMOTE_ADDR;
		//      global $customer_id;
		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') $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) . "'");

				if (!tep_db_num_rows($product_query)) {
					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($element_id) . "', '" . $qty . "',now(),'" . $this->contents[$products_id]['element_type'] . 
									"','" . 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']) . "')");
					if (isset($this->contents[$products_id]['attributes'])) {
						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'];
						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'] . "')");
						}
					}
				} else {
					if ($this->contents[$products_id]['element_type']=='P'){
						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) . "'");
					} else {
						$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("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'] . "')");
							}
						}
					}
				}
        	}
			//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);
		
		$products_query = tep_db_query("select products_id,customers_basket_quantity,products_type,old_orders_id,answer_type,answer_value 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'];
			else
				$products_id=$products['products_id'];
			$this->contents[$products_id] = array('qty' => $products['customers_basket_quantity'],'element_type'=>$products['products_type'],'old_orders_id'=>$products['old_orders_id'],'answer_type'=>$products['answer_type'],'answer_value'=>$products['answer_value'],'sessions'=>array());
			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
													 										);
				}
			}
			$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 . "'");
		}
	
		unset($this->cartID);
		if (tep_session_is_registered('cartID')) tep_session_unregister('cartID');
	}
	
   function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) {
  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);
 
  if (is_numeric($products_id) && is_numeric($qty)) {
    $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');
      }
 
      if ($this->in_cart($products_id_string)) {
        $this->update_quantity($products_id_string, $qty, $attributes);
      } else {
        $this->contents[$products_id_string] = array('qty' => $qty);
// 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) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id_string) . "', '" . (int)$qty . "', '" . date('Ymd') . "')");
 
        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 . "')");
          }
        }
      }
 
      $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) {
		global $new_products_id_in_cart, $customer_id;
	
		$qty=sizeof($sessions);
		//add 'E' to id to avoid conflict with products
		$products_id='E' . $events_id;
	
		if ($this->in_cart($products_id)){
			$this->check_sessions($products_id,$sessions);
			ksort($this->contents[$products_id]['sessions']);
			$this->contents[$products_id]['answer_type']=$answer_type;
			$this->contents[$products_id]['answer_value']=$answer_value;
			//update question answer value
			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 {
			$this->contents[$products_id] = array(	'qty' => $qty,
													'element_type'=>'E',
													'sessions'=>$sessions,
													'old_orders_id'=>$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) . "', '" . $qty . "', now(),'E','" . 
						$old_orders_id . "','" . $answer_type . "','" . $answer_value . "')");
			}
		}
		//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'] . "')");
			}
		}
		$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'];
	
		reset($new_sessions);
		while(list($id,)=each($new_sessions)){
			if (!isset($sessions[$id])) $sessions[$id]=$new_sessions[$id];
		}
		$this->contents[$products_id]['qty']=sizeof($sessions);
	}
	
	function update_quantity($products_id, $quantity = '', $attributes = '') {
  		global $customer_id;
		
		$products_id_string = tep_get_uprid($products_id, $attributes);
  		$products_id = tep_get_prid($products_id_string);
 
  if (is_numeric($products_id) && isset($this->contents[$products_id_string]) && is_numeric($quantity)) {
    $this->contents[$products_id_string] = array('qty' => $quantity,'element_type'=>'P','sessions'=>$sessions,'old_orders_id'=>$old_orders_id,'answer_type'=>'','answer_value'=>'');
// update database
    if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . (int)$quantity . "' where customers_id = '" . (int)$customer_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 . "'");
      }
    }
  }
}


	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)) . "'");
				}
			}
		}
	}

	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) {
		if (isset($this->contents[$products_id])) {
			return $this->contents[$products_id]['qty'];
		} else {
			return 0;
		}
	}

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

	function remove($products_id) {
		global $customer_id;
		// remove from database
		if ($this->contents[$products_id]['element_type']=='E') $element_id=substr($products_id,1);
		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($element_id) . "'");
			tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($element_id) . "'");
			if ($this->contents[$products_id]['element_type']=='E')
				tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_SESSIONS . " where customers_id ='" . (int)$customer_id . "' and products_id= '" . $element_id . "'");
		}
		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() {
		$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 .= ', ' . substr($products_id,1);
				else
					$product_id_list .= ', ' . $products_id;
			}
		}
		return substr($product_id_list, 1);
	}

	function calculate() {
		$this->total_virtual = 0; // ICW Gift Voucher System
		$this->total = 0;
		$this->weight = 0;
		if (!is_array($this->contents)) return 0;
	
		reset($this->contents);
		while (list($products_id, ) = each($this->contents)) {
			$qty = $this->contents[$products_id]['qty'];
			if ($this->contents[$products_id]['element_type']=='P')
			{
				// products price
				$product_query = tep_db_query("select products_id, products_price, products_tax_class_id, products_weight from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
				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;
					}
					// ICW ORDER TOTAL  CREDIT CLASS End Amendment
					$prid = $product['products_id'];
					$products_tax = tep_get_tax_rate($product['products_tax_class_id']);
					$products_price = $product['products_price'];
					$products_weight = $product['products_weight'];
					
					$special_price = tep_get_products_special_price($prid);
					if ($special_price) {
						$products_price = $special_price;
					}
					$this->total_virtual += tep_add_tax($products_price, $products_tax) * $qty * $no_count;// ICW CREDIT CLASS;
					$this->weight_virtual += ($qty * $products_weight) * $no_count;// ICW CREDIT CLASS;
					$this->total += tep_add_tax($products_price, $products_tax) * $qty;
					$this->weight += ($qty * $products_weight);
				}
	      } elseif ($this->contents[$products_id]['element_type']=='E') {
				$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']);
				
				$sessions=&$this->contents[$products_id]['sessions'];
				reset($sessions);
				$total_fees=0;
				$reminder_amount=false;
				$waitlist_stored=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=$result['events_fees']-tep_get_discount_price(substr($products_id,1),$result['events_fees'],$sessions[$id]['date']);
					} else if ($sessions[$id]['type']=='W'){
						if (!$waitlist_stored){				//waitlist user
							switch($sessions[$id]['group']){
								case 'V':
									$events_fees=$result['waitfee_vip'];	//vip group
									break;
								case 'F':
									$events_fees=$result['waitfee_firstcome']; //firstcome group
									break;
								case 'C':
									$events_fees=$result['waitfee_custom'];	//custome group
							}
							if ($result['session_style']=='E') $waitlist_stored=true;
						}
					} else {
						if (!$reminder_amount){
							$amount_details=tep_get_orders_reminder_details($this->contents[$products_id]['old_orders_id'],$result["events_id"],$result["session_style"]);
							$events_tax=$amount_details['tax'];
							$events_fees=$amount_details['amount'];
							$reminder_amount=true;
						}
					}
					$total_fees+=$events_fees;
				}
				$this->total_virtual += tep_add_tax($total_fees, $events_tax) * 1;
				$this->total += tep_add_tax($total_fees, $events_tax) * 1;
			}	
		}
		// attributes price
		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'] == '+') {
					$this->total += $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);
				} else {
					$this->total -= $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);
				}
				$this->weight += $attribute_price['products_attributes_weight'];
			}
		}
    }

	

	function attributes_price($products_id) {
		$attributes_price = 0;
	
		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 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);
				if ($attribute_price['price_prefix'] == '+') {
					$attributes_price += $attribute_price['options_values_price'];
				} else {
					$attributes_price -= $attribute_price['options_values_price'];
				}
			}
		}
		return $attributes_price;
	}

    function get_products() {
		global $languages_id;

		if (!is_array($this->contents)) return false;

		$products_array = array();
		reset($this->contents);
		while (list($products_id, ) = each($this->contents)) {
			if ($this->contents[$products_id]['element_type']=='P')
       		{
				$products_query = tep_db_query("select p.products_id, pd.products_name, p.products_model, p.products_image, p.products_price, p.products_weight, p.products_tax_class_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = '" . (int)$products_id . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'");
				if ($products = tep_db_fetch_array($products_query)) {
					$prid = $products['products_id'];
					$products_price = $products['products_price'];

					$special_price = tep_get_products_special_price($prid);
					if ($special_price) {
						$products_price = $special_price;
          			}
					$products_array[] = array(	'element_type'=>$this->contents[$products_id]['element_type'],
												'old_orders_id'=>$this->contents[$products_id]['old_orders_id'],
												'answer_type'=>$this->contents[$products_id]['answer_type'],
												'answer_value'=>$this->contents[$products_id]['answer_value'],
												'id' => $products_id,
												'sessions'=>array(),
												'name' => $products['products_name'],
												'model' => $products['products_model'],
												'image' => $products['products_image'],
												'price' => $products_price,
												'quantity' => $this->contents[$products_id]['qty'],
												'weight' => $products['products_weight'],
												'final_price' => ($products_price + $this->attributes_price($products_id)),
												'tax_class_id' => $products['products_tax_class_id'],
												'attributes' => (isset($this->contents[$products_id]['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 from " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed where e.events_id='" . (int)substr($products_id,1) . "' and e.events_id=ed.events_id and ed.language_id='" . (int)$languages_id . "'");
				
				$result = tep_db_fetch_array($result_query);
				
				$events_tax = tep_get_tax_rate($result['events_tax_class_id']);
				
				$sessions=&$this->contents[$products_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=$result['events_fees']-tep_get_discount_price(substr($products_id,1),$result['events_fees'],$sessions[$id]['date']);
						$this->contents[$products_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=$result['waitfee_vip'];	//vip group
									break;
								case 'F':
									$events_fees=$result['waitfee_firstcome']; //firstcome group
									break;
								case 'C':
									$events_fees=$result['waitfee_custom'];	//custome group
							}
							if ($result['session_style']=='E') $waitlist_stored=true;
						}
						$this->contents[$products_id]['sessions'][$id]['events_fees']=$result['events_fees'];
					} else {
						if (!$reminder_amount){
							$events_fees=tep_get_orders_reminder_amount($this->contents[$products_id]['old_orders_id'],$result["events_id"],$result["session_style"]);
							$reminder_amount=true;
						}
						$this->contents[$products_id]['sessions'][$id]['events_fees']=$result['events_fees'];
					}
					if ($sessions[$id]['type']!='U') $this->contents[$products_id]['sessions'][$id]['fees']=$events_fees;
					else $this->contents[$products_id]['sessions'][$id]['fees']=0;
					$total_fees+=$events_fees;
				}
				$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),
											'sessions'=>$this->contents[$products_id]['sessions'],
											'name' => $result['events_name'],
											'model' => '',
											'image' => $result['events_image'],
											'price' =>  $total_fees,
											'quantity' => '1',
											'weight' => '1',
											'final_price' => $total_fees,
											'tax_class_id' => $result['events_tax_class_id'],
											'old_orders_id'=>$this->contents[$products_id]['old_orders_id'],
											'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : ''));
			}
		}
	
	return $products_array;
	}

    function show_total() {
      $this->calculate();

      return $this->total;
    }

    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(, $value) = each($this->contents[$products_id]['attributes'])) {
              $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
                       $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;
	}
}
?>
