/*
 * Gets the Public Access default language key from the XML configuration file.
 */
function getPublicAccessDefaultLanguage() {
  if ((top.publicAccessXmlDoc != null) && top.publicAccessXmlDocIsLoaded) {
    return top.getPublicAccessXmlElementValue("default-language");
  } else {
    // Currently only the following are supported as a default language:
    // Empty String = english; spanish; french; and arabic
    return "";
  }
}

/*
 * Gets the Public Access English language key from the XML configuration file.
 */
function getPublicAccessEnglishLanguage() {
  if ((top.publicAccessXmlDoc != null) && top.publicAccessXmlDocIsLoaded) {
    return top.getPublicAccessXmlElementValue("english-language");
  } else {
    return "";
  }
}

/*
 * Gets the Public Access Spanish language key from the XML configuration file.
 */
function getPublicAccessSpanishLanguage() {
  if ((top.publicAccessXmlDoc != null) && top.publicAccessXmlDocIsLoaded) {
    return top.getPublicAccessXmlElementValue("spanish-language");
  } else {
    return "spanish";
  }
}

/*
 * Gets the Public Access French language key from the XML configuration file.
 */
function getPublicAccessFrenchLanguage() {
  if ((top.publicAccessXmlDoc != null) && top.publicAccessXmlDocIsLoaded) {
    return top.getPublicAccessXmlElementValue("french-language");
  } else {
    return "french";
  }
}

/*
 * Gets the Public Access Arabic language key from the XML configuration file.
 */
function getPublicAccessArabicLanguage() {
  if ((top.publicAccessXmlDoc != null) && top.publicAccessXmlDocIsLoaded) {
    return top.getPublicAccessXmlElementValue("arabic-language");
  } else {
    return "arabic";
  }
}

/*
 * Gets the Public Access language change boolean flag from the XML configuration file.
 */
function doesPublicAccessAllowLanguageChanges() {
  if ((top.publicAccessXmlDoc != null) && top.publicAccessXmlDocIsLoaded) {
    var xmlEleValue = top.getPublicAccessXmlElementValue("allow-language-change");
    return ((xmlEleValue == "true") ? true : false);
  } else {
    return false;
  }
}

/*
 * Gets boolean flag true for the supplied language, if it supports right to left
 * orientation; otherwise false. Checks the supplied language with the Public Access
 * XML configuration file, if possible.
 */
function isPublicAccessLanguageRightToLeft(lang) {
  var result = false;

  if (!lang) {
	  return result;
  }

  if ((top.publicAccessXmlDoc != null) && top.publicAccessXmlDocIsLoaded) {
    var rtlValue = top.getPublicAccessXmlElementAttribute(lang + "-language", "rtl");

    if (rtlValue == "true") {
	    result = true;
    }
  } else if (lang == "arabic") {
    result = true;
  }

  return result;
}

/*
 * Gets boolean flag true for the supplied language, if the document needs to change its
 * orientation; otherwise false. Checks the current documents direction and the supplied
 * language with the Public Access XML configuration file, if possible.
 */
function needToSwitchPublicAccessDocumentDirection(lang) {
  var result = false;
  var bodyElements = document.documentElement.getElementsByTagName("body");
  var bodyDir = null;
  var currentBodyDir = "ltr";

  if ((bodyElements != null) && (bodyElements.length > 0)) {
  	bodyDir = bodyElements[0].attributes.getNamedItem("dir");

  	if (bodyDir != null) {
  	  currentBodyDir = bodyDir.nodeValue;
  	}
  }

  if (isPublicAccessLanguageRightToLeft(lang)) {
    result = ((currentBodyDir == null) || (currentBodyDir.length == 0) || (currentBodyDir == "ltr"));
  } else {
    result = ((currentBodyDir != null) && (currentBodyDir == "rtl"));
  }

  return result;
}

/*
 * Switches the document's orientation, if needed by the supplied language. Checks the current
 * documents direction and the supplied language with the Public Access XML configuration file,
 * if possible.
 */
function switchPublicAccessDocumentDirection(lang) {
  if (needToSwitchPublicAccessDocumentDirection(lang)) {
	  var bodyElements = document.documentElement.getElementsByTagName("body");

	  if ((bodyElements != null) && (bodyElements.length > 0)) {
	    var bodyDir = bodyElements[0].attributes.getNamedItem("dir");

  		if (bodyDir != null) {
  			var currentBodyDir = bodyDir.nodeValue;

	      if ((currentBodyDir == null) || (currentBodyDir.length == 0) || (currentBodyDir == "ltr")) {
	        bodyDir.nodeValue = "rtl";
	      } else {
	        bodyDir.nodeValue = "ltr";
	      }
	    } else if ((bodyElements != null) && (bodyElements.length > 0)) {
  	  	bodyDir = document.createAttribute("dir");

	      if ((currentBodyDir == null) || (currentBodyDir.length == 0) || (currentBodyDir == "ltr")) {
	        bodyDir.nodeValue = "rtl";
	      } else {
	        bodyDir.nodeValue = "ltr";
	      }

	  	  bodyElements[0].setAttributeNode(bodyDir);
  		}
  	}
  }
}

