//----------------------------------------------------------------------
// It is necessary to use javascript to start PE in order to allow
// the user to select a PIPE mode (audience, individual, development).
// Another advantage is that the version of PE used by all PiPEs can be
// changed by changing the pe_path variable in only one place.

function start_pe(pdb_filename)
{
	var fixed_path = get_prefix() + pe_path;

	var start_url = fixed_path + "pe.htm?";

	// Inform PE of the PIPE mode the user wants
	if (pdb_filename.indexOf("_consurf_") == -1)
	{
		if (typeof(document.start_pipe_form) != "undefined")
		{
			var mode = getRadioValue(document.start_pipe_form.radio_pipemode);
			putCookie_year("pipemode", mode);
			start_url += "pm=" + mode + "&";
		}
	}

	start_url += "id=" + pipe_path;
	
	start_url += pdb_filename;

	top.document.location.href = start_url;
}

//----------------------------------------------------------------------
// for each "/" in the current location.href following "my_molecules/pipes/",
// minus one, return "../"

function get_prefix()
{
	var retval = "";
	var anchor = "my_molecules/pipes/";
	var here = "" + document.location.href;
	var slashes = 0;

	var i = here.indexOf(anchor);
	if (i == -1)
		slashes = -1; // signals error
	else
	{
		i += anchor.length;
	
		// count slashes following anchor
		while ((i = here.indexOf("/", i)) != -1)
		{
			slashes++;
			i++;
		}
	}

	slashes--;

	if (slashes < 0 || slashes > 3)
	{
		alert("ERROR: this document is not in a subdirectory of\n" +
			"\"my_molecules/pipes/\", or a sub-subdirectory\n" +
			"(up to three levels of sub-subdirectories).\n" +
			"Protein Explorer will fail to start. See pstart.js.\n" +
			"Relocating this set of files is recommended.");
		return retval;
	}

	for (i = 1; i <= slashes; i++)
		retval += "../";
//	alert(retval);
	return retval;
}
//----------------------------------------------------------------------
function getRadioValue(rObj)
{
	var val=null;
	for (var i=0; i<rObj.length; i++)
	{
		if (rObj[i].checked)
		{
			val = rObj[i].value;
			break;
		}
	}
	return(val);
}

//----------------------------------------------------------------------
function insert_pipemode_radio_buttons()
{
	var mode = getCookie("pipemode");
	if (mode == null)
		mode = "d";
	with (document)
	{
		writeln("<br><font size='3'>Show for");
		writeln("<input type='radio' name='radio_pipemode' value='a' ");
		if (mode == "a")
			writeln ("checked");
		writeln(">Audience /");
		writeln("<input type='radio' name='radio_pipemode' value='i' ");
		if (mode == "i")
			writeln ("checked");
		writeln(">Individual /");
		writeln("<input type='radio' name='radio_pipemode' value='d' ");
		if (mode == "d")
			writeln ("checked");
		writeln(">Development.");
		writeln("<a href='" + pe_path + "pipedoc/modehelp.htm'>Help</a></font>");
	}
}

//----------------------------------------------------------------------
function putCookie_year(cname, cval)
{
	var d1 = new Date();
	one_year = 31449600000;
	d1.setTime(d1.getTime() + one_year);

	document.cookie = cname + "=" + escape(cval) + "; expires=" + d1.toGMTString();
}

function getCookie(cname) // returns null if cookie absent
{
	var result = null;
	var raw = " " + document.cookie + ";";
	var toget = " " + cname + "=";
	var s0 = raw.indexOf(toget);
	var s1;

	if (s0 != -1)
	{
		s0 += toget.length // skip cname
		s1 = raw.indexOf(";", s0);
		result = unescape(raw.substring(s0, s1));
	}
	return result;
}
//----------------------------------------------------------------------
// pipe_path is used to tell PE where to find the PiPE.
// It is obtained automatically, regardless of where the PiPE is located.

// In pipe_path, slashes after dot-dots must be escaped to %2F, spaces to %20.
// The pipe_path should end with a slash.

var pipe_path = document.location.href;
var i = pipe_path.lastIndexOf("/");
pipe_path = pipe_path.substring(0, i + 1);
pipe_path = escape(pipe_path);

//alert("pstart.js #1 pipe_path:\n" + pipe_path);

//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
