﻿BasketUtils.defaultViewArr = new Array()
BasketUtils.splitViewArr = new Array()
BasketUtils.simpleView = null
BasketUtils.SummaryObject = null
BasketUtils.total_ItemsPrice = 0
BasketUtils.total_ItemsCount = 0
BasketUtils.total_DeliveryPrice = 0
BasketUtils.total_ItemsAndDeliveryPrice = 0
BasketUtils.totalPriceCellNum = 0
BasketUtils.itemsDiscount = 0

var m_currencyMark = ''
var viewMode = 'default_view'

BasketUtils.prototype.init = initializeView
BasketUtils.prototype.initSimple = initializeSimpleView
BasketUtils.prototype.changeDelivery = deliveryObj_SelectedIndexChanged
BasketUtils.prototype.switchView = switchDeliveryView

//===============
//Constructor
//===============
function BasketUtils()
{
	
}
//=====================================================================
//Display the itemsPrice, totalPrice and paymentsNum values in the page
//=====================================================================
function initializeView(defaultArr, splitArr, totalPriceCellNum)
{	
	//get the total price cell number in the grid
	BasketUtils.totalPriceCellNum = totalPriceCellNum

	//initialize all default basket objects
	for( var i=0; i<defaultArr.length; i++)
	{
		BasketUtils.defaultViewArr[defaultArr[i].itemsObjId] = new BasketView(defaultArr[i].itemsObjId, defaultArr[i].deliveryObjId )		
	}
	//initialize all split basket objects
	for( var i=0; i<splitArr.length; i++)
	{
		BasketUtils.splitViewArr[splitArr[i].itemsObjId] = new BasketView(splitArr[i].itemsObjId, splitArr[i].deliveryObjId )		
	}
	
	SummaryObject = new totalBasketsView()
	
	//set each default basket object data
	for( var i in BasketUtils.defaultViewArr )
	{
		var currObj = BasketUtils.defaultViewArr[i]
		 
		SetPayments( currObj.ddl_Payments, currObj.totalPrice - currObj.discount )
		SetToolTip(currObj.tbl_Basket)

		//alert( "totalPrice = " + currObj.totalPrice.toString()  + "\n currObj.discount = " + currObj.discount.toString() + "\n discount = " + ( currObj.totalPrice - currObj.discount) ) 		
		currObj.cell_ItemsPrice.innerText = AddThousandsQuotes( RoundFloat(currObj.itemsPrice) ) + " " + m_currencyMark
		currObj.cell_TotalPrice.innerText = AddThousandsQuotes( RoundFloat(currObj.totalPrice) )
		
		BasketUtils.total_ItemsCount += Number(currObj.itemsCount)
	}
	//set each split basket object data
	for( var i in BasketUtils.splitViewArr )
	{
		var currObj = BasketUtils.splitViewArr[i]
		 
		SetPayments( currObj.ddl_Payments, currObj.totalPrice - currObj.discount )
		SetToolTip(currObj.tbl_Basket)
		
		currObj.cell_ItemsPrice.innerText = AddThousandsQuotes( RoundFloat(currObj.itemsPrice) ) + " " + m_currencyMark
		currObj.cell_TotalPrice.innerText = AddThousandsQuotes( RoundFloat(currObj.totalPrice) )
	}
	
	BasketUtils.itemsDiscount = SummaryObject.hddn_ItemsDiscount.value
	if( Number(BasketUtils.itemsDiscount) > 0 )
	{
		SummaryObject.cell_ItemsDiscoumt.innerText = RoundFloat(BasketUtils.itemsDiscount) + ' ' +m_currencyMark
		SummaryObject.row_ItemsDiscount.style.display = 'block'
	}
	else
	{
		SummaryObject.row_ItemsDiscount.style.display = 'none'
	}
		
	setSummaryView()
}
//=====================================================================
//Display the itemsPrice and totalPrice values in the page
//=====================================================================
function initializeSimpleView(itemsObjId, summaryObjId, totalPriceCellNum)
{
	//check if basket is empty
	if( itemsObjId == '' )
		return;
		
	BasketUtils.totalPriceCellNum = totalPriceCellNum
	BasketUtils.simpleView = new BasketSimpleView(itemsObjId, summaryObjId)
	SetToolTip(BasketUtils.simpleView.tbl_Basket)	
	
	//set total price and items quantity
	for( var i=1; i<BasketUtils.simpleView.tbl_Basket.children.length; i++)
	{
		//get the input qty object
		obj = BasketUtils.simpleView.tbl_Basket.children[i].children[4].children[0]

		//set the item qty and total price
		var itemCount = obj.value;
		var itemPrice = GetPrice( obj.parentElement.parentElement.children[2] )
		var itemTotalPrice =  m_currencyMark + " " + String(AddThousandsQuotes( RoundFloat( Number(itemCount) * parseFloat(itemPrice) ) ) )
		var itemId = obj.itemId
		var promotionId = obj.promotionId
		var itemDiscount = obj.itemDiscount
		
		if( ! isNaN(promotionId) && Number( promotionId) > 0 )
			BasketUtils.simpleView.promotedItemsArr.push( new PromotedItem(itemId,itemCount,itemPrice,itemDiscount,promotionId) )
		
		if( obj.parentElement.parentElement.children[1].innerText == itemTotalPrice )
			continue
		else
			obj.parentElement.parentElement.children[1].innerText = itemTotalPrice 			
	}
	
	BasketUtils.simpleView.itemsPrice = getTotalPrice(BasketUtils.simpleView.tbl_Basket)
	BasketUtils.simpleView.itemsDiscount = getItemDiscount(BasketUtils.simpleView)

	setSimpleSummaryView()
}
//================================================================================
//This object holds a single basket entity with simple summary object : 
//all the necessary values and objects for displaying a basket and simple summary.
//================================================================================
function BasketSimpleView(itemsObjId, summaryObjId)
{
	this.tbl_Basket = document.all[itemsObjId].children[0]
	
	this.itemsPrice = 0//getTotalPrice(this.tbl_Basket)
	this.itemsCount = getItemCount(this.tbl_Basket)
	this.promotedItemsArr = new Array()
	this.itemsDiscount = 0
	this.sale3ItemsAdded = 0
	this.sale3ItemsLeftToAdd = 0
		
	this.cell_ItemsPrice = document.all[summaryObjId].children[0].children[0].children[0].children[0].children[1].children[0].children[0].children[0].children[1].children[0]
	this.cell_ItemsDiscount = document.all[summaryObjId].children[0].children[0].children[0].children[0].children[1].children[0].children[0].children[1].children[1].children[0]
	this.cell_ItemsTotalPrice = document.all[summaryObjId].children[0].children[0].children[0].children[0].children[1].children[0].children[0].children[2].children[1].children[0]
	this.cell_ItemsCount = document.all[summaryObjId].children[0].children[0].children[0].children[0].children[0].children[0]	
	
	this.cell_Sale3Added = document.all[summaryObjId].children[0].children[0].children[0].children[1].children[0].children[0]
	this.cell_Sale3LeftToAdd = document.all[summaryObjId].children[0].children[0].children[0].children[1].children[0].children[2]
}
//================================================================
//Change the total price and items count in the simple summary 
//table after they have changed in 'Qty' basket control
//================================================================
function setSimpleSummaryView()
{
	BasketUtils.simpleView.cell_ItemsPrice.innerText = AddThousandsQuotes( BasketUtils.simpleView.itemsPrice ) + " " + m_currencyMark
	BasketUtils.simpleView.cell_ItemsCount.innerText = BasketUtils.simpleView.itemsCount
	
	if( BasketUtils.simpleView.promotedItemsArr.length > 0 )
	{
		BasketUtils.simpleView.cell_ItemsDiscount.parentElement.parentElement.style.display = 'block'
		BasketUtils.simpleView.cell_ItemsTotalPrice.parentElement.parentElement.style.display = 'block'
		
		BasketUtils.simpleView.cell_ItemsDiscount.innerText = BasketUtils.simpleView.itemsDiscount + ' ' + m_currencyMark
		BasketUtils.simpleView.cell_ItemsTotalPrice.innerText = RoundFloat(parseFloat(BasketUtils.simpleView.itemsPrice) - parseFloat(BasketUtils.simpleView.itemsDiscount)) + ' ' + m_currencyMark
		
		BasketUtils.simpleView.cell_Sale3Added.innerText = BasketUtils.simpleView.sale3ItemsAdded
		BasketUtils.simpleView.cell_Sale3LeftToAdd.innerText = BasketUtils.simpleView.sale3ItemsLeftToAdd
	}
	else
	{
		BasketUtils.simpleView.cell_ItemsDiscount.parentElement.parentElement.style.display = 'none'
		BasketUtils.simpleView.cell_ItemsTotalPrice.parentElement.parentElement.style.display = 'none'
	}		 	
}
//==========================================================================
//This object holds a single basket entity : all the necessary values and 
//objects for displaying a basket.
//===========================================================================
function BasketView(itemsObjId, deliveryObjId)
{
	this.tbl_Basket = document.all[itemsObjId].children[0]
	this.itemsPrice = getTotalPrice(this.tbl_Basket)
	this.itemsCount = getItemCount(this.tbl_Basket)
	this.ddl_Delivery = document.all[deliveryObjId].children[0].children[1].children[0].children[0]	
	this.ddl_Payments = document.all[deliveryObjId].children[0].children[3].children[0].children[0]
		
	this.deliveryPrice = Number(this.ddl_Delivery.options[this.ddl_Delivery.options.selectedIndex].value)
	this.discount = document.all[deliveryObjId].children[0].children[2].children[1].children[1].value;
	
	//set the totalPrice = itemsPrice + deliveryPrice : 
	this.totalPrice = Number(this.itemsPrice) + Number(this.deliveryPrice)
		
	this.cell_ItemsPrice = document.all[deliveryObjId].children[0].children[0].children[0]
	this.cell_TotalPrice = document.all[deliveryObjId].children[0].children[2].children[0]
}
//===================================================================
//This object holds a all basket entities summary details references
//===================================================================
function totalBasketsView()
{
	this.cell_itemsCount	= document.all["_ctl0_MainContent_span_SummaryItemCount"]
	this.cell_itemsPrice	= document.all["span_SummaryPrice"]
	this.cell_deliveyPrice	= document.all["_ctl0_MainContent_span_SummaryDeliveryPrice"]
	this.cell_totalPrice	= document.all["_ctl0_MainContent_span_SummaryTotalPrice"]
	this.cell_ItemsDiscoumt = document.all["span_ItemsDiscount"]
	this.row_ItemsDiscount  = document.all["tr_ItemsDiscount"]
	this.hidden_totalPrice	= document.all["_ctl0:MainContent:hddn_TotalPrice"]
	this.hddn_ItemsDiscount = document.all["_ctl0:MainContent:hddn_ItemsDiscount"]
}
//======================================================================
//Event that accure after changing value in the delivery type drop dowm
//======================================================================
function deliveryObj_SelectedIndexChanged(objItemsId){
	if(event.propertyName != 'value')return;
	
	var basketView = null
	if( viewMode == 'default_view' )
		basketView = BasketUtils.defaultViewArr[objItemsId]
	else if( viewMode == 'split_view' )
		basketView = BasketUtils.splitViewArr[objItemsId]
	
	basketView.deliveryPrice = Number(basketView.ddl_Delivery.options[basketView.ddl_Delivery.options.selectedIndex].value)
	basketView.totalPrice = Number(basketView.itemsPrice) + Number(basketView.deliveryPrice)
	basketView.cell_TotalPrice.innerText = RoundFloat( basketView.totalPrice ) + " " + m_currencyMark

	
	basketView.ddl_Delivery.parentElement.children[1].value = basketView.ddl_Delivery.options[basketView.ddl_Delivery.options.selectedIndex].text;
	//alert("hdn : id = " + basketView.ddl_Delivery.parentElement.children[1].id + " \n value = " + basketView.ddl_Delivery.parentElement.children[1].value );
	
	setSummaryView()
}
//================================================================
//Change the delivery price and total price in the summary table
//after they changed in one of the basket controls
//================================================================
function setSummaryView()
{
	var basketViewArr = null
	if( viewMode == 'default_view' )
		basketViewArr = BasketUtils.defaultViewArr
	else if( viewMode == 'split_view' )
		basketViewArr = BasketUtils.splitViewArr
		
	BasketUtils.total_ItemsPrice = 0
	BasketUtils.total_DeliveryPrice = 0
		
	for( var i in basketViewArr )
	{
		var currObj = basketViewArr[i]
		 
		BasketUtils.total_ItemsPrice += Number(currObj.itemsPrice)
		BasketUtils.total_DeliveryPrice += Number(currObj.deliveryPrice)
	}
	
	//items count ( calculated in init function )
	SummaryObject.cell_itemsCount.innerText = BasketUtils.total_ItemsCount
	//items price
	SummaryObject.cell_itemsPrice.innerText = AddThousandsQuotes( RoundFloat(BasketUtils.total_ItemsPrice) ) + " " + m_currencyMark
	//delivery price
	SummaryObject.cell_deliveyPrice.innerText = RoundFloat(BasketUtils.total_DeliveryPrice) + " " + m_currencyMark
	
	BasketUtils.total_ItemsAndDeliveryPrice = RoundFloat( Number(BasketUtils.total_ItemsPrice) + Number(BasketUtils.total_DeliveryPrice) )
	BasketUtils.total_ItemsAndDeliveryPrice = Number(BasketUtils.total_ItemsAndDeliveryPrice) - Number(BasketUtils.itemsDiscount)
	
	//items total delivery and total price
	SummaryObject.cell_totalPrice.innerText = AddThousandsQuotes( RoundFloat(BasketUtils.total_ItemsAndDeliveryPrice) ) + " " + m_currencyMark
	//hedden field for server access
	SummaryObject.hidden_totalPrice.value = RoundFloat(BasketUtils.total_ItemsAndDeliveryPrice)
}
//============================================================================================
//Change the view between one delivery ( default_view' ) and split delivery ('split_view')
//============================================================================================
function switchDeliveryView()
{
	//ONE DELIVERY :
	if( event.srcElement.id.toString().indexOf('default_view') >= 0 )
	{
		document.all["tr_default_view"].style.display = "block";
		document.all["tr_split_view"].style.display = "none";
		viewMode = 'default_view'
	}
	//SPLIT DELIVERY : 
	else if( event.srcElement.id.toString().indexOf('split_view') >= 0 )
	{
		document.all["tr_default_view"].style.display = "none";
		document.all["tr_split_view"].style.display = "block";
		viewMode = 'split_view'
	}
	
	setSummaryView()
}


