// JavaScript Document

// 1st argument takes the ajax target with all its particular parameters (ex. 'contentName=journalIssue&jid=$jid&yr=$yr&vol=$vol&no=$no&sd=$sdNo')
// 2nd argument (displayTarget) it is the id of the div where output is displayed
// 3rd argument (spinnerId) is the display target of the in-progress spinner
// feature: open/close on same click
// feature: adds class to display target div for css transition

var xmlHttp;
var target;
var thisSpinnerId = 0;
// an array storing current open ids so we can toggle them on and off
var openIds = new Array();

function getContent(ajaxAction, displayTarget, spinnerId)
{
	if (spinnerId != null) {
		thisSpinnerId = spinnerId;
	}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }
	// fixes indexOf in IE	
	if(!Array.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
	}
	// add class to id for transition
	originalClass = document.getElementById(displayTarget).className;
	document.getElementById(displayTarget).className = originalClass + " showTarget";

	// look for new target in array of open ids
	var newTarget = openIds.indexOf(displayTarget);
	if (newTarget != -1) {
		// here we found it, so the click was meant to close the item
		// close the id in the display
		document.getElementById(displayTarget).innerHTML = "";
		//
		// and remove it from the openId array
		openIds.splice(newTarget,1)
	} else {
		// new id not in the array
		// add new target to the array
		openIds.push(displayTarget);
		// show new content, spinner first
		if (spinnerId) {
			showSpinner(spinnerId);
		}
		target = displayTarget;
		var url= ajaxAction;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=getContent_stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}
function closeContent(displayTarget) {
	document.getElementById(displayTarget).innerHTML="";
	openIds.splice(displayTarget);
}


function getContent_stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		document.getElementById(target).innerHTML=xmlHttp.responseText;
		if (thisSpinnerId != 0) {
			hideSpinner(thisSpinnerId);
		}
	}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
