var pricedropdowns = new Array();
function pricedropdown(element, action) {
	var optiontext = new Array(
		" (< $60)",
		" ($61 - $150)",
		" ($151 - $350)",
		" ($351 - $1000)",
		" ($1001+)"
	);
	var theoptions = element.getElementsByTagName('option');

	if (navigator.userAgent.indexOf('MSIE') != -1) { action = ''; }

	switch (action) {
		case "focus":
			pricedropdowns[element.getAttribute('id')] = 1;
			for (var i=1; i<theoptions.length; i++) {
				if (theoptions[i].firstChild.nodeValue.indexOf(optiontext[i-1]) == -1) {
					theoptions[i].firstChild.nodeValue += optiontext[i-1];
				}
			}
		break;
		case "blur":
		case "change":
			pricedropdowns[element.getAttribute('id')] = 0;
			for (var i=1; i<theoptions.length; i++) {
				theoptions[i].firstChild.nodeValue = theoptions[i].firstChild.nodeValue.substring(0,i);
			}
		break;
		case "click":
			pricedropdowns[element.getAttribute('id')]++;
			if (pricedropdowns[element.getAttribute('id')] % 2 == 1) { // remove stuff only
				for (var i=1; i<theoptions.length; i++) {
					theoptions[i].firstChild.nodeValue = theoptions[i].firstChild.nodeValue.substring(0,i);
				}
			} else { // add stuff only
				for (var i=1; i<theoptions.length; i++) {
					if (theoptions[i].firstChild.nodeValue.indexOf(optiontext[i-1]) == -1) {
						theoptions[i].firstChild.nodeValue += optiontext[i-1];
					}
				}
			}
		break;
		default: break;
	}
}

function getQuerystring() {
	var buildstring = window.location.search.replace(/^\?/,"");
	if (buildstring.length > 0 && buildstring.charAt(buildstring.length-1) != "&") {
		buildstring = buildstring + '&';
		var regex = "/wineid=.*?&/";
		buildstring = buildstring.replace(eval(regex), "");
	}
	return buildstring;
}

function resetSearch() {
	sm.resetHash();
	var myinputs = document.getElementById("selectform").getElementsByTagName("input");
	var area51selects = document.getElementById("area51").getElementsByTagName("select");

	for (i=0; i<myinputs.length; i++) { myinputs[i].value = ''; }
	for (i=0; i<area51selects.length; i++) { area51selects[i].selectedIndex = 0; }
}

function checkPrice() {
	var optionval = sm.getHashValue('pricelow');
	var price = document.getElementById('pricelow');

	for (i=0; i<price.options.length; i++) {
		if (price.options[i].value == optionval) price.options[i].selected = true;
	}

	if (price.selectedIndex == -1) { price.options[0].selected = true; }

	optionval = sm.getHashValue('pricehigh');
	price = document.getElementById('pricehigh');

	for (i=0; i<price.options.length; i++) {
		if (price.options[i].value == optionval) price.options[i].selected = true;
	}

	if (price.selectedIndex == -1) { price.options[0].selected = true; }
}

function getResultsPage(elem, page) {
	// Create search loading block
	var preStart = sm;

	if (elem && elem.onclick && !page) {
		sm.setHashMultiple(elem.getAttribute("href"));
	} else {
		sm.setHashValue("page", page);
	}

	if (preStart != sm.getBuildstring()) {
		document.getElementById('loadingbottom').style.visibility = "visible";
		sm.commit();
	}
}

function sendSelect(element) {
	// Create search loading block.
	document.getElementById('loadingtop').getElementsByTagName('div')[0].style.visibility = "visible";
	document.getElementById('loadingbottom').style.visibility = "visible";

	sm.unsetHashValue('page');

	if (element.value == "NOVAL") {
		sm.unsetHashValue(element.id, 1);
	} else {
		sm.setHashValue(element.id, element.value, 1);
	}

	sm.commit();
	//getAJAXresponse(getHash());
}

