
// Rsheet. Creates html spreadsheets for sets of objects in Ror.
// 2007-jan-21 brockman villc

// Notes: could use 'tabindex' property on text input fields for rsheet.

// Requires prototype.js to be loaded.

// In my experience, return is different on iE.
KEY_RETURN_IE = 10;

function rsheet_onkeypress(cell_id,e)
{
	HANDLE_SHIFT_ARROWS = 0
	// 	window.alert('rsheet_keypress: ' + cell_id + "; e.keyCode: " + e.keyCode + " e.shiftKey: " + e.shiftKey + " e.ctrlKey: " + e.ctrlKey );

	// the number + letter keys are set into charCode
	// the funny enter, tab, etc are set into keyCode
	// there are other modifiers that can help - e.altKey, e.ctrlKey
	//e.altKey
	//e.ctrlKey
	//e.metaKey
	//e.shiftKey
	// Event.* comes from prototype.

	// Note, to prevent the enter key from submitting the document,
	// I am not putting a button = submit in this form, instead the
	// apply button is just a javascript button that submits the form.

	// TOOD: Make backsapce not go backwards when in a select tag.

	if ( ( e.keyCode == Event.KEY_RETURN || e.keyCode == KEY_RETURN_IE )
			&& e.ctrlKey ) { 
			// 	window.alert('Ctrl+Return pressed - SAVE DOCUMENT');
			rsheet_id = $('rsheet_id').value

			// disable the apply button
			$('rsheet_'+rsheet_id+'_button').disabled=true;
			rsheet_form = $('rsheet_'+rsheet_id+'_form').submit();
		//window.alert('rsheet_submit');

	} else if ( e.keyCode == Event.KEY_TAB && ! e.shiftKey ) {
		// NO OP - The browser handles this pretty well already
		// if tab, next col
		// rsheet_focuson(rsheet_math(cell_id,0,1));
	} else if ( e.keyCode == Event.KEY_TAB && e.shiftKey ) {
		// NO OP - The browser handles this pretty well already
		// if shift tab, prev col
		// rsheet_focuson(rsheet_math(cell_id,0,-1));
	} else if ( e.keyCode == Event.KEY_RETURN && ! e.shiftKey && ! e.ctrlKey) {
		// if return, new row
		rsheet_focuson(rsheet_math(cell_id,1,0));
	} else if ( e.keyCode == Event.KEY_RETURN && e.shiftKey ) {
		// if shift return, prev row
		rsheet_focuson(rsheet_math(cell_id,-1,0));

	} else if ( HANDLE_SHIFT_ARROWS ) { 

		if ( e.keyCode == Event.KEY_LEFT && e.shiftKey ) { 
			// LEFT + Shift
			rsheet_focuson(rsheet_math(cell_id,0,-1));
		} else if ( e.keyCode == Event.KEY_RIGHT && e.shiftKey ) { 
			// RIGHT + Shift
			rsheet_focuson(rsheet_math(cell_id,0,1));
		} else if ( e.keyCode == Event.KEY_DOWN && e.shiftKey ) { 
			// DOWN + Shift
			rsheet_focuson(rsheet_math(cell_id,1,0));
		} else if ( e.keyCode == Event.KEY_UP && e.shiftKey ) { 
			// UP + Shift
			rsheet_focuson(rsheet_math(cell_id,-1,0));
		}
	}

	// TODO: I can return false here (and in the hmtml onkeypress call) to prevent the keystroke from
	// passing down into the normal browser layer. This is were I could do the up + down override magic.

}

//----------------------------------------

function rsheet_math(cell_id, row, col )
{

	// pick it apart

	pieces = cell_id.split('_');

	newCellId = '';

	for ( var i = 0; i < pieces.length; i ++ ) { 
		piece = pieces[i];

		if ( row != 0 && piece.substring(0,3) == 'row' ) {
			// turn 
			number = parseInt(piece.substring(3)) + row;
			piece = 'row' + number;
		}

		if ( col != 0 && piece.substring(0,3) == 'col' ) {
			// turn 
			number = parseInt(piece.substring(3)) + col;
			piece = 'col' + number;
		}

		// todo, n eed to do bounds checking.

		newCellId += ''+piece;
		if ( i < pieces.length-1 ) { newCellId += '_'; }
	}

	return newCellId;

}




function rsheet_focuson(cell_id)
{
	// window.alert("rsheet_focuson(" + cell_id +")");

	elem = $(cell_id+"_input");
	if ( elem ) { elem.focus(); }

	// Alert that displays when out of bounds.
	//else { window.alert("No element at : " + cell_id + " to focus on!"); }

}

function rsheet_onfocus(cell_id)
{
//		window.alert('rsheet_focus: ' + cell_id + "->" + $(cell_id+'_td') )

	// hilight the td background
	$(cell_id+'_input').className = 'rsheet_focus_input';
}


function rsheet_onblur(cell_id)
{
	// restore the orig color of the td background
	$(cell_id+'_input').className = 'rsheet_blur_input';
}
