/*
Go to web page based on the action parameter and for the 
given form object.
*/
function gotoPage( action, form )
{
	form.action=action;
	form.submit();
}

function launch(newURL, newName, newFeatures, orgName) {
  var remote = open(newURL, newName, newFeatures);
  self.name = "main";
}

function launchRemote(link) {
  myRemote = launch(link, "", "height=500,width=500,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0", "");
}



var request = null;

function createRequestObject()
{
	try
	{
		request = new XMLHttpRequest();
	
	}
	catch ( tryMicrosoft )
	{
		try
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		
		}
		catch ( otherMicrosoft )
		{
			try
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch ( failed )
			{
				request = null;
			}
		}
	}
	
	if ( request == null )
	{
	}
	else
	{
	  return request;
	}

}

/*
	sendRequest sends the request to the backend server for processing.
	The action parameter should be the application the request should go to.
	formParameters should be the formatted form variables to be sent to the server.
	stateChangeMethod is the method to be called when the request is complete and 
	and a response has been received. The form for this viriable should be method name only (ie updatePage )
	with no parenthesis. stateChangeErrorMethod is the method name that should be called
	when the server responds with an error, this being a status <>200.
	asynchronous should be true or false, dictating 
	if the browser should wait for a response or not.
*/
function sendRequest( action, formParameters, stateChangeMethod, stateChangeErrorMethod, asynchronous )
{
	request = createRequestObject();

	if ( request != null )
	{
		request.open("POST", action , asynchronous );
		request.onreadystatechange = function(){
										processRequest( request, stateChangeMethod, stateChangeErrorMethod );
									}
									
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Connection", "close");
		try
		{
			request.send( formParameters );
		}
		catch( Exception )
		{
			eval( stateChangeErrorMethod );
		}
		
		if ( ! asynchronous )
		{
			processRequest( request, stateChangeMethod, stateChangeErrorMethod );
		}
	}	
}

function processRequest( request, stateChangeMethod, stateChangeErrorMethod )
{
	if ( request.readyState == 4 )
	{
		if ( request.status == 200 )
		{
			if ( stateChangeMethod != '' )
			{
				eval( stateChangeMethod + "()" );
			}
		}
		else
		{
			if ( stateChangeErrorMethod != '' )
			{
				eval( stateChangeErrorMethod + "()" );
			}
		}
	}

}

function formatFormParametersToURI( form )
{
	var formStr = "";
	for ( var i=0; i<form.elements.length; i++ )
	{
		if ( i != 0 )
		{
			formStr += '&';
		}
		formStr += form.elements[i].name + '=' + escape( form.elements[i].value );
	}
	
	return formStr;
}

//Search section
function toggleSearchbox( searchType )
{
	document.getElementById( "companyList" ).style.position='absolute';
	document.getElementById( "companyList" ).style.visibility='hidden';
	document.getElementById( "symbolNameInput" ).style.position='absolute';
	document.getElementById( "symbolNameInput" ).style.visibility='hidden';
	
	document.getElementById( "symbolNameInput" ).style.position='absolute';
	document.getElementById( "symbolNameInput" ).style.visibility='hidden';
	
	try
	{
		//not used on home page
		document.getElementById( "searchLink" ).style.visibility='hidden';
		document.getElementById( "searchLink" ).style.position='absolute';

		document.getElementById( "companyListLink" ).style.visibility='hidden';
		document.getElementById( "companyListLink" ).style.position='absolute';

	}
	catch( Exception )
	{
	}	
	
	if ( searchType == "arsearch" || searchType == "contentSearch" )
	{
		if ( searchType == "arsearch" )
		{
			document.getElementById( "symbolNameInput" ).style.position='relative';
			document.getElementById( "symbolNameInput" ).style.visibility='visible';
			
			try
			{
				document.getElementById( "companyListLink" ).style.visibility='visible';
				document.getElementById( "companyListLink" ).style.position='relative';
			}
			catch( Exception )
			{
			}
		}
		else
		{		

			try
			{
				document.getElementById( "searchLink" ).style.visibility='visible';
				document.getElementById( "searchLink" ).style.position='relative';
				document.getElementById( "companyListLink" ).style.visibility='visible';
				document.getElementById( "companyListLink" ).style.position='relative';
			}
			catch( Exception )
			{
			}
		}
	}
	else
	{
		document.getElementById( "companyList" ).style.position='relative';
		document.getElementById( "companyList" ).style.visibility='visible';
		
		try
		{
			document.getElementById( "searchLink" ).style.visibility='visible';
			document.getElementById( "searchLink" ).style.position='relative';
		}
		catch( Exception )
		{
		}
	}
}

function actionClickAlpha(alphaChar)
{
	var formElement = document.alphaSearch;
	formElement.searchstr.value=alphaChar;
	formElement.submit();
}

function userDefWindow(url)
{
	udWindow = open(url, 'udWindow', "height=480,width=640,channelmode=0,dependent=0,directories=0,fullscreen=0,location=1,menubar=0,resizable=1,scrollbars=1,status=1,toolbar=1"); 
	udWindow.focus(); 
}

function populateIndices()
{
	try
	{
		var data = request.responseText;
		document.getElementById("indices").innerHTML = data;
	}
	catch ( Exception )
	{
		lookupError();
	}
}

function lookupIndicesError()
{
	document.getElementById("indicesMessage").innerHTML = '<span style="color:#600;">No data available.</span>';
}

function populateHelp()
{
	try
	{
		var data = request.responseText;
		document.getElementById("helpSection").innerHTML = data;
	}
	catch ( Exception )
	{
		lookupHelpError();
	}
}

function lookupHelpError()
{
	document.getElementById("helpSectionMessage").innerHTML = '<span style="color:#600;">No help available.</span>';
}

function getSearchHelp( disableBackground )
{
	document.getElementById("helpSectionMessage").innerHTML='<span style="color:#060;">Loading...</span>';
	document.body.style.cursor='wait';
	try
	{
		var rqValue = 'sym';
		if( document.getElementById('companyList').style.visibility != 'hidden' )
		{
			rqValue = 'alpha';
		}
		else if ( document.getElementById('keywordOptionInput').style.visibility != 'hidden' )
		{
			rqValue = 'key';
		}
		else if ( document.getElementById('symbolNameInput').style.visibility != 'hidden' )
		{
			rqValue = 'sym';
		}
		
		document.getElementById("searchHelp").rq.value = rqValue;
		
		var params = formatFormParametersToURI( document.getElementById("searchHelp") );

		if ( disableBackground )
		{
			params += "&bgDisable=1";
		}
		else
		{
		}

		sendRequest( "searchHelp", params, "populateHelp", "lookupHelpError", true );
		document.getElementById("helpSectionMessage").innerHTML='';
	}
	catch( Exception )
	{
		lookupHelpError();
	}
	
	document.body.style.cursor='auto';
}

function searchHelpHide()
{
	document.getElementById('popupHelp').style.visibility='hidden';
	document.getElementById('popupHelpBlock').style.visibility='hidden';
}

