var brightnessThreshold = 150;
var differenceThreshold = 200;

var style;
var xmlhttp;

function op(u,h,w)
{
	var w=window.open(u,'','scrollbars=yes,width='+w+',height='+h+',resizable=yes');
	w.focus();
}

function Go(x)
{
	var isPostBack = document.getElementById('lblisPostBack').innerHTML;
	if (isPostBack=='False')
	{
		dstyle(x);
	}
	else
	{
		updateAll();
	}
}

function updateAll()
{
	update('drpChartType');
	update('LegendFontColor');
	update('BackColor');
	update('ChartBackColor');
	update('HeaderBackColor');
	update('HeaderForeColor');
	update('FooterBackColor');
	update('FooterForeColor');
	update('ChartOutlineColor');
	update('drpLogo');
	update('txtHeaderCaption');
	update('drpHeaderFont');
	update('drpHeaderFontSize');
	update('txtFooterCaption');
	update('drpFooterFont');
	update('drpFooterFontSize');
	update('chkUseBackgroundImage');
}

function ud()
{
	var h=document.getElementById('txtHeaderCaption');
	var i=document.getElementById('txtFooterCaption');
	var c=document.getElementById('drpInstrument');
	var d=document.getElementById('drpChartType');
	var e=document.getElementById('chkUseBackgroundImage');
	if(h && c && c.selectedIndex>=0 && d && d.selectedIndex>=0)
	{	
		if (e && e.checked==true && i)
		{
			h.value=c.options[c.selectedIndex].text;
			i.value = d.options[d.selectedIndex].text;
			update('txtHeaderCaption');
			update('txtFooterCaption');
		}
		else
		{
			h.value=c.options[c.selectedIndex].text + ' ' + d.options[d.selectedIndex].text;
			update('txtHeaderCaption');
		}
	}
}

