// JQUERY FUNCTIONs
$(document).ready(function() {
	
	var rowsCounter = $('#matches_table tr').length;
	$('#matches_table td').click(
	   function () {
	   	if ($(this).attr('class') == 'rate center') {
	   		$(this).find('input').attr('checked','checked');
	   		$(this).nextAll().removeClass('selected');
	   		$(this).prevAll().removeClass('selected');
	   		$(this).addClass('selected');
	   	} else if ($(this).attr('class') == 'rate center selected') {
	   		// $(this).removeClass('selected');
	   		for (var i = 0; i < rowsCounter; i++) {
	   			if ($('#matches_table tr').eq(i).attr('class') != 'dontCount') {
                  for (var j = 2; j < 5; j++) {
                     if ($('#matches_table tr').eq(i).children().eq(j).attr('class') == 'rate center selected') {
                     	$('#matchForm').get(0).reset();
                     	$(this).removeClass('selected');
                     }
                  }
	   			}
	   		}
	   	}
         for (var k = 0; k < rowsCounter; k++) {
            if ($('#matches_table tr').eq(k).attr('class') != 'dontCount') {
               for (var l = 2; l < 5; l++) {
                  if ($('#matches_table tr').eq(k).children().eq(l).attr('class') == 'rate center selected') {
                     $('#matches_table tr').eq(k).children().eq(l).find('input').attr('checked','checked');
                  }
               }
            }
         }
	   }
	   );
	
	$('#czysc').click(
		function () {
			$('#matchForm').get(0).reset();
			$('#matches_table td').removeClass('selected');
		}
	);
	
	$('#shoubox_send').click(
		function () {
			setTimeout(function () {$('#shoubox_comment').attr('value','')},100);
			$('#shoubox_send').attr('value','Wyślij (255)');
		}
	);
	
	$('#betAll').click(
		function () {
			var valueCredits = $('#valueCredits').text();
			$('#stawka').attr('value',valueCredits);
         var rate = parseFloat($('#kurs').attr('value'));
         var course = parseFloat($('#stawka').attr('value').replace(/,/,"."));
         var prize = Math.round(rate * course * 100) / 100;
         if(isNaN(prize)) {
            $('#wygrana').html('?');
         } else {
            $('#wygrana').html(prize);
         }
		}
	);
	
	$('#shoubox_comment').keyup(
		function () {
			var charsLimit = 255;
			var charsShoutbox = parseInt($('#shoubox_comment').attr('value').length);
			var margin = charsLimit - charsShoutbox;
			if (margin >= 0) {
				$('#shoubox_send').attr('value','Wyślij (' + margin + ')');
			}
		}
	);
	
	$('#stawka').change(
		function () {
			var rate = parseFloat($('#kurs').attr('value'));
			var course = parseFloat($('#stawka').attr('value').replace(/,/,"."));
			var prize = Math.round(rate * course * 100) / 100;
			if(isNaN(prize)) {
				$('#wygrana').html('?');
			} else {
				$('#wygrana').html(prize);
			}
		}
	);
	
});

function mailtoAdmin() {
	parent.location.href = "mailto:admin@postawkupon.pl";
}
function mailtoMarketing() {
	parent.location.href = "mailto:marketing@postawkupon.pl";
}
function mailtoInfo() {
	parent.location.href = "mailto:info@postawkupon.pl";
}