﻿

var SEARCH = {

	data: {
		relatedSpecialties: 'RelatedSpecID',
		specialties: 'SpecID',
		selectedRelated: 'selectedRelSpec',
		refineSearch: 'RefineSearch',
		keyword: 'keyword',
		AdvancedSearch: 'AdvancedSearch',
		hideOptions: 'hideOptions',
		showOptions: 'showOptions',
		relSpecLabel: 'relSpecLabel',
		mapDivs: null
	},

	init: function () {

		this.data.specialties = document.getElementById(this.data.specialties);
		this.data.relatedSpecialties = document.getElementById(this.data.relatedSpecialties);
		this.data.refineSearch = document.getElementById(this.data.refineSearch);
		this.data.keyword = document.getElementById(this.data.keyword);
		this.data.relSpecLabel = document.getElementById(this.data.relSpecLabel);
		if (this.data.relSpecLabel) {
			this.data.relSpecLabel.style.display = 'none';
		}

		this.data.AdvancedSearch = document.getElementById(this.data.AdvancedSearch);
		this.data.showOptions = document.getElementById(this.data.showOptions);
		this.data.hideOptions = document.getElementById(this.data.hideOptions);
		if (this.data.refineSearch && (this.data.relatedSpecialties.options !== null || !this.data.relatedSpecialties.options)) {
			this.data.selectedRelated = document.getElementById(this.data.selectedRelated).value;
			this.doChange_relatedSpecialties();
		}

		Event.addListener(this.data.showOptions, 'click', this.setAdvancedSearch, this, true);
		Event.addListener(this.data.hideOptions, 'click', this.hideAdvancedSearch, this, true);

		Event.addListener(this.data.showOptions, 'mouseover', this.optionLinkMouseOver, this.data.showOptions, true);
		Event.addListener(this.data.hideOptions, 'mouseover', this.optionLinkMouseOver, this.data.hideOptions, true);
		Event.addListener(this.data.showOptions, 'mouseout', this.optionLinkMouseOver, this.data.showOptions, true);
		Event.addListener(this.data.hideOptions, 'mouseout', this.optionLinkMouseOver, this.data.hideOptions, true);

		Event.addListener(this.data.specialties, 'change', this.doChange_relatedSpecialties, this, true);

		if (this.data.AdvancedSearch !== null) {
			this.data.AdvancedSearch.style.display = 'none';
		}
		if (this.data.hideOptions !== null) {
			this.data.hideOptions.style.display = 'none';
		}
	},

	optionLinkMouseOver: function (e, o) {

		if (e.type === 'mouseover') {
			o.style.color = '#6DB33F';
			o.style.borderBottom = '1px dotted #6DB33F';
		}
		else {
			o.style.color = '#004F94';
			o.style.borderBottom = '1px dotted #004F94';
		}
	},


	doChange_relatedSpecialties: function () {

		var specID = this.data.specialties,
		kw = this.data.keyword.value,
		url = '/getSpecialtiesByService.asp';

		if (specID.options) {
			specID = specID.options[specID.selectedIndex].value;
		} else {
			specID = specID.value;
		}
		specID = parseInt(specID, 10);

		if (isNaN(specID)) {
			this.data.relatedSpecialties.selectedIndex = 0;
			this.data.relatedSpecialties.options.length = 1;
			this.data.selectedRelated = null;
			return;
		}

		url += '?specID=' + encodeURIComponent(specID) + '&keyword=' + encodeURIComponent(kw);

		if (this.data.refineSearch) {
			YAHOO.util.Connect.asyncRequest('GET', url, {
				scope: this,
				success: this.setRefineRelatedSpecialties
			});
		} else {
			YAHOO.util.Connect.asyncRequest('GET', url, {
				scope: this,
				success: this.setRelatedSpecialties
			});
		}

	},

	setRefineRelatedSpecialties: function (o) {
		var specs = o.responseText,
		div = this.data.relatedSpecialties,
		arr = this.data.selectedRelated,
		x, c, l, ul, li, i;
		arr = arr.split(',');
		specs = Lang.JSON.parse(specs);
		if (specs.length > 0) {
			this.data.relSpecLabel.style.display = 'block';
			ul = document.createElement('ul');
			for (x = 0; x < specs.length; x++) {
				li = document.createElement('li');
				c = document.createElement('input');
				c.type = 'checkbox';
				c.id = 'relatedSpecID-' + specs[x].id;
				c.value = specs[x].id;
				c.name = 'relatedSpecID';
				c.className = 'SEO-relatedSpec';
				for (i = 0; i < arr.length; i++) {
					if (c.value === arr[i].replace(' ', '')) {
						c.checked = true;
					}
				}
				l = document.createElement('label');
				l.htmlFor = c.id;
				l.innerHTML = specs[x].text;
				li.appendChild(c);
				li.appendChild(l);
				ul.appendChild(li);
			}
			div.appendChild(ul);

		}
	},

	setRelatedSpecialties: function (o) {

		var specs = o.responseText,
		select = this.data.relatedSpecialties,
		current, x, found = false;

		specs = Lang.JSON.parse(specs);

		current = this.data.selectedRelated;
		if (current) {
			current = parseInt(current.value, 10);
		}
		select.selectedIndex = 0;
		select.options.length = 1;

		if (specs.length > 0) {
			for (x = 0; x < specs.length; x++) {
				select[select.options.length] = new Option(specs[x].text, specs[x].id);

				if (specs[x].id === current) {
					select.selectedIndex = select.options.length - 1;
					found = true;
				}
			}

			if (!found) {
				this.data.selectedRelated = null;
			}
		} else {
			select.selectedIndex = select.options.length - 1;
			select[select.options.length] = new Option('No related specialties available.', '');
			select.selectedIndex = 1;
		}

	},

	setAdvancedSearch: function () {
		this.data.AdvancedSearch.style.display = 'block';
		this.data.showOptions.style.display = 'none';
		this.data.hideOptions.style.display = 'block';
	},

	hideAdvancedSearch: function () {
		this.data.AdvancedSearch.style.display = 'none';
		this.data.showOptions.style.display = 'block';
		this.data.hideOptions.style.display = 'none';
	},

	clearForm: function () {
		var t = document.getElementsByTagName('input'), s = document.getElementsByTagName('select'), i;
		for (i = 0; i < t.length; i++) {
			if (t[i].type === 'text') {
				t[i].value = '';
			}
			if (t[i].type === 'checkbox') {
				t[i].checked = false;
			}
		}
		for (i = 0; i < s.length; i++) {
			s[i].selectedIndex = 0;
		}

	},

	newwindow: function (divID) {
		var top, left, mapID, href = '';
		mapID = document.getElementById(divID);
		if (mapID !== null) {
			href = mapID.innerHTML;
		}
		top = screen.availWidth / 3;
		left = 0;
		if (href.length > 0) {
			window.open(href, 'resource', "width=800,height=800,resizable=1,scrollbars=1, top=' + top + ', left=' + left + '");
		}
		else {
			alert('No map data.');
		}

	},

	closewindow: function (href) {
		href.close();
	},

	redirectParent: function (pageID) {
		window.opener.parent.location = 'Default.asp?PageID=' + pageID + '&P=Y';
	}

	//	displayBio: function (id) {
	//		var d = document.getElementById('bio-' + id);
	//		d.style.display = 'block';
	//		d.style.zIndex = '100';
	//		d.style.width = '345px';
	//		d.style.zIndex = '100';
	//		d.style.position = 'absolute';
	//		d.style.overflow = 'hidden';
	//		d.style.padding = '2px 2px';
	//		d.style.margin = '0';
	//		d.style.backgroundColor = '#DFDED8';
	//	},


	//	hideBio: function (id) {
	//		var d;
	//		d = document.getElementById('bio-' + id);
	//		d.style.display = 'none';
	//	}

};
Event.onDOMReady(SEARCH.init, SEARCH, true);

