if (!window['VTLS']) window['VTLS'] = {};
VTLS.API_KEY = 'QAUhZeG9BPgwpU4gfVPmAtu9VYgq1msj';
VTLS.API_DOMAIN = 'tudiabetes.com';
VTLS.VWS_BASE_URL = 'http://api.vitals.com';
VTLS.VWS_VITALS_BASE_URL = 'http://www.vitals.com';
VTLS.WIDGET_BASE_URL = 'http://api.vitals.com/client/vheavy/1.0';
VTLS.VUtil = {
	'$' : function (id) {
		return document.getElementById(id);
	},
	'hasClass' : function (el, c) {
		return el.className.match(new RegExp('(\\s|^)' + c + '(\\s|$)')) !== null ? true : false;
	},
	'addClass' : function (el, c) {
		if (!this.hasClass(el, c)) el.className += ' ' + c;
	},
	'removeClass' : function (el, c) {
		if (!this.hasClass(el, c)) return;
		el.className = el.className.replace(new RegExp('(\\s|^)' + c + '(\\s|$)'), ' ');
	},
	'replaceClass' : function (el, c1, c2) {
		this.removeClass(el, c1);
		this.addClass(el, c2);
	},
	'isElement' : function (el) {
		return !!(el && el.nodeType && el.nodeType == 1);
	}
}
VTLS.VHeavyConfig = {
	'APP_ID' : 2,
	'WIDTH' : 220,
	'MIN_WIDTH' : 150,
	'RETURN' : 'PROV.ID,PROV.LAST_NAME,PROV.DISPLAY_NAME,PROV.PRACTICE_AFFILIATION,PROV.SPECIALTY,PROV.HIGHLIGHT,PRAF.PRACTICE,PRAC.NAME,PRAC.ADDRESS,ADDR.ALL,SPEC.TITLE,HILI.ALL',
	'SPECIALIST_ID' : '',
	'DEFAULT_TAB' : 'C',
	'CHECK_LABEL' : 'check up on your doc',
	'LOCATION' : '',
	'CHECK_NAME_HINT' : 'Type in doctor name (last or full)',
	'CHECK_LOC_HINT' : 'Type in city, state OR zip',
	'FIND_LABEL' : 'find a doc',
	'FIND_SPEC_HINT' : 'Select a specialist type',
	'FIND_LOC_HINT' : 'Type in city, state OR zip',
	'SUBMIT_CHECK_TEXT' : 'go',
	'SUBMIT_FIND_TEXT' : 'find',
	'ADV_SEARCH_TEXT' : 'advanced search',
	'ADV_SEARCH_CHECK_URL' : VTLS.VWS_VITALS_BASE_URL + '/ctl/search_check_up.ctl.php',
	'ADV_SEARCH_FIND_URL' : VTLS.VWS_VITALS_BASE_URL + '/v/?v=find_doc',
	'SEARCH_CHECK_URL' : VTLS.VWS_VITALS_BASE_URL + '/doctor/checkup',
	'SEARCH_FIND_URL' : VTLS.VWS_VITALS_BASE_URL + '/doctor/find',
	'VITALS_LOGO' : true
};

VTLS.VHeavyRes = {
	'specialist' : {
		"101":"Allergist & Immunologist",
		"128":"Anesthesiologist (pain control)",
		"131":"Cardiothoracic Surgeon",
		"135":"Cardiologist (heart)",
		"104":"Colorectal Surgeon (colon & rectum)",
		"105":"Dermatologist (skin)",
		"136":"Endocrinologist (diabetes, hormones)",
		"107":"Family Practitioner",
		"137":"Gastroenterologist (digestive)",
		"108":"General Practitioner",
		"134":"Geriatricist (elderly care)",
		"138":"Infectious Disease (virus, bacteria, parasites)",
		"110":"Internist",
		"112":"Medical Geneticist",
		"139":"Nephrologist (kidney)",
		"140":"Neurologist (brain, nervous system)",
		"113":"Neurological Surgeon",
		"117":"Obstetrician & Gynecologist",
		"141":"Oncologist (cancer)",
		"118":"Ophthalmologist (eyes)",
		"119":"Oral & Maxillofacial Surgeon",
		"120":"Orthopaedic Surgeon",
		"121":"Otolaryngologist (ear, nose, throat)",
		"142":"Pediatric / Adolescent Psychiatrist",
		"143":"Pediatric Surgeon",
		"123":"Pediatrician",
		"124":"Physiatrist (physical, rehabilitation)",
		"125":"Plastic Surgeon",
		"144":"Psychiatrist",
		"145":"Pulmonologist (lungs)",
		"116":"Sports Medicine",
		"130":"Surgeon",
		"133":"Urologist"
	}
};

