function isIE()
{
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
return browser.indexOf("Microsoft Internet Explorer") != -1;
}

/*
* Play video on given HTML element (oDiv)
* Return the video player object according to dependent browser
*/
function playVideo(oDiv, video, width, height){
	if (!video){
		oDiv.innerText = "No video is given to play.";
		return;
	}

	if (isIE()){
		var oObject = document.createElement("object");
		oObject.setAttribute("id","mediaPlayer");
		oObject.setAttribute("width", width);
		oObject.setAttribute("height", height+45);			//IE: Control Panel (h: 45)
		oObject.setAttribute("classid","CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95");
		oObject.setAttribute("codebase","http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701");
		oObject.setAttribute("standby","Loading Microsoft Windows Media Player components...");
		oObject.setAttribute("type","application/x-oleobject");
		var params = [
		["fileName",video],
		["animationatStart","true"],
		["transparentatStart","true"],
		["autoStart","true"],
		["showControls","true"],
		["loop","false"]
		];
		for (i = 0 ; i < params.length; i++){
			var kv = params[i];
			var oParam = document.createElement("param");
			oParam.setAttribute("name",kv[0]);
			oParam.setAttribute("value",kv[1]);
			oObject.appendChild(oParam);		
		}    
    	oDiv.innerHTML = oObject.outerHTML;    		
		return oObject;
	}else{	//EMBED object for non IE browser (e.g. FF)
		var oEmbed = document.createElement("embed");
		oEmbed.setAttribute("type","application/x-mplayer2");
		oEmbed.setAttribute("pluginspage","http://microsoft.com/windows/mediaplayer/en/download/");
		oEmbed.setAttribute("id","mediaPlayer");
		oEmbed.setAttribute("name","mediaPlayer");
		oEmbed.setAttribute("displaysize","4");
		oEmbed.setAttribute("autosize","-1");	
		oEmbed.setAttribute("bgcolor","darkblue");	
		oEmbed.setAttribute("showControls","true");	
		oEmbed.setAttribute("showtracker","-1");	
		oEmbed.setAttribute("showdisplay","0");	
		oEmbed.setAttribute("showstatusbar","-1");	
		oEmbed.setAttribute("videoborder3d","-1");	
		oEmbed.setAttribute("width", width);	
		oEmbed.setAttribute("height", height+45+24);				//FF: Control Panel (h: 45) + Status bar (h: 24)
		oEmbed.setAttribute("src",video);	
		oEmbed.setAttribute("autostart","true");	
		oEmbed.setAttribute("designtimesp","5311");	
		oEmbed.setAttribute("loop","false");	
		oDiv.appendChild(oEmbed);
		return oEmbed;
	}
}


/*
* Save current HTML as file
*/
function saveScreen(){
	var currentBrowser = navigator.appName.toLowerCase();
	if (currentBrowser.indexOf("netscape") != -1)
	{
		alert("To save this screen, please press 'Control-S'");
	}
	else if(currentBrowser.indexOf("mac")!=-1)
	{
		alert("To save this screen, please press 'Command-S'");
	}
	else{
		document.execCommand('SaveAs', '', '');
	}
	/*
	if (document.execCommand){
		document.execCommand("SaveAs");
	}else alert("You browser doesn't support save function.");
	*/
}


/*
  * Create DOM Object
*/
function createDOMObject(){
	var _xmlDom = null;
    if (!window.DOMParser  && window.ActiveXObject){
        var arrXmlDomTypes = ['MSXML2.DOMDocument.6.0','MSXML2.DOMDocument.3.0','Microsoft.XMLDOM'];
        for(var i = 0;i<arrXmlDomTypes.length;i++){
            try{
                _xmlDom = new ActiveXObject(arrXmlDomTypes[i]);
            }catch(ex){}
        }
    }else{// Mozilla browsers have a DOMParser
        try{
            if(_xmlDom == null && document.implementation && document.implementation.createDocument){
                _xmlDom = document.implementation.createDocument("","",null);
            }
            isIE = false;
        }catch (ex){alert(ex);}
    }
    
    //Warning for Unsupported browser versions
    var browser=navigator.appName;
	var version=navigator.appVersion;    
    if (!_xmlDom) alert("Warning: Your Browser is not supported:\n"+browser+"\n"+version);   
    return _xmlDom;
}
/*
XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
{
    if( !xNode )
        { xNode = this; } 
    var oNSResolver = this.createNSResolver(this.documentElement)
    var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
    var aResult = [];
    for( var i = 0; i < aItems.snapshotLength; i++)
    {
        aResult[i] =  aItems.snapshotItem(i);
    }
    return aResult;
}
*/


