var colors = new Array(
"CC3333", "E59999",
"DD4477", "EEA1BB",
"994499", "CCA1CC",
"6633CC", "B299E5",
"336699", "99B2CC",
"3366CC", "99B2E5",
"22AA99", "90D4CC",
"329262", "98C8B0",
"109618", "87CA8B",
"66AA00", "B2D47F",
"AAAA11", "D4D488",
"D6AE00", "EAD67F",
"EE8800", "F6C37F",
"DD5511", "EEAA88",
"A87070", "D3B7B7",
"8C6D8C", "C5B6C5",
"627487", "B0B9C3",
"7083A8", "B7C1D3",
"5C8D87", "ADC6C3",
"898951", "C4C4A8",
"B08B59", "D7C5AC"
)

var windowWidth = 0;
var windowHeight = 0;

var xmlhttp;
var timer;
// When body is initialized
function init()
{
	// Set method to be executed before unload
	window.onbeforeunload = saveonbeforeunload;
	// update(event) implied on NS, update(null) implied on IE
	document.onmousemove = update;
	// Move adsense to correct position
	setWindowSizes();
	setBoardSizes()
	update();
}

var newTabBoxActiveHideNext = 0;
var newNoteBoxActiveHideNext = 0;

function hide()
{
	if(newTabBoxActive == 1)
	{
		if(newTabBoxActiveHideNext == 1)
		{
			hideNewTabBox();
			newTabBoxActiveHideNext = 0;
		}
		else
		{
			newTabBoxActiveHideNext = 1;
		}
	}
	else if(newNoteBoxActive == 1)
	{
		if(newNoteBoxActiveHideNext == 1)
		{
			hideNewNoteBox();
			newNoteBoxActiveHideNext = 0;
		}
		else
		{
			newNoteBoxActiveHideNext = 1;
		}
	}
}

function setBoardSizes()
{
	for(i = 0; i < boardIds.length; i++)
	{
		var board = document.getElementById(boardIds[i]);
		setWindowSizes();
		if (mRight >= windowWidth - 125)
		{
			board.style.width = mRight + "px";
		}
		else
		{
			board.style.width = windowWidth - 122 - 10 + "px";
		}
		if (windowHeight < mBottom)	
		{
			board.style.height = mBottom;
		}
		else
		{
			board.style.bottom = "0px";
		}
	}
	setAdsensePosition();
}

function setWindowSizes()
{
	if (navigator.appName == "Netscape") 
	{
		windowWidth = document.body.clientWidth;
		windowHeitht = document.body.clientWidth;
	}
	else
	{
		windowWidth = document.body.offsetWidth;
		windowHeight = document.body.offsetHeight;
	}
}

function setAdsensePosition()
{
	// Set AdSense position
	if (mRight > windowWidth - 125)
	{
		document.getElementById("adsense").style.left = mRight + 10 + "px";
	}
	else
	{
		if(document.getElementById("adsense") != null)
		{
			document.getElementById("adsense").style.left = windowWidth - 122 + "px";
		}
	}
}

// Validate email and password
function login()
{
	// Fetch textbox content
	var email = document.getElementById('email').value;
	var password = document.getElementById('password').value;
	// Validate login
	var result = getresponse("request/login.php?email="+email+"&password="+hex_md5(password));
	if(result == "1")
	{
		// If valid, submit form
		document.getElementById('loginForm').submit();
	}
	else
	{
		// Wrong username or password
		if(document.getElementById('wrongUserOrPasswordFont') == null)
		{
			// Create font and text element
			var text = document.createTextNode('Email and password do not match.');
			var font = document.createElement('font');
			font.id = "wrongUserOrPasswordFont";
			font.color = "red";
			font.size = "-2";
			font.appendChild(text)
			// Insert under email and password textboxes
			document.getElementById('wrongUserOrPassword').appendChild(font);
		}
		// To avoid form action to be taken
		return false;
	}
}

// Get response from a specific URL incl. query string
function getresponse(url)
{
	// Code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	// Code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	//Ajax cache hack
	var date = new Date();
	if (url.indexOf('?') == -1)
	{
		url += "?random=" + date;
	}
	else
	{
		url += "&random=" + date;	
	}
	
	// Open connection
	xmlhttp.open("GET", url, false);
	// Variable to save response
	var response;
	// When response ready
	xmlhttp.onreadystatechange = function() {
		// readyState=4 => response complete
		if (xmlhttp.readyState == 4)
		{
			// Response in IE
			response = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
	// Hack for getting response in Firefox
	if(window.XMLHttpRequest)
	{
		response = xmlhttp.responseText;
	}
	return response;
}

/*
* Cookie functions start
*/
function getCookie(name)
{ 
	if (document.cookie.length > 0) 
	{ 
		begin = document.cookie.indexOf(name + "="); 
		if (begin != -1) 
		{ 
			begin += name.length+1; 
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); 
		} 
	}
	return null; 
}

function setCookie(name, value, expiredays) 
{ 
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	document.cookie = name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function delCookie (name) 
{ 
	if (getCookie(name)) 
	{
		document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
/*
* Cookie functions end
*/
