// JavaScript Document
function parse_xml(xmlURL)
{
	var xmlDoc=null;
	var xmlhttp=null;
	
	if (window.XMLHttpRequest)
	{// code for Safari Mozilla, Firefox, Opera, IE 7 and later etc.
		xmlhttp=new XMLHttpRequest();
		if (xmlhttp!=null)
		{
			try
			{
				xmlhttp.open("GET", xmlURL, false);
				xmlhttp.setRequestHeader("Pragma", "no-cache");
				xmlhttp.setRequestHeader("Cache-Control", "no-cache, must-revalidate");
				xmlhttp.send("");
				xmlDoc = xmlhttp.responseXML;
			}
			catch(e) {alert(e.message)}
		}
	}
	else if (window.ActiveXObject)
	{// code for IE 5, 6
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		try
		{
			xmlDoc.async=false;
			xmlDoc.load(xmlURL);
		}
		catch(e) {alert(e.message)}
	}
	return xmlDoc;
}

function load_blog (xmlURL)
{
	var xmlDoc=null;

	xmlDoc=parse_xml(xmlURL);
	if (xmlDoc != null)
	{
		notes=xmlDoc.getElementsByTagName('note');
		html=get_blog(notes);
	}
	else
		html='Your browser does not support XML parsing.';
	document.getElementById('content').innerHTML=html;
}

function load_notes(xmlURL)
{
	var xmlDoc=null;

	xmlDoc=parse_xml(xmlURL);
	if (xmlDoc != null)
	{
		notes=xmlDoc.getElementsByTagName('note');
		html=get_content(notes);
	}
	else
		html='Your browser does not support XML parsing.';
	document.getElementById('content').innerHTML=html;
}

function get_content(notes)
{
	var html="";
	var x, notes, child_nodes, i, j, pics, date_posted;
	
	for (i=0;i<notes.length;i++)
	{
		html += "<table cellpadding='0' cellspacing='0' style='width:676px; table-layout:fixed;'><tr><td>";
		date_posted=notes[i].getElementsByTagName("date");
		if (date_posted!=null)
		{
			html+="<h3 style='display:inline;'>Posted: ";
			html+=date_posted[0].childNodes[0].nodeValue;
			html+="</h3>";
		}
		pics=notes[i].getElementsByTagName("img");
		if (pics!=null && pics.length>0)
		{
			html+='<div class="body_pic" style="max-width:304px;">';
			for (j=0;j<pics.length;j++)
			{
				html+='<img class="body_pic_img" src="blogs/user_images/';
				try {
					html+=pics[j].childNodes[0].nodeValue; }
				catch (e) {html+="error";}
				html+='" alt="" />';
				if (j+1<pics.length)
					html+='<div style="height:10px;"></div>';
			}
			html+="</div>";
		}
		child_nodes=notes[i].childNodes;
		for (j=0;j<child_nodes.length;j++)
		{
			switch (child_nodes[j].nodeName)
			{
				case "h1":
					html+="<h1>";
					html+=child_nodes[j].childNodes[0].nodeValue;
					html+="</h1>";
					break;
				case "h3":
					html+="<h3>";
					html+=child_nodes[j].childNodes[0].nodeValue;
					html+="</h3>";
					break;
				case "p":
					html+="<p>";
					html+=child_nodes[j].childNodes[0].nodeValue;
					html+="</p>";
			}
		}
		if ((i+1) != notes.length)
				html+="</td></tr><tr><td style='height:10px;'><div style='height:0px; border-bottom:1px dashed #CCC;'></div></td></tr><tr><td style='height:10px;'>";
		html += "</td></tr></table>";
	}
	return html;
}

function get_blog(notes)
{
	var html="";
	var x, notes, child_nodes, i, j, pics, date_posted;
	
	for (i=0;i<notes.length;i++)
	{
		date_posted=notes[i].getElementsByTagName("date");
		if (date_posted!=null)
		{
			html+="<h3 style='text-align:left;'>Posted: ";
			html+=date_posted[0].childNodes[0].nodeValue;
			html+="</h3>";
		}
		child_nodes=notes[i].childNodes;
		for (j=0;j<child_nodes.length;j++)
		{
			switch (child_nodes[j].nodeName)
			{
				case "h1":
					html+="<h1>";
					try {
					html+=child_nodes[j].childNodes[0].nodeValue;}
					catch (e){html+="error"}
					html+="</h1>";
					break;
				case "h3":
					html+="<h3 style='text-align:left;'>";
					try {
					html+=child_nodes[j].childNodes[0].nodeValue;}
					catch (e){html+="error"}
					html+="</h3>";
					break;
				case "p":
					html+="<p style='text-align:left;'>";
					try {
					html+=child_nodes[j].childNodes[0].nodeValue;}
					catch (e){html+="error"}
					html+="</p>";
					break;
				case "img":
					html+='<img class="photo" style="margin-bottom:10px;" src="user_images/';
					try {
					html+=child_nodes[j].childNodes[0].nodeValue;}
					catch (e){html+="error"}
					html+='" alt="" />';
			}
			if ((j+1) == child_nodes.length && (i+1) != notes.length)
				html+="<div style='height:0px; border-bottom:1px dashed #CCC;'></div>";
		}
	}
	return html;
}

function toggle(targetId)
{
	if ( document.getElementById )
	{
		target = document.getElementById( targetId );
		if ( target.style.display == "none" )
		{
			target.style.display = "";
		}
		else
		{
			target.style.display = "none";
		}
	}
}

function swap(imageId)
{
	if(document.getElementById)
	{
		if(document.getElementById(imageId).src.indexOf('toggle-arrow.gif')>-1)
		{
			document.getElementById(imageId).src = "../images/expand-arrow.gif";
		}
		else
		{
			document.getElementById(imageId).src = "../images/toggle-arrow.gif";
		}
		return false;
	}
	else
	{
		return true;
	}
}

