// debug alerts through # 12

// DEBUGGING CONTROLS: ALL MUST BE FALSE FOR PUBLIC RELEASE

	var showView1Spt = false; // will appear in new browser window (behind?)
	var checkboxToDebugScripts = false; // also offers link to show PDBHeader
	var debugAllScripts = false;
	var buttonToShowMessages = false;
	// Note: for ConSurf/PepSurf, set debugAllScripts to true to see the
	// complete initial view script as processed by scriptToJmol().

	var showPDBURL = false; // shows pdbURL in controlsDiv below controls
	// Also now shown at the bottom of the troubleshooting help panel.

// END DEBUGGING CONTROLS

	if (debugAllScripts)
		showView1Spt = true; // must always be true

// top.js - top level js. Parsing of molecule specified in ?mol=
// and decision tree for action, depending on configuration specified
// in config.js. Getting pixel dimensions for Jmol from window size.

// Note: variables that depend on server configuration are
// defined in config.js

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
var fgijVersion = "1.45";
var releaseDate = "[Release Date Uncertain], 2010";
var releaseDateV1 = "December, 2006";

// usingFrames is defined in fg.htm etc.

// SUPPORTED SERVERS
var consurfMode = false; 
var pepsurfMode = false; 
var selectonMode = false; 
var epitopiaMode = false;
var telAvivMode = false;

// GENERAL SETTINGS
var processHeaderBeforeMolView = false;
var controlPanel = "fg";
var helpPanel = "pleaseBePatient";
var identical_chains_reported = true;

var usingSignedApplet = false;
var usingCIF = false;
var debuggingFG = false;

// modes: nf = no frames; f = frames; r = rcsb mock up.
var fgijMode = "nf"; 

var isServed = (document.location.href.indexOf("http://") == 0);

var currentView = "cartoonSpt"; 
var currentHelp = "";


// CHECKBOX MODIFICATIONS OF VIEWS
var showNonStandardResidues = false;
var starNonStandardResidues = true;

var waterIsSmall = false;
var ligandIsSmall = false;
var allWireframe = false;
var sidechainsVisible = true;
var hydrogensVisible = true; // for Vines
var showPolar = false; // for Charge
var showChargeIncompleteSidechains = true;
var colorChargeOnlyAtoms = true;

var hideMode = ""; // none, c (chain), g (group), a (atom)
var lastHideMode = "";
var hiddenChains = new Array();
var hiddenGroups = new Array();
var hiddenAtoms = new Array();
var hiddenRanges = new Array();
var hiddenRangeStart = "";
var invertHidden = false;

var hideProtein = false;
var hideDNA = false;
var hideRNA = false;
var hideCarbohydrate = false;
var hideHydrogen = false;

var selectMode = ""; // blank, c, g, a (same as hideMode)
var selectedChains = new Array();
var selectedGroups = new Array();
var selectedAtoms = new Array();
var selectedFound = "";
var selectedRanges = new Array();
var selectedRangeStart = "";
var selectedWater = false;

var axesOn = false;
var unitcellOn = false;
var boundboxOn = false;

var slabThickness = "10";
var toFind = "";

var forceBackgroundBlack = false;
var contactsShowing = false;
var markContactsTargets = false;
var backboneContacts = true;
var labelContacts = false;
var hydrogensContacts = false;
var colorContactsTargetGray = false;

var targetSF = true;
var targetCPK = false;
var contactingSF = true;
var contactingCPK = false;
var contactsRadioViews = false;

var showContactsHBonds = true; // hbonds + metal bonds
var showContactsMeMi   = true; // metals + misc
var showContactsHphob  = true; // hydrophobic
var showContactsSB     = true; // salt bridges
var showContactsCatPi  = true; // cation pi interactions
var showContactsSticks = true;
var showContactsWater  = true;
var showWaterBridges   = true;

var preContactsView = "sf";
var preContactsWater = false;

var showCatPi = false; // false shows salt bridges
var colorCatPiSBChain = false;
var showCatPiSBBackbones = true;

// ssbonds comments show names of checkboxes
var showSSBondsBackbones = true; // backbonesSSBonds
var showSSBondsLabels = true; // labelsSSBonds
var connectSSBondsBackbones = true; // connectSSBondsBB
var hideCysNotSSBonded = false; // hideCysNotSS
var colorSSBondsChain = false; // colorSSChain

var showAnomalous = true;
var anomalousDots = true;

var labelsOn = true;
var labelsFront = false;
var labelsID = false;

