var whitespace = " \t\n\r";
var browser = new browserDetectLite();

function setEnableDisable(control, state)
{
	var c = findObject(control);

	if (c != null)
	{
		c.disabled = !state;

		if (c.tagName == 'INPUT' && c.type == 'text')
		{
			c.readOnly = !state;
		}

		if (c.tagName == 'A')
		{
			c.style.color = state ? '' : '#ACA899';
		}

		if (c.tagName != 'SELECT' && c.childNodes.length > 0)
		{
			disableEnableChildren(c, state, setEnableDisable.arguments.length < 3);
		}
	}

	c = null;
}

function disableEnableChildren(control, enable, disableTextTag)
{
	if (control.tagName == 'INPUT')
	{
		if (control.type != 'text')
		{
			control.disabled = !enable;
		}
		else
		{
			if (disableTextTag)
			{
				control.disabled = !enable;
			}

			control.readOnly = !enable;
		}
	}

	if (control.tagName == 'SELECT' || control.tagName == 'A' || control.tagName == 'TD' || control.tagName == 'SPAN')
	{
		control.disabled = !enable;
		control.style.disabled = !enable;
	}

	if (control.tagName == 'TD' || control.tagName == 'A' || control.tagName == 'SPAN')
	{
		if (!control.disabled)
		{
			control.removeAttribute('disabled', false);
		}
		else
		{
			control.setAttribute('disabled', 'disabled');
		}
	}
	
	if (control.tagName == 'A')
	{
		control.style.color = enable ? '' : '#ACA899';
	}

	if (control.tagName != 'SELECT')
	{
		for (var i = 0; i < control.childNodes.length; i++)
		{
			disableEnableChildren(control.childNodes.item(i), enable, disableTextTag);
		}
	}
}

function findObject(n, d)
{
	var p, i, x;

	if (!d)
	{
		d = document;

		if ((p = n.indexOf("?")) > 0 && parent.frames.length)
		{
			d = parent.frames[n.substring(p + 1)].document;
			n = n.substring(0, p);
		}

		if (!(x=d[n]) && d.all) x = d.all[n];

		for (i=0; !x && i < d.forms.length; i++) x = d.forms[i][n];

		for (i=0; !x && d.layers && i < d.layers.length; i++) x = findObject(n, d.layers[i].document);

		if (!x && d.getElementById) x = d.getElementById(n);

		return x;
	}
	else
	{
		return findObject_recursive(n, d);
	}
}

function findAttribute(control, attribute)
{
	var __val_IE = (document.all);
	var __val_DOM = (document.getElementById);

	var attrib;

	if (__val_DOM)
	{
		attrib = control.getAttribute(attribute, false);
	}
	else
	{
		attrib = eval("document." + (__val_IE) ? "all." : (__val_DOM) ? "getElementById(\"" : ""
			+ control.id + "." + attribute + (_val_DOM && !__val_IE) ? "\")" : "");
	}

	return attrib;
}

function findObject_recursive(n, d)
{
	for (var i = 0; i < d.childNodes.length; i++)
	{
		if (d.childNodes.item(i).id == n)
		{
			return d.childNodes.item(i);
		}
		var fObj = findObject_recursive(n, d.childNodes.item(i));

		if (fObj != null) return fObj;
	}

	return null;
}

function preloadImages()
{
	var d = document;

	if (d.images)
	{
		if(!d.MM_p) d.MM_p = new Array();

		var i, j = d.MM_p.length, a = preloadImages.arguments;

		for (i = 0; i < a.length; i++)
		{
			if (a[i].indexOf("#") != 0)
			{
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
		}
	}
}

function setFocus(control)
{
	var c = findObject(control);

	if (c != null && c.focus)
	{
		c.focus();
	}
}

function textCounter(itemName, maxLimit)
{
	var field = findObject(itemName);

	if ((field != null) && (field.value.length > maxLimit))
	{
		field.value = field.value.substring(0, maxLimit);
	}
}

function replaceAll(s, fromStr, toStr)
{
	var new_s = s;

	for (var i = 0; i < 100 && new_s.indexOf (fromStr) != -1; i++)
	{
		new_s = new_s.replace (fromStr, toStr);
	}

	return new_s;
}

function isEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

function RTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var c = "";
	var endpos = 0

	for (i = str.length; i >= 0 && endpos == 0; i = i - 1)
	{
		c = str.charAt(i);

		if (whitespace.indexOf(c) == -1)
		{
			endpos = i;
		}
	}

	return str.substring(0, endpos + 1);
}

