// JavaScript Document
var arrRowCellsMembers = null;

window.addEvent('domready', function() {
	if ($('MembersNumber'))
	{
		$('MembersNumber').addEvent('keyup', ManageMembersRows);
	}
});

function InitRow (pRowBodyId){
	
	var _arrResult = new Array();
	var _oBodyRows = document.getElementById(pRowBodyId);
	var _oRow = _oBodyRows.rows[0]; 
	var _arrCells = _oRow.cells;
	for(var i = 0; i < _arrCells.length; i++){
		_arrResult[i] = new Object();
		_arrResult[i]['width'] = _arrCells[i].style.width;
		_arrResult[i]['innerHTML'] = _arrCells[i].innerHTML;
	}
	_oBodyRows.deleteRow(0);
	return _arrResult;
};

function ManageRows(pRowBodyId, pCount, pRowCells)
{
	var _oBodyRows = document.getElementById(pRowBodyId);
	var _iNbrRows = _oBodyRows.rows.length;
	var _iRowCount = pCount;
	
	if(!isNaN(_iRowCount))
	{
		if(_iRowCount <= 100)
		{
			if(_iNbrRows != _iRowCount)
			{
				if(_iRowCount > 0)
				{
					if(_iNbrRows > _iRowCount)
					{
						// we have to remove some rows
						for(var i =(_iNbrRows-1); i > (_iRowCount-1); i--)
						{
							_oBodyRows.deleteRow(i);
						}
					} else {
						// we have to add some rows
						for(var i = _iNbrRows; i < _iRowCount; i++)
						{
							var _oRow = _oBodyRows.insertRow(i);
							for (var j = 0 ; j < pRowCells.length; j++){
								_oCell = _oRow.insertCell(j);
								_oCell.innerHTML = pRowCells[j]['innerHTML'].replace(/__Index/gi, i);
								_oCell.style.width = pRowCells[j]['width'];
							}
						}
					}
				} else {
					for(var i = (_iNbrRows-1); i >= 0; i--)
					{
						_oBodyRows.deleteRow(i);
					}
				} 
			}
		}
	}
};

function InitReservationForm(){
	arrRowCellsMembers = InitRow('MembersListBody');
};

function ShowHideReservationBlock(pCheckBoxID, pBlockID){
	if(document.getElementById(pCheckBoxID).checked == true){
		ShowBlockElement(pBlockID);
	} else {
		HideBlockElement(pBlockID);
	}
};

function ManageMembersRows(){
	ManageRows('MembersListBody', new Number(document.getElementById('MembersNumber').value), arrRowCellsMembers);
};

function JumpTo(pId, pIdToJump){
	if(document.getElementById(pId).maxLength == document.getElementById(pId).value.length){document.getElementById(pIdToJump).focus();}
};
