﻿
							/*------------------------[]=================================================//=*\
													  ||
										#########################################################################
										#			  || 
										------------------------------------------------------------------- #  	  
										@Project	  ||	DMN-ProjectA VietNam
										@Build at	  ||	Start in 10/6/2010 - Completed 1/7/2010 
										#			  ||
										#			  ||	Provinder 2010 by CTE-Solution
										#			  ||	http://www.cte.vn 
										@Language	  		# Java Script language
										@Framework			# CTE Framework
										@FBuild				# 1.0 summer 2010 version
										#			  ||	
										#			  ||   
										@File :				AJax cart - Createby CTE
										#			  ||   	Show message edit content
										#			  ||
										################### { DO NOT REMOVE THIS NOTICE UNTIL PROJECT SERVICE ENDED } ########
													  ||														  	
												\*====[]------------------------------------------------------------//-*/



	var items = Array();
	//Creat Methods is ussing.  
	function scart(){
		/*
		***********
			* Method add() : insert data into database.
		***********
		*/
		this.add = function(pitem){
			var flag_nf = false;
			for(cntx = 0; cntx<items.length; cntx++){
				if(items[cntx].id == pitem.id){
					items[cntx].quantity += 1;
					flag_nf = true;
					break;
				}
			}
			if(flag_nf == false){
				items[items.length] = pitem;
			}
			return true;
		},
		/*
		***********
			* Method remove() : Delete data in database.
		***********
		*/
		this.remove = function(itemid){
			for(cntx = 0; cntx<items.length; cntx++){
				if(items[cntx].id == itemid){
					items[cntx].id=0;
					return true;
				}
			}
			return false;
		},
		
		/*
		***********
			* Method getcountproduct() : get quantity of product add in cart .
		***********
		*/
		this.getcountproduct = function(){
			var retcount  = 0;
			for(cntx = 0; cntx<items.length; cntx++){
				if(items[cntx].id != 0) retcount += items[cntx].quantity;
			}
			return retcount	;			
		},
		
		/*
		***********
			* Method getcountproductid() : get count list product in cart .
		***********
		*/
		this.getcountproductid = function(){
			var retcount  = 0;
			for(cntx = 0; cntx<items.length; cntx++){
				if(items[cntx].id != 0) retcount = parseInt(items[cntx].quantity) + retcount;
			}
			return retcount	;			
		},
		/*
		***********
			* Method refreshcart() : Refresh Cart.
		***********
		*/
		this.refreshcart = function(idhtml){
			$('#'+idhtml).html('Có '+ this.getcountproductid() +' sản phẩm trong giỏ hàng');
		}
	
		/*
		***********
			* Contructor 
		***********
		*/		
	}
	function iproduct(id,name,cost,vat,quantity,promotion,promoid,cent){
		this.id = id;
		this.name = name;
		this.cost = cost;
		this.quantity = quantity;
		this.promotion = promotion;
		this.promoid = promoid;
		this.cent = cent;
		this.vat = vat;
	}


	var gscart = new scart();
	
	
	function addtocart(id,name,cost,vat,idhtml,promotion,promoid,cent){
		if(!confirm("Thêm sản phẩm " +name+ " vào giỏ hàng ?")) return;
		p = new iproduct(id,name,cost,vat,1,promotion,promoid,cent);
		gscart.add(p);
		$.cookie("cartobject", $.toJSON(items));
		gscart.refreshcart(idhtml);
	}
	
	function removefcart(id,idhtml,idhtml2){
		
		if(!confirm("Xóa sản phẩm này ?")) return;
		gscart.remove(id,idhtml);
		$.cookie("cartobject", $.toJSON(items));
		gscart.refreshcart(idhtml,idhtml2);
		readcart();
	}
	
	function loadcart(idhtml){
		if($.cookie("cartobject")){
			var myobj = eval($.cookie("cartobject")); 
			items = myobj;
		}else {
			items = Array();
		}
		gscart.refreshcart(idhtml);
	}

	function cartremoveall(idhtml){
		if(!confirm('Xóa tất cả giỏ hàng ?')) return;
		items = Array();
		$.cookie("cartobject",null);
		gscart.refreshcart(idhtml);
		readcart();
	}
	
	function cartremoveall2(idhtml){
		items = Array();
		$.cookie("cartobject",null);
		gscart.refreshcart(idhtml);
		readcart();
	}

//items[cntx].promotion
	function readcart(){
		var sum =0;
		$('#lstcart2').html('<table class="stylecart"  width="" border="1" cellspacing="0" ><tr><th width="255px" class="titlepro" >Sản phẩm</th><th width="100px" class="soluong">Số lượng</th><th width="100px">Đơn giá</th><th width="100px">Khuyến mãi</th><th width="100px"><strong>&nbsp;Thành tiền </strong></th><th>Xóa</th></tr>');
		for(cntx = 0; cntx<items.length; cntx++){
			if(items[cntx].id != 0){
				//alert((items[cntx].cost*items[cntx].quantity)-((items[cntx].cost*items[cntx].quantity)*((items[cntx].cent != 0)?parseInt(items[cntx].cent):100)/100));
				 //costs = (items[cntx].cost*items[cntx].quantity)-((items[cntx].cost*items[cntx].quantity)*((items[cntx].cent != 0)?parseInt(items[cntx].cent):100)/100);
				
				 //costs = (items[cntx].cost*items[cntx].quantity) + ((items[cntx].cost*items[cntx].quantity) * ((items[cntx].vat !=0)?parseInt(items[cntx].vat):0)/100);
				
				//alert(((items[cntx].cent != 0)?parseInt(items[cntx].cent):100)/100 * costs);
				var costs = (items[cntx].cost*items[cntx].quantity) - ((items[cntx].cost*items[cntx].quantity) * ((items[cntx].cent != 0)?parseInt(items[cntx].cent):0)/100);
				sum = sum + costs;
				//alert(costs);
				$('#lstcart2').append('<tr><td width="250px"><input type="hidden" name="proid[]" value="'+items[cntx].id+'" />'+items[cntx].name+'</td><td width="100px"><input type="text" size="5" value="'+items[cntx].quantity+'" id="qna' + cntx + '" name="proquantity[]"></td><td width="100px">'+items[cntx].cost+'</td><td width="100px"><input type="hidden" name="promoid[]" value="'+items[cntx].promoid+'" />'+items[cntx].promotion+'</td><td width="100px" id="did'+cntx+'">'+costs+'</td><td width ="30px" ><img src="images/layout/icon_delete.png" onclick="removefcart('+items[cntx].id +',\'lstcart2\',\'lstcart\')" style="cursor:pointer"/></td></tr>');
			}
		}
		$('#lstcart2').append('<tr align="right"><td colspan="6" align="right" style="text-align:right;"><strong>Tổng số tiền: '+sum+' VND</strong></td></tr></table>');
	}

