DGS_JS.openLayoutDiv = 0;
DGS_JS.playerCount = 10000;
DGS_JS.divNum = 200;

function alterDivisions(num, division, dName) {
	if (num > 0) {
		var divSelect = getObj('divchange-' + num);
		if (divSelect) {
			var newDivCode = divSelect.options[divSelect.selectedIndex].value;
			var newDiv = divSelect.options[divSelect.selectedIndex].innerHTML.replace(/.{5}$/, '');
			$('.div_input_' + num).css('opacity', 0).val(newDivCode).animate({opacity: 1}, 200);
			$('#div_layout_' + num).html(newDiv);
			
			for (i=1; i<=8; i++) {
				var round_sel = $('#reRound' + i + 'cvID_' + num);
				if (round_sel && round_sel.attr('name')) {
					round_sel.attr('name', round_sel.attr('name').replace(/.{3}$/, newDivCode));
					//$('#course-round' + i).attr('name', $('#course-round' + i).attr('name').replace(/.{3}$/, newDivCode));
				}
			}
			$('#division-' + num).removeClass('no-div');
			if ($('#divchange-' + num + ' option:first').val() == '') { $('#divchange-' + num + ' option:first').remove(); }
		}
	}
}


function checkRoundCount() {
	for (var i=1; i<=8; i++) {
		var box = getObj('roundCount-' + i);
		if (box && box.checked == true) {
			changeRoundCount(i);
		}
	}
}


function changeRoundCount(count) {
	// find the selected round count
	for (var i=count; i<=8; i++) {
		$('.scores-input-reRound' + i).hide();
	}
	for (var i=1; i<=count; i++) {
		$('.scores-input-reRound' + i).show();
	}
	$('#tRounds').val(count);
}

function addRow(divNum, iterations) {
	for (var i=0; i<iterations; i++) {
		var cloneTR = $('#form-scores-' + divNum + ' tr:last').html();
		var newRow = '<tr class="scores" id="score-' + DGS_JS.playerCount + '">' + cloneTR + '</tr>';
		newRow = newRow.replace(/deleteRow\(\d{1,}\)/, 'deleteRow(' + DGS_JS.playerCount + ')');
		newRow = newRow.replace(/deleter\-\d{1,}/, 'deleter-' + DGS_JS.playerCount);
		newRow = newRow.replace(/player\-\d{1,}/, 'player-' + DGS_JS.playerCount);
		newRow = newRow.replace(/\_field\_\d{1,}/g, '_field_' + DGS_JS.playerCount);
		$('#form-scores-' + divNum).append(newRow);
		$('#score-' + DGS_JS.playerCount).hide();

		$('#form-scores-' + divNum + ' tr:last input').each(
			function(index) {
				if (index>1) { $(this).val(''); } 
				else if (index==0) { $(this).val(DGS_JS.playerCount); }
				else if (index==1) { $(this).val($('#divchange-' + divNum).val()); }
				$(this).attr('name', $(this).attr('name').replace(/\_\d{1,}$/, '_' + DGS_JS.playerCount));
			}
		);
		$('#_field_' + DGS_JS.playerCount).html('');
		$('#score-' + DGS_JS.playerCount).fadeIn();
		if ($('#player-' + DGS_JS.playerCount).attr('disabled') == true) { deleteRow(DGS_JS.playerCount); }
		$('#player-' + DGS_JS.playerCount).val(DGS_JS.playerCount);
		DGS_JS.playerCount++;
	}
}