function regularExpressionValidator(value, expression)
{
	var rx = new RegExp(expression);
	var matches = rx.exec(value);

	return r = (matches != null && value == matches[0]);
}

function isFileName(value)
{
	if (navigator.appVersion.toLowerCase().indexOf("mac") > 0)
	{
		return true;
	}

	if (window.opera)
	{
		value = "c:\\" + value;
	}

	return regularExpressionValidator(value, "([a-zA-Z]:\\\\[^/:\\*\\?<>\\|]+\\.\\w{2,6})|(\\\\{2}[^/:\\*\\?<>\\|]+\\.\\w{2,6})");
}

function isEmailList(value)
{
	return regularExpressionValidator(value, "^(([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5}){1,25})+)*$");
}

function getCheckBoxEnabled(val)
{
	var isEnabled = false;
	
	if (val != null && val.tagName == 'INPUT')
	{
		if (val.type == 'checkbox' && !val.disabled)
		{
			isEnabled = true;
		}
	}

	var childItems = val.childNodes;

	if (childItems != null && isEnabled == false)
	{
		for (var i = 0; i < childItems.length; i++)
		{
			isEnabled = getCheckBoxEnabled(childItems.item(i));
			
			if (isEnabled)
			{
				break;
			}
		}
	}

	return isEnabled;
}

function getCheckBoxChecked(val)
{
	var isChecked = false;

	if (val != null && val.tagName == 'INPUT')
	{
		if (val.type == 'checkbox' && !val.disabled)
		{
			if (val.checked)
			{
				isChecked = true;
			}
		}
	}

	var childItems = val.childNodes;

	if (childItems != null && isChecked == false)
	{
		for (var i = 0; i < childItems.length; i++)
		{
			isChecked = getCheckBoxChecked(childItems.item(i));
			
			if (isChecked)
			{
				break;
			}
		}
	}

	return isChecked;
}

function GetCountCheckedItem(parentName)
{
	var target = findObject(parentName);

	return getCheckedCount(target);
}

function getCheckedCount(target)
{
	var count = 0;

	if (target != null)
	{
		var col = target.childNodes;

		if (col != null)
		{
			for (var i = 0; i < col.length; i++)
			{
				if (col.item(i).tagName == 'INPUT')
				{
					if(col.item(i).checked)
					{
						count++;
					}
				}
				else
				{
					count += getCheckedCount(col.item(i));
				}
			}
		}
	}

	return count;
}

function disableFileInputs()
{
	var objects = document.all;

	for (var i = 0; i < objects.length; i++)
	{
		if ((objects.item(i).tagName == 'INPUT') && (objects.item(i).type == 'file'))
		{
			objects.item(i).disabled = true;
		}
	}
}

function validateUploadFile(control, message)
{
	var c = findObject(control);

	var result = true;

	if ((c != null) && !isEmpty(c.value))
	{
		result = isFileName(c.value);
	}

	if (!result)
	{
		alert(message);
	}

	return result;
}

