/* 
	File:		/js/documentsection.js
	Author:		Mindcast Software
	Copyright:	© 1996-2005  The GCRH Corporation  All rights reserved.
	Notes:		manages the xml display of phrase and graphic selections
*/

///////////////////////////////////////////////////////////////////////////////

var popupWindow = null;


//' this is the function that spawns the phrasesearch window if the user selects the "Phrase Select" button
function section_spawn_phrasesearch(DocId, EntityId, LangId, SecId, SecIdParent)
{
	var noException = true;
	
	//if (sectCurrDisplayLang != '' && sectCurrDisplayLang != 'undefined')
	//	LangId = sectCurrDisplayLang; 
	

	// phrasesearch.asp is in the documents/sections folder - and contains all the info for the user interface off the "Select Phrases" button on the documents sections UI
	var url = 'phrasesearch.asp?docid=' + DocId + '&entityid=' + EntityId + '&langid=' + LangId + '&secid=' + SecId + '&secidparent=' + SecIdParent + '&DisplayLibrary=1&DisplayDataSets=0&DisplayManual=1&DisplaySelectLists=0&DisplayGlobal=0';

	// opens the popup
	popupWindow = section_spawn_phrasesearch_window(url);
	
}

//' opens the popup window
function section_spawn_phrasesearch_window(url)
{
	var name;
	name = 'PhraseSearch';
	popupWindow = open(url, name, 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=600, height=673');
	//popupWindow = open(url, name, 'toolbar=yes, directories=yes, status=yes, menubar=yes, scrollbars=yes, resizable=yes, width=600, height=673');
	popupWindow.moveTo(10, 10);

	return popupWindow;
}

//' spawns the popup window if the Edit button is selected for an individual phrase
function section_spawn_phrasesearch_edit(DocId, EntityId, LangId, SecId, EditPhraseIndex, SecIdParent)
{
	LangId = sectCurrDisplayLang;

	var url = 'phrasesearch.asp?docid=' + DocId + '&entityid=' + EntityId + '&langid=' + LangId + '&secid=' + SecId + '&editphraseindex=' + EditPhraseIndex + '&secidparent=' + SecIdParent;
		url += '&DisplayLibrary=1&DisplayDataSets=0&DisplayManual=1&DisplaySelectLists=0&DisplayGlobal=0';

	//alert(url);
	section_spawn_phrasesearch_window(url);
}

//' opens the small window that captures replacement text - NOTE: THIS IS NO LONGER USED
function section_spawn_replace_items_window(lang)
{
	//popupWindow = open("Replace.asp?lang=" + lang + "", 'Replace', 'toolbar=yes, directories=yes, status=yes, menubar=yes, scrollbars=no, resizable=yes, width=250, height=60');
	popupWindow = open("Replace.asp?lang=" + lang + "", 'Replace', 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, width=250, height=60');
	popupWindow.moveTo(10, 10);
	return popupWindow;
}

 //Global Vars for sections - sect is namespace.
var sectLang_uKey;
var sectCurrDisplayLang;
var sectCurrRemove;
var sectXmlSection;
var sectMode;
var sectCurrSubSection;
var sectParentSection;

function section_xml_load()
{
//\ this is called from javascript at the bottom of the sections.htm page
//\ Gets xml obj loaded and ready.
//\ should get called once per page.	
//\
//\ Remove - Populate the Remove Buttons
//\
	// get a handle to the root of the XML source.
	de = document.all("DOCUMENTSECTION");

	// retrieve a list of nodes named section.
	sectXmlSection = new ActiveXObject("msxml2.DOMDocument");
	sectXmlSection.loadXML( de.selectNodes("//DOCUMENTSECTIONS").item(0).xml );

	var ps = document.all("PAGESTRUCT");
	if (ps == null)
		alert("PAGESTRUCT is empty");
		
	sectCurrDisplayLang = ps.selectNodes("//PAGESTRUCTS/CGI/LangID").item(0).getAttribute("value") * 1;
	sectMode = ps.selectNodes("//PAGESTRUCTS/CGI/mode").item(0).getAttribute("value");

	// have to create new object here in order for some writes to occur at all.
	phrsXmlScratchPad = new ActiveXObject("msxml2.DOMDocument");
	phrsXmlScratchPad.loadXML (	ps.selectNodes("//PAGESTRUCTS/SCRATCHPAD").item(0).xml );

	// set globals
   	sectCurrRemove = ps.selectSingleNode("//PAGESTRUCTS/CGI/Remove").getAttribute("value") * 1;
	//alert("finished section_xml_load");
}



function section_xml_save()
{
//\ this gets called on the form submit.
//\  If there are any data clean-up issues that needs to happen before submission, 
//\  this is the last chance.
//\  DOESN'T ACTUALLY DO THE SAVE!
//alert( sectXmlSection.xml);
	//' set all the Section Element numbers to their final numbers - once for each language
	var langNodeList = sectXmlSection.selectNodes("//DOCUMENTSECTIONS/SECTION/LANGUAGE");
	for (var i=0; i < langNodeList.length; i++)
	{
		elementNodeList = langNodeList.item(i).childNodes;
		for (var j=0; j < elementNodeList.length; j++)
		{
			langNodeList.item(i).setAttribute("Sec_Element_Num", (j + 1));
		}
	}

	//' set all the Section Graphics numbers to their final numbers
	var graphicNodeList = sectXmlSection.selectSingleNode("/DOCUMENTSECTIONS/SECTION/GRAPHICLIST").childNodes;
	for (i = 0; i < graphicNodeList.length; i++)
	{
		graphicNodeList.item(i).setAttribute("Sec_Element_Num", j++);
	}
//alert( sectXmlSection.xml);
	//' put the xml in a submitable location
	document.Edit.section_xml.value = sectXmlSection.xml;
}

//' returns the node if the language and section match 
function section_get_phrase_node(sec, lang, index)
{
			
	var langNodeList = sectXmlSection.selectNodes("//DOCUMENTSECTIONS/SECTION/LANGUAGE");
	
	for (var i=0; i < langNodeList.length; i++)
	{
		if (langNodeList.item(i).getAttribute("Lang_uKey") == lang && langNodeList.item(i).getAttribute("Sec_ID") == sec) {
			return langNodeList.item(i).childNodes(index);
		}
	}
	
	return null;
}

// FIRED WHEN THE AUTHOR SHIFTS BETWEEN DOCUMENT LANGUAGES
function section_xml_new_show_language(LangID)
{
	sectCurrDisplayLang = LangID;
	
	//alert('section_xml_new_show_language');
	// WE ARE REDISPLAYING THE PHRASE LIST HERE BECAUSE THE FUNCTION IS CALLED
	// DIRECTLY FROM THE SECTIONS.HTM WHEN A LANGUAGE IS SELECTED FROM THE SCREEN
	section_xml_display_phrase_list();
}

// FIRED WHEN THE AUTHOR SHIFTS BETWEEN DOCUMENT LANGUAGES
// CYCLES THROUGH ALL THE DIVS ON THE SCREEN AND HIDES EVERYTHING
// THEN TAKES THE DIVS FOR THE CURRENLTY SELECTED LANGUAGE AND REDISPLAYS THEM
function section_xml_msds_show_language(LangID)
{
	//alert("section_xml_msds_show_language");
	sectCurrDisplayLang = LangID;
	var DivBlock = document.getElementsByTagName("div");
	for (var j = 0; j <= DivBlock.length-1 ; j++)
	{	
		var myName = DivBlock[j].id;
		//alert(myName);
		if (myName.search("div_msds_") != -1) 
		{
			DivBlock[j].style.display = 'none';
		}
	}
	
	for (var i=0; i<DivBlock.length; i++)
	{
		var myName = DivBlock[i].id;
		if (myName.search("div_msds_MatCode_") != -1 ) 
		{
			DivBlock["div_msds_MatCode_" + LangID].style.display = 'block';
		}
		if (myName.search("div_msds_MatDesc_") != -1 ) 
		{
			DivBlock["div_msds_MatDesc_" + LangID].style.display = 'block';
		}
		if (myName.search("div_msds_Title_") != -1 ) 
		{
			DivBlock["div_msds_Title_" + LangID].style.display = 'block';
		}
		if (myName.search("div_msds_DraftDate_") != -1 ) 
		{
			DivBlock["div_msds_DraftDate_" + LangID].style.display = 'block';
		}		
		if (myName.search("div_msds_EffDate_") != -1 ) 
		{
			DivBlock["div_msds_EffDate_" + LangID].style.display = 'block';
		}	
		if (myName.search("div_msds_PrintDate_") != -1 ) 
		{
			DivBlock["div_msds_PrintDate_" + LangID].style.display = 'block';
		}		
		if (myName.search("div_msds_Code_") != -1 ) 
		{
			DivBlock["div_msds_Code_" + LangID].style.display = 'block';
		}	
		if (myName.search("div_msds_Company_") != -1 ) 
		{
			DivBlock["div_msds_Company_" + LangID].style.display = 'block';
		}	
		if (myName.search("div_msds_CAS_") != -1 ) 
		{
			DivBlock["div_msds_CAS_" + LangID].style.display = 'block';
		}		
		
		//10/26/05 Irinaadd for showing nfpa and hmis on sec 05 and 15 only for US English language
		if (myName.search("pane_ratings_" + Section05) != -1 ) 
		{
			if(LangID == 1)
			{
				DivBlock["pane_ratings_" + Section05].style.display = 'block';
			}else{
				DivBlock["pane_ratings_" + Section05].style.display = 'none';
			}
		}
		if (myName.search("pane_ratings_" + Section15) != -1 ) 
		{
			if(LangID == 1)
			{
				DivBlock["pane_ratings_" + Section15].style.display = 'block';
			}else{
				DivBlock["pane_ratings_" + Section15].style.display = 'none';
			}
		}
	}
	//alert('section_xml_msds_show_language');
	// WE ARE REDISPLAYING THE PHRASE LIST HERE BECAUSE THE FUNCTION IS CALLED
	// DIRECTLY FROM THE SECTIONS.HTM WHEN A LANGUAGE IS SELECTED FROM THE SCREEN
	section_xml_display_phrase_list();
	
}


function section_xml_display_phrase_list()
{
	//alert("section_xml_display_phrase_list");
	//\ simply refreshes the list of phrases from the xml object.
	//\  looks for sectCurrDisplayLang and sectCurrRemove global vars
	//\  
	var NumSubSections = arrSubSections.length;

	//alert('section_xml_display_phrase_list');
for (var xx=0; xx < arrSubSections.length; xx++)
{
	var L = sectCurrDisplayLang;
	var Remove = sectCurrRemove;
	
	var SubSection = "_" + arrSubSections[xx][0];
	var SubSectionID = arrSubSections[xx][0];
	var SubSectionNoDash = arrSubSections[xx][0];

	var P; //p represents the position in the list
	var CP = 0; //cp represents the number of CAS table phrases in the list
	
	DivBlock["PhraseList" + SubSection].innerHTML = "";

	xmlLangNodeList = sectXmlSection.selectNodes("//DOCUMENTSECTIONS/SECTION/LANGUAGE")
	var docID = sectXmlSection.documentElement.getAttribute("Doc_uKey");
	//alert("docID: " + docID);
	
	var entityID = sectXmlSection.documentElement.getAttribute("Entity_uKey");
	//alert("entityID: " + entityID);
	
	var secID = sectXmlSection.documentElement.childNodes(0).getAttribute("Sec_uKey");
	//alert("secID = " + secID);
	
	var parentSecID = sectXmlSection.documentElement.childNodes(0).getAttribute("Parent_Sec_uKey");
	//alert("parentSecID = " + parentSecID);
	//alert(xmlLangNodeList.length);
	for (var i=0; i < xmlLangNodeList.length; i++)
	{

		// CYCLE THROUGH EACH LANGUAGE IN THE XML UNTIL WE HIT UPON THE ONE THAT IS CURRENTLY BEING DISPLAYED
		if (xmlLangNodeList.item(i).getAttribute("Lang_uKey") == sectCurrDisplayLang)
		{
			var xmlPhraseList = xmlLangNodeList.item(i).childNodes;
			var langSec = "_" + xmlLangNodeList.item(i).getAttribute("Sec_ID");
			
			if (arrSubSections[xx][1] == null) {
				//alert("1")
				DivBlock["LanguageName" + SubSection].innerHTML = xmlLangNodeList.item(i).getAttribute("Lang_Description") + " Document Phrases";
			} else {
				//alert("2")
				DivBlock["LanguageName" + SubSection].innerHTML = "&nbsp;" + arrSubSections[xx][1];
				DivBlock["Languages"].innerHTML = "Document Languages - " + xmlLangNodeList.item(i).getAttribute("Lang_Description"); 
			}
				
			var P;
			//alert("langSec = ::" + langSec + ":: and SubSection = ::" + SubSection + "::");
			if (langSec == SubSection)
			{
				// CYCLE THROUGH EACH PHRASE FOR THE GIVEN LANGUAGE
				//alert("xxxx" + xmlPhraseList.length);
				for (j=0; j < xmlPhraseList.length; j++)
				{
				
					var L = sectCurrDisplayLang;
					var P = j + 1;
					
					// if this is subsection _144 (or section_cas) then we are in CAS table land and have a totally different format
					if (SubSection != '_' + SectionCas) {
						var NewPhraseRec = "<table border=1 width=100% cellspacing=0 cellpadding=0 class=Regular><tr bgcolor=#FFCC66 valign=middle>\n";
						if (P == 1) 
						{
							NewPhraseRec += "<td width=2%>#</td>";
							if (Remove)	
							{ 						
								NewPhraseRec += "<td align=center width=2%>Up</td>";
								NewPhraseRec += "<td align=center width=2%>Dn</td>";
								//NewPhraseRec += "<td align=center width=2%>&nbsp;</td>";
							} else {
								//NewPhraseRec += "<td align=center width=8%>&nbsp;</td>";
							}
								NewPhraseRec += "<td align=center width=10%>Phrase #</td>";
								NewPhraseRec += "<td align=center width=5%>Syn</td>";
								NewPhraseRec += "<td align=center width=2%>&nbsp;</td>";
								NewPhraseRec += "<td align=center width=60%>Phrase</td>";
							if (Remove)	
							{ 						
								//NewPhraseRec += "<td align=center width=2%>&nbsp;</td>";
								//NewPhraseRec += "<td align=center width=2%>&nbsp;</td>";
								NewPhraseRec += "<td align=center width=2%>Del</td>";
							} else {
								NewPhraseRec += "<td align=center width=8%>&nbsp;</td>";
							}
							NewPhraseRec += "</tr><tr bgcolor=#FFCC66 valign=middle>\n";
						}
						
						// Phrase # in Section
						NewPhraseRec +=	"<td align=center width=2%>" + ((P<10)?("0"+P):P) + "</td>\n";
						
		
						// Remove Button
						if (Remove) {
							NewPhraseRec += "<td align=center width=2%>" + section_get_up_button(j, SubSectionNoDash) + "</td>\n";
							NewPhraseRec += "<td align=center width=2%>" + section_get_down_button(j, (xmlPhraseList.length - 1), SubSectionNoDash) + "</td>\n";
							//NewPhraseRec += "<td align=center width=2%><input type=button name=L" + L + "P" + P + "_Remove Size=1 Value=\"X\" Style=\"HEIGHT: 15pt; Width: 15pt; Font-size: 10pt; Color=red; Cursor=hand;\" onClick=\"javascript:section_xml_phrase_delete(" + L + "," + Remove + "," + P + "," + SubSectionNoDash + ")\"></td>\n";
						} else {
							//NewPhraseRec += "<td align=center width=8%>&nbsp;</td>";
						} 
						
						if ( xmlPhraseList.item(j).nodeName == 'STANDARDPHRASE')
						{
							// NEED TO FIND OUT IF WE ARE IN EDIT MODE - IF NOT, HIDE INPUT BOXES...
							if ( Remove){ 
								NewPhraseRec += "<td align=center width=10%><span Class=Phrases_NotSoTiny_Box Id=L" + L + "Pnew_PhraseNum Name=L" + L + "P" + P + "_PhraseNum><input name=newPhraseNumber_" + P + SubSection + " size=14 onBlur=\"javascript:section_xml_phrase_code_onchange_list(this.value, this.name);\" value=\"" + xmlPhraseList.item(j).getAttribute("Lib_Phrase_Code") + "\"></span></td>\n";
								NewPhraseRec += "<td align=center nowrap width=5%><span Class=Phrases_Tiny_Box Id=L" + L + "P" + P + "_Syn Name=L" + L + "P" + P + "_Syn><input name=newPhraseSyn_" + P + SubSection + " size=3 value=\"" + xmlPhraseList.item(j).getAttribute("Phrase_Syn") + "\" onBlur=\"javascript:section_xml_phrase_syn_onchange_list(this.value, this.name);\"></span></td>\n";
							} else {
								NewPhraseRec += "<td align=center width=10%><span Class=Phrases_NotSoTiny_Box Id=L" + L + "Pnew_PhraseNum Name=L" + L + "P" + P + "_PhraseNum><input disabled name=newPhraseNumber_" + P + SubSection + " size=14 onBlur=\"javascript:section_xml_phrase_code_onchange_list(this.value, this.name);\" value=\"" + xmlPhraseList.item(j).getAttribute("Lib_Phrase_Code") + "\"></span></td>\n";
								NewPhraseRec += "<td align=center nowrap width=5%><span Class=Phrases_Tiny_Box Id=L" + L + "P" + P + "_Syn Name=L" + L + "P" + P + "_Syn><input disabled name=newPhraseSyn_" + P + SubSection + " size=3 value=\"" + xmlPhraseList.item(j).getAttribute("Phrase_Syn") + "\" onBlur=\"javascript:section_xml_phrase_syn_onchange_list(this.value, this.name);\"></span></td>\n";
							}
							if ( Remove )
							{
								NewPhraseRec += '<td align=center width=2%>&nbsp;<input Type="Button" class=button-sm style="width: 40px;" Name="EditPhraseButton" Id="EditPhraseButton" Value="Edit" onmouseover="this.style.backgroundColor=\'#3399ff\';" onmouseout="this.style.backgroundColor=\'#3a69a6\';" OnClick="javascript:section_spawn_phrasesearch_edit(' + docID + ', ' + entityID + ', ' + L + ', ' + SubSectionNoDash + ', ' + j + ', ' + parentSecID + ');">&nbsp;</td>';
							}
							else
							{
								NewPhraseRec += '<td align=center width=3%>&nbsp</td>';
							}
						}
						else
						{
							//alert(xmlPhraseList.item(j).nodeName);
							// HIDE THE TWO COLS BECAUSE THEY AREN'T RELEVENT
							if ( xmlPhraseList.item(j).nodeName == 'AGGREGATEPHRASE')
							{
							NewPhraseRec += "<td align=center width=20%><span Class=Phrases_NotSoTiny_Box style='background-color: #ffcc66; border: none;' Id=L" + L + "P" + P + "_PhraseNum Name=L" + L + "P" + P + "_PhraseNum>Aggregate</span></td>\n";
							} else {
							NewPhraseRec += "<td align=center width=20%><span Class=Phrases_NotSoTiny_Box style='background-color: #ffcc66; border: none;' Id=L" + L + "P" + P + "_PhraseNum Name=L" + L + "P" + P + "_PhraseNum>Freetext</span></td>\n";
							}
							
							NewPhraseRec += "<td align=center width=10%><span Class=Phrases_Tiny_Box style='background-color: #ffcc66; border: none;' Id=L" + L + "P" + P + "_Syn Name=L" + L + "P" + P + "_Syn></span></td>\n"

							var testtext = xmlPhraseList.item(j).xml.substring(xmlPhraseList.item(j).xml.length-39,xmlPhraseList.item(j).xml.length);
							
							if ( Remove)
							{	
								// we don't want to show the edit box if the user didn't select to display the text in this language
								if (testtext != 'Language Specific Phrase</MANUALPHRASE>'){
									NewPhraseRec += '<td align=center width=3%>&nbsp;<input Type="Button" class=button-sm style="width= 40px;" Name="EditPhraseButton" Id="EditPhraseButton" Value="Edit" onmouseover="this.style.backgroundColor=\'#3399ff\';" onmouseout="this.style.backgroundColor=\'#3a69a6\';" OnClick="javascript:section_spawn_phrasesearch_edit(' + docID + ', ' + entityID + ', ' + L + ', ' + SubSectionNoDash + ', ' + j + ', ' + parentSecID + ');">&nbsp;</td>';
								} else {
									NewPhraseRec += '<td align=center width=3%>&nbsp</td>';
								}
							}
							else
							{
								NewPhraseRec += '<td align=center width=3%>&nbsp</td>';
							}
						}
										
						var thetext = xmlPhraseList.item(j).xml;

						thetext = thetext.replace(/<\!\[CDATA\[/g, '');
						thetext = thetext.replace(/\]\]\>/g, '');
		 				
						NewPhraseRec += "<td align=center valign=middle width=60% valign=middle>"
		 				NewPhraseRec += "<Span Class=Phrases_Small_Box style='HEIGHT: AUTO !IMPORTANT; width: 500px;valign:middle;' Id=L" + L + "P" + P + "F5 Name=L" + L + "P" + P + "_TEXT>" + thetext + "</span></td>";
		 				
					} else { // CAS-land -- BK
						var CP = CP + 1;
						// Remove Button
						var NewPhraseRec = "<table border=0 width=100% cellspacing=0 cellpadding=0 class=Regular>\n";
						
						if (Remove)
							{
						
								NewPhraseRec += "<tr bgcolor=#FFCC66>";
								NewPhraseRec += "<td align=center nowrap width=5%>DEL</td>";
								NewPhraseRec += "<td align=center nowrap width=15%>CAS #</td>";
								NewPhraseRec += "<td align=center nowrap width=10%>Syn</td>";

								NewPhraseRec += "<td align=center nowrap width=55%>Chemical Identity</td>";
								NewPhraseRec += "<td align=center nowrap width=15%>% by Wt</td></tr>";
							}
						else
							{
								NewPhraseRec += "<tr bgcolor=#FFCC66>";
								NewPhraseRec += "<td align=center nowrap width=15%>CAS #</td>";
								NewPhraseRec += "<td align=center nowrap width=10%>Syn</td>";

								NewPhraseRec += "<td align=center nowrap width=60%>Chemical Identity</td>";
								NewPhraseRec += "<td align=center nowrap width=15%>% by Wt</td></tr>";
							}								
								
						if (Remove)
							{ NewPhraseRec += "<tr><td align=center width=5%><input type=button name=L" + L + "P" + P + "_Remove Size=1 Value=\"X\" Style=\"HEIGHT: 15pt; Font-size: 10pt; Color=red; Cursor=hand;\" onClick=\"javascript:section_xml_phrase_delete(" + L + "," + Remove + "," + P + "," + SubSectionNoDash + ")\"></td>\n";
						}
						else
						{ NewPhraseRec += "<tr>" };
						
						
						if ( xmlPhraseList.item(j).nodeName == 'STANDARDPHRASE')
						{
							// NEED TO FIND OUT IF WE ARE IN EDIT MODE - IF NOT, HIDE INPUT BOXES...
							if ( Remove){ 
								NewPhraseRec += "<td align=center width=15%><span Class=Phrases_NotSoTiny_Box_NoWidth Id=L" + L + "Pnew_PhraseNum Name=L" + L + "P" + P + "_PhraseNum><input name=newPhraseNumber_" + P + SubSection + " size=8 onBlur=\"javascript:section_xml_phrase_code_onchange_list(this.value, this.name);\" value=\"" + xmlPhraseList.item(j).getAttribute("Lib_Phrase_Code") + "\"></span></td>\n";
								NewPhraseRec += "<td align=center width=10%><span Class=Phrases_Tiny_Box_NoWidth Id=L" + L + "P" + P + "_Syn Name=L" + L + "P" + P + "_Syn><input name=newPhraseSyn_" + P + SubSection + " size=1 value=\"" + xmlPhraseList.item(j).getAttribute("Phrase_Syn") + "\" onBlur=\"javascript:section_xml_phrase_syn_onchange_list(this.value, this.name);\"></span></td>\n";
							} else {
								NewPhraseRec += "<td align=center width=15%><span Class=Phrases_NotSoTiny_Box_NoWidth Id=L" + L + "Pnew_PhraseNum Name=L" + L + "P" + P + "_PhraseNum><input disabled name=newPhraseNumber_" + P + SubSection + " size=8 onBlur=\"javascript:section_xml_phrase_code_onchange_list(this.value, this.name);\" value=\"" + xmlPhraseList.item(j).getAttribute("Lib_Phrase_Code") + "\"></span></td>\n";
								NewPhraseRec += "<td align=center width=10%><span Class=Phrases_Tiny_Box_NoWidth Id=L" + L + "P" + P + "_Syn Name=L" + L + "P" + P + "_Syn><input disabled name=newPhraseSyn_" + P + SubSection + " size=1 value=\"" + xmlPhraseList.item(j).getAttribute("Phrase_Syn") + "\" onBlur=\"javascript:section_xml_phrase_syn_onchange_list(this.value, this.name);\"></span></td>\n";
							}
						}
						else
						{
							NewPhraseRec += "<td align=center width=15%>&nbsp;</td>\n"
							NewPhraseRec += "<td align=center width=10%>&nbsp;</td>\n"
							var testtext = xmlPhraseList.item(j).xml.substring(xmlPhraseList.item(j).xml.length-39,xmlPhraseList.item(j).xml.length);
						}
										
						var thetext = xmlPhraseList.item(j).xml;
						// rid ourselves of the filthy CDATA nodes
						thetext = thetext.replace(/<\!\[CDATA\[/g, '');
						thetext = thetext.replace(/\]\]\>/g, '');
						
						// the zzzzs are there to handle the ampersand character
		 				thetext = replaceString("zzzz","&",thetext);
		 				
						NewPhraseRec += "<td align=center width=60% valign=middle ><input type=hidden name=phrase_" + P + " value='" + xmlPhraseList.item(j).text + "'><input type=hidden name=lib_phrase_code_" + P + " value='" + xmlPhraseList.item(j).getAttribute("Lib_Phrase_Code") + "'>";
						
					
						if (arrSec02Phrases.length > P-1) {
							
							for(k=0; k<arrLang.length; k++)
							{
								NewPhraseRec += "<input type=hidden name=ExpLimits_" + P  + "_" + arrLang[k] + " value=''>";
								NewPhraseRec += "<input type=hidden name=ToxLimits_" + P + "_" + arrLang[k] + " value=''>";
								NewPhraseRec += "<input type=hidden name=EcoToxLimits_" + P + "_" + arrLang[k] + " value=''>";
							}
							
						} else {
							for(k=0; k<arrLang.length; k++)
							{
								NewPhraseRec += "<input type=hidden name=ExpLimits_" + P + "_" + arrLang[k] + " value=''>"
								NewPhraseRec += "<input type=hidden name=ToxLimits_" + P + "_" + arrLang[k] + " value=''>";
								NewPhraseRec += "<input type=hidden name=EcoToxLimits_" + P + "_" + arrLang[k] + " value=''>";
							}
						}
						
		 				NewPhraseRec += "<Span Class=Phrases_Small_Box_NoWidth Id=L" + L + "P" + P + "F5 Name=L" + L + "P" + P + "_TEXT>" + thetext + "</span></td>\n";
						
		 				if (arrSec02Phrases.length > P-1) {
		 					//alert(arrSec02Table.length);
							NewPhraseRec += "<td align=center width=5%><span Class=Phrases_Tiny_Box_NoWidth Id=L" + L + "P" + P + "_WT Name=L" + L + "P" + P + "_WT>";
							for(k=0; k<arrLang.length; k++)
							{
								if(L == arrLang[k])
								{
									NewPhraseRec += "<div id=WT_" + P + "_" + arrLang[k] + " style='display:block;'>";
								}else{
									NewPhraseRec += "<div id=WT_" + P + "_" + arrLang[k] + " style='display:none;'>";
								}
								
								NewPhraseRec += "<input name=WT_" + P + "_" + arrLang[k] + " size=6 value=\"";
								
								if(typeof(arrSec02Phrases[P-1][k][0]) != "undefined")
									NewPhraseRec += arrSec02Phrases[P-1][k][0];
								
								NewPhraseRec +=  "\" onBlur=\"changeVals(\'WT\'," + P + ",this.value," + k + "," + arrLang[k]+ ", 1)\"";
								
								//when we aren't in edit mode, the input must be disabled
								if (!Remove)	
									NewPhraseRec += " DISABLED ";
									
								NewPhraseRec += "></div>";
							}
							NewPhraseRec += "</span>";
							NewPhraseRec += "</td>\n"

						} else {
							
							
							NewPhraseRec += "<td align=center width=25%><span Class=Phrases_Tiny_Box_NoWidth Id=L" + L + "P" + P + "_WT Name=L" + L + "P" + P + "_WT>";
							for(k=0; k<arrLang.length; k++)
							{
								if(L == arrLang[k])
								{
									NewPhraseRec += "<div id=WT_" + P + "_" + arrLang[k] + " style='display:block;'>";
								}else{
									NewPhraseRec += "<div id=WT_" + P + "_" + arrLang[k] + " style='display:none;'>";
								}
								NewPhraseRec += "<input name=WT_" + P + "_" + arrLang[k] + " size=6 value=\"\" onBlur=\"changeVals(\'WT\'," + P + ",this.value," + k + "," + arrLang[k]+ ", 1)\" ";
								
								//when we aren't in edit mode, the input must be disabled
								if (!Remove)	
									NewPhraseRec += " DISABLED ";
									
								NewPhraseRec += "></div>";
							}
							NewPhraseRec += "</span>";
							NewPhraseRec += "</td>\n"
						}
					}
					
					
				// Remove Button
						if (Remove) {
							
							NewPhraseRec += "<td align=center width=2%><input type=button name=L" + L + "P" + P + "_Remove Size=1 Value=\"X\" Style=\"HEIGHT: 15pt; Width: 15pt; Font-size: 10pt; Color=red; Cursor=hand;\" onClick=\"javascript:section_xml_phrase_delete(" + L + "," + Remove + "," + P + "," + SubSectionNoDash + ")\"></td>\n";
						} else {
							NewPhraseRec += "<td align=center width=8%>&nbsp;</td>";
						} 
					NewPhraseRec += "</tr></table>";
					//alert(NewPhraseRec);
					DivBlock["PhraseList" + SubSection].insertAdjacentHTML("BeforeEnd",NewPhraseRec);
				}
				
				//add one more phrase for new inserts - remove is show buttons, like "edit view"
				if (Remove) {
				//section_add_phrase_new(L, P);
					var NewPhraseRec = "<table border=0 width=100% cellspacing=0 cellpadding=0 class=Regular><tr bgcolor=#FFCC66>\n<br><hr size=1><br>";

					// Phrase # in Section
					NewPhraseRec +=	"<td align=center width=3%>&nbsp;</td>"
				
					// Remove Button column
						NewPhraseRec += "<td align=center width=15%>Add New Entry</td>\n";
				
					NewPhraseRec += "<td align=center width=20%><span Class=Phrases_Tiny_Box_NoWidth Id=L" + L + "Pnew_PhraseNum Name=L" + L + "P" + P + "_PhraseNum><input name=newPhraseNumber" + SubSection + " size=14 onBlur=\"javascript:section_xml_phrase_code_onchange(this.value, this.name);\"></span></td>\n";
					NewPhraseRec += "<td align=center width=5%><div id='syn_list" + SubSection + "' Style='Display: inline;'></div></td>";
					NewPhraseRec += "<td align=center width=3%>&nbsp;</td>";
				
					NewPhraseRec += "<td align=center width=60%>&nbsp;</td>\n</tr></table>"
					//alert(NewPhraseRec);
					DivBlock["PhraseList" + SubSection].insertAdjacentHTML("BeforeEnd",NewPhraseRec);
				}
				
				if (CP > 0) {
					var NewPhraseRecCount = "<input type=hidden name=phrase_count value=" + CP + ">"
					DivBlock["PhraseList" + SubSection].insertAdjacentHTML("BeforeEnd",NewPhraseRecCount);
				}
			}	

		}
	}

   }
  
}


function section_xml_show_language(LangID)
{
	sectCurrDisplayLang = LangID;
	//alert('section_xml_show_language');
	// WE ARE REDISPLAYING THE PHRASE LIST HERE BECAUSE THE FUNCTION IS CALLED
	// DIRECTLY FROM THE SECTIONS.HTM WHEN A LANGUAGE IS SELECTED FROM THE SCREEN
	section_xml_display_phrase_list();
}

function section_xml_display_pod_phrases_donotuse()
{
//\ simply refreshes the list of phrases from the xml object.
//\  looks for sectCurrDisplayLang and sectCurrRemove global vars
//\  
	var NumSubSections = arrSubSections.length;


	
	for (var xx=0; xx < arrSubSections.length; xx++)
	{
		var L = sectCurrDisplayLang;
		var Remove = sectCurrRemove;
		var P; //p represents the position in the list
		var SubSection = "_" + arrSubSections[xx][0];
		var SubSectionID = arrSubSections[xx][0];
		var SubSectionNoDash = arrSubSections[xx][0];
				
		var SpanBlock = document.getElementsByTagName("span");
	
		for (var j = 0; j <= SpanBlock.length-1 ; j++)
		{	
			var myName = SpanBlock[j].id;
			
			if (myName.search("_DisplayOnly_") != -1) {
				//alert(myName);
				SpanBlock[j].innerHTML = "";
			}
		}
	
		xmlLangNodeList = sectXmlSection.selectNodes("//DOCUMENTSECTIONS/SECTION/LANGUAGE");
		var docID = sectXmlSection.documentElement.getAttribute("Doc_uKey");
		//alert("docID: " + docID);
		
		var entityID = sectXmlSection.documentElement.getAttribute("Entity_uKey");
		//alert("entityID: " + entityID);
		
		var secID = sectXmlSection.documentElement.childNodes(0).getAttribute("Sec_uKey");
		//alert("secID = " + secID);
		
		var parentSecID = sectXmlSection.documentElement.childNodes(0).getAttribute("Parent_Sec_uKey");
		//alert("parentSecID = " + parentSecID);
		
		// this will tell us where to put the information
		var CM_Phrase_Type = sectXmlSection.documentElement.childNodes(0).getAttribute("CM_Phrase_Type");
		
		if (CM_Phrase_Type != "")
		{
			for (var i=0; i < xmlLangNodeList.length; i++)
			{
		
				// CYCLE THROUGH EACH LANGUAGE IN THE XML UNTIL WE HIT UPON THE ONE THAT IS CURRENTLY BEING DISPLAYED
				if (xmlLangNodeList.item(i).getAttribute("Lang_uKey") == sectCurrDisplayLang)
				{
					var xmlPhraseList = xmlLangNodeList.item(i).childNodes;
					var langSec = "_" + xmlLangNodeList.item(i).getAttribute("Sec_ID");
					DivBlock["LanguageName"].innerHTML = xmlLangNodeList.item(i).getAttribute("Lang_Description") + " Document Phrases";
						
					// CYCLE THROUGH EACH PHRASE FOR THE GIVEN LANGUAGE
					for (j=0; j < xmlPhraseList.length; j++)
					{															
							var thetext = xmlPhraseList.item(j).xml;
							thetext = thetext.replace(/<\!\[CDATA\[/g, '');
							thetext = thetext.replace(/\]\]\>/g, '');
							SpanBlock[CM_Phrase_Type + '_DisplayOnly_' + j + '_' + sectCurrDisplayLang].insertHTML(thetext);
					}	// for each phrase			
		
				} // if we are in the currently displayed language
			
		   } // for each language
	  	} // if in CM
	}

}

var sxpa_lib_phrase_code;
var sxpa_lib_graphic_code;
var lastAddedNode;

//' called when an author uses the "Quick Entry" method of entering a phrase directly into the "New Entry" text box
//' populates the Syn drop down with all available Syns for that phrase - 
function section_xml_phrase_code_onchange(Phrase_Code, Field_Name)
{
	if (Phrase_Code != '') {
		sxpa_lib_phrase_code = Phrase_Code;
			
		var DivBlock = document.getElementsByTagName("div");
		var results = Field_Name.split("_");
		var newPhrase = new ActiveXObject("msxml2.DOMDocument");
		var getXML
		var xmlurl
		
		newPhrase.async = false;
		//alert(SERVER_NAME + "/MassUpdateWizard/syn_quick_search.asp?Lib_Phrase_Code=" + sxpa_lib_phrase_code + "&Loc=" + results[1] + "&LangID=" + sectCurrDisplayLang);
		xmlurl = SERVER_NAME + "/MassUpdateWizard/syn_quick_search.asp?Lib_Phrase_Code=" + sxpa_lib_phrase_code + "&Loc=" + results[1] + "&LangID=" + sectCurrDisplayLang;
		getXML = new ActiveXObject("Msxml2.XMLHTTP.3.0");
       	getXML.open ("GET",xmlurl,false);
       	getXML.send();
		newPhrase.load(getXML.responseXML);

		var oxmlPhrase = newPhrase.selectSingleNode("//PHRASELIST");
		if (null == oxmlPhrase)
		{
			alert("Could not find Phrase in this language:" + sxpa_lib_phrase_code);
			eval("document.Edit." + Field_Name + ".value = '';");
			eval("document.Edit." + Field_Name + ".focus();");
			eval("document.Edit." + Field_Name + ".select();");
			DivBlock["syn_list_" + results[1]].innerHTML = "";
		}
		else
		{
			myPhrase1 = oxmlPhrase.xml;
			DivBlock["syn_list_" + results[1]].innerHTML = myPhrase1;
			eval("document.Edit.newPhraseSyn_" + results[1] + ".focus();");
		}
		
	}
}


//' called when an author uses the "Quick Entry" method of entering a graphic directly into the "New Entry" text box
//' populates the Syn drop down with all available Syns for that graphic - 
function section_xml_graphic_code_onchange(Graphic_Code)
{
	if (Graphic_Code != '') {
		sxpa_lib_graphic_code = Graphic_Code;
			
		var DivBlock = document.getElementsByTagName("div");
		var newGraphic = new ActiveXObject("msxml2.DOMDocument");
		var xmlurl = SERVER_NAME + "/MassUpdateWizard/graphic_syn_quick_search.asp?Lib_Graphic_Code=" + sxpa_lib_graphic_code;
		newGraphic.async = false;
		var getXML;
		
	    getXML = new ActiveXObject("Msxml2.XMLHTTP.3.0");
        getXML.open ("GET",xmlurl,false);
        getXML.send();
		
		newGraphic.load(getXML.responseXML);
		
		var oxmlGraphic = newGraphic.selectSingleNode("//GRAPHICLIST");
		if (null == oxmlGraphic)
		{
			alert("Could not find GRAPHIC:" + sxpa_lib_graphic_code);
			DivBlock["graphic_syn_list"].innerHTML = "";
		}
		else
		{
			myGraphic1 = oxmlGraphic.xml;
			DivBlock["graphic_syn_list"].innerHTML = myGraphic1;
			document.Edit.newGraphicSyn.focus();
		}
		
	}
	grphcCode = Graphic_Code;
}

function section_xml_phrase_code_onchange_list(Phrase_Code, Field_Name)
{
	//' for now we are just storing the newly entered phrase_code....
	//' TBD-SA - IF WE ARE GOING TO INCLUDE CODE THAT WILL DYNAMICALLY POPULATE A SYN DROP DOWN UPON DATA ENTRY - IT WILL GO HERE
	sxpa_lib_phrase_code = Phrase_Code;
	//alert(sxpa_lib_phrase_code);
}

//' called from the "Quick Entry" method - when a Syn is selected from the drop down of available syns
//' NOTE: SYN ACTUALLY HOLDS TWO PIECES OF INFORMATION - THE SYN | THE LIBRARY UKEY
function section_xml_phrase_syn_onchange(Phrase_Syn, Field_Name)
{
	//alert('section_xml_phrase_syn_onchange');
	
	var newPhrase = new ActiveXObject("msxml2.DOMDocument");
	newPhrase.async = false;

	var ps = document.all("PAGESTRUCT").XMLDocument;
	var DocID = ps.selectSingleNode("//PAGESTRUCTS/CGI/DocID").getAttribute("value");	
	
	var results = Field_Name.split("_");
	//alert(SERVER_NAME + "/Documents/Sections/phrase_quick_add.asp?Lib_Phrase_Code=" + sxpa_lib_phrase_code + "&Phrase_Syn=" + Phrase_Syn + "&LangID=" + sectCurrDisplayLang);

		var getXML
		var xmlurl
		
		newPhrase.async = false;
		xmlurl = SERVER_NAME + "/Documents/Sections/phrase_quick_add.asp?Lib_Phrase_Code=" + sxpa_lib_phrase_code + "&Phrase_Syn=" + Phrase_Syn + "&LangID=" + sectCurrDisplayLang;
		getXML = new ActiveXObject("Msxml2.XMLHTTP.3.0");
       	getXML.open ("GET",xmlurl,false);
       	getXML.send();
       	//alert("1")
		//alert(xmlurl);
		//alert(getXML.responseXML.xml);		
		newPhrase.load(getXML.responseXML);


	var oxmlPhrase = newPhrase.selectSingleNode("//PHRASELIST/STANDARDPHRASE");
	
	if (null == oxmlPhrase)
	{
		alert("Could not find Phrase");
		eval("document.Edit.newPhraseNumber_" + results[1] + ".focus();");
		eval("document.Edit.newPhraseNumber_" + results[1] + ".select();");
	}
	else if (section_replacement_items_needed(oxmlPhrase)) {
		alert("The selected phrase requires replacement variable text to be entered. Please use the 'Select Phrases' button and enter this phrase from the screen provided."); 
		eval("document.Edit.newPhraseNumber_" + results[1] + ".value = '';");
		eval("document.Edit.newPhraseNumber_" + results[1] + ".focus();");
		eval("document.Edit.newPhraseNumber_" + results[1] + ".select();");
		var DivBlock = document.getElementsByTagName("div");
		DivBlock["syn_list_" + results[1]].innerHTML = "";
	} 
	else
	{

		var PhraseSearch_LanguageList;
		PhraseSearch_LanguageList = new ActiveXObject("msxml2.DOMDocument");
		PhraseSearch_LanguageList.loadXML ( section_xml_get_language_list() );
		if (null == PhraseSearch_LanguageList )
			alert(" could not get language list ");


		// load up translated versions of this phrase
		phrsTransList = new ActiveXObject("msxml2.DOMDocument");
		phrsTransList.async = false;
		//alert(SERVER_NAME + "/Documents/Sections/phrase_getTranslated.asp?DocID=" + DocID  + "&Phrase_Syn=" + Phrase_Syn + "&Lib_Phrase_uKey=" + oxmlPhrase.getAttribute("Lib_Phrase_uKey")) ;
		var xmlurl = SERVER_NAME + "/Documents/Sections/phrase_getTranslated.asp?DocID=" + DocID + "&Phrase_Syn=" + Phrase_Syn + "&Lib_Phrase_uKey=" + oxmlPhrase.getAttribute("Lib_Phrase_uKey")  ;
		var getXML;
		
		getXML = new ActiveXObject("Msxml2.XMLHTTP.3.0");
       	getXML.open ("GET",xmlurl,false);
       	getXML.send();
		//alert("2")
		//alert(xmlurl);
		//alert(getXML.responseXML.xml);		
		phrsTransList.load(getXML.responseXML);


		newNode = new ActiveXObject("msxml2.DOMDocument");

		var langNodeList = PhraseSearch_LanguageList.selectNodes("//LANGUAGES/LANG");
		
		for (var j = 0; j < langNodeList.length; j++ )
		{
			//alert("current language is:" + langNodeList.item(j).getAttribute("lang_uKey"));
			if ( langNodeList.item(j).getAttribute("lang_uKey") == sectCurrDisplayLang )
			{		
				//alert("Im the current phrase");
				section_xml_phrase_append_node(oxmlPhrase, sectCurrDisplayLang,results[1]);
			}
			else
			{
				transNodeList = phrsTransList.selectNodes("//RESULTS/*");
				newNode.loadXML("<BLANK/>");
				var ExactMatch = 0;
				for (var k = 0; k < transNodeList.length; k++)
				{
					//algorhythm, take either the one with the same syn code, next, take the first one.
					if (transNodeList.item(k).getAttribute("Lang_uKey") == langNodeList.item(j).getAttribute("lang_uKey"))
					{
						if (transNodeList.item(k).getAttribute("Phrase_Syn") == oxmlPhrase.getAttribute("Phrase_Syn"))
						{
							//alert("syns match for lang " + transNodeList.item(k).getAttribute("Lang_uKey"));
							newNode.loadXML(transNodeList.item(k).xml);
							ExactMatch++;
						} else {
							//alert("syns don't match for lang " + transNodeList.item(k).getAttribute("Lang_uKey"));
						}
					}
				}
				if (ExactMatch == 0)
				{
					var MP_phrase_ukey = oxmlPhrase.getAttribute("Phrase_Text_uKey")
					var MP_library_ukey = oxmlPhrase.getAttribute("Library_uKey")
					alert("Warning: Could not find phrase translation!\nPhrase: " + sxpa_lib_phrase_code + "\nLanguage: " + langNodeList.item(j).getAttribute("Lang_Abv") );				
					newNode.loadXML('<MANUALPHRASE Doc_Phrase_uKey="?" Sec_Phrase_Num="?" Phrase_Text_uKey="' + MP_phrase_ukey + '" Translation_Request_Status="None" Library_uKey="' + MP_library_ukey + '">No translation for phrase: ' + sxpa_lib_phrase_code + ' Syn: ' + Phrase_Syn + '</MANUALPHRASE>');
					
				}
				
			
				//when called from the new text entry area - we should append
				section_xml_phrase_append_node(newNode.documentElement, langNodeList.item(j).getAttribute("lang_uKey"),results[1]);
			}//end else
		} //endfor
		
		document.displaySection.revised.value = 'yes';

	}
}


//' called when the user changes the syn in an existing phrase and hits tab key
function section_xml_phrase_syn_onchange_list(Phrase_Syn, Field_Name)
{
		//alert('section_xml_phrase_syn_onchange_list');
	var newPhrase = new ActiveXObject("msxml2.DOMDocument");
	var results = Field_Name.split("_");
	var ps = document.all("PAGESTRUCT").XMLDocument;
	var DocID = ps.selectSingleNode("//PAGESTRUCTS/CGI/DocID").getAttribute("value");	

	sxpa_lib_phrase_code = eval('document.Edit.newPhraseNumber_' + results[1] + '_' + results[2] + '.value;');
	
	newPhrase.async = false;
	
	
		var xmlurl = SERVER_NAME + "/Documents/Sections/phrase_quick_add.asp?Lib_Phrase_Code=" + sxpa_lib_phrase_code + "&Phrase_Syn=" + Phrase_Syn + "&LangID=" + sectCurrDisplayLang;
		var getXML = new ActiveXObject("Msxml2.XMLHTTP.3.0");
       	getXML.open ("GET",xmlurl,false);
       	getXML.send();
	
		newPhrase.load(getXML.responseXML);
	
	
	var oxmlPhrase = newPhrase.selectSingleNode("//PHRASELIST/STANDARDPHRASE");
	//alert("|" & oxmlPhrase.text & "|");
	if (null == oxmlPhrase)
	{
		alert("Could not find Phrase");
		// PUT FOCUS BACK ON THE EDITED FIELD
		eval('document.Edit.newPhraseNumber_' + results[1] + '_' + results[2] + '.focus();')
		eval('document.Edit.newPhraseNumber_' + results[1] + '_' + results[2] + '.select();')
		
		// PUT THE OLD PHRASE BACK!
		section_xml_display_phrase_list();
	}
	else if ("TWO LIBRARY MATCH" == oxmlPhrase.text)
	{
		alert("The phrase/syn combination you entered was found in both libraries and therefore can't be quick entered in-line. Please use the Add New Entry box below to make your selection and then use the up/down arrow keys to position your new entry correctly.");
		// PUT THE OLD PHRASE BACK!
		section_xml_display_phrase_list();
	}
	else if (section_replacement_items_needed(oxmlPhrase)) 
	{
		alert("The selected phrase requires replacement variable text to be entered. Please use the 'Select Phrases' button and enter this phrase from the screen provided."); 
		eval('document.Edit.newPhraseNumber_' + results[1] + '_' + results[2] + '.focus();')
		eval('document.Edit.newPhraseNumber_' + results[1] + '_' + results[2] + '.select();')
		
		// PUT THE OLD PHRASE BACK!
		section_xml_display_phrase_list();
	} 
	else
	{
		var PhraseSearch_LanguageList;
		PhraseSearch_LanguageList = new ActiveXObject("msxml2.DOMDocument");
		PhraseSearch_LanguageList.loadXML ( section_xml_get_language_list() );
		if (null == PhraseSearch_LanguageList )
			alert(" could not get language list ");


		// load up translated versions of this phrase
		phrsTransList = new ActiveXObject("msxml2.DOMDocument");
		phrsTransList.async = false;
		
		var xmlurl = SERVER_NAME + "/Documents/Sections/phrase_getTranslated.asp?DocID=" + DocID + "&Phrase_Syn=" + Phrase_Syn +  "&Lib_Phrase_uKey=" + oxmlPhrase.getAttribute("Lib_Phrase_uKey");
		var getXML;
		
		getXML = new ActiveXObject("Msxml2.XMLHTTP.3.0");
       	getXML.open ("GET",xmlurl,false);
       	getXML.send();
	
		phrsTransList.load(getXML.responseXML);

		newNode = new ActiveXObject("msxml2.DOMDocument");

		var langNodeList = PhraseSearch_LanguageList.selectNodes("//LANGUAGES/LANG");
		for (var j = 0; j < langNodeList.length; j++ )
		{
			if ( langNodeList.item(j).getAttribute("lang_uKey") == sectCurrDisplayLang )
			{		
				section_xml_phrase_replace_node(oxmlPhrase, sectCurrDisplayLang, results[1], results[2])
			}
			else
			{
				transNodeList = phrsTransList.selectNodes("//RESULTS/*");
				newNode.loadXML("<BLANK/>");
				var ExactMatch = 0;
				for (var k = 0; k < transNodeList.length; k++)
				{
					//algorhythm, take either the one with the same syn code, next, take the first one.
					if (transNodeList.item(k).getAttribute("Lang_uKey") == langNodeList.item(j).getAttribute("lang_uKey"))
					{
						if (transNodeList.item(k).getAttribute("Phrase_Syn") == oxmlPhrase.getAttribute("Phrase_Syn"))
						{
							
							newNode.loadXML(transNodeList.item(k).xml);
							ExactMatch++;
						}
					}
				}
				if (ExactMatch == 0)
				{
				
					var MP_phrase_ukey = oxmlPhrase.getAttribute("Phrase_Text_uKey")
					var MP_library_ukey = oxmlPhrase.getAttribute("Library_uKey")
					alert("Warning: Could not find phrase translation!\nPhrase: " + sxpa_lib_phrase_code + "\nLanguage: " + langNodeList.item(j).getAttribute("Lang_Abv") );				
					newNode.loadXML('<MANUALPHRASE Doc_Phrase_uKey="?" Sec_Phrase_Num="?" Phrase_Text_uKey="' + MP_phrase_ukey + '" Translation_Request_Status="None" Library_uKey="' + MP_library_ukey + '">No translation for phrase: ' + sxpa_lib_phrase_code + ' Syn: ' + Phrase_Syn + '</MANUALPHRASE>');
				}
			
				//when called from an existing node, we should replace
				section_xml_phrase_replace_node(newNode.documentElement, langNodeList.item(j).getAttribute("lang_uKey"), results[1], results[2])
			}
		}
		document.displaySection.revised.value = 'yes';

	}
}


//' TBD-SA WON'T BE NEEDED IF WE REMOVE THE POPUP FOR REPLACEMENT TEXT
function section_get_last_added_node()
{
	return lastAddedNode;
}

// tests to see if the selected phrase requires replacement text
function section_replacement_items_needed(node)
{
	var status = false;
	var er1 = node.getAttribute("Engage_Replacement_1");
	var er2 = node.getAttribute("Engage_Replacement_2");
	
	if (er1 == 1 || er2 == 1) {
		status = true;
	}
	
	return status;
}

function section_xml_phrase_delete(L, Remove, P, SubSection)
{

	close_Popups();
	
	// delete the phrase
	xmlLangNodeList = sectXmlSection.selectNodes("//DOCUMENTSECTIONS/SECTION/LANGUAGE");
	
	for (var i=0; i < xmlLangNodeList.length; i++)
	{
		var lang_sec = xmlLangNodeList.item(i).getAttribute("Sec_ID")
		if (lang_sec == SubSection) {
			var xmlPhraseList = xmlLangNodeList.item(i).childNodes;
			for (j=0; j < xmlPhraseList.length; j++)
			{
				//alert("P:" + P + "Sec_Phrase_Num:" + xmlPhraseList.item(j).getAttribute("Sec_Element_Num") );
				if (xmlPhraseList.item(j).getAttribute("Sec_Element_Num") == P)
				{
					xmlLangNodeList.item(i).removeChild( xmlPhraseList.item(j) );
					section_xml_phrase_renumber( xmlLangNodeList.item(i) );
				}
			}
		}
	}
	
	//fix the sec02Table for MSDS
	if (SubSection == SectionCas)
	{	
		//alert(arrSec02Phrases.length);
		
		//Irina comment that and add lines before that tofix add/remove element into cas table to reflect in the sameway in limits
		/*
		arrSec02Table.splice(P-1,1);
		//remove phrases content from thw array for the node that was removed
		for(i=0; i<arrLang.length; i++)
			arrSec02Phrases[P-1][i] = '';		*/
		for(i=P; i<arrSec02Phrases.length; i++)
			for(j=0; j<arrLang.length; j++)
				arrSec02Phrases[i][j][2] = eval(arrSec02Phrases[i][j][2] - 1)

		arrSec02Phrases.splice(P-1,1);
	}
	
	//refresh the list
	document.displaySection.revised.value = 'yes';
	section_xml_display_phrase_list(L, Remove);

}


function section_xml_phrase_renumber(parent)
{
	// this renumber all the childeren
	var nodeList = parent.childNodes;
	for (var i=0; i < nodeList.length; i++)
	{
		nodeList.item(i).setAttribute("Sec_Element_Num", i + 1);
	}

}

function section_xml_phrase_append_node(node, lang, subsec)
{
//\ node - xml node to add - should be some sort of phrase
//\ lang - language to add to.
//alert('section_xml_phrase_append_node');
	xmlLangNodeList = sectXmlSection.selectNodes("//DOCUMENTSECTIONS/SECTION/LANGUAGE");
	//alert("lang node length:" & xmlLangNodeList.length);
	for (var i=0; i < xmlLangNodeList.length; i++)
	{

		var Lang_uKey = xmlLangNodeList.item(i).getAttribute("Lang_uKey");
		var SubSec_uKey = xmlLangNodeList.item(i).getAttribute("Sec_ID");
		//alert("lang and subsec:" + Lang_uKey + "--" + lang + "  " + SubSec_uKey + "--" + subsec);
		if (Lang_uKey == lang && SubSec_uKey == subsec)
		{
			//alert("Langnodelist item: " + xmlLangNodeList.item(i).xml);
			xmlLangNodeList.item(i).appendChild(node);
			//alert("child has been appended");
			
			section_xml_phrase_renumber(xmlLangNodeList.item(i) );
			//alert("section has been renumbered");
		}
	}

	//refresh the list
	document.displaySection.revised.value = 'yes';
	
	//add new array that reprezent the content of new node - so now we can store phrases value betwwen cahnges of languages
	if (subsec == SectionCas)
	{
		len = arrSec02Phrases.length;

		arrSec02Phrases[len] = new Array();
		for(i=0; i<arrLang.length; i++)
		{
			arrSec02Phrases[len][i] = new Array('',String(len),String(len));	
		}
	}
		
	//alert("before section_xml_display_phrase_list");
	section_xml_display_phrase_list();
	//alert("after section_xml_display_phrase_list");

}


function section_xml_phrase_replace_pod_node(node, lang, pos, subsec)
{
//\ node - xml node to add - should be some sort of phrase
//\ pos - position to add, inserts before this number.
//\ lang - language where the replacement should happen
//\ subsec - current subsection	xmlLangNodeList = sectXmlSection.selectNodes("//DOCUMENTSECTIONS/SECTION/LANGUAGE");
	for (var i=0; i < xmlLangNodeList.length; i++)
	{

		var Lang_uKey = xmlLangNodeList.item(i).getAttribute("Lang_uKey");
		var SubSec_uKey = xmlLangNodeList.item(i).getAttribute("Sec_ID");
		//alert("lang_ukey: " + Lang_uKey + " == lang: " + lang + " and SubSec:" + SubSec_uKey + " == subsec: " + subsec);
		if (Lang_uKey == lang && SubSec_uKey == subsec)
		{
			var xmlPhraseList = xmlLangNodeList.item(i).childNodes;
			for (j=0; j < xmlPhraseList.length; j++)
			{
				//alert("pos: " + pos + " == secelemetnnum:" + xmlPhraseList.item(j).getAttribute("Sec_Element_Num"));
				if (pos == xmlPhraseList.item(j).getAttribute("Sec_Element_Num"))
				{
					node.setAttribute("Sec_Element_Num", j+1);

					xmlLangNodeList.item(i).replaceChild(node, xmlPhraseList.item(j));
					section_xml_phrase_renumber(xmlLangNodeList.item(i) );
				}
			}
		}
	}
}

function section_xml_phrase_replace_node(node, lang, pos, subsec)
{
//\ node - xml node to add - should be some sort of phrase
//\ pos - position to add, inserts before this number.
//\ lang - language where the replacement should happen
//\ subsec - current subsection

	xmlLangNodeList = sectXmlSection.selectNodes("//DOCUMENTSECTIONS/SECTION/LANGUAGE");
	//alert("my len:" + xmlLangNodeList.length);
	for (var i=0; i < xmlLangNodeList.length; i++)
	{

		var Lang_uKey = xmlLangNodeList.item(i).getAttribute("Lang_uKey");
		var SubSec_uKey = xmlLangNodeList.item(i).getAttribute("Sec_ID");
		//alert("lang_ukey: " + Lang_uKey + " == lang: " + lang + " and SubSec:" + SubSec_uKey + " == subsec: " + subsec);
		if (Lang_uKey == lang && SubSec_uKey == subsec)
		{
			var xmlPhraseList = xmlLangNodeList.item(i).childNodes;
			for (j=0; j < xmlPhraseList.length; j++)
			{
				//alert("pos: " + pos + " == secelemetnnum:" + xmlPhraseList.item(j).getAttribute("Sec_Element_Num"));
				if (pos == xmlPhraseList.item(j).getAttribute("Sec_Element_Num"))
				{
					node.setAttribute("Sec_Element_Num", j+1);

					xmlLangNodeList.item(i).replaceChild(node, xmlPhraseList.item(j));
					section_xml_phrase_renumber(xmlLangNodeList.item(i) );
				}
			}
		}
	}

	//refresh the lista
	document.displaySection.revised.value = 'yes';
	section_xml_display_phrase_list();
}

function section_xml_get_language_list()
{
//\ just used to whip up language list real quick for child window use.
	var retXml;

	retXml = '<LANGUAGES>';

	xmlLangNodeList = sectXmlSection.selectNodes("//DOCUMENTSECTIONS/SECTION/LANGUAGE");

	myString = new String("")

	for (var i=0; i < xmlLangNodeList.length; i++)
	{
		// because msds phrases have subsections there are duplicated language nodes for each of those sections -- we only want to capture the language once, not once per sub section
		// so need to keep an array of already selected lang_uKeys
		// only add to the xml if the lang isn't in the array
		var testString = "#"+xmlLangNodeList.item(i).getAttribute("Lang_uKey")+","
		if (myString.indexOf(testString,0) == -1)
		{
			retXml += '<LANG Lang_Description="' + xmlLangNodeList.item(i).getAttribute("Lang_Description") + '" lang_uKey="' + xmlLangNodeList.item(i).getAttribute("Lang_uKey") + '" Lang_Abv="' + xmlLangNodeList.item(i).getAttribute("Lang_Abv") + '"/>';
			myString += "#"+ xmlLangNodeList.item(i).getAttribute("Lang_uKey") + ","
		}

	}

	retXml += '</LANGUAGES>';
	//alert(retXml);
	return (retXml);
}


function section_xml_get_phrase_count(lang, sec)
{
	xmlLangNodeList = sectXmlSection.selectNodes("//DOCUMENTSECTIONS/SECTION/LANGUAGE");
	for (var i=0; i < xmlLangNodeList.length; i++)
	{
		var Lang_uKey = xmlLangNodeList.item(i).getAttribute("Lang_uKey");
		var Sec_uKey = xmlLangNodeList.item(i).getAttribute("Sec_ID");
//		alert("LU:" + Lang_uKey + "L:" + lang);
		if (Lang_uKey == lang && Sec_uKey == sec)
		{
//			alert("lang=" + lang);
			//phrase count should be the same no matter which language
			//  but we check just the current one inside this loop.
			return( xmlLangNodeList.item(i).childNodes.length );
		}
	}
}


//\-------------------------------------------------------
//\------- Graphics ! ---------------------------------
//\-------------------------------------------------------


function section_xml_show_graphics()
{
//\ performs pane trickery to show the graphics to the user, and hides the phrases
	//alert("ok");
	document.displaySection.mode.value = 'showGraphic';

	pane_phrase_buttons.style.display = "none";
	pane_phrase_languages_discrete.style.display = "none";
	pane_phrase_phrases.style.display = "none";

	pane_graphic_buttons.style.display = "block";
	pane_graphic_graphics.style.display = "block";
}

function section_xml_show_graphics_msds(sec, blnFixed)
{
//\ performs pane trickery to show the graphics to the user, and hides the phrases
	document.displaySection.mode.value = 'showMSDSGraphic';

	pane_phrase_buttons.style.display = "none";
	pane_phrase_languages_discrete.style.display = "none";
	if (blnFixed == 1 ) {
		eval("pane_phrase_fixedphrases_" + sec + ".style.display = 'none';");
	}
	if (sec == Section01 || sec == Section05 || sec == Section15) {
		eval("pane_ratings_" + sec + ".style.display = 'none';");
		eval("pane_hf_" + sec + ".style.display = 'none';");
	}
	if (sec == Section02 ) {
		eval("pane_cas_" + sec + ".style.display = 'none';");
	}
	
	for (var xx=0; xx < arrSubSections.length; xx++)
	{
		eval("pane_phrase_phrases_" + arrSubSections[xx][0] + ".style.display = 'none';");
	}
	
	pane_graphic_buttons.style.display = "block";
	pane_graphic_graphics.style.display = "block";
}

function section_xml_show_phrases_msds(sec, blnFixed)
{
	//alert("section_xml_show_phrases_msds");
//\ performs pane trickery to show the graphics to the user, and hides the phrases
	document.displaySection.mode.value = 'showMSDSPhrase';

	pane_phrase_buttons.style.display = "block";
	pane_phrase_languages_discrete.style.display = "block";
	if (blnFixed == 1 ) {
		eval("pane_phrase_fixedphrases_" + sec + ".style.display = 'block';");
		eval("pane_hf_" + sec + ".style.display = 'block';");
	}

	if (sec == Section01 || sec == Section05 || sec == Section15) {		
		eval("pane_ratings_" + sec + ".style.display = 'block';");
	}
	if (sec == Section02 ) {
		eval("pane_cas_" + sec + ".style.display = 'block';");
	}
	
	for (var xx=0; xx < arrSubSections.length; xx++)
	{
		eval("pane_phrase_phrases_" + arrSubSections[xx][0] + ".style.display = 'block';");
	}
	
	pane_graphic_buttons.style.display = "none";
	pane_graphic_graphics.style.display = "none";
}

function section_xml_show_phrases()
{
//\ performs pane trickery to show the phrases to a user, and hides the graphics.
	document.displaySection.mode.value = 'showPhrase';

	pane_phrase_buttons.style.display = "block";
	pane_phrase_languages.style.display = "block";
	pane_phrase_phrases.style.display = "block";

	pane_graphic_buttons.style.display = "none";
	pane_graphic_graphics.style.display = "none";
}

function section_xml_display()
{
	//alert("in section xml display");
	if (sectMode == "POD")
	{
		section_xml_display_pod_phrases();
	}
	else
	{
		section_xml_display_phrase_list();
		//alert("finished display list");
		section_xml_display_graphics();
		//alert("finsihed display graphics");

		if ( sectMode == 'showGraphic' )
 		{
			section_xml_show_graphics();
		}
	}
}

function documentsubsection_confirm_exit(message, iSecID, displayMsg)
{
	
	//alert(document.Edit.ddlSection(document.Edit.ddlSection.selectedIndex).value);
	var iSubSecID = document.Edit.ddlSection(document.Edit.ddlSection.selectedIndex).value;
	if (popupWindow != null)
			popupWindow.close();
			
	if ( displayMsg && document.displaySection.revised.value == "yes")
	{
			if ( window.confirm(message)) 
			{
				document.displaySection.SecId.value = iSecID;
				document.displaySection.SubSecId.value = iSubSecID;
				document.displaySection.submit();
			}

	} else {
		document.displaySection.SecId.value = iSecID;
		document.displaySection.SubSecId.value = iSubSecID;
		document.displaySection.submit();		
	}
}

function documentnotusedsubsection_confirm_exit(message, iSecID, displayMsg)
{
	
	//alert(document.Edit.ddlSection(document.Edit.ddlNotUsedSection.selectedIndex).value);
	var iSubSecID = document.Edit.ddlNotUsedSection(document.Edit.ddlNotUsedSection.selectedIndex).value;
	if (popupWindow != null)
			popupWindow.close();
			
	if ( displayMsg && document.displaySection.revised.value == "yes")
	{
			if ( window.confirm(message)) 
			{
				document.displaySection.SecId.value = iSecID;
				document.displaySection.SubSecId.value = iSubSecID;
				document.displaySection.submit();
			}

	} else {
		document.displaySection.SecId.value = iSecID;
		document.displaySection.SubSecId.value = iSubSecID;
		document.displaySection.submit();		
	}
}

function documentsection_confirm_exit(message, iSecID, displayMsg)
{
	if (popupWindow != null)
			popupWindow.close();
			
	if ( displayMsg && document.displaySection.revised.value == "yes")
	{
			if ( window.confirm(message)) 
			{
				document.displaySection.SecId.value = iSecID;
				document.displaySection.submit();
			}

	} else {
		document.displaySection.SecId.value = iSecID;
		document.displaySection.submit();		
	}
}

function section_xml_display_graphics()
{
//\ simply refreshes the list of graphics from the xml object.
//\  looks for sectCurrDisplayLang and sectCurrRemove global vars
//\  
	var NumSubSections = arrSubSections.length;

   if (NumSubSections > 0) {

		var DivBlock = document.getElementsByTagName("div");
	
		for (var j = 0; j <= DivBlock.length-1 ; j++)
		{	
			var myName = DivBlock[j].id;
	
			if (myName.search("GraphicList") != -1) {
				var Remove = sectCurrRemove;
				var P;
		
				DivBlock["GraphicList"].innerHTML = ""; //("BeforeEnd",NewGraphicRec);
		
				var xmlGraphicNode = sectXmlSection.selectSingleNode("//DOCUMENTSECTIONS/SECTION/GRAPHICLIST");
				var xmlGraphicNodeList;
		
				var errMsg;
				if (! xmlGraphicNode.getAttribute("ERROR") == "")
				{
					DivBlock["GraphicList"].insertAdjacentHTML("BeforeEnd", "");
					// get rid of error message right away, really should do it when adding a graphic, but then
					//  it would have to be in more than one place.
					xmlGraphicNode.setAttribute("ERROR", "");
				} 
				else
				{
					xmlGraphicNodeList = xmlGraphicNode.childNodes;
										
					for (var i=0; i < xmlGraphicNodeList.length; i++)
					{
						
						
						
						
						P = i+1;
						
						if (P == 1) 
						{
						    var NewGraphicRec = "<br/><table border=0 width=100% cellspacing=0 cellpadding=0 class=Regular><tr bgcolor=#FFCC66>\n";
							NewGraphicRec += "<td width=2%>#</td>";
							if (Remove)	
							{ 						
								NewGraphicRec += "<td align=center width=2%>Up</td>";
								NewGraphicRec += "<td align=center width=2%>Dn</td>";
							}
								NewGraphicRec += "<td align=center width=10%>Graphic Code</td>";
								NewGraphicRec += "<td align=center width=5%>Syn</td>";
								NewGraphicRec += "<td align=center width=40%>Description</td>";
								NewGraphicRec += "<td align=center width=20%>Thumbnail</td>";

							if (Remove)	
							{ 						
								NewGraphicRec += "<td align=center width=2%>Del</td>";
							} else {
								NewGraphicRec += "<td align=center width=8%>&nbsp;</td>";
							}
							NewGraphicRec += "</tr><tr bgcolor=#FFCC66 valign=middle>\n";
						} else {
							NewGraphicRec += "</tr><tr bgcolor=#FFCC66 valign=middle>\n";

						}
						
						// Graphic # in Section
						NewGraphicRec +=	"<td align=center width=2%>" + ((P<10)?("0"+P):P) + "</td>\n";
											
					
						// Remove Button
						if (Remove)
						{
							NewGraphicRec += "<td align=center width=2%>" + section_graphic_get_up_button(i) + "</td>\n";
							NewGraphicRec += "<td align=center width=2%>" + section_graphic_get_down_button(i, xmlGraphicNodeList.length-1 ) + "</td>\n";
						}
					
						var GraphicNode = xmlGraphicNodeList.item(i);		

					
						// Graphic # from Library - Library_Graphics_Text.Lib_Graphic_uKey --> vDoc_Graphics.Graphic_Text_uKey
						NewGraphicRec += "<td align=center width=10%><span Class=Graphics_Tiny_Box Id=P" + P + "_GraphicNum Name=P" + P + "_GraphicNum>" + GraphicNode.getAttribute("Lib_Graphic_Code") + "</span></td>\n"
					
						// Graphic Synonym from Library - Library_Graphics_Text.Graphic_Syn
						NewGraphicRec += "<td align=center width=5%><span Class=Graphics_Tiny_Box Id=P" + P + "_Syn Name=P" + P + "_Syn>" + GraphicNode.getAttribute("Graphic_Syn") + "</span></td>\n"
					
						// Graphic from Library, --> vDoc_Graphics.Graphic
						NewGraphicRec += "<TD align='center' width='40%'>" + GraphicNode.getAttribute("Description") + "</td>";
						NewGraphicRec += "<TD align='center' width='40%'>";
						

						NewGraphicRec += "<a onclick=\"overlib('large format of the image below:<br/><img src=" + GraphicNode.getAttribute("Fullsize_URL") + " alt=large />','1', HAUTO,VAUTO, BELOW, STICKY, CAPTION, 'Full Graphic', CLOSECLICK, CLOSECOLOR, 'White', CLOSETEXT, 'Close')\" href='#' Id=P" + P + "_a Name=P" + P + "_a>";
						
						
						NewGraphicRec += "<img border=0 src='" + GraphicNode.getAttribute("Thumbnail_URL") + "' Id=P" + P + "_img Name=P" + P + "_img></a></td>";
						
						// Remove Button
						if (Remove)
						{
							NewGraphicRec += "<td align=center width=10%><input type=button name=graphic_Remove Size=1 Value=\"X\" Style=\"HEIGHT: 18px; Width: 18px; Color=red; Cursor=hand;\" onClick=\"javascript:section_remove_graphic(" + P + ")\"></td>\n";
						}
			
					}
				}
					NewGraphicRec += "\n</tr></table>";
					   //alert(NewGraphicRec);
						DivBlock["GraphicList"].insertAdjacentHTML("BeforeEnd",NewGraphicRec);

				
				if (Remove)
					section_add_graphic_new(P);
					
			}
		}
   }
}

function section_xml_add_graphic_container(P, Remove)
{
	//alert("section_xml_add_graphic_container");
//\ creates a html container for displaying a graphic.
//\ P - number of graphic, used for order and referencing later
//\ Remove - if true, then add a remove button.

	var NewGraphicRec = "<table border=0 width=100% cellspacing=0 cellpadding=0 class=Regular><tr bgcolor=#FFCC66>\n";

	// Graphic # in Section
	NewGraphicRec +=	"<td align=center width=3%>" + P
	NewGraphicRec += "</td>\n";
	
	// Remove Button
	if (Remove)
		NewGraphicRec += "<td align=center width=2%>" + section_graphic_get_up_button(j) + "</td>\n";
		NewGraphicRec += "<td align=center width=2%>" + section_graphic_get_down_button(j, (xmlPhraseList.length - 1)) + "</td>\n";
		NewGraphicRec += "<td align=center width=10%><input type=button name=graphic_Remove Size=1 Value=\"X\" Style=\"HEIGHT: 18px; Width: 18px; Color=red; Cursor=hand;\" onClick=\"javascript:section_remove_graphic(" + P + ")\"></td>\n";


	// Graphic # from Library - Library_Graphics_Text.Lib_Graphic_uKey --> vDoc_Graphics.Graphic_Text_uKey
	NewGraphicRec += "<td align=center width=10%><span Class=Graphics_Tiny_Box Id=P" + P + "_GraphicNum Name=P" + P + "_GraphicNum></span></td>\n"

	// Graphic Synonym from Library - Library_Graphics_Text.Graphic_Syn
	NewGraphicRec += "<td align=center width=10%><span Class=Graphics_Tiny_Box Id=P" + P + "_Syn Name=P" + P + "_Syn></span></td>\n"

	// Graphic from Library, --> vDoc_Graphics.Graphic
//	NewGraphicRec += "<td align=center width=70%><input type=hidden name=L" + L + "P" + P + "F5>";
//	NewGraphicRec += "<Span Class=Graphics_Small_Box Id=L" + L + "P" + P + "F5 Name=L" + L + "P" + P + "F5></span></td>\n</tr></table>";
	NewGraphicRec += "<TD align=center>";
	NewGraphicRec += "<a target=\"_new\" Id=P" + P + "_a Name=P" + P + "_a>";
	NewGraphicRec += "<img Id=P" + P + "_img Name=P" + P + "_img></a></td>";
	NewGraphicRec += "\n</tr></table>";

//	alert(NewGraphicRec);
	DivBlock["GraphicList"].insertAdjacentHTML("BeforeEnd",NewGraphicRec);

	return P;
}

function section_add_graphic_new(P)
{
//\
//\ adds new graphic row at bottom of list
//\
	var NewGraphicRec = "<table border=0 width=100% cellspacing=0 cellpadding=0 class=Regular><tr bgcolor=#FFCC66>\n";

	// Graphic # in Section
	NewGraphicRec +=	"<td align=center width=3%></td>\n";

	// Remove Button column
		NewGraphicRec += "<td align=center width=10%>Add New Entry</td>\n";

	// Graphic # from Library - Library_Graphics_Text.Lib_Graphic_uKey --> vDoc_Graphics.Graphic_Text_uKey
	//NewGraphicRec += "<td align=center width=10%><span Class=Graphics_Tiny_Box Id=Pnew_GraphicNum_old Name=Pnew_GraphicNum_old><input name=newGraphicNumber_old size=3 onChange=\"javascript:section_xml_graphic_remember_num(this.value);\"></span></td>\n"

	// Graphic Synonym from Library - Library_Graphics_Text.Graphic_Syn
	//NewGraphicRec += "<td align=center width=10%><span Class=Graphics_Tiny_Box Id=Pnew_Syn Name=Pnew_Syn><input name=newGraphicSyn size=3 onChange=\"javascript:section_xml_graphic_add_quick(this.value);\"></span></td>\n"

	NewGraphicRec += "<td align=center width=20%><span Class=Graphics_Tiny_Box Id=Pnew_GraphicNum Name=Pnew_GraphicNum><input name=newGraphicNumber size=3 onBlur=\"javascript:section_xml_graphic_code_onchange(this.value, this.name);\"></span></td>\n";

	NewGraphicRec += "<td align=center width=5%><div id='graphic_syn_list' Style='Display: inline;'></div></td>";

	// Graphic from Library, --> vDoc_Graphics.Graphic
	NewGraphicRec += "<TD colspan=\"3\"></td>";
	NewGraphicRec += "\n</tr></table>";

	DivBlock["GraphicList"].insertAdjacentHTML("BeforeEnd",NewGraphicRec);

	return P;
}

var grphcCode;
function section_xml_graphic_remember_num(value)
{
	grphcCode = value;
}


function section_xml_graphic_add_quick(Syn)
{
//' Queries DB for a note based on Graphic_uKey.
//' example of querying DB behind the scenes
	var newGraphic = new ActiveXObject("msxml2.DOMDocument");
	newGraphic.async = false;
	//alert(SERVER_NAME + "/Documents/Sections/graphic_quick_add.asp?Graphic_Code=" + grphcCode + "&Graphic_Syn=" + Syn);
	//newGraphic.load(SERVER_NAME + "/Documents/Sections/graphic_quick_add.asp?Graphic_Code=" + grphcCode + "&Graphic_Syn=" + Syn);
			var xmlurl = SERVER_NAME + "/Documents/Sections/graphic_quick_add.asp?Graphic_Code=" + grphcCode + "&Graphic_Syn=" + Syn;
		var getXML;
		
		getXML = new ActiveXObject("Msxml2.XMLHTTP.3.0");
       	getXML.open ("GET",xmlurl,false);
       	getXML.send();
			
		newGraphic.load(getXML.responseXML);
	
	
	if (newGraphic.documentElement == null)
	{
		alert("Could not find Graphic");
	}
	else
	{
/*		alert (newGraphic.xml);
		var newNode = newGraphic.createElement("GRAPHIC");
		newNode.setAttribute("Graphic_uKey", newGraphic.selectSingleNode("GRAPHIC").getAttribute("Graphic_uKey"));
		newNode.setAttribute("Library_uKey", newGraphic.selectSingleNode("GRAPHIC").getAttribute("Library_uKey"));
		newNode.setAttribute("Graphic_Description", newGraphic.documentElement.getAttribute("Graphic_Description"));
		newNode.setAttribute("Graphic_Syn", newGraphic.documentElement.getAttribute("Graphic_Syn"));
		newNode.setAttribute("Grouping", newGraphic.documentElement.getAttribute("Grouping"));
		newNode.setAttribute("Fullsize_URL", newGraphic.documentElement.getAttribute("Fullsize_URL"));
		newNode.setAttribute("Thumbnail_URL", newGraphic.documentElement.getAttribute("Thumbnail_URL"));
		newNode.setAttribute("Fullsize_EPS_URL", newGraphic.documentElement.getAttribute("Fullsize_EPS_URL"));
*/
		newGraphic.documentElement.setAttribute("Element_uKey", "?");
		newGraphic.documentElement.setAttribute("Doc_Element_Num", "?");
		newGraphic.documentElement.setAttribute("Sec_Element_Num", "?");
  

		section_xml_graphic_append_node(newGraphic.documentElement);
	}
}

function section_xml_graphic_append_node(node)
{
	//alert("section_xml_graphic_append_node");
//\ node - xml node to add - should be some sort of phrase

	var sectionNode = sectXmlSection.selectSingleNode("//DOCUMENTSECTIONS/SECTION/GRAPHICLIST");

	sectionNode.appendChild(node);
	
	//refresh the list
	document.displaySection.revised.value = 'yes';
	section_xml_display_graphics();
}

function section_xml_graphic_replace_node(newNode, pos)
{
//\ node - xml node to add - should be some sort of phrase
//\ pos - position to add, inserts before this number.

	sectionNode = sectXmlSection.selectSingleNode("//DOCUMENTSECTIONS/SECTION/GRAPHICLIST");
	graphicNodeList = sectXmlSection.selectNodes("//DOCUMENTSECTIONS/SECTION/GRAPHICLIST/GRAPHIC");
	newNode.setAttribute("Sec_Element_Num", pos);

	sectionNode.replaceChild(newNode, graphicNodeList.item(pos - 1));

	//refresh the list
	document.displaySection.revised.value = 'yes';
	section_xml_display_graphics();
}


//
//  section_spawn_graphicsearch()
//
function section_spawn_graphicsearch(DocId, EntityId, LangId, SecId)
{
	var url
	url = 'graphicsearch.asp?docid=' + DocId + '&entityid=' + EntityId + '&langid=' + LangId + '&secid=' + SecId;
	
	popupWindow = open(url, 'graphicsearch', 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=600, height=670');
	popupWindow.moveTo(10, 10);
}

//  The POD graphic search is used on the pod section page for author setup of pod allowable graphic selections
//  section_spawn_pod_graphicsearch()
//
function section_spawn_pod_graphicsearch(DocId, EntityId, SecId, Index)
{
	var url
	url = 'graphicsearch.asp?docid=' + DocId + '&entityid=' + EntityId + '&Mode=POD&secid=' + SecId + '&Index=' + Index ;
	
	popupWindow = open(url, 'graphicsearch', 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=600, height=670');
	popupWindow.moveTo(10, 10);
}

//
//  section_remove_graphic()
//
//		Create a new array of Graphics and Subgraphics
//
function section_remove_graphic(Num)
{
	var A, spid, i;

	// delete the phrase
	var sectionNode = sectXmlSection.selectSingleNode("/DOCUMENTSECTIONS/SECTION/GRAPHICLIST");
	var graphicNodeList = sectXmlSection.selectSingleNode("/DOCUMENTSECTIONS/SECTION/GRAPHICLIST").childNodes;

	for (var i=0; i < graphicNodeList.length; i++)
	{
		if (Num == (i + 1))
		{
			sectionNode.removeChild( graphicNodeList.item(i) );
			// TODO - renumber
		}
	}

	section_xml_display_graphics();
}

function section_xml_get_graphic_count()
{
	return sectXmlSection.selectNodes("//DOCUMENTSECTIONS/SECTION/GRAPHICLIST/GRAPHIC").length;
}

function section_get_up_button(index, SubSection)
{
	if (index == 0) {
		return '<input type=image src=/images/orangearrow.gif name=up Style="HEIGHT: 14px; Width: 14px;">';
	}
	
	return '<input type=image src=/images/arrowUp.gif name=up_' + index + ' Style="HEIGHT: 14px; Width: 14px;" onClick="javascript:section_move_subphrase_up(' + index + ', ' + SubSection + ')">';
}

function section_get_down_button(index, maxIndex, SubSection)
{
	if (index == maxIndex) {
		return '<input type=image src=/images/orangearrow.gif name=up Style="HEIGHT: 14px; Width: 14px;">';
	}
	
	return '<input type=image src=/images/arrowDown.gif name=down_' + index + ' Style="HEIGHT: 14px; Width: 14px;" onClick="javascript:section_move_subphrase_down(' + index + ', ' + SubSection + ')">';
}


function section_move_subphrase_down(index, SubSection)
{

	for (var i=0; i < xmlLangNodeList.length; i++)
	{
		var langSec = xmlLangNodeList.item(i).getAttribute("Sec_ID");

		if (langSec == SubSection)
		{		
			var downNode = xmlLangNodeList.item(i).childNodes(index);
			var upNode = xmlLangNodeList.item(i).childNodes(index + 1);
	
			xmlLangNodeList.item(i).insertBefore(upNode, downNode);
			upNode.setAttribute("Sec_Element_Num", (index+1));
			downNode.setAttribute("Sec_Element_Num", (index + 2));
		}
	}
	//alert('section_move_subphrase_down');
	section_xml_display_phrase_list();
}

function section_move_subphrase_up(index, SubSection)
{

	for (var i=0; i < xmlLangNodeList.length; i++)
	{
		var langSec = xmlLangNodeList.item(i).getAttribute("Sec_ID");

		if (langSec == SubSection)
		{
			var downNode = xmlLangNodeList.item(i).childNodes(index - 1);
			var upNode = xmlLangNodeList.item(i).childNodes(index);
			xmlLangNodeList.item(i).insertBefore(upNode, downNode);
			upNode.setAttribute("Sec_Element_Num", (index ));
			downNode.setAttribute("Sec_Element_Num", (index+1));
		}
	}
	//alert('section_move_subphrase_up');
	section_xml_display_phrase_list();
}

function section_graphic_get_up_button(index)
{
	if (index == 0) {
		return '<input type=image src=/images/orangearrow.gif name=up Style="HEIGHT: 14px; Width: 14px;">';
	}
	
	return '<input type=image src=/images/arrowUp.gif name=up_' + index + ' Style="HEIGHT: 14px; Width: 14px;" onClick="javascript:section_move_subgraphic_up(' + index + ')">';
}

function section_graphic_get_down_button(index, maxIndex)
{
	if (index == maxIndex) {
		return '<input type=image src=/images/orangearrow.gif name=up Style="HEIGHT: 14px; Width: 14px;">';
	}
	
	return '<input type=image src=/images/arrowDown.gif name=down_' + index + ' Style="HEIGHT: 14px; Width: 14px;" onClick="javascript:section_move_subgraphic_down(' + index + ')">';

}



function section_move_subgraphic_down(index)
{
	var graphicNodeList = sectXmlSection.selectNodes("/DOCUMENTSECTIONS/SECTION/GRAPHICLIST");
	
	for (var i=0; i < graphicNodeList.length; i++)
	{
		if(graphicNodeList.item(i).childNodes.length > 0)
		{
			var downNode = graphicNodeList.item(i).childNodes(index);
			var upNode = graphicNodeList.item(i).childNodes(index + 1);
			graphicNodeList.item(i).insertBefore(upNode, downNode);
			upNode.setAttribute("Sec_Element_Num", (index+1));
			downNode.setAttribute("Sec_Element_Num", (index + 2));
		}
	}

	section_xml_display_graphics();
}

function section_move_subgraphic_up(index)
{
	var graphicNodeList = sectXmlSection.selectNodes("/DOCUMENTSECTIONS/SECTION/GRAPHICLIST");
	
	for (var i=0; i < graphicNodeList.length; i++)
	{
		if(graphicNodeList.item(i).childNodes.length > 0)
		{
			var downNode = graphicNodeList.item(i).childNodes(index - 1);
			var upNode = graphicNodeList.item(i).childNodes(index);
			graphicNodeList.item(i).insertBefore(upNode, downNode);
			upNode.setAttribute("Sec_Element_Num", (index ));
			downNode.setAttribute("Sec_Element_Num", (index+1));
		}
	}
	section_xml_display_graphics();
	
}


///////////////////////////////////////////////////////////////////////////////
