function sendMsg()
{
	var pars = Form.serialize('chatForm');
	$('chatSend').disabled=true;
	var myAjax = new Ajax.Request (
	'index.php?view=chat',
	{
		methode: 'post',
		parameters: pars,
		onComplete: function (resp) { if (resp.responseText=="OK") { $('chatMsg').value=''; loadChat(c); $('chatSend').disabled=false; } else { alert(resp.responseText); }; }
	});		
}
function loadChat(channel)
{

	$('chatSend').disabled=true;
	if (channel == "-1")
	{
		
		$('chatlog').innerHTML="Pick a channel";
		c = channel;
		if (timerID!=-1)
			clearTimeout(timerID)
		timerID = -1;
		return;
	} 
	var d = 0;
	
	
	if (c==channel)
	{
		d = lastMsg;
		
	} else {
		c = channel;
		d=0;
		$('chatlog').innerHTML="";
	}
	
	var pars = "msgGroup="+channel+"&getMsgs="+d;
	x=5;
	var myAjax = new Ajax.Request (
	'index.php?view=chat',
	{
		methode: 'post',
		parameters: pars,
		onComplete: readChat
		
	});
		
}
						

var c = -1;
var lastMsg = "";
function readChat(reply)
{
	var msgs = reply.responseXML.getElementsByTagName("message");
	var chatlog = $('chatlog');
	
	for (i=0;i<msgs.length;i++)
	{
		
		var tmp = msgs[i].childNodes.length;	

			var from = msgs[i].getElementsByTagName("from")[0].firstChild.nodeValue;
			var content = msgs[i].getElementsByTagName("content")[0].firstChild.nodeValue;
			var priv = msgs[i].getElementsByTagName("priv")[0].firstChild.nodeValue;
			if (priv=="y")
			{
				from = '+'+from;
			} 
			var regel = document.createElement("div");
			regel.innerHTML = from+': '+content;
			$('chatlog').appendChild(regel);
			lastMsg = msgs[i].getElementsByTagName("when")[0].firstChild.nodeValue;
	}
	$('chatlog').scrollTop = $('chatlog').scrollHeight;

	$('chatSend').disabled=false;
	x=5;
		if (timerID==-1)
			updateChat();	
	
}

var x = -1;
var y = 1
var timerID = -1;
function updateChat(){
  x = x-y
  timerID = setTimeout("updateChat()", 1000)
  if(x==0){
      	loadChat(c);
       	x=5;
	}
}


function showShort ()
{
	$('#historyTable').hide ();
	$('#normalTable').show ();
	$('#_showShort').css ('text-decoration', 'underline');
	$('#_showLong').css ('text-decoration', 'none');
}
function showLong ()
{
	$('#_showShort').css ('text-decoration', 'none');
	$('#_showLong').css ('text-decoration', 'underline');
	
	$('#historyTable').show ();
	$('#normalTable').hide ();
}
var tabsOldImage	= new Array ();


function doTab (tab)
{
	var name	= '#tab_' + tab;
	tabsOldImage[tab]	= $(name).attr ('src');


	var newImage	= tabsOldImage[tab].replace ('active', 'over');
	var newImage	= tabsOldImage[tab].replace ('base', 'over');
	$(name).attr ('src', newImage);
}

function unDoTab (tab)
{
	var name	= '#tab_' + tab;
	$(name).attr ('src', tabsOldImage[tab]);
}

function instrumentsByGroup (group)
{
	$('#instrumentGraph').remove ();

	if (group == 2)
	{
		$('.dataTable tr[group=1]').hide ();
		$('.dataTable tr[group=2]').show ();
	}
	else if (group == 3)
	{
	}
	else
	{
		$('.dataTable tr[group=1]').show ();
		$('.dataTable tr[group=2]').show ();
	}
}

var	currGraph	= 0;

function appendGraph (element, id, uid)
{
	if (currGraph == id)
	{
		$('#instrumentGraph').remove ();
	}
	else
	{
		$('#instrumentGraph').remove ();
		currGraph	= id;

		$('<tr id="instrumentGraph"><td colspan="12"><img src="" id="instrumentGraphImage1" /><br /><br /><img src="" id="instrumentGraphImage2" /></td> </tr>').insertAfter (element);

		var src1	= 'stockGraph1.php?id=' + id + '&uid=' + uid;
		var src2	= 'stockGraph2.php?id=' + id + '&uid=' + uid;

		$('#instrumentGraphImage1').attr ('src', src1);
		$('#instrumentGraphImage2').attr ('src', src2);
	}
}

function removeOrder (oid)
{
	if (window.confirm ('Are you sure you wish to delete this order'))
	{
		var response	= $.ajax ({
								  url: 'index.php?view=ajax&trigger=deleteOrder&order_id=' + oid,
								  async: true,
								  dataType: 'html',
								  success: function (response) { __removeOrder (response, oid); }
								  });
	}
}

