﻿// © Copyright Strategy Builders Inc. 2000-2009. All rights reserved.
//   
// The entire contents of this file is protected by International and
// U.S.Copyright Laws. Unauthorized reproduction, distribution, 
// redistribution and reverse engineering of any or all portion of the code 
// contained in this file is strictly prohibited and may
// result in severe civil and criminal penalties and will be
// prosecuted to the maximum extent possible under the law.
//  
// RESTRICTIONS 
//  
// THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES  
// ARE CONFIDENTIAL AND PROPRIETARY TRADE  
// SECRETS OF Strategy Builders Inc. 
//  
// THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED
// FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE  
// COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE 
// AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  
// AND PERMISSION FROM Strategy Builders Inc. 
//  
// CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON 
// ADDITIONAL RESTRICTIONS.
//  

function SearchTermSuggestControl(id, data, cntrl) {
    ContainerControl.call(this, id);

    this._searchTermSuggestItems = new RepeaterControl('SearchTermSuggestItems', 'SearchTermSuggestItem', data);

    this.addControl(this._searchTermSuggestItems);

    this._initiatorHTMLControl = cntrl;

}

SearchTermSuggestControl.prototype = new ContainerControl();

SearchTermSuggestControl.prototype.setDataSource = function(data) {
    this._searchTermSuggestItems.setCollection(data);
}

SearchTermSuggestControl.prototype.setInitiatorHTMLControl = function(ctrl) {
    this._initiatorHTMLControl = cntrl;
}

SearchTermSuggestControl.prototype.setSearchTermSuggestItemsTemplate = function(templateText) {
    this._searchTermSuggestItems.setTemplate(templateText);
}

SearchTermSuggestControl.prototype.SelectSuggestion_EventHandler = function(e) {
    //    alert('SelectSuggestion_EventHandler ');

    var targ;
    // get event if not passed
    if (!e) var e = window.event;

    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    var controls = targ.id.split('_');

    var me;
    me = eval(controls[0]);

    var numCharCode;
    var elTarget;
    var strType;

    // get character code of key pressed
    if (e.keyCode) numCharCode = e.keyCode;
    else if (e.which) numCharCode = e.which;

    // if this is a return
    if (numCharCode == 13) {
        me._initiatorHTMLControl.value = $get(targ.id).value;
        me.getEventHandlers()['CloseControl'](me);
        if (me._initiatorHTMLControl != null) {
            me._initiatorHTMLControl.focus();
        }
    }
    else if (numCharCode == 38) {
        if ($get(targ.id).selectedIndex == 0) {
            me.getEventHandlers()['CloseControl'](me);
            if (me._initiatorHTMLControl != null) {
                me._initiatorHTMLControl.focus();
            }
        }
    }
}

SearchTermSuggestControl.prototype.SelectSuggestionOnDblClk_EventHandler = function(e) {
    //    alert('SelectSuggestion_EventHandler ');

    // get event if not passed
    if (!e) var e = window.event;

    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3 /*|| targ.nodeType == 1*/) // defeat Safari bug // for ie nodeType is 1 so it was taking the parent node which was td
        targ = targ.parentNode;

    var targ;
    getEventTarget(e);
    
    var controls = targ.id.split('_');

    var me;
    me = eval(controls[0]); //document.getElementById(targ.id);

    var numCharCode;
    var elTarget;
    var strType;

    // get character code of key pressed
    if (e.keyCode) numCharCode = e.keyCode;
    else if (e.which) numCharCode = e.which;

    me._initiatorHTMLControl.value = document.getElementById(targ.id).value; //$get(e.srcElement.id).value;
    me.getEventHandlers()['CloseControl'](me);
    if (me._initiatorHTMLControl != null) {
        me._initiatorHTMLControl.focus();
    }
}

