
var xmlDoc;	 // The XML-parser

var editingXMLIndexes = new Array();
// Style classes
var CategoryStyle = "style21";
var LinkStyle = "style62";

// Image filenames
var EditImageSrc = "/RGS-IBG-SITE/Images/edit.gif";
var RemoveImageSrc = "/RGS-IBG-SITE/Images/delete.gif";
var MoveUpImageSrc = "/RGS-IBG-SITE/Images/up_button_black.gif";
var MoveDownImageSrc = "/RGS-IBG-SITE/Images/down_button_black.gif";

function PrintPage() {
	var cntrl = document.getElementById( "divPrint");
	cntrl.style.display = "none";        
	window.print();
}

function ShowResourceBrowser(textboxName)
{
	var survey = window.open('/RGS-IBG-SITE/ResourceNav.aspx?textboxName=' + textboxName, 'ResourceBrowser', 'location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes,width=400,height=400,left=200,top=200');
}

function PageOpenInNewWindow(pageUrl)
{		
	window.open(pageUrl);
}

function OpenSizedPageInNewWindow(pageUrl, width, height)
{		
	params = 'location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no,width=' + width + ',height=' + height + ',left=200,top=200';	
		
	window.open(pageUrl, '', params);	
}

// Popup window for the print friendly window
function OpenSearchResultsInNewWindow(pageUrl)
{		
	window.open(pageUrl);
}

// Loads the XML string in parameter xmlText into the XML-parser(global variable xmlDoc).
function ResourceListAuthoring_LoadXml(xmlText)
{
	if (window.ActiveXObject)  // IE
	{
		xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
		xmlDoc.async = false;
		xmlDoc.loadXML(xmlText);
	}
	else if (document.implementation.createDocument)  // NN
	{
		xmlDoc = (new DOMParser()).parseFromString(xmlText,'application/xml');
	}
	else
	{
		alert("Your browser does not support editing the resource list.");
	}
}


// Builds the links 
function ResourceListAuthoting_BuildLinksTable(hidden, linksTable)
{
	ResourceListAuthoring_LoadXml(hidden.value);

	for ( var i=linksTable.rows.length-1; i>=0; i-- )
	{
		linksTable.deleteRow( i );
	}
	var typeSelect = document.getElementById(linksTable.id.toString().replace("LinksTable", "TypeSelect"));

	var currentCategory = -1;
	var newCategory;
	var lastInCategory;
	
	if (xmlDoc.documentElement != null && xmlDoc.documentElement.childNodes != null)
	{
		for (var i=0; i<xmlDoc.documentElement.childNodes.length; i++)
		{
			if (xmlDoc.documentElement.childNodes[i].getAttribute("Category") != currentCategory)
			{
				newCategory = true;
				currentCategory = xmlDoc.documentElement.childNodes[i].getAttribute("Category");
				var row = linksTable.insertRow(linksTable.rows.length);
				var cell = row.insertCell(row.cells.length);
				
				for (var j=0; j<typeSelect.options.length; j++)
				{
					if (typeSelect.options[j].value == currentCategory)
					{
						var category = typeSelect.options[j].text;
						var lastIndex = typeSelect.options[j].text.lastIndexOf(" / ");
						if (lastIndex >= 0 && lastIndex < category.length - 3)
						{
							category = category.substring(lastIndex + 3, category.length);
						}
						cell.className = CategoryStyle;
						cell.innerHTML = category;
						break;
					}
				}
			}
			
			if (i == xmlDoc.documentElement.childNodes.length - 1 || xmlDoc.documentElement.childNodes[i+1].getAttribute("Category") != currentCategory )
			{
				lastInCategory = true;
			}
			
			var row = linksTable.insertRow(linksTable.rows.length);
			var cell = row.insertCell(row.cells.length);
			
			cell.innerHTML += "<a class=\"" + LinkStyle + "\" href=\"" + xmlDoc.documentElement.childNodes[i].getAttribute("Path") + "\">" + xmlDoc.documentElement.childNodes[i].getAttribute("Name") + "</a>&nbsp;&nbsp;";
			cell.innerHTML += "<img src=\"" + EditImageSrc + "\" id=\"" + linksTable.id.toString().replace("LinksTable", "Edit" + i) + "\" onclick=\"javascript:ResourceListAuthoring_EditNode(" + i + ", this)\">&nbsp;&nbsp;";
			cell.innerHTML += "<img src=\"" + RemoveImageSrc + "\" id=\"" + linksTable.id.toString().replace("LinksTable", "Remove" + i) + "\" onclick=\"javascript:ResourceListAuthoring_DeleteNode(" + i + ", this)\">&nbsp;&nbsp;";

			if (!newCategory)
			{
				cell.innerHTML += "<img src=\"" + MoveUpImageSrc + "\" id =\"" + linksTable.id.toString().replace("LinksTable", "MoveUp" + i) + "\" onclick=\"javascript:ResourceListAuthoring_MoveUpNode(" + i + ", this)\">&nbsp;&nbsp;";
			} else
			{
				newCategory = false;
			}

			if (!lastInCategory)
			{
				cell.innerHTML += "<img src=\"" + MoveDownImageSrc + "\" id =\"" + linksTable.id.toString().replace("LinksTable", "MoveDown" + i) + "\" onclick=\"javascript:ResourceListAuthoring_MoveDownNode(" + i + ", this)\">&nbsp;&nbsp;";
			} else
			{
				lastInCategory = false;
			}
		}
	}
	else
	{
		hidden.value = "<Links />";
	}
}


