/*
 * Send an ajax request to a server, and specify the callback method
 * that should get called asynchronously when the XML returns.
 */
function ajaxFetch(urlStr, params, callback, method) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open(method, urlStr, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = callback;
    self.xmlHttpReq.send(params);
}

/*
*	Processes the incoming request from the Flash piece.
*/
function getTimelineData(param) {
	if(param.indexOf('uq') >= 0){
		ajaxFetch('/ajax_timeline_processor.jsp', param, searchResultsCallback,'POST');
	}else{
		var doc = param.substring("doc=".length);
		// timeline swf escapes special characters so doc = S%26H
		doc = unescape(doc);
		// Open the document in the "Document Viewer" tab.
		window.parent.openDocViewer(doc);
	}
}

function trim(text){
	return text.replace(/(?:(?:^|\n|\r)\s+|\s+(?:$|\n|\r))/g,"");
}

/*
*	Populates a div tag on the page with search hits.
*/
function searchResultsCallback() {
	if (self.xmlHttpReq.readyState == 4) {
		var searchResults = self.xmlHttpReq.responseText;
		/*
		*	Hides static content on the timeline page.
		*/
		var staticContent = document.getElementById('static_content');
		staticContent.style.display = 'none';
		
		/*
		*	Display search results on the timeline page.
		*/
		var divTag = document.getElementById('searchresults');
		divTag.innerHTML = searchResults;  	
    }
}

/*
 *	Hide/show the element with the given id.
 */
function toggleElement(id) {
	var element = document.getElementById(id);
	if (element.style.display == '') {
		element.style.display = 'none';
	} else {
		element.style.display = '';
	}
}

/*
*	Use this method to detect a browser plugin for Firefox, Safari or Netscape.
*	You can pass one or more plugin names or prefixes that can represent confirm the existence of a plugin.
*	For example, for Adobe Acrobat Reader Browser plugin the prefixes will be:
*
*	"Acrobat Reader", "Adobe PDF Plug-In". 
*
*	You call this function is this manner:
*	
*	detectPluginNS("Acrobat Reader", "Adobe PDF Plug-In")
*	
*	If the plugin is found, the method returns "true". Otherwise, "false".
*/
function detectPluginNS() {
	// allow for multiple checks in a single pass
	var daPlugins = detectPluginNS.arguments;
	// consider pluginFound to be false until proven true
	var pluginFound = false;
	// if plugins array is there and not fake
	if (navigator.plugins && navigator.plugins.length > 0) {
		var pluginsArrayLength = navigator.plugins.length;
		// for each plugin...
		for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
			// loop through all desired names and check each against the current plugin name
			var numFound = 0;
			for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
				// if desired plugin name is found in either plugin name or description
				if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
					(navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
					// this name was found
					numFound++;
				}   
			}
			// now that we have checked all the required names against this one plugin,
			// if the number we found matches the total number provided then we were successful
			if(numFound > 0) {
				pluginFound = true;
				// if we've found the plugin, we can stop looking through at the rest of the plugins
				break;
			}
		}
	}
	return pluginFound;
}

/*
*	Use this method to detect a browser ActiveX control for Internet Explorer 4.0+
*	You can pass one or more ActiveX Control names .
*	For example, for Adobe Acrobat Reader 7  the ActiveX Control name will be:
*	
*	"AcroPDF.PDF.1"
*
*	You call this function is this manner:
*	
*	detectPluginIE("AcroPDF.PDF.1")
*	
*	If the ActiveX Control is found, the method returns "true". Otherwise, "false".
*/
function detectPluginIE() {
	var pluginFound = false;	
	if(window.ActiveXObject){
		// allow for multiple checks in a single pass
		var daPlugins = detectPluginIE.arguments;
		// consider pluginFound to be false until proven true
		for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
			try{
				var obj = new ActiveXObject (daPlugins[namesCounter]);
				if(typeof(obj) != "undefined"){
					pluginFound = true;
					// if we've found the ActiveXControl, we can stop looking through at the rest of the control names.
					break;
				}
			}catch(e){
				/* In case of an error do nothing. */
			}
		}
	}
	return pluginFound;
} // detectPlugin

/*
*	Use this method to detect Adobe Acrobat Reader 7.x+ Browser Plug-In for
*	Internet Explorer, Firefox, Safari and Netscape.
*	If the plug-in is found, the method returns "true". Otherwise "false".
*/
function hasAcrobatReader() {
	// IE Adobe Acrobat 7.x+ ActiveXControl.
	var ADOBE_ACROBAT_7_ACTIVEX_CTRL = "AcroPDF.PDF.1";
	
	// Firefox, Netscape or Safari plgin prefixes.
	var ADOBE_ACROBAT_PLUGIN_PREFIX_1 = "Adobe Acrobat";
	var ADOBE_ACROBAT_PLUGIN_PREFIX_2 = "Adobe PDF Plug-In";
	
	return detectPluginNS(ADOBE_ACROBAT_PLUGIN_PREFIX_1, ADOBE_ACROBAT_PLUGIN_PREFIX_2) || detectPluginIE(ADOBE_ACROBAT_7_ACTIVEX_CTRL); 
}

function displayAdobeReaderMessage(){
	var el = document.getElementById('adobe_plugin');
	if(hasAcrobatReader()){
		el.style.display = 'none';
	}else{
		el.style.display = '';
	}
}
