var month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

function reloadVerification(anc)
{
	$(anc).setStyle('backgroundImage', 'url(../z_images/content/misc/verification.php?cache='+(new Date()).getTime()+')');
	document.posted = false;
}

function doCommentPost()
{
	if(!document.posted)
	{
		document.posted 	= true;
		
		var name 			= $('commentname').getValue();
		var website 		= $('commentwebsite').getValue();
		var verification	= $('commentverification').getValue();
		var copy			= $('commentcopy').getValue().split("\n").join("<br/>").split("\r").join("");
		var blogentryid		= $('commentblogentryid').getValue();
		var objPost			= {bAjax:true, blogid:blogentryid, name:name, website:website, verification:verification, copy:copy};
		var disallowed		= "&";
		var bOK				= true;
		for(var i = 0; i < disallowed.length; i++)
			if(copy.indexOf(disallowed.charAt(i))>=0)
			{
				document.posted = false;
				bOK = false;
				break;
			}
			
		if(name && verification && copy && blogentryid && bOK)
		{
			new Ajax("../z_ajax/ajax_handler.php", {onComplete:outputToBlog, postBody:objPost}).request();
		}
		else
		{
			document.posted = false;
			alert("You must fill in every field (except the website address) and you may not put any of the following characters in your comment: "+disallowed);
		}
	}
	else
		alert("You have already submitted a post. Please re-enter the code or click on the verification code, enter the new code and submit again.");
		
	return false;
}

function doEmailPost()
{
	if(!document.posted)
	{
		document.posted 	= true;
		
		var name 			= $('yourname').getValue();
		var verification	= $('yourverification').getValue();
		var copy			= $('yourmessage').getValue().split("\n").join("<br/>").split("\r").join("");
		var objPost			= {name:name, verification:verification, copy:copy, sendmail:true};
		var disallowed		= "&";
		var bOK				= true;
		for(var i = 0; i < disallowed.length; i++)
			if(copy.indexOf(disallowed.charAt(i))>=0)
			{
				document.posted = false;
				bOK = false;
				break;
			}
			
		if(name && verification && copy && bOK)
		{
			new Ajax("../z_ajax/ajax_handler.php", {onComplete:showResponse, postBody:objPost}).request();
		}
		else
		{
			document.posted = false;
			alert("You must fill in every field (except the website address) and you may not put any of the following characters in your comment: "+disallowed);
		}
	}
	else
		alert("You have already submitted a post. Please re-enter the code or click on the verification code, enter the new code and submit again.");
		
	return false;
}

function showResponse(objResponseText, objResponseXML)
{
	if(objResponseXML)
	{
		for(var i = 0; i < objResponseXML.childNodes.length; i++)
		{
			if(objResponseXML.childNodes[i].nodeName == "response")
			{
				var objResponseValue 	= Json.evaluate(objResponseXML.childNodes[i].firstChild.nodeValue);
				if(objResponseValue.strError)
				{
					alert(objResponseValue.strError);
				}
				else
				{
					$('contactformdetails').setOpacity(0);
					$('contactformdetails').setHTML("Your message was sent. Thanks very much.");
					new Fx.Style($('contactformdetails'), 'opacity', {duration:1500, transition:Fx.Transitions.expoOut}).start(0,1);
					
				}
			}
		}
	}
}

function dtSuffix(day)
{
	if(day < 10 || day > 20)
	{
		if(day%10==1)
			strSuffix = "st";
		else if(day%10==2)
			strSuffix = "nd";
		else if(day%10==3)
			strSuffix = "rd";
		else
			strSuffix = "th";
	}
	else
		strSuffix = "th";
		
	return strSuffix;
}

function outputToBlog(objResponseText, objResponseXML)
{
	if(objResponseXML)
	{
		for(var i = 0; i < objResponseXML.childNodes.length; i++)
		{
			if(objResponseXML.childNodes[i].nodeName == "response" && objResponseXML.childNodes[i].childNodes.length>0)
			{
				var objResponseValue 	= Json.evaluate(objResponseXML.childNodes[i].firstChild.nodeValue);
				if(objResponseValue.strError)
				{
					alert(objResponseValue.strError);
				}
				else
				{
					var divBlogEntry 		= new Element('div');
					divBlogEntry.addClass('blogcomment');
					divBlogEntry.setProperty('id', 'blogcomment'+objResponseValue.fAjaxInsert);
					h3Title					= new Element('h3');
					var strNameLink			= "("+($$('.blogcomment').length+1)+") "+objResponseValue.fName;
					var dtCommentDate		= new Date();
					var strDate				= " :: " + dtCommentDate.getDate()+dtSuffix(dtCommentDate.getDate())+" "+month[dtCommentDate.getMonth()]+", "+dtCommentDate.getFullYear();
					if(objResponseValue.fWebsite)
						h3Title.setHTML('<a href="'+objResponseValue.fWebsite+'">'+strNameLink+'</a>' + strDate);
					else
						h3Title.setHTML(strNameLink + strDate);
					var pCommentCopy		= new Element('p');
					pCommentCopy.setHTML(objResponseValue.fComment);
					divBlogEntry.adopt(h3Title);
					divBlogEntry.adopt(pCommentCopy);
					if($('blogcomment'+(objResponseValue.fAjaxInsert-1)))
					{
						divBlogEntry.injectBefore($('blogcomment'+($$('.blogcomment').length)));
					}
					else
						$('blogcommentslist').adopt(divBlogEntry);
					
					divBlogEntry.setOpacity(0);
					new Fx.Style(divBlogEntry, 'opacity', {duration:1500, transition:Fx.Transitions.expoOut}).start(0,1);
				}
			}
		}
	}
	else
		alert("Error: "+objResponseText);
}

Window.onDomReady( function()
				   {
					   	var arrAnchors 		= $$("a");
						var arrAnchorHrefs	= new Array();
						var iAnchorCount	= 1;
						arrAnchors.each(function(objAnchor)
										{
											if(Window.ie7 && !objAnchor.hasClass('nounderscore'))
												objAnchor.setHTML("_"+objAnchor.innerHTML);
											
											var strHref 	= objAnchor.getProperty("href");
											var divSupId	= new Element("sup");
											if(!arrAnchorHrefs[strHref])
												arrAnchorHrefs[strHref] = iAnchorCount++;
												
											divSupId.setHTML(arrAnchorHrefs[strHref]);
											divSupId.injectAfter(objAnchor);
										});
						
						divAnchorList		= new Element("div");
						divAnchorList.addClass("hidden");
						strHref				= "";
						
						for(var i in arrAnchorHrefs)
						{
							if(typeof(arrAnchorHrefs[i])=="number")
								strHref			+= "<li>"+i+"</li>";
						}
						
						divAnchorList.setHTML("<hr/><h2>Links on this page:</h2><ol>"+strHref+"</ol>");
						
						$(document.body).adopt(divAnchorList);
				   });