function update(control)
{


		if (control=='drpChartType')
		{
			var ls = document.getElementById('drpLineStyle').options[document.getElementById('drpLineStyle').selectedIndex].text;
			cont = document.getElementById(control);
			linestyle = document.getElementById('drpLineStyle');
			oldindex = linestyle.selectedIndex;
			ctype = cont.options[cont.selectedIndex].text;
			
			Items = new Array('Open/High/Low/Close','Candlestick','Close','Line','Lease','Stocks');

			if (ctype.indexOf('OHLC') != -1)
			{
				linestyle.options.length=0
				linestyle.options[0] = new Option(Items[0],0);
				linestyle.options[1] = new Option(Items[1],1);
				linestyle.options[2] = new Option(Items[2],2);
				if (ls == 'Line' || ls == 'Lease' || ls == 'Stocks')
				{
					linestyle.selectedIndex = 0;
				}
				else
				{
					linestyle.selectedIndex = oldindex;
				}
			}
			else
			{
				if (ctype=='Lease Rates')
				{
					linestyle.options.length=0
					linestyle.options[0] = new Option(Items[4],4);
					linestyle.selectedIndex = 0;
				}
				else
				{
					if (ctype=='Stock Close & Warrants')
					{
						linestyle.options.length=0
						linestyle.options[0] = new Option(Items[5],5);
						linestyle.selectedIndex = 0;
					}
					else
					{
						linestyle.options.length=0
						linestyle.options[0] = new Option(Items[3],3);
						linestyle.selectedIndex = 0;
					}
				}
			}
			update('drpLineStyle');			
		}
		if (control=='BackColor')
		{
			var color = document.getElementById(control+'field').value;		
			if (validateColor(color)==true)
			{
				document.getElementById(control).style.backgroundColor =  '#' + color;
				document.getElementById('ChartBackColorMock').style.backgroundColor = '#' + color;
				update('drpLogo');
			}
		}
		if (control=='ChartBackColor')
		{
			var ls = document.getElementById('drpLineStyle').options[document.getElementById('drpLineStyle').selectedIndex].text;
			var color = document.getElementById(control+'field').value;		
			if (validateColor(color)==true)
			{
				document.getElementById(control).style.backgroundColor =  '#' + color;
				document.getElementById('ChartBorderMock').style.backgroundColor = '#' + color;
				if (document.getElementById('chkUseBackgroundImage').checked==true)
				{
					update('drpLogo');
				}

			}
		}
		if (control=='HeaderBackColor')
		{
			var color = document.getElementById(control+'field').value;		
			if (validateColor(color)==true)
			{
				document.getElementById(control).style.backgroundColor =  '#' + color;
				document.getElementById('CaptionMock').style.backgroundColor = '#' + color;
			}
		}
		if (control=='FooterBackColor')
		{
			var color = document.getElementById(control+'field').value;		
			if (validateColor(color)==true)
			{
				document.getElementById(control).style.backgroundColor =  '#' + color;
				document.getElementById('CaptionBottomMock').style.backgroundColor = '#' + color;
			}
		}
		if (control=='HeaderForeColor')
		{
			var color = document.getElementById(control+'field').value;		
			if (validateColor(color)==true)
			{
				document.getElementById(control).style.backgroundColor =  '#' + color;
				document.getElementById('CaptionMock').style.color = '#' + color;
			}
		}
		if (control=='FooterForeColor')
		{
			var color = document.getElementById(control+'field').value;		
			if (validateColor(color)==true)
			{
				document.getElementById(control).style.backgroundColor =  '#' + color;
				document.getElementById('CaptionBottomMock').style.color = '#' + color;
			}
		}
		if (control=='ChartOutlineColor')
		{
			var color = document.getElementById(control+'field').value;
			if (validateColor(color)==true)
			{
				document.getElementById(control).style.backgroundColor = '#'+color;
				document.getElementById('ChartBackColorMock').style.borderColor = '#'+color;
			}
		}
		if (control=='Line1Color')
		{
			var ls = document.getElementById('drpLineStyle').options[document.getElementById('drpLineStyle').selectedIndex].text;
			var color = document.getElementById(control+'field').value;	
			if (validateColor(color)==true)
			{
				var chart = document.getElementById('ChartLines');
				var i;
				for (i=1;i<=(chart.childNodes.length-1);i++)
				{
					if (ls == 'Close' || ls =='Line')
					{	
						chart.childNodes[i].style.borderColor = '#'+color;
					}
					else if (ls == 'Open/High/Low/Close')
					{
						chart.childNodes[i].style.backgroundColor = '#'+color;
					}
					else if ( (ls=='Candlestick') && (chart.childNodes[i].id.substring(0,2)=='up') )
					{
						chart.childNodes[i].style.backgroundColor = '#'+color;
					}
					else if ( (ls=='Stocks' || ls=='Lease') && (chart.childNodes[i].id.substring(0,3)=='ln1') )
					{
						chart.childNodes[i].style.borderColor = '#'+color;
					}
				}
				document.getElementById(control).style.backgroundColor =  '#' + color;
				document.getElementById('LegendAMock').style.backgroundColor = '#' + color;
				document.getElementById('LegendLabelAMock').style.color = '#' + color;
				document.getElementById('LegendLabelBMock').style.color = '#' + color;
				document.getElementById('LegendLabelCMock').style.color = '#' + color;
				document.getElementById('LegendLabelDMock').style.color = '#' + color;
			}
		}
		if (control=='Line2Color')
		{
			var ls = document.getElementById('drpLineStyle').options[document.getElementById('drpLineStyle').selectedIndex].text;
			var color = document.getElementById(control+'field').value;		
			if (validateColor(color)==true)
			{
				var chart = document.getElementById('ChartLines');
				var i;
				for (i=1;i<=(chart.childNodes.length-1);i++)
				{
				 	if (ls == 'Close' || ls =='Line')
					{
						chart.childNodes[i].style.borderColor = '#'+color;
					}
					else if ( (ls=='Candlestick') && (chart.childNodes[i].id.substring(0,4)=='down') )
					{
						chart.childNodes[i].style.backgroundColor = '#'+color;
					}
					else if ( (ls=='Stocks' || ls=='Lease') && (chart.childNodes[i].id.substring(0,3)=='ln2') )
					{
						chart.childNodes[i].style.borderColor = '#'+color;
					}
				}
				document.getElementById(control).style.backgroundColor =  '#' + color;
				if (ls == 'Lease' || ls == 'Stocks')
				{
					document.getElementById('LegendBMock').style.backgroundColor = '#'+color;
				}
			}
		}
		if (control=='Line3Color')
		{
			var ls = document.getElementById('drpLineStyle').options[document.getElementById('drpLineStyle').selectedIndex].text;
			var color = document.getElementById(control+'field').value;		
			if (validateColor(color)==true)
			{
				var chart = document.getElementById('ChartLines');
				var i;
				for (i=1;i<=(chart.childNodes.length-1);i++)
				{
					if (ls=='Candlestick')
					{
						chart.childNodes[i].style.borderColor = '#'+color;
					}
					else if ( (ls=='Stocks' || ls=='Lease') && (chart.childNodes[i].id.substring(0,3)=='ln3') )
					{
						chart.childNodes[i].style.borderColor = '#'+color;
					}
				}
				document.getElementById(control).style.backgroundColor =  '#' + color;
				if (ls == 'Lease'  || ls == 'Stocks' ) 
				{
					document.getElementById('LegendCMock').style.backgroundColor = '#'+color;
				}
			}
		}

		if (control=='Line4Color')
		{
			var ls = document.getElementById('drpLineStyle').options[document.getElementById('drpLineStyle').selectedIndex].text;
			var color = document.getElementById(control+'field').value;		
			if (validateColor(color)==true)
			{
				var chart = document.getElementById('ChartLines');
				var i;
				for (i=1;i<=(chart.childNodes.length-1);i++)
				{
					if ( (ls=='Stocks' || ls=='Lease') && (chart.childNodes[i].id.substring(0,3)=='ln4') )
					{
						chart.childNodes[i].style.borderColor = '#'+color;
					}
				}
				document.getElementById(control).style.backgroundColor =  '#' + color;
				if (ls == 'Lease' || ls == 'Stocks')
				{
					document.getElementById('LegendDMock').style.backgroundColor = '#'+color;
				}
			}
		}


		if (control=='LegendFontColor')
		{
			var color = document.getElementById(control+'field').value;		
			if (validateColor(color)==true)
			{
				document.getElementById(control).style.backgroundColor =  '#' + color;

				document.getElementById('XAxisLabelAMock').style.color = '#' + color;
				document.getElementById('XAxisLabelBMock').style.color = '#' + color;
				document.getElementById('XAxisLabelCMock').style.color = '#' + color;
				document.getElementById('XAxisLabelDMock').style.color = '#' + color;
				document.getElementById('XAxisLabelEMock').style.color = '#' + color;
				document.getElementById('XAxisLabelFMock').style.color = '#' + color;
				document.getElementById('XAxisLabelGMock').style.color = '#' + color;
				document.getElementById('XAxisLabelHMock').style.color = '#' + color;

				document.getElementById('YAxisLabelAMock').style.color = '#' + color;
				document.getElementById('YAxisLabelBMock').style.color = '#' + color;
				document.getElementById('YAxisLabelCMock').style.color = '#' + color;
				document.getElementById('YAxisLabelDMock').style.color = '#' + color;
				document.getElementById('YAxisLabelEMock').style.color = '#' + color;
				document.getElementById('YAxisLabelFMock').style.color = '#' + color;
				document.getElementById('YAxisLabelGMock').style.color = '#' + color;
				document.getElementById('YAxisLabelHMock').style.color = '#' + color;
				document.getElementById('YAxisLabelIMock').style.color = '#' + color;
				document.getElementById('YAxisLabelJMock').style.color = '#' + color;
			}
		}
		if (control=='drpLogo')
		{
			var site=document.getElementById('drpLogo').value;
	
			var logocol = 'FFCC33';
			if (site == 'TheBullionDesk')
			{
				logocol = 'FFCC33';
			}
			if (site == 'BaseMetals')
			{
				logocol = 'FF9933';
			}
			if (site == 'MinorMetals')
			{
				logocol = 'FF4242';
			}
			var lr= parseInt(logocol.substring(0,2),16);
			var lg= parseInt(logocol.substring(2,4),16);
			var lb= parseInt(logocol.substring(4,6),16);

			var color = document.getElementById('ChartBackColorfield').value;	
			var r= parseInt(color.substring(0,2),16);
			var g= parseInt(color.substring(2,4),16);
			var b= parseInt(color.substring(4,6),16);

			var bright = ((r*299) + (g*587) + (b*114)) / 1000;
			var diff = Math.max(r, lr) - Math.min(r , lr) + Math.max(g, lg) - Math.min(g , lg) + Math.max(b, lb) - Math.min(b , lb);
			
			if (bright > brightnessThreshold)
			{
				if (diff < differenceThreshold)
				{document.getElementById('WatermarkMock').src = 'img/watermark/'+site+'ko.gif';}
				else
				{document.getElementById('WatermarkMock').src = 'img/watermark/'+site+'k.gif';}
			}
			else
			{
				if (diff < differenceThreshold)
				{document.getElementById('WatermarkMock').src = 'img/watermark/'+site+'wo.gif';}
				else
				{document.getElementById('WatermarkMock').src = 'img/watermark/'+site+'w.gif';}
			}			
			var color = document.getElementById('BackColorfield').value;	
			var r = parseInt(color.substring(0,2),16);
			var g = parseInt(color.substring(2,4),16);
			var b = parseInt(color.substring(4,6),16);
			var bright = ((r*299) + (g*587) + (b*114)) / 1000;
			var diff = Math.max(r, lr) - Math.min(r , lr) + Math.max(g, lg) - Math.min(g , lg) + Math.max(b, lb) - Math.min(b , lb);
			if (bright > brightnessThreshold)
			{
				if (diff < differenceThreshold)
				{
					document.getElementById('LogoMock').src = 'img/logo/'+site+'ko.gif';
				}
				else
				{	
					document.getElementById('LogoMock').src = 'img/logo/'+site+'k.gif';
				}
			}
			else
			{
				if (diff < differenceThreshold)
				{
					document.getElementById('LogoMock').src = 'img/logo/'+site+'wo.gif';
				}
				else
				{
					document.getElementById('LogoMock').src = 'img/logo/'+site+'w.gif';
				}
			}	
		}

		if (control=='txtHeaderCaption')
		{
			var caption = document.getElementById(control).value;
			if (caption == '')
			{
				document.getElementById('CaptionMock').style.display = 'none';
			}	
			else
			{
				document.getElementById('CaptionMock').style.display = 'block';
			}
			document.getElementById('CaptionMock').innerHTML = caption.replace(/ /g,'&nbsp;');
		}
		if (control=='txtFooterCaption')
		{
			var caption = document.getElementById(control).value;
			if (caption == '')
			{
				document.getElementById('CaptionBottomMock').style.display = 'none';
				document.getElementById('LogoMock').style.display = 'block';
			}
			else
			{
				document.getElementById('CaptionBottomMock').style.display = 'block';
				if (document.getElementById('chkUseBackgroundImage').checked==true)
				{
					document.getElementById('LogoMock').style.display = 'none';
				}
			}
			document.getElementById('CaptionBottomMock').innerHTML = caption.replace(/ /g,'&nbsp;');
		}
		if (control=='drpHeaderFont')
		{
			var font=document.getElementById(control).options[document.getElementById(control).selectedIndex].text;
			document.getElementById('CaptionMock').style.fontFamily = font;
		}
		if (control=='drpFooterFont')
		{
			var font=document.getElementById(control).options[document.getElementById(control).selectedIndex].text;
			document.getElementById('CaptionBottomMock').style.fontFamily = font;
		}
		if (control=='drpHeaderFontSize')
		{
			var fontsize = parseInt(document.getElementById(control).value);
			document.getElementById('CaptionMock').style.fontSize = fontsize+'pt';
		}
		if (control=='drpFooterFontSize')
		{
			var fontsize = parseInt(document.getElementById(control).value);
			document.getElementById('CaptionBottomMock').style.fontSize = fontsize+'pt';
		}
		if (control=='drpChartPoints')
		{
			drawLines();
		}
		if (control=='drpLineStyle'||control=='drpChartSize')
		{
			var ls = document.getElementById('drpLineStyle').options[document.getElementById('drpLineStyle').selectedIndex].text;
			var size = document.getElementById('drpChartSize').value;
			var line;
			if (ls=='Candlestick')
			{
				line='cs';
				document.getElementById('Line1ColorTitle').innerHTML = 'Box Up Color';
				document.getElementById('Line2ColorTitle').innerHTML = 'Box Down Color';
				document.getElementById('Line3ColorTitle').innerHTML = 'Box Outline Color';
				document.getElementById('tblLines23').style.display = 'block';
				document.getElementById('tblLines4').style.display = 'none';
			}
			else
			{
				if (ls=='Lease')
				{
					line='ls';
					document.getElementById('tblLines23').style.display = 'block';
					document.getElementById('tblLines4').style.display = 'block';
					document.getElementById('LegendLabelAMock').innerHTML = '1M Lease %';
					document.getElementById('LegendLabelBMock').innerHTML = '3M Line Color';
					document.getElementById('LegendLabelCMock').innerHTML = '6M Line Color';
					document.getElementById('Line1ColorTitle').innerHTML = '1M Line Color';
					document.getElementById('Line2ColorTitle').innerHTML = '3M Line Color';
					document.getElementById('Line3ColorTitle').innerHTML = '6M Line Color';
					document.getElementById('Line4ColorTitle').innerHTML = '12M Line Color';
				}
				else
				{
					if (ls=='Stocks')
					{
						line='st';
						document.getElementById('tblLines23').style.display = 'block';
						document.getElementById('tblLines4').style.display = 'none';
						document.getElementById('LegendLabelAMock').innerHTML = 'Closing Stock';
						document.getElementById('LegendLabelBMock').innerHTML = 'Cancelled Warrants';
						document.getElementById('LegendLabelCMock').innerHTML = 'On Warrant';
						document.getElementById('Line1ColorTitle').innerHTML = 'Closing Stock';
						document.getElementById('Line2ColorTitle').innerHTML = 'Cancelled Warrants';
						document.getElementById('Line3ColorTitle').innerHTML = 'On Warrant';
					}
					else
					{
						document.getElementById('tblLines23').style.display = 'none';
						document.getElementById('tblLines4').style.display = 'none';
						document.getElementById('Line1ColorTitle').innerHTML = 'Line Color';										
						if (ls=='Open/High/Low/Close') line='oh';
						if (ls=='Close') line='cl';
						if (ls=='Line')
						{
							line='ln';
							var charttype=document.getElementById('drpChartType');
							document.getElementById('LegendLabelAMock').innerHTML = charttype.options[charttype.selectedIndex].text;
						}
					}

				}
			}
			var chrtpnts = document.getElementById('drpChartPoints');
			var newselindex = chrtpnts.selectedIndex;
			var oldselection = chrtpnts.options[newselindex].text;
			if (ls=='Open/High/Low/Close' || ls=='Candlestick')
			{
				if (chrtpnts.options.length > 5) 
				{
					chrtpnts.options.length = 0;
					values = new Array('5','7','10','25','50','100','200','400','800','1200');
					var i;	
					for (i=0;i < 5; i++)
					{
						chrtpnts.options[i] = new Option(values[i],i+1);
						if (values[i] == oldselection) 
						{
							newselindex = i;
						}
					}
				}
			}
			else
			{
				if (chrtpnts.options.length <= 5 || ls=='Lease') 
				{
					chrtpnts.options.length = 0;
					values = new Array('5','7','10','25','50','100','200','400','800','1200');
					var i;
					var leaseoffset = 0;
					if (ls=='Lease') 
					{
						values = new Array('25','50','100','200','400','800','1200');
						leaseoffset = 3;
					}
					for (i=0;i < values.length; i++)
					{
						chrtpnts.options[i] = new Option(values[i],i+1+leaseoffset);
						if (values[i] == oldselection) 
						{
							newselindex = i;
						}
					}
				}
			}	
			if (newselindex > chrtpnts.options.length) newselindex = 4;	
			document.getElementById('drpChartPoints').selectedIndex = newselindex;

			document.getElementById('ChartBackColorMock').className=size+line;

			//update('Line1Color');
			//update('Line2Color');
			//update('Line3Color');
			//update('Line4Color');
			drawLines();
		}
		if (control =='chkUseBackgroundImage')
		{
			if (document.getElementById(control).checked==true)
			{
				document.getElementById('WatermarkMock').style.display='block';
				document.getElementById('tblBottomCaption').style.display = 'block';
				update('txtFooterCaption');
			}
			else
			{
				document.getElementById('WatermarkMock').style.display='none';
				document.getElementById('tblBottomCaption').style.display='none';
				document.getElementById('CaptionBottomMock').style.display='none';
				document.getElementById('LogoMock').style.display='block';
			}
		}

}