function __removeOrder (response, oid)
{
	if (response == 'TRUE')
	{
		$('#orderData_' + oid).fadeOut ();
	}
}


function updateMarket ()
{
	if ($('.ajaxLoaderImage').slice (0,1).attr ('src') != undefined)
	{
		$('.ajaxLoaderImage').show (); //each (function () { $(this).toggle (); } );
		setTimeout ('__updateMarket ()', 500);
	}
}

var instrumentPrices	= new Array ();

function __color (id, color)
{
	$('#bid_' + id).css ('background', color);
	$('#offer_' + id).css ('background', color);
	$('#bid2_' + id).css ('background', color);
	$('#offer2_' + id).css ('background', color);
	setTimeout ('__noColor (' + id + ')', 3000);
}

function __noColor (id)
{
	$('#bid_' + id).css ('background', '#EFF0F4');
	$('#offer_' + id).css ('background', '#EFF0F4');
	$('#bid2_' + id).css ('background', '#EFF0F4');
	$('#offer2_' + id).css ('background', '#EFF0F4');
}

function __updateMarket ()
{
	var response		= $.ajax ({
								  	url: 'xmlOutput.php?instruments=' + myInstruments,
									async: false,
									dataType: 'xml'
								  }).responseXML;



	var instruments		= xml2js ({
							  xml: response,
							  tags: ['id', 'tpl', 'rpl', 'costs', 'interest', 'urp', 'xpos', 'value', 'rval', 'bid', 'offer', 'avg']
							  });

	var total			= instruments.id.length;
	var i;
	var id;

	for (i = 0; i < total; i++)
	{
		id	= instruments.id[i];
		if (instrumentPrices[id] == undefined)
		{
			instrumentPrices[id]	= instruments.value[i];
		}

		if (instruments.value[i] > instrumentPrices[id])
		{
			__color (id, '#00CC00');
		}
		else if (instruments.value[i] < instrumentPrices[id])
		{
			__color (id, '#FF3333');
		}
		else
		{
		}




		//$('#var_' + id).text (instruments.[i]);
		$('#tpl_' + id).text (instruments.tpl[i]);
		$('#int_' + id).text (instruments.interest[i]);
		$('#upl_' + id).text (instruments.urp[i]);
		//$('#value_' + id).text (instruments.val[i]);
		//$('#rpl_' + id).text (instruments.rval[i]);
		$('#bid_' + id).text (instruments.bid[i]);
		$('#offer_' + id).text (instruments.offer[i]);
		//$('#var_' + id).text (instruments.[i]);
		$('#tpl2_' + id).text (instruments.tpl[i]);
		$('#int2_' + id).text (instruments.interest[i]);
		$('#upl2_' + id).text (instruments.urp[i]);
		$('#position_' + id).text (instruments.xpos[i]);
		$('#position2_' + id).text (instruments.xpos[i]);
		$('#avg_' + id).text (instruments.avg[i]);
		$('#avg2_' + id).text (instruments.avg[i]);
		$('#int_' + id).text (instruments.interest[i]);
		$('#int2_' + id).text (instruments.interest[i]);
		$('#ulp_' + id).text (instruments.urp[i]);
		$('#ulp2_' + id).text (instruments.urp[i]);
		$('#rpl_' + id).text (instruments.rpl[i]);
		$('#rpl2_' + id).text (instruments.rpl[i]);
		$('#costs_' + id).text (instruments.costs[i]);
		$('#costs2_' + id).text (instruments.costs[i]);
		//$('#value_' + id).text (instruments.val[i]);
		//$('#rpl_' + id).text (instruments.rval[i]);
		$('#bid2_' + id).text (instruments.bid[i]);
		$('#offer2_' + id).text (instruments.offer[i]);
	}




	var totals			= xml2js ({
							  xml: response,
							  tags: ['tupl', 'trpl', 'tint', 'tcost', 'ttpl', 'tvar', 'tvarl', 'tbank', 'tpos', 'ttrades']
							  });

	$('#tupl').text (totals.tupl[0]);
	$('#trpl').text (totals.trpl[0]);
	$('#tint').text (totals.tint[0]);
	$('#tcosts').text (totals.tcost[0]);
	$('#ttpl').text (totals.ttpl[0]);
	$('#tvar').text (totals.tvar[0]);
	$('#tvarl').text (totals.tvarl[0]);
	$('#tbank').text (totals.tbank[0]);
	$('#tpos').text (totals.tpos[0]);
	$('#ttrades').text (totals.ttrades[0]);

	

	setTimeout ('updateMarket ();', 10000);
	$('.ajaxLoaderImage').hide ();
	
}

function xml2js (data)
{
	
	var	elements	= function () {};
	var total		= data.tags.length;
	var tag2;
	var ttotal;
	var i;
	var ii;

	for (i = 0; i < total; i++)
	{
		eval ('elements.' + data.tags[i] + '	= new Array ();');
		tags	= data.xml.getElementsByTagName ('' + data.tags[i] + '');
		ttotal	= tags.length;

		for (ii = 0; ii < ttotal; ii++)
		{
			eval ('elements.' + data.tags[i] + '[' + ii + ']	= tags[' + ii + '].firstChild.nodeValue;');	
		}
	}

	return elements;
}

