/**
 * Is used by the SmileTable tables.
 */

function clearTableHeaders() {
	map(document.getElementsByTagName("th"), function(a){
		map(a.getElementsByTagName("input"), function(b) {
			b.value = "";
			b.onblur();
		});
	});
}

function changePage(pageNr, id) {
	var page = document.getElementById("page_" + id);
	page.value = pageNr;
	page.form.method="get";
	submitForm(page.form);
}

function tableFilterOnEnter(event, obj){
	obj.form.method = "get";
	return submitOnEnter(event, obj);
}

function showInput(fieldName, currentLink) {
	var field = document.getElementById(fieldName); 
	field.style.display = 'inline';
	field.value = currentLink.innerHTML;
	field.focus();
	field.select();
	currentLink.style.display = 'none';
	return false;
}

function hideInput(linkName, currentField) {
	var link = document.getElementById(linkName);
	
	// Hide the field if the text is the same as the link or if the field is empty.
	if (link.innerHTML == currentField.value || currentField.value == "") {
		link.style.display = 'inline';
		currentField.style.display = 'none';
		currentField.value = "";
	}
	return false;
}

function submitOnEnter(event, field) {
	if (onEnter(event, "true"))
	{
		submitForm(field.form);
		return false;
	}
	return true;
}

function sortFromTable(icon, orderBy, sortOrder) {
    var form = getEnclosingElem(icon, "form");
    if (form.orderby) {
        form.orderby.value = orderBy;
    }
    if (form.sortorder) {
        form.sortorder.value = sortOrder;
    }
    submitForm(form);
}

function toExport(full)
{
	var exportLocation = location.href.replace(/[\w]+\.action.*/, 'Export.action');
	
	if (exportLocation.indexOf('?') == -1)
		exportLocation += "?";
	else
		exportLocation += "&";
	
	exportLocation += "fnc="+document.getElementById("bodyform").fnc.value;
	exportLocation += "&prv="+document.getElementById("bodyform").prv.value;
	exportLocation += "&form="+document.getElementById("bodyform").form.value;
	exportLocation += "&search="+document.getElementById("bodyform").search.value;
	exportLocation += urlVar('overview');
	
	exportLocation += urlVar('orderby');
	exportLocation += urlVar('sortorder');
	exportLocation += urlVar('page');
	exportLocation += urlVar('elementsPerPage');
	exportLocation += urlVar('totalElements');
	
	var form = document.getElementById("bodyform");
	var i = 1;
	while (form['hdr' + i + 'value'])
	{
		exportLocation += urlVar('hdr' + i + 'value');	
		i++;
	}
		
	exportLocation += '&all=true' + amp + 'method=csv';
	if(full){exportLocation += '&full=true';}
	location.href = exportLocation;
}

function urlVar(param)
{
	var value = document.getElementById("bodyform")[param];
	if (value == null || value == undefined || value.value == null || value.value == undefined || value.value == '')
		return '';
	return '&' + param + '=' + value.value
}