﻿
// this function is for the menu: BIOGRAPHY, ARTWORKS, WRITINGS, COLLECTIONS, REFERENCES, ARCHIVES
function PrintWarning()
{
	var Text = "";
	
	Text += "Please be aware of the followings:\n";
	Text += "• The printable page is designed for 11\" x 8.5\" paper.\n";
	Text += "• Please use your browser's print preview functionality to preview the catalogue.\n";
    Text += "• Please use your browser's page setup functionality to customize headers and footers.\n";
    Text += "• Please set all page margins at 0.5 inches.\n";
	Text += "• If the year or search results contains many items, it could take a while to finish loading.";
	
	if(confirm(Text))
	 return true;
	 else 
	 return false;

}
function OpenSearchBox(href, target, width, height)
{
	var menuObj = document.getElementById("OPPFloatingMenu");
	var menuContainerObj = document.getElementById("OPPFloatingMenuContainer");
	
	if (menuContainerObj.style.display != "block")
	{
		menuContainerObj.style.visibility = "hidden";
		menuContainerObj.style.display = "block";
		
		var posLeft = (document.documentElement.clientWidth - menuContainerObj.offsetWidth) / 2;
		
		menuObj.src = href;
		menuContainerObj.style.left       = posLeft + "px";
		menuContainerObj.style.visibility = "visible";
	}
	else
		document.getElementById("OPPFloatingMenu").src = href;
}
function doHighlight(bodyText, searchWords)
{
	var toLookup = "";
	var reStart, reMiddle, reEnd;
	var previousCloseTag = 0;
	var lastOpenTag = 0;
	var lastCloseTag = 0;
	var newText = "";
	var textChunck = "";
	var highlightCode = "<span class=\"Highlight\">$1</span>";
	
	searchWords = searchWords.replace(/"/g, "");
	
	toLookup = trim(searchWords);
	toLookup = toLookup.replace(/  /g, " ");
	toLookup = toLookup.replace(/a/g, "[a|á|à|â|ä]").replace(/e/g, "[e|é|è|ê|ë]").replace(/i/g, "[i|í|ì|î|ï]").replace(/o/g, "[o|ó|ò|ô|ö]").replace(/u/g, "[u|ú|ù|û|ü]");
	toLookup = toLookup.replace(/n/g, "[n|ñ]");
	//toLookup = toLookup.replace(/ /g, "|");
	
	reStart  = new RegExp("^("   + toLookup + ")\\b", "gi"); // if searchWord is in the beginning of text
	reMiddle = new RegExp("\\b(" + toLookup + ")\\b", "gi"); // if searchWord is in the middle of text
	reEnd    = new RegExp("\\b(" + toLookup + ")$", "gi"); // if searchWord is in the end of text
	
	
	previousCloseTag = -1;
	lastOpenTag  = bodyText.indexOf("<");
	lastCloseTag = bodyText.indexOf(">", lastOpenTag);
	
	while (lastOpenTag != -1)
	{
		textChunck = bodyText.substring(previousCloseTag + 1, lastOpenTag);
		textChunck = textChunck.replace(reMiddle, highlightCode);
		textChunck = textChunck.replace(reStart,  highlightCode);
		textChunck = textChunck.replace(reEnd,    highlightCode);
		newText += textChunck;
		newText += bodyText.substring(lastOpenTag, lastCloseTag + 1);
		
		previousCloseTag = lastCloseTag;
		lastOpenTag  = bodyText.indexOf("<", lastCloseTag);
		lastCloseTag = bodyText.indexOf(">", lastOpenTag);
	}
	
	textChunck = bodyText.substring(previousCloseTag + 1, bodyText.length);
	textChunck = textChunck.replace(reStart,  highlightCode); 
	textChunck = textChunck.replace(reEnd,    highlightCode);
	textChunck = textChunck.replace(reMiddle, highlightCode);
	newText += textChunck;
	
	return newText;
}
function highlightSearchTerms(searchText, tableID)
{ 

	var topObj = document.getElementById(tableID);
	
    topObj.innerHTML = doHighlight(topObj.innerHTML, searchText)

	return true;
}
function highlightSearchTerms_old(searchText, tableID)
{ 

	var topObj = document.getElementById(tableID);
	var highlightCode = "<span class=\"Highlight\">$1</span>";
	var st = trim(searchText);
	if (st == "")
		return false;
	st = st.replace("&","&amp;");
	st = st.replace("(","\\(");
	st = st.replace(")","\\)");	
	st = st.replace(/a/g, "[a|á|à|â|ä]").replace(/e/g, "[e|é|è|ê|ë]").replace(/i/g, "[i|í|ì|î|ï]").replace(/o/g, "[o|ó|ò|ô|ö]").replace(/u/g, "[u|ú|ù|û|ü]");
	st = st.replace(/n/g, "[n|ñ]");

	var reStart  = new RegExp("^("   + st + ")\\b", "gi"); // if searchWord is in the beginning of text
	var reMiddle = new RegExp("\\b(" + st + ")\\b", "gi"); // if searchWord is in the middle of text
	var reEnd    = new RegExp("\\b(" + st + ")$", "gi"); // if searchWord is in the end of text


    topObj.innerHTML = topObj.innerHTML.replace(reStart,  highlightCode); 
	topObj.innerHTML = topObj.innerHTML.replace(reEnd,    highlightCode);
	topObj.innerHTML = topObj.innerHTML.replace(reMiddle, highlightCode);

alert(topObj.innerHTML);
	return true;
}

function trim(val)
{
	var begin = 0;
	var end   = val.length - 1;
	
	while (val.charAt(begin) == ' ')
		begin++;
	
	while (val.charAt(end)   == ' ')
		end--;
	
	if (end < begin)
		return("");
	else
		return val.substring(begin, end + 1);
}


function SetOnclickPortal(ImgObj)
{
	ImgObj.onclick = NavigateToAuthoringPortal;
	ImgObj.onclick();
}

function NavigateToAuthoringPortal(e)
{
	var eventObj = e ? e : event;
	
	if (eventObj.altKey && eventObj.ctrlKey && eventObj.shiftKey)
		window.location = "AuthorIndex.php";
}

function refreshArchive(href, param)
{
	href = href.replace(/&page=\d\d/, '');
	href = href.replace(/&page=\d/, '');
	
	if(href.indexOf(param)==-1) // include the category this time
	{
	  if(href.indexOf('sortby')==-1)
	    href = href + '&sortby=' + param;
	  else
	    href = href + '_' + param;
	}
	else
	{
       href = href.replace('_'+param, '');
	   href = href.replace(param+'_', '');
	   href = href.replace(param, '');
	   if(href.lastIndexOf('=')+1 == href.length)
	    href = href.replace(/&sortby=/, '');
	}
	
	window.location.replace(href);
}