function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function run_query()
{

	if(document.forms.form1.firstname.value == '')
		document.getElementById('formerror').innerHTML = "<br />You didn't fill out your first name!";
	else if(document.forms.form1.lastname.value == '')
		document.getElementById('formerror').innerHTML = "<br />You didn't fill out your last name!";
	else if(document.forms.form1.email.value == '')
		document.getElementById('formerror').innerHTML = "<br />You didn't fill out your email address!";
	else if(document.forms.form1.occupation.value == '-1')
		document.getElementById('formerror').innerHTML = "<br />You didn't tell us what you do!";
	else
	{
		createCookie("Voted",0,365);
		var url = 'index.php';
		var values = $H({vote1: $F('vote1'), firstname: $F('firstname'), lastname: $F('lastname'), email: $F('email'), occupation: $F('occupation'), business: $F('business'), ajax_request: '1'});
		var params = values.toQueryString();
		ajax = new Ajax.Request(
			url,
			{
				method: 'post', parameters: params,
				onComplete: function(xhr,json)
				{
					tagsubmitted();
					//Element.update('table_info', xhr.responseText);
					//$('query_tb').value = 'select * from ' + values['table'];
				}
			}
		);
	}
}


/*function plusminusupdate() {
	var Parent = document.getElementById('topten');
	toptenlistimg = Parent.getElementsByTagName('img');
	toptenlistli = Parent.getElementsByTagName('li');
	
	for (var i = 0; i < toptenlistli.length; i++)
	{
		toptenlistimg[i].src = "i/minus.png";
		toptenlistli[i].innerHTML = toptenlistli[i].innerHTML.replace("bartenderitem","toptenitem");
//		alert(toptenlistli[i].innerHTML);
	}

//	alert(Parent.firstChild.innerHTML);


//	var Parent = document.getElementById('bartenders');
//	toptenlistimg = Parent.getElementsByTagName('img');
//	toptenlistli = Parent.getElementsByTagName('a');
	
//	for (var i = 0; i < toptenlistli.length; i++)
//	{
//		toptenlistimg[i].src = "i/movetotop.png";
//		toptenlistli[i].innerHTML = toptenlistli[i].innerHTML.replace("toptenitem","bartenderitem");
//	}

}
*/
function movetotop(item, name, whichlist) {
	//remove from list
	var Childid = "item_" + item;
	var Childtoremove = document.getElementById(Childid);

	Childtoremove.parentNode.removeChild(Childtoremove);

	if(whichlist == "bartenderitem")
	{
		//add to topten list
		var Parent = document.getElementById('topten');
		var NewLI = document.createElement("LI");

		NewLI.id = "item_" + item;
		NewLI.innerHTML = name + ' <a href=\"#\" onclick=\"movetotop('+item+',\''+name+'\',\'toptenitem\'); this.blur(); return false;\"><img src=\"i/minus.png\" width=\"35\" height=\"30\" alt=\"remove bartender\" class=\"movetotop\" /><\/a><\/li>';
		Parent.insertBefore(NewLI, Parent.firstChild);
	}

	if(whichlist == "toptenitem")
	{
		//add to bartender list
		var Parent = document.getElementById('bartenders');
		var NewLI = document.createElement("LI");

		NewLI.id = "item_" + item;
		NewLI.innerHTML = name + ' <a href=\"#\" onclick=\"movetotop('+item+',\''+name+'\',\'bartenderitem\'); this.blur(); return false;\"><img src=\"i/movetotop.png\" width=\"35\" height=\"30\" alt=\"add bartender\" class=\"movetotop\" /><\/a><\/li>';
		Parent.insertBefore(NewLI, Parent.firstChild);
	}

	//rebuild the lists so they are sortable
	createsortables();
	var newOrder = Sortable.serialize(document.getElementById('topten'));
	$('vote1').value = newOrder;

	//determines how many items are in the topten list
	var topten = newOrder.split("&");
	var toptencount = 0;
	while(toptencount < topten.length)
		toptencount+=1;
	if(toptencount>10)
		document.getElementById('voteerror').innerHTML = "<p>Think you're cool and can vote for more than ten?  Try hitting that minus sign...<\/p>";
	else 
		document.getElementById('voteerror').innerHTML = "<p><\/p>";
}

function createsortables()
{

		Sortable.destroy('bartenders');
		Sortable.destroy('topten');
		
				Sortable.create('topten',{containment: ['topten','bartenders'], dropOnEmpty: true, onChange:function(element){
				
				//update img and onclick
				//plusminusupdate();

				//put data into the form for submission
				var newOrder = Sortable.serialize(document.getElementById('topten'));
				$('vote1').value = newOrder;
//				document.getElementById('testarea').innerHTML = $('vote1').value;

/*				//determines how many items are in the topten list
				var topten = newOrder.split("&");
				var toptencount = 0;
				while(toptencount < topten.length)
					toptencount+=1;
				if(toptencount>10)
					document.getElementById('voteerror').innerHTML = "<p>Think you're cool and can vote for more than ten?  Try hitting that minus sign...<\/p>";
				else 
					document.getElementById('voteerror').innerHTML = "<p><\/p>";
*/
					}

				});

				//Sortable.create('bartenders',{containment: ['topten','bartenders'], dropOnEmpty: true, scroll:window, onChange:function(element){
				//plusminusupdate();
				//}
				
//				});
}

function tagsubmitted() {
	  	document.getElementById('votetitle1').innerHTML = "Thanks for Voting!";
	  	document.getElementById('darkBackgroundLayer').style.display = 'none';
	  	document.getElementById('submitbox').innerHTML = "<img src=\"i/voted.png\" width=\"195\" height=\"163\" alt=\"voted\" />";
	  	document.getElementById('votebutton').src = "i/voted.png";
	  }
	  
function votingtime() {
	var newOrder = Sortable.serialize(document.getElementById('topten'));
	var topten = newOrder.split("&");
	var toptencount = 0;
	while(toptencount < topten.length)
		toptencount+=1;
	if(toptencount>10)
		document.getElementById('voteerror').innerHTML = "<p>Think you're cool and can vote for more than ten?  Try hitting that minus sign...<\/p>";
	else if(toptencount==10)
		document.getElementById('darkBackgroundLayer').style.display = '';
	else
		document.getElementById('voteerror').innerHTML = "<p>Make sure to pick 10 bartenders!<\/p>";
	}

function keycheck(event)
{
	if(event.keyCode==27)
	document.getElementById('darkBackgroundLayer').style.display = 'none';
}