function setCookie(strName, strValue)
{
	var objDate = new Date();
	
	// Set expiration as 1 year in seconds
	objDate.setTime(objDate.getTime() + 31536000);

	document.cookie = strName + '=' + strValue + '; expires=' + objDate.toGMTString() + '; path=/';
}


function isString(strValue)
{
	return (typeof strValue == 'string' && strValue !== '' && isNaN(strValue));
}

function isEmail(strValue)
{
	var objRE = /^[\w\-\.\']{1,}\@([\da-zA-Z\-]{1,}\.){1,}[\da-zA-Z\-]{2,}$/;

	return (strValue !== '' && objRE.test(strValue));
}

function toggleStyle(objAnchor)
{
	var strLinkPhrase = objAnchor.firstChild.data;
	var objNewNode;

	if (strLinkPhrase == 'Switch to High Contrast')
	{
		objNewNode = document.createTextNode('Switch to Regular Layout');
		objAnchor.replaceChild(objNewNode, objAnchor.firstChild);
		objAnchor.title = 'Switch the current style sheet to the default style sheet';
		setCookie("style", "alt");
	}
	else
	{
		objNewNode = document.createTextNode('Switch to High Contrast');
		objAnchor.replaceChild(objNewNode, objAnchor.firstChild);
		objAnchor.title = 'Switch the current style sheet to a high contrast style sheet';
		setCookie("style", "regular");
	}

	for(j=0; j<document.styleSheets.length; j++)
	{
		if (document.styleSheets[j].title)
		{
			document.styleSheets[j].disabled = !document.styleSheets[j].disabled;
		}
	}

	return false;
}

function  addStyleEvents()
{
	var objAnchor = document.getElementById("togstyle");

	if (objAnchor)
	{
		objAnchor.onclick = function(){return toggleStyle(this);};
	}

	return true;
}

function furnishExternals()
{
	var objAnchors = document.getElementsByTagName('a');
	var objImages, objSpan;

	for (var iCounter=0; iCounter<objAnchors.length; iCounter++)
	{
		objImages = objAnchors[iCounter].getElementsByTagName('img');

		if (objImages.length === 0 && objAnchors[iCounter].href.indexOf('http://juicystudio') == -1 && objAnchors[iCounter].href.indexOf('http://www.juicystudio') == -1 && objAnchors[iCounter].href.indexOf('http://localhost') == -1)
		{
			objSpan = document.createElement('span');
			objSpan.appendChild(document.createTextNode('\u00a0'));
			objSpan.className = 'externallink';
			objAnchors[iCounter].parentNode.insertBefore(objSpan, objAnchors[iCounter].nextSibling);
		}
	}
}

function addIETabOrder()
{
	var objHeadings = document.getElementsByTagName('h2');
	
	for (var iCounter=0; iCounter<objHeadings.length; iCounter++)
	{
		objHeadings[iCounter].tabIndex = -1;
	}
}

function isNested(objElement)
{
	var objParent = objElement;
	do
	{
		objParent = objParent.parentNode;
		if (objParent.tagName && objParent.tagName.toLowerCase() == 'q')
		{
			return true;
		}
	} while (objParent.parentNode);

	return false;
}

function fixIEQuotes()
{
	var objQuotes = document.getElementsByTagName('q');
	var strOpen, strClose;

	for (var iCounter=0; iCounter<objQuotes.length; iCounter++)
	{
		if (isNested(objQuotes[iCounter]))
		{
			strOpen = document.createTextNode('\u2018');
			strClose = document.createTextNode('\u2019');
		}
		else
		{
			strOpen = document.createTextNode('\u201c');
			strClose = document.createTextNode('\u201d');
		}
		objQuotes[iCounter].parentNode.insertBefore(strOpen, objQuotes[iCounter]);
		objQuotes[iCounter].appendChild(strClose);
	}

	objQuotes = null;
}

function addARIARole(strID, strRole)
{
	var objElement = document.getElementById(strID);

	if (objElement)
	{
		objElement.setAttribute('role', strRole);
	}
}

function addARIARequired()
{
	var objName = document.getElementById('contactname');
	var objComment = document.getElementById('comment');

	if (objName)
	{
		objName.setAttribute('aria-required', 'true');
	}

	if (objComment)
	{
		objComment.setAttribute('aria-required', 'true');
	}
}

function checkComment(objComment)
{
	var strName = objComment.contactname.value.replace(/^\s*|\s*$/g, '');
	var strEmail = objComment.email.value.replace(/^\s*|\s*$/g, '');
	var strComment = objComment.comment.value.replace(/^\s*|\s*$/g, '');

	if (isString(strName) === false)
	{
		alert('Please enter your name');
		objComment.contactname.focus();
		return false;
	}
	if (isString(strComment) === false)
	{
		alert('Please enter your comment');
		objComment.comment.focus();
		return false;
	}
	
	if (strEmail !== '')
	{
		if (isEmail(strEmail) === false)
		{
			alert('Please ensure your email address is valid. Email is an optional field, so you may leave it blank if you prefer');
			objComment.email.focus();
			return false;
		}
	}

	return true;
}

function initialise()
{
	var objCommentform = document.getElementById('articlecommentform');

	furnishExternals();
	addStyleEvents();

	// Add ARIA document landmark roles
	addARIARole('content', 'main');
	addARIARole('navcontainer', 'navigation');
	addARIARole('searchform', 'search');
	addARIARole('searchpage', 'search');
	addARIARole('secondary', 'complementary');

	if (objCommentform)
	{
		objCommentform.onsubmit = function(){return checkComment(this);};
		addARIARequired();
	}

	if (!window.opera && navigator.userAgent.indexOf('MSIE') != -1)
	{
		fixIEQuotes();
		addIETabOrder();
	}
}

window.onload = initialise;

// ö
