/*
Copyright (c) 2007, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
version: 2.3.0
*/
 /**
 * The AutoComplete control provides the front-end logic for text-entry suggestion and
 * completion functionality.
 *
 * @module autocomplete
 * @requires yahoo, dom, event, datasource
 * @optional animation, connection
 * @namespace YAHOO.widget
 * @title AutoComplete Widget
 */

/**
 * The AutoComplete class provides the customizable functionality of a plug-and-play DHTML
 * auto completion widget.  Some key features:
 * <ul>
 * <li>Navigate with up/down arrow keys and/or mouse to pick a selection</li>
 * <li>The drop down container can "roll down" or "fly out" via configurable
 * animation</li>
 * <li>UI look-and-feel customizable through CSS, including container
 * attributes, borders, position, fonts, etc</li>
 * </ul>
 *
 * @class AutoComplete
 * @constructor
 * @param elInput {HTMLElement} DOM element reference of an input field.
 * @param elInput {String} String ID of an input field.
 * @param elContainer {HTMLElement} DOM element reference of an existing DIV.
 * @param elContainer {String} String ID of an existing DIV.
 * @param oDataSource {YAHOO.widget.DataSource} DataSource instance.
 * @param oConfigs {Object} (optional) Object literal of configuration params.
 */

var count = 0;
var arrResult = new Array(25);

var summaryResults = "";

YAHOO.example.ACJson = new function(){
    
    // Add 8080 to the host name for development environment.
    this.oACDS = new YAHOO.widget.DS_XHR("/marketing/rxsaving/findDrug.html", ["ResultSet.Result","Desc"]);
    //this.oACDS = new YAHOO.widget.DS_XHR("http://" + window.location.hostname + ":8080/rxsaving/findDrug.html", ["ResultSet.Result","Desc"]);
    // this.oACDS = new YAHOO.widget.DS_XHR("http://" + window.location.hostname + "/rxsaving/findDrug.html", ["ResultSet.Result","Desc"]);
    this.oACDS.queryMatchContains = true;
    this.oACDS.scriptQueryAppend = "output=json&results=1000"; // Needed for YWS

    // Instantiate AutoComplete
    this.oAutoComp = new YAHOO.widget.AutoComplete("dsearchinput","dsearchcontainer", this.oACDS);
    
    this.oAutoComp.useShadow = false;
    this.oAutoComp.alwaysShowContainer = true;

    this.oAutoComp.formatResult = function(oResultItem, sQuery) {
    	createResultArr(oResultItem[1].Desc, oResultItem[1].Name, oResultItem[1].Id, count);
    	count++;
        /*return oResultItem[1].Desc + " (" + oResultItem[1].Name + oResultItem[1].Id + ")";*/
        return "<div id='d" + count + "'><input type='checkbox' name='chkDrugName' id='chkDrugName"+count+"' value='" + oResultItem[1].Id + "::" + oResultItem[1].Desc + "' onclick='buttonEnable();'><label for='chkDrugName"+count+"' id='chkDrug"+count+"' >&nbsp;&nbsp;" + oResultItem[1].Desc + "</label></div>";
    };
    
    this.oAutoComp.doBeforeExpandContainer = function(oTextbox, oContainer, sQuery, aResults) {
        return true;
    };

    // Stub for form validation
    this.validateForm = function() {
		var drugDesc = document.searchDrugInfo.searchKey.value;
    	
    	try {
			for (i = 0; i < arrResult.length; i++) {
				if (drugDesc.match(arrResult[i][0])) {
					document.searchDrugInfo.particularDrug.value = arrResult[i][1]; 
					document.searchDrugInfo.id.value = arrResult[i][2]; 
					query = "?query=" + arrResult[i][1];
					break;
				}
			}
    	}
    	catch (e) {
			query = "?query=" + drugDesc;
    	}
    	
    	document.searchDrugInfo.action = "/marketing/rxsaving/findDrug.html" + query ;
        return true;
    };
};

function createResultArr(pDesc, pName, pId, pCount) {
	arrResult[pCount] = new Array(3);
	arrResult[pCount][0] = pDesc;
	arrResult[pCount][1] = pName;
	arrResult[pCount][2] = pId;
};