var SEO = {
	data: {
		SpecID: 'SpecID',
		ProviderType: 'ProviderType',
		RelatedSpecID: 'RelatedSpecID',
		specialtyName: 'Specialty',
		keyword: 'keyword',
		relatedSpecialtyName: 'RelatedSpecialty',
		providerTypeName: 'providerTypeName',
		search_btn: 'search_btn',
		returnToResults: 'returnToResults',
		SEOrelatedSpecs: 'SEO-relatedSpec',
		SEOrelatedSpecLabels: []
	},

	init: function () {

		this.data.search_btn = document.getElementById(this.data.search_btn);
		this.data.returnToResults = document.getElementById(this.data.returnToResults);
		this.data.SpecID = document.getElementById(this.data.SpecID);
		this.data.keyword = document.getElementById(this.data.keyword);
		this.data.specialtyName = document.getElementById(this.data.specialtyName);

		Event.addListener(this.data.search_btn, 'click', this.setSEOvalues, this, true);
		//Event.addListener(this.data.returnToResults, 'click', this.setSEOvalues, this, true);

		this.data.ProviderType = document.getElementById(this.data.ProviderType);
		this.data.providerTypeName = document.getElementById(this.data.providerTypeName);

		this.data.RelatedSpecID = document.getElementById(this.data.RelatedSpecID);
		this.data.relatedSpecialtyName = document.getElementById(this.data.relatedSpecialtyName);
		if (this.data.relatedSpecialtyName && this.data.relatedSpecialtyName.value.length > 0) {
			this.data.relatedSpecialtyName.value = '';
		}
		SEO.clear();
	},

	clear: function () {
		this.data.SpecID.selectedIndex = 0;
		this.data.specialtyName.value = '';
		this.data.ProviderType.selectedIndex = 0;
		this.data.providerTypeName.value = '';
		this.data.keyword.value = '';
	},

	setSEOvalues: function () {
		this.setSpecialty();
		this.setRelatedSpecFromDropDown();
		this.setProviderType();
		this.setRelatedSpecsFromCheckBoxes();
	},

	setProviderType: function () {
		if (this.data.ProviderType && this.data.ProviderType.options[this.data.ProviderType.selectedIndex].text !== '- All -') {
			if (this.data.providerTypeName) {
				this.data.providerTypeName.value = this.data.ProviderType.options[this.data.ProviderType.selectedIndex].text;
			}
		} else {
			if (this.data.ProviderType && this.data.ProviderType.options[this.data.ProviderType.selectedIndex].text === '- All -') {
				this.data.providerTypeName.value = 'All Providers';
			}
		}
	},

	setSpecialty: function () {
		if (this.data.SpecID && this.data.SpecID.selectedIndex > 0) {
			this.data.specialtyName.value = this.data.SpecID.options[this.data.SpecID.selectedIndex].text;
		}
		else {
			if (this.data.SpecID && this.data.SpecID.selectedIndex === 0)
				this.data.specialtyName.value = '';
		}
	},

	setRelatedSpecFromDropDown: function () {
		if (this.data.RelatedSpecID && this.data.RelatedSpecID.selectedIndex > 0 && this.data.RelatedSpecID.options[this.data.RelatedSpecID.selectedIndex].text !== 'No related specialties available.') {
			this.data.relatedSpecialtyName.value += this.data.RelatedSpecID.options[this.data.RelatedSpecID.selectedIndex].text;
		}
		else {
			this.data.relatedSpecialtyName.value = '';
		}
	},

	setRelatedSpecsFromCheckBoxes: function () {
		var labels = document.getElementsByTagName('label');
		this.data.SEOrelatedSpecs = YAHOO.util.Dom.getElementsByClassName(this.data.SEOrelatedSpecs);

		for (var x in this.data.SEOrelatedSpecs) {
			if (this.data.SEOrelatedSpecs.hasOwnProperty(x)) {
				if (this.data.SEOrelatedSpecs[x].checked === true) {
					this.data.SEOrelatedSpecLabels.push(this.data.SEOrelatedSpecs[x].id);
				}
			}
		}

		for (var w in labels) {
			for (var x in this.data.SEOrelatedSpecLabels) {
				if (this.data.SEOrelatedSpecLabels.hasOwnProperty(x)) {
					if (labels[w].htmlFor === this.data.SEOrelatedSpecLabels[x]) {
						this.data.relatedSpecialtyName.value += labels[w].innerHTML;
						if (x < this.data.SEOrelatedSpecLabels.length - 1) {
							this.data.relatedSpecialtyName.value += ', ';
						}
					}
				}
			}
		}
	}
};
Event.onDOMReady(SEO.init, SEO, true);
//window.onload = function () { SEO.clear(); }
