function stripChars(str)
{
    regx = /[\$\:\^\<\>\[\]\#]/g;
    return str.replace(regx, " ");
}

function stripSingleOperators(str)
{
    regx = /\s*\-\s/g
    return str.replace(regx, "");
}

function fixupLonelyQuotes(str)
{
	var quotes = str.match(/"/g);
	if (quotes != null && quotes.length % 2 == 1)
	{
		return str + "\"";
	}
	else
	{
		return str;
	}
}

function notPhrase(str)
{

    var terms = str.split(" ");
    if (terms.length == 1)
    {
        if (terms[0][0] == "-")
        {
            return true;
        }
    }
    return false;
}

function HeaderTC(callBack)
{
     /**
	 * The parent layout's callback method for this tile.
	 * @private
	 * @type function
	 */
	this.m_callback = callBack;

     /**
     * Element that triggered the event
     * @private
     * @type object
     */
    this.m_element = null;

         /**
     * Name of tile event
     * @private
     * @type string
     */
    this.m_eventName = null;


    this.trim = HeaderTC_trim;

    this.timerID = 0;

}

HeaderTC.prototype = new EventController();
HeaderTC.prototype.constructor = HeaderTC;

HeaderTC.prototype.name = "HeaderTile";
HeaderTC.prototype.sourceFrame;
HeaderTC.prototype.targetFrame;
HeaderTC.prototype.URL;

HeaderTC.prototype.setElement = HeaderTC_setElement;
HeaderTC.prototype.getElement = HeaderTC_getElement;
HeaderTC.prototype.handleEvent = HeaderTC_handleEvent;
HeaderTC.prototype.getEventName = HeaderTC_getEventName;

HeaderTC.prototype.goSearch = HeaderTC_goSearch;
HeaderTC.prototype.getSearchForm = HeaderTC_getSearchForm;
HeaderTC.prototype.onKeyPress = HeaderTC_onKeyPress;
HeaderTC.prototype.loadFrame = HeaderTC_loadFrame;
HeaderTC.prototype.loadFrames = HeaderTC_loadFrames;
HeaderTC.prototype.ssMouseIn = HeaderTC_ssMouseIn;
HeaderTC.prototype.ssMouseOut = HeaderTC_ssMouseOut;
HeaderTC.prototype.setSavedSearch = HeaderTC_setSavedSearch;
HeaderTC.prototype.removeSavedSearch = HeaderTC_removeSavedSearch;
HeaderTC.prototype.insertSearchItem = HeaderTC_insertSearchItem;
HeaderTC.prototype.removeSearchItem = HeaderTC_removeSearchItem;
HeaderTC.prototype.ssTextMouseIn = HeaderTC_ssTextMouseIn;
HeaderTC.prototype.ssTextMouseOut = HeaderTC_ssTextMouseOut;
HeaderTC.prototype.ssTextOnClick = HeaderTC_ssTextOnClick;
HeaderTC.prototype.ssDeleteOnClick = HeaderTC_ssDeleteOnClick;
HeaderTC.prototype.ssHighlight = HeaderTC_ssHighlight;
HeaderTC.prototype.ssClearHighlight = HeaderTC_ssClearHighlight;
HeaderTC.prototype.ssClick = HeaderTC_ssClick;
HeaderTC.prototype.isSavedSearch = HeaderTC_isSavedSearch;
HeaderTC.prototype.ssMouseBodyOver = HeaderTC_ssMouseBodyOver;

HeaderTC.prototype.lastSearchForm = "advanced";
HeaderTC.prototype.getTOCWidth = HeaderGetTOCWidth;


function HeaderTC_getEventName()
{
    return this.m_eventName;
}


function HeaderTC_setElement(element)
{
    this.m_element = element;
}

function HeaderTC_getElement()
{
    return this.m_element;
}

function HeaderTC_handleEvent(name, event, element)
{
    this.m_eventName = name;
    this.setEvent(event);
    this.m_element = element;
    return this.m_callback(this);
}

function HeaderTC_trim(value)
{
    var w_space = String.fromCharCode(32);
    var v_length = value.length;
    var strTemp = "";

    if(v_length < 0)
    {
        return "";
    }
    var iTemp = v_length -1;

    while(iTemp > -1)
    {
        if(value.charAt(iTemp) == w_space){}
        else
        {
            strTemp = value.substring(0,iTemp +1);
            break;
        }
        iTemp = iTemp-1;
    }
    return strTemp;
}

function HeaderTC_goSearch(type, t)
{
    terms = document.getElementById("searchText");
    terms.value = this.trim(terms.value);
    terms.value = stripChars(terms.value);
    terms.value = stripSingleOperators(terms.value);
    	terms.value = fixupLonelyQuotes(terms.value);



    if (notPhrase(terms.value) == true)
    {
        alert("Can not perform a \"not\" search by itself. Include atleast one other search term with a \"not\" search.");
        return false;
    }
    else
    {
        t = this.trim(terms.value);
        if (t.length > 0)
        {
        
            ret = this.handleEvent('search', type, terms);
            if (ret)
            {
                homeFrame.leftFrame.LCO.tocTile.setShowCheckBoxes(0);
            }
            return ret;
        }
        else
        {
            alert("Enter search terms to perform a search");
            return false;
        }
    }
}

function HeaderTC_onKeyPress(event, terms)
{
	var keycode = 0;
	if (window.event)
	{
		keycode = window.event.keyCode;
	}
	else if (event)
	{
		keycode = event.which;
	}

	if(keycode == 13)
	{
		this.goSearch(event, terms);
	}
}



function HeaderTC_loadFrame(srcFrameId, targetFrameId, url)
{
    this.sourceFrame = srcFrameId;
    this.targetFrame = targetFrameId;
    this.URL         = url;
    this.handleEvent("tab", "click", null);
    homeFrame.leftFrame.LCO.tocTile.setShowCheckBoxes(0);
}

function HeaderGetTOCWidth() {
return homeFrame.leftFrame.document.body.clientWidth;
}

function HeaderTC_loadFrames(srcFrameId, targetFrameId1, targetFrameId2, url1, url2)
{
    var srcFrameObj = document.getElementById(srcFrameId);

    var docObj = null;

    // Document logic for Internet Explorer
    if (srcFrameObj.contentWindow)
    {
        docObj = srcFrameObj.contentWindow.document;
    }
    // Document logic for Mozilla
    else if (srcFrameObj.contentDocument)
    {
        docObj = srcFrameObj.contentDocument;
    }

    var targetFrameObj1 = docObj.getElementById(targetFrameId1);
//    var targetFrameObj2 = docObj.getElementById(targetFrameId2);

    targetFrameObj1.contentWindow.location.href = url1;
//    targetFrameObj2.contentWindow.location.href = url2;
    homeFrame.leftFrame.LCO.tocTile.setShowCheckBoxes(1);

}

function HeaderTC_getSearchForm(lastForm)
{
    if (this.lastSearchForm != null && this.lastSearchForm.length > 0)
    {
        if (this.lastSearchForm == "advanced")
        {
            this.loadFrames('homeFrame', 'rightFrame', 'leftFrame', 'search.htm?view=advanced', 'template.htm?view=tocQuery');
        }
        else
        {
            this.loadFrames('homeFrame', 'rightFrame', 'leftFrame', 'search.htm?view=boolean', 'template.htm?view=tocQuery');
        }
    }
    else
    {
        this.loadFrames('homeFrame', 'rightFrame', 'leftFrame', 'search.htm?view=boolean', 'template.htm?view=tocQuery');
    }
    homeFrame.leftFrame.LCO.tocTile.setShowCheckBoxes();
}

function HeaderTC_ssMouseIn(id, thisObj)
{
    var obj = document.getElementById(id);

    if (this.timerID > 0)
    {
        clearTimeout(this.timerID);
        this.timerID = 0;
    }
    if (thisObj)
    {
        obj.style.left = thisObj.offsetLeft;
        obj.style.top = thisObj.offsetTop + 12;
    }
    obj.style.display = '';
    if (thisObj)
    {
        var winWidth;

        if (window.innerWidth)
        {
            winWidth = window.innerWidth;
        }
        else
        {
            winWidth = document.body.clientWidth;
        }
        if ((thisObj.offsetLeft + obj.offsetWidth) > winWidth)
        {
            obj.width = obj.offsetWidth - ((thisObj.offsetLeft + obj.offsetWidth) - winWidth) - 20;
        }
    }
}

function HeaderTC_ssMouseOut(e, id)
{
    var obj = document.getElementById(e);
    if (!e)
	    e = window.event;

    var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;

    if (reltg)
    {
        if (reltg.id == "span_" + id || reltg.id == id)
	    {
		    return;
	    }
    }
    obj.style.display = 'none';
}

function HeaderTC_ssMouseBodyOver()
{
    var obj = document.getElementById('ssTable');
    if (this.timerID == 0 && obj.style.display == '')
    {
        this.timerID = window.setTimeout("var obj = document.getElementById('ssTable'); obj.style.display = 'none'", 1000);
    }
}

function HeaderTC_ssClick(id, thisObj)
{
    var obj = document.getElementById(id);

    if (obj.style.display == '')
    {
        obj.style.display = 'none';
    }
    else
    {
        var allTDs = obj.getElementsByTagName('td');

        for(i=0; i < allTDs.length; i++)
        {
            allTDs[i].style.backgroundColor = "white";
        }
        LCO.headerTile.ssMouseIn(id, thisObj);
    }
}

function HeaderTC_removeSavedSearch(name)
{
	this.handleEvent("removeSavedSearch", null, name);
}

function HeaderTC_setSavedSearch(name)
{
	this.handleEvent("setSavedSearch", null, name);
}

function HeaderTC_ssTextOnClick()
{
	LCO.headerTile.setSavedSearch(this.id);
}

function HeaderTC_ssDeleteOnClick()
{
	LCO.headerTile.removeSavedSearch(this.id);
}

function HeaderTC_insertSearchItem(name, id)
{
	var tableObj = document.getElementById("ssTable");
	var rowObj;
	var cellObj;
	if (tableObj.rows.length == 1)
	{
		rowObj = tableObj.rows[0];
		cellObj = rowObj.cells[0];

		if (cellObj.id == "td0")
		{
			tableObj.deleteRow(0);
		}
	}
	rowObj = tableObj.insertRow(-1);
	// insert text cell
	cellObj = rowObj.insertCell(-1);
	cellObj.onmouseover = HeaderTC_ssTextMouseIn;
	cellObj.onmouseout = HeaderTC_ssTextMouseOut;
	cellObj.onclick = HeaderTC_ssTextOnClick;
	cellObj.style.cursor = 'default';
	cellObj.id = String(id);
	cellObj.innerHTML = name;
	//cellObj.innerHTML = '<TD onclick="LCO.headerTile.setSavedSearch(\'' + id + '\')" onMouseOver="LCO.headerTile.ssHighlight(event, this);" onMouseOut="LCO.headerTile.ssClearHighlight(this);" style="cursor: default">' + name + '</TD>';
	// insert delete cell
	cellObj = rowObj.insertCell(-1);
	cellObj.id = String(id);
	cellObj.onmouseover = HeaderTC_ssTextMouseIn;
	cellObj.onmouseout = HeaderTC_ssTextMouseOut;
	cellObj.onclick = HeaderTC_ssDeleteOnClick;
	cellObj.innerHTML = "<IMG SRC='images/delete.gif' ALT='delete' WIDTH='16' HEIGHT='15' BORDER='0'>";
	//cellObj.innerHTML = '<TD align="right" id="' + id + '" onclick="LCO.headerTile.removeSavedSearch(\'' + id + '\')" onMouseOver="LCO.headerTile.ssHighlight(event, this);" onMouseOut="LCO.headerTile.ssClearHighlight(this);"><IMG SRC="images/delete.gif" ALT="delete" WIDTH="16" HEIGHT="15" BORDER="0"></TD>';
	alert("The search was successfully saved.");
}

function HeaderTC_removeSearchItem(name)
{
	var tableObj = document.getElementById("ssTable");
	var index = -1;
	var rowObj, cellObj;
	for (var i=0; i<tableObj.rows.length; i++)
	{
		rowObj = tableObj.rows[i];
		cellObj = rowObj.cells[1];
		if (cellObj.id == name)
		{
			index = i;
			break;
		}
	}
	if (index > -1)
	{
		tableObj.deleteRow(index);
		if (tableObj.rows.length == 0)
		{
			rowObj = tableObj.insertRow(tableObj.rows.length);
			cellObj = rowObj.insertCell(-1);
			cellObj.id = "td0";
			cellObj.innerHTML = "No saved searches";
		}
	}
}

function HeaderTC_ssTextMouseIn(e)
{
    HeaderTC_ssHighlight(e, this);
}

function HeaderTC_ssTextMouseOut()
{
    HeaderTC_ssClearHighlight(this);
}

function HeaderTC_ssHighlight(e, obj)
{
	if (!e)
		e = window.event;

	var reltg = (e.relatedTarget) ? e.relatedTarget : e.fromElement;

    if (reltg)
    {
        if (reltg.nodeName == 'TD')
	    {
		    reltg.style.backgroundColor = 'white';
        }
    }
    obj.style.backgroundColor = "#DFDFC8";
    obj.style.cursor = "pointer";
}

function HeaderTC_ssClearHighlight(obj)
{
	obj.style.backgroundColor = 'white';
    obj.style.cursor = "default";
}

function HeaderTC_isSavedSearch(name)
{
	var rowObj, cellObj;

	var tableObj = document.getElementById("ssTable");
	var rowLength = tableObj.rows.length;
	for (var i=0; i<rowLength; i++)
	{
		rowObj = tableObj.rows[i];
		cellObj = rowObj.cells[0];
		if (name == cellObj.innerHTML)
		{
			return true;
		}
	}

	return false;
}

