function CookieManager()
{
/*
 * CookieManager -- Manages sending and receiving cookies.
 * */

	this.parseCookies=function()
	{// Parses document.cookie and returns array of objects with name and value 
	// properties.
	var name,value,cook=document.cookie,count=0,index=0,a=new Array();
	while(count<cook.length){
		name='';
		value='';
		name=cook.substring(count,index=cook.indexOf('=',count)).replace(/^\s*/,'');
		if(name.length==0||index<0){
			return cs;}
		count=index+1;
		index=cook.indexOf(';',count);
		if(index<0){
			index=cook.length;}
		value=cook.substring(count,index);
		count=index+1;
		value=window.unescape?window.unescape(value):window.decodeURIComponent(value);
		a[a.length]={name:name,value:value};
		count++;}
	return a;}

	this.setCookie=function(settings)
	{/* Sets cookie based on properties of settings object.
	The following properties are used:
	
	settings={
		// All settings except name and value are optional.
		name: 'cookieName',
		value:'cookieValue',
		path: '/path/',
		domain: 'cookieDomain',
		secure: (true|false),
		
		// Expire time settings. Adds to current time.
		secs: 100,	// Adds 100 seconds.
		mins: 100,	// Adds 100 minutes.
		hrs:  100,	// Adds 100 hours.
		days: 100,	// Adds 100 days.
		months:1	// Adds 1 month.
	}
	
	Returns this. */
	var timeSettings=new Array('days','months','hrs','mins','secs'),
	timeSet=0,
	n=0,
	cookieStr='',
	expire=new Date(), // Current time.
	// Some browsers might not have window.escape(),
	// in which case, we use window.encodeURIComponent().
	encoder=window.escape?window.escape:window.encodeURIComponent;
	cookieStr=settings['name']+'='+encoder(settings['value']);
	// Calculate date and time.
	for(;n<timeSettings.length;++n){
		if(timeSettings[n] in settings){
			timeSet=1;
			// Add hours.
			if(timeSettings[n]=='hrs'){
				expire.setUTCHours(expire.getUTCHours()+settings['hrs']);}
			// Add minutes.
			if(timeSettings[n]=='mins'){
				expire.setUTCMinutes(expire.getUTCMinutes()+settings['mins']);}
			// Add seconds.
			if(timeSettings[n]=='secs'){
				expire.setUTCSeconds(expire.getUTCSeconds()+settings['secs']);}
			// Add days.
			if(timeSettings[n]=='days'){
				expire.setUTCSeconds(expire.getUTCSeconds()+settings['days']*86400);}
			// Add months.
			if(timeSettings[n]=='months'){
				expire.setUTCMonth(expire.getUTCMonth()+settings['months']);}}}
	// Set path.
	if('path' in settings){
		cookieStr+=';path='+encoder(settings['path']);}
	else{
		cookieStr+=';path=/';}
	// Set expire time. If no times given,
	// expires at end of session.
	if(timeSet){
		cookieStr+=';expires='+expire.toGMTString();}
	// Set domain.
	if('domain' in settings){
		cookieStr+=';domain='+encoder(settings['domain']);}
	// Set secure.
	if('secure' in settings&&settings.secure){
		cookieStr+=';secure';}
	// Set cookie.
	document.cookie=cookieStr;
	return cookieStr;}

	this.cookie=function(name)
	{// Returns cookie value.
	var k=this.cookieExists(name);
	return k>-1?this.cookies[k].value:'';}

	this.cookieExists=function(name)
	{// Returns >=0 if cookie name exists, -1 if not.
	for(var k=0;k<this.cookies.length;k++){
		if(this.cookies[k].name==name){
			return k;}}
	return -1;}

	this.reparse=function()
	{// Reparses cookies. Returns this.
	this.cookies=this.parseCookies();
	return this;}

	this.kill=function()
	{// Expires all cookies.
	for(var k=0;k<this.cookies.length;k++){
		this.setCookie({
			name:this.cookies[k].name,
			value:this.cookies[k].value,
			hrs:-24});}
	return this;}

	this.expire=function(name)
	{if(this.cookieExists(name)>-1){
		this.setCookie({name:name,value:'',hrs:-1});}
	return this;}
	
	// Expire odsuserId cookie.
	this.logout=function()
	{return this.expire('odsuserId');}
	
	this.cookies=this.parseCookies();
}

function openPopup(src,name,features,replace)
{// Opens popup window
name=arguments.length<2?'adminWindow':name;
features=arguments.length<3?'height=500,menubar,resizable,scrollbars,toolbar,width=580':features;
replace=arguments.length<4?true:replace;
if(name in windows&&windows[name]){
	windows[name].close();}
windows[name]=window.open(src,name,features,replace);
return windows[name];}

function showPopup(src,name,features,replace)
{// Shows popup window (name 'popup' by default).
// Not restricted to using popup directory URLs.
name=arguments.length<2?'popup':name;
features=arguments.length<3?'height=500,menubar,resizable,scrollbars,toolbar,width=580':features;
replace=arguments.length<4?true:replace;
windows[name]=window.open(src,name,features,replace);
return windows[name];}

function openInPopup(src,features,replace)
{
features=arguments.length<2?'height=500,scrollbars,width=580':features;
replace=arguments.length<3?true:replace;
windows['newPopup'+(openInPopup.popupID)]=window.open(src,'newPopup'+(openInPopup.popupID++),features,replace);
}
openInPopup.popupID=0;

function openModulePopup(src,name,features,replace)
{// Opens module popup.
name=arguments.length<1?'adminWindow':name;
features=arguments.length<2?'height=500,scrollbars,width=580':features;
replace=arguments.length<3?true:replace;
windows[name]=window.open('/odsWindows/modules/'+src,name,features,replace);
return undefined;}

function openHelpPopup(src,features,replace)
{// Opens help popup window.
features=arguments.length<2?'height=500,scrollbars,width=580':features;
replace=arguments.length<3?true:replace;
windows[name]=window.open(src,'helpWindow',features,replace);
return undefined;}

function nodeURL(nodeID,blockHash)
{return '/node'+nodeID+(blockHash?blockHash:'');}

function loadNodeInParent(nodeID,blockHash)
{if(window.opener){
	window.opener.location=nodeURL(nodeID,blockHash);}
}

function loadNode(nodeID,blockHash)
{window.location=nodeURL(nodeID,blockHash);}

function previewNodeURL(nodeID,blockHash)
{return '/preview'+nodeID+(blockHash?blockHash:'');}

function loadPreviewInParent(nodeID,blockHash)
{if(window.opener){
	// If URL is not correct, change it.
	if(
		window.location.hostname!=window.opener.location.hostname||
		(
			window.opener.location.pathname+window.opener.location.hash!=previewNodeURL(nodeID,blockHash)
			&&window.opener.location.pathname!=previewNodeURL(nodeID,blockHash)
		)){
		window.opener.location='http://'+window.location.hostname+previewNodeURL(nodeID,blockHash);
	}
	else{
		// Reload preview.
		window.opener.location.reload();}
}
}

function previewNode(nodeID,blockHash)
{window.location=previewNodeURL(nodeID,blockHash);}

function openInParent(url)
{if(window.opener){
	window.opener.location=url;}}

function checkPasswords(formRef,pwInput,pwInput2,msg)
{// Checks passwords. Returns true if matching, false if not.
if(arguments.length<4){
	msg='Passwords don\'t match! Please try again!';}
if(formRef.elements[pwInput].value!=formRef.elements[pwInput2].value){
	alert(msg);
	return false;}
return true;}

var cookieManager=new CookieManager(),
windows={
	adminWindow:null
};

