/** 
 * @package cookies
 * @version 1.0 
 * @author Frédéric LECOINTRE<frederic.lecointre@burnweb.net>
 * 
 */
 
Cookies = new Object();

/**
 * Write a cookie
 * @param string sName
 * @param string mValue
 * @return void
 */
Cookies.write = function (sName, mValue){

	var argv = Cookies.write.arguments;
	var argc = Cookies.write.arguments.length;
	
	var oExpires 	= (argc > 2) ? argv[2] : null;
	var sPath 		= (argc > 3) ? argv[3] : null;
	var sDomain 	= (argc > 4) ? argv[4] : null;
	var bSecure 	= (argc > 5) ? argv[5] : false;
	
	document.cookie = sName + "=" + escape(mValue)
		+ ((oExpires == null) 	? "" 			: "; expires=" + oExpires.toGMTString())
		+ ((sPath == null) 		? "" 			: "; path=" + sPath)
		+ ((sDomain == null) 	? "" 			: "; domain=" + sDomain)
		+ ((bSecure == true) 	? "; secure" 	: "");
		
}//end function

/**
 * Read a cookie and retun its value or null if that cookie does not exists
 * @param string sName
 * @return string
 */
Cookies.read = function (sName){

	var arg = sName + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0, iOffset = 0;
	
	while (i < clen){

		iOffset = i + alen;
		
		if (document.cookie.substring(i, iOffset) == arg){
			var sEnd = document.cookie.indexOf (";", iOffset);
			
			if (sEnd == -1){
				sEnd = document.cookie.length;
			}//end if
			
			return unescape(document.cookie.substring(iOffset, sEnd)); 
		}//end if
	
		i = document.cookie.indexOf(" ", i) + 1;
		
		if (i == 0){
			break;
		}//end if
	
	}//end while

	return null; 

}//end function

/**
 * Return TRUE if the cookie sName exists, FALSE if not
 * @param string sName
 * @return bool
 */
Cookies.exists = function (sName){

	var arg = sName + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0, iOffset = 0;
	
	while (i < clen){

		iOffset = i + alen;

		if (document.cookie.substring(i, iOffset) == arg){
			return true;
		}//end if
	
		i = document.cookie.indexOf(" ", i) + 1;
		
		if (i == 0){
			break;
		}//end if
	
	}//end while

	return false; 

}//end function

/**
 * Remove the cookie sName 
 * @param string sName
 * @return void
 */
Cookies.remove = function (sName){
	var oDate = new Date;
	oDate.setFullYear(oDate.getFullYear() -1);
	Cookies.write(sName, null, oDate); 
}//end function