function ResourceListAuthoring_NewNode(newButton)
{
	var inputTable = document.getElementById(newButton.id.toString().replace("NewButton", "InputTable"));
	inputTable.style.display = "";
	
	var addButton = document.getElementById(newButton.id.toString().replace("NewButton", "AddButton"));
	addButton.style.display = "";
	
	var updateButton = document.getElementById(newButton.id.toString().replace("NewButton", "UpdateButton"));
	updateButton.style.display = "none";
	
	var cancelButton = document.getElementById(newButton.id.toString().replace("NewButton", "CancelButton"));
	cancelButton.style.display = "";

	newButton.style.display = "none";
}

//Occurs when the "Edit"-link is pressed
function ResourceListAuthoring_EditNode(index, editLink)
{
	var inputTable = document.getElementById(editLink.id.toString().replace("Edit" + index, "InputTable"));
	inputTable.style.display = "";
	var newButton = document.getElementById(editLink.id.toString().replace("Edit" + index, "NewButton"));
	newButton.style.display = "none";

	var hidden = document.getElementById(editLink.id.toString().replace("Edit" + index, "HiddenXML"));
	ResourceListAuthoring_LoadXml(hidden.value);
	var typeSelect = document.getElementById(editLink.id.toString().replace("Edit" + index, "TypeSelect"));
	for (var i=0; i<typeSelect.options.length; i++)
	{
		if (typeSelect.options[i].value == xmlDoc.documentElement.childNodes[index].getAttribute("Category"))
		{
			typeSelect.selectedIndex = i;
		}
	}
	
	var nameText = document.getElementById(editLink.id.toString().replace("Edit" + index, "NameText"));
	nameText.value = xmlDoc.documentElement.childNodes[index].getAttribute("Name");
	
	var pathText = document.getElementById(editLink.id.toString().replace("Edit" + index, "PathText"));
	pathText.value = xmlDoc.documentElement.childNodes[index].getAttribute("Path");
	
	var addButton = document.getElementById(editLink.id.toString().replace("Edit" + index, "AddButton"));
	addButton.style.display = "none";
	
	var updateButton = document.getElementById(editLink.id.toString().replace("Edit" + index, "UpdateButton"));
	updateButton.style.display = "";
	
	var cancelButton = document.getElementById(editLink.id.toString().replace("Edit" + index, "CancelButton"));
	cancelButton.style.display = "";
	
	var found = false;
	for (var item in editingXMLIndexes)
	{
		if (item == hidden.id)
		{
			editingXMLIndexes[hidden.id] = index;
			found = true;
			break;
		}
	}
	if (!found)
	{
		editingXMLIndexes[hidden.id] = index;
	}
}