function sendInitialPosts(initialAr) {
	for (key in initialAr) { 
		sm.setHashValue(key, initialAr[key]);
	}
	sm.commit();
}

function sendInputs() {
	var myinputs = document.getElementById("selectform").getElementsByTagName("input");
	sm.unsetHashValue('page');
	sm.unsetHashValue('section');

	var headvisible = false;

	for (i=0; i<myinputs.length; i++) {
		if (myinputs[i].value.length > 0) {
			sm.setHashValue(myinputs[i].id, myinputs[i].value);
			addSearchcrumb(myinputs[i]);
			headvisible = true;
		} else {
			sm.unsetHashValue(myinputs[i].id, 1);
			removeSearchcrumb(myinputs[i]);
		}
	}

	document.getElementById('head_breadcrumbs').style.display = (headvisible ? "block" : "none");

	if (sm.getHashValue('text_producer') != decodeURIComponent(sm.getBuildstringValue('text_producer')) || sm.getHashValue('text_varietal') != decodeURIComponent(sm.getBuildstringValue('text_varietal')) || sm.getHashValue('text_vineyard') != decodeURIComponent(sm.getBuildstringValue('text_vineyard')) || sm.getHashValue('text_proprietary') != decodeURIComponent(sm.getBuildstringValue('text_proprietary'))) {
		// Create search loading block.
		document.getElementById('loadingtop').getElementsByTagName('div')[0].style.visibility = "visible";
		document.getElementById('loadingbottom').style.visibility = "visible";
		sm.commit();
	}
}

function addSearchcrumb(input) {
	var crumbid = 'searchcrumbs_' + input.id.split('_')[1];
	var crumbvalue = input.value;
	
	$(crumbid).style.display = 'inline';
	$(crumbid).nextSibling.style.display = 'inline';

	var dl = $(crumbid).nextSibling;
	dl.innerHTML = crumbvalue;
}

function removeSearchcrumb(input) {
	var crumbid = 'searchcrumbs_' + input.id.split('_')[1];
	var crumbvalue = input.value;
	
	$(crumbid).style.display = 'none';
	$(crumbid).nextSibling.style.display = 'none';
}

function getAJAXresponse(poststring) {
//	if (poststring.length == undefined && firstsearch) { poststring = escape(poststring).replace(/%3D/ig, '=').replace(/%26/ig, '&').replace(/%25/ig, '%'); }
	firstsearch = false;

	var url = "/searchqueries.php";
	var pars = poststring + 'rand=' + Math.floor(Math.random() * 999);
	var myAjax = new Ajax.Request( 
		url,
		{
			method: 'post',
			parameters: pars,
			onComplete: refreshPage,
			onFailure: function() { alert("There was trouble getting your information from the server."); }
		}
   );
}