function drawLines()
{
	var chart = document.getElementById('ChartLines');

	var linestyle = document.getElementById('drpLineStyle').options[document.getElementById('drpLineStyle').selectedIndex].text;

	var color = '#' + document.getElementById('Line1Colorfield').value;		
	var color2 = '#' + document.getElementById('Line2Colorfield').value;		
	var color3 = '#' + document.getElementById('Line3Colorfield').value;		
	var color4 = '#' + document.getElementById('Line4Colorfield').value;		
	var points = document.getElementById('drpChartPoints').options[document.getElementById('drpChartPoints').selectedIndex].text;
	
	var size = document.getElementById('drpChartSize').options[document.getElementById('drpChartSize').selectedIndex].text;

	var top = 5;
	var bottom;
	var width;

	if (size=='Large')
	{
		bottom = 147;
		width = 171;
	}
	if (size=='Medium')
	{
		bottom = 97;
		width = 121;
	}
	if (size=='Small')
	{
		bottom = 57;
		width = 81;
	}

	if ( ( ( linestyle == 'Close' ) || ( linestyle == 'Line' ) ) && ( validateColor(color.substring(1)) ) )
	{
		chart.innerHTML = drawNormalLine(top,bottom,color,width,points);
	}
	
	if ( ( linestyle == 'Candlestick' ) && ( validateColor(color.substring(1)) ) && ( validateColor(color2.substring(1)) ) && ( validateColor(color3.substring(1)) ) )
	{
		chart.innerHTML = drawCandleStick(top,bottom,color,color2,color3,width,points);
	}

	if ( ( linestyle == 'Open/High/Low/Close' ) && ( validateColor(color.substring(1)) ) )
	{
		chart.innerHTML = drawOHLCLines(top,bottom,color,width,points);
	}

	if ( (linestyle == 'Lease')  && ( validateColor(color.substring(1)) ) && ( validateColor(color2.substring(1)) ) && ( validateColor(color3.substring(1)) ) )
	{
		chart.innerHTML = drawMultiLines((top+20),bottom,color,color2,color3,color4,4,width,points);
	}
	if ( (linestyle == 'Stocks')  && ( validateColor(color.substring(1)) ) && ( validateColor(color2.substring(1)) ) && ( validateColor(color3.substring(1)) ) && ( validateColor(color4.substring(1)) ) )
	{
		chart.innerHTML = drawMultiLines((top+20),bottom,color,color2,color3,color4,3,width,points);
	}
	
	var ls = document.getElementById('drpLineStyle').options[document.getElementById('drpLineStyle').selectedIndex].text;
	if ( validateColor(color.substring(1)) )
	{
		document.getElementById('Line1Color').style.backgroundColor =  color;
		document.getElementById('LegendAMock').style.backgroundColor = color;
		document.getElementById('LegendLabelAMock').style.color = color;
		document.getElementById('LegendLabelBMock').style.color = color;
		document.getElementById('LegendLabelCMock').style.color = color;
		document.getElementById('LegendLabelDMock').style.color = color;
	}
	if ( validateColor(color2.substring(1)) )
	{
		document.getElementById('Line2Color').style.backgroundColor =  color2;
		if (ls == 'Lease' || ls == 'Stocks')
		{
			document.getElementById('LegendBMock').style.backgroundColor = color2;
		}
	}
	if ( validateColor(color3.substring(1)) )
	{
		document.getElementById('Line3Color').style.backgroundColor =  color3;
		if (ls == 'Lease'  || ls == 'Stocks' ) 
		{
			document.getElementById('LegendCMock').style.backgroundColor = color3;
		}
	}
	if ( validateColor(color4.substring(1)) )
	{
		document.getElementById('Line4Color').style.backgroundColor =  color4;
		if (ls == 'Lease' || ls == 'Stocks')
		{
			document.getElementById('LegendDMock').style.backgroundColor = color4;
		}
	}
}

