/**
 * Class: basket
 */

var gMessageElement = null;

Basket.prototype.items = new Hashtable();
Basket.prototype.basketElement = null;
Basket.prototype.frameElement = null;
Basket.prototype.customerDiscount = null;
Basket.prototype.lastMessage = null;

Basket.prototype.addTB = "addToBasket.php?pid=";
Basket.prototype.removeFB = "removeFromBasket.php?pid=";

function Basket(){
}

Basket.prototype.setBasketElements = function (){
	this.basketElement = document.getElementById("basketBox");
	this.frameElement = document.getElementById("basketFrame");
	gMessageElement = document.getElementById("basketMessageItem");
}

Basket.prototype.addItem = function (item, reload, name){
//	if (reload && this.isItemExists(item.getPid())){
//		var res = confirm("'" + name + "' קיים בסל הקניות.\nהאם אתה מעוניין לשנות את הכמות ל - "+item.getAmount()+"?");
//		if (!res) return false;
//	}
	
	this.items.put(new String(item.getPid()), item);
	if (reload){
		document.getElementById("_bmPname").innerHTML = name;
		var str = "'"+name+"' התווסף לסל הקניות.<br><br>";
		var x = y = 0;
		var w = 350;
		var h = 100;
		var loc = "";
	 	if (window.innerWidth){
			x = (((window.innerWidth/2)+document.body.scrollLeft) - (w/2));
	    	y = (((window.innerHeight/2)+document.body.scrollTop) - (h/2));
	    	loc = "screenX="+x+",screenY="+y;
		}else{
			x = (((document.body.clientWidth/2)+window.screenLeft) - (w/2));
	    	y = (((document.body.clientHeight/2)+window.screenTop) - (h /2));
	    	loc = "left="+x+",top="+y;
		}
		str += "<b>";
		var s = this.items.size();
		
		if (s == 0){
			str += "סל הקניות ריק";
		}else{
			if (s == 1)
				str += "מוצר אחד בסל הקניות";
			else
				str += s + " מוצרים בסל הקניות ";
			var total = this.getTotal();
			str += ", סה\"כ " + trimFloat(total, 2) + " ₪";
		}
		
		str += "</b>";

		this.lastMessage = str;
		
		var w = window.open("cube.php","cube","width="+w+",height="+h+","+loc);
		w.focus();
		
		this.frameElement.src = this.addTB + item.getPid () + "&amount=" + item.getAmount() + "&price=" + item.getPrice() + "&origenPrice=" + item.getOrignPrice();
	}
	
	return true;
}

Basket.prototype.getLastMessage = function (){
	return this.lastMessage;
}

Basket.prototype.changeItemAmount = function (pid, amount){
	var item = this.items.get(new String(pid));
	if (item != null){
		item.setAmount(amount);//change amount
		this.items.put(new String(item.getPid()), item);
		this.frameElement.src = this.addTB + item.getPid() + "&amount=" + item.getAmount() + "&price=" + item.getPrice() + "&origenPrice=" + item.getOrignPrice();
	}
}

Basket.prototype.changeItemPrice = function (pid, price){
	var item = this.items.get(new String(pid));
	if (item != null){
		item.setPrice(price);
		this.items.put(new String(item.getPid()), item);
	}
}

Basket.prototype.removeItem = function (pid){
	this.items.remove(pid);
	this.frameElement.src = this.removeFB + pid;
//	this.frameElement.src = this.removeFB + item.getPid();
}

Basket.prototype.isItemExists = function (pid){
	var item = this.items.get(new String(pid));
	if (item != null)
		return true;
		
	return false;
}

Basket.prototype.getTotal = function (){
	var total = 0;
	this.items.moveFirst();
	while(this.items.next()){
		var item = this.items.getValue();
		total += item.getAmount()*item.getPrice();
	}
	
	return total;
}

Basket.prototype.draw = function (){
	var str = null;
	var s = this.items.size();
	
	if (s == 0){
		str = "סל הקניות ריק";
		//<a href="basket.php" id="basketBox" style="color: black;"></a>
	}else{
		str = "<a href=\"basket.php?cat="+ g_cat +"\" style=\"color: black;\">";
		if (s == 1)
			str += "מוצר אחד בסל הקניות";
		else
			str += s + " מוצרים בסל הקניות";

		var total = this.getTotal();
//		var total = 0;
//		this.items.moveFirst();
//		while(this.items.next()){
//			var item = this.items.getValue();
//			total += item.getAmount()*item.getPrice();
//		}
		
		str += "<BR>סה\"כ " + trimFloat(total, 2) + " ₪</a>";
	}

	this.basketElement.innerHTML = str;
}

Basket.prototype.getItemsHash = function(){
	return this.items;
}

Basket.prototype.setCustomerDiscount = function(discount){
	this.customerDiscount = discount;
}
Basket.prototype.getCustomerDiscount = function(){
	return this.customerDiscount;
}

/**
 * Class: BasketItem
 */
 
BasketItem.prototype.pid = null;
BasketItem.prototype.amount = null;
BasketItem.prototype.price = null;
BasketItem.prototype.originalPrice = null;
BasketItem.prototype.couponSum = null;
BasketItem.prototype.couponIsPrecentage = false;

function BasketItem(pid, amount, price, originalPrice, couponSum, couponIsPrecentage){
	this.pid = pid;
	this.amount = amount;
	this.price = price;
	this.originalPrice = originalPrice || price;
	this.couponSum = couponSum;
	this.couponIsPrecentage = couponIsPrecentage == 1;
}

BasketItem.prototype.setPid = function (pid){
	this.pid = pid;
}
BasketItem.prototype.setPrice = function (price){
	this.price = price;
}
BasketItem.prototype.setAmount = function (amount){
	this.amount = amount;
}

BasketItem.prototype.getPid = function (){
	return this.pid;
}

BasketItem.prototype.getPrice = function (){
	return this.price;
}

BasketItem.prototype.getOrignPrice = function (){
	return this.originalPrice;
}

BasketItem.prototype.getAmount = function (){
	return this.amount;
}

BasketItem.prototype.isCouponPrecentage = function(){
	return this.couponIsPrecentage;
}

BasketItem.prototype.getCouponSum = function(){
	return this.couponSum;
}

function closeMessage(){
	gMessageElement.style.display = 'none';
}