/*
* Firefox extension to implement HTMLElement.innerText method
* Ref: http://www.cnlei.org/blog/article.asp?id=425
* Ref: https://developer.mozilla.org/en/Gecko_DOM_Reference (Firefox Reference)
*/
if (navigator.userAgent.indexOf("Firefox")!=-1)
{
 HTMLElement.prototype.__defineGetter__( "innerText",
  function(){
  return this.textContent;
  }
  );
  HTMLElement.prototype.__defineSetter__( "innerText",
  function(sText){
  this.textContent=sText;
  }
  );
 }


/*
* Firefox extension to XPath engine in IE
*/
if(document.implementation.hasFeature("XPath", "3.0") )
{

	/*
	* Firefox externsion to text() getter in IE
	*/
	Element.prototype.__defineGetter__("text",function(){ return this.textContent; });	
	Text.prototype.__defineGetter__("text",function(){ return this.data; });

	/*
	 * Firefox extension to LoadXML() in IE
	 */
	Document.prototype.loadXML = function(sXml){
	    var oParser= new DOMParser();
	    var _xmlDom = oParser.parseFromString(sXml, "text/xml");
	    
		while(this.firstChild){
		       this.removeChild(this.firstChild);
		}
		
		for(var i=0;i<_xmlDom.childNodes.length;i++){
		       var oNewNode = this.importNode(_xmlDom.childNodes[i],true);
		       this.appendChild(oNewNode);
		}
	}



   Document.prototype.selectNodes = function(cXPathString, xNode)
   {
      if( !xNode ) { xNode = this; } 
      var oNSResolver = this.createNSResolver(this.documentElement)
      var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                   XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
      var aResult = [];
      for( var i = 0; i < aItems.snapshotLength; i++)
      {
         aResult[i] =  aItems.snapshotItem(i);
      }
      return aResult;
    }

   Element.prototype.selectNodes = function(cXPathString)
   {
      if(this.ownerDocument.selectNodes)
      {
         return this.ownerDocument.selectNodes(cXPathString, this);
      }
      else{throw "For XML Elements Only";}
   } 

   Document.prototype.selectSingleNode = function(cXPathString, xNode)
   {
      if( !xNode ) { xNode = this; } 
      var xItems = this.selectNodes(cXPathString, xNode);
      if( xItems.length > 0 )
      {
         return xItems[0];
      }
      else
      {
         return null;
      }
   }
    /*  
   Element.prototype.selectSingleNode = function(cXPathString)
   {    
      if(this.ownerDocument.selectSingleNode)
      {
         return this.ownerDocument.selectSingleNode(cXPathString, this);
      }
      else{throw "For XML Elements Only";}
   }
   */

	/*
	 * Firefox Extension on selectSingleNode() in IE
	*/  
	Element.prototype.selectSingleNode=function(sXPath){
	    var oEvaluator = new XPathEvaluator();
	    var oResult = oEvaluator.evaluate(sXPath,this,null, XPathResult.FIRST_ORDERED_NODE_TYPE,null);
	     return oResult.singleNodeValue; 
	}


}//else alert("This browser doesn't support XPath 3.0, please contact administrator.");



/**
* Return true if the current connection is secured..
*/
function isSecureConnection(){
	var protocol = window.location.protocol;
	return protocol.indexOf("https:") != -1;	
}

/**
 * Using secure connection
 * Append https to url
 * E.g. https://localhost:9080/HSI-Net/HSI-Net?cmd=navigate&pageId=
 */
function secureConnection(pageId){	
	if (!isSecureConnection()){
		var action = constants['com.hsi.client.esFormAction'];
		var contextPath = constants['com.hsi.client.contextPath'];
		var pageId = constants['com.hsi.client.pageId'];
		try{
			var url = "https://"+ window.location.host + contextPath + "/"+ action +"?cmd=navigate&pageId="+pageId;
			if (debug) alert("Secured Url: "+url);
			return;
			window.location.href = url;
		}catch(e){
			alert("Error when assigning translated server url: "+url +"\nReason: "+ e);
			return;
		}
	}
}
