var daysInMonth = new Array();
daysInMonth[1] = 31;
daysInMonth[2] = 28;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

var leapYears = new Array();
leapYears['1996'] = 1;
leapYears['2000'] = 1;
leapYears['2004'] = 1;
leapYears['2008'] = 1;
leapYears['2012'] = 1;
leapYears['2016'] = 1;
leapYears['2020'] = 1;
leapYears['2024'] = 1;
leapYears['2028'] = 1;
leapYears['2032'] = 1;
leapYears['2036'] = 1;
leapYears['2040'] = 1;
leapYears['2044'] = 1;
leapYears['2048'] = 1;
leapYears['2052'] = 1;
leapYears['2056'] = 1;
leapYears['2060'] = 1;
leapYears['2064'] = 1;
leapYears['2068'] = 1;
leapYears['2072'] = 1;
leapYears['2076'] = 1;
leapYears['2080'] = 1;
leapYears['2084'] = 1;
leapYears['2088'] = 1;
leapYears['2092'] = 1;
leapYears['2096'] = 1;

function MakeDaysDropdown( form_name, dayfield, monthfield, yearfield )
{
	var daysInThisMonth = daysInMonth[document.forms[form_name].elements[monthfield].value];
	var daysDropdown = document.forms[form_name].elements[dayfield];

	var currentDay = daysDropdown.selectedIndex;
	
	if( document.forms[form_name].elements[monthfield].value == '2'
	&& leapYears[document.forms[form_name].elements[yearfield].value] )
		daysInThisMonth++;

	while( daysDropdown.options.length > 28 )
		daysDropdown.removeChild( daysDropdown.options[daysDropdown.options.length - 1] );
	
	while( daysDropdown.options.length < daysInThisMonth )
	{
		var newDay = document.createElement( 'OPTION' );
		var cDayNumber = daysDropdown.options.length + 1;

		newDay.appendChild( document.createTextNode( cDayNumber ) );
		newDay.value = cDayNumber;
		daysDropdown.appendChild( newDay );
	}

	if( currentDay >= daysDropdown.options.length )
		currentDay = daysDropdown.options.length - 1;
	daysDropdown.selectedIndex = currentDay;
}

function addEventSimple( obj, evt, fn )
{
	if( obj.addEventListener )
		obj.addEventListener( evt, fn, false );
	else if( obj.attachEvent )
		obj.attachEvent( 'on'+evt, fn );
}

// Auto-color rows in all tables using cname CSS class,
// using provided even and odd CSS style names

function getElementsByClassName( strClass, strTag, objContElm )
{
	strTag = strTag || "*";
	objContElm = objContElm || document;
	var objColl = objContElm.getElementsByTagName(strTag);
	if( !objColl.length && strTag == "*" &&  objContElm.all )
		objColl = objContElm.all;
	
	var arr = new Array();
	var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
	var arrClass = strClass.split(delim);
	for( var i = 0, j = objColl.length; i < j; i++ )
	{
		var arrObjClass = objColl[i].className.split(' ');
		if( delim == ' ' && arrClass.length > arrObjClass.length ) continue;
		var c = 0;
		comparisonLoop:
		for( var k = 0, l = arrObjClass.length; k < l; k++ )
		{
      	for( var m = 0, n = arrClass.length; m < n; m++ )
      	{
				if( arrClass[m] == arrObjClass[k] ) c++;
				if(( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length))
				{
					arr.push(objColl[i]);
					break comparisonLoop;
				}
			}
		}
	}
	return arr;
}

// To cover IE 5.0's lack of the push method
Array.prototype.push = function( fValue )
{
	this[this.length] = fValue;
}

function colorRowsIn( cname, even, odd )
{
	if( document.getElementsByTagName )
	{
		var tables = document.getElementsByTagName( 'table' );

		for( var t = 0; t < tables.length; t++ )
		{
			if( tables[t].className == cname )
			{
				var rows = tables[t].getElementsByTagName( 'tr' );
				var commonParent = rows[0].parentNode;
				var isOddCounter = 1;
				
				for( var i = 0; i < rows.length; i++ )
				{
					if( rows[i].parentNode == commonParent )
					{
						if( isOddCounter == 1 )
						{
							rows[i].className = odd;
						}
						else
						{
							rows[i].className = even;
						}
						isOddCounter *= -1;
					}
				}
			}
		}
	}
}

