﻿addEvent(window, "load", textAreasInit);
function addEvent(elm, evType, fn, useCapture)
{
	if (elm.addEventListener)
	{
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} 
	else if (elm.attachEvent)
	{
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	} 
	else
	{
		alert("Handler could not be removed");
	}
	return false;
}
function textAreasInit()
{
	var objs = document.getElementsByTagName("textarea");
	var oi = 0; //oi is object index
	var thisObj;
	for (oi=0; oi<objs.length; oi++)
	{
		thisObj = objs[oi];
		// note that MaxLength is case sensitve
		if (thisObj.getAttribute('MaxLength'))
		{
			thisObj.onkeyup = forceMaxLength;
			thisObj.onchange = forceMaxLength;
			thisObj.onfocus = forceMaxLength;
		}
	}
	return null;
}
function forceMaxLength()
{
	if(document.getElementById(this.getAttribute('id') + 'WordCount'))
	{
		document.getElementById(this.getAttribute('id') + 'WordCount').value = countWords(this.value)
		var maxLength = parseInt(this.getAttribute('MaxLength'));
		if((maxLength > 0) && (countWords(this.value) > maxLength))
		{
			this.value = this.value.substring(0, getWordCountOveragePosition(this.value, maxLength));
			alert('Word limit (' + this.getAttribute('MaxLength') + ') exceeded')
		}
	}
	return null;
}
function countWords(strElementValue)
{
	strElementValue = strElementValue.replace('\n',' ');
	var arrstrElementValue = strElementValue.split(' ');
	var intWordCount = 0;
	for (var i = 0 ; i < arrstrElementValue.length ; i++)
	{ 
		if (trim(arrstrElementValue[i]) != "")
		{
			intWordCount++; 
		} 
	}	
	strElementValue = null;
	arrstrElementValue = null;
	return intWordCount;
}
function getWordCountOveragePosition(strElementValue, intMaxWordCount)
{
	strElementValue = strElementValue.replace('\n',' ');
	var arrstrElementValue = strElementValue.split(' ');
	var intWordCount = 0;
	var intPosition = 0;
	for (var i = 0 ; i < arrstrElementValue.length ; i++)
	{ 
		intPosition = intPosition + arrstrElementValue[i].length + 1
		if (trim(arrstrElementValue[i]) != "")
		{
			intWordCount++;
		}
		if (intWordCount == intMaxWordCount)
		{
			return intPosition;
		}
	}
	return intPosition;
}
function logoutSession()
{
	window.location = 'AbandonSession.asp';
}
function setTitle(strTitle)
{
	document.title = strTitle;
}
function retrievePassword ()
{
	if (document.frmJudgingLogin.Username.value != '')
	{
		document.frmJudgingLogin.action = "retrievePassword.asp";
		document.frmJudgingLogin.submit();
	}
	else
	{
		alert("Please provide your username.")
		document.frmJudgingLogin.Username.focus();
	}
}

function trim(strString)
{
	while (strString.substring(0,1) == ' ')
	{
		strString = strString.substring(1, strString.length);
	}
	while (strString.substring(strString.length-1, strString.length) == ' ')
	{
		strString = strString.substring(0,strString.length-1);
	}
	return strString;
}
function viewChecklist ()
{
	frmChecklistLogin.action = "viewChecklist.asp";
	frmChecklistLogin.submit();
}