<?php
/* YES SMARTY INCLUDES GLOBAL */
function smarty_function_yes_vars($params,&$smarty){
	$ccontents = false;
	if(is_object($_SESSION['cart']))
		$ccontents = $_SESSION['cart']->count_contents();
	$is_checkout = ( strstr( $_SERVER['PHP_SELF'],'checkout') or strstr( $_SERVER['PHP_SELF'],'ebay_account_edit') ) ? true : false;
	$is_checkout_success = ( strstr( $_SERVER['PHP_SELF'],'checkout_success') ) ? true : false;

	// GOOGLE ANALYTICS
	$ANALYTICS_CODE = '';
	if(GOOGLE_ANALYTICS == 'true'){
		if($is_checkout_success and GOOGLE_ANALYTICS_ECOM == 'true'){
			$orders_query = xtc_db_query("select orders_id from " . TABLE_ORDERS . " where customers_id = '" . (int)$_SESSION['customer_id'] . "' order by date_purchased desc limit 1");
			$orders = xtc_db_fetch_array($orders_query);
			$order = new order($orders['orders_id'],&$xtPrice);
			$ANALYTICS_CODE = '
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
  var pageTracker = _gat._getTracker("'.GOOGLE_ANALYTICS_ID.'");
  pageTracker._initData();
  pageTracker._trackPageview();

  pageTracker._addTrans(
    "'.$orders['orders_id'].'",                                     // Order ID
    "'.xtc_parse_input_field_data(STORE_NAME, array('"' => '')).'",                            // Affiliation
    "'.$order->info['pp_total'].'",                                    // Total
    "'.$order->info['pp_tax'].'",                                     // Tax
    "'.$order->info['pp_shipping'].'",                                        // Shipping
    "'.$order->customer['city'].'",                                 // City
    "'.$order->customer['state'].'",                               // State
    "'.$order->customer['country'].'"                                       // Country
  );'."\r\n";
			for($i=0;$i<sizeOf($order->products);$i++){
				$category_name = xtc_output_generated_category_path(xtc_get_product_path($order->products[$i]['id']));
				$ANALYTICS_CODE .= '
  pageTracker._addItem(
    "'.$orders['orders_id'].'",                                     // Order ID
    "'.$order->products[$i]['model'].'",                                     // SKU
    "'.$order->products[$i]['name'].'",                                  // Product Name 
    "'.xtc_parse_input_field_data($category_name, array('"' => '')).'",                             // Category
    "'.$order->products[$i]['price'].'",                                    // Price
    "'.$order->products[$i]['qty'].'"                                         // Quantity
  );'."\r\n";
			}

			$ANALYTICS_CODE .= '
  pageTracker._trackTrans();
</script>';
		}else{
			$ANALYTICS_CODE = '
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("'.GOOGLE_ANALYTICS_ID.'");
pageTracker._initData();
pageTracker._trackPageview();
</script>';
		}
	}

	$vars = array('SMARTY_SESSION_customer_id'=>(($_SESSION['customer_id'])?$_SESSION['customer_id']:false),
		'SMARTY_SESSION_cart_contents'=>$ccontents,
		'HTTP_SERVER'=>HTTP_SERVER,
		'IS_CHECKOUT'=>$is_checkout,
		'GOOGLE_ANALYTICS_CODE'=>$ANALYTICS_CODE
	);
	$smarty->assign($params['assign'],$vars);
}
?>