/*
 * Switches the document's language to the supplied language.
 */
function switchPublicAccessDocumentLanguage(lang) {
  if (lang != null) {
    var currentHref = location.href;
    var queryPos = currentHref.indexOf("?");

    if (queryPos < 0) {
      queryPos = currentHref.length - 1;
    }

		var typePos = currentHref.lastIndexOf(".", queryPos);

		if (typePos > -1) {
			var langPos = currentHref.lastIndexOf("_", queryPos);

			if (lang.length > 0) {
			  if (langPos == -1) {
			    currentHref = currentHref.substring(0, typePos) + "_" + lang + currentHref.substr(typePos);
			  } else if (lang != currentHref.substring(langPos + 1, typePos)) {
			    currentHref = currentHref.substring(0, langPos + 1) + lang + currentHref.substr(typePos);
		    } else {
			    currentHref = "";
			  }
			} else {
			  if ((langPos > -1) && (langPos < typePos)) {
			    currentHref = currentHref.substring(0, langPos) + currentHref.substr(typePos);
			  } else if (langPos == -1) {
			    currentHref = "";
			  }
			}

			if (currentHref.length > 0) {
			  var langParamPos = currentHref.lastIndexOf("language=");

				if (langParamPos > -1) {
			    var nextParamPos = currentHref.indexOf("&", langParamPos + 8);

			    if (nextParamPos > langParamPos) {
			    	currentHref = currentHref.substring(0, langParamPos + 9) + lang + currentHref.substr(nextParamPos);
			    } else {
			    	currentHref = currentHref.substring(0, langParamPos + 9) + lang;
			    }
				} else {
			    queryPos = currentHref.indexOf("?");

					if (queryPos > -1) {
			    	currentHref += "&language=" + lang;
					} else {
			    	currentHref += "?language=" + lang;
					}
				}

	  		location.replace(currentHref);
	  	}
		}
  }
}

/*
 * Loads the OpenLaszlo Shockwave file (SWF) with the supplied name, width,
 * height, and parameters in the current location where this function is called.
 */
function loadPublicAccessOpenLaszloSWFFile(lzxSwfFile, width, height, params) {
  if ((lzxSwfFile != null) && (lzxSwfFile.length > 8) &&
      (lzxSwfFile.substr(lzxSwfFile.length - 8) == ".lzx.swf")) {
    var lzCanvasRuntimeVersion = 6;

    if (isIE && isWin || detectFlash() >= lzCanvasRuntimeVersion) {
      var swfWidth = "100%";

      if ((width != null) && (width.length > 0)) {
      	swfWidth = width;
      }

      var swfHeight = "100%";

      if ((height != null) && (height.length > 0)) {
      	swfHeight = height;
      }

      var swfParams = "";

      if ((params != null) && (params.length > 0)) {
        if (params.substr(0, 1) == "?") {
          swfParams = params.substr(1);
        } else {
          swfParams = params;
        }
      }

      if ((swfParams.length > 0) && (swfParams.substr(0, 1) != "&")) {
        swfParams = "&" + swfParams;
      }

      if (location.search.length > 1) {
        swfParams += "&" + location.search.substr(1);
      }

      lzEmbed({url: lzxSwfFile + "?__lzhistconn=" + top.connuid  + swfParams, width: swfWidth, height: swfHeight});
      lzHistEmbed(lzGetWebAppRoot(location));
    } else {
      document.write('This application requires Flash player ' + lzCanvasRuntimeVersion +
                     ' or greater. <a href="http://www.macromedia.com/go/getflashplayer" target="fpupgrade">Click here</a> to upgrade.');
    }
  }
}

/*
 * Print content frame.
 */
function contentPrint() {
  var contentFrame = top.frames["content-frame"];

  if (contentFrame != null) {
  	contentFrame.focus();
  	contentFrame.print();
  }
}

/*
 * Display status text in bottom frame.
 */
