// Main JavaScript Include File
// Copyright (C) 2005 by Barchart.com, Inc. All Rights Reserved.
// This file may only be used by an authorized firm, on an authorized website.
// Any unauthorized use is expressly prohibited.

if (document.forms[0]) { document.forms[0].onsubmit = function() { return false; } }

function bcfn_CheckDomain() {
	var d = bcfn_GetDomain();
	if ((d == 'barchart') || (d == 'admis') || (d == 'archerfinancials') || (d == 'ricedairy'))
		return true;
	else
		return false;
}

function bcfn_GetDomain() {
	var s = document.domain;
	s = s.toLowerCase();
	if (s.substring(0, 7) == 'http://') {
		s = s.substring(7);
	}

	var ary1 = s.split('/');
	var ary2 = ary1[0].split('.');

	var d = '';

	if ((ary2[ary2.length - 1] == 'com') ||
			(ary2[ary2.length - 1] == 'org') ||
			(ary2[ary2.length - 1] == 'net') ||
			(ary2[ary2.length - 1] == 'edu') ||
			(ary2[ary2.length - 1] == 'gov')) {
		if (ary2.length > 1)
			d = ary2[ary2.length - 2];
	}
	else {
		if (ary2.length > 2)
			d = ary2[ary2.length - 3];
	}

	return d;
}


function bcfn_GetColor(s) {
	var c = s.substring(0, 1);
	if (c == '+')
		return '#008800';
	else if (c == '-')
		return '#ee0000';
	else
		return '#0000ff';
}

 function bcfn_GetImageColor(s) {
	var c = s.substring(0, 1);
	if (c == '+')
		return bc_ImageURL + '/up.gif';
	else if (c == '-')
		return bc_ImageURL + '/dn.gif';
	else
		return bc_ImageURL + '/noch.gif';
 }

function bcfn_GetChart(sym2, sp, nasdaq, dow, overlay){
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	if(sp.checked){ sp = "true"; }else{ sp = ""; }
	if(nasdaq.checked){ nasdaq = "true"; }else{ nasdaq = ""; }
	if(dow.checked){ dow = "true"; }else{ dow = ""; }
	location.href = sPage + "?page=ccharts&sym=" + sym2 + "&overlay=" + overlay.value + "&sp=" + sp + "&nasdaq=" + nasdaq + "&dow=" + dow;
}

function bcfn_GetCustomQuote(fullquery){
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	var symlist = '';
	var count = 1;
	for(var i = 0; i < fullquery.length; i++) { 
		if(fullquery[i].value != ""){
			symlist = symlist + '&sym' + i + "=" + fullquery[i].value; 
			count = count + 1; 
		}
	}
	location.href = sPage + "?page=cquotes&what=change&action=update" + symlist + "&maxcnt=" + count;
}

function bcfn_GetChart(sym2, sp, nasdaq, dow, overlay){
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	if(sp.checked){ sp = "true"; }else{ sp = ""; }
	if(nasdaq.checked){ nasdaq = "true"; }else{ nasdaq = ""; }
	if(dow.checked){ dow = "true"; }else{ dow = ""; }
	location.href = sPage + "?page=ccharts&sym=" + sym2 + "&overlay=" + overlay.value + "&sp=" + sp + "&nasdaq=" + nasdaq + "&dow=" + dow;
}

function bcfn_GetQuote(page, sym){
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	var sym = document.getElementById('bcSearchSym').value;

	if (sym) {
		if (document.getElementById('bcSearchChart').checked)
			location.href = sPage + "?page=chart&sym=" + sym;
		else if (document.getElementById('bcSearchOption').checked)
			location.href = sPage + "?page=optqte&sym=" + sym;
		else
			location.href = sPage + "?page=quote&sym=" + sym;
	}
}

function bcfn_getSpread(f) {
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	var loc = sPage + "?page=cspreads&spr1="+f.spr1.value+"&spr2="+f.spr2.value+"&spr3="+f.spr3.value+"&siz1="+f.siz1.value+"&siz2="+f.siz2.value+"&siz3="+f.siz3.value+"&size="+f.size[f.size.selectedIndex].value+"&den="+f.den[f.den.selectedIndex].value+"&jav="+f.jav[f.jav.selectedIndex].value+"&expm="+f.expm[f.expm.selectedIndex].value+"&styp="+f.styp[f.styp.selectedIndex].value;
	if (f.late.checked)
		loc += "&late="+f.late.value;
	if (f.grid.checked)
		loc += "&late="+f.grid.value;
	loc += "&date="+f.date.value+"&data="+f.data[f.data.selectedIndex].value+"&ch1="+f.ch1[f.ch1.selectedIndex].value+"&arga="+f.arga.value+"&argb="+f.argb.value+"&argc="+f.argc.value+"&ov1="+f.ov1[f.ov1.selectedIndex].value+"&argd="+f.argd.value+"&arge="+f.arge.value+"&argf="+f.argf.value+"&code="+f.code.value+"&org="+f.org.value+"&crea="+f.crea.value+"&sprd="+f.sprd.value+"&button="+f.button.value;

	if (validate(f)) {
		location.href = loc;
	}
}

