var SymbolSearch = function(elForm) {
	this.localCache = {};
	this.eventManager = new EventManager();
	this.query = "";
	this.issueType = "";
	
	this.CSS_HIDDEN = "";
	//this.CSS_SELECTED = "";
	this.CSS_RESULTS = "";
	
	this.setDestinationURL(this.DESTINATION_URL);
	this.setRequestURL(this.REQUEST_URL);
	this.setFinderURL(this.FINDER_URL);
	
	if (elForm || Element.get(this.DEFAULT_FORM_ID)) {
		// DOM has loaded
		this.setForm(elForm);
	} else {
		this.eventManager.add(window, "load", function() { this.setForm(elForm) }, this);
	}
}

//SymbolSearch.prototype.DEFAULT_FORM_ID = "";
//SymbolSearch.prototype.FORM_INPUT_SELECTOR = "";

SymbolSearch.prototype.REQUEST_URL = "../../common/symbolLookup/getSymbols.asp";
SymbolSearch.prototype.DESTINATION_URL = "";
SymbolSearch.prototype.FUND_DESTINATION_URL = "?symbol=";
SymbolSearch.prototype.FINDER_URL = "../../symbolLookup/symbolLookup/symbolLookup.asp";
SymbolSearch.prototype.KEY_PRESS_WAIT = 0;

//SymbolSearch.prototype.CSS_HIDDEN = "symbolSearchHidden";
SymbolSearch.prototype.CSS_SELECTED = "selected";
//SymbolSearch.prototype.CSS_RESULTS = "symbolSearch";
SymbolSearch.prototype.CSS_ISSUE_NAME = "issueName";
SymbolSearch.prototype.CSS_GROUP_END = "symbolSearchGroupEnd";
SymbolSearch.prototype.CSS_FLAG = "wsod-flag";
SymbolSearch.prototype.CSS_FLAG_COUNTRY = "flag-";
SymbolSearch.prototype.CSS_MORE_LINK = "more";

SymbolSearch.prototype.ATTR_NO_RESULTS = "noresults";

SymbolSearch.prototype.KEY_CODE_UP = 38;
SymbolSearch.prototype.KEY_CODE_DOWN = 40;
SymbolSearch.prototype.KEY_CODE_ESC = 27;
SymbolSearch.prototype.KEY_CODE_ENTER = 13;

SymbolSearch.prototype.invalidChars = /\s/g;
SymbolSearch.prototype.countryPrefix = /^[a-z]{2};/i;

SymbolSearch.prototype.ISSUE_TYPES = {
	ALL: "",
	EQ: "EQ",
	MF: "MF",
	ETF:"ETF",
	IN: 'IN',
	CF: 'CF',
	PR: 'PR',
	ETN: 'ETN'
};

SymbolSearch.prototype.STOCK_ISSUE_TYPES = {
	ALL: "",
	EQ: "EQ",
	IN: 'IN',
	CF: 'CF',
	PR: 'PR',
	ETN: 'ETN'
};

SymbolSearch.prototype.FUND_ISSUE_TYPES = {
	MF: "MF",
	ETF:"ETF"
};

SymbolSearch.prototype.setCSSSelector = function(hiddenCSS, visibleCSS) {
	this.CSS_HIDDEN = visibleCSS;
	this.CSS_RESULTS = hiddenCSS;
};

SymbolSearch.prototype.setFormElements = function(elForm, elInput) {
	this.DEFAULT_FORM_ID = elForm;
	this.FORM_INPUT_SELECTOR = elInput;
};

SymbolSearch.prototype.setForm = function(elForm) {
	this.clearInputEventHandlers();
	
	this.elForm = elForm || Element.get(this.DEFAULT_FORM_ID);
	this.elInput = Element.get(this.FORM_INPUT_SELECTOR);
	
	this.addInputEventHandlers();
};