function refreshPage(response) {
	var ajaxResponse = Try.these(
		function() { return new DOMParser().parseFromString(response.responseText, 'text/xml'); },
		function() { var xmldom = new ActiveXObject('Microsoft.XMLDOM'); xmldom.loadXML(response.responseText); return xmldom; }
	);

	// Find out what content we got back (selects? item count? results?)
	for (i=0; i<ajaxResponse.getElementsByTagName("response").length; i++) {
		switch (ajaxResponse.getElementsByTagName("response")[i].getAttribute('id')) {
			case "breadcrumbs":
				var breadcrumblabels = document.getElementById('searchcrumbs').getElementsByTagName('dt');
				var breadcrumbs = document.getElementById('searchcrumbs').getElementsByTagName('dd');
				var mycrumbs = ajaxResponse.getElementsByTagName("response")[i].getElementsByTagName("dd");
				var myinputs = document.getElementById("selectform").getElementsByTagName("input");
				var headvisible = false;

				for (j=0; j<mycrumbs.length; j++) {
					if (mycrumbs[j].firstChild) {
						breadcrumblabels[j].style.display = "inline";
						breadcrumbs[j].style.display = "inline";
						breadcrumbs[j].innerHTML = mycrumbs[j].firstChild.nodeValue;
						myinputs[j].value = mycrumbs[j].firstChild.nodeValue;
						headvisible = true;
					} else {
						breadcrumblabels[j].style.display = "none";
						breadcrumbs[j].style.display = "none";
						breadcrumbs[j].innerHTML = "";
						myinputs[j].value = "";
					}
				}

				document.getElementById('head_breadcrumbs').style.display = (headvisible ? "block" : "none");

			break;
			case "select":
				// Allows for infinite numbers of selects being returned
				var myselects = ajaxResponse.getElementsByTagName("response")[i].getElementsByTagName("select");
				for (j=0; j<myselects.length; j++) {
					if (document.getElementById(myselects[j].getAttribute('id'))) {
						var pageElem = document.getElementById(myselects[j].getAttribute('id'))
						pageElem.options.length = 1;
						for (k=0; k<myselects[j].getElementsByTagName('option').length; k++) {
							pageElem.options[k] = new Option(myselects[j].getElementsByTagName('option')[k].firstChild.nodeValue, myselects[j].getElementsByTagName('option')[k].getAttribute('value'));
							if (myselects[j].getElementsByTagName('option')[k].getAttribute('selected')) { pageElem.options[k].selected = true; }
						}
						if (pageElem.className == "disabled") pageElem.className = "";
					}
				}
			break;
			case "count":
				var num = ajaxResponse.getElementsByTagName("response")[i].getElementsByTagName("count")[0].getAttribute('value');
				
				var k = 2;
				var tempelem;
				while (tempelem = document.getElementById('count' + k)) {
					tempelem.firstChild.nodeValue = num;
					if (k==2 && num == 1) { document.getElementById("recordCheck").style.display = 'none'; } else { document.getElementById("recordCheck").style.display = 'inline'; }
					k++;
				}
				if (num > 0) {
					// Unhide the bottom bar, in case it is hidden.
					document.getElementById('column1').style.display = "";
					document.getElementById('column2').style.marginLeft = "";
					document.getElementById('column2').style.width = "";

					if ($('partialmatch')) {
						$('count2').parentNode.removeChild($('partialmatch'));
					}

					$('searchmessage').style.display = "none";
					if ($('searchmessage').firstChild) {
						$('searchmessage').firstChild.nodeValue = "";
					}

					var dl = $('partialmatches');
						dl.style.margin = "0";
						dl.style.display = "none";
						dl.style.clear = "";
					
					dl.innerHTML = '';

					document.getElementById('results').getElementsByTagName('table')[0].style.display = "block";
					document.getElementById('results').getElementsByTagName('ul')[0].style.display = "block";
					document.getElementById('lower').style.display = "block";
				}
			break;
			case "partials":
				if ($('count2').firstChild.nodeValue == 0) {
					// Hide the bottom bar, count was zero.
					document.getElementById('column1').style.display = "none";
					document.getElementById('column2').style.marginLeft = "0";
					document.getElementById('column2').style.width = "100%";

					var buildstring = '';
					var partials = new Array();
					var labels = { text_producer: "Wine Producer:", text_varietal: "Varietal:", text_vineyard: "Vineyard Name:", text_proprietary: "Proprietary Name:"};

					var partialarray = ajaxResponse.getElementsByTagName("response")[i].getElementsByTagName("count");
					for (var v=0; v<partialarray.length; v++) {
						partials.push({ label: labels[partialarray[v].getAttribute('field')], qsval: partialarray[v].getAttribute('field'), value: partialarray[v].firstChild.nodeValue, results: partialarray[v].getAttribute('value')})
					}

					if (partials.length > 0) {
						buildstring = ' and these partial matches:';

						if (!$('partialmatch')) {
							var partialmatch = document.createElement('span');
								partialmatch.setAttribute('id', 'partialmatch');
								partialmatch.appendChild(document.createTextNode(buildstring));
	
							$('count2').parentNode.appendChild(partialmatch);
						}

						$('searchmessage').style.display = "none";
						if ($('searchmessage').firstChild) {
							$('searchmessage').firstChild.nodeValue = "";
						}

						var dl = $('partialmatches');
							dl.style.margin = "";
							dl.style.display = "";
							dl.style.clear = "both";
						
						dl.innerHTML = '';

						for (var v=0; v<partials.length; v++) {
							var temp1 = document.createElement('dt');
								temp1.appendChild(document.createTextNode(partials[v].label));

							var partiallink = document.createElement('a');
								partiallink.setAttribute("href", "/search/?"+partials[v].qsval+"="+partials[v].value);
								partiallink.appendChild(document.createTextNode(partials[v].value));
							var temp2 = document.createElement('dd');
								temp2.appendChild(partiallink);
								temp2.appendChild(document.createTextNode(' ' + partials[v].results + ' result' + (partials[v].results != 1 ? "s" : "")));

							dl.appendChild(temp1);
							dl.appendChild(temp2);
						}
						
					} else {
						if ($('partialmatch')) {
							$('count2').parentNode.removeChild($('partialmatch'));
						}

						var dl = $('partialmatches');
							dl.style.margin = "0";
							dl.style.display = "none";
							dl.style.clear = "";
						
						dl.innerHTML = '';

						var imgs = $('searchcrumbs').getElementsByTagName('img');
						var dds = $('searchcrumbs').getElementsByTagName('dd');

						for (var v=0; v < dds.length; v++) {
							if (dds[v].firstChild && dds[v].firstChild.nodeValue != "") {
								buildstring += imgs[v].getAttribute('alt') + ' "' + dds[v].firstChild.nodeValue + '" or ';
							}
						}
						
						buildstring = buildstring.substring(0, buildstring.length-4);
						buildstring = "We have no records in our database for " + buildstring + ".";

						$('searchmessage').style.display = "block";
						if ($('searchmessage').firstChild) {
							$('searchmessage').firstChild.nodeValue = buildstring;
						} else {
							$('searchmessage').appendChild(document.createTextNode(buildstring));
						}
					}

					document.getElementById('results').getElementsByTagName('table')[0].style.display = "none";
					document.getElementById('results').getElementsByTagName('ul')[0].style.display = "none";
					document.getElementById('lower').style.display = "none";
				}
			break;
			case "results":
				var resultstable = document.getElementById('results').getElementsByTagName('table')[0];

				var trelems = resultstable.getElementsByTagName('tr');
				while (trelems.length > 0) { resultstable.deleteRow(0); }

				var resultsAr = ajaxResponse.getElementsByTagName("response")[i].getElementsByTagName("result");
				var querystring = getQuerystring();

				for (j=0; j<resultsAr.length; j++) {
					var tdAvail = document.createElement('td');
						tdAvail.className = 'availability';

					if (resultsAr[j].getAttribute('cases') > 0 && resultsAr[j].getAttribute('cases') <= 5000) {
						var imgAvail = document.createElement('img');
							if (resultsAr[j].getAttribute('cases') <= 2000) {
								imgAvail.setAttribute('src', '/_images/search/available_el.gif');
								imgAvail.setAttribute('alt', 'EL');
								imgAvail.setAttribute('title', 'Extremely Limited');
							} else {
								imgAvail.setAttribute('src', '/_images/search/available_l.gif');
								imgAvail.setAttribute('alt', 'L');
								imgAvail.setAttribute('title', 'Limited');
							}
					}

					var tdWineName = document.createElement('td');
						tdWineName.className = "winename";
					
					var aWineName = document.createElement('a');
						aWineName.setAttribute('href', '/search/'+ resultsAr[j].getAttribute('id').substring(6) +'/');
						if (aWineName.attachEvent) {
							aWineName.onclick = function() { return displaywine(this); }
						} else {
							aWineName.setAttribute("onclick", "return displaywine(this);");
						}

					var tdPrice = document.createElement('td');
						tdPrice.className = 'price';

					var spanDollars = document.createElement('span');
						spanDollars.className = "dollar" + resultsAr[j].getAttribute('signs');

						var dollars = '';
						while (dollars.length < resultsAr[j].getAttribute('signs')) { dollars += '$'; }

						spanDollars.appendChild(document.createTextNode(dollars));

					var tdReserve = document.createElement('td');
						tdReserve.className = "reserve";

					var aReserve = document.createElement('a');
						aReserve.className = "reservewine";
						aReserve.setAttribute('href', '/search/'+ resultsAr[j].getAttribute('id').substring(6) +'/');
						if (aReserve.attachEvent) {
							aReserve.onclick = function() { return displaywine(this); }
						} else {
							aReserve.setAttribute("onclick", "return displaywine(this);");
						}
					
					var tr = resultstable.insertRow(-1);
						tr.setAttribute('id', resultsAr[j].getAttribute('id'));
						if (j % 2 == 0) { tr.className = "alt" }

					aWineName.appendChild(document.createTextNode(resultsAr[j].firstChild.nodeValue));
					aReserve.appendChild(document.createTextNode("Reserve Wine"));
					tdWineName.appendChild(aWineName);
					if (imgAvail) { tdAvail.appendChild(imgAvail); }
					tdReserve.appendChild(aReserve);

					tdPrice.appendChild(spanDollars);
					tr.appendChild(tdAvail);
					tr.appendChild(tdWineName);
					tr.appendChild(tdPrice);
					tr.appendChild(tdReserve);

					tr = undefined;
					tdAvail = undefined;
					imgAvail = undefined;
					tdWineName = undefined;
					aWineName = undefined;
					tdPrice = undefined;
					spanDollars = undefined;
					dollars = undefined;
					tdReserve = undefined;
					aReserve = undefined;
				}

				var paginationUls = document.getElementById('results').getElementsByTagName('ul');
				var totalpages = ajaxResponse.getElementsByTagName("response")[i].getAttribute("totalpages");
				var currentpage = ajaxResponse.getElementsByTagName("response")[i].getAttribute("currentpage");

				var liAR = new Array();

				if (currentpage != 1) { // first element
					var li = document.createElement('li');
						li.className = "direction";
					var a = document.createElement('a');
						a.setAttribute("href","#section=results&" + sm.hashBuilder("page", (Number(currentpage)-1)));
						if (a.attachEvent) {
							a.onclick = function() { getResultsPage(this); return false; }
						} else {
							a.setAttribute("onclick", "getResultsPage(null, "+ (Number(currentpage)-1) +"); return false;");
						}
					var img = document.createElement('img');
						img.setAttribute("src", "/_images/search/pagination_left.gif");
						img.setAttribute("width", "10");
						img.setAttribute("height", "7");
						img.setAttribute("alt", "&lt;&lt;");

					a.appendChild(img);
					li.appendChild(a);
					liAR.push(li);
				}

				for (j=Math.max(0, (currentpage-5)); j < Math.min(totalpages, Number(currentpage)+4); j++) {
					if (totalpages != 1) {
						var li = document.createElement('li');
							if (j+1 == currentpage) { li.className = "focused"; }
						var a = document.createElement('a');
							a.setAttribute("href", "#section=results&" + sm.hashBuilder("page", (j+1)));
							if (a.attachEvent) {
								a.onclick = function() { getResultsPage(this); return false; }
							} else {
								a.setAttribute("onclick", "getResultsPage(null, "+ (j+1) +"); return false;");
							}
						a.appendChild(document.createTextNode(j+1));
						li.appendChild(a);
						liAR.push(li);
					}
				}

				if (currentpage != totalpages) { // last element
					var li = document.createElement('li');
						li.className = "direction";
					var a = document.createElement('a');
						a.setAttribute("href","#section=results&" + sm.hashBuilder("page", (Number(currentpage)+1)));
						if (a.attachEvent) {
							a.onclick = function() { getResultsPage(this); return false; }
						} else {
							a.setAttribute("onclick", "getResultsPage(null,"+ (Number(currentpage)+1) +"); return false;");
						}
					var img = document.createElement('img');
						img.setAttribute("src", "/_images/search/pagination_right.gif");
						img.setAttribute("width", "10");
						img.setAttribute("height", "7");
						img.setAttribute("alt", "&gt;&gt;");

					a.appendChild(img);
					li.appendChild(a);
					liAR.push(li);
				}

				for (j=0; j<paginationUls.length; j++) {
					// Remove existing pagination elements
					while (paginationUls[j].childNodes.length > 0) {
						paginationUls[j].removeChild(paginationUls[j].childNodes[0]);
					}

					// Add new pagination elements
					for (var k=0; k<liAR.length; k++) {
						if (j > 0) {
							paginationUls[j].appendChild(document.createTextNode(" "));
							paginationUls[j].appendChild(liAR[k].cloneNode(true));
						} else {
							paginationUls[j].appendChild(document.createTextNode(" "));
							paginationUls[j].appendChild(liAR[k]);
						}

					}
				}
			break;
		}
	}
	// Remove the search loading block
	document.getElementById('loadingtop').getElementsByTagName('div')[0].style.visibility = "hidden";
	document.getElementById('loadingbottom').style.visibility = "hidden";
}

