﻿function submitit()
{
	var email=document.frmMailingList.email.value;
	if (email=="")
	{
		alert("Please type your Email")
		document.frmMailingList.email.focus()
		return false
	}

	if (email.indexOf('@', 0) == -1 || email.indexOf('.', 0) == -1)
	{ 
		alert("No valid e-mail address!");
		document.frmMailingList.email.focus()
		return false
	}
}

var activeMenu = -1;
var firefox = (window.navigator.appName == "Netscape");
var calendarTab = "<table><tr><td><img src='images/calendar/Cal-Cor-Right.jpg'></td><td style='background-color:#D6E8FC;width:100%;'></td><td><img src='images/calendar/Cal-Cor-Left.jpg'></td></tr><tr><td colspan='3' style='background-color:#D6E8FC; padding-right:18px; padding-left:18px; padding-bottom:2px;'>" +
				"<table style='width:100%'><tr><td id='years'></td>" +
				"<td style='text-align:right;'><span style='color:navy; font-size:9pt; font-weight:bold;'>Order: </span><span id='sort'><a style='text-decoration:none; color:navy; cursor:pointer; font-size:9pt;' onmousedown='showYear(activeYear)'>Asc</a> | <a style='text-decoration:none; color:navy; cursor:pointer; font-size:9pt;' onmousedown='showYear(activeYear, true)'>Desc</a></span></td></tr></table>" +
				"</td></tr></table>" +
				"<table class='calendarBody' id='calendarTable' cellspacing='0' cellpadding='0'><tr><td></td></tr></table>";
var indexTab = "<table id='indexTable'></table>";
var topicsTab = "<table style='margin-left:20px; direction:ltr; height:25px;'><tr><td>Combination: </td><td><input type='radio' name='combination' onclick='showResults();' checked='true'></td><td style='padding-right:10px;'>And</td><td><input type='radio' name='combination' onclick='showResults();'></td><td>Or</td>" + 
				"<td style='padding-left:20px;'>Select: " + "<a style='text-decoration:none; color:blue; cursor:pointer;' onmousedown='checkAll(true)'>All</a> | " + 
				"<a style='text-decoration:none; color:blue; cursor:pointer;' onmousedown='checkAll(false)'>None</a></td></tr></table>" +
				"<table id='topicsTable' style='width:100%;'></table>" +			
				"<table style='width: 100%;'><tr><td style='padding-right:20px'></td><td><img src='images/topics/corner2.jpg'></td>" + 
				"<td style='border-top:1px #97c7ff solid'></td>" +
				"<td><img src='images/topics/corner1.jpg'></td></tr>" +
				"<tr><td></td><td style='border-left:1px #97c7ff solid'></td>" +
				"<td id='results' style='width:100%'><div style='color:#808080; text-align:center;'></div></td>" +
				"<td style='border-right:1px #97c7ff solid'></td></tr>" +
				"<tr><td></td><td><img src='images/topics/corner3.jpg'></td>" +
				"<td style='border-bottom:1px #97c7ff solid'></td>" +
				"<td><img src='images/topics/corner4.jpg'></td></tr></table>";

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];								

function id(id)
{
	return document.getElementById(id);
}

function checkAll(flag)
{
	inputs = document.getElementsByTagName("input");
	for (i = 0;  i < inputs.length; i++)
		if (inputs[i].type == "checkbox")
			inputs[i].checked = flag;
	
	showResults();
}