SymbolSearch.prototype.addInputEventHandlers = function() {
	this.eventManager.add(this.elInput, "keyup", this.search, this, null, this.KEY_PRESS_WAIT);
	this.eventManager.add(this.elInput, "click", function(e) {
		e.cancel();
	});
};

SymbolSearch.prototype.clearInputEventHandlers = function() {
	if (this.elInput) {
		this.eventManager.remove(this.elInput);
	}
};

SymbolSearch.prototype.addResultEvents = function(noResults) {
	this.selectedRow = -1;
	
	if (!noResults) {
		this.getOnNavResults().addElement(this.elInput);
		this.getOnHoverResults().addElement(this.elResultRows);
		this.getOnClickResults().addElement(this.elResultRows);
	}
	
	this.getOnHideResults().addElement(document);
	this.getOnSubmitResults().addElement(this.elInput);
};

SymbolSearch.prototype.removeResultEvents = function() {
	this.getOnNavResults().removeAllElements();
	this.getOnHoverResults().removeAllElements();
	this.getOnClickResults().removeAllElements();
	this.getOnHideResults().removeAllElements();
	this.getOnSubmitResults().removeAllElements();
};

SymbolSearch.prototype.getOnNavResults = function() {
	var eventSource = this.eventManager.add(null, "keydown", this.navigate, this);
	
	this.getOnNavResults = function() {
		return eventSource;
	};
	
	return this.getOnNavResults();
};

SymbolSearch.prototype.getOnHoverResults = function() {
	var eventHandler = function(e, el) {
		this.highlightRow(el.rowIndex);
	};
	
	var eventSource = this.eventManager.add(null, "mouseover", eventHandler, this);
	
	this.getOnMouseOverResults = function() {
		return eventSource;
	};
	
	return this.getOnMouseOverResults();
};

SymbolSearch.prototype.getOnHideResults = function() {
	var eventSource = this.eventManager.add(null, "click", this.clearResults, this);

	this.getOnHideResults = function() {
		return eventSource;
	};
	
	return this.getOnHideResults();
};

SymbolSearch.prototype.getOnClickResults = function() {
	var eventSource = this.eventManager.add(null, "click", this.selectResult, this);
	
	this.getOnClickResults = function() {
		return eventSource;
	};
	
	return this.getOnClickResults();
};

SymbolSearch.prototype.getOnSubmitResults = function() {
	var eventSource = this.eventManager.add(null, "keypress", this.selectResult, this);
	
	this.getOnSubmitResults = function() {
		return eventSource;
	};
	
	return this.getOnSubmitResults();
};

SymbolSearch.prototype.setRequestor = function(requestor) {
	this.requestor = requestor;
};

SymbolSearch.prototype.setDestinationURL = function(URL) {
	this.destinationURL = URL;
};

SymbolSearch.prototype.setRequestURL = function(URL) {
	this.requestURL = URL;
};

SymbolSearch.prototype.setFinderURL = function(URL) {
	this.finderURL = URL;
};

SymbolSearch.prototype.highlightText = function(c) {
	var sTermArray = String(this.query).replace(this.countryPrefix, "").split(" ");
	var sTermLen = sTermArray.length;
	
	if (c.n.indexOf(" ")) {
		var coName = c.n.split(" ");
		var coNameLen = coName.length;
	} else {
		var coName = c.n;
		var coNameLen = 1;
	}
	
	if(sTermLen == 1){
		var replacement = '<b>$1</b>';
		var re = new RegExp("\\b(" + sTermArray + ")", "gi");
		c.d = String(c.s).replace(re, replacement);
		c.n = String(c.n).replace(re, replacement);
	} else {
		for (var i = 0; i < coNameLen; i++) {
			var coNamePart = coName[i];
			if(!coNamePart.length){
				continue;
			}
			for(var z = 0; z < sTermLen; z++){
				var term = sTermArray[z].replace(/\s/g, "");
				var termLen = term.length;
				if (termLen && term.toLowerCase() == coNamePart.toLowerCase().substring(0, termLen)) {
					coName[i] = '<b>' + coNamePart.substring(0, termLen) + '</b>' + coNamePart.substring(termLen);
					z = sTermLen;
				}
			}
			
			c.n = coName.join(' ');
		}
	}
	
	return c;
};