function drawNormalLine(t,b,col,w,no)
{
	var step = (w/no);
	if (step < 1)
	{
		step = 1;
		no = w;
	}

	var ie;	
	if (window.ActiveXObject)
	{
		ie = true;
	}
	else
	{
		ie = false;
	}
	var top;
	var left;
	var height = 0;
	var i;
	var line = '';
	var exit = Math.floor(rand(t,b));
	var newexit;
	var low;
	var high;	
	var rf = ( 10*((b-t)/no) );
	if (rf < 1) rf = 1;
	var trendup = 0;
	var trenddown = rf;

	for (i=0;i<(no);i++)
	{
		if ((exit-(rf+trendup)) < t)
		{
			low = t;
		}
		else
		{
			low = exit - (rf + trendup);
		}
		if ((exit + (rf+trenddown)) > b)
		{
			high = b;
		}
		else
		{
			high = exit + rf + trenddown;
		}
		if (Math.abs(exit-t) < 5)
		{
			trendup = 0;
			trenddown = (rf/2);
		}
		if (Math.abs(exit-b) < 5)
		{
			trendup = (rf/2);
			trenddown = 0;
		}
		newexit = rand(low,high);
		height = newexit-exit;
		if (height > 0)
		{
			top = exit;
		}
		else
		{
			top = newexit;
		}		
		if (ie) 
		{
			line += '<div style="position:absolute;top:'+(21+top);
		}
		else
		{
			line += '<div style="position:absolute;top:'+(22+top);
		}
		line += ';left:'+(6+(i*step));
		if (height>=0)
		{
			if(i!=(no-1))
			{
				line += ';border-right: 1px solid '+col;
			}
			line += ';border-top: 1px solid '+col;
		}
		else
		{
			if(i!=(no-1))
			{
				line += ';border-right: 1px solid '+col;
			}
			line += ';border-bottom: 1px solid '+col;
		}
		if (ie)
		{
			line += ';width:'+(step+1);
			line += ';height:'+ (Math.abs(height)+1);
		}
		else
		{
			line += ';width:'+step;
			line += ';height:'+ Math.abs(height);
		}
		line += ';font-size:1px;line-height:0;">&nbsp;</div>';
		exit = newexit;
	}
	return line;
	
}

