var curSideValue = 0;
var curPeriod = 0;	// 0:Day, 1:Week, 2:Month, 3:Min, 4:Tick
var onChart = true;
function changePeriod(index) {
	if ( chart_on == false ) return;
	curPeriod = index;

	if (index == 3) {
		if (document.all.cycle)
			document.all.cycle.innerText = "Min";
	} else if (index == 4) {
		if (document.all.cycle)
			document.all.cycle.innerText = "Tick";
	}
	inquiry();
}

function showSideBar() {
	if ( chart_on == false ) return;
	var chart = document.all.TWebChart;

	if (curSideValue == 0) {
		curSideValue = 1;
		document.all.sideBar.innerText = "<<";
	} else {
		curSideValue = 0;
		document.all.sideBar.innerText = ">>";
	}
	chart.ShowSidebar = curSideValue;
}

function inquiry(f) {
	if ( chart_on == false ) { alert('Chart is disabled.'); return false; }
	if ( f == null ) {
		if ( document.forms == null ) { alert('There are not any forms.'); return false; }
		for( var i = 0; i < document.forms.length; i++) {
			if(document.forms[i].symbol != null) { f = document.forms[i]; break; }				
		}
		if ( f == null ) { alert('There is not a chart form.'); return false; }
	}
	// var f = document.forms[0];
	var chart = document.all.TWebChart;
	/*
	var symbol = f.symbol.value;
	var symbolName = getSymbolName(symbol);

	if (symbol == "0001") {
		symbolName = "HOSE";
		chart.MarketType = "09";
	}
	else if (symbol == "0101") {
		symbolName = "HNX";
		chart.MarketType = "19";
	}
	else if (symbolName == "") {
		return false;
		chart.MarketType = getMarketType(symbol, "1");
	}
	*/

	var sym = getSymbol(f.symbol.value);
	if(sym == null) return;
	var symbol = sym.symbol;
	var symbolName = sym.getLangName();

	chart.strJongCode = symbol;
	chart.strJongName = symbolName;
	chart.MarketType  = sym.clas;

	if (f.startDate.value.length == 8)
		chart.StartDate = f.startDate.value;
	else
		chart.StartDate = isBlank(f.startDate.value) ? "0" : convertYmd(f.startDate.value);

	if (f.endDate.value.length == 8)
		chart.EndDate = f.endDate.value;
	else
		chart.EndDate = isBlank(f.endDate.value) ? "0" : convertYmd(f.endDate.value);

	chart.DataCount		= f.dataCnt.value;
	chart.ChartPeriod	= curPeriod;
	chart.CycleCount	= f.cycleCnt.value;
    chart.QueryChartData();

	//f.symbol.select();
	f.symbol.focus();
	//document.body.focus();
	return true;
}

// Parameter - mode(0:Detail, 1:Simple)
function showChart(mode) {
	try {
		if ( chart_on == false ) return;
		var chart = document.all.TWebChart;
	    chart.style.display = "block";
	    if (inquiry() == false) return;
		if (mode == 1) {
			chart.ShowLeftAxis(false); // Left Number Unvisible
			chart.ShowCurInfoWnd(false); // Top Quotation Unvisible
			chart.ShowHighLow(false); // Chart Higest/Lowest Unvisible
			chart.ShowHLC(false); // LC, HC Unvisible
		}
	} catch(e) {
		// alert(e);
	}
}

function addChart(userID, chartWidth, chartHeight, obj) {
	if ( chart_on == false ) return;
	var endDate = '';
	if(obj != null) endDate = obj.value;
	else {
		var es = document.getElementsByName('endDate');
		if(es != null) for(var i=0; i<es.length; i++) { if(es[i].value != null) endDate = es[i].value; break; }
	}
	var hst = document.URL.replace(/.*:\/\/([^:\/]+).*/i, '$1');
	var str = "<object classid=\"" + GLS.WChart.classid + "\"\n"
			+ "        codebase=\"" + GLS.WChart.codebase + "\"\n"
			+ "        standby=\"" + GLS.WChart.standby + "\"\n"
			+ "        id=\"TWebChart\" width=\"" + chartWidth + "\" height=\"" + chartHeight + "\" style=\"display:none;\">\n"
			+ "	<param name=\"UserID\"      value=\"" + userID + "\">\n"			
			+ "	<param name=\"ServerIP\"    value=\"" + hst + "\">\n"
			+ "	<param name=\"TCPPort\"     value=\"" + GLS.WChart.TCPPort + "\">\n"
			+ "	<param name=\"AUTOPort\"    value=\"" + GLS.WChart.AUTOPort + "\">\n"
			+ "	<param name=\"strJongCode\" value=\"\">\n"
			+ "	<param name=\"strJongName\" value=\"\">\n"
			+ "	<param name=\"MarketType\"  value=\"\">\n"
			+ "	<param name=\"ChartPeriod\" value=\"2\">\n"
			+ "	<param name=\"CycleCount\"  value=\"1\">\n"
			+ "	<param name=\"DataCount\"   value=\"150\">\n"
			+ "	<param name=\"StartDate\"   value=\"0\">\n"
			+ "	<param name=\"EndDate\"     value=\"" + endDate + "\">\n"
			+ "	<param name=\"ShowSidebar\" value=\"0\">\n"
			+ "</object>\n";
	document.write(str);
}