SymbolSearch.prototype.setIssueType = function(type) {
	this.issueType = type;
};

SymbolSearch.prototype.search = function(e, el){
	if (el.value == this.query) {
		return;
	} else if (!String(el.value).replace(this.invalidChars, "")) {
		this.query = el.value;
		this.clearResults();
	} else {
		this.abortActiveRequests();
		this.query = el.value;
		if (this.localCache[this.query + this.issueType]) {
			this.drawResults(this.localCache[this.query + this.issueType]);
		} else {
			this.requestor.load({
				url: this.requestURL,
				contentType: "text/javascript",
				data: {
					q: this.query,
					issueType: this.issueType,
					callback: "handleResults",
					context: "this"
				},
				context: this
			});
		}
	}
};

SymbolSearch.prototype.handleResults = function(query, results, showMoreLink) {
	var cachedNodes = [];
	if(Element.parseSelector('div.'+this.CSS_RESULTS, '', 'first')){
		Element.parseSelector('div.'+this.CSS_RESULTS, '', 'first').className = 'symbolSearch';
	} else if(Element.parseSelector('div.symbolSearchHidden2', '', 'first')){
		Element.parseSelector('div.symbolSearchHidden2', '', 'first').className = 'symbolSearch2';
	} else if(Element.parseSelector('div.symbolSearchHidden3', '', 'first')){
		Element.parseSelector('div.symbolSearchHidden3', '', 'first').className = 'symbolSearch3';
	}
	var elTable = Element.create("table");
	
	var resultType = ['Symbol matches', 'Name matches', 'Symbol starts with'];
	
	for (var i=0,group; i<results.length; i++) {
		group = results[i];
		if (group.length) {
			var elTbody = Element.create("tbody");
			var tr = Element.create('tr', {'class':'header-cell'}, null, elTbody);
			for (var j=0,c; j<group.length; j++) {
				c = this.highlightText(group[j]);
				if(j == 0){
					if(c.lt == 'Symbol.5.0'){
						Element.create('td', {colspan:3}, resultType[0], tr);
					}else if(c.lt == 'IssueNameFast.5.0'){
						Element.create('td', {colspan:3}, resultType[1], tr);
					}else{
						Element.create('td', {colspan:3}, resultType[2], tr);
					}
				}
				
				var elRow = Element.create("tr", { symbol: c.s, isfund: c.f }, [
					Element.create("td", { "class": this.CSS_ISSUE_NAME }, group[j].n),
					//Element.create("td", {'class':'light'}, c.c), //--no exchange info
					Element.create("td", {'class':''}, "&nbsp;"),
					Element.create("td", null, c.d)
				], elTbody);
				
				if (group.length-1 == j && (results.length -1 != i || showMoreLink)) {
					Element.addClass(elRow, this.CSS_GROUP_END);
				}

			}
			Element.addChild(elTable, elTbody);
		}
	}
	/*
	if (showMoreLink) {
		Element.create("tbody", null, [
			Element.create("tr", { "class": this.CSS_MORE_LINK }, [
				Element.create("td", { "colspan":3 }, [
					Element.create("a", { href: this.buildMoreLink() }, "Additional matches for " + query + " &gt;")
				])
			])
		], elTable);	
	}
	*/
	if (!elTable.childNodes.length) {
		Element.create("tbody", null, [
			Element.create("tr", { "class": this.CSS_GROUP_END }, [
				Element.create("td", null, "No securities were found for \"<b>" + query + "</b>\".")
			])
		], elTable);
		elTable.setAttribute(this.ATTR_NO_RESULTS, "true");
	}
	
	cachedNodes.push(elTable);
	this.localCache[query + this.issueType] = cachedNodes;
	
	this.drawResults(cachedNodes);
	
};