VTLS.VHeavyControl = function () {
	this.tabs = [];
	this.form = VTLS.VHeavyConfig.DEFAULT_TAB;
	
	this.fields = [];
	this.el = {};
	this.searching = false;
	this.rs = null;
	this.page = 0;
	this.perpage = 5; // 5 by default
	if (!VTLS.VHeavyControl.hasOwnProperty('collection')) VTLS.VHeavyControl.collection = [];
	VTLS.VHeavyControl.collection.push(this);
	this.instance = VTLS.VHeavyControl.collection.length - 1;
	
	this.value = {
		'check' : {
			'name' : '',
			'loc' : ''
		},
		'find' : {
			'sid' : '',
			'loc' : ''
		}
	}; 
};

VTLS.VHeavyControl.prototype.correctPNG = function() {
// correctly handle PNG transparency in Win IE 5.5 & 6.
   var arVersion = navigator.appVersion.split("MSIE")
   if(!arVersion) return;
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='image');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}


VTLS.VHeavyControl.prototype.selectByValue = function(el_select, val, trigger_on_change) {
	for (var i = 0; i < el_select.options.length; i++) {
		if (el_select.options[i].value == val) {		
			el_select.options[i].selected = true;
			if (trigger_on_change && el_select.onchange) el_select.onchange();
			return el_select.options[i];
		}
	}
}

VTLS.VHeavyControl.prototype.setLoc = function(l) {
	if(l == undefined || l == '')
		return;
		
	VTLS.VHeavyConfig.LOCATION = l;
}

VTLS.VHeavyControl.prototype.setNumResults = function(l) {
	if(l == undefined || l == '')
		return;

	this.perpage = parseInt(l);
}
VTLS.VHeavyControl.prototype.setSpecialistID = function(id) {
	if(id == undefined || id == '')
		return;
		
	VTLS.VHeavyConfig.SPECIALIST_ID = id;
}

VTLS.VHeavyControl.prototype.setWidth = function(w) {
	if(w <= 200) // small ver
	{
		VTLS.VHeavyConfig.CHECK_LABEL = 'check up';
		VTLS.VHeavyConfig.CHECK_NAME_HINT = 'doctor\'s name';
		VTLS.VHeavyConfig.CHECK_LOC_HINT = 'city, state OR zip';
		VTLS.VHeavyConfig.FIND_LABEL = 'find a doc';
		VTLS.VHeavyConfig.FIND_SPEC_HINT = 'select a specialty';
		VTLS.VHeavyConfig.FIND_LOC_HINT = 'city, state OR zip';
		VTLS.VHeavyConfig.ADV_SEARCH_TEXT = 'advanced';
		VTLS.VHeavyConfig.WIDTH = Math.max(VTLS.VHeavyConfig.MIN_WIDTH, +w);
	}
	else
	{
		VTLS.VHeavyConfig.WIDTH = Math.max(VTLS.VHeavyConfig.MIN_WIDTH, +w);
	}
}

VTLS.VHeavyControl.prototype.setCheckUpLabel = function(l) {
	if (typeof l != 'string') return;
	VTLS.VHeavyConfig.CHECK_LABEL = l;
}

VTLS.VHeavyControl.prototype.setFindLabel = function(l) {
	if (typeof l != 'string') return;
	VTLS.VHeavyConfig.FIND_LABEL = l;
}

VTLS.VHeavyControl.prototype.setVitalsLogo = function(b) {
	VTLS.VHeavyConfig.VITALS_LOGO = (b ? true : false);
}