function showYear(year, descending)
{
	activeYear = year;
	id("years").innerHTML = id("years").innerHTML.replace(/<(b|\/b)>/gi, "");
	id("years").innerHTML = id("years").innerHTML.replace(">" + year + "<", "><b>" + year + "</b><");
	var calendarTable = id("calendarTable");
	while (calendarTable.rows.length > 0)
		calendarTable.deleteRow(0);

	var subsArray = new Array();
	for (i = 0; i < subs.length; i++)
		if (subs[i][1].substr(0,4) == year)
		{
			var dateArray = subs[i][1].split("\/");		
			if (subs[i][5] == "0")
				subsArray.push(subs[i][1] + "<div id='dateText'><div style='width:20px;'>[&nbsp;" + parseInt(dateArray[2],10) + "</div><div style='width:35px; text-align:center;'>" + months[parseInt(dateArray[1], 10) - 1] + "</div><div>" + dateArray[0] + "&nbsp;]</div></div>&nbsp; - <a style='text-decoration:none; color:#0134a9; font-weight:bold; font-size:8pt;' href='http://www.roshd.org/eng/subs/show.asp?code=" + subs[i][0] + "'>" + subs[i][2] + " : <span style='font-weight:normal; font-size:9pt;'>" + subs[i][3] +"</span></a>");
			else
				subsArray.push(subs[i][1] + "<div id='dateText'><div style='width:20px;'>[&nbsp;" + parseInt(dateArray[2],10) + "</div><div style='width:35px; text-align:center;'>" + months[parseInt(dateArray[1], 10) - 1] + "</div><div>" + dateArray[0] + "&nbsp;]</div></div>&nbsp; - <a style='text-decoration:none; color:#0134a9; font-weight:" + "normal; font-size:9pt;"  + "'>" + subs[i][2] + "</a>");			
		}

	if (descending)
		subsArray.sort().reverse();
	else
		subsArray.sort();		
	
	id("sort").innerHTML = id("sort").innerHTML.replace(/<(b|\/b)>/gi, "");
	id("sort").innerHTML = descending ? id("sort").innerHTML.replace("Desc", "<b>Desc</b>") : id("sort").innerHTML.replace("Asc", "<b>Asc</b>");
	
	for (i = 0; i < subsArray.length; i++)
	{
		row = calendarTable.insertRow(-1);
		cell = row.insertCell(0);
		cell.className = "calPad";

		cell = row.insertCell(1);
		cell.className = "calRow";
		cell.innerHTML = subsArray[i].substr(10);
		
		cell = row.insertCell(2);
		cell.className = "calPad";
	}
		
	row = calendarTable.insertRow(-1);
	cell = row.insertCell(0);
	cell.style.height = "100%";
}

function buildCalendarTab()
{
	id("content").innerHTML = calendarTab;
	id("content").style.paddingLeft = "20px";
	
	yearsMenu = "";
	for (i = 0; i < years.length - 1; i++)
		yearsMenu += "<a style='text-decoration:none; color:navy; cursor:pointer;' onmousedown='showYear(" + years[i] + ");'>" + years[i] + "</a> | "
	yearsMenu += "<a style='text-decoration:none; color:navy; cursor:pointer;' onmousedown='showYear(" + years[i] + ");'>" + years[i] + "</a>";
	
	id("years").innerHTML = "<span style='color:navy; font-weight:bold; font-size:9pt;'>Year: </span>" + yearsMenu;
	showYear(years[years.length - 1]);
}