function displayStatusText(text) {
  var bottomFrame = top.frames["bottom-frame"];

  if ((text != null) && (bottomFrame != null)) {
    var status = bottomFrame.document.getElementById("status");

    if ((status != null) && (status.innerHTML != null)) {
      status.innerHTML = text;
    }
  }
}

/*
 * Variable for progress status text.
 */
var publicAccessProgressStatusText = null;

/*
 * Display progress status text in bottom frame; or clear it.
 */
function displayProgressStatusText(text) {
	top.publicAccessProgressStatusText = text;

	if ((top.publicAccessProgressStatusText != null) && (top.publicAccessProgressStatusText.length > 0)) {
	  top.publicAccessProgressStatusText += "&nbsp;&bull;";
	  displayStatusText(top.publicAccessProgressStatusText);
	  window.setTimeout("updateProgressStatusText()", 150);
	} else {
	  displayStatusText("");
	}
}

/*
 * Update progress status text in bottom frame; or stop the progress.
 */
function updateProgressStatusText() {
	if ((top.publicAccessProgressStatusText != null) && (top.publicAccessProgressStatusText.length > 0)) {
	  top.publicAccessProgressStatusText += "&bull;";
	  displayStatusText(top.publicAccessProgressStatusText);
	  window.setTimeout("updateProgressStatusText()", 150);
	}
}

/*
 * Changes the supplied element to use the supplied class name.
 */
function changeElementClass(theElement, theClassName) {
  if ((theElement != null) && (theClassName != null) && (theClassName.length > 0)) {
    theElement.className = theClassName;
  }
}

/*
 * Sends the tab forward event supplied from the window supplied to the next window.
 */
function sendTabForwardEvent(theFromWindow, theEvent) {
  if (theFromWindow != null) {
    if (top != window) {
      top.sendTabForwardEvent(theFromWindow, theEvent);
    } else {
	    if (theEvent == null) {
	      theEvent = window.event;
	    }

	    if (theEvent != null) {
				var code = null;

				if (theEvent.keyCode) {
				  code = theEvent.keyCode;
				}	else if (theEvent.which) {
				  code = theEvent.which;
				}

				var shift = false;

  			if (theEvent.shiftKey && (theEvent.shiftKey != null)) {
  			  shift = theEvent.shiftKey;
  			} else if (theEvent.modifiers && (theEvent.modifiers != null) && window.Event) {
  			  shift = ((theEvent.modifiers & Event.SHIFT_MASK) != 0);
  			}

	      if ((code != null) && (code == 9) && !shift) {
	        // Send the tab forward event to another frame
	        if (theFromWindow.name == "top-frame") {
		        // try menu frame and then content frame
		        var theFrame = top.frames["menu-frame"];

		        if (theFrame.receiveTabForwardEvent && (theFrame.receiveTabForwardEvent != null)) {
		          theFrame.receiveTabForwardEvent(theFromWindow, theEvent);
		        } else {
		        	var theFrame = top.frames["content-frame"];

		        	if (theFrame.receiveTabForwardEvent && (theFrame.receiveTabForwardEvent != null)) {
		          	theFrame.receiveTabForwardEvent(theFromWindow, theEvent);
		          }
		        }
	        } else if (theFromWindow.name == "menu-frame") {
		        // try content frame and then top frame
		        var theFrame = top.frames["content-frame"];

		        if (theFrame.receiveTabForwardEvent && (theFrame.receiveTabForwardEvent != null)) {
		          theFrame.receiveTabForwardEvent(theFromWindow, theEvent);
		        } else {
		        	var theFrame = top.frames["top-frame"];

		        	if (theFrame.receiveTabForwardEvent && (theFrame.receiveTabForwardEvent != null)) {
		          	theFrame.receiveTabForwardEvent(theFromWindow, theEvent);
		          }
		        }
	        } else {
	  	      // try top frame and then menu frame
		        var theFrame = top.frames["top-frame"];

		        if (theFrame.receiveTabForwardEvent && (theFrame.receiveTabForwardEvent != null)) {
		          theFrame.receiveTabForwardEvent(theFromWindow, theEvent);
		        } else {
		        	var theFrame = top.frames["menu-frame"];

		        	if (theFrame.receiveTabForwardEvent && (theFrame.receiveTabForwardEvent != null)) {
		          	theFrame.receiveTabForwardEvent(theFromWindow, theEvent);
		          }
		        }
	        }
	      }
	    }
    }
  }
}

/*
 * Sends the tab backward event supplied from the window supplied to the next window.
 */