function detect()
{
	var user_agent=navigator.userAgent;
	var ie=/msie/i;
	var ff=/firefox/i;
	var s=/safari/i;
	
	if (ie.test(user_agent))
		return "ie";
	else
		if (ff.test(user_agent))
			return "ff";
		else
			if (s.test(user_agent))
				return "s";
}

function add_h1()
{
	var i, table, row, cell_0, cell_1, cell_2;
	
	
	table=document.getElementById('user_content');
	i=table.rows.length;
	row=table.insertRow(i);
	cell_0=row.insertCell(0);
	cell_1=row.insertCell(1);
	cell_2=row.insertCell(2);
	cell_0.innerHTML="Large Heading:";
	cell_0.style.paddingLeft="5px";
	cell_1.innerHTML="<input name='h1' id='h1' value='' type='text' style='width:840px' />";
	cell_2.innerHTML="<a href='javascript:del_row("+i+");' name='delete_button'><img style='border-width:0px;' src='../images/del.gif' /></a>";
	cell_2.style.textAlign="center";
}

function add_h3()
{
	var i, table, row, cell_0, cell_1, cell_2;
	
	table=document.getElementById('user_content');
	i=table.rows.length;
	row=table.insertRow(i);
	cell_0=row.insertCell(0);
	cell_1=row.insertCell(1);
	cell_2=row.insertCell(2);
	cell_0.innerHTML="Small Heading:";
	cell_0.style.paddingLeft="5px";
	cell_1.innerHTML="<input name='h3' id='h3' value='' type='text' style='width:840px' />";
	cell_2.innerHTML="<a href='javascript:del_row("+i+");' name='delete_button'><img style='border-width:0px;' src='../images/del.gif' /></a>";
	cell_2.style.textAlign="center";
}

function add_p()
{
	var i, table, row, cell_0, cell_1, cell_2, cols, browser;
	
	user_browser=detect();
	if (user_browser == "ie")
	{
		cols = 103;
	}
	else
	{
		if(user_browser == "ff")
		{
			cols = 106;
		}
		else
		{
			if(user_browser == "s")
			{
				cols = 118;
			}
		}
	}
	table=document.getElementById('user_content');
	i=table.rows.length;
	row=table.insertRow(i);
	cell_0=row.insertCell(0);
	cell_1=row.insertCell(1);
	cell_2=row.insertCell(2);
	cell_0.innerHTML="Paragraph:";
	cell_0.style.paddingLeft="5px";
	cell_1.innerHTML="<textarea name='p' id='p' value='' type='text' cols='"+cols+"' rows='5' ></textarea>";
	cell_2.innerHTML="<a href='javascript:del_row("+i+");' name='delete_button'><img style='border-width:0px;' src='../images/del.gif' /></a>";
	cell_2.style.textAlign="center";
}

function add_img()
{
	var i, table, row, cell_0, cell_1, cell_2;
	
	table=document.getElementById('user_content');
	i=table.rows.length;
	row=table.insertRow(i);
	cell_0=row.insertCell(0);
	cell_1=row.insertCell(1);
	cell_2=row.insertCell(2);
	cell_0.innerHTML="Picture:";
	cell_0.style.paddingLeft="5px";
	cell_1.innerHTML='<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />';
	cell_1.innerHTML="<input name='img' id='img' type='file' style='width:840px' />";
	cell_2.innerHTML="<a href='javascript:del_row("+i+");' name='delete_button'><img style='border-width:0px;' src='../images/del.gif' /></a>";
	cell_2.style.textAlign="center";
}

function del_row(row)
{
	var anchors, i;
	
	document.getElementById("user_content").deleteRow(row);
	anchors=document.getElementsByName("delete_button");
	if(anchors.length>0)
		for(i=0; i<anchors.length; i++)
			anchors[i].href="javascript:del_row("+(i+1)+");";
}

function check_add_form()
{
	/*  the inputs index is set to 0, not i because as each name is changed, the next one becomes 0  */
	var i, rows, content, input;
	
	if (document.getElementById("action").value=="Cancel")
		return true;
	content=false;
	rows=document.getElementById("user_content").rows;
	document.getElementById("content_inputs").innerHTML="<input type='hidden' name='elements' value='"+(rows.length-1)+"' />";
	for(i=0;i<rows.length;i++)
	{
		switch(rows[i].cells[0].innerHTML)
		{
			case "Large Heading:":
				rows[i].cells[1].getElementsByTagName("input")[0].name="h1_"+i;
				document.getElementById("content_inputs").innerHTML+="<input type='hidden' name='t"+i+"' value='h1' />";
				content=true;
				break;
			case "Small Heading:":
				rows[i].cells[1].getElementsByTagName("input")[0].name="h3_"+i;
				document.getElementById("content_inputs").innerHTML+="<input type='hidden' name='t"+i+"' value='h3' />";
				content=true;
				break;
			case "Paragraph:":
				rows[i].cells[1].getElementsByTagName("textarea")[0].name="p_"+i;
				document.getElementById("content_inputs").innerHTML+="<input type='hidden' name='t"+i+"' value='p' />";
				content=true;
				break;
			case "Picture:":
				rows[i].cells[1].getElementsByTagName("input")[0].name="img_"+i;
				document.getElementById("content_inputs").innerHTML+="<input type='hidden' name='t"+i+"' value='img' />";
				content=true;
				break;
		}
	}
	if (content)
		return true;
	else
	{
		document.getElementById("message").innerHTML='<span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; font-weight:bold; color:#FFF; background-color: #F00; padding:5px;">No Content Present</span>';
		return false;
	}
}