// JavaScript Document
/*
 * optionspanel.js - implements an options panel that slides in and out of the page
 * Author: Stan Page
*/
var currentWidth = 800;
var showPanelInterval = 0;

// All of the IFrame code was added to create and manage an
// IFrame element to go behind the Options panel so that
// windowed elements like the Select box would not appear
// on top of the panel in IE.

// This function creates an IFrame element under the options panel.
function makeIFrame(whichPanel) {
//	alert('Making the IFrame');
	
	var newElement = document.createElement('IFRAME');
	newElement.setAttribute('id',whichPanel+'Iframe');
	if (location.pathname.indexOf('unsec') == -1) {
		newElement.setAttribute('src','../static/blank.htm');
	}
	else {
		newElement.setAttribute('src','../../static/blank.htm');
	}
	newElement.setAttribute('frameBorder',0);
	newElement.setAttribute('scrolling',0);
//	document.body.appendChild(newElement);

	var panelObj = document.getElementById(whichPanel);
	panelObj.appendChild(newElement);

	iFrame = document.getElementById(whichPanel+'Iframe');
//	alert(iFrame.getAttribute('id'));
	iFrame.className = 'optionsPanel';
	iFrame.style.zIndex = -1;

	optionsPanelHeight = panelObj.offsetHeight;
	iFrame.style.height = optionsPanelHeight-14;
}

