////////////////////////////////////////////////////////////////////////////////
// replaceGetParamAndReload
//
// extracts given get parameter and value and replaces or adds in url -> reload
//
// get_param_input_id: id of html element to get parameter value from
// get_param: name of parameter to replace or set
////////////////////////////////////////////////////////////////////////////////
function replaceGetParamAndReload(get_param_input_id, get_param){

	// generate different regular expresions (depending on ampersands or question marks in url
	var entries_reg_exp_mid_ampersand = new RegExp("[&]"+get_param+"[=][^&]*[&]{1}","gi");
	var entries_reg_exp_end_ampersand = new RegExp("[&]"+get_param+"[=].*$","gi");
	var entries_reg_exp_mid_questionmark = new RegExp("[?]"+get_param+"[=][^&]*[&]{1}","gi");
	var entries_reg_exp_end_questionmark = new RegExp("[?]"+get_param+"[=].*$","gi");

	// generate different replacemant strings
	var new_entries_string_mid_ampersand = "&"+get_param+"="+document.getElementById(get_param_input_id).value+"&";
	var new_entries_string_end_ampersand = "&"+get_param+"="+document.getElementById(get_param_input_id).value;
	var new_entries_string_mid_questionmark = "?"+get_param+"="+document.getElementById(get_param_input_id).value+"&";
	var new_entries_string_end_questionmark = "?"+get_param+"="+document.getElementById(get_param_input_id).value;
	var new_entries_string_end = get_param+"="+document.getElementById(get_param_input_id).value;

	// check on occureance
	// string in middle with ampersand at start
	if(location.href.match(entries_reg_exp_mid_ampersand)){
		location.replace(location.href.replace(entries_reg_exp_mid_ampersand,new_entries_string_mid_ampersand)  );
	// string at end with ampersand at start
	}else if(location.href.match(entries_reg_exp_end_ampersand)){
		location.replace(location.href.replace(entries_reg_exp_end_ampersand,new_entries_string_end_ampersand)  );
	// string in middle with questionmark at start
	}else if(location.href.match(entries_reg_exp_mid_questionmark)){
		location.replace(location.href.replace(entries_reg_exp_mid_questionmark,new_entries_string_mid_questionmark)  );
	// string at end with questionmark at start
	}else if(location.href.match(entries_reg_exp_end_questionmark)){
		location.replace(location.href.replace(entries_reg_exp_end_questionmark,new_entries_string_end_questionmark)  );
	// string not found -> add to current url
	}else{

		if( (location.href.charAt(location.href.length-1)=='&') || (location.href.charAt(location.href.length-1)=='?') ){
			new_string_end = new_entries_string_end;
		}else{
			if(location.href.match("[?]")){
				new_string_end = '&' + new_entries_string_end;
			}else{
				new_string_end = '?' + new_entries_string_end;
			}
		}
		location.replace(location.href+new_string_end);
	}
}

////////////////////////////////////////////////////////////////////////////////
// replaceGetParamByValueAndReload
//
// replaces parameter by given fixed value
//
// value: value to replace
// get_param: name of parameter to replace or set
////////////////////////////////////////////////////////////////////////////////
function replaceGetParamByValueAndReload(value, get_param){

	// generate different regular expresions (depending on ampersands or question marks in url
	var entries_reg_exp_mid_ampersand = new RegExp("[&]"+get_param+"[=][^&]*[&]{1}","gi");
	var entries_reg_exp_end_ampersand = new RegExp("[&]"+get_param+"[=].*$","gi");
	var entries_reg_exp_mid_questionmark = new RegExp("[?]"+get_param+"[=][^&]*[&]{1}","gi");
	var entries_reg_exp_end_questionmark = new RegExp("[?]"+get_param+"[=].*$","gi");

	// generate different replacemant strings
	var new_entries_string_mid_ampersand = "&"+get_param+"="+value+"&";
	var new_entries_string_end_ampersand = "&"+get_param+"="+value;
	var new_entries_string_mid_questionmark = "?"+get_param+"="+value+"&";
	var new_entries_string_end_questionmark = "?"+get_param+"="+value;
	var new_entries_string_end = get_param+"="+value;

	// check on occureance
	// string in middle with ampersand at start
	if(location.href.match(entries_reg_exp_mid_ampersand)){
		location.replace(location.href.replace(entries_reg_exp_mid_ampersand,new_entries_string_mid_ampersand)  );
	// string at end with ampersand at start
	}else if(location.href.match(entries_reg_exp_end_ampersand)){
		location.replace(location.href.replace(entries_reg_exp_end_ampersand,new_entries_string_end_ampersand)  );
	// string in middle with questionmark at start
	}else if(location.href.match(entries_reg_exp_mid_questionmark)){
		location.replace(location.href.replace(entries_reg_exp_mid_questionmark,new_entries_string_mid_questionmark)  );
	// string at end with questionmark at start
	}else if(location.href.match(entries_reg_exp_end_questionmark)){
		location.replace(location.href.replace(entries_reg_exp_end_questionmark,new_entries_string_end_questionmark)  );
	// string not found -> add to current url
	}else{

		if( (location.href.charAt(location.href.length-1)=='&') || (location.href.charAt(location.href.length-1)=='?') ){
			new_string_end = new_entries_string_end;
		}else{
			if(location.href.match("[?]")){
				new_string_end = '&' + new_entries_string_end;
			}else{
				new_string_end = '?' + new_entries_string_end;
			}
		}
		location.replace(location.href+new_string_end);
	}
}