var rcsbPDBURLHead =
"http:\/\/www.pdb.org\/pdb\/search\/structidSearch.do?structureId=";

var jmolDocsURL = "http://chemapps.stolaf.edu/jmol/docs/";

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

var jmolWidth, jmolHeight;

var pdbToLoad; // Query paramerer "mol=". Can be PDB Id or filename/URL.

var pdbFilename; // when mol=url/filename.pdb, pdbFilename is "filename.pdb".

var pdbId; // filename (.pdb removed) derived from pdbToLoad.
// When mol=url/filename.pdb, pdbId is "filename".
// pdbId may not be a valid PDB Id.

var validPDBId = ""; // Contains a valid PDB Id if pdbToLoad was a valid PDB Id,
// or if the first four characters of pdbId are a valid PDB Id. Else blank.

var pdbURL; // URL Jmol uses to fetch the molecule.

var molIncludesDotPDB; // True if pdbToLoad includes ".pdb".
var molIncludesDot; // True if pdbToLoad includes ".".
var molIncludesHTTP; // http:// in pdbToLoad
var molIncludesFTP;  //  ftp:// in pdbToLoad
var molIncludesSlash; // relative path (if no TP)
var usePDBId; // get PDB from RCSB

var helpTarget;
var controlsTarget;

var DOC_WIDTH, DOC_HEIGHT;

