﻿
							/*------------------------[]=================================================//=*\
													  ||
										#########################################################################
										#			  || 
										------------------------------------------------------------------- #  	  
										@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 refreshcart() : Refresh Cart.
		***********
		*/
		this.refreshcart = function(idhtml,idhtml2){
			$('#'+idhtml).html('<th>Sản phẩm</th><th>Số lượng</th><th>Đơn giá</th><th> </th>');
			if(idhtml2 !='' && items.length!=0){
			$('#'+idhtml2).html('<center><table class="stylecart"  width="495px" border="1" cellspacing="0" ><tr><th width="255px" class="title">Sản phẩm</th><th width="100px">Số lượng</th><th width="150px">Đơn giá</th><th>Xóa</th></tr>');
			}else {
				$('#'+idhtml2).html(' ');
			}
			
			for(cntx = 0; cntx<items.length; cntx++){
				if(items[cntx].id != 0){
					
					$('#'+idhtml).append('<tr><td>'+items[cntx].name+'</td><td>'+items[cntx].quantity+'</td><td>'+items[cntx].cost+'</td><td><a onclick="removefcart('+items[cntx].id+',\'lstcart\',\'lstcart2\')" style="cursor:pointer">Xóa</a></td></tr>');
					
					$('#'+idhtml2).append('<tr><td>'+items[cntx].name+'</td><td>'+items[cntx].quantity+'</td><td>'+items[cntx].cost+'</td><td><a onclick="removefcart('+items[cntx].id+',\'lstcart\',\'lstcart2\')" style="cursor:pointer">Xóa</a></td></tr>');
					}

				}
			}			
		}
	
		/*
		***********
			* Contructor 
		***********
		*/		
	
	function iproduct(id,name,cost,quantity){
		this.id = id;
		this.name = name;
		this.cost = cost;
		this.quantity = quantity;
	}


	var gscart = new scart();
	
	
	function addtocart(id,name,cost,idhtml){
		
		if(!confirm("Thêm sản phẩm " +name+ " vào giỏ hàng ?")) return;
		p = new iproduct(id,name,cost,1);
		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);
	}
	
	function loadcart(idhtml){
		//alert('d');
		//alert($.cookie("cartobject"));
		if($.cookie("cartobject")){
			var myobj = eval($.cookie("cartobject")); 
			items = myobj;
		}else {
			items = Array();
		}
		gscart.refreshcart(idhtml);
	}

	 
	function cartremoveall(idhtml,idhtml2){
		if(!confirm('Xóa tất cả giỏ hàng ?')) return;
		items = Array();
		$.cookie("cartobject",null);
		gscart.refreshcart(idhtml,idhtml2);

	}

function readcart(){
		$('#lstcart2').html('<center><table class="stylecart"  width="495px" border="1" cellspacing="0" ><tr><th width="255px" class="title">Sản phẩm</th><th width="100px">Số lượng</th><th width="150px">Đơn giá</th><th>Xóa</th></tr>');
		var sum=0;
		for(cntx = 0; cntx<items.length; cntx++){
			if(items[cntx].id != 0){
				var costs = items[cntx].cost*items[cntx].quantity;
				sum = sum + costs;
				$('#lstcart2').append('<tr><td width="250px">'+items[cntx].name+'</td><td width="100px">'+items[cntx].quantity+'</td><td width="150px">'+items[cntx].cost+'</td><td width ="15%" ><a onclick="removefcart('+items[cntx].id +',\'lstcart2\',\'lstcart\')" style="cursor:pointer">Xóa</a></td></tr>');
				
			}
			
		}	
		$('#lstcart2').append('<tr align="right"><td colspan="4" align="right" style="text-align:right;"><strong>Tổng số tiền: '+sum+' VND</strong></td></tr></table></center>');
	}
