var mail = new Array();
var profile = "";
var lastSelected = 0;

// If browser supports JavaScript, run graphics initialization.
function postProcess() {

	// Search Box
	var safari = (navigator.userAgent.match("AppleWebKit") != null);
	if (safari) {
		var s = document.getElementById("box");
		s.type = "search";
		s.setAttribute("results", "5");
	}
}

function MailUnMark(id)
{
	var obj = getObj("mail-"+mail[id].id);
	obj.style.backgroundColor = "white";
	obj.style.color = "black";
	mail[id].selected = false;
}

function MailMark(id)
{
	var obj = getObj("mail-"+mail[id].id);
	obj.style.backgroundColor = "#176ced";
	obj.style.color = "white";
	mail[id].selected = true;
}

function MailMarkCount()
{
	var c = 0;
	for (var id = 0; id < mail.length; id++) {
		if (mail[id].selected == true) c++;
	}
	return c;
}

// If browser supports JavaScript, this is triggered when selecting mail.
function selectMail(e, id) {
	if (getObj("mail-"+mail[id].id) == null) return;

	var id2 = id;
	if (e.shiftKey && id != lastSelected)
	{
		var unmark = false;
		if (mail[id].selected == true) {
			unmark = true;
		}
		while(true) {
			if (unmark)
				MailUnMark(id);
			else
				MailMark(id);

			if (id > lastSelected)
				id--;
			else if (id < lastSelected)
				id++;
			else
				break;
		}
	} else {
		var obj = getObj("mail-"+mail[id].id);
		if (mail[id].selected == true) {
			MailUnMark(id);
		} else {
			MailMark(id);
		} 
	}
	lastSelected = id2;
	getObj("mail-"+mail[id].id).focus();
	getObj("box").focus();
}

// If browser supports JavaScript, this is triggered when pressing
// the Select All button. It calls selectMail multiple times.
function selectAll() {

	var unmark = false;
	if (MailMarkCount() == mail.length)
		unmark = true;

	for (var id = 0; id < mail.length; id++) {
		if (unmark)
			MailUnMark(id);
		else 
			MailMark(id);
	}
	return true;
}

function previewMessage() {
	var id = -1;
	for (var i in mail)
	{
		if (mail[i].selected == true)
		{
			id = mail[i].id;
			break;
		}
	}
	if (id == -1) return false;

	window.location.href = "index.html?page=preview&msgid=" + id + "&profile=" + profile;
}

// If browser supports JavaScript, handle multi-selection actions
function handleMail(action) {
	try {
		var form = document.createElement("form");  
		form.setAttribute("action", "index.html?profile=" + profile);
		form.setAttribute("method", "post");
		form.setAttribute("id", "myform");
		for (var i in mail) { if (mail[i].selected == true) {
				var field = document.createElement("input");
				field.setAttribute("name","id-"+mail[i].id);
				field.setAttribute("type","text");
				field.setAttribute("value",action);
				form.appendChild(field);
		} }
		var field = document.createElement("input");
		field.setAttribute("type","submit");
		form.appendChild(field);
		document.body.appendChild(form);
		document.getElementById("myform").submit();
	} catch (err) {
		var query = "";
		for (var i in mail) { if (mail[i].selected == true) {
				query += "&id-" + mail[i].id + "=" + action;
		} }
		window.location.href = "index.html?profile=" + profile + query;
	}
}

// Support function
function getObj(obj) {
	if (document.getElementById(obj)) return document.getElementById(obj);
	else return null;
}
