{{assign var=ProductID 	value=$smarty.get.prodid}}
{{assign var=Quantity  	value=$smarty.get.quantity|string_format:'%d'}}
{{assign var=PriceID   	value=$smarty.get.price}}
{{assign var=PageToRedirect   	value=$smarty.get.page}}
{{assign var=SessionID   value=''|sessionid}}
{{assign var=Time	 	  value=$smarty.now}}
{{assign var=CartCollection value=133}}
{{assign var=ProdCollection value=129}}
{{assign var=PriceCollection	value=131}}
{{assign var=CartDetailsCollection value=134}}

{{* Check if we allready have a cart defined and if it is closed *}}
{{if $smarty.get.prodid gt 0 and $smarty.get.quantity gt 0 and $smarty.get.price gt 0}}

{{getCollectionByCategory CollectionID=$CartCollection Collection_133_1=$SessionID aditionalFilter=' (Collection_133_5<>"on" or Collection_133_5 IS NULL) ' var=CartData OrderColumn='ID'}}

{{if $CartData|@count eq 0}}
	{{* We will need to define the cart *}}
	{{CollectionAdd CollectionID=$CartCollection var=CartID Collection_133_1=$SessionID Collection_133_2=$smarty.session.UserID Collection_133_3=$smarty.now Collection_133_4=$smarty.server.REMOTE_ADDR Collection_133_5=' '}}
	{{SetCookie name=$smarty.const.CART_NAME value=$SessionID period=30}}
{{else}}
	{{assign var=CartDataItem value=$CartData[0]}}
	{{assign var=CartID value=$CartDataItem.Collection_133_ID}}
{{/if}}

{{assign var=CartClosed value=$CartDataItem.Collection_133_28}}
{{if $CartClosed neq 'on'}}

	{{* We need to determine if the product and price are valid *}}
	{{assign var=CartAddValid value=1}}	
	{{* We check to see if product exists *}}
	
	{{getCollectionByCategory CollectionID=$ProdCollection ColumnID='ID' Collection_129_8='on' Value=$ProductID var=Prod OrderColumn='ID'}}
	{{if $Prod|@count eq 0}}
			{{assign var=CartAddValid value=0}}	
			prod not valid
	{{else}}
			{{assign var=ProdDetails value=$Prod[0]}}	
	{{/if}}
	
	
	{{* We check to see if priceid is right *}}
	{{getCollectionByCategory CollectionID=$PriceCollection ColumnID='ID' Value=$PriceID Collection_131_1=$ProductID var=Price OrderColumn='ID'}}
	{{if $Price|@count eq 0}}
			{{assign var=CartAddValid value=0}}	
			price not valid
	{{else}}
			{{assign var=PriceDetails value=$Price[0]}}
	{{/if}}
	
	{{* We check to see if quantity is greater than 0 *}}
	{{if $Quantity lt 1}}
		{{assign var=CartAddValid value=0}}	
		quantity {{$Quantity}} not valid
	{{/if}}
	
	{{if $CartAddValid eq 1}}
			{{* If the cart add is valid we need to determine if the product & price is allready added or not *}}
			
			{{addProductToCart productID=$ProductID priceID=$PriceID cartID=$CartID quantity=$Quantity productDetails=$ProdDetails priceDetails=$PriceDetails }}
			
			{{getCollectionByCategory CollectionID=$CartDetailsCollection ColumnID=1 Value=$CartID var=CartDetails OrderColumn='ID'}}
			{{updateCartSummary cartID=$CartID cartData=$CartData cartDetails=$CartDetails}}
									
	{{else}}
			
	{{/if}}
{{* end if cart is closed *}}
{{/if}}	
{{/if}} 		
	{{if $smarty.get.r neq 1}}
		 {{redirect page=$PageToRedirect}} 
    {{/if}} 