VTLS.VHeavyControl.prototype.switchTo = function(me, f, tab) {
	for (var i = 0; i < this.tabs.length; i++) {
		this.tabs[i].div.style.display = 'none';
		VTLS.VUtil.removeClass(this.tabs[i].a, 'vl-selected');
		VTLS.VUtil.replaceClass(this.tabs[i].a, 'vl-font-tab-on', 'vl-font-tab-off');
		VTLS.VUtil.replaceClass(this.tabs[i].a, 'vl-color-tab-bg-on', 'vl-color-tab-bg-off');
	}
	tab.style.display = 'block';
	VTLS.VUtil.addClass(me, 'vl-selected');
	VTLS.VUtil.replaceClass(me, 'vl-font-tab-off', 'vl-font-tab-on');
	VTLS.VUtil.replaceClass(me, 'vl-color-tab-bg-off', 'vl-color-tab-bg-on');
	this.form = f;
	this.btn_submit.value = this.form == 'C' ? VTLS.VHeavyConfig.SUBMIT_CHECK_TEXT : VTLS.VHeavyConfig.SUBMIT_FIND_TEXT;
	this.a_adv.href = this.form == 'C' ? VTLS.VHeavyConfig.SEARCH_CHECK_URL : VTLS.VHeavyConfig.ADV_SEARCH_FIND_URL;
	this.a_adv.style.visibility = this.form == 'C' ? 'visible' : 'hidden';
};


VTLS.VHeavyControl.prototype.setDefaultTab = function(val) {
	this.form = val;
}

VTLS.VHeavyControl.prototype._firstFocus = function(self) {
	return function() {
		if (self.value == self._value) {
			self.value = '';
			self.style.color = '';
		}
	}
}

VTLS.VHeavyControl.prototype._firstBlur = function(self) {
	return function() {
		if (self.value == '') {
			self.value = self._value;
			self.style.color = self._color;
		}
	}
}

VTLS.VHeavyControl.prototype._inputKey = function (e) {
	if (e == null) e = event;
	var nKeyCode = (e.keyCode ? e.keyCode : e.which);
	this.onchange(e);
	if (nKeyCode == 13) {
		this.control.doSubmit();
		return false;
	}
}

VTLS.VHeavyControl.prototype._inputChange = function (e) {
	if (e == null) e = event;
	var nKeyCode = (e.keyCode ? e.keyCode : e.which);
	this.updateval();
	if (nKeyCode == 13) {
		this.updateval();
		this.control.doSubmit();
		return false;
	}
}

