/* SEARCH */
var loadResults;

function GetParams(controller, form)
{
	var params = '';
	if ((form != null) && (form.elements != null))
	{		
		for (var i = 0; i < form.elements.length; i++)
		{
			params += (form.elements[i].name.lastIndexOf('param') > -1) ? '&' + form.elements[i].name + '=' + form.elements[i].value : '';
		}
	}
	var result = controller + params + '&cd=' + new Date().getTime();
	return result;
}

function CheckForZZZInstrumentInput(form)
{
	var instruments = form.paramInstrumentCode;
	if (instruments != null)
	{
		return instruments.value != 'ZZZZZ';
	}
	return false;
}

function PerformSearch(form)
{
	if (!CheckForZZZInstrumentInput(form))
	{
		alert('Please select an instrument');
		return;
	};

	document.getElementById('SearchInput').style.display = "none";
	document.getElementById('SearchResults').style.display = "block";
	document.getElementById('SearchResultsContent').innerHTML = '<div id="SearchingThrobber"><p>Searching Database</p><p><img src="images/progress.gif" /></p';
	
	loadResults = new XMLHttpRequest();
	loadResults.open("GET", GetParams('ajaxSearch.asp?action=Search', form), true);
	loadResults.send('');
	loadResults.onreadystatechange = ResultsReady;
}

function ResultsReady()
{
	if(loadResults.readyState == 4)
	{	if   ( loadResults.status != 404) { document.getElementById('SearchResultsContent').innerHTML = loadResults.responseText; }
		else { alert('An unexpected error occured. If this problem persists, please notify Musictelex.'); }
	}
};

function RefineSearch()
{
	document.getElementById('SearchInput').style.display = "block";
	document.getElementById('SearchResults').style.display = "none";
}

var loadDetails;
function ShowDetails(form)
{
	loadDetails = new XMLHttpRequest();
	loadDetails.open("GET", GetParams('ajaxSearch.asp?action=Search', form), true);
	loadDetails.send('');
	
	document.getElementById('SearchResults').style.display = "none";
	document.getElementById('SearchDetails').style.display = "block";
	document.getElementById('SearchDetailsContent').innerHTML = '<div id="SearchingThrobber"><p>Getting article details...</p><p><img src="images/progress.gif" /></p';
	loadDetails.onreadystatechange = DetailsReady;
};

function DetailsReady()
{
	if(loadDetails.readyState == 4)
	{
		if (loadDetails.status != 404)
		{
			document.getElementById('SearchDetailsContent').innerHTML = loadDetails.responseText;
		}
		else
		{
			alert('An unexpected error occured. If this problem persists, please notify Musictelex.');
		}
	}
};

function BackToResults()
{
	document.getElementById('SearchResults').style.display = "block";
	document.getElementById('SearchDetails').style.display = "none";
}




var loadPage;
function ResultsPage(url)
{
	var params = url.substr(url.lastIndexOf('?'), url.length);
	loadPage = new XMLHttpRequest();
	loadPage.open("GET", "ajaxSearch.asp" + params, true);
	loadPage.send('');
	
	document.getElementById('SearchInput').style.display = "none";
	document.getElementById('SearchResults').style.display = "block";
	document.getElementById('SearchResultsContent').innerHTML = '<p>Searching Database<img src="images/progress.gif" /></p';
	loadPage.onreadystatechange = PageReady;
};

function PageReady()
{
	if(loadPage.readyState == 4)
	{	if   ( loadPage.status != 404) { document.getElementById('SearchResultsContent').innerHTML = loadPage.responseText; }
		else { alert('An unexpected error occured. If this problem persists, please notify Musictelex.'); }
	}
};

/* ... Dyanmic loading of the instruments ... */
var loadInstruments;
function SetSelectedInstrument(obj)
{
	loadInstruments = new XMLHttpRequest();
	var ic   = obj.options[obj.selectedIndex].value;
	var icn  = obj.options[obj.selectedIndex].text;

	if (ic.length > 1)
	{
		var sUrl = 'ajaxSearch.asp?action=dll&ic='+ ic +'&icn=' + icn;
		loadInstruments.open("GET", sUrl, true);
		loadInstruments.send('');	}
	else { document.getElementById('Instruments').innerHTML = ''; }
	loadInstruments.onreadystatechange = InstrumentsLoaded;
}

function InstrumentsLoaded()
{
	if(loadInstruments.readyState == 4)
	{	if   ( loadInstruments.status != 404) { document.getElementById('Instruments').innerHTML = loadInstruments.responseText; }
		else { document.getElementById('Instruments').innerHTML = "Active Form could not be loaded. Error: " + loadInstruments.status; }
	}
};