var bc_com_ContractMonths = Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

function bc_com_GetExchangeName(exch) {
	if (exch == 'cbot')
		return 'CBOT: Chicago Board of Trade';
	else if (exch == 'cme')
		return 'CME: Chicago Mercantile Exchange';
	else if (exch == 'comex')
		return 'COMEX: Commodities Exchange';
	else if (exch == 'csce')
		return 'NYBOT / CSCE: Coffee, Sugar, and Cocoa Exchange';
	else if (exch == 'finex')
		return 'NYBOT / FINEX: Financial Exchange';
	else if (exch == 'kcbt')
		return 'KCBT: Kansas City Board of Trade';
	else if (exch == 'mgex')
		return 'MGEX: Minneapolis Grain Exchange';
	else if (exch == 'nyce')
		return 'NYBOT / NYCE: New York Cotton Exchange';
	else if (exch == 'nyfe')
		return 'NYBOT / NYFE: New York Futures Exchange';
	else if (exch == 'nymex')
		return 'NYMEX: New York Mercantile Exchange';
	else if ((exch == 'wpg') || (exch == 'wce'))
		return 'WPG (WCE): Winnipeg Commodities Exchange';
	else
		return exch;
}


function bc_com_GetMonth(m) {
	if (m == 'F')
		return 0;
	else if (m == 'G')
		return 1;
	else if (m == 'H')
		return 2;
	else if (m == 'J')
		return 3;
	else if (m == 'K')
		return 4;
	else if (m == 'M')
		return 5;
	else if (m == 'N')
		return 6;
	else if (m == 'Q')
		return 7;
	else if (m == 'U')
		return 8;
	else if (m == 'V')
		return 9;
	else if (m == 'X')
		return 10;
	else if (m == 'Z')
		return 11;
}

function bc_com_GetOptionYear(s) {
	var thisyear = (new Date()).getYear();
	if (thisyear < 1900)
		thisyear += 1900;

	
	s = s.toUpperCase();
	var c = 'P';
	var diff = s.charCodeAt(0) - c.charCodeAt(0);
	if (diff < 0) {
		c = 'C';
		diff = s.charCodeAt(0) - c.charCodeAt(0);
	}

	return (thisyear + diff);	
}

function bc_com_GetYear(y, m) {
	var d = new Date();
	var yr = d.getYear();
	if (yr < 1900) {
		// Damn Mozilla
		yr += 1900;
	}

	var yr2 = 0;
	y = y * 1;

	if (y < 10) {
		yr2 = Math.floor(yr / 10) * 10;
		yr2 = (yr2 * 1) + (y * 1);
		if (yr > yr2)
			yr2 += 10;
		else if (yr == yr2) {
			var m1 = d.getMonth();
			var m2 = bc_com_GetMonth(m);
			if (m1 > m2)
				yr2 += 10;
		}
	}
	else if (y < 100) {
		yr2 = Math.floor(yr / 100) * 100;
		yr2 = (yr * 1) + (y * 1);
	}
	else 
		return y;

	return yr2;
}

// Symbol Object
function bc_com_SymbolObject(s) {
	this.type = 'unknown';
	this.symbol = '';
	this.commoditycode = '';
	this.month = '';
	this.year = 0;
	this.strike = '';
	this.optiontype = '';
  this.originalsymbol = s.replace(/\s+/g, '');

	var re = /[0-9]/;
	if (re.exec(s) == null) {
		this.type = 'stock';
		this.symbol = s;
		return;
	}

	re = /[0-9]$/;
	if (re.exec(s) == null) {
		// Option
		this.type = 'option';
		var idx = s.length - 1;
		var type = s.charAt(idx--);
		var c = 'P';
		if (type.charCodeAt(0) >= c.charCodeAt(0))
			this.optiontype = 'put';
		else
			this.optiontype = 'call';
		this.year = bc_com_GetOptionYear(type);

		var b = false;
		while (!b) {
			var c = s.charAt(idx--);
			if (isNaN(c)) {
				this.month = c;
				b = true;
			}
			else {
				this.strike = c + this.strike;
			}
		}
		this.commoditycode = s.substring(0, idx + 1);
	}
	else {
		// Future
		this.type = 'future';

		if (s.length < 3) {
			this.symbol = s;
			this.commoditycode = s;
		}

		var b = false;
		var idx = s.length - 1;
		var yr = '';
		while (!b) {
			var c = s.charAt(idx--);
			if (isNaN(c)) {
				this.month = c;
				b = true;
			}
			else {
				yr = c + yr;
			}
		}

		this.commoditycode = s.substring(0, idx + 1);
		this.year = bc_com_GetYear(yr, this.month);
	}
  this.commoditycode = this.commoditycode.replace(/\s+/g, '');
}