//Occurs when the "Remove"-link is pressed
function ResourceListAuthoring_DeleteNode(index, deleteLink)
{
	var hidden = document.getElementById(deleteLink.id.toString().replace("Remove" + index, "HiddenXML"));
	
	ResourceListAuthoring_LoadXml(hidden.value);
	xmlDoc.documentElement.removeChild(xmlDoc.documentElement.childNodes[index]);
	hidden.value = xmlDoc.xml;
	
	var table = document.getElementById(deleteLink.id.toString().replace("Remove" + index, "LinksTable"));
	ResourceListAuthoting_BuildLinksTable(hidden, table);

	for (var item in editingXMLIndexes)
	{
		if (item == hidden.id)
		{
			editingXMLIndexes[hidden.id] = -1;
			ResourceListAuthoring_Cancel(document.getElementById(deleteLink.id.toString().replace("Remove" + index, "CancelButton")));
			break;
		}
	}
}

// Move a link up one position. Does not check if the indexes are valid. This is to be done by whoever calls this function
function ResourceListAuthoring_MoveUpNode(index, moveUpLink)
{
	var hidden = document.getElementById(moveUpLink.id.toString().replace("MoveUp" + index, "HiddenXML")); 
	var parentNode, childNode;
	ResourceListAuthoring_LoadXml(hidden.value);

	childNode = xmlDoc.documentElement.childNodes[index];
	parentNode = childNode.parentNode;
	parentNode.removeChild(childNode);       
    parentNode.insertBefore(childNode, xmlDoc.documentElement.childNodes[index - 1]);
         
	hidden.value = xmlDoc.xml;

	var table = document.getElementById(moveUpLink.id.toString().replace("MoveUp" + index, "LinksTable"));
	ResourceListAuthoting_BuildLinksTable(hidden, table);
	
	for (var item in editingXMLIndexes)
	{
		if (item == hidden.id)
		{
			editingXMLIndexes[hidden.id] = -1;
			ResourceListAuthoring_Cancel(document.getElementById(moveUpLink.id.toString().replace("MoveUp" + index, "CancelButton")));
			break;
		}
	}

}

// Call the MoveUpNode function for the link under the moveDownLink
function ResourceListAuthoring_MoveDownNode(index, moveDownLink)
{
	var moveUpLink = document.getElementById(moveDownLink.id.toString().replace("MoveDown" + index , "MoveUp" + (index+1))); 
	ResourceListAuthoring_MoveUpNode(index + 1, moveUpLink);
}


//Occurs when the "Add"-button is pressed
function ResourceListAuthoring_AddNode(addButton)
{
	var typeSelect = document.getElementById(addButton.id.toString().replace("AddButton", "TypeSelect"));
	var nameText = document.getElementById(addButton.id.toString().replace("AddButton", "NameText"));
	var pathText = document.getElementById(addButton.id.toString().replace("AddButton", "PathText"));

	if (nameText.value == "")
	{
		alert("Please give a name");
		return;
	}
	if (pathText.value == "")
	{
		alert("Please give a path");
		return;
	}

	var inputTable = document.getElementById(addButton.id.toString().replace("AddButton", "InputTable"));
	inputTable.style.display = "none";
	var newButton = document.getElementById(addButton.id.toString().replace("AddButton", "NewButton"));
	newButton.style.display = "";

	var hidden = document.getElementById(addButton.id.toString().replace("AddButton", "HiddenXML"));
	ResourceListAuthoring_LoadXml(hidden.value);

	var newNode = xmlDoc.createElement("Link");
	newNode.setAttribute("Category", typeSelect.options[typeSelect.selectedIndex].value);
	newNode.setAttribute("Name", nameText.value);
	newNode.setAttribute("Path", pathText.value);
	
	var inserted = false;
	
	if (xmlDoc.documentElement != null && xmlDoc.documentElement.childNodes != null)
	{
		for (var i=0; i<xmlDoc.documentElement.childNodes.length; i++)
		{
			if (!ResourceListAuthoring_Category1ComesAfterCategory2(typeSelect.options[typeSelect.selectedIndex].value, xmlDoc.documentElement.childNodes[i].getAttribute("Category"), typeSelect))
			{
				xmlDoc.documentElement.insertBefore(newNode,xmlDoc.documentElement.childNodes[i]);
				inserted = true;
				break;
			}	
		}
	}
	
	if (!inserted)
	{
		if (xmlDoc.documentElement != null)
		{
			xmlDoc.documentElement.appendChild(newNode); 
		}
	}
	
	hidden.value = xmlDoc.xml;
	var table = document.getElementById(addButton.id.toString().replace("AddButton", "LinksTable"));
	ResourceListAuthoting_BuildLinksTable(hidden, table);
	
	typeSelect.selectedIndex = 0;
	nameText.value = "";
	pathText.value = "";
}