VTLS.VHeavyControl.prototype.renderAt = function(el) {
	if (typeof el == 'string') el = VTLS.VUtil.$(el);
	if (!el) return false;	
	
	this.el.parent = el;
	
	/* main div */
	var div_main = document.createElement('div');
	div_main.className = 'vl-main vl-font';
	div_main.style.width = VTLS.VHeavyConfig.WIDTH + 'px';
	
	/* tab bar */
	var ul_nav = document.createElement('ul');
	ul_nav.className = 'vl-tab';
	div_main.appendChild(ul_nav);

	/* check tab */
	var li_check = document.createElement('li');
	var a_check = document.createElement('a');
	li_check.appendChild(a_check);
	a_check.href = '#';
	a_check.control = this;
	a_check.onclick = function () { this.control.switchTo(this, 'C', this.tab); return false; }
	a_check.className = 'vl-color-border';
	a_check.appendChild(document.createTextNode(VTLS.VHeavyConfig.CHECK_LABEL));
	ul_nav.appendChild(li_check);

	/* find tab */
	var li_find = document.createElement('li');
	var a_find = document.createElement('a');
	li_find.appendChild(a_find);
	a_find.href = '#';
	a_find.control = this;
	a_find.onclick = function () { this.control.switchTo(this, 'F', this.tab); return false; }
	a_find.className = 'vl-color-border';
	a_find.appendChild(document.createTextNode(VTLS.VHeavyConfig.FIND_LABEL));
	ul_nav.appendChild(li_find);

	/* form wrapper */
	var div_form = document.createElement('div');
	div_form.className = 'vl-form vl-color-border vl-color-tab-bg-on';
	div_main.appendChild(div_form);

	/* form */
	var form_form = document.createElement('form');
	form_form.control = this;
	form_form.style.margin = '0px';
	form_form.style.padding = '0px';
	form_form.onsubmit = function() { try { this.control.doSubmit(); } catch (e) { alert(e); } return false; }
	//form_form.onsubmit = function() { return false; }
	div_form.appendChild(form_form);
	
	/* check div */
	var div_check = document.createElement('div');
	this.tabs.push({'div':div_check,'a':a_check});
	a_check.tab = div_check;
	div_check.className = 'vl-tab-div';
	div_check.style.display = VTLS.VHeavyConfig.DEFAULT_TAB == 'C' ? 'block' : 'none';
	form_form.appendChild(div_check);
	
	/* check field: name */
	var i_check_name = document.createElement('input');
	i_check_name.type = 'text';
	i_check_name.className = 'vl-font';
	i_check_name.value = i_check_name._value = VTLS.VHeavyConfig.CHECK_NAME_HINT;
	i_check_name.style.color = i_check_name._color = '#888';
	i_check_name.onfocus = this._firstFocus(i_check_name);
	i_check_name.onblur = this._firstBlur(i_check_name);
	i_check_name.style.width = (VTLS.VHeavyConfig.WIDTH - 20) + 'px';
	i_check_name.control = this;
	i_check_name.updateval = function () { this.control.value.check.name = this.value; }
	i_check_name.onchange = this._inputChange;
	i_check_name.onkeypress = this._inputKey;
	var div_i_check_name = document.createElement('div');
	div_i_check_name.className = 'vl-wrap-input';
	div_i_check_name.appendChild(i_check_name);
	div_check.appendChild(div_i_check_name);

	/* check field: location */
	var i_check_loc = document.createElement('input');
	i_check_loc.type = 'text';
	i_check_loc.className = 'vl-font';
	i_check_loc.value = i_check_loc._value = VTLS.VHeavyConfig.CHECK_LOC_HINT;
	i_check_loc.style.color = i_check_loc._color = '#888';
	i_check_loc.onfocus = this._firstFocus(i_check_loc);
	i_check_loc.onblur = this._firstBlur(i_check_loc);
	if(VTLS.VHeavyConfig.LOCATION != '')
	{
		i_check_loc.onfocus();
		i_check_loc.value = VTLS.VHeavyConfig.LOCATION;
		this.value.check.loc = VTLS.VHeavyConfig.LOCATION;
	}
	i_check_loc.style.width = (VTLS.VHeavyConfig.WIDTH - 20) + 'px';
	i_check_loc.control = this;
	i_check_loc.updateval = function () { this.control.value.check.loc = this.value; }
	i_check_loc.onchange = this._inputChange;
	i_check_loc.onkeypress = this._inputKey;
	var div_i_check_loc = document.createElement('div');
	div_i_check_loc.className = 'vl-wrap-input';
	div_i_check_loc.appendChild(i_check_loc);
	div_check.appendChild(div_i_check_loc);

	/* find div */
	var div_find = document.createElement('div');
	this.tabs.push({'div':div_find,'a':a_find});
	a_find.tab = div_find;
	div_find.className = 'vl-tab-div';
	div_find.style.display = VTLS.VHeavyConfig.DEFAULT_TAB != 'C' ? 'block' : 'none';
	form_form.appendChild(div_find);
	
	/* find field: specialty */
	var i_find_name = document.createElement('select');
	var opt = document.createElement('option');
	opt.value = '';
	opt.text = VTLS.VHeavyConfig.FIND_SPEC_HINT;
	document.all ? i_find_name.add(opt) : i_find_name.add(opt, null);
	for (sid in VTLS.VHeavyRes.specialist) if (VTLS.VHeavyRes.specialist.hasOwnProperty(sid)) {
		opt = document.createElement('option');
		opt.value = sid;
		opt.text = VTLS.VHeavyRes.specialist[sid];
		document.all ? i_find_name.add(opt) : i_find_name.add(opt, null);
	}
	i_find_name.className = 'vl-font';
	i_find_name.style.width = (VTLS.VHeavyConfig.WIDTH - 20) + 'px';
	i_find_name.control = this;
	i_find_name.control = this;
	i_find_name.updateval = function () { this.control.value.find.sid = this.value; }
	i_find_name.onchange = this._inputChange;
	if(VTLS.VHeavyConfig.SPECIALIST_ID != '')
		this.value.find.sid = VTLS.VHeavyConfig.SPECIALIST_ID;
	var div_i_find_name = document.createElement('div');
	div_i_find_name.className = 'vl-wrap-input';
	div_i_find_name.appendChild(i_find_name);
	div_find.appendChild(div_i_find_name);
	this.selectByValue(i_find_name, VTLS.VHeavyConfig.SPECIALIST_ID);

	/* find field: location */
	var i_find_loc = document.createElement('input');
	i_find_loc.type = 'text';
	i_find_loc.className = 'vl-font';
	i_find_loc.value = i_find_loc._value = VTLS.VHeavyConfig.FIND_LOC_HINT;
	i_find_loc.style.color = i_find_loc._color = '#888';
	i_find_loc.onfocus = this._firstFocus(i_find_loc);	
	i_find_loc.onblur = this._firstBlur(i_find_loc);	
	if(VTLS.VHeavyConfig.LOCATION != '')
	{
		i_find_loc.onfocus();
		i_find_loc.value = VTLS.VHeavyConfig.LOCATION;
		this.value.find.loc = VTLS.VHeavyConfig.LOCATION;
	}
	i_find_loc.style.width = (VTLS.VHeavyConfig.WIDTH - 20) + 'px';
	i_find_loc.control = this;
	i_find_loc.updateval = function () { this.control.value.find.loc = this.value; }
	i_find_loc.onchange = this._inputChange;
	i_find_loc.onkeypress = this._inputKey;
	var div_i_find_loc = document.createElement('div');
	div_i_find_loc.className = 'vl-wrap-input';
	div_i_find_loc.appendChild(i_find_loc);
	div_find.appendChild(div_i_find_loc);
	
	/* brand div */
	var brand_div = document.createElement('div');
	brand_div.className = 'vl-brand';
	form_form.appendChild(brand_div);
	
	/* result_container */
	var result_container = document.createElement('div');
	result_container.className = 'vl-result-cointainer';
	form_form.appendChild(result_container);
	
	/* submit div */
	var submit_div = document.createElement('div');
	submit_div.className = 'vl-float-r';
	submit_div.style.textAlign = 'right';
	brand_div.appendChild(submit_div);
	
	/* logo div */
	if (VTLS.VHeavyConfig.VITALS_LOGO) {
		var logo_div = document.createElement('div');
		logo_div.className = 'vl-float-l';
		var img_logo = document.createElement('img');
		img_logo.style.width = '78px';
		img_logo.style.height = '38px';
		img_logo.src = VTLS.VWS_BASE_URL + '/client/res.php?a=vheavy&v=1.0&t=img&key=' + VTLS.API_KEY +'&f=logo.png';
		a_logo = document.createElement('a');
		a_logo.href = VTLS.VWS_VITALS_BASE_URL;
		a_logo.title = 'powered by Vitals';
		a_logo.appendChild(img_logo);
		logo_div.appendChild(a_logo);
		var clear_div = document.createElement('div');
		clear_div.style.clear = 'both';
		logo_div.appendChild(clear_div);
		brand_div.appendChild(logo_div);
	}
	
	/* submit button */
	var btn_submit = document.createElement('input');
	btn_submit.type = 'button';
	btn_submit.value = '';
	btn_submit.className = 'vl-font vl-submit';
	btn_submit.control = this;
	btn_submit.onclick = function () { this.control.search(); };
	this.btn_submit = btn_submit;
	this.el.submit = btn_submit;
	submit_div.appendChild(btn_submit);
	var clear_div = document.createElement('div');
	clear_div.style.clear = 'both';

	submit_div.appendChild(clear_div);
	
	/* advanced search */
	var a_adv = document.createElement('a');
	a_adv.href = '#';
	a_adv.appendChild(document.createTextNode(VTLS.VHeavyConfig.ADV_SEARCH_TEXT));
	a_adv.style.lineHeight = '28px';
	a_adv.control = this;
	a_adv.target = '_blank';
	this.a_adv = a_adv;
	//submit_div.appendChild(a_adv);
	
	var clear_div = document.createElement('div');
	clear_div.style.clear = 'both';
	brand_div.appendChild(clear_div);
	
	var result_div = document.createElement('div');
	result_div.className = 'vl-result';
	this.el.result = result_div;
	result_container.appendChild(result_div);
	
	var r = document.createElement('div');
	r.className = 'vs-ad';
	r.innerHTML = '<div id="vs-ad" style="text-align:center; display:none;"><br/><iframe style="width:235px; height:70px; padding:0px; overflow: hidden;" frameborder="0" marginwidth="0" marginheight="0" id="ad_iframe" src="' + VTLS.VWS_VITALS_BASE_URL + '/widget/ad" /></div>';
	form_form.appendChild(r);
	
	el.appendChild(div_main);
	
	this.correctPNG();	
	
	this.form == 'C' ? a_check.onclick() : a_find.onclick();
	
};

