// Functions to load and read key/id from xml file for localization - should only be used for pages 
// outside of the SmA framework
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// Globals and Logic

sma_config_to_use = "fpemailupdate";

//Object to contain the sma_config.xml file
var xmlDoc;
xmlDoc = importXML("../sma_config.xml", onLoadConfig);  

//Object to contain the language XML
var xmlTextDoc;
var docLoc = document.location.href;

//Clarify that there are arguments prior to allowing the fix below
if (docLoc.indexOf("?") != -1) docLoc = docLoc.substr(0, docLoc.indexOf("?"));  // this logic was susceptible to periods or slashes

var strBegin = docLoc.lastIndexOf("/");
var strEnd = docLoc.lastIndexOf(".");
var strLength = (strEnd - strBegin);
var xmlFileName = (docLoc.substr(strBegin,strLength) + ".xml");

//Set Language doc
xmlTextDoc = importXML("../language/en" + xmlFileName, null);


var si_class = sma_cfg_GetValue("smartissue", "si_class");

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

function sma_GetTextFromXML(key, defaultVal)
{
  
  //If Firefox then use Firefox specific XPATH code.
  if( document.implementation.hasFeature("XPath", "3.0") ) 
  { 
    var exp = '/resources/res[@id="'+ key + '"]'; 
    var serializer = new XMLSerializer();
    var xml = serializer.serializeToString(xmlTextDoc);
    if (xml == "")
    {
      document.location.reload(true);
    }
    var config = document.evaluate(exp, xmlTextDoc, null, XPathResult.ANY_TYPE, null);
    if(config != null){
      return config.iterateNext().textContent;
    }
    else
      return (defaultVal == null ? key : defaultVal);
  }
  
	try
	{
		var config = xmlTextDoc.selectSingleNode("//res[@id=\""+ key + "\"]");
		return(config.text);
	}
	catch (ee)
	{
		return (defaultVal == null ? key : defaultVal);
	}
}

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

function sma_loc_GlobalXLate(key, defaultVal)
{
  //If Firefox then use Firefox specific XPATH code.
  if( document.implementation.hasFeature("XPath", "3.0") ) 
  { 
    var exp = '/resources/res[@id="'+ key + '"]'; 
    var config = document.evaluate(exp, xmlTextDoc, null, XPathResult.ANY_TYPE, null);
    if(config != null)
      return config.iterateNext().textContent;
    else
      return "";
  }
    
	try
	{
		var config = xmlTextDoc.selectSingleNode("//res[@id=\""+ key + "\"]");
		return(config.text);
	}
	catch (ee)
	{
		return (defaultVal == null ? key : defaultVal);
	}
}

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

function sma_QueryRegValue(regRoot, regKey, regVal)
{ 
  try
  {
  return window.external.QueryRegValue(regRoot, regKey, regVal); 	
  }
  catch (ee) { return ""; }  
}

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//This function intentionally overloads the function from sma_config.js

function sma_cfg_GetValue(section, key, defaultVal)
  {
  //If Firefox then use Firefox specific XPATH code.
  if( document.implementation.hasFeature("XPath", "3.0") ) 
  { 
    var exp = '/sprt/configuration[@name="'+ sma_config_to_use + '"]/config-section[@name="'+ section + '"]/config[@name="'+ key + '"]'; 
    var config = document.evaluate(exp, xmlDoc, null, XPathResult.ANY_TYPE, null);
    if(config != null)
      return config.iterateNext().textContent;
    else
      return "";
  }
  
  try
    { 
    var config = xmlDoc.selectSingleNode("//configuration[@name=\""+ sma_config_to_use + "\"]//config-section[@name=\""+ section + "\"]//config[@name=\""+ key + "\"]");
    return(config.text);
    }
  catch (ee)
    { 
    return (defaultVal == null ? "" : defaultVal);
    }
  }

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

function sma_cfg_ToUse()
  {
  return sma_config_to_use;
  }

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// Cross-Browser XML Document loading
function importXML(doc, callback)
{ 
  var xmlDoc;
  
  if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		if(callback){
      xmlDoc.onload = callback;
    }
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
 	}
 	
	xmlDoc.load(doc);
	
	return xmlDoc;
}

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// XML onLoad Callback functions
function onLoadConfig() {
  si_class = sma_cfg_GetValue("smartissue", "si_class");
}


