﻿

PB = {
	physNameAC: null,
	keywordAC: null,
	physNames: null,
	keywords: null,
	physNameData: null,
	keywordData: null,
	providertype: 'ProviderType',
	providerTypeValue: null,

	init: function () {
		Dom.addClass(document.body, 'yui-skin-sam');
		this.providertype = document.getElementById(this.providertype);
		this.providerTypeValue = this.providertype.options[this.providertype.selectedIndex].value;
		Event.addListener(this.providertype, 'change', this.setProviderType, this, true);
		this.build();
	},

	build: function () {
		this.buildKeywords();
		this.buildPhysNames();
	},

	buildKeywords: function () {
		YAHOO.util.Connect.asyncRequest('POST', '/load.asp?action=keywords&ProviderType=' + this.providerTypeValue, {
			scope: this,
			success: function (o) {
				this.keywords = YAHOO.lang.JSON.parse(o.responseText);

				// Use a LocalDataSource

				this.keywordData = new YAHOO.util.LocalDataSource(this.keywords);

				// Optional to define fields for single-dimensional array
				this.keywordData.responseSchema = { fields: ["keyword"] };

				// Instantiate the AutoComplete
				this.keywordAC = new YAHOO.widget.AutoComplete("keyword", "keywordContainer", this.keywordData);
				this.keywordAC.prehighlightClassName = "yui-ac-prehighlight";
				this.keywordAC.useShadow = true;
				this.keywordAC.useIFrame = true;
				this.keywordAC.allowBrowserAutocomplete = false;
			}
		});
	},

	buildPhysNames: function () {
		YAHOO.util.Connect.asyncRequest('POST', '/load.asp?action=physicians&ProviderType=' + this.providerTypeValue, {
			scope: this,
			success: function (o) {
				this.physNames = YAHOO.lang.JSON.parse(o.responseText);

				// Use a LocalDataSource
				if (this.physNameData !== null) {
					this.physNameData = null;
				}

				this.physNameData = new YAHOO.util.LocalDataSource(this.physNames);

				// Optional to define fields for single-dimensional array
				this.physNameData.responseSchema = { fields: ["name"] };

				// Instantiate the AutoComplete
				this.physNameAC = new YAHOO.widget.AutoComplete("familyName", "lastNameContainer", this.physNameData);

				this.physNameAC.prehighlightClassName = "yui-ac-prehighlight";
				this.physNameAC.useShadow = true;
				this.physNameAC.useIFrame = true;
				this.physNameAC.allowBrowserAutocomplete = false;
			}
		});
	},

	setProviderType: function () {
		this.providerTypeValue = this.providertype.options[this.providertype.selectedIndex].value;
		this.build();
	}

};

Event.onDOMReady(PB.init, PB, true);