VTLS.VHeavyControl.prototype.onResults = function (rs) {
	this.rs = rs;
	if (!rs) return;
	if (rs.hasOwnProperty('status') && rs['status'] == 'error') {
		this.el.result.innerHTML = '<center><span style="color: red; font-weight: bold;">'+rs.error+'</span></center>';
		//alert('An error occurred. The server said:\n\n' + rs.error);
	}
	else if (rs.hasOwnProperty('result_set')) {
		var r = null;
		var s = '';
		var t = 0;
		var l = +rs.result_set.count;
		r = this.renderPagination(this.page, l, this.perpage);
		if (r === false) return;
		
		var divclear = document.createElement('div');
		divclear.style.clear = 'both';
		this.el.result.innerHTML = '';
		this.el.result.appendChild(divclear);
		this.el.result.appendChild(r);
		this.el.result.appendChild(document.createElement('br'));
		var si = this.page * this.perpage;
		var div_wrap = document.createElement('div');
		div_wrap.className = 'vl-wrapper';
		for (var i = si; i < si + this.perpage; i++) {
			if (!rs.result_set[i]) break;
			r = this.renderResult(rs.result_set[i]);
			div_wrap.appendChild(r);
			t = 1 - t;		
		}
		this.el.result.appendChild(div_wrap);
		
		if(this.form == 'C')
			document.getElementById('ad_iframe').src = VTLS.VWS_VITALS_BASE_URL +'/widget/ad?name='+encodeURIComponent(this.value.check.name)+'&location='+encodeURIComponent(this.value.check.loc)+'&key='+encodeURIComponent(VTLS.API_KEY);
		else
			document.getElementById('ad_iframe').src = VTLS.VWS_VITALS_BASE_URL +'/widget/ad?spec_id='+encodeURIComponent(this.value.find.sid)+'&location='+encodeURIComponent(this.value.find.loc)+'&key='+encodeURIComponent(VTLS.API_KEY);
			
		document.getElementById('vs-ad').style.display = 'block';
	}
}