function ResourceListAuthoring_Category1ComesAfterCategory2(cat1, cat2, typeSelect)
{
	for (var i=0; i<typeSelect.options.length; i++)
	{
		if (typeSelect.options[i].value == cat2)
		{
			return true;
		}
		if (typeSelect.options[i].value == cat1)
		{
			return false;
		}
	}
	return true;
}



//Occurs when the "Update"-button is pressed
function ResourceListAuthoring_UpdateNode(updateButton)
{
	var typeSelect = document.getElementById(updateButton.id.toString().replace("UpdateButton", "TypeSelect"));
	var nameText = document.getElementById(updateButton.id.toString().replace("UpdateButton", "NameText"));
	var pathText = document.getElementById(updateButton.id.toString().replace("UpdateButton", "PathText"));

	if (nameText.value == "")
	{
		alert("Please give a name");
		return;
	}
	if (pathText.value == "")
	{
		alert("Please give a path");
		return;
	}

	var hidden = document.getElementById(updateButton.id.toString().replace("UpdateButton", "HiddenXML"));
	ResourceListAuthoring_LoadXml(hidden.value);

	var inputTable = document.getElementById(updateButton.id.toString().replace("UpdateButton", "InputTable"));
	inputTable.style.display = "none";
	var newButton = document.getElementById(updateButton.id.toString().replace("UpdateButton", "NewButton"));
	newButton.style.display = "";

	for (var item in editingXMLIndexes)
	{
		if (item == hidden.id)
		{
			var index = editingXMLIndexes[hidden.id];

			nodeToUpdate = xmlDoc.documentElement.childNodes[index];
			
			if (nodeToUpdate.getAttribute("Category") != typeSelect.options[typeSelect.selectedIndex].value)
			{
				xmlDoc.documentElement.removeChild(nodeToUpdate);
				
				var inserted = false;
				for (var i=0; i<xmlDoc.documentElement.childNodes.length; i++)
				{
					if (!ResourceListAuthoring_Category1ComesAfterCategory2(typeSelect.options[typeSelect.selectedIndex].value, xmlDoc.documentElement.childNodes[i].getAttribute("Category"), typeSelect))
					{
						xmlDoc.documentElement.insertBefore(nodeToUpdate,xmlDoc.documentElement.childNodes[i]);
						inserted = true;
						break;
					}	
				}
				if (!inserted)
				{
					xmlDoc.documentElement.appendChild(nodeToUpdate); 
				}
			}
			nodeToUpdate.setAttribute("Category", typeSelect.options[typeSelect.selectedIndex].value);
			nodeToUpdate.setAttribute("Name", nameText.value);
			nodeToUpdate.setAttribute("Path", pathText.value);

			editingXMLIndexes[hidden.id] = -1;
			
			typeSelect.selectedIndex = 0;
			nameText.value = "";
			pathText.value = "";
			
			break;
		}
	}

	hidden.value = xmlDoc.xml;
	var table = document.getElementById(updateButton.id.toString().replace("UpdateButton", "LinksTable"));
	ResourceListAuthoting_BuildLinksTable(hidden, table);
}



//Occurs when the "Cancel"-button is pressed
function ResourceListAuthoring_Cancel(cancelButton)
{
	var inputTable = document.getElementById(cancelButton.id.toString().replace("CancelButton", "InputTable"));
	inputTable.style.display = "none";

	var newButton = document.getElementById(cancelButton.id.toString().replace("CancelButton", "NewButton"));
	newButton.style.display = "";

	var typeSelect = document.getElementById(cancelButton.id.toString().replace("CancelButton", "TypeSelect"));
	typeSelect.selectedIndex = 0;
	
	var nameText = document.getElementById(cancelButton.id.toString().replace("CancelButton", "NameText"));
	nameText.value = "";
	
	var pathText = document.getElementById(cancelButton.id.toString().replace("CancelButton", "PathText"));
	pathText.value = "";
}

function ResourceListAuthoting_StartUpScript(hiddenXMLfield, linksTableName)
{
	ResourceListAuthoting_BuildLinksTable(document.getElementById(hiddenXMLfield), document.getElementById(linksTableName));
}

