function getClassByRel(rel)
{
	var thisClass;
	if( rel == 'o')
	{
		if($("#suitMatterBtn").val() == 'Suits Matter')
		{
			thisClass = 'donotsuit';
		}else{
			thisClass = 'offsuit';
		}
	}else if(rel == 's'){
		thisClass = 'suited';
	}else if(rel == 'p')
	{
		thisClass = 'pair';
	}
	return thisClass;
}
function updateTDSelection(td)
{
	var prevClassName= td.className;
	
	var colnumber = td.getAttribute("c");
	var rownumber = td.getAttribute("r");
	var thisRel = td.getAttribute('rel');
	var originClass = getClassByRel(thisRel);
				
	if(prevClassName != 'selected')
	{
		td.className = 'selected';
		
		if(thisRel == 'p')
		{
			if( $("td[rel='" + thisRel + "']").hasClass(originClass) == false )
				$("input[type='checkbox'][rel='p']").attr('checked', true);
		}else{
			if( $("td[rel='" + thisRel + "'][r='" + rownumber + "']").hasClass(originClass) == false )
				$("#" + thisRel + "r" + rownumber).attr('checked', true);

			if( $("td[rel='" + thisRel + "'][c='" + colnumber + "']").hasClass(originClass) == false)
				$("#" + thisRel + "c" + colnumber).attr('checked', true);
		}
	}else{
		
		td.className = originClass;
		if(thisRel == 'p')
		{
			$("input[type='checkbox'][rel='p']").attr('checked', false);
		}else{
			$("#" + thisRel + "c" + colnumber).attr('checked', false);
			$("#" + thisRel + "r" + rownumber).attr('checked', false);
		}
	}
	return false;
}

function clearAll()
{
	$(".selected").each(function(){
		this.className = getClassByRel($(this).attr("rel"));
	});
	$("input[type='checkbox'][rel='o']").attr('checked', false);
	$('#sAllchk').attr('checked', false);
	
	$("input[type='checkbox'][rel='s']").attr('checked', false);
	$('#oAllchk').attr('checked', false);
	$("input[type='checkbox'][rel='p']").attr('checked', false);
}

function suitMatter()
{
	if($("#suitMatterBtn").val() == 'Suits Matter')
	{
		$("#if_suits_matter").val(1);
		$("#suitMatterBtn").val("Suits do not Matter");
		$("td[rel='s']").show();
		$(".headerTop").show();
		$(".cbTDs").show();
		$(".rightChk").show();
		$(".headerHidden").addClass('gray');

		$("td[rel='o'][class='donotsuit']").removeClass('donotsuit').addClass('offsuit');
	}else{
		$("#if_suits_matter").val(0);
		$("#suitMatterBtn").val("Suits Matter");
		$("td[rel='s']").removeClass().addClass('suited').hide();
		$(".headerTop").hide();
		$(".cbTDs").hide();
		$(".rightChk").hide();
		$(".headerHidden").removeClass('gray');

		$("td[rel='o'][class='offsuit']").removeClass('offsuit').addClass('donotsuit');
	}
}
function selectCol(type, id)
{	
	$("td[rel='" + type + "'][c='" + id + "']").each(function() {
		var colnumber = $(this).attr("c");

		if($("#" + type + "c" + colnumber).attr('checked') == true)
		{
			$(this).removeClass().addClass('selected');
		}else{
			$(this).removeClass('selected').addClass(getClassByRel(type));
		}
	});
}
function selectRow(type, id)
{
	$("td[rel='" + type + "'][r='" + id + "']").each(function() {
		var rownumber = $(this).attr("r");

		if($("#" + type + "r" + rownumber).attr('checked') == true)
		{
			$(this).removeClass().addClass('selected');
		}else{
			$(this).removeClass('selected').addClass(getClassByRel(type));
		}
	});
}
function selectAllsuited(checkbox)
{
	if(checkbox.checked != '')
	{
		$("td[rel='s']").removeClass().addClass('selected');
		$("input[type='checkbox'][rel='s']").attr('checked', true);
	}else{
		$("td[rel='s']").removeClass('selected').addClass('suited');
		$("input[type='checkbox'][rel='s']").attr('checked', false);
	}
}
function selectAlloffsuit(checkbox)
{
	if(checkbox.checked != '')
	{
		$("td[rel='o']").removeClass().addClass('selected');
		$("input[type='checkbox'][rel='o']").attr('checked', true);
	}else{
		var originClass = getClassByRel('o');
		$("td[rel='o']").removeClass('selected').addClass(originClass);

		$("input[type='checkbox'][rel='o']").attr('checked', false);
	}
}
function selectAllpair(checkbox)
{
	if(checkbox.checked != '')
	{
		$("td[rel='p']").removeClass().addClass('selected');
		$("input[type='checkbox'][rel='p']").attr('checked', true);
	}else{
		$("td[rel='p']").removeClass('selected').addClass('pair');
		$("input[type='checkbox'][rel='p']").attr('checked', false);
	}
}

function submitRange()
{
	var tab = document.getElementById("rangeTable");
	var range = "";

	for(var i = 0; i< 15; i++)
	{
		for(var j=0;j<15;j++)
		{
			if (tab.rows[i].cells[j].className == "selected")
			{
				range += (tab.rows[i].cells[j].getAttribute("r") - 1) + "," + (tab.rows[i].cells[j].getAttribute("c") - 1) + "|";
			}
		}
	}
	if(range == "")
	{
		alert("Please select at least one hand");
	}else{
		$('#range').val(range.substr(0, range.length - 1));
		document.rangeFrm.submit();
	}
}