function sendTabBackwardEvent(theFromWindow, theEvent) {
  if (theFromWindow != null) {
    if (top != window) {
      top.sendTabBackwardEvent(theFromWindow, theEvent);
    } else {
	    if (theEvent == null) {
	      theEvent = window.event;
	    }

	    if (theEvent != null) {
				var code = null;

				if (theEvent.keyCode) {
				  code = theEvent.keyCode;
				}	else if (theEvent.which) {
				  code = theEvent.which;
				}

				var shift = false;

  			if (theEvent.shiftKey && (theEvent.shiftKey != null)) {
  			  shift = theEvent.shiftKey;
  			} else if (theEvent.modifiers && (theEvent.modifiers != null) && window.Event) {
  			  shift = ((theEvent.modifiers & Event.SHIFT_MASK) != 0);
  			}

	      if ((code != null) && (code == 9) && shift) {
	        // Send the tab backward event to another frame
	        if (theFromWindow.name == "top-frame") {
		        // try content frame and then menu frame
		        var theFrame = top.frames["content-frame"];

		        if (theFrame.receiveTabBackwardEvent && (theFrame.receiveTabBackwardEvent != null)) {
		          theFrame.receiveTabBackwardEvent(theFromWindow, theEvent);
		        } else {
		        	var theFrame = top.frames["menu-frame"];

		        	if (theFrame.receiveTabBackwardEvent && (theFrame.receiveTabBackwardEvent != null)) {
		          	theFrame.receiveTabBackwardEvent(theFromWindow, theEvent);
		          }
		        }
	        } else if (theFromWindow.name == "menu-frame") {
		        // try top frame and then content frame
		        var theFrame = top.frames["top-frame"];

		        if (theFrame.receiveTabBackwardEvent && (theFrame.receiveTabBackwardEvent != null)) {
		          theFrame.receiveTabBackwardEvent(theFromWindow, theEvent);
		        } else {
		        	var theFrame = top.frames["content-frame"];

		        	if (theFrame.receiveTabBackwardEvent && (theFrame.receiveTabBackwardEvent != null)) {
		          	theFrame.receiveTabBackwardEvent(theFromWindow, theEvent);
		          }
		        }
	        } else {
		        // try menu frame and then top frame
		        var theFrame = top.frames["menu-frame"];

		        if (theFrame.receiveTabBackwardEvent && (theFrame.receiveTabBackwardEvent != null)) {
		          theFrame.receiveTabBackwardEvent(theFromWindow, theEvent);
		        } else {
		        	var theFrame = top.frames["top-frame"];

		        	if (theFrame.receiveTabBackwardEvent && (theFrame.receiveTabBackwardEvent != null)) {
		          	theFrame.receiveTabBackwardEvent(theFromWindow, theEvent);
		          }
		        }
	        }
	      }
	    }
    }
  }
}

/*
 * Sends the Enter key event supplied to the function name supplied.
 */
function sendEnterKeyEvent(theEvent, theFunctionName) {
  if ((theFunctionName != null) && (theFunctionName.length > 0) && (eval(theFunctionName))) {
    if (theEvent == null) {
      theEvent = window.event;
    }

    if (theEvent != null) {
			var code = null;

			if (theEvent.keyCode) {
			  code = theEvent.keyCode;
			}	else if (theEvent.which) {
			  code = theEvent.which;
			}

      if ((code != null) && (code == 13)) {
        eval(theFunctionName + "()");
      }
		}
  }
}

/*
 * get XML Http Request
 *
 * Returns an new XMLHttpRequest object, or false if the browser
 * doesn't support it
 */
function getXMLHttpRequest() {

  var xmlreq = false;

  // Create XMLHttpRequest object in non-Microsoft browsers
  if (window.XMLHttpRequest) {
      xmlreq = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer
       xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      // Failed to create required ActiveXObject
      try {
        // Try version supported by older versions
        // of Internet Explorer
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        // Unable to create an XMLHttpRequest by any means
        xmlreq = false;
      }
    }
  }

  return xmlreq;
}

 /*
  * get Call Back Handler
  * Returns a function that waits for the specified XMLHttpRequest
  * to complete, then passes it XML response to the given handler function.
  * params
  * req                - The XMLHttpRequest whose state is changing
  * responseXmlHandler - Function to pass the XML response to
  */
 function getCallBackHandler(req, responseXmlHandler) {

   // Return an anonymous function that listens to the XMLHttpRequest instance
   return function () {

     // If the request's status is "complete"
     if (req.readyState == 4) {

       // Check that we received a successful response from the server
       if (req.status == 200) {

         // Pass the XML payload of the response to the handler function.
         responseXmlHandler(req.responseXML);

       } else {

         // An HTTP problem has occurred
         alert("HTTP error "+req.status+": "+req.statusText);
       }
     }
   }
 }
