/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/LICENSE-M1.txt
 *
 * @category   AW
 * @package    AW_Seacrhautocomplete
 * @copyright  Copyright (c) 2003-2009 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/LICENSE-M1.txt
 */
function replaceAll( str, searchTerm, replaceWith, ignoreCase )	{
	var regex = "/"+searchTerm+"/g";
	if( ignoreCase ) regex += "i";
	return str.replace( eval(regex), replaceWith );
}

YAHOO.example.CustomFormatting = (function(){
    var oDS = new YAHOO.util.ScriptNodeDataSource(installPath + "/searchautocomplete/ajax/suggest/");
    oDS.responseSchema = {
        resultsList: "ResultSet.Result",
        fields: ["Title","Url","Image"]
    };
    oDS.scriptCallbackParam = "callback";
    var oAC = new YAHOO.widget.AutoComplete("myInput","myContainer", oDS);
    oAC.generateRequest = function(sQuery) {
        return "?q=" + sQuery +'&storeId='+storeId;
    };
    oAC.resultTypeList = false;
    oAC.queryDelay = 0.5;
    oAC.minQueryLength = 3;
    oAC.setHeader('Most relevant matches shown. Click Go button for more items.');
    oAC.formatResult = function(oResultData, sQuery, sResultMatch) {
        var img = "", nonimg = "";
        img = "<img src=\""+ oResultData.Image + "\">";
        return "<div class=\"result\">" + img + " <span class=\"name\">" + sResultMatch + "</span></div>";
    };
    var validateForm = function() {
        return true;
    };
    oAC.itemSelectEvent.subscribe(function(sType, aArgs){
    	var myInput=document.getElementById('myInput');
    	var URL = myInput.value;
    	URL = replaceAll(URL, '<strong>', '', '');
    	URL = replaceAll(URL, '<\\/strong>', '', '');
    	// strReplaceAll = strText.replace( new RegExp( "th", "g" ), "[X]" );
    	//URL = URL.replace( new RegExp(/<strong>/, "g" ),'');
    	//URL = URL.replace(/<\/strong>/,'');
		myInput.value = URL;
    	window.location = aArgs[2]['Url'];
    });
    oAC.dataRequestEvent.subscribe(function(){
    	var myInput=document.getElementById('myInput');
    	myInput.style.backgroundImage = 'url(' + skinPath + 'images/al.gif)';
    	myInput.style.backgroundRepeat = 'no-repeat';
    	myInput.style.backgroundPosition = 'right';
    });
    oAC.dataReturnEvent.subscribe(function(){
    	var myInput=document.getElementById('myInput');
    	myInput.style.backgroundImage = '';
    });
    oAC.dataErrorEvent.subscribe(function(){
    	var myInput=document.getElementById('myInput');
    	myInput.style.backgroundImage = '';
    });



    return {
        oDS: oDS,
        oAC: oAC,
        validateForm: validateForm
    }
})();