//==================================
//==================================
//		Essential methods :
//==================================
//==================================


//set tooltip for basket objects
function SetToolTip(objItems)
{
	for(var rows=1; rows<objItems.children.length; rows++)
	{
		for( var cellIndex=0; cellIndex<objItems.children[rows].children.length; cellIndex++)
		{
			objItems.children[rows].children[cellIndex].title = objItems.children[rows].children[cellIndex].innerText;
			//set the thousand quote for then number in totalPrice cell
			if( cellIndex == Number(BasketUtils.totalPriceCellNum) )
			{
				totalPrice = objItems.children[rows].children[cellIndex].innerText.split(' ')
				objItems.children[rows].children[cellIndex].innerText = m_currencyMark + " " + RoundFloat(AddThousandsQuotes( totalPrice[1] ) ) 
			}
		}		
	}				
}
//saves the payments number in a hidden input so it 
//could be read from server side
function setPaymentNum( obj )
{
	if( event.propertyName == "value" )
	{
		if( ! obj )	obj = this;
		
		var payments_input = obj.parentElement.parentElement.children[1].children[0];
		var selectedText = '1';
		if( obj.options.length != 0 )
			selectedText = obj.options[obj.options.selectedIndex].text;
			
		payments_input.value = selectedText;		
	}
}
//Populate the payements drop down by min_amount criteria
function SetPayments(ddl_payments, min_amount)
{
	ddl_payments.options.length = 0;
	
	var iCounter = 0;		
	for( var i=0; i<payments_Arr.length; i++)
	{	
		if( i == 0 )
		{
			var option = document.createElement("option");
			option.text = payments_Arr[i].payments_num;
			option.value = payments_Arr[i].min_amount;			
			ddl_payments.options[iCounter]= option;
			iCounter++;			
		}		
		else 
		{
			if( Number(payments_Arr[i].min_amount) <= Number(min_amount) )
			{			
				var option = document.createElement("option");
				option.text = payments_Arr[i].payments_num;
				option.value = payments_Arr[i].min_amount;			
				ddl_payments.options[iCounter]= option;
				iCounter++;			
			}
		}
	}
}
//Round the numbers after the floating point in a float number
function RoundFloat(num)
{
	var splitNumber = num.toString().split('.');
	//check for very small numbers 
	//(with e-X where X represents 10^X if e- its dots after(very small number) the zero if its e+ its dots before (very large number))
	var splitNumberCheckIfTooSmall = num.toString().split('e-');
	if( splitNumber.length == 1 )		
		return splitNumber[0];
		
	tmpNum = splitNumber[1].substring(0,1);
	tmpNum = tmpNum + "." + splitNumber[1].substring(1,splitNumber[1].length);
	tmpNum = Math.round( Number(tmpNum) );
	if  (splitNumberCheckIfTooSmall[0] != num.toString()&& Math.abs(num) < 0.00001) //  there was "e-" in the string because item was TOO SMALL	
		num = 0
	else 
		num = splitNumber[0] + "." + tmpNum + "0";		
	return num;		
}
//Add a thousand quote to a number that has more that three letters
function AddThousandsQuotes(num)
{
	num = num.toString()
	
	if( num.length < 4 )
		return num
	if( num.indexOf('.') >= 0 && num.length < 7 )
		return num
		
	if( num.indexOf(',')>=0 ) {//alert('num contain quotes ' + num)
		num = num.replace(',','');
		//alert( 'num after quotes removed ' + num )
		}	
		
	var decmlNum = ''
			
	try
	{	
		if( num.indexOf('.') >= 0 )
		{
			tmpNum = num
		//	alert( tmpNum )
			num = tmpNum.substring( 0, tmpNum.indexOf('.') )
		//	alert( num )
			decmlNum = tmpNum.substring(tmpNum.indexOf('.'))
		//	alert( decmlNum )
		}
		var len = num.length
		var newNumber = num.toString()
	
		for( var i=3; i<len; i=i+3)
		{
			number1 = num.substring(0,len-i)
			number2 = num.substring(len-i,len)
			
			newNumber = number1.toString() + ',' + number2.toString()
		}
		newNumber = newNumber + decmlNum
	}
	catch(e)
	{
		//alert( e.reason )
		return num
	}
	
	return newNumber;
}
//Count the item's num in objItems
function getItemCount(objItems)
{
	var itemCount = 0;
	for( i=1; i<objItems.children.length; i++)
	{
		itemCount += parseInt( ( objItems.children[i].children[4].children[0] ) ? objItems.children[i].children[4].children[0].value :objItems.children[i].children[4].innerText);
	}				
	return itemCount;
}
//Sum all the prices in objItems
function getTotalPrice(objItems)
{		
	var totalPrice = 0;	
	for( i=1; i<objItems.children.length; i++)
	{		
		totalPrice += GetPrice( objItems.children[i].children[1] ) 					
	}	
						
	return RoundFloat(totalPrice);
}
//Remove the שח from the price and check if it is a round number
//if so round it and update the table filed
//Return float legal number
function GetPrice(obj)
{			
	var tmpPrice = obj.innerText;
	var floatPrice = 0;
		
	tmpPrice = tmpPrice.replace(',','');	
	tmpPrice = tmpPrice.split(" ")			
	
	try{
		m_currencyMark = tmpPrice[0];
		if( Number(m_currencyMark).toString() == 'NaN' )
		{
			m_currencyMark = tmpPrice[0];
			floatPrice = tmpPrice[1];
		}
		else
		{
			m_currencyMark = tmpPrice[1];
			floatPrice = tmpPrice[0];
		}
		
		roundPrice = floatPrice.split(".");		
		if( parseInt(roundPrice[1]) == 0 )
			obj.innerText = m_currencyMark + " " + roundPrice[0];
				
		return Number( floatPrice ); 
	}
	catch(e){
		alert(e.reason)
	}
}
//Calculate the prices after change to Quantity in a simple basket view ( show.aspx )
function basketChangeQty( obj )
{	
	if( ! obj || obj.value == '' || ! BasketUtils.simpleView ) return	
	
	if( event.propertyName == "value" )
	{
		if( isNaN(Number(obj.value)) ){
			alert( 'יש להזין מספר בלבד' )
			obj.value = 1
			return;
		}
		
		//set the items (single changed item ) new  qty and total price
		var itemCount = obj.value;
		var itemPrice = GetPrice( obj.parentElement.parentElement.children[2] );
	
	//change item count in promoted items array
		if(obj.promotionId != '-1'){
			var item = new PromotedItem(obj.itemId,null,null,null,null)
			i = promotedItemsArr_search( BasketUtils.simpleView.promotedItemsArr, item )
			if( i > -1 )
				BasketUtils.simpleView.promotedItemsArr[i].qty = itemCount
		}
		var itemTotalPrice = AddThousandsQuotes( RoundFloat( Number(itemCount) * parseFloat(itemPrice) ) ) + ' ' + m_currencyMark;
		if( obj.parentElement.parentElement.children[1].innerText == itemTotalPrice )
			return;
			
		obj.parentElement.parentElement.children[1].innerText = itemTotalPrice;
		obj.parentElement.parentElement.children[1].title = itemTotalPrice;
		obj.parentElement.parentElement.children[1].style.direction = 'rtl';
				
		//set the summary	
		BasketUtils.simpleView.itemsPrice = getTotalPrice(BasketUtils.simpleView.tbl_Basket)
		BasketUtils.simpleView.itemsCount = getItemCount(BasketUtils.simpleView.tbl_Basket)
		BasketUtils.simpleView.itemsDiscount = getItemDiscount(BasketUtils.simpleView)
				
		setSimpleSummaryView()
	}
}
//Get all promoted items discount
function getItemDiscount(objSimpleView)
{
	var sale3ItemsArr = new Array()
	var sale4ItemsArr = new Array()
	var discount = 0
	var promoArr = objSimpleView.promotedItemsArr
	var sale3discount=false
	var sale4discount=false
	
	if( promoArr.length == 0 ) return 0
	
	for( i=0; i< promoArr.length; i++ )
	{
		//Get sale3 promoted items
		if( promoArr[i].promotionId == "1" )
			{
				sale3ItemsArr.push(promoArr[i])
				sale3discount=true
			}
		else
		//Get sale4 promoted items
		if( promoArr[i].promotionId == "3" )
			{
				sale4ItemsArr.push(promoArr[i])
				sale4discount=true
			}
		else
			discount += parseFloat( parseFloat( promoArr[i].discount ) * Number( promoArr[i].qty ) )
	}
		
	if (sale4discount)
		discount += parseFloat(getSale3ItemsDiscount(sale4ItemsArr, objSimpleView))
	
	if (sale3discount)
		discount += parseFloat(getSale3ItemsDiscount(sale3ItemsArr, objSimpleView))
		
	return RoundFloat(discount)
}
//Get the sale3 promoted items discount 
//by counting 3 items at a time and sub thier price by 99.90
//ALSO:
//Get the sale4 promoted items discount 
//by counting 4 items at a time and sub thier price by 99.90