function resultsToggle(callback) {
	if (document.body.className == "open") {
		sm.setHashValue("section", "topwrapper");
		sm.commit();
		new Effect.BlindUp('results', {
			duration: .5,
			afterFinish: function() {
				var area51 = document.getElementById('area51');
				var area52 = document.getElementById('area52');

				var tempelem;
				while(tempelem = area52.firstChild) {
					area52.removeChild(tempelem);
					area51.appendChild(tempelem);
				}


				new Effect.BlindDown('topwrapper', {
					duration: .5,
					afterFinish: function() {
						// Fade to closed state
						new Effect.Opacity('contentfooter', {
							duration: .3,
							from: 1.0,
							to: 0.0,
							afterFinish: function() {
								var table = document.getElementById('contentfooter');
								var contentfooter = table.parentNode;
								contentfooter.removeChild(table);
								contentfooter.appendChild(table);
								if (callback) { resetSearch(); }
								document.body.className = "";
								document.getElementById('searchcrumbs').parentNode.style.display = "none";
								new Effect.Opacity('contentfooter', {
									duration: .3,
									from: 0.0,
									to: 1.0
								} );
							}
						} );

					}
				} );

			}
		} );
	} else if (document.body.className == "wineid" || document.body.className.indexOf("detailopen") != -1) {
		sm.setHashValue("section", "topwrapper");
		sm.commit();
		new Effect.BlindUp('winedetail', {
			duration: .5,
			afterFinish: function() {
				var area51 = document.getElementById('area51');
				var area52 = document.getElementById('area52');

				if (sm.getHashValue('section') != 'results') {
					sm.setHashValue("section", "topwrapper");
					sm.commit();
					var tempelem;
					while(tempelem = area52.firstChild) {
						area52.removeChild(tempelem);
						area51.appendChild(tempelem);
					}

					
					new Effect.BlindDown('topwrapper', {
						duration: .5,
						afterFinish: function() {
							// Fade to closed state
							new Effect.Opacity('contentfooter', {
								duration: .3,
								from: 1.0,
								to: 0.0,
								afterFinish: function() {
									var table = document.getElementById('contentfooter');
									var contentfooter = table.parentNode;
									contentfooter.removeChild(table);
									contentfooter.appendChild(table);
									if (callback) { resetSearch(); }
									document.body.className = "";
									document.getElementById('searchcrumbs').parentNode.style.display = "none";
									new Effect.Opacity('contentfooter', {
										duration: .3,
										from: 0.0,
										to: 1.0
									} );
								}
							} );

						}
					} );
				} else {
					sm.setHashValue("section", "results");
					sm.commit();
					var tempelem;
					while(tempelem = area51.firstChild) {
						area51.removeChild(tempelem);
						area52.appendChild(tempelem);
					}

					new Effect.BlindDown('results', {
						duration: .5,
						afterFinish: function() {
							// Fade to open state
							new Effect.Opacity('contentfooter', {
								duration: .3,
								from: 1.0,
								to: 0.0,
								afterFinish: function() {
									document.body.className = "open";
									document.getElementById('searchcrumbs').parentNode.style.display = "table-cell";
									new Effect.Opacity('contentfooter', {
										duration: .3,
										from: 0.0,
										to: 1.0
									} );
								}
							} );
						}
					} );
				}
			}
		} );
	} else {
		sm.setHashValue("section", "results");
		sm.commit();
		new Effect.BlindUp('topwrapper', {
			duration: .5,
			afterFinish: function() {
				var area51 = document.getElementById('area51');
				var area52 = document.getElementById('area52');

				var tempelem;
				while(tempelem = area51.firstChild) {
					area51.removeChild(tempelem);
					area52.appendChild(tempelem);
				}

				new Effect.BlindDown('results', {
					duration: .5,
					afterFinish: function() {
						// Fade to open state
						new Effect.Opacity('contentfooter', {
							duration: .3,
							from: 1.0,
							to: 0.0,
							afterFinish: function() {
								document.body.className = "open";
								document.getElementById('searchcrumbs').parentNode.style.display = "block";
								new Effect.Opacity('contentfooter', {
									duration: .3,
									from: 0.0,
									to: 1.0
								} );
							}
						} );
					}
				} );
			}
		} );
	}
}