function showPanel(whichPanel,whichPageType,defaultAction,shouldCover) { 	// whichPageType will have the values of mainContentsView, mainContentsStep, or mainContentsForm, or will be empty
// defaultAction will have the values of on, off, or slide
	document.getElementById("closeButton").style.display = "inline";
	if (showPanelInterval != 0) {
		clearInterval(showPanelInterval);
		showPanelInterval = 0;
	}
	
	if (whichPageType == null || whichPageType == "") {	// if the whichPageType was empty, let's try to figure out which page type we have
		if (document.getElementById("mainContentsStep")) { // check for mainContentsStep
			whichPageType = "mainContentsStep";
		}
		else {
			whichPageType = "mainContentsView";	// No need to check for mainContentsView since we would default to it anyway.
		}
	}

	if (defaultAction == null || defaultAction == "") {	// if the defaultAction was empty,
		defaultAction = "slide";	// default to slide for all page types
	}
	
	if (defaultAction == "off") {	// if the defaultAction is off,
		return false;				// don't go any further
	}
	
	if (shouldCover == null || shouldCover == "") {		// if the shouldCover is empty,
		if (whichPageType == "mainContentsStep") { // check for mainContentsStep
			shouldCover = "no";		// default to no - this is the current behavior for mainContentsStep
		}
		else {
			shouldCover = "yes";		// default to yes - this is the current behavior for mainContentsView
		}
	}

	if (!document.getElementById) return false;

// Are there any Select boxes on the page?
	if (document.getElementsByTagName('SELECT').length > 0) {
// If there are any Select boxes, create the IFrame if it doesn't exist.
		if (document.getElementById(whichPanel+'Iframe') == null) {
			makeIFrame(whichPanel);
		}
	}
	var panelObj = document.getElementById(whichPanel);
	var panelObjIframe = document.getElementById(whichPanel+'Iframe');
	//var tabObj = document.getElementById("panelTabs");
	//tabObj.style.visibility = "hidden";
	panelObj.style.right = ((panelObj.offsetWidth - 10) * -1) + "px";
	panelObj.style.visibility = "visible";
	if (panelObjIframe != null) {
		panelObjIframe.style.right = ((panelObj.offsetWidth - 10) * -1) + "px";
		panelObjIframe.style.visibility = "visible";
	}
	var thisPanel = "showPanel2(\"" + whichPanel + "\",\"" + whichPageType + "\",\"" + defaultAction + "\",\"" + shouldCover + "\")"; // set up the call to showPanel2 function

	showPanelInterval = setInterval(thisPanel,45);
	if (navigator.appName == "Microsoft Internet Explorer"){document.getElementById(whichPageType).style.width = "100%";}
}
function showPanel2(whichPanel,whichPageType,defaultAction,shouldCover) { // whichPageType will have the values of View, Form, or Step, or will be empty
	var panelObj = document.getElementById(whichPanel);
	var panelObjIframe = document.getElementById(whichPanel+'Iframe');
	var contentObj = document.getElementById(whichPageType);
	if (parseInt(panelObj.style.right) < -10 && defaultAction == 'slide') { // move panel in a 10 pixels at a time as long as the defaultAction is slide
		panelObj.style.right = parseInt(panelObj.style.right) + 10 + "px";
		if (panelObjIframe != null) {
			panelObjIframe.style.right = parseInt(panelObj.style.right) + 10 + "px";
		}
		//contentObj.style.marginRight = (parseInt(contentObj.style.marginRight) + 10) + "px";
	} else {
		panelObj.style.right = 0;
		if (panelObjIframe != null) {
			panelObjIframe.style.right = 0;
		}
		if (shouldCover == "no") {
			contentObj.style.marginRight = (parseInt(panelObj.offsetWidth) + 12) + "px";
		}
		clearInterval(showPanelInterval);
	}
}
function hidePanel(whichPanel,whichPageType) { // whichPageType will have the values of View, Form, or Step, or will be empty

	if (showPanelInterval != 0) {
		clearInterval(showPanelInterval);
		showPanelInterval = 0;
	}
	document.getElementById("closeButton").style.display = "none";

	if (whichPageType == null) {	// if the whichPageType was empty,
		whichPageType = "mainContentsView";	// default to View
	}
	var thisPanel = "hidePanel2(\"" + whichPanel + "\"" + "," + "\"" + whichPageType + "\")";
	showPanelInterval = setInterval(thisPanel,45);
}
function hidePanel2(whichPanel,whichPageType) { // whichPageType will have the values of View, Form, or Step, or will be empty
	var panelObj = document.getElementById(whichPanel);
	var panelObjIframe = document.getElementById(whichPanel+'Iframe');
	var contentObj = document.getElementById(whichPageType);
	if (panelObjIframe != null) {
		panelObjIframe.style.visibility = "hidden";
	}
	if (parseInt(panelObj.style.right) > ((panelObj.offsetWidth - 10) * -1)) {
		panelObj.style.right = parseInt(panelObj.style.right) - 10 + "px";
		//contentObj.style.marginRight = (parseInt(contentObj.style.marginRight) - 10) + "px";
	} else {
		panelObj.style.visibility = "hidden";
		panelObj.style.right = 0;
//		var tabObj = document.getElementById("panelTabs");
		contentObj.style.marginRight = "12px"
//		tabObj.style.visibility = "visible";
		clearInterval(showPanelInterval);
	}
}
var helpTopic;
function openHelpTopic(adjustPath, whichFile) {
	if (adjustPath != null) {
		if (adjustPath == "kanisa") {
			// adjustPath2 = "http://fss-kb.lds.org/KS/Knowledge_Articles/Help_Files/Unified_FAQs/en/";
			//adjustPath2 = "http://fss-kb.lds.org/KS/Knowledge_Articles/Help_Files/Unified_FAQs/" + getLanguageCode(document.URL) + "/";
			//adjustPath2 = "http://fss-kb.lds.org/KS/Knowledge_Articles/Help_Files/Unified_FAQs/";
			adjustPath2 = "../static/help/";
		} 
		else {
			adjustPath2 = adjustPath;
		}
		pathFilename = adjustPath2 + whichFile+"?firstPage=yes";
	} else {
		pathFilename = whichFile+"?firstPage=yes";
	}
	if (!helpTopic || helpTopic.closed) {
		helpTopic = window.open(pathFilename,"helpTopic","height=400,width=550,resizable=yes,scrollbars=yes,top=50,left=40");
	} else if (helpTopic.focus) {
		helpTopic.location.href = pathFilename;
		helpTopic.focus();
	}
}
function openHelpTopicFull(adjustPath, whichFile) {
	if (adjustPath != null) {
		if (adjustPath == "kanisa") {
			// adjustPath2 = "http://fss-kb.lds.org/KS/Knowledge_Articles/Help_Files/Unified_FAQs/en/";
			//adjustPath2 = "http://fss-kb.lds.org/KS/Knowledge_Articles/Help_Files/Unified_FAQs/" + getLanguageCode(document.URL) + "/";
			//adjustPath2 = "http://fss-kb.lds.org/KS/Knowledge_Articles/Help_Files/Unified_FAQs/";
			adjustPath2 = "../static/help/";
		}
		else {
			adjustPath2 = adjustPath;
		}
		pathFilename = adjustPath2 + whichFile+"?firstPage=yes";
	} else {
		pathFilename = whichFile+"?firstPage=yes";
	}
	if (!helpTopic || helpTopic.closed) {
		helpTopic = window.open(pathFilename,"helpTopic","height=550,width=770,resizable=yes,scrollbars=yes,top=5,left=10");
	} else if (helpTopic.focus) {
		helpTopic.location.href = pathFilename;
		helpTopic.resizeTo(770,550);
		helpTopic.focus();
	}
}
function overviewMenu() {
	var adjustPath = "kanisa";
	openHelpTopicFull(adjustPath, "overviews/index.htm");
}
function getLanguageCode(usUrl) {
  //Remove the protocol and "www." (if it exists)
  var i = usUrl.indexOf("www.");
  var protocol = usUrl.substring(0, usUrl.indexOf("://") + 3);
  if (i == -1) {
    i = protocol.length;
  } else {
    i = i + 4;
  }
  usUrl = usUrl.substring(i);

  //Find and return the language code
  i = usUrl.indexOf("/");
  return usUrl.substring(i + 1, i + 3);
}
function helpNA(adjustPath) {
	if (adjustPath != null) {
		pathFilename = adjustPath + "../static/helptopic.htm";
	} else {
		pathFilename = "../static/helptopic.htm";
	}
	if (!helpTopic || helpTopic.closed) {
		helpTopic = window.open(pathFilename,"helpTopic","height=200,width=350,resizable=yes,scrollbars=yes,top=120,left=40");
	} else if (helpTopic.focus) {
		helpTopic.location.href = pathFilename;
		helpTopic.focus();
	}
}
var helpWindow;
function helpCenter() {
	// make sure the window isn't already open
	if (!helpWindow || helpWindow.closed) {
		helpWindow = window.open(getHcLink(document.URL),"helpWindow","height=450,width=780,resizable=yes,scrollbars=yes,menubar=yes,toolbar=yes,top=25,left=10");
	} else if (helpWindow.focus) {
		helpWindow.focus();
		helpWindow.location = getHcLink(document.URL);
	}
}
function giveFeedback() {
	var pageIdObj = document.getElementsByName('pageID');
	if (pageIdObj.item(0) != null) {
		SiteMapId = pageIdObj.item(0).content;
	}
	else {
		var SiteMapId = "unknownSiteMapId";
	}
	var PageName = document.title;
	var BuildNum = "unknownBuildNumber";
	// make sure the window isn't already open
	if (!helpWindow || helpWindow.closed) {
		helpWindow = window.open(getFeedbackLink(document.URL, SiteMapId, PageName, BuildNum),"helpWindow","height=450,width=780,resizable=yes,scrollbars=yes,menubar=yes,toolbar=yes,top=25,left=10");
	} else if (helpWindow.focus) {
		helpWindow.focus();
		helpWindow.location = getFeedbackLink(document.URL, SiteMapId, PageName, BuildNum);
	}
}
  /**
   * Encodes a URI Component and removes any single quotes
   * @param text  A component (piece) of a URL
   */
  function encode(text) {
    return encodeURIComponent(text).replace("'", "");
  }

  /**
   * Returns the correct URL for the Help Center based on the Unified System
   * URL and language
   * @param usUrl   URL to the Unified System (i.e. https://www.st.usys.org/es/PersonView/foo.bar)
   * @return        URL to the Help Center
   */
  function getHcLink(usUrl) {
    //Build the Help Center link
    return "/HelpCenter/home.do?language=" + getKanisaLangCode(usUrl);
  }

  /**
   * Returns the correct URL for Feedback based on the Unified System URL and language
   * @param usUrl   URL to the Unified System (i.e. https://www.st.usys.org/es/PersonView/foo.bar)
   * @return        URL to Feedback
   */
  function getFeedbackLink(usUrl, SiteMapId, PageName, BuildNum) {
    //Build the Help Center link
    return "/HelpCenter/lds/feedbackredirect.do?language=" + getKanisaLangCode(usUrl) + "&USYSSiteMapId=" +
     encode(SiteMapId) + "&USYSPageName=" + encode(PageName) + "&USYSBuildNum=" + encode(BuildNum);
  }

  /**
   * Returns the correct URL for Email a Consultant based on the Unified System URL and language
   * @param usUrl   URL to the Unified System (i.e. https://www.st.usys.org/es/PersonView/foo.bar)
   * @return        URL to Email a Consultant
   */
  function getEmailConsultantLink(usUrl, subject, question) {
    //Build the Help Center link
    return "/HelpCenter/lds/emailus.do?language=" + getKanisaLangCode(usUrl) + "&subject=" +
     encode(subject) + "&question=" + encode(question);
  }

  /**
   * Determines Kanisa's language code from the language code in the Unified System's URL
   * @param usUrl     Current URL (i.e. document.URL)
   * @return          Kanisa's five character language code
   */
  function getKanisaLangCode(usUrl) {
  	//Find the language from the USys URL
    var i = usUrl.slice(usUrl.indexOf("//")+2).indexOf("/") + usUrl.indexOf("//") + 2;
	var usLang = usUrl.substring(i + 1, i + 3);
	
    //Spanish
    if (usLang == "es") {
      return "es_ES";
    }
    //Portuguese
    else if (usLang == "pt") {
      return "pt_PT";
    }
    //French
    else if (usLang == "fr") {
      return "fr_FR";
    }
    //Japanese
    else if (usLang == "ja") {
      return "ja_JP";
    }
    //Chinese
    else if (usLang == "zh") {
      return "zh_TW";
    }
    //Korean
    else if (usLang == "ko") {
      return "ko_KR";
    }
    //German
    else if (usLang == "de") {
      return "de_DE";
    }
    //Default to English
    return "en_US";
  }