function drawCandleStick(t,b,col,col2,col3,w,no)
{
	var line='';
	var line2='';
	var line3='';
	if (window.ActiveXObject)
	{
		ie = true;
	}
	else
	{
		ie = false;
	}
	var step = (w/no);
	if (step < 1)
	{
		step = 1;
		no = w;
	}
	var barw = Math.floor(5*(step/12));
	var offset = Math.floor(step/2);
	var top;
	var left;
	var height = 0;
	var i;
	var line;
	var exit = Math.floor(rand(t,b));
	var newexit;
	var low;
	var high;
	var low2;
	var high2;
	var bxid;
	var rf = ( 6*((b-t)/no) );
	var rf2 = ( 2*((b-t)/no) );
	if (rf < 1) rf = 1;
	if (rf2 < 1) rf2 = 1;
	var trendup = 0;
	var trenddown = rf;
	var colarray = new Array (col,col2);
	var bxc;
	for (i=0;i<no;i++)
	{
		if ( (exit-rf-trendup) < t )
		{
			low = t;
		}
		else
		{
			low = (exit - rf - trendup);
		}

		if ( (exit+rf+trenddown) > b )
		{
			high = b;
		}
		else
		{
			high = (exit + rf + trenddown);
		}
		
		if ( (exit - rf - rf2) < t )
		{
			low2 = t;
		}
		else
		{
			low2 = (exit - rf - rf2);
		}

		if ( (exit + rf + rf2) > b )
		{
			high2 = b;
		}
		else
		{
			high2 = (exit + rf + rf2);
		}
		newexit = rand(low,high);
		if (newexit >= exit)
		{
			bxt = exit;
			bxb = newexit;
			bxh = Math.abs(exit - newexit);
			bxc = col2;
			bxid='down';	
		}
		else
		{
			bxt = newexit;
			bxb = exit;
			bxh = Math.abs(exit - newexit);
			bxc = col;
			bxid = 'up';
		}
		cst = rand(bxt,low2);
		csb = rand(bxb,high2);
		csh = csb-cst;	
		if (Math.abs(cst-t)<5)
		{
			trenddown = rf;
			trendup = 0;
		}
		
		if (Math.abs(csb-b)<5)
		{
			trenddown = 0;
			trendup = rf;
		}
		line += '<div style="position:absolute';
		line += ';left:' + (5 + offset + (i*step));
		line += ';top:' + (22 + cst);
		line += ';height:' + csh;
		line += ';width:1px';
		line += ';border-left-width: 1px;';
		line += ';border-left-style: solid;';
		line += ';border-color:'+col3;
		line += ';font-size:1px;line-height:0;">&nbsp;</div>';	

		line += '<div '
		line += 'id = "'+ bxid + i;
		line += '"style="position:absolute';
		line += ';left:' + (5 + offset + (i*step) - (barw));
		line += ';top:' + (22 + bxt);
		line += ';height:' + bxh;
		if (ie)
		{
			line += ';width:' + ((2*(barw))+2);
		}
		else
		{
			line += ';width:' + (2*(barw));
		}
		line += ';background-color:' + bxc;
		line += ';border-width: 1px;';
		line += ';border-style: solid;';		
		line += ';border-color:' + col3;
		line += ';font-size:1px;line-height:0;">&nbsp;</div>';

		exit = newexit;	
	}

	return line
}

