/**
* Market Sessions Javascript Engine
* Copyright (c) Forex Factory
* All rights reserved
*/

document.write('<div id="sessdebug"></div>');

Market_Sessions_Engine = function()
{
	this.debugdata = function(data)
	{//return false;
		if (arguments[1])
		{
			data = '<b><font color="' + arguments[1] + '">' + data + '</font></b>';
		}

		document.getElementById('sessdebug').innerHTML = document.getElementById('sessdebug').innerHTML + '<br />' + data;
	}

	this.clear_debug = function()
	{//return false;
		document.getElementById('sessdebug').innerHTML = '';
	}

	this.data = new Object;
	this.sessions = new Array();
	this.holidays = new Array();
	this.activeholidays = new Array();
	this.otmdays = new Array();
	this.marketsopen = new Array();
	this.usertzoffset = 0;
	this.hour_offset = 0;
	this.weekdays = new Array('Sun.', 'Mon.', 'Tue.', 'Wed.', 'Thu.', 'Fri.', 'Sat.');
	this.monthnames = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	this.dateobj = false;
	this.est_time = '';
	this.stop = false;
	this.debug_time = 0;
	this.marketzones = {
		'sydney'	: -15,
		'tokyo'		: -14,
		'london'	: -5,
		'newyork'	: 0
	};

	this.init = function()
	{return false;
		setTimeout("MSE.doinit()", 1000); // change back to 10000
	}

	this.doinit = function()
	{
		if (!fetch_object('london_status'))
		{
			return false;
		}

		this.debugdata('Initialising...');

		// Collect all markets and data linked to them
		this.data.ajax = new vB_AJAX_Handler(true);
		this.data.ajax.onreadystatechange(MSE.init_complete);
		this.data.ajax.send('marketsession.php', 'do=fetchjsdata');

		this.est_time = this.make_est_time();
		this.usertzoffset = vBulletin_Clock.offset;
	}

	this.make_output_24hour = function(time)
	{
		if (time.match(/^(.+[^0-9])(\d+):(\d+)$/i))
		{
			time = new Array();
			time[0] = RegExp.$1;
			time[1] = RegExp.$2;
			time[2] = RegExp.$3;

			if (parseInt(time[2]) < 10)
			{
				time[2] = '0' + time[2].toString();
			}

			return time[0] + time[1] + ':' + time[2];
		}

		return time;
	}

	this.make_output_12hour = function(time)
	{
		if (time.match(/^(.+[^0-9])(\d+):(\d+)$/i))
		{
			time = new Array();
			time[0] = RegExp.$1;
			time[1] = RegExp.$2;
			time[2] = RegExp.$3;

			if (parseInt(time[1]) > 12)
			{
				time[1] = parseInt(time[1]) - 12;
			}

			if (parseInt(time[2]) < 10)
			{
				time[2] = '0' + time[2].toString();
			}

			if (parseInt(time[1]) == 0)
			{
				time[1] = 12;
			}

			return time[0] + time[1] + ':' + time[2] + ((parseInt(RegExp.$2) > 11) ? 'pm' : 'am');
		}

		return time;
	}

	this.init_complete = function()
	{
		ajax = MSE.data.ajax;

		if (ajax.handler.readyState == 4 && ajax.handler.status == 200)
		{
			data = PHP_Unserialize(ajax.handler.responseText);
			MSE.sessions = PHP_Unserialize(data['sessions']);
			MSE.holidays = PHP_Unserialize(data['holidays']);
			data = '';

			MSE.update_markets();
		}
	}

	this.update_markets = function()
	{
		if (this.stop)
		{
			return false;
		}

		this.clear_debug();

		this.dateobj = false;
		this.est_time = this.make_est_time();

		for (m in this.sessions)
		{
			this.fetch_market_status(m);
		}

		this.calculate_liquidity();

		for (m in this.sessions)
		{
			data = this.exec_liquidity(m);

			if (fetch_object(m + '_status_image'))
			{
				fetch_object(m + '_status_image').src = data[0];
				fetch_object(m + '_status_image').alt = data[1];
			}
		}

		setTimeout("MSE.update_markets()", 10000);
	}

	this.make_est_time = function()
	{
		return (new Date().getUTCHours() - 5) + ':' + this.strpadleft(new Date().getUTCMinutes(), 2, 0);

		dateobj = this.fetch_date_object();
		hours = this.correct_est_hours(dateobj.getUTCHours());

		return hours + ':' + this.strpadleft(dateobj.getUTCMinutes(), 2, 0);
	}

	this.correct_est_hours = function(esthours)
	{
		if (esthours < 0)
		{
			esthours += 24;	
		}

		return esthours;
	}

	this.get_est_offset = function()
	{
		// Calculate hours off from EST users computer is
		gmt_hours = new Date().getUTCHours();
		est_hours = gmt_hours - 5;

		if (est_hours < 0)
		{
			est_hours += 24;
		}

		local_hours = new Date().getHours();
		offset_hours = est_hours - local_hours;

		this.debugdata('Offset Hours To EST: ' + offset_hours, 'blue');

		return offset_hours;
	}

	this.is_gmt_today_local_today = function()
	{
		// Checks "GMT" day is same as "Local" day
		gmt_day = new Date().getUTCDay();
		local_day = new Date().getUTCDay();

		return gmt_day == local_day;
	}

	this.get_esttimestamp = function()
	{
		// Rather than a timestamp, we adjust the clock to EST based on the users local clock
		gmt_current_hour = new Date().getUTCHours();
		local_current_hour = new Date().getHours();
		local_gmt_offset = local_current_hour - gmt_current_hour;
		local_est_offset = local_gmt_offset - 5;

		this.debugdata('Local Hours Offset From GMT: ' + local_gmt_offset + ', EST Offset is then: ' + local_est_offset);

		est_timestamp = this.fetch_timestamp() + (local_est_offset * (60 * 60) * 1000);
		est_do = new Date();
		est_do.setTime(est_timestamp);

		return this.fetch_timestamp();

		return this.fetch_timestamp() + (local_est_offset * (60 * 60) * 1000);
	}

	this.make_24_to_12 = function(time)
	{
		time = time.split(':');

		if (time[0] > 12)
		{
			time[0] -= 12;
		}

		return time.join(':');
	}

	this.strpadleft = function(str, tolength, pad)
	{
		while (str.toString().length < tolength)
		{
			str = pad.toString() + str;
		}

		return str;
	}

	this.fetch_date_object = function()
	{
		if (!this.dateobj)
		{
			this.dateobj = new Date();
			this.dateobj.setTime(this.get_esttimestamp());

			if (this.debug_time > 0)
			{
				this.dateobj.setTime(Date.parse(new Date().toString()) + (this.debug_time * (60 * 60) * 1000))
				this.debugdata('The time is now: - ' + this.dateobj.getHours() + ':' + this.dateobj.getMinutes(), 'orange');
			}
		}

		return this.dateobj;
	}

	this.fetch_today_index = function()
	{
		indextoday = new Date().getUTCDay();

		// Let's correct to EST Today
		gmt_hours = new Date().getUTCHours();

		if (gmt_hours < 5)
		{
			indextoday--;

			if (indextoday < 0)
			{
				indextoday = 6;
			}
		}

		return indextoday;
	}

	this.fetch_today = function()
	{
		return this.weekdays[this.fetch_today_index()];
	}

	this.fetch_tomorrow = function()
	{
		day = this.fetch_today_index();

		if (!this.weekdays[day + 1])
		{
			day = 0;
		}

		return this.weekdays[day];
	}

	this.fetch_tomorrow_index = function(day)
	{
		for (var d = 0; d < this.weekdays.length; d++)
		{
			if (this.weekdays[d] == day)
			{
				theday = (d + 1);
			}
		}

		if (!this.weekdays[theday])
		{
			theday = 0;
		}

		return this.weekdays[theday];
	}

	this.fetch_yesterday = function()
	{
		day = this.fetch_today_index();
		day = day - 1;

		if (!this.weekdays[day])
		{
			day = this.weekdays.length - 1;
		}

		return this.weekdays[day];
	}

	this.fetch_time = function(times, fetch)
	{
		if (times['alter' + fetch]['verify'])
		{
			return times['alter' + fetch]['time'];
		}

		return times[fetch]['time'];
	}

	this.break_time = function(time)
	{
		try
		{
			time.split(':');
		}
		catch(e)
		{
			this.debugdata(time+ ' could not be split, its an array?', 'red');
			return false;
		}

		split		= time.split(':');
		time		= new Array();
		time['hour']	= parseInt(split[0]);
		time['minute']	= parseInt(split[1]);

		return time;
	}

	this.time_greater = function(check, against)
	{
		check	= this.break_time(check);
		against	= this.break_time(against);

		if (check['hour'] > against['hour'])
		{
			return true;
		}
		else if (check['hour'] == against['hour'] && check['minute'] > against['minute'])
		{
			return true;
		}

		return false;
	}

	this.yesterday_closes_today = function(market, day)
	{
		times = this.sessions[market][day];

		yctopentime = this.fetch_time(times, 'open');
		yctclosetime = this.fetch_time(times, 'close');

		this.debugdata('Checking Yesterday Closes Today ['+market+'] [Yesterday Opened: '+yctopentime+'| Yesterday Closes: '+yctclosetime+' | Day: '+day+']', 'brown');

		if (this.time_greater(yctopentime, yctclosetime) && yctclosetime != '00:00')
		{
			return true;
		}

		return false;
	}

	this.is_closed_all_day = function(opentime_check, closetime_check)
	{
		this.debugdata('Checking closed all day: Open '+opentime_check+' | Close '+ closetime_check+' | Result:: ' + (((opentime_check == closetime_check && opentime_check == '00:00')) ? 'true' : 'false'), 'red');
		return (opentime_check == closetime_check && opentime_check == '00:00');
	}

	this.in_array = function(needle, stack)
	{
		try
		{
			for (s in stack)
			{
				if (stack[s] == needle)
				{
					return true;
				}
			}
		}
		catch(e)
		{}

		return false;
	}

	this.apply_auto_otms = function(today, offset, formarket)
	{
		stamp	= this.get_esttimestamp() + ((offset * (60 * 60)) * 1000);
		day	= this.date('D', stamp);

		try
		{
			for (id in this.holidays)
			{
				markets		= this.holidays[id]['markets'].split(',');
				otmopentime	= this.holidays[id]['time']['open']['time'].split(':');
				otmclosetime	= this.holidays[id]['time']['close']['time'].split(':');

				opens		= this.mktime(
							otmopentime[0],
							otmopentime[1],
							0,
							this.holidays[id]['time']['open']['month'] - 1,
							this.holidays[id]['time']['open']['day'],
							this.fetch_date_object().getUTCFullYear(),
							this.fetch_date_object().getUTCDay()
				);

				closes		= this.mktime(otmclosetime[0], otmclosetime[1], 0, this.holidays[id]['time']['close']['month'] - 1, this.holidays[id]['time']['close']['day'], this.fetch_date_object().getUTCFullYear(), this.fetch_date_object().getUTCDay())// - (5 * (60 * 60) * 1000);

				if (!this.in_array(formarket, markets))
				{
					continue;
				}

				try
				{
					for (mid in markets)
					{
						if (!this.otmdays[markets[mid]])
						{
							this.otmdays[markets[mid]] = new Array();
							this.otmdays[markets[mid]][today] = false;
						}

						this.activeholidays[markets[mid]] = false;

						thisopen	= (opens + (this.marketzones[markets[mid]] * (60 * 60) * 1000));
						thisclose	= (closes + (this.marketzones[markets[mid]] * (60 * 60) * 1000));

						this.debugdata('<b>Checking holiday...</b> ' + this.holidays[id]['name'] + '\n\n [Open: ' +this.date('D h:i', thisopen)+':'+thisopen+']\n\n [Closes: '+this.date('D h:i', thisclose) + ':'+thisclose+']\n\n [Nowtime: ' + this.date('D h:i', stamp) + ':'+stamp+']')

						if (stamp >= thisopen && stamp <= thisclose)
						{this.debugdata('holiday is active! ' + this.holidays[id]['name'] + '\n\n [Open: ' +this.date('D h:i', thisopen)+']\n\n [Closes: '+this.date('D h:i', thisclose) + ']\n\n [Nowtime: ' + this.date('D h:i', stamp) + ']')
							if (!this.otmdays[markets[mid]])
							{
								this.otmdays[markets[mid]] = new Array();
							}

							this.otmdays[markets[mid]][today] = true;

							if (offset == 0)
							{
								this.activeholidays[markets[mid]] = this.holidays[id]['name'];
							}
						}
					}
				}
				catch(e)
				{alert(e.message)}
			}
		}
		catch(e)
		{alert(e.message)}
	}

	this.fetch_market_status = function(market)
	{
		this.hour_offset = 0;
		doroll = false;

		today = this.fetch_today();
		tomorrow = this.fetch_tomorrow();
		yesterday = this.fetch_yesterday();

		this.apply_auto_otms(today, 0, market);

		if (!this.otmdays[market])
		{
			this.otmdays[market] = new Array();
			this.otmdays[market][today] = false;
		}

		market_is_open = false;
		times		= this.sessions[market][today];
		opentime	= this.fetch_time(times, 'open');
		closetime	= this.fetch_time(times, 'close');
		tomorrowtimes	= this.sessions[market][tomorrow];
		tomorrowopen	= this.fetch_time(tomorrowtimes, 'open');
		tomorrowclose	= this.fetch_time(tomorrowtimes, 'close');

		this.debugdata('Market Times : "'+market+'" [Open: '+opentime+'| Close: '+closetime+' | Now: '+this.est_time+' | Day: '+today+' | Yesterday: '+yesterday+']', 'green');

		/**
		 * Discover if we've reached/gone past opening times
		 * * * * * * * * * * * * * * * * *
		 */
		pastopen = this.time_greater(this.est_time, opentime);

		/**
		 * Discover if we've reached/gone past closing times
		 * * * * * * * * * * * * * * * * *
		 */
		pastclose = this.time_greater(this.est_time, closetime);

		/**
		 * Discover if the market close time goes into tomorrow
		 * * * * * * * * * * * * * * * * *
		 */
		closetomorrow = this.time_greater(opentime, closetime);

		/**
		 * Discover if the market close time from yesterday went into today
		 * * * * * * * * * * * * * * * * *
		 */
		yesterdayclosetoday = this.yesterday_closes_today(market, yesterday);
		yesterdayclose = this.fetch_time(this.sessions[market][yesterday], 'close');
		pastyesterdayclose = this.time_greater(this.est_time, yesterdayclose);

		/**
		 * Discover if the market is open
		 * Condition 1: Past open times
		 * Condition 2: Not past closed times OR past closed times and closes tomorrow
		 * Condition 3: Market is not closed all day
		 * Condition 4: Yesterdays close time has past (assuming yesterdays went into today)
		 * * * * * * * * * * * * * * * * *
		 */
		if (pastopen && (!pastclose || (pastclose && closetomorrow)) && (!this.is_closed_all_day(opentime, closetime) && ((!yesterdayclosetoday) || (yesterdayclosetoday && pastyesterdayclose))))
		{
			market_is_open = true;
		}

		/**
		 * Discover if the market is open from yesterday
		 * Condition 1: Market is considered closed
		 * Condition 2: Yesterday closed today
		 * Condition 3: We have NOT passed yesterdays closed tme
		 * * * * * * * * * * * * * * * * *
		 */
		if (!market_is_open && yesterdayclosetoday && !pastyesterdayclose)
		{
			market_is_open = true;

			closetime = yesterdayclose;
			closetomorrow = false;
		}

		this.debug_check('market_is_open');
		this.debug_check('pastopen');
		this.debug_check('pastclose');
		this.debug_check('closetomorrow');
		this.debug_check('yesterdayclosetoday');

		/**
		 * Discover when the market will next change status (open/close)
		 * * * * * * * * * * * * * * * * *
		 */
		if (market_is_open && !closetomorrow)
		{
			status_time = new Array();
			status_time['days'] = 0;
			status_time['time'] = closetime;
		}
		else if (market_is_open)
		{
			doroll = true;
			this.debugdata(market + ' is open but closes tomorrow? close time registers: ' + closetime, 'green');
		}
		else if (((pastyesterdayclose && yesterdayclosetoday) || (!yesterdayclosetoday && !pastopen)) && (!this.is_closed_all_day(opentime, closetime) || (this.is_closed_all_day(opentime, closetime) && !this.is_closed_all_day(tomorrowopen, tomorrowclose) && this.otmdays[market][today])))
		{
			/**
			 * Current logic:
			 * past yesterdays close time and yesterday did close today OR yesterday did not close today and we have not past todays open time
			 * AND
			 * market is not closed all day, or is but is not tomorrow and is closed all today due to a holiday
			 * * * * * * * * * * * * * * * * *
			 */
			status_time = new Array();
			status_time['days'] = 0;
			status_time['time'] = opentime;

			// If indeed the market was closed today for an holiday, but isn't closed tomorrow, and tomorrows open time is actually starting today, we'll need to use that time
			if ((this.is_closed_all_day(opentime, closetime) && !this.is_closed_all_day(tomorrowopen, tomorrowclose) && this.otmdays[market][today]))
			{
				status_time['time'] = tomorrow_open;
			}
		}
		else
		{
			// The market is closed all today, or it is closed and opens tomorrow, either way we need to roll
			doroll = true;
		}

		if (doroll)
		{
			rolling = true;

			this.debugdata(market + ' is rolling....');

			//this.hour_offset = -24;

			while (rolling)
			{
				if (this.hour_offset == 0)
				{
					//this.hour_offset = -24;
				}

				this.hour_offset += 24;
				today = this.fetch_tomorrow_index(today);

				this.apply_auto_otms(today, this.hour_offset, market);

				times		= this.sessions[market][today];
				checkopentime	= this.fetch_time(times, 'open');
				checkclosetime	= this.fetch_time(times, 'close');

				if (!this.is_closed_all_day(checkopentime, checkclosetime))
				{
					status_time = new Array();
					status_time['days'] = this.hour_offset / 24;
					status_time['time'] = market_is_open ? checkclosetime : checkopentime;

					this.debugdata(market + ' finished rolling... close time registers: ' + closetime + ' | ' + (this.hour_offset / 24), 'green');

					rolling = false;
				}
				else if (market_is_open)
				{
					this.debugdata(market + ' finished rolling... close time registers: ' + closetime + ' | ' + (this.hour_offset / 24), 'green');

					status_time = new Array();
					status_time['days'] = this.hour_offset / 24;
					status_time['time'] = closetime;

					rolling = false;
				}
			}

			this.debugdata(market + ' rolled and day was: ' + status_time['days'] + ',' + status_time['time']);
		}

		stamp = new Array();
		stamp['actualtime'] = '';
		stamp['countdown'] = '';
		stamp['holiday'] = '';
		stamp['clock'] = '';

		real = status_time['time'];
		status_time['time'] = this.break_time(status_time['time']);

		milisecs = this.mktime(
			parseInt(status_time['time']['hour']),
			parseInt(status_time['time']['minute']),
			0,
			new Date().getUTCMonth(),
			new Date().getUTCDate(),
			new Date().getUTCFullYear(),
			new Date().getUTCDay() //this.fetch_date_object().getUTCDay()
		);

		if (!status_time['days'])
		{
			status_time['days'] = 0;
		}

		milisecs += (((status_time['days'] * 24) * (60 * 60)) * (1000));

		// Adjust time for GMT
		milisecs += ((5 * (60 * 60)) * 1000);

		countdown = this.get_countdown(milisecs);

		// Adjust for user offset
		milisecs += ((this.usertzoffset * (60 * 60)) * 1000);

		stamp['actualtime'] = this.make_output_24hour(this.date('D h:i', milisecs));

		nowest = Date.parse('Wed Jun 13 14:16:43 UTC-0500 2007');
		this.debugdata(market + ' is calculating time... ' + this.date('D h:i', milisecs) + ', ' + milisecs + ', ' + this.fetch_date_object().toString() + ', ' + nowest);

		if (vBulletin_Clock.format != 'G:i')
		{
			stamp['actualtime'] = this.make_output_12hour(this.date('D h:i', milisecs));
		}
		else
		{
			stamp['actualtime'] = this.make_output_24hour(this.date('D h:i', milisecs));
		}

		this.marketsopen[market] = market_is_open;

		if (!fetch_object(market + '_status_time'))
		{
			this.stop = true;
			return false;
		}

		fetch_object(market + '_status_time').innerHTML = (market_is_open ? 'Ends' : 'Begins') + ' ' + stamp['actualtime'];
		fetch_object(market + '_status').innerHTML = (market_is_open ? 'Session Active' : 'Session Inactive');
		fetch_object(market + '_status').style.color = (market_is_open ? '#24A421' : '#b2b2b2');
		fetch_object(market + '_countdown').innerHTML = countdown[0];

		if (PHP.trim(countdown[1]) != '')
		{
			fetch_object(market + '_clock').innerHTML = ' <img src="../../../../James16_Archive/James16 Trading Education/Confluence Education and Examples/Confluence and Examples_files/images/misc/session_notice.gif" alt="' + ((market_is_open ? 'Ends' : 'Begins')) + ' ' + countdown[1] + '" style="position:relative; vertical-align: bottom;" />';
		}
		else
		{
			fetch_object(market + '_clock').innerHTML = '';
		}

		if (market_is_open && this.activeholidays[market])
		{
			fetch_object(market + '_holiday').innerHTML = '<div style="color: #666686; padding-top: 7px;">Low liquidity expected due to holiday:<br /><strong>' + this.activeholidays[market] + '</strong></div>';
		}
		else
		{
			fetch_object(market + '_holiday').innerHTML = '';
		}

		return stamp;
	}

	this.mktime = function(hour, minute, second, month, date, year, day)
	{
		timestring = this.weekdays[day].replace('.', '') + ' ' + this.monthnames[month] + ' ' + date + ' ' + hour + ':' + minute + ':' + second + ' UTC+0000 ' + year;

		return Date.parse(timestring);
	}

	this.calculate_liquidity = function()
	{
		sydney		= this.marketsopen['sydney'];
		tokyo		= this.marketsopen['tokyo'];
		london		= this.marketsopen['london'];
		newyork		= this.marketsopen['newyork'];

		sydney_h	= this.activeholidays['sydney'];
		tokyo_h		= this.activeholidays['tokyo'];
		london_h	= this.activeholidays['london'];
		newyork_h	= this.activeholidays['newyork'];

		this.liquidity = {
			'sydney'	: 'yellow',
			'tokyo'		: 'yellow',
			'london'	: 'orange',
			'newyork'	: 'orange'
		};

		if (london_h && london)
		{
			this.liquidity['newyork'] = 'orange';
		}
		else if (newyork && newyork_h)
		{
			this.liquidity['london'] = 'orange';
		}
		else if (london && tokyo)
		{
			this.liquidity['tokyo'] = 'orange';
		}
		else if (london && newyork)
		{
			this.liquidity['london'] = this.liquidity['newyork'] = 'red';
		}

	}

	this.exec_liquidity = function(id)
	{
		if (this.liquidity[id] == 'yellow')
		{
			lsrc = 'low';
			lalt = 'Forex Market Liquidity: Low';
		}
		else if (this.liquidity[id] == 'orange')
		{
			lsrc = 'med';
			lalt = 'Forex Market Liquidity: Medium';
		}
		else
		{
			lsrc = 'high';
			lalt = 'Forex Market Liquidity: High';
		}

		if (!this.marketsopen[id])
		{
			lsrc = 'closed';
			lalt = 'Session Inactive';
		}
		else if (this.activeholidays[id])
		{
			lsrc = 'holiday';
			lalt = 'Holiday Liquidity';
		}

		return new Array('images/misc/sessions_' + lsrc + '.gif', lalt);
	}

	this.get_countdown = function(thetime)
	{
		if (this.get_esttimestamp() > thetime)
		{
			diff = this.get_esttimestamp() - thetime;
		}
		else
		{
			diff = thetime - this.get_esttimestamp();
		}

		diff = diff / 1000;

		mins = Math.floor(diff / 60);
		hours = 0;

		while (mins > 59)
		{
			hours++;
			mins -= 60;
		}

		clock = '';

		if (hours == 0 && mins >= 0)
		{
			clock = 'in ' + mins + ' mins.';
		}

		countdown = 'in ' + ((hours > 0) ? hours + ' hrs. ' : '') + mins + ' mins.';

		return [countdown, clock];
	}

	this.date = function(string, timestamp)
	{
		thedate = new Date();
		thedate.setTime(timestamp);

		hour = thedate.getUTCHours();
		minute = thedate.getUTCMinutes();
		day = real = this.weekdays[thedate.getUTCDay()];

		if (day == this.weekdays[this.fetch_date_object().getUTCDay()])
		{
			//day = 'Today';
		}

		// Is that really today?
		if (day != 'Today')
		{
			tmpdate = new Date();
			tmpdate.setTime(tmpdate.getTime() + ((this.usertzoffset * (60 * 60)) * 1000));

			if (day == this.weekdays[tmpdate.getUTCDay()])
			{
				day = 'Today';
			}
		}

		string = string.replace(/h/g, hour);
		string = string.replace(/i/g, minute);
		string = string.replace(/D/g, day);

		return string;
	}

	this.fetch_unix_timestamp = function()
	{
		return parseInt(this.fetch_timestamp().substring(0, 10));
	}

	this.fetch_timestamp = function()
	{
		return parseInt(new Date().getTime().toString());
	}

	this.debug_check = function(thevar)
	{
		eval("MSE.debugdata(thevar.toString() + ' is ' + ("+thevar+" ? 'true' : 'false'))");
	}

	this.market_status = function(mid, value)
	{
		this.market_status_log[mid] = value;
	}

	this.toggle_market = function(mid)
	{
		if (fetch_object('marketsession_' + mid).style.display == '')
		{
			fetch_object('marketsession_' + mid).style.display = 'none';
			//this.market_status(mid, false);
		}
		else
		{
			fetch_object('marketsession_' + mid).style.display = '';
			//this.market_status(mid, true);
		}
	}
}

MSE = new Market_Sessions_Engine;
MSE.init();