function validateTextFields()
{
	var params = validateTextFields.arguments;
	var curElem, curForm, allForms = document.forms;

	for (var i = 0; i < params.length; i += 2)
	{
		for (var j = 0; j < allForms.length; j++)
		{
			curForm = allForms.item(j);

			for(var k = 0; k < curForm.elements.length; k++)
			{
				curElem = curForm.elements.item(k);

				if ((curElem.tagName == "INPUT") && (curElem.type == "text") 
												 && (curElem.id.indexOf(params[i]) == 0))
				{
					if (isEmpty(curElem.value))
					{
						alert(params[i + 1]);
						curElem.focus();

						return false;
					}
				}
			}
		}
	}

	return true;
}

function validateFileFields(fieldID, message, expression, allowEmpties)
{
	var curElem, curForm, allForms = document.forms;

	for (var i = 0; i < allForms.length; i++)
	{
		curForm = allForms.item(i);

		for (var j = 0; j < curForm.elements.length; j++)
		{
			curElem = curForm.elements.item(j);

			if ((curElem.tagName == "INPUT") && (curElem.type == "file") 
											 && (curElem.id.indexOf(fieldID) != -1))
			{
				if (allowEmpties && isEmpty(curElem.value)) continue;

				var isValidExpression = true;

				if ((expression != null) && (expression.length > 0))
				{
					isValidExpression = regularExpressionValidator(curElem.value, expression);
				}

				if	((isEmpty(curElem.value)) || (!isFileName(curElem.value)) || (!isValidExpression))
				{
					alert(message);
					curElem.focus();

					return false;
				}
			}
		}
	}

	return true;
}

function validateTrackFields(trackControl, trackMessage, fileControl, fileMessage)
{
	var curElem, curForm, allForms = document.forms;

	for (var j = 0; j < allForms.length; j++)
	{
		curForm = allForms.item(j);

		for(var k = 0; k < curForm.elements.length; k++)
		{
			curElem = curForm.elements.item(k);

			if ((curElem.tagName == "INPUT") && (curElem.id.indexOf(trackControl) == 0))
			{
				var pairElem = findObject(fileControl + curElem.id.substr(trackControl.length));

				if (pairElem != null)
				{
					if (isEmpty(curElem.value) && isEmpty(pairElem.value)) return true;

					if (isEmpty(curElem.value))
					{
						alert(trackMessage);
						curElem.focus();

						return false;
					}
					else if (isEmpty(pairElem.value) || !isFileName(pairElem.value) || !regularExpressionValidator(pairElem.value, ".*\.(mp3|MP3|wav|WAV|ra|RA|mid|MID|avi|AVI|mpg|MPG|mpeg|MPEG|mov|MOV|wmv|WMV)$"))
					{
						alert(fileMessage);
						pairElem.focus();

						return false;
					}
				}
			}
		}
	}

	return true;
}

// Session watcher
var clientTimeout = 0;
var expiredLimit = 1800 - 20;
var warningLimit = expiredLimit - 300;
var warnWnd = null;
var warnWndPath = null;
var timerID;

function attachWatch(wndPath)
{
	warnWndPath = wndPath;

	if (window.addEventListener)
	{
		window.addEventListener('load', startWatch, false);
		window.addEventListener('unload', closeWarning, false);
	}
	else
	{
		window.attachEvent('onload', startWatch);
		window.attachEvent('onunload', closeWarning);
	}
}

function resetWatch()
{
	if (warnWnd == null) clientTimeout = 0;
		else if(!warnWnd.closed) warnWnd.focus();
}

function startWatch()
{
	if (timerID != null)
	{
		window.clearInterval(timerID);
	}

	timerID = window.setInterval("watchDog()", 1000);
}

function watchDog()
{
	var el;

	clientTimeout++;

	if (clientTimeout > expiredLimit)
	{
		closeWarning();

		location = "logout.aspx";
	}
	else if (clientTimeout > warningLimit)
	{
		var positionLeft = Math.round (document.body.offsetWidth / 2) - 150 + document.body.scrollLeft;
		var positionTop = Math.round (document.body.offsetHeight / 2) - 125 + document.body.scrollTop;

		if (warnWnd == null)
		{
			warnWnd = window.open(warnWndPath, "_blank", "width=300,height=250,menubar=no,toolbar=no,scrollbars=no,resizable=no,statusbar=no,left=" + positionLeft + ",top=" + positionTop);

			warnWnd.focus();
		}

		if (warnWnd != null && !warnWnd.closed && (el = warnWnd.document.getElementById("expire")))
		{
			el.value = expiredLimit - clientTimeout;
		}
	}
}

