//  it would be nice if we don't have to use this!!
var URL = "/Search";
// in case of preview please set this to empty
var EDITORIAL_SITE_CONTEXT = "";
// this variable is used to verify the dropdown link is tools
var SEARCH_LINK_TOOLS = "tools.afr.com";

function submitForm(form, url){
	
	var searchTerms = document.getElementById("search_input").value;
	
	if(searchTerms != "" && searchTerms != "Search news, quotes, announcements and people")
	{
		if(form.current_search_selection.value == searchTerms)
		{			
			if(form.url_action.value)
			{
				form.action = form.url_action.value;
			}
			else
				form.action = url;
				
			form.method = "POST";
			form.submit();
		}
		else
		{
			//--------------------
			document.getElementById("url_action").value = "";
			document.getElementById("current_search_selection").value = "";
			document.getElementById("mdsearch").action = url;
			document.getElementById("mdsearch").method = "POST";
			document.getElementById("mdsearch").submit();
			//--------------------
		}
	}
	else
		return false;
}

var dataResponse;
/**
* posts request to the url and sets value to 
* dataResponse
**/
function loadXMLDoc(url)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    	// alert('here? ' +xmlhttp.responseText);
    	dataResponse = xmlhttp.responseText;
    	return;
    }
  }
xmlhttp.open("GET", url ,true);
xmlhttp.send();
}

function handleEnterKeyUrl(event, url){
	URL = url;
	return handleEnterKey(event);
}

function handleEnterKey(e)
{
	var key;
	if(window.event)
		key = window.event.keyCode;//IE
	else
		key = e.which;//firefox
	if(key == 13)
	{
		var searchTerms = document.getElementById("search_input").value;
		
		if(searchTerms.length == 3){
			var validstockurl = EDITORIAL_SITE_CONTEXT + "/eom/SysConfig/WebPortal/afr/_files/common/check_valid_stock.jsp?q=";
			validstockurl  += searchTerms;
			
			//alert(validstockurl)
			// populate dataResponse variable..
			jQuery.get(validstockurl, function(data){
				// alert(data)
				// console.log(data);
				if(data == "true\n")
				{
					document.getElementById("asx_code").value = searchTerms;	
					submitForm(document.getElementById("mdsearch"), URL);
				}else if(data == "false\n"){
					document.getElementById("asx_code").value = "";
					// console.log(document.getElementById("search_input").value); 
					submitForm(document.getElementById("mdsearch"), URL);
				}
			});
			
			return false;
		}else{
			// submit form as-is..
			submitForm(document.getElementById("mdsearch"), URL);
		}

		return false;
	}
	else
		return true;
}

jQuery.noConflict();
jQuery(document).ready(function(){
	function formatAlertItem(row) {	
			return "<span class='companyCode'>" + row[0] + "</span>" + row[1];
	}
	function selectAlertItem(li){
		
		var comp = jQuery.trim(document.getElementById("company").value);
		
		if (comp && comp.length > 0)
		{
			var arr = comp.split(',');
			//alert(comp)
			comp = comp.replace(arr[arr.length-1], "")
			comp = comp.replace(li.selectValue + ',', "")
			//comp = comp.substring(0,comp.length - li.selectValue.length);
			//comp += li.selectValue + ',';
		}
		comp += li.selectValue + ',';
		document.getElementById("company").value = comp;
	}
	
	function formatItem(row) {
		if(row[0].length == 3)
			return "<span class='companyCode'>" + row[0] + "</span>" + row[1];
		else
			return "<span class='companyName'>" + row[0] + "</span>";
	}

	function selectItem(li){
		document.getElementById("asx_code").value = li.extra[0];
		
		// this is appending ed-context
		var dropurl = li.extra[1];
		
		if(dropurl.indexOf(SEARCH_LINK_TOOLS) < 0){
			dropurl = EDITORIAL_SITE_CONTEXT + li.extra[1];
		}
		document.getElementById("url_action").value = dropurl;
		document.getElementById("current_search_selection").value = li.selectValue;
		document.getElementById("search_input").value = li.selectValue;
		
		// this is appending url action
		submitForm(document.getElementById("mdsearch"), URL);
		document.getElementById("mdsearch").submit();
	}	
	
	jQuery("#search_input").autocomplete(EDITORIAL_SITE_CONTEXT + "/eom/SysConfig/WebPortal/afr/_files/common/auto_complete.jsp",
		{
		delay:500,
		minChars:2,
		matchSubset:1,
		matchContains:1,
		cacheLength:1,
		formatItem:formatItem,
		onItemSelect:selectItem
		}
	);

	jQuery("#company").autocomplete(EDITORIAL_SITE_CONTEXT + "/eom/SysConfig/WebPortal/afr/_files/common/auto_complete.jsp",	
		{
		delay:500,
		minChars:2,
		matchSubset:1,
		matchContains:1,
		cacheLength:1,
		formatItem:formatAlertItem,
		onItemSelect:selectAlertItem,
		displayMode:2,
		maxItemsToShow:10
		}
	);
});