function displaywine(element, value) {
	//todo
	return true;
	var id = 0;
	if (value || element.getAttribute('href').indexOf("wineid") != -1) {
		if (value) { id = value; } else {
			var regex = "/(.*)(wineid=)(.*?)/";
			id = element.getAttribute('href').replace(eval(regex), "$3");
		}
		setHashValue("wine",id,true);

		var section = 'results';
		if ($('topwrapper').style.display == "block") {
			section = 'topwrapper';
		}

		setHashValue("section", "winedetail",1);

		new Effect.BlindUp(section, {
			duration: .5,
			afterFinish: function() {
				new Effect.BlindDown('winedetail', {
					duration: .5,
					afterFinish: function() {
						document.body.className = "open detailopen";
					}
				} );
			}
		} );
	}
}

function collapsewine() {
	//unsetHashValue("wine", true);

	sm.setHashValue("section", "results");
	sm.commit();
	new Effect.BlindUp('winedetail', {
		duration: .5,
		afterFinish: function() {
			new Effect.BlindDown('results', {
				duration: .5,
				afterFinish: function() {
					document.body.className = "open";
				}
			} );
		}
	} );
}

function chooserestaurant(element) {
	element.style.backgroundPosition = "left bottom";
	new Effect.Fade(element, {duration: .2, afterFinish: function() { element.style.backgroundPosition = ""; element.parentNode.style.verticalAlign = "top"; }, queue: 'end'} );

	var therow = element.parentNode.parentNode;
	var rows = therow.parentNode.getElementsByTagName('tr');


	for (i=0; i<rows.length; i++) {
		if (rows[i] != therow && rows[i].className != "submit") {
			new Effect.Fade(rows[i], { duration: .3 } );
		} else if (rows[i] == therow) {
			// it is presently on the element, skip the following row too.
			i++;
		}
	}

	var compstartcolor = (therow.className != "alt" ? "#F3ECE3" : '#E4DFD9');
	new Effect.Highlight(therow, { startcolor: compstartcolor, endcolor:'#E4DFD9', restorecolor: '#E4DFD9', duration: .3 } );

	new Effect.Appear(rows[therow.rowIndex+1], { duration: .5, queue: 'end'} );
	new Effect.Appear(element.nextSibling, {duration: .2, queue: 'end'} );

}

