// Javascript library for Mission Mastermind

// Retrieve a search parameter value.
function getSearchParameter(p) {
  var ps = p + "";
  var pp = "&" + p;
  var s;
  if (ghsearch.length <=0)
    s = "&";
  else
    s = "&" + ghsearch.substring(1);
  var ip = 0;
  var iv, iamp, nc;
  var done = false;

  var v = new Object;
  v.exists = false;
  v.value = '';
  v.fChar = 0;
  v.lChar = 0;
  while (!done) {
	ip = s.indexOf(pp, ip);
  	if (ip >= 0) {
	  nc = s.charAt(ip + pp.length);
	  if (nc == "" || nc == "&") {
		v.exists = true;
	  	v.value = true;
		v.fChar = ip;
		v.lChar = ip + pp.length - 1;
		done = true;
      } else if (nc == "=") {
    	iv = s.indexOf("=", ip) + 1;
    	iamp = s.indexOf("&", iv);
    	if (iamp < 0) iamp = s.length;
		v.exists = true;
    	v.value = unescape(s.substring(iv, iamp));
		v.fChar = ip;
		v.lChar = iamp - 1;
		done = true;
	  } else {
		ip += ps.length;
	  }
  	} else {
	  done = true;
	}
  }
  return v;
}

// ap: Add Paramter: Add paramter to page's search string.  If it is already present, the current value will be replaced.
function ap(p) {
  var nv;
  if (arguments.length < 2)
    nv = p;
  else
    nv = p + "=" + myEscape(unescape(arguments[1]));
  var ov = getSearchParameter(p);
  var s = ghsearch;
  if (!ov.exists) {
    if (s.length == 0)
	  s = "?" + nv;
	else
	  s = s + "&" + nv;
  } else {
    s = s.substring(0, ov.fChar + 1) + nv + s.substring(ov.lChar +1);
  }
  ghsearch = s;
}

// dp: Delete parameter: remove a parameter from the page's search string.
function dp(p) {
  var v = getSearchParameter(p);
  if (v.exists) {
    var s = ghsearch;
	var lChar = Math.min(v.lChar + 1, s.length);
	var fChar = v.fChar;
	if (fChar == 0 && lChar < s.length) {
	  fChar = 1;
	  lChar += 1;
	}
	ghsearch = s.substring(0, fChar) + s.substring(lChar);
  }
}

// nps: Next Page with Search: Go to the specified URL but append the current search list.
function nps(url) {
  window.location = url + ghsearch;;
}

// apnps: Append Parameter/Next Page with Search: Combine ap and nps into a single call.
function apnps(url, p) {
  if (arguments.length < 3)
    ap(p);
  else
    ap(p, arguments[2]);
  nps(url);
}

// setDefaultSrc: Set the default src for an image that uses MM_swapImage/Restore.
function setDefaultSrc() { //v3.0
  var i,j=0,x,a=setDefaultSrc.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; x.oSrc=a[i+2];x.src=x.oSrc;x.isSwapped=false;}
}

// rto: Return TO- Return to a specific value of a parameter.
function rto(pname, alturl) {
  var url = getSearchParameter(pname);
  dp(pname);
  if (!url.exists)
    if (alturl)
	  nps(alturl);
	else
      history.back();
  else
    nps(url.value);
}

// gomain: close the activity window and send the main window to the specified url.
function gomain(url) {
  window.open(url, "original");
  window.close();
}

// myEscape: Hack to espace to also encode '/'
function myEscape(s) {
  var es = escape(s);
  for (var i = 0; i < es.length; ++i) {
    if (es.charAt(i) == '/')
	  if (i == 0)
	    es = "%2F" + es.substring(i+1);
	  else if (i == es.length)
	    es = es.substring(0, i) + "%2F";
	  else
	    es = es.substring(0, i) + "%2F" + es.substring(i+1);
  }
  return es;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function STSCI_closeMBrWindows() {
  for (var i in document.STScI_popups)
    if (document.STScI_popupsState[i] == "open")
      document.STScI_popups[i].close();
  document.MM_returnValue = false;
}
function STSCI_openMBrWindow(theURL,winName,features) {
  if (document.STScI_popupsState[winName]) {
    if (document.STScI_popupsState[winName] == "open") {
	  if (document.STScI_popups[winName].close)
        document.STScI_popups[winName].close();
	}
  }
  document.STScI_popups[winName] = window.open(theURL,winName,features);
  document.MM_returnValue = false;
}

function STScI_windowClose() {
  if (window.opener)
    if (window.opener.setClose)
      window.opener.setClose(window.name);
}

function STScI_windowOpen() {
  if (window.opener)
    if (window.opener.setOpen)
	  window.opener.setOpen(window.name);
}

function setOpen(winName) {
  document.STScI_popupsState[winName] = "open";
}

function setClose(winName) {
  document.STScI_popupsState[winName] = "close";
}

// Initializers.
var ghsearch = window.location.search;
document.STScI_popups = new Array;
document.STScI_popupsState = new Array;
