
var num_dimensions = [];
var num_selection = [];

function linkNumber_click() {
		
	num_selection = getSelectedSeatsArray();
	
	if(num_selection.length == 0) {
		alert('Please select some seats.');
	}
	else {
		
		num_dimensions = getSelectDimensions(num_selection);
		seatNumberMatrix(num_selection, num_dimensions);
		
		$('form#formNumber').dialog({
			modal: true, 
			title: 'Number Seats',
			width: '90%',
			buttons: {
				"Save": function() {
					// bind form using 'ajaxForm' 
					var options = { 
					    target: '#wrapper-number-form',
						success: formNumber_Submitted,
					};
					$('form#formNumber').ajaxForm(options);
					$('form#formNumber').submit();
					
					$(this).dialog( "close" );
				},
				Cancel: function() {
					$(this).dialog( "close" );
				}
			}
		});
	}
}

function formNumber_Submitted() {
	fireboxPlanEdit_init($('#plan'));
}

function convertIfNeededBase10(value) {

	if(!isNaN(parseInt(value))) {
		base10 = value;
	}
	else {
		base10 = AlphaToNumericBase10(value);
	}
	
	return base10;
}

function linkPopulate_click() {
	
	// get the steps
	var step_x = parseInt($('#txtXStep').val());
	var step_y = parseInt($('#txtYStep').val());	
	
	// get the starting values
	var starting_x = $('#txtXStart').val();
	var starting_y = $('#txtYStart').val();
	
	// cursors
	var x_base10_cur = starting_x;
	var y_base10_cur = starting_y;
	var selection_index = 0;
		
	$('#seat-numbering-matrix ')	
	
	for (y = 0; y <= num_dimensions[0]-1; y++) {		
		
		for (x = 0; x <= num_dimensions[1]-1; x++) {
			
			// create a pointer to the textbox
			var element = $('#num_' + num_selection[selection_index][0] + '_' + num_selection[selection_index][1]);
			
			element.val(y_base10_cur + '/' + x_base10_cur);
			x_base10_cur = incrementBase10(x_base10_cur);
			
			// increament the seat index
			selection_index++;
		}
		
		x_base10_cur = starting_x;
		y_base10_cur = incrementBase10(y_base10_cur);
	}
}

function incrementBase10(value) {
	
	var out = 0;
		
	if (isNumeric(value) == false) {
		out = incrementLetters(value);	
	} else {
		value = parseInt(value);
		out = value + 1;
	}
	
	//console.log(value, isNumeric(value), out);
	
	return out;
}

function incrementLetters(value, decrease) {
	
	if (!decrease) var decrease = false;
	
	var out = '';
	var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	var huns = 0; var tens = 0; 
	
	// currently we can handle thousands
	if (value.length > 3) {
		return 'error';
	}
	
	var index = 0;
	for (var i = value.length - 1; i >= 0; i--) {
		
		if (index == 0) {
			ones = alpha.indexOf(value[i]) + 1;
		}
		if (index == 1) {
			tens = (alpha.indexOf(value[i]) + 1) * 26;
		}
		index++;
	}
	
	if (decrease) {
		ones--;
	} else {
		ones++;
	}
		
	if (ones > 26) {
		ones = 1;
		tens = tens + 26;
	}	
	
	if (ones < 1) {
		ones = 26;
		tens = tens - 26;
	}
	
	if (tens < 26) {
		tens = -1;
	}
		
	if (tens > -1) 
		out += alpha.charAt((tens / 26) - 1).toString();
	
	out += alpha.charAt(ones - 1).toString();
	
	return out;
}

function linkPopulate_click_old() {
	
	// declarations
	var starting_x = $('#txtXStart').val();
	var starting_y = $('#txtYStart').val();
	var step_x = parseInt($('#txtXStep').val());
	var step_y = parseInt($('#txtYStep').val());	
	var x_base10 = 0;
	var y_base10 = 0;
	var x_base10_cur = 0;
	var y_base10_cur = 0;
	var selection_index = 0;
	var x_alpha = false;
	var y_alpha = false;
	
	// are these letters
	if(isNaN(parseInt(starting_x))) {
		x_alpha = true;
	}
	if(isNaN(parseInt(starting_y))) {
		y_alpha = true;
	}
	
	// if base26 convert to 10
	x_base10 = parseInt(convertIfNeededBase10(starting_x));
	y_base10 = parseInt(convertIfNeededBase10(starting_y));	
	
	// start a loop
	y_base10_cur = y_base10;
	for (y = 0; y <= num_dimensions[1]-1; y++) {
		
		x_base10_cur = x_base10;
		
		for (x = 0; x <= num_dimensions[0]-1; x++) {
			
			// set the numbering for the seat
			var element_id = '#num_' + num_selection[selection_index][0] + '_' + num_selection[selection_index][1];
			
			var x_value, y_value;
			
			if(x_alpha) {
				x_value = base24NumericToAlpha(x_base10_cur);
			}
			else {
				x_value = x_base10_cur;
			}
			if(y_alpha) {
				y_value = base24NumericToAlpha(y_base10_cur);
			}
			else {
				y_value = y_base10_cur;
			}
			
			var value = x_value + '/' + y_value;
			$(element_id).val(value);		
			
			// step x
			x_base10_cur = x_base10_cur + step_x;
			
			// increament the seat index
			selection_index++;
		}
		
		// step y
		y_base10_cur = y_base10_cur + step_y;
	}
}

function seatNumberMatrix(selection, dimensions) {
	
	var html = '';
	var i = 0;
	
	html += '<table>';
	
	
	for (x = 0; x <= dimensions[0] - 1; x++) {
		html += '<tr>';	
		for (y = 0; y <= dimensions[1] - 1; y++) {
			html += seatNumberMatrixCell(selection[i][0], selection[i][1]);
			i++;
		}
		html += '</tr>';
	}
		
	html += '</table>';
	
	$('#seat-numbering-matrix').html(html);
}

function seatNumberMatrixCell(x, y) {
	
	var element_id = 'num_' + x + '_' + y;
	var html = '<td><input id="' + element_id + '" name="' + element_id + '" type="text" size="4"/></td>';
	
	return html;
}

function getSelectedSeatsArray() {
	
	var selection = [];
	
	$('td.plan-edit-selection.seat').each(function(index, value){	
		selection.push($(value).attr('id').split('_'));
	});
	
	return selection;
}


function getSelectDimensions(selection) {
	
	var width = 1;
	var height = 0;
	var firstRow = selection[0][0];
	var out = [];
				
	for (i=0;i<=selection.length;i++) {
		if(selection[i+1] != undefined) {
			if(selection[i+1][0] == firstRow) {
				width++;
			}
		}
		else {
			break;
		}
	}
	
	height = selection.length / width;
	out.push(width);
	out.push(height);
		
	return out;
}
