// JavaScript Document

function setRoom(rID)
{
	$('#buyInType > option:last').html(rooms[rID]['pointNames']);
	if($('#buyInTypeText').html() != '$')
		$('#buyInTypeText').html(rooms[rID]['pointNames']);;
}

function gameStyle(style)
{
	if(style == 'omaha' || style == 'stud') $('#gamestyle').show();
	else  $('#gamestyle').hide();
}

function changeInterval(intervalType)
{
	$('.intervalOpt').hide();
	if(intervalType == 'week')
		$('#interval_week').show();
	if(intervalType == 'single')
		$('#interval_single').show();
}

function changeBuyIn(buyInType)
{
	if(buyInType == 'money')
	{
		$('#buyInTypeText').html('$');
		$('#rake').css({display:'inline'});
	}
	else
	{
		$('#buyInTypeText').html($('#buyInType :selected').html());
		$('#rake').hide();
	}
}

function addTourney()
{
	$('#newForm')[0].reset();
	$('#newForm fieldset:first legend').html(language['addTourney']);
	$('#newForm #formBtn').html(language['addTourney']);
	$('#newForm').slideDown('slow');
	$('#newForm #action').val('add');
}

function editTourney(tID)
{
	var data = tourneys[tID];
	$('.tourney.reporting').removeClass('reporting');
	$('#t_' + tID).addClass('reporting');
	$(document).scrollTop(0)
	$('#newForm fieldset:first legend').html(language['editTourney']);
	$('#newForm #tourneyID').val(tID);
	$('#newForm #formBtn').html(language['editTourney']);
	$('#newForm').slideDown('slow');
	$('#newForm #action').val('edit');
	$('#newForm #room').selectOptions([data['rID']],true);
	setRoom(data['rID']);
	$('#newForm #type').selectOptions([data['type']],true);
	$('#newForm #game').selectOptions([data['game']],true);
	gameStyle(data['game']);
	if(data['gamestyle'])
		$('#newForm #gamestyle').selectOptions([data['gamestyle']],true);
	$('#newForm #name').val(data['name']);
	$('#newForm #hour').val((data['time'].substr(0,2)));
	$('#newForm #minutes').val((data['time'].substr(3,2)));
	$('#newForm #interval').selectOptions([data['interval']],true);
	changeInterval(data['interval']);
	if(data['interval'] == 'week')
	{
		$('#newForm #interval_week').selectOptions([data['intervalOptions']],true);
	}
	else if(data['interval'] == 'single')
	{
		$('#newForm #single_date').val(data['intervalOptions']);
		$('#newForm #interval_single_picker').val(data['datum']);
	}
	$('#iconTab input').attr('checked',null);
	if(data['tablesize'] == '6') $('#newForm #icon_6max').attr('checked','checked');
	else if(data['tablesize'] == '2') $('#newForm #icon_2max').attr('checked','checked');
	else if(data['tablesize'] == '4') $('#newForm #icon_4max').attr('checked','checked');
	for(var i = 0, end = data['icons'].length;i < end;i++)
	{
		$('#newForm #icon_' + data['icons'][i]['file'].toLowerCase()).attr('checked','checked');
	}
	$('#newForm #buyInType').selectOptions([data['buyInType']],true);
	changeBuyIn(data['buyInType']);
	$('#newForm #buyIn').val(data['buyIn']);
	$('#newForm #rake_input').val(data['rake']);
	$('#newForm #guaranteedPricePool').val(data['guaranteedPricePool']);
	$('#newForm #maxPlayers').val(data['maxPlayers']);
}

function reportBug(tID)
{
	$('.tourney .reporting').removeClass('reporting');
	$('#t_' + tID).addClass('reporting');
	$('#reportForm #tID').val(tID);
	$("#report").dialog({
		bgiframe: true,
		height: 300,
		modal: true,
		draggable: false,
		resizeable: false,
		buttons: {
			Send : function() {
				sendReport();
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close : function () {
			$(this).dialog('destroy');
			$('#t_' + tID).removeClass('reporting');	
		}
	});
}

function sendReport()
{
	if($("#reportForm #name").val() == '' || $("#reportForm #email").val() == '' || $("#reportForm #text").val() == '')
	{
		newAlert(false,language['fillAllFields'],'alert');
		return false;
	}
	$.ajax({
		type: "POST",
		url: "/modules/tourneys/ajax/sendReport.php",
		data: $("#reportForm").serialize(),
		dataType : 'json',
		success: function(msg)
		{
			debuglog(msg);
			if(msg['status'] == 'done')
			{
				$('#dialog').attr('title',language['thanks']);
				$('#dialog').html(language['reportThx']);
				$('#dialog').dialog();
		   }
		   else
		   {
			   newAlert(false,msg['msg'],'alert',msg['debug']);
		   }   
		},
		error : function(XMLHttpRequest, textStatus, errorThrown)
		{
			var hiddendebug = false;
			if(XMLHttpRequest['status'] == 404) hiddendebug = language['mainErrorText'] + '<span class="transText">notfound</span>';
			else if(textStatus == 'parsererror') hiddendebug = language['mainErrorText'] + '<span class="transText">unexspected return</span>';
			newAlert(false,hiddendebug);
		}
	});
	$("#report").dialog('close');
}



function testInsert(time)
{
	for(tourney in tourneys)
	{
		debuglog(tourney);
	}
}