var centerMode = false;

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function initialize()
{
	top.get_dimensions();
	top.getMolToLoad();
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function initTargetDivs()
{
	if (top.usingFrames)
	{
		helpTarget = top.frameLeft.frameLeftBottom;
	}
	else
	{
		if (document.layers) // true in N4; false in IE, Gecko
		{
			helpTarget = document.layers['helpDiv'];
//			alert("top.js #2 document.layers 'true'");
//			alert("top.js #2a: " + document.layers['helpDiv']);
		}

		else
		{
			helpTarget = top.document.getElementById('helpDiv');
//			alert("top.js #2b document.layers 'false'");
		}
	}

	controlsTarget = top.document.getElementById('controlsDiv');

//	alert("top.js #8: " + top.helpTarget);
//	alert("top.js #8b: controlsTarget = " + top.controlsTarget);
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function getMolToLoad()
{
	var qs = document.location.search; // qp query parameters
	var ieq, iaa, ipdb, islash;

	if (qs.indexOf("appl=s") != -1)
		usingSignedApplet = true;

	if (qs.indexOf("cif=1") != -1)
		usingCIF = true;

	if (qs.indexOf("db=1") != -1)
		debuggingFG = true;

	// qs: mode is now handled in gallery.js

	// mol=http://... will be escaped: colon -> %3A.
	pdbToLoad = unescape(qs); // ?mol=1d66&mode=x

	// consurfMode, pepsurfMode, selectonMode, epitopiaMode
	if (pdbToLoad.indexOf("consurf") != -1 ||
		pdbToLoad.indexOf("pepsurf") != -1 ||
		pdbToLoad.indexOf("selecton") != -1 ||
		pdbToLoad.indexOf("epitopia") != -1)
			processHeaderBeforeMolView = true;

	// Extract mol=...&...
	if ((ieq = pdbToLoad.indexOf("mol=")) != -1)
		pdbToLoad = pdbToLoad.substring(ieq + 4);
	else
	{
		alert("ERROR: No PDB file specified, for example\n" +
			"fg.htm?mol=1d66.pdb");
		document.location.replace("index.htm");
	}

	// trim off trailing &...
	if ((iaa = pdbToLoad.indexOf("&")) != -1)
		pdbToLoad = pdbToLoad.substring(0, iaa);

	// Note type of PDB URL
	molIncludesDotPDB = (pdbToLoad.indexOf(".pdb") != -1); // NOT USED
	molIncludesDot = (pdbToLoad.indexOf(".") != -1);
	molIncludesHTTP = (pdbToLoad.indexOf("http://") != -1);
	molIncludesFTP = (pdbToLoad.indexOf("ftp://") != -1);
	molIncludesSlash = (pdbToLoad.indexOf("/") != -1);

	if ((molIncludesHTTP && !apacheGeneralRewriteRulesAvailable) ||
		molIncludesFTP)
	{
		if (!usingSignedApplet)
		{
			if (confirm(
"Obtaining this PDB file requires the Signed Jmol\n" +
"Applet. Click OK to use Signed Jmol, then agree\n" +
"only if you do trust the source of this applet."))
				usingSignedApplet = true;

			// else continue with unsigned applet, which will fail
			else alert("You have declined to use the signed applet.\n" +
				"The unsigned applet will be unable to obtain\n" +
				"this molecule from the source specified!");
		}
	}

	// Get pdbId for display at top of Jmol
	pdbFilename = pdbToLoad;

	// Trim off leading path
	if ((islash = pdbFilename.lastIndexOf("/")) != -1)
		pdbFilename = pdbFilename.substring(islash + 1);

	pdbId = pdbFilename;

	// Trim off ".pdb"
	if ((ipdb = pdbId.indexOf(".pdb")) != -1)
		pdbId = pdbId.substring(0, ipdb);

	pdbId = pdbId.toUpperCase();

	// Set PDB Id to be loaded by Jmol applet
	usePDBId = isValidPDBId(pdbToLoad);

	// Attempt to get valid PDB Id.
	validPDBId = "";
	if (usePDBId)
		validPDBId = pdbId;
	else
	{
		validPDBId = pdbId.substring(0, 4);
		if (!isValidPDBId(validPDBId))
			validPDBId = "";
	}

	// DECIDED THAT "words" ARE NOT ILLEGAL.
	// Exclude "words" that lack a file extension and are not URL's.
//	if (!usePDBId && !molIncludesDot && !molIncludesHTTP &&
//		!molIncludesFTP && !molIncludesSlash && isServed)
//	{
//		alert(
//"ERROR: The atomic coordinate data file is given
//in an illegal format: \"" + pdbToLoad + "\"\n" +
//FirstGlance does not know how to obtain it.");
//		pdbURL = "";
//	}

	//= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
	// Construct URL for use by Jmol

	var signedTail = rcsbURLSignedTail;
	var localTail = localPDBCodesPathTail;
	var rcsbTail = rcsbPathTail;
	if (usingCIF)
	{
		signedTail = rcsbURLSignedTailCIF;
		localTail = localPDBCodesPathTailCIF;
		rcsbTail = rcsbPathTailCIF;
	}

	// SIGNED APPLET
	if (usingSignedApplet)
	{
		if (usePDBId)
		{
			if (localPDBCodesAvailable)
				pdbURL = localPDBCodesPathHead + pdbId + localTail;
			else
				pdbURL = rcsbURLSignedHead + pdbId + signedTail;
		}
		else if (molIncludesHTTP) // http://...
			pdbURL = pdbToLoad;
		else if (molIncludesSlash) // relative URL
			pdbURL = pdbToLoad;
		else // no slash, just filename, assume local
			pdbURL = localPDBFiles + pdbToLoad;
	}
	// FTP DISALLOWED (signed applet CAN use FTP)
	else if (molIncludesFTP)
	{
		alert("Sorry, this PDB file URL requires FTP\n" +
			"(File Transfer Protocol), but the\n" +
			"unsigned Jmol applet cannot use this method.");
		pdbURL = "";
	}
	// UNSIGNED APPLET
	else if (usePDBId)
	{
		if (isServed)
		{
			if (localPDBCodesAvailable)
				pdbURL = localPDBCodesPathHead + pdbId + localTail;

			else if (apacheRCSBRewriteRulesAvailable)
				pdbURL = rcsbPathHead + pdbId + rcsbTail;

			else
			{
				alert(
"Sorry, this installation of FirstGlance has no\n" +
"method for obtaining a PDB Id code!");
				pdbURL = "";
			}
		}
		else // use test location for local PDB files
			pdbURL = localTestPDBFiles + pdbToLoad + ".pdb";
	}
	else if (molIncludesHTTP)
	{
		if (!apacheGeneralRewriteRulesAvailable && !usingSignedApplet)
			pdbURL = ""; // already warned above
		else
			pdbURL = generalPathHead + pdbToLoad.substring(7); // trim off http://
	}
	else if (molIncludesSlash) // relative URL, OK isServed or not.
	{
		pdbURL = pdbToLoad;

		if (!usingSignedApplet && !isServed && pdbToLoad.indexOf("..") != -1)
		{
			if (confirm(
"Obtaining this PDB file requires the Signed Jmol\n" +
"Applet. Click OK to use Signed Jmol, then agree,\n" +
"if you are asked to trust it."))

			usingSignedApplet = true;

			else pdbURL = "";
		}
	}
	else // typically pdbToLoad will end ".pdb", but not required as long
	// as the complete filename and extension (if there is one) is given.
		pdbURL = localPDBFiles + pdbToLoad;

	if (debuggingFG)
	{
		alert("top.js #3" +
			",\n pdbToLoad="         + pdbToLoad +
			",\n usePDBId="          + usePDBId +
			",\n pdbId="             + pdbId +
			",\n molIncludesDotPDB=" + molIncludesDotPDB +
			",\n molIncludesHTTP="     + molIncludesHTTP +
			",\n molIncludesFTP="     + molIncludesFTP +
			",\n molIncludesSlash="  + molIncludesSlash +
			",\n pdbURL="            + pdbURL +
			"");
	}
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function isValidPDBId(pid)
{
	if (pid.length != 4)
		return(false);

	pid = pid.toUpperCase();

	// First character must be 1-9
	if (pid.charAt(0) < '1' || pid.charAt(0) > '9')
		return(false);
	// Last three characters must be 1-9 or A-Z
	if (!pdbIdAlphaNum(pid, 1))
		return(false);
	if (!pdbIdAlphaNum(pid, 2))
		return(false);
	if (!pdbIdAlphaNum(pid, 3))
		return(false);

	// all tests passed		
	return(true);
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function pdbIdAlphaNum(pid, pos)
{
	var x = pid.charAt(pos);
	if ((x >= '0' && x <= '9') ||
		(x >= 'A' && x <= 'Z'))
		return true;
	else return false;
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function get_dimensions()
{
	get_applet_dimensions(); // and DOC_HEIGHT, DOC_WIDTH

	if (outerTable)
	{
		// get helpHeight, used only in RCSB mock up fgijr.htm
		// The last term is a fudge factor because we're not in fact limiting
		// FGiJ by the full heightDecrement.
		// heightDecrement was already subtracted.
		helpHeight = DOC_HEIGHT - controlsHeight +	helpHeightFudge;
	}
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function get_applet_dimensions()
{
	DOC_WIDTH = 790; 
	DOC_HEIGHT = 400; 

	if (typeof window.innerWidth != 'undefined')
	{ 
		DOC_WIDTH = window.innerWidth; 
		DOC_HEIGHT = window.innerHeight; 
	} 
 
	else if (typeof document.documentElement.offsetWidth != 'undefined')
	{ 
		DOC_WIDTH = document.documentElement.offsetWidth;  
		DOC_HEIGHT = document.documentElement.offsetHeight;  
	} 
 
	else
	{ 
		DOC_WIDTH = document.body.offsetWidth;  
		DOC_HEIGHT = document.body.offsetHeight;  
	} 
 
//	alert ('top.js #4 DOC_WIDTH=' + DOC_WIDTH + ', DOC_HEIGHT=' + DOC_HEIGHT); 

	if (outerTable)
	{
		DOC_WIDTH -= widthDecrement;
		DOC_HEIGHT -= heightDecrement;
	}
 
	jmolWidth = parseInt( DOC_WIDTH * 0.56 ); // 0.56
	jmolHeight = parseInt( (DOC_HEIGHT * 0.95) - 30 ); // 0.95

// These dimensions are for a square applet, full window height "Tshirt"
//	jmolWidth = parseInt( DOC_WIDTH * 0.72 ); // 0.56
//	jmolHeight = parseInt(DOC_HEIGHT);

//	alert ('top.js #5 jmolWidth=' + jmolWidth + ', jmolHeight=' + jmolHeight); 
 
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function showHelp(h)
{
	if (usingFrames)
		top.frameLeft.frameLeftTop.showHelp2(h);
	else
		showHelp2(h);
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function aboutFG()
{
	window.open("about.htm");
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function openOptions()
{
	window.open("options.htm");
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function toggleNSRStars() // crosses
{
	if (starNonStandardResidues)
		starNonStandardResidues = false;
	else
		starNonStandardResidues = true;

	doMolViewSpt(""); // star rendering is done in scriptToJmol()
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function toggleNonStandardResidues()
{
//	alert("top.js #7 showNonStandardResidues = " +
//		showNonStandardResidues);

	if (showNonStandardResidues)
		showNonStandardResidues = false;
	else
		showNonStandardResidues = true;

	// This function can be called when Ligands help, or More Views are displayed.
	// The currentView can be anything.
	// When the currentView is changed, it will either hide ligands+
	// (including non-standard residues), or if it leaves ligands+ displayed,
	// it will add script commands for non-standard residues if they are shown.

	// Modify currentView to show/hide non-standard residues.
	// Display is either spacefill or ball and stick.
	// Colors (when shown) are always CPK.

	var nsrSpt = selectNonStandardResiduesSpt;
	if (showNonStandardResidues)
	{
			nsrSpt += ballAndStickSpt;
	}
	else // hide
	{
		nsrSpt += hideBSSpt;
	}

	doMolViewSpt(nsrSpt);	
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function cancelCentering()
{
	cancelCenteringNoHelp();
	showHelp('centeringCancelledHelp');
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function centerAll()
{
	scriptToJmol(top.centerAllSpt);
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function cancelHiding()
{
	cancelHidingNoHelp();
	showHelp('makeHideNoMoreHelp()');
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function cancelHidingNoHelp()
{
	top.lastHideMode = top.hideMode;
	top.hideMode = "";
	cancelHiddenRangeNoHelp();
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function changeHideRadio()
{
//	alert("top.js #9a entering changeHideRadio()");

	hideMode = getRadioValue(top.document.hideForm.hidewhat);

//	alert("top.js #9b hideMode = " + hideMode);

	showHelp("makeHideHelp()");
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function changeSelectRadio()
{
	selectMode = getRadioValue(top.document.contactsForm.selectwhat);
	showHelp("makePreContactsHelp()"); // in changeSelectRadio()
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function clearSelectedArrays()
{
	selectedChains.length = 0;
	selectedGroups.length = 0;
	selectedAtoms.length = 0;
	selectedFound = "";
	selectedRanges.length = 0;
	selectedRangeStart = "";
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function clearSelected()
{
	clearSelectedArrays();
	scriptToJmol(makeSelectSpt()); // to regen. ~targ_current (in clearSelected())
	top.showHelp2("makePreContactsHelp()"); // in clearSelected()
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// direction is "" or "-"
function doZoom(direction)
{
	centerMode = false; // scriptToJmol turns off picking center

	var starslash = "*";
	if (direction == "-")
		starslash = "/";
//	scriptToJmol("move 0 0 0 " + direction + "30 0 0 0 0 1\n");
	scriptToJmol("zoomto " + starslash + "2\n");

	// Don't interfere with hidden range selection, or with preContacts,
	// by showing zoomHelp.
	if (hideMode != "r" && selectMode == "")
		showHelp('zoomHelp');
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function showWaterSmall()
{
	if (waterIsSmall)
	{
		doMolViewSpt(bigWaterSpt);
		waterIsSmall = false;
	}
	else
	{
		doMolViewSpt(smallWaterSpt);
		waterIsSmall = true;
	}

}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function showLigandSmall()
{
	if (ligandIsSmall)
	{
		doMolViewSpt(bigLigandSpt);
		ligandIsSmall = false;
	}
	else
	{
		doMolViewSpt(smallLigandSpt);
		ligandIsSmall = true;
	}

}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function toggleWireframeAll() // more detail
{
//	alert("top.js #6 allWireframe = " + allWireframe);

	if (allWireframe) // more detail to less
	{
		allWireframe = false;
		doMolViewSpt(makeVineSpt());
	}
	else // less detail to more
	{
		allWireframe = true;
		doMolViewSpt(makeWireframeAllSpt());
	}
	showHelp("makeVinesHelp()");
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function toggleHydrogens()
{
	if (hydrogensVisible)
	{
		hydrogensVisible = false;
		doMolViewSpt(hideHydrogensSpt);
	}
	else
	{
		hydrogensVisible = true;
		if (allWireframe)
			doMolViewSpt(makeWireframeAllSpt());
		else
			doMolViewSpt(makeVineSpt());
	}
	showHelp("makeVinesHelp()");
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function toggleSidechains()
{
	if (sidechainsVisible)
	{
		sidechainsVisible = false;
		doMolViewSpt(makeHideSidechainsSpt());
	}
	else
	{
		sidechainsVisible = true;
		if (allWireframe)
			doMolViewSpt(makeWireframeAllSpt());
		else
			doMolViewSpt(makeVineSpt());
	}
	showHelp("makeVinesHelp()");
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function togglePolarColor()
{
	showPolar = !showPolar;

	doMolViewSpt(makeChargeSpt());
	showHelp("makeChargeHelp()");
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function toggleChargeIncompleteSidechains()
{
	showChargeIncompleteSidechains = !showChargeIncompleteSidechains;

	doMolViewSpt(makeChargeSpt());
	showHelp("makeChargeHelp()");
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function toggleChargeOnlyAtoms()
{
	colorChargeOnlyAtoms = !colorChargeOnlyAtoms;

	doMolViewSpt(makeChargeSpt());
	showHelp("makeChargeHelp()");
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// scriptToJmol() is the main function to send a script to jmol.
// Scripts that change the molecular view should come through doMolViewSpt().
// See techinfo.htm for more.
// (just for finding while developing: stj, s2j)

function scriptToJmol(spt)
{
//	alert("top.js #11: scriptToJmol():\ncurrentView = " + currentView);

	// DOTS are handled in doMolViewSpt()

	spt += "# BEGIN scriptToJmol() additions;\n";
//	spt += "set refreshing false;\n"; BEFORE JMOL.JS

	if (!centerMode)
		spt += setPickingIdentSpt; // cancels centering by picking

	// MARK CONTACTS TARGETS WITH * LABELS
	spt += makeMarkContactsTargetSpt();

	// LABEL SSBONDS
	spt += makeLabelSSBondsSpt();

	if (labelsOn) // ADD LABELS
	{
		if (pepsurfMode && controlPanel == "consurf")
		{
			spt += make_label_pepsurf_alignments_spt();
		}

		else // FG
		{
		// MARK NONSTANDARD RESIDUES WITH x LABELS
		// controlPanel not yet set to "consurf" for view1Spt.
		if (starNonStandardResidues && !processHeaderBeforeMolView)
			spt += makeXNonStandardResiduesSpt();
//	else
//		spt += unStarNonStandardResiduesSpt;

		// MARK INCOMPLETE SIDECHAINS WITH s- LABELS
		spt += makeMarkIncompleteSidechainsSpt();

		// MARK ANOMALOUS ATOMS WITH ? LABELS
		spt += makeMarkAnomalousAtomsSpt();
		}
	}

	// MUST BE LAST SO HALOS REFLECT SELECTED
	// toFind is blanked in  doViewLink() when selectMode goes on.
	if (toFind != "") 
		spt += makeFindSpt();
	else
		spt += "set display normal # hides halos;\n"; // scriptToJmol()

//	spt += "set refreshing true;\n";

		spt += "# END scriptToJmol() additions;\n";

//	alert("top.js #1: scriptToJmol()\n" + spt);
	if (debugAllScripts)
	{
		showSptInWindow(spt);
	}

	if (epitopiaMode && match(helpPanel, "consurf") && Ctot > 1)
		document.cgrades.radioFindChain[0].checked = true;

	unchangedScriptToJmol(spt);
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function unchangedScriptToJmol(spt)
{
//	if (top.usingFrames)
//		top.frameRight.document.applets[0].script(spt);
//	else

	// BEFORE JMOL.JS
		top.document.applets[0].script(spt); // still flashes black in Safari

	//jmolScript(spt); // w/ Wait: panels don't load; didn't investigate
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function returnToIntro()
{
	hideMode = "";
	cancelCenteringNoHelp(); // sends script set picking on
	showHelp('makeIntroHelp()');
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
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 makeEchoSpt(txt, posn)
{
	var echoColor = top.getEchoColor();

	if (typeof(posn) == "undefined")
		posn = "bottom left";

	var ecs =
"set echo " + posn + ";\
font echo 18 sanserif bold;\
color echo " + echoColor + ";\
echo \"" + txt + "\";\n";

	return ecs;
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function firstWord(smw)
{
	var spn = smw.indexOf(" ");
	if (spn == -1)
	{
		alert("Programmer's error: firstWord() finds no space!");
		return(smw);
	}
	var fw = smw.substring(0, spn);
//	alert(fw + " " + fw.length);
	return fw;
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function setDebugScripts()
{
	debugAllScripts =
		document.controlsForm.debugSpts.checked;
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function showSptInWindow(spt)
{
		var debugWin = window.open("");
		with (debugWin.document)
		{
			writeln("<pre>\n" + spt + "</pre>");
		}
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function makeNotesLink(descrip, targ, tail, ttl)
{
	var towr = "<a href='notes.htm";

	if (validPDBId != "")
		towr += "?mol=" + validPDBId;
	
	if (targ != "")
		towr += "#" + targ;

	towr += "' target=_blank";

	if (typeof(ttl) != "undefined")
		towr += " title='" + ttl + "'"

	towr += ">" + descrip + "</a>";

	if (typeof(tail) != "undefined")
		towr += tail;

//	alert("top.js #12: makeNotesLink\n" + towr);
	return (towr);
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function writeNotesLink(descrip, targ, tail, ttl)
{
	document.writeln(makeNotesLink(descrip, targ, tail, ttl));
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