SearchTermSuggestControl.prototype.SearchTermSuggest_EventHandler = function (e) {

    var targ;
    // get event if not passed
    if (!e) var e = window.event;

    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    var controls = targ.id.split('_');

    var className;
    className = document.getElementById(controls[0]).getAttribute("class");
    if (className == null) className = document.getElementById(controls[0]).className;

    var me;
    me = eval(className);

    var numCharCode;
    var elTarget;
    var strType;

    // get character code of key pressed
    if (e.keyCode) numCharCode = e.keyCode;
    else if (e.which) numCharCode = e.which;

    // if this is a return
    if (numCharCode == 40) {
        if ($get(this.tag + '_0') != null) {
            $get(this.tag + '_0').focus();
            $get(this.tag + '_0').selectedIndex = 0;
        }
    }
    else if (numCharCode == 13) {
        // submit the form
        return true;
    }
    else {
        me.getEventHandlers()['CallWebService'](me);

        //CallSearchSuggestService(this);
        //SearchTermSuggest.Suggest($get('q').value, onWSSuggestionsRetrieved, OnWSError, OnWSTimeout);
    }


}


SearchTermSuggestControl.prototype.CloseControl_EventHandler = function(e) {

    var targ;
    // get event if not passed
    if (!e) var e = window.event;

    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    var controls = targ.id.split('_');

    var me;
    me = eval(controls[0]);

    var numCharCode;
    var elTarget;
    var strType;

    me.getEventHandlers()['CloseControl'](me);

}


SearchTermSuggestControl.prototype.RegisterEvents = function() {
    ContainerControl.prototype.RegisterEvents.call(this);

    for (var ControlEventName in this.getRegisterEventExpressions()) {
        //alert(this.getRegisterEventExpressions()[ControlEventName]);
        eval(this.getRegisterEventExpressions()[ControlEventName]);
    }
    for (var i = 0; i < this.getChildCount(); i++) {
        //alert(this.getChild(i).getId());
        for (ControlEventName in this.getChild(i).getRegisterEventExpressions()) {

            //            alert(this.getChild(i).getRegisterEventExpressions()[ControlEventName]);
            eval(this.getChild(i).getRegisterEventExpressions()[ControlEventName]);
        }
    }

    if (this._initiatorHTMLControl != null) {
        this._initiatorHTMLControl.onkeyup = this.SearchTermSuggest_EventHandler;
    }
}


// Following function do not belong here because they are not part of
// the control but, just because they go on evenry page where we have
// control we have added them here for convenience
// They could be moved to other file later.

function jsProductListPage() {
    location.href = 'ProductList.aspx?q=' + encodeURIComponent($get('q').value);
}

function onWSSuggestionsRetrieved(suggestions) {
    var myJSONText = JSON.stringify(suggestions);
    $get('txtErrorBox').value = myJSONText;

    $get('spnSearchTermSuggest').style.visibility = 'visible';
    SearchTermSuggestionsControl.setDataSource(suggestions);
    $get('spnSearchTermSuggest').innerHTML = SearchTermSuggestionsControl.getHTML();
    SearchTermSuggestionsControl.RegisterEvents();
}

function closeSuggestions(obj) {
    $get('spnSearchTermSuggest').style.visibility = 'hidden';
    $get('spnSearchTermSuggest').innerHTML = '';
}


function CallSearchSuggestService(obj) {
    var SearchTermSuggest = new StoreFrontService.SearchTermSuggest();
    SearchTermSuggest.Suggest($get('q').value, onWSSuggestionsRetrieved, OnWSError, OnWSTimeout);
}

function jsOnSearchBoxKeyPressed(e) {
    var numCharCode;
    var elTarget;
    var strType;

    var targ;
    if (!e) var e = window.event;
    if (e != undefined) {
        if (e.target) targ = e.target;
        else if (e.srcElement) targ = e.srcElement;
    }

    if (targ != undefined) 
        if (targ.nodeType == 3) // defeat Safari bug
            targ = targ.parentNode;

    // get character code of key pressed
    if (e.keyCode) numCharCode = e.keyCode;
    else if (e.which) numCharCode = e.which;

    // if this is a return
    if (numCharCode == 13) {
        location.href = 'ProductList.aspx?q=' + encodeURIComponent($get('q').value);
        return true;
    }
    return false;
}