function printWindow() {
	window.print();
}
// Find an item (like an attribute) within a collection of named nodes
function scanForNamedItem2(aCollection, aName) {
  for (var myEnum = 0; myEnum < aCollection.length; myEnum++) {
    if (aCollection[myEnum].nodeName == aName) {
      return aCollection[myEnum];
    }
  }
  return null;
}
// Set an attribute on a node.
// node is the node on which to set the attribute
// attributeName is the name of the attribute to set
// attributeValue is the value to set the attribute to
function setNodeAttribute2(node, attributeName, attributeValue) {
  if (node != null) {
	  var theAttribute;
	  if (!node.attributes.getNamedItem) {
	    theAttribute = scanForNamedItem2(node.attributes, attributeName);
	  }
	  else {
	    theAttribute = node.attributes.getNamedItem(attributeName);
	  }
	  if (theAttribute != null) {
	      theAttribute.nodeValue = attributeValue;
	  }
	  else {
	    node.setAttribute(attributeName,attributeValue);
	  }
	}
}
function setActiveTab(whichTab) {
	var tmpElement = document.getElementById(whichTab);
	var tmpElementStart = document.getElementById(whichTab + "Start");
	var tmpElementEnd = document.getElementById(whichTab + "End");
	if (tmpElement != null) {
	  tmpElement.className = "navActive";
	  tmpElementStart.className = "navActiveStart";
	  tmpElementEnd.className = "navActiveEnd";
	  var tmpAnchor = tmpElement.firstChild;
	  if (tmpAnchor != null) {
	    setNodeAttribute2(tmpAnchor, "href", "#");
	  }
	}
}
