/* Redirect that removes spaces in url */
function gotoHref (src) {
	url = src.replace(/ /, "%20");
	document.location.href=url;
}

/*function replaceBackslash (src) {
	url = src.replace(/\/, "/")
	replaceBackslash = url;
}*/

/* Popup with scrolling */
function popUp (src) {
	browser = navigator.appName;
	version = navigator.appVersion;
	popupLoc = src;
	popupName = "popup";
	popupStr = "location=no,menubar=no,status=no,scrollbars=yes,resizable=no,toolbar=no,directories=no";
	if (browser == "Microsoft Internet Explorer" || (browser == "Netscape" && version >= '5' )) {
		popupStr = popupStr+",top=0,left=0";
	}
	window.open(popupLoc, popupName, popupStr);
}

/* Popup with toolbar and scrolling */
function popUpScroll (src) {
	browser = navigator.appName;
	version = navigator.appVersion;
	popupLoc = src;
	popupName = "popup";
	popupStr = "location=no,menubar=no,status=no,scrollbars=yes,resizable=no,toolbar=yes,directories=no";
	if (browser == "Microsoft Internet Explorer" || (browser == "Netscape" && version >= '5' )) {
		popupStr = popupStr+",top=0,left=0";
	}
	window.open(popupLoc, popupName, popupStr);
}

/* Checks if given date is a number, if not input field is cleared and alert is showed */
function kolla(date, typ) {
	var number = false;

	if (parseInt(date)) {
		number = true;
	}
	else if (date=="") {
		number = true;
	}
	else if (date==0) {
		number = true;
	}
	else {
		number=false;
		alert("Du måste skriva årtalet med siffror.");
		if (typ=="dat_min") {
			document.sok.dat_min.value = "";
		}
		else if (typ=="dat_max") {
			document.sok.dat_max.value = "";
		}
	}
}

/* Validates all form elements, ie checks if they are empty, but skips a number of elements at the end of form (given by 'minus').
** You need different validations for different element, so far select-one, input text and hidden are implemented.
*/
function validate(theForm, minus) {
	for (i = 0; i < theForm.length-minus; i++) {
		if (theForm.elements[i].type == 'select-one') {
			if (theForm.elements[i].options[theForm.elements[i].selectedIndex].value!='') {
    		return true;
	  	}
		}
	  else if (theForm.elements[i].type == 'text' || theForm.elements[i].type == 'hidden') {
			if (theForm.elements[i].value!='') {
    		return true;
	  	}
		}
	}
	alert('Du måste fylla i minst ett sökkriterium.');
	return false;
}

/* Gets radio button value */
function getRadioValue (radioArray) {
	for (i=0; radioArray.length; i++) {
		if (radioArray[i].checked) {
			return radioArray[i].value;
		}
	}
	return '';
}

/* Adds arguments to url query */
function addToQuery (strName, strValue) {
	url = document.location.href;
	query = window.location.search;
	
	if (query != '') {
		strSearch = strName+'=';
		testIndex = query.search(strSearch);
		if (testIndex != -1) {
			intStartIndex = testIndex + strSearch.length;
			intEndIndex = query.indexOf('&', intStartIndex);
			if (intEndIndex == -1) {
				intEndIndex = query.length;
			}
			strSub = query.substring(intStartIndex, intEndIndex);
			query = query.replace(strSub, strValue);
		}
		else {
			query = query+'&'+strName+'='+strValue;
		}
	}
	else {
		query = '?'+strName+'='+strValue;
	}
	
	gotoHref (url);
	window.location.search = query;
}
