// set cookie expiry date to a week after creation
var today = new Date();
var expiry = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000);

// set expiration to last week
var expireNow = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
var cookieName = "BaseModuleCookie";

// initialize an array
var arrBaseModule = init_array();

function BaseModule_Init(imgId, divId)
{	
	var img = document.getElementById(imgId);
	var div =  document.getElementById(divId);
	

	img.switchView = showHide;
	img.showHideElement = div;
	img.onclick = img.switchView;
	img.cookieKey = imgId;

	// set the cookies for the first time
	if(get_cookie(cookieName) == null)
	{
		set_cookie(cookieName, null, expiry);
	}
	
	// read the values of the cookies and show the modules like the they were the last time user was logged in
	else
	{
		get_array(cookieName, arrBaseModule);

		for (var i=1; i < next_entry(arrBaseModule); i++) 
		{
			if(arrBaseModule[i].substring(0,arrBaseModule[i].indexOf("=")) == img.name)
			{	
				//alert("arrBaseModule[i]="+arrBaseModule[i]+", img.name="+img.name);
				isChanged = check_Status(arrBaseModule[i]);
				
				if(isChanged == "true" || isChanged == "false")
				{
					cookieShowHide(div, img);
				}
		
				else
				{
					// by default the items are shown....
					return;
				}
			}
		}
	}
}

function cookieShowHide(divName, imgName)
{
	//alert("entered cookieShowHide");
	var isOpen = false;
	if (divName.style.display=='block')
	{	
		divName.style.display = 'none';
		imgName.src = '/Afs.Web.Intranet/images/modOpen.gif';
		isOpen = true;
	}
	else
	{
		divName.style.display = 'block';
		imgName.src = '/Afs.Web.Intranet/images/modClosed.gif';
		isOpen = false;
	}	
	//set_cookie(this.cookieKey, isOpen, expiry);
	return;
}

function showHide()
{
	//alert("entered showHide");
	var isOpen = false;
	if (this.showHideElement.style.display=='block')
	{	
		this.showHideElement.style.display = 'none';
		this.src = '/Afs.Web.Intranet/images/modOpen.gif';
		isOpen = true;
	}
	else
	{
		this.showHideElement.style.display = 'block';
		this.src = '/Afs.Web.Intranet/images/modClosed.gif';
		isOpen = false;
	}
	
	// adds cookies to the cookie array
	get_array(cookieName, arrBaseModule);
	
	// check to see if the cookie is already there.  if it is update it, else add it.
	for (var i=1; i < next_entry(arrBaseModule); i++)
	{
		if(arrBaseModule[i].substring(0,arrBaseModule[i].indexOf("=")) == this.name)
		{
			del_entry(cookieName, arrBaseModule, i, expireNow);
		}
	}
	
	var nextEntry = next_entry(arrBaseModule);
	arrBaseModule[nextEntry] = this.name + "=" + isOpen;
	set_array(cookieName, arrBaseModule, expiry);
	
	//set_cookie(this.cookieKey, isOpen, expiry);	
	return;
}
/*==========================COOKIE TOOLBOX=============================*/
// Cookie Toolbox Javascript
// copyright 4th September 2002, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.
// For instructions on how to use these functions see "A Cookie Toolbox"
// in the Javascript section of our site at http://get-me.to/felgall/
var dbug = 0;
function d_a(ary) 
{
	var beg = next_entry(ary) - 1; 
	for (var i = beg ; i > -1; i--) 
	{
		ary[i] = null;
	}
} 

/*This initializes an array called myarray that will be used with the cookie array functions. 
You will load the values that you want to save in your cookie into this array before saving the 
cookie and when you retrieve the cookie the array will be cleared and then loaded with the values from the cookie.*/
function init_array() 
{
	if (dbug) 
		alert('init_cookie');  
	var ary = new Array(null); 
	return ary;
}

/*This function allows you to create a cookie called name and store value in it. 
You specify the expiry date for this cookie in the expires field. If you don't specify an expiry date then 
the function will use the current date and time and the cookie will expire at the end of your visitor's browser session.*/
function set_cookie(name,value,expires) 
{
	if (dbug) 
		alert('set_cookie'); 
	if (!expires) 
		expires = new Date();
	document.cookie = name + '=' + escape(value) + '; expires=' + expires.toGMTString();
} 

// This function allows you to retrieve the content of a previously saved cookie called name into a field called value.	
function get_cookie(name) 
{
	if (dbug) 
		alert('get_cookie'); 
	var dcookie = document.cookie; 
	var cname = name + "="; 
	var clen = dcookie.length; 
	var cbegin = 0; 
	while (cbegin < clen) 
	{
		var vbegin = cbegin + cname.length;
		if (dcookie.substring(cbegin, vbegin) == cname) 
		{
			var vend = dcookie.indexOf (";", vbegin);
			if (vend == -1) 
				vend = clen; 
			return unescape(dcookie.substring(vbegin, vend));
		} 
		cbegin = dcookie.indexOf(" ", cbegin) + 1; 
		if (cbegin == 0) 
			break;
	} 
	return null;
} 

// This function deletes the cookie called name
function del_cookie(name) 
{
	if (dbug) 
		alert('del_cookie');
	document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/';
}


function get_array(name, ary) 
{
	if (dbug) 
		alert('get_array'); 
	d_a(ary); 
	var ent = get_cookie(name); 
	if (ent) 
	{
		i = 1;
		while (ent.indexOf('^') != '-1') 
		{
			ary[i] = ent.substring(0,ent.indexOf('^')); 
			i++;
			ent = ent.substring(ent.indexOf('^')+1, ent.length);
		}
	}
}

function check_Status(arrItem)
{
	if(dbug)
		alert('check_status');
	var retVal = arrItem.substring(arrItem.indexOf("=") + 1, arrItem.length);
	return retVal;
}

/*This function works the same as the set_cookie function except that it first 
converts the array into a single value field before storing it in the cookie. 
Note that this function uses the caret (^) as a field separator in this 
conversion so you will need to ensure that your array entries do not contain 
that character.*/	
function set_array(name, ary, expires) 
{
	if (dbug) 
		alert('set_array'); 
	var value = ''; 
	for (var i = 1; ary[i]; i++) 
	{
		value += ary[i] + '^';
	} 
	set_cookie(name, value, expires);
}
	
function del_entry(name, ary, pos, expires) 
{
	if (dbug) alert('del_entry');
	var value = ''; 
	get_array(name, ary); 
	for (var i = 1; i < pos; i++) 
	{
		value += ary[i] + '^';
	} 
	for (var j = pos + 1; ary[j]; j++) 
	{
		value += ary[j] + '^';
	} 
	set_cookie(name, value, expires);
}
	
function next_entry(ary) 
{
	if (dbug) 
	alert('next_entry'); 
	var j = 0; 
	for (var i = 1; ary[i]; i++) 
	{
		j = i
	} 
	return j + 1;
}
function debug_on() 
{
	dbug = 1;
} 

function debug_off() 
{
	dbug = 0;
}
	
function dump_cookies() 
{
	if (document.cookie == '') 
		document.write('No Cookies Found'); 
	else 
	{
		thisCookie = document.cookie.split('; '); 
		for (i=0; i<thisCookie.length; i++) 
		{
			document.write(thisCookie[i] + '<br \/>');
		}
	}
} 