function drawOHLCLines(t,b,col,w,no)
{
	var step = (w/no);
	if (step < 1)
	{
		step = 1;
		no = w;
	}
	var offset = Math.floor(step/2);
	var top;
	var left;
	var height = 0;
	var i;
	var line;
	var exit = Math.floor(rand(t,b));
	var newexit;
	var low;
	var high;	
	var rf = ( 10*((b-t)/no) );
	if (rf < 1) rf = 1;
	var trendup = 0;
	var trenddown = 0;

	for (i=0;i<(no);i++)
	{
		if ((exit - (rf+trendup)) < t)
		{
			low = t;
		}
		else
		{
			low = exit - (rf + trendup);
		}

		if ((exit + (rf+trenddown)) > b)
		{
			high = b;
		}
		else
		{
			high = exit + rf + trenddown;
		}
		
		highpoint = rand(exit,low);
		lowpoint = rand(high, exit);

		if (Math.abs(highpoint - t) < 5)
		{
			trenddown = rf;
			trendup = 0;
		}
		if (Math.abs(lowpoint - b) < 5)
		{
			trenddown = 0;
			trendup = rf;
		}

		height = lowpoint-highpoint;
		
		line += '<div style="position:absolute;top:'+(22+exit);
		if (i==0)
		{
			line += ';left:'+(6);
			line += ';width:'+(step/2);
		}
		else
		{
			line += ';left:'+(offset+((i-1)*step)+6);
			line += ';width:'+(step);
		}
		line += ';height:1'
		line += ';background-color:'+col;
		line += ';font-size:1px;line-height:0;">&nbsp;</div>';
		
		line += '<div style="position:absolute;top:'+(22+highpoint);
		line += ';left:'+(offset+ 6+(i*step));
		line += ';background-color:'+col;
		line += ';width:1';
		line += ';height:'+ Math.abs(height);
		line += ';font-size:1px;line-height:0;">&nbsp;</div>';

		exit = rand(highpoint, lowpoint)
	}
	line += '<div style="position:absolute;top:'+(22+exit);
	line += ';left:'+(offset+((no-1)*step)+6);
	line += ';width:'+(step/2);
	line += ';height:1'
	line += ';background-color:'+col;
	line += ';"><!-- --></div>';
	return line;
}