////////////////////////////////////////////////////////////////////////////////
// replaceGetParamOfCheckboxAndReload
//
// extracts given get parameter of checkbox and replaces 1 or 0 or adds in url -> reload
//
// get_param_input_id: id of checkbox html element to checked status
// get_param: name of parameter to replace or set
////////////////////////////////////////////////////////////////////////////////
function replaceGetParamOfCheckboxAndReload(get_param_input_id, get_param){

	// generate different regular expresions (depending on ampersands or question marks in url
	var entries_reg_exp_mid_ampersand = new RegExp("[&]"+get_param+"[=][^&]*[&]{1}","gi");
	var entries_reg_exp_end_ampersand = new RegExp("[&]"+get_param+"[=].*$","gi");
	var entries_reg_exp_mid_questionmark = new RegExp("[?]"+get_param+"[=][^&]*[&]{1}","gi");
	var entries_reg_exp_end_questionmark = new RegExp("[?]"+get_param+"[=].*$","gi");

	if(document.getElementById(get_param_input_id).checked == 1)
		checked_value = 1;
	if(document.getElementById(get_param_input_id).checked == 0)
		checked_value = 0;

	// generate different replacemant strings
	var new_entries_string_mid_ampersand = "&"+get_param+"="+checked_value+"&";
	var new_entries_string_end_ampersand = "&"+get_param+"="+checked_value;
	var new_entries_string_mid_questionmark = "?"+get_param+"="+checked_value+"&";
	var new_entries_string_end_questionmark = "?"+get_param+"="+checked_value;
	var new_entries_string_end = get_param+"="+checked_value;

	// check on occureance
	// string in middle with ampersand at start
	if(location.href.match(entries_reg_exp_mid_ampersand)){
		location.replace(location.href.replace(entries_reg_exp_mid_ampersand,new_entries_string_mid_ampersand)  );
	// string at end with ampersand at start
	}else if(location.href.match(entries_reg_exp_end_ampersand)){
		location.replace(location.href.replace(entries_reg_exp_end_ampersand,new_entries_string_end_ampersand)  );
	// string in middle with questionmark at start
	}else if(location.href.match(entries_reg_exp_mid_questionmark)){
		location.replace(location.href.replace(entries_reg_exp_mid_questionmark,new_entries_string_mid_questionmark)  );
	// string at end with questionmark at start
	}else if(location.href.match(entries_reg_exp_end_questionmark)){
		location.replace(location.href.replace(entries_reg_exp_end_questionmark,new_entries_string_end_questionmark)  );
	// string not found -> add to current url
	}else{

		if( (location.href.charAt(location.href.length-1)=='&') || (location.href.charAt(location.href.length-1)=='?') ){
			new_string_end = new_entries_string_end;
		}else{
			if(location.href.match("[?]")){
				new_string_end = '&' + new_entries_string_end;
			}else{
				new_string_end = '?' + new_entries_string_end;
			}
		}
		location.replace(location.href+new_string_end);
	}
}