/*
 * cookie.js        19/10/2009
 *
 * <p>Company: Desato Inc.</p>
 * Copyright (c) 2009 Desato Inc. All rights reserved.
 * <p>Title: resuable module </p>
 * <p>Description: The js file deals with the session in the client.
 *  1)createCookie: create a cookie in the client computer 
 *  2)readCookie: return a cookie value by the cookie name
 *  3)appendCookieValue: append the new cookie to the old cookie.If the old cookie doesn't exist create a new one.
 *  4)deleteCookie: delete a cookie by the cookie name
 *  5)isExistedCookie: judge if there is any cookies existed in the client computer.
 *  6)getAllCookies: return all cookies.
 * </p>
 * 
 * @author tony.chen
 *
 *
 */
 
	function createCookie(cookieName,cookieValue,expireDays) {
	    var expires = "";
	    if (expireDays > 0) {
	        var date = new Date();
	        date.setTime(date.getTime() + (expireDays*24*60*60*1000));
	        expires = "; expires="+date.toGMTString();
	    }
	    // if you want the cookie to be never expired, set expireDays == "".
	    document.cookie = cookieName + "=" + escape(cookieValue) + expires+"; path=/";
	}
	
	function readCookie(cookieName){
		var cookieValue = null;
		var arryOfCookies = document.cookie.split("; "); 
		for (var i = 0; i < arryOfCookies.length; i++) { 
			var cookie = arryOfCookies[i].split("="); 
			if (cookie[0] == cookieName) {
				cookieValue = unescape(cookie[1]);
				if ((cookieValue + "") == "undefined") {
		        	cookieValue = null;      		
        		}
				break;
			} 
  		}
  		return cookieValue; 
	}
	
	function appendCookieValue(cookieName,cookieValue,expireDays) {
	    var newCookieValue = cookieValue;
	    var oldCookieValue = readCookie(cookieName);
	    if (oldCookieValue != null) {
	        newCookieValue = oldCookieValue + "," + cookieValue;
	    } 
	    createCookie(name, newvalue, days);
	}
	
	function deleteCookie(cookieName){ 
		var date = new Date(); 
		date.setTime(date.getTime() - 10000); 
		var value = readCookie(cookieName);
		document.cookie = cookieName + "=" +  escape(value) + "; expires=" + date.toGMTString() + "; path=/"; 
	}

	function isExistedCookie(){
		var result = true;
		var str = document.cookie; 
		if (str == ""){ 
			result = false; 
		} 
		return result; 
	 } 
	 
	function getAllCookies(){ 
		return document.cookie; 
	} 		