VTLS.VHeavyControl.prototype.renderPagination = function (curpage, count, perpage) {
	var self = this;
	var numpages = Math.ceil(count / perpage);
	var div = document.createElement('div');
	div.className = 'vs-float-pagination-left';

	if (numpages < 1) 	{
		div.style.textAlign = 'center';
		div.innerHTML =  'No results';
		return div;
	}
	if (curpage < 0 || curpage >= numpages) return false;
	
	var a;
	var divprev = document.createElement('div');
	divprev.style.textAlign = 'left';
	if (numpages > 1 && curpage > 0) {
		a = document.createElement('a');
		a.innerHTML = '&lt;&lt; Prev';
		a.href = '#';
		a.onclick = function() {
			self.page--;
			self.onResults(self.rs);
			return false;
		}
	}
	else {
		a = document.createTextNode(' ');
	}
	divprev.appendChild(a);

	var divnext = document.createElement('div');
	divnext.style.textAlign = 'right';
	if (numpages > 1 && curpage < numpages - 1) {
		a = document.createElement('a');
		a.innerHTML = 'Next &gt;&gt;';
		a.href = '#';
		a.onclick = function() {
			self.page++;
			self.onResults(self.rs);
			return false;
		}
	}
	else {
		a = document.createTextNode(' ');
	}
	divnext.appendChild(a);
	
	var divpage = document.createElement('div');
	divpage.style.textAlign = 'center';
	
	var pagepad = 2;
	var pagepadx2 = pagepad * 2;
	var sn = Math.max(curpage + 1 - pagepad, 1);
	var en = Math.min(curpage + 1 + pagepad, numpages);
	if (numpages > pagepad * 2 && en - sn < pagepad * 2) {
		var dn = (pagepad * 2) - (en - sn);
		if (sn == 1) en += dn;
		else sn -= dn;
	}
	
	for (var i = sn; i <= en; i++) {

		var istr = i;	
		if (i == sn && sn != 1) istr = '&hellip; ' + istr;
		else if (i == en && en != numpages) istr = istr + ' &hellip;';
	
		if (curpage + 1 != i) {
			a = document.createElement('a');
			a.innerHTML = istr;
			a.href = '#';
			a.i = i - 1;
			a.onclick = function() {
				self.page = this.i;
				self.onResults(self.rs);
				return false;
			}
		}
		else {
			a = document.createElement('span');
			a.innerHTML = istr;
			a.style.fontWeight = 'bold';
		}
		divpage.appendChild(a);
		divpage.appendChild(document.createTextNode(' '));
	}
	
	divprev.style.width = divnext.style.width = '15%';
	divpage.style.width = '55%';
	
	divprev.className = divnext.className = divpage.className = 'vs-float-left';
	div.appendChild(divprev);
	div.appendChild(divpage);
	div.appendChild(divnext);
	var clear_div = document.createElement('div');
	clear_div.style.clear = 'both';
	div.appendChild(clear_div);
	return div;
}