function drawMultiLines(t,b,col,col2,col3,col4,ln,w,no)
{
	var step = (w/no);
	if (step < 1)
	{
		step = 1;
		no = w;
	}

	var ie;	
	if (window.ActiveXObject)
	{
		ie = true;
	}
	else
	{
		ie = false;
	}
	
	var mloffset = ( (b-t)/ln );
	b = t + mloffset;

	var top;
	var left;
	var height = 0;
	var i;
	var line = '';
	var line2 = '';
	var line3 = '';
	var line4 = '';
	var exit = Math.floor(rand(t,b));
	var newexit;
	var low;
	var high;	
	var rf = ( 10*((b-t)/no) );
	if (rf < 1) rf = 1;
	var trendup = 0;
	var trenddown = rf;


	for (i=0;i<(no);i++)
	{

		if ((exit-(rf+trendup)) < t)
		{
			low = t;
		}
		else
		{
			low = exit - (rf + trendup);
		}

		if ((exit + (rf+trenddown)) > b)
		{
			high = b;
		}
		else
		{
			high = exit + rf + trenddown;
		}
		
		if (Math.abs(exit-t) < 5)
		{
			trendup = 0;
			trenddown = (rf/2);
		}

		if (Math.abs(exit-b) < 5)
		{
			trendup = (rf/2);
			trenddown = 0;
		}

		newexit = rand(low,high);
		height = newexit-exit;

		if (height > 0)
		{
			top = exit;
		}
		else
		{
			top = newexit;
		}		

		if (ie) 
		{
			line  += '<div id="ln1'+i+'" style="position:absolute;top:'+(21+top);
			line2 += '<div id="ln2'+i+'" style="position:absolute;top:'+(21+top+(1*mloffset));
			line3 += '<div id="ln3'+i+'" style="position:absolute;top:'+(21+top+(2*mloffset));
			line4 += '<div id="ln4'+i+'" style="position:absolute;top:'+(21+top+(3*mloffset));
		}
		else
		{
			line  += '<div id="ln1'+i+'" style="position:absolute;top:'+(22+top);
			line2 += '<div id="ln2'+i+'" style="position:absolute;top:'+(22+top+(1*mloffset));
			line3 += '<div id="ln3'+i+'" style="position:absolute;top:'+(22+top+(2*mloffset));
			line4 += '<div id="ln4'+i+'" style="position:absolute;top:'+(22+top+(3*mloffset));
		}

		line  += ';left:'+(6+(i*step));
		line2 += ';left:'+(6+(i*step));
		line3 += ';left:'+(6+(i*step));
		line4 += ';left:'+(6+(i*step));

		if (height>=0)
		{
			if(i!=(no-1))
			{
				line  += ';border-right: 1px solid '+col;
				line2 += ';border-right: 1px solid '+col2;
				line3 += ';border-right: 1px solid '+col3;
				line4 += ';border-right: 1px solid '+col4;
			}
			line  += ';border-top: 1px solid '+col;
			line2 += ';border-top: 1px solid '+col2;
			line3 += ';border-top: 1px solid '+col3;
			line4 += ';border-top: 1px solid '+col4;
		}
		else
		{
			if(i!=(no-1))
			{
				line  += ';border-right: 1px solid '+col;
				line2 += ';border-right: 1px solid '+col2;
				line3 += ';border-right: 1px solid '+col3;
				line4 += ';border-right: 1px solid '+col4;
			}
			line  += ';border-bottom: 1px solid '+col;
			line2 += ';border-bottom: 1px solid '+col2;
			line3 += ';border-bottom: 1px solid '+col3;
			line4 += ';border-bottom: 1px solid '+col4;
		}
		
		if (ie)
		{
			line  += ';width:'+(step+1);
			line  += ';height:'+ (Math.abs(height)+1);
			line2 += ';width:'+(step+1);
			line2 += ';height:'+ (Math.abs(height)+1);
			line3 += ';width:'+(step+1);
			line3 += ';height:'+ (Math.abs(height)+1);
			line4 += ';width:'+(step+1);
			line4 += ';height:'+ (Math.abs(height)+1);
		}
		else
		{
			line  += ';width:'+step;
			line  += ';height:'+ Math.abs(height);
			line2 += ';width:'+step;
			line2 += ';height:'+ Math.abs(height);
			line3 += ';width:'+step;
			line3 += ';height:'+ Math.abs(height);
			line4 += ';width:'+step;
			line4 += ';height:'+ Math.abs(height);
		}

		line  += ';font-size:1px;line-height:0;">&nbsp;</div>';
		line2 += ';font-size:1px;line-height:0;">&nbsp;</div>';
		line3 += ';font-size:1px;line-height:0;">&nbsp;</div>';
		line4 += ';font-size:1px;line-height:0;">&nbsp;</div>';

		exit = newexit;
	}
	if (ln>=2)
	{
		line += line2;
	}
	if (ln>=3)
	{	
		line += line3;
	}
	if (ln>=4)
	{
		line += line4;
	}
	return (line);
	
}