function getStyle( el, styleProp )
{
	var y = undefined;

	if( el.currentStyle )
	{
		y = el.currentStyle[styleProp];
	}
	else if( window.getComputedStyle )
	{
		y = document.defaultView.getComputedStyle( el,null ).getPropertyValue( styleProp );
	}
	
	return y;
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie( name, value, expires, path, domain, secure )
{
	document.cookie = name + "=" + escape( value ) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie( name )
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf( "; " + prefix );
	if( begin == -1 )
	{
		begin = dc.indexOf( prefix );
		if( begin != 0 ) return null;
	}
	else
	{
		begin += 2;
	}
	var end = document.cookie.indexOf( ";", begin );
	if( end == -1 )
	{
		end = dc.length;
	}
	return unescape( dc.substring( begin + prefix.length, end ) );
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie( name, path, domain )
{
	if( getCookie( name ) )
	{
		document.cookie = name + "=" + 
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function showHelp( topic )
{
	if( eval( topic ) )
	{
		alert( eval( topic ) );
	}

	return false;
}

function activateHelpTokens()
{
	var hTokens = new Array();
	hTokens = getElementsByClassName( 'hlpToken', 'SPAN' );
	for( var i = 0; i < hTokens.length; i++ )
	{
		hTokens[i].style.display = 'inline';
	}
}

function addHighlightToInputs()
{
	var inpElems = getElementsByClassName( 'txt', 'INPUT' );

	for( var i = 0; i < inpElems.length; i++ )
	{
		addEventSimple( inpElems[i], 'focus', function( e ) {
			if( !e )
				var e = window.event;
			var target = e.target ? e.target : e.srcElement;
			if( typeof(target) != 'undefined' && target.className == 'txt' )
				target.className = 'hl_input';
		});
		addEventSimple( inpElems[i], 'blur', function( e ) {
			if( !e )
				var e = window.event;
			var target = e.target ? e.target : e.srcElement;
			if( typeof(target) != 'undefined' && target.className == 'hl_input' )
				target.className = 'txt';
		});
	}
}

function firstFocus()
{
	if( document.getElementById('firstfocus') && document.getElementById('firstfocus').focus )
	{
		document.getElementById('firstfocus').focus();
	}
}

// Executed onLoad of most pages, unless redefined explicitly in the template
function doPreprocess()
{
	try
	{ 
		colorRowsIn( 'd_table_colored', 'even', 'odd' );
		activateHelpTokens();
		addHighlightToInputs();
		firstFocus();
	}
	catch( err )
	{
		return true;
	};
	
	return true;
}

// get XMLHTTPRequest object, cross-browser
function get_http()
{
	var xhr = false;
	if( window.XMLHttpRequest )
	{
		xhr = new XMLHttpRequest();
	}
	else if( window.ActiveXObject )
	{
		try
		{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				xhr = false;
			}
		}
	}
	
	return xhr;

}

var account_time_utc = 0;

function accountTime(utc)
{
	if(account_time_utc == 0)
		account_time_utc = utc;

	d = new Date();
	d.setTime(account_time_utc);

	gid('account_time').innerHTML = d.getUTCHours() + ":" + (d.getUTCMinutes() < 10 ? '0' : '') + d.getUTCMinutes() + ":" + (d.getUTCSeconds() < 10 ? '0' : '') + d.getUTCSeconds();

	account_time_utc += 1000; // 1 second

	setTimeout("accountTime()", 1000);
}

function gid( identificator )
{
	return document.getElementById( identificator );
}

function encodeXML( x )
{
	x = x.replace( /\&/gm, '&amp;' );
	x = x.replace( /\</gm, '&lt;' );
	x = x.replace( /\>/gm, '&gt;' );
	return x;
}