VTLS.VHeavyControl.prototype.renderResult = function (r) {

	var div = document.createElement('div');
	div.className = 'vs-r';
	if (!r.provider) return div;
	r = r.provider;
	
	var xh = '';
	
	var divb = document.createElement('div');
	divb.className = 'vs-r-caption';
	if (r.display_name) xh += '<b><a href="' + VTLS.VWS_VITALS_BASE_URL + '/doctors/Dr_' + (r.master_name) + '.html?utm_source='+VTLS.API_DOMAIN+'&utm_medium=widget&utm_campaign=widget_partner" target="_blank">' + r.display_name + '</a></b><br/>';
	if (r.specialty_set &&  r.specialty_set.length > 0) {	
		var xh_spec = '';
		for (var i = 0, l = r.specialty_set.length; i < l; i++) {
			if (r.specialty_set[i].title) xh_spec += r.specialty_set[i].title + ', ';
		}
		if (xh_spec.length > 0) {
			xh_spec = xh_spec.substr(0, xh_spec.length - 2);
			xh += '<span class="vs-color-light" style="font-size: 11px;">' + xh_spec + '</span><br/>';
		}
	}
	divb.innerHTML = xh; xh = '';
	
	var divh = document.createElement('div');
	divh.className = 'vs-r-highlight';
	if (r.highlight) {
		hl = r.highlight.split(',');
		if (hl.length > 0) {
			for (var i = 0, l = hl.length; i < l; i++) xh += '&bull; ' + hl[i] + '<br/>';
		}
	}
	divh.innerHTML = xh; xh = '';
	
	var diva = document.createElement('div');
	diva.className = 'vs-r-address';
	if (r.practice_affiliation_set && r.practice_affiliation_set.length > 0) {
		var praf = r.practice_affiliation_set[0];
		if (praf.practice) {
			var prac = praf.practice;
			//if (prac.name && prac.name != r.display_name) xh += '<b>' + prac.name + '</b><br/>';
			if (prac.address) {
				var addr = prac.address;
				if (addr.line1) xh += addr.line1 + '<br/>';
				if (addr.line2) xh += addr.line2 + '<br/>';
				if (addr.city && addr.state) xh += addr.city + ', ' + addr.state + (addr.zip ? ' ' + addr.zip : '') + '<br/>';
			}
		}
	}
	diva.innerHTML = xh;
	
	div.appendChild(divb);
	div.appendChild(diva);
	div.appendChild(divh);
	
	return div;
}


