/*
USAGE:
SetCookie(NAME, COOKIE, DAYS);
	1.
      Replace NAME with the name for the cookie. You'll use the same name when you read the value of the cookie. The name is like an index or pointer to the location of the cookie in the browser's cookie database.
      The name may contain only letters, numbers and underscore characters, and it must begin with a letter.
      If you set a cookie with the same name as a previous cookie set from the same domain, the previous cookie will be overwritten.
   2.
      Replace COOKIE with the cookie itself. That would be the value you want stored as the cookie. The cookie can be composed of any combination of characters.
   3.
      DAYS is optional. If used, replace DAYS with the number of days the cookie shall last. If DAYS is omitted, the cookie will last until the browser is closed. (Of course, the user and other software can delete the cookie prematurely, which is something you have no control over.)

ReadCookie(NAME);
	Replace NAME with the name of the cookie you want to see. If a cookie by the name you specify does not exist, the function will return a null string (a string containing no characters and with a length of zero).
*/
function SetCookie() {
if(arguments.length < 2) { return; }
var n = arguments[0];
var v = arguments[1];
var d = 0;
if(arguments.length > 2) { d = parseInt(arguments[2]); }
var exp = '';
if(d > 0) {
	var now = new Date();
	then = now.getTime() + (d * 24 * 60 * 60 * 1000);
	now.setTime(then);
	exp = '; expires=' + now.toGMTString();
	}
document.cookie = n + "=" + escape(String(v)) + '; path=/' + exp;
} // function SetCookie()

function ReadCookie(n) {
var cookiecontent = new String();
if(document.cookie.length > 0) {
	var cookiename = n+ '=';
	var cookiebegin = document.cookie.indexOf(cookiename);
	var cookieend = 0;
	if(cookiebegin > -1) {
		cookiebegin += cookiename.length;
		cookieend = document.cookie.indexOf(";",cookiebegin);
		if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
		cookiecontent = document.cookie.substring(cookiebegin,cookieend);
		}
	}
return unescape(cookiecontent);
}
/*
fetches a cookie that consists of a GET string and
reditrects the userAgent to the retrieved URL
uses Prototype.
*/
function setUserForm(cookieName){
	var redirect_url = ReadCookie(cookieName);
	if (redirect_url != ""){
		//alert("redirecting " + redirect_url);
		window.location = redirect_url;
	}
}

/*
sets the cookie consist of the full url to redirect 
*/
function getUserForm(rform,c_name){
	var query_string = document.forms[rform].serialize();
	var url_string = document.URL+"";
	var url_query_index = url_string.indexOf("?");
	if (url_query_index == -1){
		// user has not selected any parameters
		return;
	}
	var r_uri = url_string.removeSection(url_query_index,0,url_string.length);
	SetCookie(c_name, r_uri+"?"+query_string, 365);
	//alert("cookie set " + c_name + "val: " + ReadCookie(c_name));
}

/*13/07/2007 USED in crisisprofile.comp for the whowhatwhere tab*/
function change_div_state(divid, state , message, no_teaser){
	if (message == undefined )	{
		message = "Full details";
	}
	if (state == "closed"){
		//alert("opening " + divid );
		// open div with full details and change html to hide 
		show_div('yes','art_'+divid);
		document.getElementById('hd_'+divid).innerHTML="Hide " + message;
		// hide teaser view
		if (no_teaser != 1){
			show_div('no','teaser_'+divid);
		}
		return "open";
	}
	else{
		//alert("closing " + divid );
		// close div and change html to hide
		show_div('no','art_'+divid);
		document.getElementById('hd_'+divid).innerHTML="Show " + message;
		// show teaser view
		if (no_teaser != 1){
			show_div('yes','teaser_'+divid);		
		}
		return "closed";
	}
}
