







/**
 * Removes the parameter 'parameterName' and its value if present
 * Will remove more parameters and its value if he parameterName occurs more than once
 * param: parameterName, the parameter to remove from string
 * param: search, the actual string to be parsed
 * param: start, index for the actual paramter 
 * return: new string with parameters removed if any	
 */
function removeParameter(parameterName, search, start)
{
	var idx = search.indexOf(parameterName, start);
	if (idx > 0) {
		var charBefore = search.charAt(idx-1);
		if (charBefore == '?' || charBefore == '&') {
			if (search.length >= idx+parameterName.length+1) {
				if (search.charAt(idx+parameterName.length) == '=') {
					var nextValueStart = search.indexOf('&', idx+parameterName.length);
					if (nextValueStart < 0) {
						nextValueStart = search.length;
					}
				
					var result = search.substring(0, idx-1) + search.substring(nextValueStart);
										
					if (result.length < 1) {
						return '';
					}
					
					if (result.charAt(0) == '&') {
						result = '?' + result.substring(1);
					}
					
					return removeParameter(parameterName, result, idx);
				}
			}
			
			return removeParameter(parameterName, search, idx+parameterName.length+1);
		}
	}
	
	return search;
}

/**
 * change language based on a given parameter: pri or sec
 * Applied to filter
 * return: change search query for the url for the language to change. 
 */
function changeLanguage()
{
	var locale = "pri";
	var search = document.location.search;			
	var LANGUAGE_PARAM = 'AnonymousLoginFilter_language';
	search = removeParameter(LANGUAGE_PARAM, search, 1);	
	document.location.search = (search.indexOf('?') > -1 ? search + '&' : '?') + LANGUAGE_PARAM + '=' + (locale == 'pri' ? 'sec' : 'pri');	
}

/**
 * Replaces a parameters value with a new value
 * param: search normally the result of document.location.search
 * param: param, to change the value of
 * param: value, the actual value to change 
 *
 */
function replaceParameter(search, param, value)
{
	search = removeParameter(param, search, 1);
	return (search.indexOf('?') > -1 ? search + '&' : '?') + param + '=' + value;
}

/**
 * Fetches the value of the paramter from the given url
 * param: url, the current url to get the parameter value from 
 * param: parameter, the parameter to return the value from 
 */ 
function getParameter(url, parameter) 
{	
		var arrParams = url.split("?");
		var str = "no value";
		if (arrParams[1]) {	 
			var arrURLParams = arrParams[1].split("&");		
			var arrParamNames = new Array(arrURLParams.length);
			var arrParamValues = new Array(arrURLParams.length);		
			var i = 0;
			for (i=0; i<arrURLParams.length; i++) {
				var sParam =  arrURLParams[i].split("=");
				arrParamNames[i] = sParam[0];
				if (arrParamNames[i] == 	parameter) {
					str = arrParamValues[i];
				}
			}
		}								
		return str;	
	
}   
/**
 * Validates the given String for an inputfield. No ? and * is allowed in search
 * param: form, the current form
 * param: arrayId, the placement in the form. Responding to array placement. Could also be name
 * return: return false if first char == ? || * otherwise submit form
 */
function validateSearch(form, arrayId)
{				
	var s = form.elements[arrayId].value;
	while ((s.substring(0,1) == ' ')){
	 	s = s.substring(1, s.length);
	}
	if (form.vbn && form.clipYearActive == true) {		
		var reg = new RegExp("(\\d\\d)[.](\\d\\d)[.](\\d{4})");		
		var r1 = reg.exec(form.clippingStart.value);
		var r2 = reg.exec(form.clippingEnd.value);		
		if ((form.clippingStart.value != null && r1 == null) || (form.clippingEnd.value && r2 == null)) {
			alert('???da_DK.search.error.javascript.format???');
			return false;
		}
	}					
	if( s == "*" || s == "?" || s.substring(0,1) == "*" || s.substring(0,1) == "?"){
		alert('Søgningen må ikke starte med ? eller *');
        return false;
	} else {			
		form.hasSearched.value = true;	
		form.submit();
	}			
}

/**
 * Simple function to enable focus in an input field
 * param: element, the element to place focus in
 */
function placeFocus(element)
{  	
 	element.focus();
}
 
 /**
  * Checks browser for dhtml
  *
  */
function getElement(aID)
{ 
   return (document.getElementById) ? document.getElementById(aID)
                                      : document.all[aID];
}     
 