function showBox (type)
{
	if (type == 1) // trade
	{
		$('#tradeLink').css ('font-weight', 'bold');
		$('#orderLink').css ('font-weight', 'normal');

		$('#tradeBox').show ();
		$('#orderBox').hide ();

	}
	else
	{
		$('#tradeLink').css ('font-weight', 'normal');
		$('#orderLink').css ('font-weight', 'bold');

		$('#tradeBox').hide ();
		$('#orderBox').show ();

	}
}

function placeTrade (id)
{
	if ($('#trade_volume').val () == '' || $('#trade_volume').val () == '0')
	{
		alert ('Please fill in a volume higher than 0.');
		$('#tvolume').css ('color', 'red');
		return false;
	}
	else
	{
		$('#tvolume').css ('color', 'black');
	}
	if ($('#trade_action1').attr ('checked') != true && $('#trade_action2').attr ('checked') != true)
	{
		alert ('Please select an action, buy or sell.');
		$('#taction').css ('color', 'red');
		return false;
	}
	else
	{
		$('#taction').css ('color', 'black');
	}
	if ($('#trade_note').val () == '')
	{
		alert ('Please specify a note/reason for your trade.');
		$('#tnote').css ('color', 'red');
		return false;
	}
	else
	{
		$('#tnote').css ('color', 'black');
	}

	
	var trade;
	var price;
	if ($('#trade_action1').attr ('checked') == true)
	{
		price	= $('#aprice').val ();
		trade	= 'buy';
	}
	else
	{
		price	= $('#bprice').val ();
		trade	= 'sell';
	}


	var postData	= 'trade=' + trade + '&amount=' + $('#trade_volume').val () + '&note=' + $('#trade_note').val () + '&price=' + price + '&instr=' + id;
	$.ajax ({
			url: 'index.php?view=trade&place=trade&id=' + id,
			async: false,
			dataType: 'html',
			data: postData,
			type: 'POST',
			success: function (response) { $('#between').html (''); $('#tradeLink').unbind ().html (''); $('#orderLink').unbind ().html (''); $('#ajaxResult').html (response); }
			})
	
	updateMarket ();

}


function placeOrder (id)
{
	if ($('#order_volume').val () == '' || $('#order_volume').val () == '0')
	{
		alert ('Please fill in a volume higher than 0.');
		$('#ovolume').css ('color', 'red');
		return false;
	}
	else
	{
		$('#ovolume').css ('color', 'black');
	}


	if ($('#order_action1').attr ('checked') != true && $('#order_action2').attr ('checked') != true)
	{
		alert ('Please select an action, buy or sell.');
		$('#oaction').css ('color', 'red');
		return false;
	}
	else
	{
		$('#oaction').css ('color', 'black');
	}
	if ($('#order_price').val () == '')
	{
		alert ('Please specify your order price type, higher or lower than..');
		$('#oprice').css ('color', 'red');
		return false;
	}
	else
	{
		$('#oprice').css ('color', 'black');
	}
	if ($('#order_price_amount').val () == '')
	{
		alert ('Please the price at which your order should be executed.');
		$('#oprice').css ('color', 'red');
		return false;
	}
	else
	{
		$('#oprice').css ('color', 'black');
	}
	if ($('#order_note').val () == '')
	{
		alert ('Please specify a note/reason for your trade.');
		$('#onote').css ('color', 'red');
		return false;
	}
	else
	{
		$('#onote').css ('color', 'black');
	}


	
	var order;
	var price;
	if ($('#order_action1').attr ('checked') == true)
	{
		price	= $('#aprice').val ();
		order	= 'buy';
	}
	else
	{
		price	= $('#bprice').val ();
		order	= 'sell';
	}

//em=y&am=0&action=buy&lt=&gt=&note=


	var append1;
	var append2;

	if ($('#order_price').val () == 1)
	{
		append1	= 'lt=&gt=' + $('#order_price_amount').val ();
	}
	else
	{
		append1	= 'gt=&lt=' + $('#order_price_amount').val ();
	}

	
	if ($('#order_mail').attr ('checked') == true)
	{
		append2	= 'em=y';
	}
	else
	{
		append2	= 'em=n';
	}

	var postData	= append1 + '&' + append2+ '&action=' + order + '&am=' + $('#order_volume').val () + '&note=' + $('#order_note').val () + '&price=' + price + '&instr=' + id;

	$.ajax ({
			url: 'index.php?view=trade&place=order&id=' + id,
			async: false,
			dataType: 'html',
			data: postData,
			type: 'POST',
			success: function (response) { $('#between').html (''); $('#tradeLink').unbind ().html (''); $('#orderLink').unbind ().html (''); $('#ajaxResult').html (response); }
			})
	updateMarket ();

}