function unchooserestaurant(element) {
	element.style.backgroundPosition = "left bottom";
	new Effect.Fade(element, {duration: .2, afterFinish: function() { element.style.backgroundPosition = ""; element.parentNode.style.verticalAlign = "middle"; }, queue: 'end'} );

	var therow = element.parentNode.parentNode;
	var rows = therow.parentNode.getElementsByTagName('tr');

	var compendcolor = (therow.className != "alt" ? "#F3ECE3" : '#E4DFD9');
	new Effect.Highlight(therow, { startcolor: '#E4DFD9', endcolor: compendcolor, restorecolor: compendcolor, duration: .5 } );

	for (i=0; i<rows.length; i++) {
		if (rows[i] == therow) { new Effect.Fade(rows[i+1], { queue: 'start', duration: .5, afterFinish: function() {
			for (i=0; i<rows.length; i++) {
				if (rows[i] != therow && rows[i].className != "submit") {
					new Effect.Appear(rows[i], { duration: .3 } );
				}
			}
		} } ); }
	}

	new Effect.Appear(element.previousSibling, {duration: .2, queue: 'end'} );

}

function toggledetails(element) {
	var extended = $('winedescription').nextSibling;

	if (element.className.indexOf("open") != -1) {
		Effect.toggle(extended, 'blind', { duration: .5, afterFinish: function () { element.className = "viewhide"; } } );
	} else {
		Effect.toggle(extended, 'blind', { duration: .5, afterFinish: function () { element.className = "viewhide open"; } } );
	}
}