function openCloseGroup(evt)
{
	evt = (evt) ? evt : event;
	group = (evt.target) ? evt.target : evt.srcElement;

	while (group.nodeName != "TR") 
		group = group.parentNode;
		
	var title;
	for (i = 0; i < titles.length; i++)
		if (titles[i][0] == group.id)
		{
			title = titles[i][1];
			break;
		}
		
	images = group.getElementsByTagName("img");
	if (images[0].src.indexOf("open") == -1)
	{
		group.getElementsByTagName("span")[0].innerHTML = "&#9660; ";		
		images[0].src = images[0].src.replace(/\.jpg/, "-open.jpg");
		images[1].src = images[1].src.replace(/\.jpg/, "-open.jpg");
	
		var listArray = new Array(2);
		listArray[0] = new Array();
		listArray[1] = new Array();
		
		var list = new Array(2);
		list[0] = list[1] = "";
		
		for (i = 0; i < subs.length; i++)
			if (subs[i][4] == group.id)
			{
				j = (subs[i][2].search(/muharram|martyrdom|demise|tasooa|ashura|arba'een/ig) == -1) ? 0 : 1;
				if (subs[i][5] == 0)
					listArray[j].push(subs[i][1] + "<li style='padding-bottom:5px; font-size:9pt;'><a style='text-decoration:none;' title='" + subs[i][2] + "' " + 
								"href='http://www.roshd.org/eng/subs/show.asp?code=" + subs[i][0] + "'>" + subs[i][3] + "</a>&nbsp;<span dir='ltr' style='color:#0067ee;'>(" + subs[i][1] + ")</span></li>");
				else
					listArray[j].push(subs[i][1] + "<li style='padding-bottom:5px; font-size:9pt;'><a style='text-decoration:none; color:#9999FF;' title='" + subs[i][2] + "'>" + 
								subs[i][2] + "</a>&nbsp;<span dir='ltr' style='color:#9999FF;'>(" + subs[i][1] + ")</span></li>");

			}
		
		listArray[0].sort();
		listArray[1].sort();
		for (i = 0; i < 2; i++)
			for (j = 0; j < listArray[i].length; j++)
				list[i] += listArray[i][j].substr(10);		
						
		var pre = new Array(2);
		pre[0] = pre[1] = "";

		title = title.replace(/\(/, "<span style='font-size:8pt;'>(");
		title = title.replace(/\)/, ")</span>");

		if (list[0] != "" && title.search(/other|eid|ramadan|hajj/ig) == -1)
		{
			pre[0] = "The Birthday of " + title;
			pre[0] = "<span style='color:#0134a9;'>" + pre[0] + ":</span>";			
		}
		
		if (list[1] != "" && title.search(/other|eid|ramadan|hajj/ig) == -1)
		{
			if (title.search(/prophet|khadijah|zainab|ma'soomah/ig) == -1)
				pre[1] = "The Martyrdom of " + title;
			else
				pre[1] = "The Demise of " + title;
			
			pre[1] = "<span style='color:#0134a9;'>" + pre[1] + ":</span>";
		}
		
		row = document.createElement("tr");
		col = document.createElement("td");
		col.setAttribute("colSpan", "4");
		t = id("openGroup").cloneNode(true);
		t.rows[0].cells[0].style.borderLeftColor = "#e1eeff";
		t.rows[0].cells[0].style.borderRightColor = "#e1eeff";
		t.rows[0].cells[1].style.borderLeftColor = "#e1eeff";
		t.rows[0].cells[1].style.borderRightColor = "#e1eeff";
		t.removeAttribute("id");
		t.style.display = "inline";
		col.appendChild(t);
		row.appendChild(col);	
		
		t.rows[0].cells[0].innerHTML = pre[0] + "<ul style='list-style:disc; padding-top:5px; margin:0; padding-left:15px;'>" + list[0] + "</ul>";
		t.rows[0].cells[1].innerHTML = pre[1] + "<ul style='list-style:disc; padding-top:5px; margin:0; padding-left:15px;'>" + list[1] + "</ul>";
		
		if (list[0] == "")
			t.rows[0].removeChild(t.rows[0].cells[0]);
			
		if (list[1] == "")
			t.rows[0].removeChild(t.rows[0].cells[1]);

		if (list[0] == "" || list[1] == "")
		{
			t.rows[0].cells[0].style.width = "100%";
			t.rows[0].cells[0].colSpan = 4;
			t.rows[1].cells[1].style.borderRightColor = "white";		
		}		
			
		group.parentNode.insertBefore(row, group.nextSibling);			
	}
	else
	{
		group.getElementsByTagName("span")[0].innerHTML = "► ";	
		images[0].src = images[0].src.replace(/-open\.jpg/, ".jpg");
		images[1].src = images[1].src.replace(/-open\.jpg/, ".jpg");
		group.parentNode.removeChild(group.nextSibling);
	}
}

function buildIndexTab()
{
	id("content").innerHTML = indexTab;
	id("content").style.paddingLeft = "20px";
	var indexTable = id("indexTable");
	
	for (i = 0; i < titles.length; i++)
	{
			row = indexTable.insertRow(-1);
			row.onmousedown = openCloseGroup;
			row.className = "indexRow" + i % 2;
			row.id = titles[i][0];
			
			cell = row.insertCell(0);
			cell.innerHTML = "<img src='images/index/Ind-row" + (i % 2 + 1) +"-right.jpg'>";

			cell = row.insertCell(1);
			cell.style.width = "100%";
			cell.style.paddingLeft = "5px";
			title = titles[i][1];
			title = title.replace(/\(/, "<span style='font-size:8pt;'>(");
			title = title.replace(/\)/, ")</span>");
			cell.innerHTML = "<span style='font-family:times new roman; font-size:7pt; padding-right:3px;'>► </span>" +
							 "<span style='color:#0134a9; font-weight:bold; font-size:9pt;'>" + title + "</span>";

			cell = row.insertCell(2);
			cell.innerHTML = "<img src='images/index/Ind-row" + (i % 2 + 1) +"-left.jpg'>";
			
			row = indexTable.insertRow(-1);
			cell = row.insertCell(0);
			cell.style.height = "3px";
			cell.colSpan = "3";
	}
}

function onGroupChange(id)
{
	checkBoxes = id.parentNode.parentNode.getElementsByTagName("table")[0].getElementsByTagName("input");
	for (i = 0; i < checkBoxes.length; i++)
		checkBoxes[i].checked = id.checked;
		 
	var big = checkBoxes.length > 5 ? "big" : "";		
//	id.parentNode.parentNode.getElementsByTagName("img")[0].src = (id.checked) ? "images/topics/TopicCheck" + big + ".jpg" : "images/topics/Topiccorner" + big + ".jpg";
	id.parentNode.parentNode.getElementsByTagName("img")[0].src = (id.checked) ? "images/topics/TopicCheck.jpg" : "images/topics/Topiccorner.jpg";

	showResults();
}

function selectByTopic(topicId, number, checkbox)
{
	var t = id("topicsTable"); 

	var checkboxes = t.rows[2 * number].getElementsByTagName('input');
	var big = checkboxes.length > 5 ? "big" : "";	
//	t.rows[2 * number].getElementsByTagName("img")[0].src = (checkbox.checked) ? "images/topics/TopicCheck" + big + ".jpg" : "images/topics/Topiccorner" + big + ".jpg";
	t.rows[2 * number].getElementsByTagName("img")[0].src = (checkbox.checked) ? "images/topics/TopicCheck.jpg" : "images/topics/Topiccorner.jpg";	

	var checked = true;
	for (k = 1; k < checkboxes.length; k++)
		checked &= checkboxes[k].checked;
	
	checkboxes[0].checked = checked;

	showResults();
}

function buildTopicsTab(catcode)
{
	id("content").innerHTML = topicsTab;
	id("content").style.paddingLeft = "0px";
	var topicsTable = id("topicsTable");
	var mainCategories = ["Beliefs", "Moralities", "Practices of the Religion", "Quran", "Others"];
	var widths = [140, 80, 100, 90, 140, 80, 100, 90];
	for (i = 0; i < megaCategories.length; i++)
	{
		for (l = 0; l < Math.floor(megaCategories[i].length / 5) + 1; l++)
		{
			if (l == 0)
			{
				row = topicsTable.insertRow(-1);
				row.style.height = "29px";
//				row.onmouseover= function(){this.getElementsByTagName('img')[0].src='images/topics/TopicCheckover' + this.getElementsByTagName('input').length > 4 ? 'big' : '' + '.jpg';};
				row.onmouseover= function(){this.getElementsByTagName('img')[0].src='images/topics/TopicCheckover.jpg';};				
				row.onmouseout=function()
				{
					var checkboxes = this.getElementsByTagName('input');
					var checked = false;
					for (k = 1; k < checkboxes.length; k++)
						checked |= checkboxes[k].checked;
		
					var big = checkboxes.length > 5 ? "big" : "";
					this.getElementsByTagName('img')[0].src = checked ? 'images/topics/TopicCheck' + big + '.jpg' : 'images/topics/Topiccorner' + big + '.jpg';
					this.getElementsByTagName('img')[0].src = checked ? 'images/topics/TopicCheck.jpg' : 'images/topics/Topiccorner.jpg';					
				};
			
				cell = row.insertCell(0);
				cell.rowSpan = 2;
				cell.style.width = "35px";
				cell.style.verticalAlign = "top";
//				cell.innerHTML = "<img src='images/topics/Topiccorner" + (megaCategories[i].length > 5 ? "big" : "") + ".jpg'>";
				cell.innerHTML = "<img src='images/topics/Topiccorner.jpg'>";
			
				cell = row.insertCell(1);
				cell.className = "topicRow0";
				checkBox = document.createElement("input");
				checkBox.setAttribute("type", "checkbox");
				checkBox.setAttribute("id", String(i));
				checkBox.onclick = function(){onGroupChange(this);};
				cell.appendChild(checkBox);
				
				cell = row.insertCell(2);
				cell.className = "topicRow1";
				cell.innerHTML = mainCategories[i];
			
				cell = row.insertCell(3);
				table = document.createElement("table");
				cell.appendChild(table);
				table.className = "topicRow2";
			}
		/*	else
			{
				row = topicsTable.insertRow(-1);
				row.style.height = "29px";
				/*row.onmouseover= function(){this.getElementsByTagName('img')[0].src='images/topics/TopicCheckover.jpg';};
				row.onmouseout=function()
				{
					var checkboxes = this.getElementsByTagName('input');
					var checked = false;
					for (k = 1; k < checkboxes.length; k++)
						checked |= checkboxes[k].checked;
		
					this.getElementsByTagName('img')[0].src = checked ? 'images/topics/TopicCheck.jpg' : 'images/topics/Topiccorner.jpg';
				}; // taa injaa comment
			
				cell = row.insertCell(0);
				cell.rowSpan = 3;
				cell.style.width = "100%";
				cell.style.paddingRight = "37px";
				
				cell = row.insertCell(1);
				table = document.createElement("table");
				cell.appendChild(table);
				table.className = "topicRow2";				
			}*/
		
			row = table.insertRow(-1);

			for (j = 0; j < 4; j++)
			{
				cell = row.insertCell(2 * j);
				cell.style.width = "10px";
			//	cell.style.verticalAlign = "top";
//				cell.style.paddingTop = "10px";
	
				cell.innerHTML = "&nbsp;";
				if (String(megaCategories[i][4 * l + j]) != "undefined")
					cell.innerHTML = "<input type='checkbox' id='" + megaCategories[i][4 * l + j] + "' " + ((megaCategories[i][4 * l + j] == catcode) ? "checked='true'" : "") + "' name='cat' onclick='selectByTopic(\"" + megaCategories[i][4 * l + j] + "\"," + i + ", this);'>";
				else
					cell.style.width = "20px";
	
				
				cell = row.insertCell(2 * j + 1);
			//	cell.style.verticalAlign = "top";
			//	cell.style.paddingTop = "10px";
				
				
//				if (j != 0)
					cell.style.width = widths[4 * l + j];
//				else
//					cell.style.width = "130px";
					
				cell.innerHTML = "&nbsp;";
				if (String(megaCategories[i][j]) != "undefined")
					for (k = 0; k < subCategories.length; k++)
						if (subCategories[k][0] == megaCategories[i][4 * l + j])
							cell.innerHTML = subCategories[k][2];					
			}
		}
		
		row = topicsTable.insertRow(-1);
		cell = row.insertCell(0);
		cell.colSpan = 30;
		cell.className = "topicRow3";
	}
	
	showResults()
}

function changeMenu(active, catcode)
{
	if (activeMenu == active)
		return;
		
	if (String(active).search(/\b0\b|\b2\b|\b4\b/) == -1)
		active = 0;
	if (String(catcode).search(/\d+/) == -1)
		catcode = 100;
	
	activeMenu = active;

	var	images = id("menu").getElementsByTagName("img");
	for (i = 0; i < 6; i++)
	{
		images[i].src = images[i].src.replace(/\.a\./, ".");
		images[i].style.cursor = "pointer";
		if (i % 2 == 1)
		{
			node = (firefox) ? images[i] : images[i].parentNode;
			node.style.display = "inline";			
		}
	}
	
	if (activeMenu > 0)
	{
		node = (firefox) ? images[activeMenu - 1] : images[activeMenu - 1].parentNode;	
		node.style.display = "none";
	}
			
	if (activeMenu < 5)
	{
		node = (firefox) ? images[activeMenu + 1] : images[activeMenu + 1].parentNode;			
		node.style.display = "none";				
	}
	
	images[activeMenu].src = images[activeMenu].src.replace(/\.jpg/, ".a.jpg");	
	images[activeMenu].style.cursor = "default";
	
	switch(activeMenu)
	{
		case 0:
		id("content").style.paddingTop = "5px";
		buildCalendarTab();
		break;
		
		case 2:
		id("content").style.paddingTop = "5px";
		buildIndexTab();
		break;
		
		case 4:
		id("content").style.paddingTop = "0px";
		buildTopicsTab(catcode);
		break;
	}
}

function showResults()
{
	var selectedTopics = ".";		
	checkBoxes = document.getElementsByName("cat");
	for (i = 0; i < checkBoxes.length; i++)
		if (checkBoxes[i].checked)
			selectedTopics += checkBoxes[i].id + ".";

	var selectedSubs = "";
	var results = "";

	if (document.getElementsByName("combination")[0].checked)
	{
		topicsArray = selectedTopics.replace(/^\./, "").replace(/\.$/, "").split("\.");
		subsTopicCount = new Array();
		for (i = 0; i < topicsArray.length; i++)
			for (j = 0; j < subMiddle.length; j++)
				if (subMiddle[j][1] == topicsArray[i])
				{
					if (String(subsTopicCount[subMiddle[j][2]]) == "undefined")
						subsTopicCount[subMiddle[j][2]] = 0;
					subsTopicCount[subMiddle[j][2]]++;
				}	
		
		for (k = 0; k < subsTopicCount.length; k++)
			if (subsTopicCount[k] == topicsArray.length)
				selectedSubs += k + ","
		
		selectedSubs = "," + selectedSubs;
		
		var array = new Array();
		
		for (i = 0; i < subs.length; i++)
			if (selectedSubs.indexOf("," + String(subs[i][0]) + ",") != -1)
				array.push(subs[i][1] + "<li style='padding-bottom:5px; font-size:9pt;position:relative;background-image:url(images/topics/dot.bmp);background-repeat:repeat-x;background-position:0px 8px;'><a style='text-decoration:none; position:absolute; left:0px; background-color:white;' href='http://www.roshd.org/eng/subs/show.asp?code=" + subs[i][0] + "'>" + subs[i][2] + " : " + subs[i][3] + "&nbsp;</a>&nbsp;<span dir='ltr' style='position:absolute; right:0px;background-color:white;color:rgb(0, 103, 238);'>&nbsp;(" + subs[i][1] + ")</span></li>");
	}
	else
	{
		selectedSubs = "";
		for (i = 0; i < subMiddle.length; i++)
			if (selectedTopics.indexOf("." + subMiddle[i][1].toString() + ".") != -1)
				selectedSubs += "." + subMiddle[i][2] + ".";
	
		var array = new Array();
		for (i = 0; i < subs.length; i++)
			if (selectedSubs.indexOf("." + String(subs[i][0]) + ".") != -1)
				array.push(subs[i][1] + "<li style='padding-bottom:5px; font-size:9pt;position:relative;background-image:url(images/topics/dot.bmp);background-repeat:repeat-x;background-position:0px 8px;'><a style='text-decoration:none; position:absolute; left:0px; background-color:white;' href='http://www.roshd.org/eng/subs/show.asp?code=" + subs[i][0] + "'>" + subs[i][2] + " : " + subs[i][3] + "&nbsp;</a>&nbsp;<span dir='ltr' style='position:absolute; right:0px;background-color:white;color:rgb(0, 103, 238);'>&nbsp;(" + subs[i][1] + ")</span></li>");
	}
	
	array.sort().reverse();
	for (i = 0; i < array.length; i++)
		results += array[i].substr(10);

	if (selectedTopics == ".")
		results = "<div style='color:#808080; text-align:center;'>By choosing any of the topics, the occasion letters relevant to that topic will be presented.</div>";
	else if (results == "")
		results = "<div style='color:black; text-align:center;'>There is no occasion letter about the selected topics.</div>";
	else
		results = "<ol style='margin-top:10px;'>" + results + "</ol>";
	
	id("results").innerHTML = results;
}