VTLS.VHeavyControl.onResults = function(rs) {
	if (!rs.response) return;
	rs = rs.response;
	var instance = +rs['jsonp_i'];
	if (instance >= VTLS.VHeavyControl.collection.length) return;
	var heavyctl = VTLS.VHeavyControl.collection[instance];
	if (heavyctl.onResults && typeof heavyctl.onResults == 'function') heavyctl.onResults(rs);
	heavyctl.el.submit.disabled = false;
	//heavyctl.el.status.innerHTML = '';
	heavyctl.searching = false;
	heavyctl.el.result.style.display = 'block';
}

VTLS.VHeavyControl.prototype.search = function () {
	if (this.searching) return;	
	this.el.submit.disabled = true;
	//this.el.status.innerHTML = '<a href="http://ad.doubleclick.net/jump/dev.site268.tmus;col=yell;sz=234x60;ord=123456789?" target="_blank" ><img src="http://ad.doubleclick.net/ad/dev.site268.tmus;col=yell;sz=234x60;ord=123456789?" border="0" /></a>';
	//------used to be---^		'<img src="' + VTLS.WIDGET_BASE_URL + '/loading.gif" style="width: ' + VTLS.VSearchConfig.WIDTH + 'px; height: 16px;"/>';
	this.el.result.innerHTML = '';

	this.searching = true;
	var q = '?key=' + VTLS.API_KEY + '&app_id=' + VTLS.VHeavyConfig.APP_ID + '&method=search&format=jsonp&jsonp_cb=VTLS.VHeavyControl.onResults&jsonp_i=' + this.instance + '&ret=' + encodeURIComponent(VTLS.VHeavyConfig.RETURN) + '&xfo=highlight_to_text';
	if (this.form == 'C') {
		if (this.value.check.name != '')
			q += '&name=' + encodeURIComponent(this.value.check.name);
		if (this.value.check.loc != '')
			q += '&location=' + encodeURIComponent(this.value.check.loc);
	}
	else {
		if (this.value.find.sid != '' && this.value.find.loc != '') {
			q += '&specialist_id=' + encodeURIComponent(this.value.find.sid);
			q += '&location=' + encodeURIComponent(this.value.find.loc);
		}
	}
	
	var scr = document.createElement('script');
	scr.setAttribute('type', 'text/javascript');
	scr.src = VTLS.VWS_BASE_URL + '/' + q + '&' + (new Date().getTime().toString());
	if (this.el.script != null) {
		document.getElementsByTagName('head').item(0).replaceChild(scr, this.el.script)
	}
	else {
		document.getElementsByTagName('head').item(0).appendChild(scr);
		//this.el.parent.appendChild(scr);
	}
	this.el.script = scr;
}

VTLS.VHeavyControl.prototype.doSubmit = function() {
	var xhref = '';
	this.search();
	/*
	this.form.control = this;
	if (this.form == 'C') {
		if (this.value.check.name != '')
			xhref = VTLS.VHeavyConfig.SEARCH_CHECK_URL + '/?name=' + encodeURIComponent(this.value.check.name) + '&location=' + encodeURIComponent(this.value.check.loc);
	}
	else {
		if (this.value.find.sid != '' && this.value.find.loc != '') {
			VTLS.VHeavyControl.search();
			//xhref = VTLS.VHeavyConfig.SEARCH_FIND_URL + '/?sid=' + encodeURIComponent(this.value.find.sid) + '&loc=' + encodeURIComponent(this.value.find.loc);
		}
	}
	if (xhref == '') return;
	//xhref += '&key=' + encodeURIComponent(VTLS.API_KEY);
	//window.open(xhref);*/
};