function getSale3ItemsDiscount(sale3ItemsArr, objSimpleView)
{	
	var sale3ItemsCount = 0
	var totalPrice = 0
	var promotionCount
	
	promotionCount = "3"
	if (sale3ItemsArr[0].promotionId == "3") 	
		promotionCount = "4"
	
	for( i=0; i< sale3ItemsArr.length; i++ )
	{
		sale3ItemsCount += Number(sale3ItemsArr[i].qty)
		totalPrice += parseFloat(sale3ItemsArr[i].price)*parseFloat(sale3ItemsArr[i].qty)
	}
	
	if( sale3ItemsCount == 0 )
		return 0
		
	sale3ItemsArr.sort( promotedlItemsArr_sort )
		
	//special sale count
	div3 = Math.floor(sale3ItemsCount/ promotionCount)
	
	//left ( of special sale ) items count
	mod3 = sale3ItemsCount % promotionCount		
	
	var iPos = sale3ItemsArr.length-1
	var specialPrices = Number(div3) * parseFloat(99.90)
	var regularPrice = 0
	var item
	while(mod3>0)
	{
		item = sale3ItemsArr[iPos]
		iPos--		
		if( mod3 <= Number(item.qty) )
		{
			regularPrice += parseFloat(item.price) * Number(mod3)
			mod3 = 0
		}
		else//mod3 > item.qty
		{
			regularPrice += Number(item.qty) * parseFloat(item.price)
			mod3 = Number(mod3) - Number(item.qty)
		}
	}
	
	price = parseFloat(specialPrices) + parseFloat(regularPrice)
	
	var discount = RoundFloat( totalPrice - price )
	
	objSimpleView.sale3ItemsAdded = sale3ItemsCount
	objSimpleView.sale3ItemsLeftToAdd = promotionCount - ( sale3ItemsCount % promotionCount == 0 ? promotionCount : sale3ItemsCount % promotionCount )	
	return discount
}
//Object that represent an item
function PromotedItem(id, qty, price, discount, promotionId)
{
	this.id = id
	this.qty = qty
	this.price = price
	this.discount = discount
	this.promotionId = promotionId
}
//sort items by price
function promotedlItemsArr_sort(a,b)
{
	return a.price - b.price
}		
//search for current items index
function promotedItemsArr_search(arr, item)
{
	for( i=0; i<arr.length; i++ )
	{
		if( arr[i].id == item.id )
			return i;
	}
	return -1;
}
