var showDebugMessages = false;



function getParams() {
    var qString = new String(window.location);
    var queryStringAr = qString.split("?");

    paramsPairs = queryStringAr[1].split("&");

    var retParams = new Array();
    var buffParam;
    for (var i = 0; i < paramsPairs.length; i++) {
        buffParam = paramsPairs[i].split("=");
        retParams[i] = {
            name : buffParam[0],
            value : buffParam[1]
        }
    }
    return retParams;
}

function buildParams(params) {
    var loc = window.location;
    var retString = window.location.protocol + "//" + window.location.host + window.location.pathname + "?";
    for (var i = 0; i < params.length - 1; i++) {
        retString += params[i].name + "=" + params[i].value + "&";
    }
    retString += params[params.length - 1].name + "=" + params[params.length - 1].value;
    return retString;
}

function processContextSearchold() {
    return;
    var searchStr = document.getElementById("searchWord").value;
    var replacedWord = false;
    var foundContextMode = false;

    params = getParams();

    for (var i = 0; i < params.length; i++) {
        if (params[i].name == "teiSearchWord") {
            replacedWord = true;
            params[i].value = searchStr;
        }

        if (params[i].name == "contextSearchMode") {
            foundContextMode = true;
        }
    }

    if (!replacedWord) {
        params[params.length] = {
            name: "teiSearchWord",
            value: searchStr
        }
    }
    if (!foundContextMode) {
        params[params.length] = {
            name: "contextSearchMode",
            value: "true"
        }
    }

    var newLoc = buildParams(params);

    window.location = newLoc;
}

function processContextSearch() {
    if (!xhr) {
        setTimeout("processContextSearch()", 500);
        return;
    }
    var searchStr = document.getElementById("searchWord").value;
    var xhri = xhrRequest();
    fromContextMode = true;
    searchPutStatusMessage("Search Initiated.....");
    xhr[xhri].open('GET', "search.metaInfo?pid=" + pid + "&id=" + segId + "&contentModel=" + contentModel + "&teiSearchWord=" + searchStr, true);
    xhr[xhri].onreadystatechange = function() {
        if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
            parceContextSearch(xhr[xhri].responseXML);
            xi[xhri] = 1;
            xhr[xhri] = null;
            searchPutStatusMessage("Search Completed");
        }
    };
    xhr[xhri].send(null);
}


//============================ High Lights ===============================
function setHighLight(element) {
    //------Synchronize scrolling------------
    var divElement = document.getElementById("transcriptArea");
    divElement.scrollTop = (element.offsetTop - 50);
    //------Element HigghLight---------------
    element.className = "highLighted";
    element.setAttribute("class", "highLighted")
    element.setAttribute("className", "highLighted");
}

function removeHighLight(element) {
    element.className = "";
    element.removeAttribute("class");
    element.removeAttribute("className");
}
//==================================================================
function searchPostMessage(msg, debug) {
    if (msg != "") {
        if (debug){
         if (showDebugMessages){
           alert(msg);
         }
        } else {
            alert(msg);
        }
    }
}
//==================================================================
/*function doHighlightOld(bodyText, highlightStartTag, highlightEndTag)
{
  if ((!highlightStartTag) || (!highlightEndTag)) {
    //highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
    //highlightEndTag = "</font>";
    highlightStartTag = "<font id=\"highlight"+occCount+"\">"
    highlightEndTag = "</font>";
  }

  regExpB = new RegExp("<HI>","");
  regExpE = new RegExp("</HI>","");

  newText = bodyText.replace(regExpB,highlightStartTag)
  newText = newText.replace(regExpE,highlightEndTag)

  return newText;
}*/

function doHighlight(element)
{
    var retEl = document.createElement("font");
    retEl.setAttribute("id", "highlight" + occCount);
	var textContent;
//	if (element.ownerDocument != document){
        textContent = document.importNode(element.firstChild, true);
//	} else {
//		textContent = element.firstChild;
//	}
    retEl.appendChild(textContent);
    occCount++;
    return retEl;
}

if (typeof document.importNode != "function") {
    document.importNode = function(importedNode, deep) {
        var newNode;

        if (importedNode.nodeType == 1) { // Node.ELEMENT_NODE
            newNode = document.createElement(importedNode.nodeName);
            for (var i = 0; i < importedNode.attributes.length; i++) {
                var attr = importedNode.attributes[i];
                if (attr.nodeValue != null && attr.nodeValue != '') {
                    newNode.setAttribute(attr.name, attr.nodeValue);
                }
            }
            if (typeof importedNode.style != "undefined")
                newNode.style.cssText = importedNode.style.cssText;
        } else if (importedNode.nodeType == 3) { // Node.TEXT_NODE
            newNode = document.createTextNode(importedNode.nodeValue);
        }

        if (deep && importedNode.hasChildNodes()) {
            for (var i = 0; i < importedNode.childNodes.length; i++) {
                newNode.appendChild(
                        document.importNode(importedNode.childNodes[i], true)
                        );
            }
        }

        return newNode;
    }
}

function searchPutStatusMessage(msg){
    window.status = msg;
}

//Element.prototype.nativeRemoveChild = Element.prototype.removeChild;
//Element.prototype.removeChild = function(el){
//    Cacher.remove(el.getAttribute("id"))
//}

/*
Node.prototype.nativeAppendChild = Node.prototype.appendChild;
Node.prototype.appendChild = function(el){
    alert("appendChild");
    Cacher.set(el.getAttribute("id"))
    this.nativeAppendChild(el);
}

Node.prototype.nativeInsertBefore = Node.prototype.insertBefore;
Node.prototype.insertBefore = function(newElement, referenceElement){
    alert("insertBefore");
    Cacher.set(newElement.getAttribute("id"))
    this.nativeInsertBefore(newElement, referenceElement);
}
*/

function searchGetElementById(id,tagName){
    //alert("searchGetElementById(\""+id+"\",\""+tagName+"\")");
    var inputs = document.getElementsByTagName(tagName);
    var descField = null;
    for (var i = 0; i < inputs.length; i++) {
        if (inputs.item(i).getAttribute('id') == id) {
            return inputs.item(i);
        }
    }
    return  null;
}