function addRowReg(table_id, iterations) {
	var lastTR = $('#' + table_id + ' tr:last');
	var cloneTR = lastTR.html();
	var lastID = lastTR.attr('id');
	var lastID_int = Math.abs(lastID.replace(/\D/g, ''));
	var lastID_str = lastID.replace(/\d/g, '');
	
	var lastClass = lastTR.attr('class');
	var newID_int = lastID_int + 1;	
	
	for (var i=0; i<iterations; i++) {
		var newID = '' + lastID_str + newID_int;
		var newRow = '<tr class="' + lastClass + '" id="' + newID + '">' + cloneTR + '</tr>';
		
		var re = new RegExp(lastID, 'gi');
		newRow = newRow.replace(re, newID);
		newRow = newRow.replace(/((value\=\")|\_|\-)\d{1,}\"/g, '$1' + newID_int + '"');
		
		newRow = newRow.replace(/reg\_add\_\d{1,}/g, 'reg_add_' + newID_int);
		
		newRow = newRow.replace(/regID\_\d{1,}/g, 'regID_' + newID_int);
		newRow = newRow.replace(/reg\_name\_\d{1,}/g, 'reg_name_' + newID_int);
		newRow = newRow.replace(/reg\_email\_\d{1,}/g, 'reg_email_' + newID_int);
		newRow = newRow.replace(/reg\_pdga\_\d{1,}/g, 'reg_pdga_' + newID_int);
		newRow = newRow.replace(/reg\_paid\_\d{1,}/g, 'reg_paid_' + newID_int);
		newRow = newRow.replace(/dCode\_\d{1,}/g, 'dCode_' + newID_int);
		$('#' + table_id).append(newRow);
		$('#' + newID).hide();
		
		
		$('#' + table_id + ' tr:last input').each(
			function(index) {
				if (index>2) { $(this).val(''); }
			}
		);
		$('#' + table_id + ' tr:last select').each(
			function(index) {
				$(this).attr('selectedIndex', 0);
			}
		);
		$('#regID_' + newID_int).val(0);
		$('#reg-' + newID_int + '-arr').val(newID_int);
		//$('#_field_' + newID_int).html('');
		//alert($('#' + table_id + ' tr:last').html());
		$('#' + newID).fadeIn();
		//if ($('#player-' + newID_int).attr('disabled') == true) { deleteRow(newID_int); }
		//$('#player-' + newID_int).val(newID_int);
		newID_int++;
	}
}
function deleteRowReg(id) {
	
	if ($('#' + id + '-add').attr('disabled') == true) {
		$('#' + id + '-add').attr('disabled', false);
		$('#' + id + ' td').animate({opacity: 1});
		$('#deleter-' + id).attr('class', 'deleter');
		$('#deleter-' + id).attr('title', 'Delete this player from the Registration');
	}
	else {		
		$('#' + id + '-add').attr('disabled', true);
		$('#' + id + ' td').animate({opacity: 0.3});
		$('#deleter-' + id).attr('class', 'deleter-undelete');
		$('#deleter-' + id).attr('title', 'This player will be removed from the Registration upon saving changes. Click to un-delete this player');
	}
}



function addDivision() {
	var divHTML = $('#form-scores-division-1').html();
	
	divHTML = divHTML.replace(/\(1\)/g, '(' + DGS_JS.divNum + ')');
	divHTML = divHTML.replace(/divchange\-1/g, 'divchange-' + DGS_JS.divNum);
	divHTML = divHTML.replace(/division\-1/g, 'division-' + DGS_JS.divNum);
	
	divHTML = divHTML.replace(/div\_layout\_1/g, 'div_layout_' + DGS_JS.divNum);
	divHTML = divHTML.replace(/div\_input\_1/g, 'div_input_' + DGS_JS.divNum);
	divHTML = divHTML.replace(/add\-layout\-1/g, 'add-layout-' + DGS_JS.divNum);
	divHTML = divHTML.replace(/courseLayout\(\'create\'\, (\d{1,})\, 1\)/g, "courseLayout('create', $1, " + DGS_JS.divNum + ")");
	divHTML = divHTML.replace(/cvID\_1/g, 'cvID_' + DGS_JS.divNum);
	divHTML = divHTML.replace(/form\-scores\-1/g, 'form-scores-' + DGS_JS.divNum);
	divHTML = divHTML.replace(/addRow\(1\,/g, 'addRow(' + DGS_JS.divNum + ',');
	
	$('#scores-division-add').append('<div id="form-scores-division-' + DGS_JS.divNum + '">' + divHTML + '</div>');
	$('#form-scores-division-' + DGS_JS.divNum).hide();
	
	$('#div_layout_' + DGS_JS.divNum).html('');
	$('#form-scores-division-' + DGS_JS.divNum + ' tr.scores:not(:first)').remove();
	addRow(DGS_JS.divNum, 1);
	$('#form-scores-division-' + DGS_JS.divNum + ' tr.scores:first').remove();
	$('#form-scores-division-' + DGS_JS.divNum + ' tr.scores:first input.dCode_field').val('');
	
	$('#division-' + DGS_JS.divNum).addClass('no-div');
	$('#divchange-' + DGS_JS.divNum).prepend('<option value="">choose ...</option>');
	$('#divchange-' + DGS_JS.divNum).attr('selectedIndex', 0);
	
	$('#form-scores-division-' + DGS_JS.divNum).animate({opacity: 'show', height: 'show'});
	DGS_JS.divNum++;
}

function applyLayout(round, id) {
	var index = $('#' + id).attr('selectedIndex');
	if (index >= 0) {
		$('.cv_round_' + round).hide().attr('selectedIndex', index).fadeIn();
	}
	return false;
}

function deleteRow(id) {
	if ($('#player-' + id).attr('disabled') == true) {
		$('#player-' + id).attr('disabled', false);
		$('#score-' + id + ' td').animate({opacity: 1});
		$('#deleter-' + id).attr('class', 'deleter');
		$('#deleter-' + id).attr('title', 'Delete this player from the tournament');
	}
	else {
		if ($('#rePlayer_field_' + id).val() == '') {
			var removeDivAll = false;
			if ($('#score-' + id).siblings().length <= 1) {
				removeDivAll = true;
				var division_to_remove = $('#score-' + id).parent().parent().parent();
			}
			if (removeDivAll) {
				$(division_to_remove).animate({opacity: 0}, 300, function() { $(this).remove(); });
			}
			else {
				$('#score-' + id).animate({opacity: 0}, 300, function() { $(this).remove(); });
			}
		}
		else {
			$('#player-' + id).attr('disabled', true);
			$('#score-' + id + ' td').animate({opacity: 0.3});
			$('#deleter-' + id).attr('class', 'deleter-undelete');
			$('#deleter-' + id).attr('title', 'This player will be deleted upon saving scores. Click to un-delete this player');
		}
	}
}


function courseLayout(action, tournID, div) {
	if (action == "create") {
		$.ajax({ 
			method: "GET",
			url: "/ajax_courseLayouts.php",
			data: "js=true&tournID=" + tournID, 
			success: function(responseText) { 
				$('#form-success').hide();
				if (DGS_JS.openLayoutDiv > 0) { $('#add-layout-' + DGS_JS.openLayoutDiv).hide().html(''); }
				$('#add-layout-' + div).hide().html(responseText).slideDown();
				DGS_JS.openLayoutDiv = div;
			} 
		});
	}
	else if (action == 'post') {
		var post_data = "js=true&tournID=" + tournID + "&cvName=" + $('#cvName').val() + "&cvHoles=" + $('#cvHoles').val() + "&cvPar=" + $('#cvPar').val() + "&courseID=" + $('#courseID').val() + "&verify=" + $('#verify').val();
		$.ajax({ 
			method: "GET",
			url: "/ajax_courseLayouts.php",
			data: post_data, 
			success: function(responseText) {
				var result = responseText.split('|');
				var result_msg = '';
				var complete = false;
				if (result[0] == 'PASS') {
					var result_msg = '<div class="message-success">The new course layout has been added.</div>';
					var newCVID = result[1];
					var newCVname = result[2];
					complete = true;
					$('div.course-round select').each( function() {
							$(this).html($(this).html() + '<option value="' + newCVID + '" style="background: #ffd;">' + newCVname + '</option>')
						}
					);
				}
				else {
					var result_msg = '<div class="message-warning">' + result[1] + '</div>';
				}
				$('#form-success').hide().html(result_msg).fadeIn();
				if (complete) { $('#form-layout').slideUp(500, function() { $('#form-layout').remove(); }); }
			} 
		});
	}
	else if (action == 'cancel') {
		$('#form-success').animate({height: 'hide', opacity: 'hide'});
		$('#form-layout-container').slideUp(500, function() { $('#form-layout').remove(); });
	}
}