/**
 * spa location selectBox update function
 *
 * -------- requires a selectBox with the id 'spaLocationSearch' --------
**/
lm.spaLocationUpdate = {
	/**
	 * adds events to spa radio buttons in order to perform selectBox update
	**/
	init: function () {
		var regionOpts = lm.getElementsBy({p: 'spaSearchByLocation', tagName: 'input', attributes: {'type': 'radio'}});
		lm.e('spaLocationSearch').style.display = 'none';
		lm.e('engine_map_submit').style.display = 'none';
		// attach click event to radios
		//
		lm.forEach(regionOpts, function (el, index) {
				el.checked = false;
				lm.addEvent({e: el, 
							eT: 'click', 
							fn: function () { 
								lm.spaLocationUpdate.createOptions(el.value); 
							} 
				});	
		});
	},
	/**
	 * performs an AJAX call to populate the spa location search dropdown
	 *
	 * @param {String} id - the region ID to perform an AJAX call with.
	**/
	createOptions: function (id) {
		var selectBox, parentNode, loader, previousLoader, labelText, h4;
		// get select box and previous loader if there is one, plus the parentNode
		//
		selectBox = lm.e('spaLocationSearch');
		lm.e('engine_map_submit').style.display = '';
		if (!selectBox) {
			selectBox = lm.ce('select');
			selectBox.id = 'spaLocationSearch';
			selectBox.size = 1;
			selectBox.name = 'CATID';
			lm.styles(selectBox, {marginBottom: '5px', marginTop: '5px', marginRight: '8px', marginLeft: '8px', display: 'none'});
			selectBox.style.width = '94px';
			lm.getElementsBy({p: 'spaSearchByLocation', className: 'engine_map_right'})[0].getElementsByTagName('label')[0].appendChild(selectBox);
		}
		previousLoader = lm.e('spaImgLoader');
		parentNode = lm.getElementsBy({p: 'spaSearchByLocation', className: 'engine_map_right'})[0];
		lm.forEach(lm.getElementsBy({p: 'spaSearchByLocation', className: 'hiddenPost'}), function (field, i) {
			field.parentNode.removeChild(field);	
		});
		// create image loader and append it
		//
		loader = (previousLoader) ? previousLoader : lm.ce('img');
		loader.src = 'http://cdn.lastminute.com/site/activityIndicator.gif?skin=lastminute';
		loader.alt = 'loading...';
		loader.id = 'spaImgLoader';
		loader.style.paddingTop = '5px';
		lm.styles(selectBox, {display: 'none'});
		parentNode.insertBefore(loader, lm.getElementsBy({p: 'spaSearchByLocation', className: 'engine_map_right_bottom'})[0]);
		labelText = lm.e('displayName' + id).innerHTML;		
		h4 = lm.getElementsBy({p: 'spaSearchByLocation', tagName: 'h4'})[0];
		h4.innerHTML = labelText;
		lm.styles(h4, {fontSize: '12px', fontWeight: 'bold'});
		// perform AJAX call to populate dropdown
		//
		lm.httpRequest({url: frontierURL + '/entertainment/healthandbeauty/spa_search_ajax.html?region=' + id + '&' + 'skin=' + frontierSkin, 
						response: 'text', 
						onComplete: function (response) {
							var hiddenInput, selOption;
							if (selectBox) {
								// strip extra HTML from response and evaluate it
								//
								response = response.replace(/<.*>/g, '');
								response = eval('('+ response +')');
								selectBox.innerHTML = '';
								selectBox.disabled = false;
								lm.forEach(response.regions, function (opt, index) {
									// create option tag and clean unescaped HTML from result
									//
									var el = document.createElement('option');
									el.text = opt.region.name.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
									el.value = opt.region.value.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
									selectBox.options.add(el);
								});
								// if selectBox only has one value then create hidden fields
								//
								if (selectBox.options.length === 1) {
									selOption = selectBox.options[selectBox.selectedIndex];
									hiddenInput = lm.ce('input');
									hiddenInput.type = 'hidden';
									hiddenInput.className = 'hiddenPost';
									hiddenInput.name = selectBox.name;
									hiddenInput.value = selOption.value;
									h4.innerHTML = selOption.text;
									parentNode.appendChild(hiddenInput);
									selectBox.parentNode.removeChild(selectBox);
									selectBox.disabled = true;
								} else {
									selectBox.style.display = '';
								}
								parentNode.removeChild(loader);
							}
						}
		});
	}
};
lm.spaLocationUpdate.init();