function bcfn_OpenFlipbook(u, p) {
	var url = 'http://quotes-cust.barchart.com/pl/adm/flipbook.asp?list=' + p;
	if(u == 1) { var w='635'; var h='449'; } 
	else if (u == 0) { var w='635'; var h='540'; } 
	window.open(url,'_nova','width='+w+',height='+h+',left=25,top=10');
}


function bcfn_ParseQuery(s) {
	var ary = new Array();
	var pairs = s.split('&');

	for (var i = 0; i < pairs.length; i++) {
		var tmp = pairs[i].split('=');
		ary[tmp[0]] = tmp[1];
	}

	return ary;
}


function bcfn_PrintDisclaimer() {
	document.write('<hr size="1">');//document.write('<CENTER><HR size=1>');
	document.write('<table width="100%" cellpadding="0" cellspacing="0"><tr><td align="center" class="bcDisclaimer">');
	document.write('Website provided by Barchart.com. <a href="?mid=4547&page=agreement">User Agreement Applies</a>.<br/>');
	document.write('Intraday data provided by <a target="new" href="http://www.ddfplus.com">ddfplus</a> and subject to ');
	document.write('<a href="?mid=4547&page=ddfplus">terms of use</a>.<br/>');
	document.write('<p>End of Day and historical data provided by <a target="new" href="http://www.crbtrader.com">CRB - Commodity Research Bureau</a>.<br/>');
	document.write('Data is exchange delayed. 10 minutes for CME, CBOT, KCBT, WCE, and MGEX. 30 minutes for NYBOT and NYMEX.</p>');
	document.write('<p>Data and information is provided for informational purposes only, and is not intended for trading purposes.</p>');
	document.write('</td></tr></table>');
	//document.write('</CENTER>');
}

function keyPressed(f,e) {
	var key;
	if(window.event)
		key = window.event.keyCode;     //IE
	else
		key = e.which;     //firefox

	if (key == 13) {
		submitSearch(f);
	}
}

function submitSearch(f) {
	var sym = f.sym.value;
	var page = 'quote';

	for (var i = 0; i < f.page.length; i++) {
		if (f.page[i].checked)
			page = f.page[i].value; 
	}

	location.href = "?page="+page+"&sym="+sym;
}

var bc_bShowDisclaimer = true;
var bc_QueryString = (new String(document.location.search)).substring(1);
var bc_QueryForm = bcfn_ParseQuery(bc_QueryString);

// Core URL's for some of the support files
var bc_ImageURL = 'http://images.barchart.com/pl';


if (!bc_QueryForm['sym'])
	bc_QueryForm['sym'] = '';


// Start Universal Header

document.write('<table width="100%" cellpadding="3" cellspacing="4" border="0">');
document.write('<tr><td class="bcQuotesCharts">Quotes &amp; Charts</td></tr>');
document.write('<tr><td class="bcTD"><a href="?mid=4547">Futures Main</a> | <a href="?mid=4547&page=cquotes">Custom Quotes</a> | <a href="?mid=4547&page=ccharts">Custom Charts</a> | <a href="?mid=4547&page=cspreads">Custom Spreads</a> | <a href="?mid=4547&page=ap_news">Top Financial News</a></td>');
document.write('<td align="right" valign="top">');
document.write('<table cellpadding="0" cellspacing="0" border="0">');
document.write('<tr>');
//document.write('<form>');
document.write('<td class="bcTD">');
document.write('<a href="?mid=4547&page=lookup">Enter Symbol:</a> &nbsp;');
document.write('<input id="bcSearchSym" type="text" name="sym" size="5" maxlength="10" value="' + bc_QueryForm['sym'] + '" class="bcInput" onkeypress="keyPressed(this.form,event)">');
document.write('<input type="button" value="Get" class="bcSubmit" onclick="bcfn_GetQuote()">');
document.write('</td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td align="right" class="bcTD">');

if (bc_QueryForm['page'] == 'optqte') {
	document.write('<input type="radio" id="bcSearchQuote" name="page" value="quote">Quote');
	document.write('<input type="radio" id="bcSearchChart" name="page" value="chart">Chart');
	document.write('<input type="radio" id="bcSearchOption" name="page" value="optqte" checked="yes">Option');
}
else if (bc_QueryForm['page'] == 'chart') {
	document.write('<input type="radio" id="bcSearchQuote" name="page" value="quote">Quote');
	document.write('<input type="radio" id="bcSearchChart" name="page" value="chart" checked="yes">Chart');
	document.write('<input type="radio" id="bcSearchOption" name="page" value="optqte">Option');
}
else {
	document.write('<input type="radio" id="bcSearchQuote" name="page" value="quote" checked="yes">Quote');
	document.write('<input type="radio" id="bcSearchChart" name="page" value="chart">Chart');
	document.write('<input type="radio" id="bcSearchOption" name="page" value="optqte">Option');
}