SymbolSearch.prototype.createResultsContainer = function() {
	this.elResults = Element.create("div", { "class": this.CSS_HIDDEN }, null, document.body);
	Element.addClass(this.elResults, this.CSS_RESULTS);
};


SymbolSearch.prototype.drawResults = function(cachedNodes) {
	this.clearResults();
	
	if (!this.elResults) {
		this.createResultsContainer(); 
	}

	var pos = Element.getXY(this.elInput);
	var size = Element.getSize(this.elInput);
	
	Element.setXY(this.elResults, pos.x, pos.y + size.height);
	
	Element.removeClass(this.elResults, this.CSS_HIDDEN);
	for (var i=0; i<cachedNodes.length; i++) {
		Element.addChild(this.elResults, cachedNodes[i]);
	}
	this.elResultRows = Element.parseSelector("tr", this.elResults);
	this.elMoreLink = Element.parseSelector("tr." + this.CSS_MORE_LINK + " a", this.elResults, "first");
	
	this.addResultEvents(cachedNodes[0].getAttribute(this.ATTR_NO_RESULTS));
	
	WCH.Apply(this.elResults, null, true);
};

SymbolSearch.prototype.clearResults = function() {
	if (!this.elResults) {
		this.createResultsContainer();
	}
	
	if (this.elResultRows && this.elResultRows.length) {
		Element.removeClass(this.elResultRows, this.CSS_SELECTED);
	}
	
	Element.addClass(this.elResults, this.CSS_HIDDEN);
	Element.removeChildNodes(this.elResults);
	this.removeResultEvents();
	WCH.Discard(this.elResults);
};

SymbolSearch.prototype.buildMoreLink = function() {
	var URL = this.finderURL + "?textIn=" + this.query + '&issueType=All';
	return URL;
};

SymbolSearch.prototype.navigate = function(e) {
	switch (e.nativeEvent.keyCode) {
		case this.KEY_CODE_DOWN :
			e.cancel();
			var rowIdx = Math.min(this.selectedRow + 1, this.elResultRows.length-1);
			
			if (this.elResultRows[rowIdx]) {
				if (this.elResultRows[rowIdx].className == 'header-cell') {
					rowIdx += 1;
				}
			}
			
			this.highlightRow(rowIdx);
			break;
		
		case this.KEY_CODE_UP :
			e.cancel();
			var rowIdx = Math.max(this.selectedRow - 1, -1);
			
			if (this.elResultRows[rowIdx]) {
				if (this.elResultRows[rowIdx].className == 'header-cell') {
					rowIdx -= 1;
				}
			}
			
			this.highlightRow(rowIdx);
			break;
		
		case this.KEY_CODE_ESC :
			e.cancel();
			this.clearResults();
			break;
	}

};

SymbolSearch.prototype.highlightRow = function(rowIdx) {

	if (this.selectedRow != rowIdx) {
		if (this.elResultRows[this.selectedRow]) {
			Element.removeClass(this.elResultRows[this.selectedRow], this.CSS_SELECTED);		
		}
		
		if (this.elResultRows[rowIdx]) {
			Element.addClass(this.elResultRows[rowIdx], this.CSS_SELECTED)
		}
		
		this.selectedRow = rowIdx;
	}

};

SymbolSearch.prototype.selectResult = function(e) {
	if ("click" == e.nativeEvent.type || this.KEY_CODE_ENTER == e.nativeEvent.keyCode) {
		e.cancel();

		if (this.selectedRow == -1) {
			// only one result, go to it
			if (this.elResultRows && 1 == this.elResultRows.length) {
				var passSymbol = this.elResultRows[0].getAttribute("symbol");
				if ("-32768" != this.elResultRows[0].getAttribute("symbol")) {
					passSymbol = this.elResultRows[0].getAttribute("symbol");
				}
				
				this.go(passSymbol /*, (this.elResultRows[0].getAttribute("isfund") == "1")*/);
			} else {
				//multiple results, just submit the form?
				this.go(this.query);
			}
		} else {
			if (Element.hasClass(this.elResultRows[this.selectedRow], this.CSS_MORE_LINK)) {
				window.location = this.elMoreLink.href;
			} else {
				var passSymbol = this.elResultRows[this.selectedRow].getAttribute("symbol");
				if ("-32768" != this.elResultRows[this.selectedRow].getAttribute("symbol")) {
					passSymbol = this.elResultRows[this.selectedRow].getAttribute("symbol");
				}
				this.go(passSymbol /*, (this.elResultRows[this.selectedRow].getAttribute("isfund") == "1")*/);
			}
		}
	}
};

