/**
 * This function refreshes the page with all querystrings resend
 * and with "querystring"'s value changed to "value".
*/
function refresh_qs(qsnames, values) {
	// If the values are strings, then convert to arrays.
	if( typeof qsnames == 'string' ) {
		qsnames = [qsnames];
		values = [values];
	}
	
	var len = qsnames.length;
	
	// Get all querystrings
	var url = window.location.href;
	
	if( url.indexOf('?') >= 0 ) {
		// There is querystrings in the url.
		url = '&' + url.substring( url.indexOf('?') + 1 );
	} else {
		 url = '';
	}
	
	// Loop through the querystrings to update and update them.
	for(var i = 0; i < len; i++) {
		var regexp = new RegExp('&' + qsnames[i] + '=[^&]*', 'gi');
		var qs = '&' + qsnames[i] + '=';

		if( url.indexOf(qs) >= 0 ) {
			// The querystring is already a querystring, so replace it.
			url = url.replace(regexp, qs + values[i]);
		} else {
			// The querystring does not exist, so append it.
			url += qs + values[i];
		}
	}
	
	window.location = '?' + url.replace(/^&+/, '');
	
}

// Add CLICKCOLOR to all rows in table lists.
$(document).ready(function() {
	$('table.list tbody tr').click(function() {
		if( $(this).hasClass('colored') ) {
			// There has been clicked on the row which already is colored.
			$(this).removeClass('colored');
		} else {
			// There has been clicked on a not-colored row.
			$('table.list tbody tr.colored').removeClass('colored');
			$(this).addClass('colored');
		}
	});
});

// Add RESETVALUE to inputs field that should reset the value on blur.
$(document).ready(function() {
	$('input.resetvalue').focus(function() {
		if( $(this).val() == this.defaultValue ) {
			$(this).val('');
		}
	}).blur(function() {
		if( $(this).val() == '' ) {
			$(this).val(this.defaultValue);
		}
	});
});

// Add SUBMIT functionality to inputs fields within no form - using the GET method. (Keep querystrings)
$(document).ready(function() {
	$('input.submit').keyup(function(e) {
		if(e.keyCode == 13) {
			// If ENTER is pressed submit.
			refresh_qs($(this).attr('name'), $(this).val());
		}
	});
});
