/**
*  shopping cart
*/
// hashtable
var basket = new Hashtable();
var pd_hash = new Hashtable();

function Product() {
	this.id = 0;
	this.price  = 0;
	this.quantity = 1;
	//this.old_price = 0;
	//this.discount = 0;
	
	this.toString = function() {
		var temp = "";
		temp = this.id + "|";
		temp += this.price + "|";
		temp += this.price_us + "|";
		temp += this.quantity + "|";
		//temp += this.old_price + "|";
		//temp += this.discount + "|";
		return temp;
	}
	this.toObject = function(str) {
		var str_arr = str.split("|");
		
		this.id = str_arr[0];
		this.price = str_arr[1];
		this.price_us = str_arr[2];
		this.quantity = str_arr[3];
		//this.old_price = str_arr[4];
		//this.discount = str_arr[5];
	}
}

function objProduct() {
	this.id = 0;
	this.price  = 0;
	this.quantity = 1;
	this.old_price_thb = 0;
	this.old_price_usd = 0;
}

function init_hash_product (id,price,price_us,quantity,old_price_thb,old_price_usd){
/*
		var obj = new Product();	
		obj.id	= id;
		obj.price = price;
		obj.price_us = price_us;
		obj.quantity = quantity;
		//Put object
		pd_hash.put(obj.id,obj);
*/
	var obj = new Product();	
	obj.id	= id;
	obj.price = price;
	obj.price_us = price_us;
	obj.quantity = quantity;
	obj.old_price_thb = old_price_thb;
	obj.old_price_usd = old_price_usd;
	//Put object
	pd_hash.put(obj.id,obj);
}

function basket2cookie() {
	 //var obj = basket.getValue();
	 var obj = basket;
	 var str = '';

	obj.moveFirst();
	i= 0;
	while (obj.next()){
		i++;
		str += obj.getValue().toString();
		if(i != (obj.size-1)) {
			str  += ",";
		}
	}
	
	var today = new Date();
	var expire = new Date();
	nDays = 7;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	//setCookie('pasayainfo', value,expire);
	setCookie("cbasket", str,expire,'/');
}

function cookie2basket() {
	if(getCookie("cbasket")!=null){
		var temp = getCookie("cbasket").split(",");
		basket = new Hashtable();
		var obj = null;

		for(i=0; i<temp.length; i++) {
			if(temp[i]!=''){	
				obj = new Product();
				obj.toObject(temp[i]);
				basket.put(obj.id, obj);
			}
		}
	}
	
}

function select_product(){
	var sel = false;
	var entry =document.getElementsByName('chk_product'); //document.frmshopping.chk_product;
	
	for(i=0; i < entry.length; i++) {
		if(entry[i].checked==true){
			id = entry[i].value;
			obj = pd_hash.get(id);
			
			if (basket.get(id) !=null){
				var obj_new  = basket.get(id);
				obj_new.quantity = parseInt(obj_new.quantity) + 1;
				//basket2cookie();
			}else{
				var obj_new = new Product();	
				obj_new.id = obj.id;
				obj_new.price = obj.price;
				obj_new.price_us = obj.price_us;
				obj_new.quantity = 1;	
				//Put object
				basket.put(obj_new.id,obj_new);
			}
			sel = true;
		}
	}
	
	if(sel){
		basket2cookie(); 
		render_cart();
		Form.reset('frmshopping');
		alert('Add to shopping cart success.');
	}
}

function select_product_detail(id,price,price_us,quantity){
	if(basket.size()==0){
		cookie2basket();
	}
	if (basket.get(id) !=null){
		var obj_new  = basket.get(id);
		obj_new.quantity = parseInt(quantity);
		//basket2cookie();
	}else{
		var obj_new = new Product();
		obj_new.id = id;
		obj_new.price = price;
		obj_new.price_us = price_us;
		obj_new.quantity = quantity;
		//Put object
		basket.put(obj_new.id,obj_new);
	}
	basket2cookie();
	render_cart();
	alert('Add to shopping cart success.');
}

function select_product_size(id,price,price_us,quantity){
	if(basket.size()==0){
	 cookie2basket();
	}
	
	if (basket.get(id) !=null){
		var obj_new  = basket.get(id);
		obj_new.quantity = parseInt(quantity);
		//basket2cookie();
	}else{
		var obj_new = new Product();	
		obj_new.id = id;
		obj_new.price = price;
		obj_new.price_us = price_us;
		obj_new.quantity = quantity;
		//Put object
		basket.put(obj_new.id,obj_new);
	}

	basket2cookie(); 
}

// add to cart from wish list
function select_product_myWishList(){
	if(basket.size()==0){
	 cookie2basket();
	}
	
	var str_id = '';
	var sel = false;
	var entry =document.getElementsByName('chk_product'); //document.frmshopping.chk_product;
	
	for(i=0; i < entry.length; i++) {
		if(entry[i].checked==true){
			id = entry[i].value;
			obj = pd_hash.get(id);
			str_id += id + ',';
			
			
			if (basket.get(id) !=null){
				var obj_new  = basket.get(id);
				//obj_new.quantity = parseInt(obj_new.quantity) + 1;
				q = eval("frmshopping.q_"+ id +".value");
				obj_new.quantity = parseInt(obj_new.quantity) + parseInt(q);
				//basket2cookie();
			}else{
				var obj_new = new Product();
				q = eval("frmshopping.q_"+ id +".value");
				obj_new.id = obj.id;
				obj_new.price = obj.price;
				obj_new.price_us = obj.price_us;
				obj_new.quantity = parseInt(q);
				//Put object
				basket.put(obj_new.id,obj_new);
			}
			sel = true;
		}
	}
	
	if(sel){
		basket2cookie(); 
		delete_product_add(str_id);
		//render_cart();
		//Form.reset('frmshopping');
		alert('Add to shopping cart success.');
		location.href = 'items_list.php';
	}
}

function render_cart(){
	var str = "";
	price_unit = "THB";

	if(basket.size()==0){
		cookie2basket();
	}

	th_price = 0;
	us_price = 0;
	cnt_item = 0;
	basket.moveFirst();

	while (basket.next()){ 
		obj = basket.getValue();

		th_p = parseFloat(obj.price) * parseFloat(obj.quantity);
		th_price = parseFloat(th_price) + parseFloat(th_p);

		us_p = parseFloat(obj.price_us) * parseFloat(obj.quantity);
		us_price = parseFloat(us_price) + parseFloat(us_p);
		
		cnt_item = parseFloat(cnt_item) + parseFloat(obj.quantity);
	}
	if(getCookie("excCurrency")=="THB" || getCookie("excCurrency")=="" || getCookie("excCurrency")==null ){
		price_unit = "THB";
		display_price = th_price;
	}else{	
		price_unit = "USD";
		display_price =us_price;
	}

	parent.document.getElementById("sp_cnt_items").innerText = cnt_item;
	parent.document.getElementById("sp_total_price").innerText = format(display_price,2);
	parent.document.getElementById("sp_currency").innerText = price_unit;

}

function deleteBasket(id){
	basket.remove(id);
	basket2cookie(); 
	//render();
}

function emptyBasket(){
	var basket = new Hashtable();
	deleteCookie('cbasket','/');
}