document.write('</td>');
//document.write('</form>');
document.write('</tr>');
document.write('</table>');
document.write('</td>');
document.write('</tr>');
document.write('</table>');
document.write('<hr size="1" color="#295294" width="100%" align="left" />');



if (bcfn_CheckDomain()) {
	document.write('<scr' + 'ipt type="text/javascript" src="');

	if (bc_QueryForm['page'] == 'agreement') {
		document.write('http://content.barchart.com/shared/agreement.php');
	}
	else if (bc_QueryForm['page'] == 'ccharts') {
		document.write('http://sites2.barchart.com/pl/adm/ccharts.htx?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'chart') {
		document.write('http://sites2.barchart.com/pl/adm/ccharts.htx?' + bc_QueryString);
		bc_bShowDisclaimer = false;
	}
	else if (bc_QueryForm['page'] == 'cquotes') {
		document.write('http://content.barchart.com/shared/stream_pl/cquotes.php?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'cspreads') {
		document.write('http://customer1.barchart.com/cgi-bin/mri/admspread.htx?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'custom') {
		document.write('http://sites2.barchart.com/pl/adm/support/custom.htx?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'ddfplus') {
		document.write('http://sites2.barchart.com/pl/adm/ddfplus.js');
	}
	else if (bc_QueryForm['page'] == 'cbot') {
		document.write('http://sites2.barchart.com/pl/adm/cbot.htx');
	}
	else if (bc_QueryForm['page'] == 'futs101') {
		document.write('http://sites2.barchart.com/pl/adm/support/futs101.htx?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'futspread') {
		document.write('http://sites2.barchart.com/pl/adm/futspread.htx?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'glossary') {
		document.write('http://sites2.barchart.com/pl/adm/support/glossary.htx?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'heatmap') {
		document.write('http://charts4.barchart.com/pl/adm/heatmap/static/index.phpx?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'ap_news') {
	//	document.write('http://content.barchart.com/adm/ap_news.js');
	}
	else if (bc_QueryForm['page'] == 'read_news') {
		document.write('http://content.barchart.com/adm/read_news.php?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'learning') {
		document.write('http://sites2.barchart.com/pl/adm/support/learning.htx?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'lookup') {
		document.write('http://sites2.barchart.com/pl/adm/lookup.htx?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'optqte') {
		document.write('http://content.barchart.com/shared/stream_pl/optqte.php?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'opts101') {
		document.write('http://sites2.barchart.com/pl/adm/support/opts101.htx?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'portfolio') {
		document.write('http://sites2.barchart.com/pl/adm/portfolio.htx?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'profile') {
		document.write('http://sites2.barchart.com/pl/adm/profile.htx?sym=' + bc_QueryForm['sym']);
	}
	else if (bc_QueryForm['page'] == 'quote') {
		document.write('http://content.barchart.com/shared/stream_pl/quote.php?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'quote2') {
		document.write('http://content.barchart.com/shared/stream_pl/extquote.php?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'techrept') {
		document.write('http://quotes-cust.barchart.com/pl/common/techrept.htx?' + bc_QueryString);
	}
	else if (bc_QueryForm['page'] == 'texpert') {
		document.write('http://quotes-cust.barchart.com/pl/common/texpert.htx?' + bc_QueryString);
	}
	else {
		document.write('http://content.barchart.com/shared/stream_pl/default.php?' + bc_QueryString);
	}
	document.write('"></scr' + 'ipt>\n');
	
	if (bc_QueryForm['page'] == 'ap_news') {
		document.write('<script language="javascript" type="text/javascript"  src="http://news2.barchart.com/js/NewsHdlers.js"></script>');
		document.write('<script language="javascript" type="text/javascript"  src="http://news2.barchart.com/js/MultiNewsDisplay.js"></script>');
		document.write('<script language="javascript" type="text/javascript"  src="http://news2.barchart.com/js/Util.js"></script>');
		document.write('<script language="JavaScript" SRC="http://content.barchart.com/adm/ap_news_main.js"></script>');
	}

}
else {
	document.write('!!! Unauthorized Access !!!');
}

if (bc_bShowDisclaimer) {
	document.write('<scr' + 'ipt type="text/javascript" src="http://content.barchart.com/shared/disclaimer.php?logo=true"></scr' + 'ipt>\n');
}
