﻿

PB = {

	loader: null,
	autocomplete: null,
	autocomplete2: null,
	autocomplete3: null,
	data: null,
	keywords: null,
	datasource: null,
	datasource2: null,
	datasource3: null,
	providertype: null,

	init: function() {
		Dom.addClass(document.body, 'yui-skin-sam');
		this.providertype = document.getElementById('ProviderType');
		this.build();
	},

	build: function() {

		YAHOO.util.Connect.asyncRequest('POST', '/load.asp?action=keywords', {
			scope: this,
			success: function(o) {
				this.keywords = YAHOO.lang.JSON.parse(o.responseText);

				// Use a LocalDataSource
				this.datasource3 = new YAHOO.util.LocalDataSource(this.keywords);

				// Optional to define fields for single-dimensional array
				this.datasource3.responseSchema = { fields: ["Keyword"] };

				// Instantiate the AutoComplete
				this.autocomplete3 = new YAHOO.widget.AutoComplete("keyword", "keywordContainer", this.datasource3);
				this.autocomplete3.prehighlightClassName = "yui-ac-prehighlight";
				this.autocomplete3.useShadow = true;
				this.autocomplete3.useIFrame = true;
				this.autocomplete3.allowBrowserAutocomplete = false;

				YAHOO.util.Connect.asyncRequest('POST', '/load.asp?action=physicians&ProviderType=8', {
					scope: this,
					success: function(o) {
						this.data = YAHOO.lang.JSON.parse(o.responseText);

						// Use a LocalDataSource
						if (this.datasource !== null) {
							this.datasource === null;
						}
						//console.log(this.datasource);
						this.datasource = new YAHOO.util.LocalDataSource(this.data);

						//console.log(this.datasource);
						// Optional to define fields for single-dimensional array
						this.datasource.responseSchema = { fields: ["name"] };

						// Instantiate the AutoComplete
						this.autocomplete = new YAHOO.widget.AutoComplete("familyName", "lastNameContainer", this.datasource);

						// IFrame workaround for IE
						/*var ua = navigator.userAgent.toLowerCase();
						if (ua.indexOf('msie') != -1 && ua.indexOf('opera') < 0) {
						this.autocomplete.useIFrame = true;
						YAHOO.util.Dom.get('useiframe').checked = true;
						}*/

						this.autocomplete.prehighlightClassName = "yui-ac-prehighlight";
						this.autocomplete.useShadow = true;
						this.autocomplete.useIFrame = true;
						this.autocomplete.allowBrowserAutocomplete = false;
						Event.addListener(this.providertype, 'change', this.reload, this, true);
					}
				});
			}
		});

	},

	reload: function() {

		var qs = '/load.asp?action=physicians';
		if (this.providertype.value === '8' || this.providertype.value === '12') {
			qs += '&ProviderType=' + encodeURIComponent(this.providertype.value);
		}

		YAHOO.util.Connect.asyncRequest('POST', qs, {

			scope: this,
			success: function(o) {

				if (o.responseText === null) {
					return;
				}

				var x, newData = YAHOO.lang.JSON.parse(o.responseText);

				this.data.length = 0;

				for (x = 0; x < newData.length; x++) {
					this.data[x] = newData[x];
				}

			}

		});

	}

}

Event.onDOMReady(PB.init, PB, true);