function closeWarning()
{
	if (warnWnd != null && !warnWnd.closed)
	{
		warnWnd.close();
	}

	warnWnd = null;

	resetWatch();
}

/* Multiline script */

var names = new Array;
var count;

function addRow(elem){
	tableObj=findParent(elem,'TABLE')
	if (!tableObj.processed) {
		recursive(tableObj,'save')
		tableObj.processed=true
	}
	tableObj.tBodies[0].insertBefore(tableObj.rows[1].cloneNode(true),tableObj.rows[tableObj.rows.length-1])
	recursive(tableObj.rows[tableObj.rows.length-2], 'clear')
	setIndex(tableObj)
}

function remRow(elem){
	tableObj=findParent(elem,'TABLE')
	if (tableObj.rows.length>3){
		trObj=findParent(elem,'TR')
		tableObj.deleteRow(trObj.sectionRowIndex)
	}
}

function findParent(childObj,parentTag){
	try {while (childObj.parentNode.tagName!=parentTag) childObj = childObj.parentNode} catch(childObj){return null}
	return childObj.parentNode
}

function setIndex(tableObj){
	for (i=1;i<tableObj.rows.length;i++){
		count=0
		recursive(tableObj.rows[i], 'set', i)
	}
}

function recursive(parentObj, mode, row){
	var i, childObj, mode;
	childObj=parentObj.childNodes
	for (i=0; i<childObj.length; i++){
		if (childObj[i].tagName=='INPUT' || childObj[i].tagName=='SELECT' || childObj[i].tagName=='TEXTAREA')
			switch (mode){
				case "save":
					names[tableObj,names.length]=childObj[i].name
				break
				case "set":
					childObj[i].id=childObj[i].name=names[tableObj,count]+row.toString()
					count++
				break
				case "clear":
					if (childObj[i].tagName=='INPUT' && childObj[i].type=='file'){
						//childObj[i].setAttribute('VALUE', '', 1)
					}
					else childObj[i].value=''
				break
			}
		if (childObj[i].childNodes.length>0) recursive(childObj[i], mode, row)
	}
}

function checkServerTicketNumber(serverTicketKey, pageClassNameKey, formID)
{
	if (typeof(NFNY) != "undefined")
	{
		var t = findObject(serverTicketKey);
		var c = findObject(pageClassNameKey);

		if (t != null && c != null)
		{
			var result = NFNY.CheckServerTicketNumber(t.value, c.value);

			if (result != null && typeof(result) == "object" && result.error != true)
			{
				var value = result.value;

				if (value != null && value == false)
				{
					var pageUrl = window.location.href;

					if (pageUrl.match(/\?/))
					{
						pageUrl = removeQueryStringParameter(pageUrl, "tsid");

						if (pageUrl.substring(pageUrl.length - 1) != "?")
						{
							pageUrl += "&";
						}
					}
					else
					{
						pageUrl += "?"
					}

					window.location.replace(pageUrl + "tsid=" + new Date().getTime());
				}
			}
		}
	}

	var mainFormObject = document.getElementById(formID);

	if (mainFormObject == null)
	{
		mainFormObject = document.forms[0];
	}

	updateFormAction(mainFormObject);
}

function updateFormAction(form)
{
	if (typeof(form.action) == "undefined") form.action = "";

	if (form.action.match(/\?/))
	{
		form.action = removeQueryStringParameter(form.action, "tsid");

		if (form.action.substring(form.action.length - 1) != "?")
		{
			form.action += "&";
		}
	}
	else
	{
		form.action += "?"
	}

	form.action += "tsid=" + new Date().getTime();
}

