// ******************************************************************
// *** Cookies read/write library 
// *** Author: Youri Ginieys <yginieys@stepnet-ingenierie.com>
// *** Company: Stepnet Ingénierie
// ******************************************************************
// *** Returns value of cookie named 'cookieName'
function getCookie(cookieName) {
	var allcookies = document.cookie;	
	if(allcookies.indexOf(cookieName+'=') != -1) {		
		var start = allcookies.indexOf(cookieName) ;
		var end = allcookies.indexOf(";", start) ;		
		if(end == -1) {
			end = allcookies.length+1;
		}
		return unescape(allcookies.substring(start+cookieName.length+1, end));
	} else {
		return null ;
	}	

}

// *** Set cookie.
function setCookie(cookieName, cookieValue, cookieLifetime, cookiePath) {
	var lifetime = '' ;
	if(cookieLifetime != null) {
		var now = new Date();
		var expires = new Date(now.getTime()+cookieLifetime*1000);
		lifetime = "expires="+expires.toGMTString()+";";
	}
	var path = '' ;
	if(cookiePath != null) {
		path = "path="+cookiePath+";";	
	}
	document.cookie = cookieName+"="+escape(cookieValue)+";"+lifetime+path;
}


// ******************************************************
// *** Test functions ***********************************
// ******************************************************
function writeCookie(cookieName) {
	document.write(getCookie(cookieName));	
}

function formSubmit(form) {
	setCookie("test", form.testField.value, 10, "/");
	setCookie("testBis", form.testBisField.value, 20, "/");
	
	return true;	
}
