function $(id){ return document.getElementById(id); }

// Ask for user confirmation and redirect
function confirmAndRedirect(msg, url, ref){
	if((ref != null) && (ref != ''))
		ref = '&ref=' + encodeURIComponent(ref);
	else ref = '';
	if(confirm(msg)) window.location = url + ref;
}

// Select all checkboxes in specified column in given table
function selectAll(obj, col_id, checked, customFunc, forced){
	var table = getParentTable(obj)
	var count = table.rows.length;

	for(var i=1; i<count; ++i){
		var selectBox = table.rows[i].cells[col_id].childNodes[0];
		if(selectBox && (selectBox.type == 'checkbox') && (!selectBox.disabled || forced)){
			selectBox.checked = checked;
			if(customFunc != '') eval(customFunc);
		}
	}
}

/********** Dynamic Form Tools **********/
function getParentRow(obj){
	var row = obj;
	while(!row.rowIndex){
		row = row.parentNode;
		if(row == null) return null;
	}
	return row;
}

function getParentTable(obj){
	var table = obj;
	while(!table.rows){
		table = table.parentNode;
		if(table == null) return null;
	}
	return table;
}

/********** Page Number input field script **********/
function pageNumberInputFocus(obj){
	var id = obj.id;
	if(id.indexOf('page_') != 0) return;

	obj.value = Number(id.substr(5));
	obj.select();
}

function pageNumberInputBlur(obj){
	obj.value = obj.defaultValue;
}

