/**
 * ÄíÅ°¸¦ ÀÌ¿ëÇÑ Shopping Cart
 * 
 * @param _domain   ÄíÅ°¸¦ »ç¿ëÇÒ µµ¸ÞÀÎ¸í
 * @param _name     ÄíÅ°ÀÌ¸§
 */
function ShoppingCart(_domain, _name) {
    this.cookieName = _name;                								// ÄíÅ°¿¡ »ç¿ëÇÒ ÀÌ¸§
    this.domain 		= _domain;
    this.arrProd  	= new Array();
}

/**
 * ÄíÅ°¿¡ ÀÖ´Â »óÇ°À» load ÇÑ´Ù.
 */
ShoppingCart.prototype.init = function() {
	var cookies = document.cookie.split("; ");
	var strCookie;
	
	for(var i=0; i < cookies.length; i++) {
		strCookie   = cookies[i].split("=");
		cookieName  = strCookie[0];
		cookieValue = strCookie[1];
	}
}

/**
 * ÄíÅ°¿¡ ÀÖ´Â »óÇ°À» ¸ðµÎ »èÁ¦ÇÑ´Ù.
 */
ShoppingCart.prototype.clean = function() {
	thisCookie = document.cookie.split("; "); 
	
	expireDate = new Date; 											// »õ·Î¿î expireDate °´Ã¼¸¦ »ý¼º 
	expireDate.setDate(expireDate.getDate()-1); // À¯È¿±â°£ÀÇ ³¯Â¥°ªÀ» ¿À´Ãº¸´Ù ÇÏ·çÀû°Ô ¼³Á¤ 
	  
	for (var i=0; i<thisCookie.length; i++) { // ÄíÅ°°¡ ¹ß°ßµÉ¶§±îÁö Ã£±â 
		strCookie = thisCookie[i].split("=");
		cookieName = strCookie[0]; 							// ÄíÅ°¸¦ Ã£¾Æ¼­"=" ·Î ºÐ¸®ÇÑÈÄ º¯¼ö·Î ÀúÀå 
		if(cookieName.indexOf(this.cookieName) != -1)  {
			setCookie(cookieName, "", expireDate);
		}
		//document.writeln(cookieName + "ÀÌ »èÁ¦µÇ¾ú½À´Ï´Ù.<br/>");
	} 
}

/**
 * Ä«Æ®³»¿¡ »óÇ°À» µî·ÏÇÑ´Ù.
 */
ShoppingCart.prototype.add = function(_prodInfo) {
  if(this.exists(escape(_prodInfo))) return;
  
  expireDate = new Date();
  expireDate.setDate(expireDate.getDate() + 1); // ÄíÅ°»èÁ¦ÀÏÀÚ
  
  setCookie(this.cookieName + this.getCount(), _prodInfo, expireDate);
}

/**
 * Ä«Æ®³»¿¡ µî·ÏµÈ »óÇ°ÀÇ ¼ö¸¦ ¹ÝÈ¯ÇÑ´Ù.
 */
ShoppingCart.prototype.getCount = function() {
	idx = 0;
	cnt = 0;
	while(idx > -1) {
		idx = document.cookie.indexOf(this.cookieName, idx);
		if(idx > -1) {
			idx += this.cookieName.length;
			cnt++;
		}
	}
	
	return cnt;
}

/**
 * Ä«Æ®³»¿¡ »óÇ°ÀÌ ÀÖ´ÂÁö È®ÀÎÇÑ´Ù.
 */
ShoppingCart.prototype.exists = function(_prod) {
	if(document.cookie.indexOf(_prod) > -1) {
		return true;
	}
	return false;
}

ShoppingCart.prototype.toString = function() {
	cookies = document.cookie.split("; ");
	for(i=0; i < cookies.length; i++) {
		strCookie = cookies[i].split("=");
		if(strCookie[0].indexOf(this.cookieName) != -1) {
			product = new Product(strCookie[1]);
			strOut = product.code + ", " + product.name + ", " + product.image;
			document.write("<br>" + strOut);
		}
	}
}

ShoppingCart.prototype.getTestOut = function() {
	arr = this.getList();
	html = "<table border=1>\n";
	for(i=0; i < arr.length; i++) {
		html += "<tr>\n";
		html += "<td>" + arr[i].code + "</td>\n";
		html += "<td>" + arr[i].name + "</td>\n";
		html += "<td>" + arr[i].image  + "</td>\n";
		html += "</tr>\n";
	}
	html += "</table>\n";
	
	document.write(html);
}

ShoppingCart.prototype.getList = function() {
	cookies = document.cookie.split("; ");
	this.arrProd.length = 0;
	for(i=0; i < cookies.length; i++) {
		strCookie = cookies[i].split("=");
		if(strCookie[0].indexOf(this.cookieName) != -1) {
			this.arrProd[this.arrProd.length] = new Product(strCookie[1]);
		}
	}
	return this.arrProd;
}

/**
 * Ä«Æ®¿¡ ´ãÀ» »óÇ°
 */
function Product(_prodInfo) {
	if(_prodInfo==null || _prodInfo.length < 1) return;
	str = unescape(_prodInfo);
	arrStr = str.split(";");
	this.code = arrStr[0];		// »óÇ°ÄÚµå
	this.name = arrStr[1];		// »óÇ°¸í
	this.image  = arrStr[2];		// ¸µÅ©ÁÖ¼Ò
}

/**
 * ÄíÅ°¸¦ ¼³Á¤ÇÑ´Ù.
 * @param _name  			ÄíÅ°¸í
 * @param _value 			ÄíÅ°ÀúÀå°ª
 * @param _expireDate ÄíÅ°»èÁ¦ÀÏÀÚ
 */
function setCookie(_name, _value, _expireDate) {
  strCookie = "";
  strCookie += _name + "=" + escape(_value);
  strCookie += "; expires=" + _expireDate.toGMTString();
  strCookie += "; path=/";
  //strCookie += "; domain=" + document.domain;
  
  document.cookie = strCookie;
}