SymbolSearch.prototype.go = function(symbol, isFund){
	if (symbol) {
		this.query = symbol;
		this.elInput.value = symbol;
	}
	
	this.clearResults();
	
};

SymbolSearch.prototype.abortActiveRequests = function() {
	this.requestor.abortRequests();
};

//WCH
var WCH_Constructor=function(){if(!(document.all&&document.getElementById&&!window.opera&&navigator.userAgent.toLowerCase().indexOf("mac")==-1)){this.Apply=function(){};this.Discard=function(){};return;}
var _bIE55=false;var _bIE6=false;var _oRule=null;var _bSetup=true;var _oSelf=this;this.Apply=function(vLayer,vContainer,bResize){if(_bSetup)_Setup();if(_bIE55&&(oIframe=_Hider(vLayer,vContainer,bResize))){oIframe.style.visibility="visible";}else if(_oRule!=null){_oRule.style.visibility="hidden";}};this.Discard=function(vLayer,vContainer){if(_bIE55&&(oIframe=_Hider(vLayer,vContainer,false))){oIframe.style.visibility="hidden";}else if(_oRule!=null){_oRule.style.visibility="visible";}};function _Hider(vLayer,vContainer,bResize){var oLayer=_GetObj(vLayer);var oContainer=((oTmp=_GetObj(vContainer))?oTmp:document.getElementsByTagName("body")[0]);if(!oLayer||!oContainer)return;var oIframe=document.getElementById("WCHhider"+oLayer.id);if(!oIframe){var sFilter=(_bIE6)?"filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);":"";var zIndex=oLayer.style.zIndex;if(zIndex=="")zIndex=oLayer.currentStyle.zIndex;zIndex=parseInt(zIndex);if(isNaN(zIndex))return null;if(zIndex<2)return null;zIndex--;var sHiderID="WCHhider"+oLayer.id;oContainer.insertAdjacentHTML("afterBegin",'<iframe class="WCHiframe" src="javascript:false;" id="'+sHiderID+'" scroll="no" frameborder="0" style="position:absolute;visibility:hidden;'+sFilter+'border:0;top:0;left;0;width:0;height:0;background-color:#ccc;z-index:'+zIndex+';"></iframe>');oIframe=document.getElementById(sHiderID);_SetPos(oIframe,oLayer);}else if(bResize){_SetPos(oIframe,oLayer);}
return oIframe;};function _SetPos(oIframe,oLayer){oIframe.style.width=oLayer.offsetWidth+"px";oIframe.style.height=oLayer.offsetHeight+"px";oIframe.style.left=oLayer.offsetLeft+"px";oIframe.style.top=oLayer.offsetTop+"px";};function _GetObj(vObj){var oObj=null;switch(typeof(vObj)){case"object":oObj=vObj;break;case"string":oObj=document.getElementById(vObj);break;}
return oObj;};function _Setup(){_bIE55=(typeof(document.body.contentEditable)!="undefined");_bIE6=(typeof(document.compatMode)!="undefined");if(!_bIE55){if(document.styleSheets.length==0)
document.createStyleSheet();var oSheet=document.styleSheets[0];oSheet.addRule(".WCHhider","visibility:visible");_oRule=oSheet.rules(oSheet.rules.length-1);}
_bSetup=false;};};var WCH=new WCH_Constructor();
