function playslip() {
	//var game = $('form').parent().parent().parent().parent().attr('class');
	//Finds the first luckydip class and uses the class that follows it
	
	if($('.c15 .luckydip').attr('class')){
		$.imageprefix = $('.c15 .luckydip').attr('class').split(' ').slice(-1); 
		$('.c15 .luckydip').replaceWith('<li class="luckydip"><a class="luckydiplink" href="#"><img src="/player/g/playslip/'+$.imageprefix+'_lucky_dip_small.png" alt="Lucky Dip" width="42" height="26" class="luckydipimg png_bg" /></a></li>');
	}
	if($('.c16 .luckydip').attr('class')){
		$.imageprefix = $('.c16 .luckydip').attr('class').split(' ').slice(-1); 
		$('.c16 .luckydip').replaceWith('<li class="luckydip"><a class="luckydiplink" href="#"><img src="/player/g/playslip/'+$.imageprefix+'_lucky_dip.png" alt="Lucky Dip" width="67" height="23" class="luckydipimg png_bg" /></a></li>');
	}
	
	
/* CLEARING FUNCTION */
$('.clear').replaceWith('<li class="clear"><a class="clearline" href="#">Clear</a></li>');
$('.clearline').bind('click',function(){enableClearLine(this); return false;});
// Enables clearing of a whole play line
function enableClearLine(row) {
	var playline = $(row).parent().parent().attr('id');
	$('#' + playline + ' > li > input:text').val('');
	$('#' + playline + ' > li > input:checkbox:checked').attr('checked', false);
	$('#' + playline + ' > li > input:text').removeAttr('readonly');
	$('#' + playline + ' > li > input:checkbox').removeClass('plus5checked');
	var gameClass = $(row).parents(".playslip").attr('class');
	var game = gameClass.split(" ");
	if (game = "c15") {
		$("#jserrmsgcontainer_lo").addClass("hide");
	} 
	if (game = "c16") {
	    $("#jserrmsgcontainer_em").addClass("hide");
	}
	$('#' + playline).removeClass("error");
}
	
	//Add Plus 5 for Lotto
	
	if($('.c15 .fivesbox').attr('class')){
		$('.fivesbox').removeClass('fivesbox').addClass('fivesboxreplaced');
	}
	
	if($('.c15 #playsliplist_lo')){
		$('#playsliplist_lo').addClass('jsenabled');
	}
	
	//Opacity for disabled Plus 5
	if($('.c15 ol.fivesdisabled').attr('class')){
		$('.fivesboxreplaced').animate({opacity: 0.6});
	}
	
	$('.luckydiplink').bind('click',function(){enableLuckyDip(this); return false;});
}

function enableLuckyDip(row) {
	var playline = $(row).parent().parent().attr('id');
	$('#' + playline + ' > li > input:text').val('LD');
	$('#' + playline + ' > li > input:text').attr('readonly','readonly');
	$('#' + playline).removeClass("error");
}

function outputError(currentElement, gameID, whichRow, errorMessage) {
	$('.'+gameID+' div.errormess').removeClass("hide");
	$('.'+gameID+' div.errormess span').text(errorMessage);
	$('#'+whichRow).addClass("error");
	$('#'+focusID).addClass("error").val("");
	setTimeout("document.getElementById(focusID).focus();",1);
}

$(document).ready(function(){
	//
	var errorDuplication = 0;
	var errorRange = 0;
	var errorNan = 0;
	//
	playslip();
	var whichRow="init";
	$('input[class~="Input"]').focus(function() {
		// Returns the playline		
		whereAmI = $(this).parent().parent().attr("id");
		whichRow=whereAmI;
		// returns the game
		whichPlayslip = $(this).parents('div.playslip').attr("class");	
		whichGame = whichPlayslip.split(" ");
	});
	
	$('input[class~="Input"]').focus(function(){
		var fieldValue = $(this).val();
		if (fieldValue=="LD") {
			$('.'+whichGame[0]+' div.errormess').removeClass("hide");
			$('.'+whichGame[0]+' div.errormess span').text("To play your own numbers you must first clear Lucky Dip.");
		}
	});
	
	$('input[class~="Input"]').change(function(row) {
    	var $current = $(this);
		var fieldLength = $(this).val().length;
		var fieldValue = $(this).val();
		
		//alert(playline);
		$('#'+whichRow+' input[class~="Input"]').each(function() {
       		if ($(this).val() == $current.val() && $(this).attr('id') != $current.attr('id') && $(this).val() != "") {
				$('.'+whichGame[0]+' div.errormess span').text("You have entered one number twice. Please choose a different number.");
				$('.'+whichGame[0]+' div.errormess').removeClass("hide");
				$('#'+whichRow).addClass("error");
				$($current).addClass("error");
				$($current).val("");
				focusID = $current.attr("id");
				setTimeout("document.getElementById(focusID).focus();",1);
			} 
		});
		
		if (fieldLength==1) {
			$(this).val("0"+fieldValue);
		}
		
		if (whichGame[0]=="c15"){
			focusID = $(this).attr("id");
			if ((fieldValue > 49 | fieldValue < 1) && fieldValue!="LD" && fieldValue!="" ) {
				outputError(focusID, whichGame[0], whichRow, "Please enter a number between 1 and 49.");
			}
		}
		
		if (whichGame[0]=="c16"){
			focusID = $(this).attr("id");
			if ($(this).hasClass("luckyStar")) {
				if ((fieldValue > 11 | fieldValue < 1) && fieldValue!="LD" && fieldValue!="" ) {
					outputError(focusID, whichGame[0], whichRow, "Please enter a number between 1 and 11.");
				}
			} else {
				if ((fieldValue > 50 | fieldValue < 1) && fieldValue!="LD" && fieldValue!="" ) {
					outputError(focusID, whichGame[0], whichRow, "Please enter a number between 1 and 50.");
				}
			}
		}
		
		if(isNaN($(this).val()) && fieldValue!="LD" && fieldValue!="") {
			focusID = $(this).attr("id");
			outputError(focusID, whichGame[0], whichRow, "Please enter numbers only.");
		}
	});
	
	$('input[class~="Input"]').keydown(function() {
		if ($(this).hasClass("error")) {
			$(this).removeClass("error");
			$('#'+whichRow).removeClass("error");
			$('.'+whichGame[0]+' div.errormess').addClass("hide");
		}	
	});
});