function removeQueryStringParameter(queryString, parameter)
{
	var parameterRegExp = new RegExp("&?" + parameter + "=[^&]*");

	if (queryString.match(parameterRegExp))
	{
		return queryString.replace(parameterRegExp, "");
	}

	return queryString;
}

function validateNumericField(e)
{
	var keyCode = (browser.isNSCompatible || browser.isMozilla) ? e.which : event.keyCode;

	if ((keyCode >= 48 && keyCode <= 57) || keyCode == 13 || keyCode == 8 || keyCode == 0)
	{
		return true;
	}
	else
	{
		if (browser.isNSCompatible || browser.isMozilla) e.preventDefault();

		return false;
	}
}

function browserDetectLite()
{
	var ua = navigator.userAgent.toLowerCase();

	this.ua = ua;

	this.isGecko     = (ua.indexOf('gecko') != -1);
	this.isMozilla   = (this.isGecko && ua.indexOf("gecko/") + 14 == ua.length);
	this.isNS        = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('opera') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
	this.isIE        = ( (ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1) ); 
	this.isOpera     = (ua.indexOf("opera") != -1); 
	this.isKonqueror = (ua.indexOf("konqueror") != -1); 
	this.isAol       = (ua.indexOf("aol") != -1); 
	this.isOmniweb   = (ua.indexOf("omniweb") != -1);
	this.isDreamcast   = (ua.indexOf("dreamcast") != -1);

	this.isIECompatible = ( (ua.indexOf("msie") != -1) && !this.isIE);
	this.isNSCompatible = ( (ua.indexOf("mozilla") != -1) && !this.isIE && !this.isNS && !this.isMozilla);

	this.versionMinor = parseFloat(navigator.appVersion); 

	if (this.isNS && this.isGecko)
	{
		this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('/') + 1 ) );
	}

	else if (this.isIE && this.versionMinor >= 4)
	{
		this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
	}

	else if (this.isOpera)
	{
		if (ua.indexOf('opera/') != -1)
		{
			this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera/') + 6 ) );
		}
		else
		{
			this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera ') + 6 ) );
		}
	}
	else if (this.isKonqueror)
	{
		this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
	}

	this.versionMajor = parseInt(this.versionMinor);
	this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );

	this.isWin   = (ua.indexOf('win') != -1);
	this.isWin32 = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1) );
	this.isMac   = (ua.indexOf('mac') != -1);
	this.isUnix  = (ua.indexOf('unix') != -1 || ua.indexOf('linux') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)

	this.isNS4x = (this.isNS && this.versionMajor == 4);
	this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
	this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
	this.isNS4up = (this.isNS && this.versionMinor >= 4);
	this.isNS6x = (this.isNS && this.versionMajor == 6);
	this.isNS6up = (this.isNS && this.versionMajor >= 6);

	this.isIE4x = (this.isIE && this.versionMajor == 4);
	this.isIE4up = (this.isIE && this.versionMajor >= 4);
	this.isIE5x = (this.isIE && this.versionMajor == 5);
	this.isIE55 = (this.isIE && this.versionMinor == 5.5);
	this.isIE5up = (this.isIE && this.versionMajor >= 5);
	this.isIE6x = (this.isIE && this.versionMajor == 6);
	this.isIE6up = (this.isIE && this.versionMajor >= 6);

	this.isIE4xMac = (this.isIE4x && this.isMac);
}

function openPopup(e, url)
{
	var popupWindow;

	popupWindow = window.open(url, null, "width=700, height=500, toolbar=no, location=no, menubar=no, scrollbars=yes, resizable=no, statusbar=no");
	popupWindow.focus();

	if (e != null)
	{
		var e = (e) ? e : window.event;

		e.cancelBubble = true;

		if (e.stopPropagation) e.stopPropagation();
	}

	return false
}