function getWine() {

}

function getQty(element) {
	var qty = $("qty_" + element.getAttribute('id').split("_")[1]);
	var therow = element.parentNode.parentNode.parentNode.parentNode;
	var rootindex = Math.max(element.options.selectedIndex -1, 0);
	if (bottles[(therow.rowIndex-1)/2][rootindex][0] == element.value) {

		qty.style.width = qty.getDimensions().width;

		qty.options.length = 1;
		var min = 1;
		var max = Math.min(bottles[(therow.rowIndex-1)/2][rootindex][1], 5);
		for (i=min; i<=max; i++) {
			var temp = document.createElement('option');
			temp.setAttribute('value', i);
			temp.appendChild(document.createTextNode(i));
			qty.appendChild(temp);
		}
		try {
			qty.options[1].selected = true;
		} catch (e) { /* Appears to work even with the error. Just prevent user from seeing it. */ }
	} else {
		qty.options[0].selected = true;
		qty.options.length = 1;
	}
}

function makereservation(restid) {
	if ($('qty_'+restid).value == 0 || $('bottlesize_'+restid).value == 0) {
		alert("You must select a Bottle Size and Quantity to make a reservation.");
	} else {
		$('frm_restselect_qty').value = $('qty_'+restid).value;
		$('frm_restselect_size').value = $('bottlesize_'+restid).value;
		$('frm_restselect_restaurantID').value = restid;
		var wineid = sm.getHashValue('wine');
		if (wineid) { $('frm_restselect_wineID').value = wineid; }

		$('restselect').submit();
	}
};

function getCityRests(element) {
	// create options based upon a for
	var restaurant = $('restaurant_Id');
	if (element.value != "NOVAL") {
		var theoptions = restaurant.getElementsByTagName('option');

		// remove options after index 2
		restaurant.style.width = restaurant.getDimensions().width;
		restaurant.options.length = 1;

		for (var i in cityrest[element.value]["restaurants"]) {
			var temp = document.createElement('option');
			temp.setAttribute('value', cityrest[element.value]["restaurants"][i]["id"]);
			temp.appendChild(document.createTextNode(cityrest[element.value]["restaurants"][i]["name"]));
			restaurant.appendChild(temp);
		}
	}
}

function checksubmit(e) {
	return false;
	var key = window.event ? e.keyCode : e.which;
	if (key == 13) {
		sendInputs();
		resultsToggle();
	}
}