// ----------------------------------------------------
// save tile client side functions
// ----------------------------------------------------
// Componay: FAST Search & Transfer
// Copyright 2005 All Rights Reserved
// ----------------------------------------------------
// Author: Brett Jorgensen
// Created: July 26, 2005
// -------------------------------------------------------------------------------------
var xmlReq = null;

function SaveTC(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;
    
     /**
     * ID of saved search that triggered the event
     * @private
     * @type object
     */
    this.m_id = null;
}

SaveTC.prototype = new EventController();
SaveTC.prototype.name = "saveTile";


SaveTC.prototype.setElement = function (element)
{
    this.m_element = element;
}

SaveTC.prototype.getElement = function ()
{
    return this.m_element;
}

SaveTC.prototype.setID = function (id)
{
    this.m_id = id;
}

SaveTC.prototype.getID = function ()
{
    return this.m_id;
}

SaveTC.prototype.sendEvent = function (name, element, id)
{
    this.setEvent(name);
    this.setElement(element);
    this.setID(id);
    return this.m_callback(this);
}

SaveTC.prototype.onSaveSearchKeyPress = function(event, obj)
{
    var keycode = 0;
    if (window.event)
    {
        keycode = window.event.keyCode;
    }
    else if (event)
    {
        keycode = event.which;
    }

    if(keycode == 13)
    {
        this.onSaveSearch(obj.id);
        return false;
    }
}

SaveTC.prototype.onSaveSearch = function(id)
{
    var name = document.getElementById(id).value;
    if (name == "")
    {
        alert(SAVE_SPECIFY_SEARCH_NAME);
        return;
    }
    this.sendEvent("E_SAVE_SEARCH", name);
}

SaveTC.prototype.saveBookmark = function (object)
{
    var name = object.getAttribute("name");
    var key = object.getAttribute("key");
    this.sendDomRequest("save.htm?action=savebookmark&name=" + name + "&keytype=tocid&key=" + key);
}

SaveTC.prototype.removeBookmark = function (id)
{
    this.sendDomRequest("save.htm?action=removebookmark&id=" + id);
}

SaveTC.prototype.saveSearch = function (name)
{
    this.sendDomRequest("save.htm?action=savesearch&name=" + name);
}

SaveTC.prototype.removeSearch = function (id)
{
    this.sendDomRequest("save.htm?action=removesearch&id=" + id);
}

SaveTC.prototype.sendDomRequest = function (url)
{
    try
    {
        // branch for native XMLHttpRequest object
        if (window.XMLHttpRequest)
        {
            xmlReq = new XMLHttpRequest();
            xmlReq.onreadystatechange = this.domCallback;
            xmlReq.open("POST", url, true);
            xmlReq.send(null);
        }
        // branch for IE/Windows ActiveX version
        else if (window.ActiveXObject)
        {

            xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
            if (xmlReq)
            {
                xmlReq.onreadystatechange = this.domCallback;
                xmlReq.open("POST", url, true);
                xmlReq.send();
            }
        }
        else
        {
            alert("SAVE ERROR: Requires at least Netscape 6.2, Internet Explorer 5.5, Firefox 1.0, or Safari 1.2.4.");
        }
    }
    catch (e)
    {
        alert("SAVE ERROR: Verify that the server is available.");
    }
}

SaveTC.prototype.domCallback = function ()
{
    if (xmlReq != null)
    {
        if (xmlReq.readyState == 4)
        {
            if (xmlReq.status != 200)
            {
                alert("SAVE ERROR: There was a problem retrieving the XML data:\n" + xmlReq.statusText);
            }
            else
            {
                var domDoc = xmlReq.responseXML;
                var error = domDoc.getElementsByTagName("error")[0].firstChild.nodeValue;
                if (error != "null")
                {
                    var saveName = domDoc.getElementsByTagName("savename")[0].firstChild.nodeValue;
					var saveId = domDoc.getElementsByTagName("saveid")[0].firstChild.nodeValue;
					if (error == "ERROR_SEARCH_ID_EXISTS")
                    {
                        alert(SAVE_ERROR_SEARCH_ID_EXISTS + saveId);
                    }
                    else if (error == "ERROR_BOOKMARK_ID_EXISTS")
                    {
                        alert(SAVE_ERROR_BOOKMARK_ID_EXISTS + saveId);
                    }
					else if (error == "ERROR_SAVING_SEARCH")
                    {
                        alert(SAVE_ERROR_SAVING_SEARCH + saveName);
                    }
                    else if (error == "ERROR_REMOVING_SEARCH")
                    {
                        alert(SAVE_ERROR_REMOVING_SEARCH + saveName);
                    }
					else if (error == "ERROR_SAVING_BOOKMARK")
                    {
                        alert(SAVE_ERROR_SAVING_BOOKMARK + saveName);
                    }
                    else if (error == "ERROR_REMOVING_BOOKMARK")
                    {
                        alert(SAVE_ERROR_REMOVING_BOOKMARK + saveName);
                    }
					else if (error == "ERROR_MISSING_PARAMETER")
					{
						alert(SAVE_ERROR_MISSING_PARAMETER);
					}
                    else
                    {
                        alert(error);
                    }
                }
                else
                {
                    var action = domDoc.getElementsByTagName("action")[0].firstChild.nodeValue;
                    if (action == "null")
                    {
                        alert("SAVE ERROR: Invalid response returned by the server.");
                        return;
                    }

                    var id = domDoc.getElementsByTagName("saveid")[0].firstChild.nodeValue;
                    var name = domDoc.getElementsByTagName("savename")[0].firstChild.nodeValue;
                    if (action == "savesearch")
                    {
                        LCO.saveTile.sendEvent("E_SAVE_SEARCH_ADDED", name, id);
                    }
                    else if (action == "removesearch")
                    {
                        LCO.saveTile.sendEvent("E_SAVE_SEARCH_REMOVED", name, id);
                    }
                }
            }
        }
    }
}
