/**
 * @namespace useful.cookieMonster
 * @author patrickc
 * @sdoc cookiemonster.sdoc
 */


/** @id CookieMonster */
function CookieMonster(){

	this.cookieEnabled 	= CookieEnabled;
	this.getCookie 		= GetCookie;
	this.killCookie 	= KillCookie;
	this.setCookie 		= SetCookie;
	this.syncForm		= SyncForm;
	this.version		= 1.1;
	

	/** @id CookieMonster.SyncForm */
	function SyncForm(form){
		if (!form) {form = 0;}
		form = (typeof form == 'object') ? form : document.forms[form];
		for (var i = 0; i < form.elements.length; ++i){
			if (!this.getCookie(form.elements[i].name)){
				continue;
			}
			switch (form.elements[i].type){
				case 'hidden' : 
				case 'text' : 
				case 'textarea' :
					form.elements[i].value = this.getCookie(form.elements[i].name);
				break;		
				case 'select-one' :
					SelectDropdown(form.elements[i],this.get(form.elements[i].name),true);
				break;		
				case 'select-multiple':
					SelectDropdown(form.elements[i],this.get(form.elements[i].name));
				break;	
				/** TODO: support for other sorts of form element */
			}
		}
		/** @id CookieMonster.SyncForm.SelectDropdown */
		function SelectDropdown(list,value,single){
			for (var i = 0; i < list.options.length; i++ ){
				if ( list.options[i].value == value  ){
					list.options[i].selected = true;
					if (single) {break;}
				}
			}
		}	
	}

	
	/** @id CookieMonster.SetCookie */
	function SetCookie(name, value, days, path, domain, secure){
		var expires;
		if(!isNaN(days) && days >= 0) {
			var d = new Date();
			d.setTime(d.getTime()+(days*24*60*60*1000));
			expires = d.toGMTString();
		} else {
			expires = -1;
		}
		if (window.logger){logger.log('setting cookie '+name+':'+value+' expires['+expires+'] path['+path+'] domain['+domain+'] secure['+secure+']')};
		document.cookie = name + "=" + escape(value) + ";"
			+ (expires != -1 ? " expires=" + expires + ";" : "")
			+ (path ? "path=" + path : "")
			+ (domain ? "; domain=" + domain : "")
			+ (secure ? "; secure" : "");		
	}


	/** @id CookieMonster.KillCookie */
	function KillCookie(name){
		this.setCookie(name, '', 0);
	}
	

	/** @id CookieMonster.GetCookie */
	function GetCookie(name){
		var cookies = document.cookie.split(";");
		var tempcookie = new Array;
		var returnVal = false;
		for (var i=0; i < cookies.length; i++) {
			tempcookie = cookies[i].split("="); 
			if (tempcookie[0].trim() == name){
				returnVal = unescape(tempcookie[1]).trim(); 
				break;
			} 
		}
		return returnVal;
		
	}
	
	
	/** @id CookieMonster.CookieEnabled */
	function CookieEnabled(){
		if(typeof this.cookieTest != "boolean") {
			var name 		= 'TEST_BROWSER_COOKIE_SUPPORT';
			this.setCookie(name,name);
			var val = this.getCookie(name);
			this.cookieTest = (val == name) ? true : false;
			if(this.cookieTest) {
				this.killCookie(name);
			}
		}
		return this.cookieTest;		
	}
	
}

var cookieMonster = new CookieMonster();

/** prequisites (string.trim) */
if ( !(new String).trim ){
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
}


