/* common.js, scripts that are loaded by part1.htm and part2.htm */

	/* this prevents the frameset from showing up inside another frame */
	if (parent.location.href != window.location.href)
		parent.location.href = window.location.href;

	// check URL parameters; part1.htm and part2.htm may be called with, e.g., ?ch=03&di=12
	var strParam = window.location.search;
	var Ch = 0; var Di = 0;

	if (strParam.length != 0) {
		if (strParam.indexOf("?") == 0 && strParam.indexOf("ch=") == 1 && strParam.indexOf("di=") == 7) {
			var Ch = strParam.substr(4, 2) * 1;
			var Di = strParam.substr(10, 2) * 1;
		}

		if (isNaN(Ch)) {
			alert("URL parameters are badly formatted.")
			Ch = 0; Di = 0;
		} else if (Ch < 1 || Ch >24 || Di < 1 || Di > 12) {
					Ch = 0; Di = 0;
				}

		if (strParam.indexOf("?") == 0 && strParam.indexOf("login=") == 13) {
			if (strParam.substr(19, 1) == "1") {
				/* opens a login window for WARec */
				var winFeatures = "toolbar=no,width=400,height=130,status=yes,scrollbars=no,resizable=yes,menubar=no";
				WARecWindow=window.open("warec.cfm?action=login","WarecWin", winFeatures);
			}
		}
	}

	function DialogUnloaded() {
	/* called when the text of the dialog is unloaded */
		if (top.PromptListWindow) top.PromptListWindow.close();
		if (top.audio) top.audio.location.href = "/loras_dialogs/scripts/aud_blank.htm";
	}

	function WeedPunc(What) {
	/* returns string What in which all common punctuation symbols are weeded out */

		var BadChars = " !`~@#$%^&'()*+,-.=_|\{}[]/\0123456789:;<=>?" + String.fromCharCode(34);
			BadChars = BadChars + String.fromCharCode(171) + String.fromCharCode(187) + String.fromCharCode(150) + String.fromCharCode(151); // Cyrillic quotes and dashes
			BadChars = BadChars + String.fromCharCode(145) + String.fromCharCode(146) + String.fromCharCode(147) + String.fromCharCode(148) + String.fromCharCode(132); // smart quotes

/* this is unnecessary assuming the Mac user types the target in a UTF-8 page, because then these characters have the same codes in mac OS as in Windows:
	    var agt=navigator.userAgent.toLowerCase();
	    var is_mac = (agt.indexOf("mac")!=-1);
		
		if (is_mac) {
			BadChars = BadChars + String.fromCharCode(199) + String.fromCharCode(200) + String.fromCharCode(208) + String.fromCharCode(209); // Cyrillic quotes and dashes
			BadChars = BadChars + String.fromCharCode(210) + String.fromCharCode(211) + String.fromCharCode(212) + String.fromCharCode(213) + String.fromCharCode(215); // smart quotes
		} else
			BadChars = BadChars + String.fromCharCode(171) + String.fromCharCode(187) + String.fromCharCode(150) + String.fromCharCode(151); // Cyrillic quotes and dashes
			BadChars = BadChars + String.fromCharCode(145) + String.fromCharCode(146) + String.fromCharCode(147) + String.fromCharCode(148) + String.fromCharCode(132); // smart quotes
*/
		var CleanedTarget = "";
		for (var i=0; i<What.length; i++) {
			var CurrChar = What.substr(i,1);

// alert(CurrChar.charCodeAt(0));

			if (BadChars.indexOf(CurrChar) == -1) 
				CleanedTarget = CleanedTarget + CurrChar;
		}

		return CleanedTarget;
	}
	
	function GetGloss(RawTarget,CalledBy) {
		var ScriptsFolder = "http://132.236.21.62/rdt/"
		if (GetGloss.arguments.length > 1)
			if (GetGloss.arguments[1] == "PromptList")	// function called from the PromptList window
				var ScriptsFolder = "http://132.236.21.62/rdt/";

		var SearchTarget = WeedPunc(RawTarget); //weed out common punctuation symbols

		// convert SearchTarget into a comma-delimited list of decimal Unicode numbers for each character
		var DecimalNumList = ""; //this will be a string consisting of a list of decimal numbers, Unicode codes for the search target, e.g. "1040,1045,1055,1078"
		for (var i=0; i<SearchTarget.length; i++) {
			DecimalNumList = DecimalNumList + "," + SearchTarget.substr(i, 1).charCodeAt(0);
		}
		DecimalNumList = DecimalNumList.substring(1); //this gets rid of the opening comma

		if ((DecimalNumList!=null) && (DecimalNumList!=""))
			glossary.location.href = ScriptsFolder +"getlisting.cfm?RawTarget=" + escape(RawTarget) + "&NumListTarget=" + DecimalNumList;
	}

	function OpenGrammar(GrammarURL) {
		var OffsetY = screen.height - 200 - 80;
		var LocPrefix = "/grammar/html/";	// relative URL of the Grammar folder
		var FullURL = LocPrefix + GrammarURL
		var winFeatures = "left=0,top=" + OffsetY + ",width=640,height=200,location=no,menubar=no,resizable=yes,status=no,toolbar=no,scrollbars=yes,directories=no";
		GrammarWindow = window.open(FullURL,"CurrGrammarWindow",winFeatures);
		GrammarWindow.focus();
	}

	function OpenPromptList() {
		var winFeatures = "left=0,top=0,width=120,height=200,location=no,menubar=no,resizable=yes,status=no,toolbar=no,scrollbars=yes,directories=no";
		PromptListWindow = window.open("/loras_dialogs/scripts/promptlist_blank.htm","PromptList",winFeatures);
		PromptListWindow.focus();
	}

	function TrimPunc(What) {

	/* strips leading and trailing punctuation */
		var Punc = " `~!@#$%^&*()_-+=|\\{}[]:;\"\<>,.?/'«»";

		while (Punc.indexOf(What.charAt(0)) > -1) What = What.substr(1,What.length-1);
		while (Punc.indexOf(What.charAt(What.length-1)) > -1) What = What.substr(0,What.length-1);
		return What;
	}

	function TrimSp(What) {

	/* strips leading and trailing spaces */
		while (What.charAt(0)==" ") What = What.substr(1,What.length-1);
		while (What.charAt(What.length-1)==" ") What = What.substr(0,What.length-1);
		return What;
	}

	function HiliteButton(Which) {
		// sets all buttons in Grammar frame to default appearance, then sets Which button to Hot
		// note that images 0 and 1 are not buttons
		for (i=2; i<grammar.document.images.length; i++)
			if (grammar.document.images[i].name != "Dot")
				grammar.document.images[i].src = "/loras_dialogs/graphics/pushbutt_up.gif";
		Which.src = "../../graphics/pushbutt_dn.gif";
	}