function validateColor(color)
{
	var regex= new RegExp('^#?[0-9|A-F|a-f]{6,6}$');
	return (color.match(regex)!=null);
}

function dstyle(z)
{
	style = z;
	xmlDoc = loadXMLDoc("styles.xml");
}

function dstyleresponse()
{
	if(xmlhttp.readyState!=4)
	{
		return;
	}

	if(xmlhttp.status!=200)
	{
		alert("Problem retrieving XML data");
		return;
	}
	
	xmlDoc = xmlhttp.responseXML.documentElement;
	
	count =  xmlDoc.getElementsByTagName("chartstylemapping")[0].getElementsByTagName("*").length;
	var i;
	for(i=0 ; i<=count-1 ; i++)
	{
		tagname = xmlDoc.getElementsByTagName("chartstylemapping")[0].getElementsByTagName("*")[i].tagName;
		elementname = xmlDoc.getElementsByTagName("chartstylemapping")[0].getElementsByTagName("*")[i].childNodes[0].nodeValue;
		noofstyles = xmlDoc.getElementsByTagName("chartstyle").length;
		var j;
		for (j=0 ; j<noofstyles ; j++)
		{
			if (xmlDoc.getElementsByTagName("chartstyle")[j].attributes.getNamedItem("id").value == style )
			{
				elementvalue = xmlDoc.getElementsByTagName("chartstyle")[j].getElementsByTagName("*")[i].childNodes[0].nodeValue;
				try
				{
					if (elementname.indexOf('chk') == 0)
					{
						document.getElementById(elementname).checked = (elementvalue == 'true');
					}
					else
					{
						if (elementname.indexOf('drp') == 0)
						{				
							var optiondrop = document.getElementById(elementname);
							var k;
							if (optiondrop)
							{
								for (k=0;k<optiondrop.options.length;k++)
								{
									if (optiondrop.options[k].text == elementvalue)
									{
										optiondrop.selectedIndex = k;
										break;
									}
								}
							}
						}
						else
						{
							document.getElementById(elementname).value = elementvalue;

						}
					}
				}
				catch (e)
				{
				}
				break;
			}
		}
	}
	ud();
	updateAll();

}

function loadXMLDoc(url)
{
	xmlhttp=null;

	if (window.XMLHttpRequest)
	{// code for IE7, Firefox, Mozilla, etc.
		  xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE5, IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=dstyleresponse;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}


function rand(a,b)
{
    var ranNum= a + (Math.random()*(b-a));
    return ranNum;
}
