function highlightOnLoad() {
	if (/&blismobile\=1/.test(window.location.search)) {
		if (location.href.indexOf("translate.legislation.gov.hk") >= 0) {
			setCookie(window, "blismobile", "1", null, "/gb/www.legislation.gov.hk/blis_ind.nsf/", null, null);
		}
		else {
			setCookie(window, "blismobile", "1", null, "/blis_ind.nsf/", null, null);
		}
	}

  // Get search string
  try {
  if (/&s\=/.test(window.location.search)) {   
    var searchString = getSearchString();   
    // Starting node, parent to all nodes you want to search   
    var textContainerNode = document.getElementById("docContent");
    // Split search terms on '*' and iterate over resulting array   
    var searchTerms = searchString.split('*');   
    var tempinnerHTML = textContainerNode.innerHTML;   
    for (var i in searchTerms)  {   
      tempinnerHTML = doHighlight(tempinnerHTML, unescapeTerm(searchTerms[i]), '<span class="highlighted term'+i+'">', "</span>")
    }
    textContainerNode.innerHTML = tempinnerHTML;
 }
 } catch (ex) { }
}   
  
// Pull the search string out of the URL   
function getSearchString() {   
  // Return sanitized search string if it exists   
  //var rawSearchString = unescape(window.location.search).replace(/[a-zA-Z0-9\?\&\=\%\#]+s\=(\w+)(\&.*)?/,"$1");   
  var rawSearchString = window.location.search;
  var pos = (rawSearchString.indexOf("&s="));
  rawSearchString = rawSearchString.substring(pos + 3);
  // Replace '+' with '|' for regex   
  // Also replace '%20' if your cms/blog uses this instead (credit to erlando for adding this)   
  return rawSearchString.replace(/\%20|\+|[>]/g," ").replace(/\%22/g,"");
}   

function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  if (searchTerm.length == 0)
    return bodyText;

  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
    highlightEndTag = "</font>";
  }
  
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
}

function showResult(href, isMobile) {
	highlightTerm = document.termForm.elements["highlightTerm"].value;
	if (isMobile) {
		//var w = window.open("", "BlisDoc");
		//w.close();
		window.open(href + "&s=" + escapeTerm(highlightTerm), "BlisDoc");
	}
	else
		location.href = href + "&s=" + escapeTerm(highlightTerm);
    return false;
}

function showPastVersions(vwAlias, docid) {
    var url = "../WebShowPastVersions"+QM_KHBYPASS+"OpenAgent&cur=" + vwAlias + "*" + docid;
    window.open(url, "BlisPastVersions", "toolbar=0,location=0, directories=0, status=1, menubar=0, fullscreen=no, scrollbars=2, resizable=1,copyhistory=0,width=400,height=250");
    //return false;
}

function loadPastVersion(vwAlias, verdocid) {
    var url = vwAlias.replace(/^Cur/,"AllVer") + "/" + verdocid + QM_KHBYPASS + "OpenDocument&bt=0";
    window.open(url, "BlisPastVersion", "toolbar=1,location=0, directories=0, status=1, menubar=1, fullscreen=no, scrollbars=2, resizable=1,copyhistory=0,width=820,height=600");
    window.close();
    return false;
}

function webBacktoView()
{
    var href;
    var s;
    
    if (parent && parent.document.getElementById('viewState') && (href = parent.document.getElementById('viewState').value).length != 0)
        s = "../WebView" + QM_KHBYPASS + href;
    else
        s = "../WebBacktoView" + QM_KHBYPASS + "OpenAgent";
        location.href = s;
}

function webSwitchLang()
{
    var s = "../WebSwitchLang" + QM_KHBYPASS + "OpenAgent&href=" + escape(location.href);
    location.href = s;
}

function loadPdf(doc) {
    var url = "../../blis_pdf.nsf/WebOpenDoc" + QM_KHBYPASS + "OpenAgent&doc=" + doc;
    window.open(url, "BlisPdf", "toolbar=1,location=0, directories=0, status=1, menubar=1, fullscreen=no, scrollbars=2, resizable=1,copyhistory=0,width=820,height=600");
    return false;
}

function setParentFrmValue(form_name, key, value ) {
	if( value == null ) return;
	var p = parent.document.forms[form_name];
	if( p.elements[key] == null ) {
		if(key =="")
			return;
		p.innerHTML += '<input Type=HIDDEN name="' + key  + '" id="' + key + '" value=""/>';
	}
	p.elements[key].value = value;
}

function escapeTerm(s) {
	return escape(s).replace(/\%u/g, "_u");		//BlackBerry does not like %u.
}

function unescapeTerm(s) {
	return unescape(s.replace(/\_u/g, "%u"));
}
