
/**
 * If there is a form on the page, this functions
 * makes sure that the first inputfield always recieves focus.
 */
function setGlobalFocus() {

	<!-- Sets focus to the first available input field always -->
	if(document.forms) {
		for(var formCnt = 0; formCnt < document.forms.length; formCnt++) {
			var thisForm = document.forms[formCnt];
			
			for(var cnt = 0;cnt < thisForm.elements.length; cnt++) {
			
				if(thisForm.elements[cnt]) {
					if(thisForm.elements[cnt].type != "hidden") {
						thisForm.elements[cnt].focus();
						break;
					}
				}
			}
		}
	}
}

/**
 * This function logs in the the user by adding a parameter the forces the login to be shown.
 */
function login() {

	var theUrl = location.href;

	//Switch protocol if not running SSL.
	if(theUrl.indexOf("https://") == -1 && theUrl.indexOf("http://") != -1) {

		var hostnameStart = theUrl.indexOf("//") + 2;
		theUrl = "https://" + theUrl.substring(hostnameStart);
	}

	//Add the parameters needed for the login to trigger correctly.
	theUrl = addURLParameter(theUrl, "dtlsecure", "true");
	theUrl = addURLParameter(theUrl, "site", "dtl");

	location.href = theUrl;
}

/**
 * This function calls a secure page on the website, if the current
 * url isnt secure, it is made so.
 */
function callSecureGetCodanPage(pageId) {

	var theUrl = "https://" + location.host + "/web/beregn/GetCodanPage";
	theUrl = addURLParameter(theUrl, "site", "dtl");
	theUrl = addURLParameter(theUrl, "PageID", pageId);
	location.href = theUrl;
}

/**
 * This function adds a parameter=value to the url.
 * If the keyword is already part of the given url, it is removed
 * before readded.
 */
function addURLParameter(theUrl, parameter, value) {

	//If the url already contains our parameter, strip this here.
	if(theUrl.indexOf(parameter + "=") != -1) {
	
		//figure out the start and end of the parameter to remove.
		var startPos = theUrl.indexOf(parameter);
		var endPos = theUrl.indexOf('&', startPos + 1);
		if(endPos == -1) {
		
			endPos = theUrl.length;
		}
		
		//Preserve the ? char in the url, if the parameter is the first after the base url.
		if(theUrl.charAt(startPos - 1) == '?' && endPos != theUrl.length) {
		
			theUrl = theUrl.substring(0, endPos) + '?' + theUrl.substring(endPos + 1);
		}

		//Remove the parameter.
		theUrl = theUrl.substring(0, startPos - 1) + theUrl.substring(endPos);
	}

	//Append the parameter that will trigger the login / logout.
	if(theUrl.indexOf("?") == -1) {

		theUrl += "?" + parameter + "=" + value;
	}
	else {

		theUrl += "&" + parameter + "=" + value;
	}

	return theUrl;
}

/**
 * Launches a popup window.
 */
	function openPopup(url, width, height) {
		var xPos= 100;
		var yPos= 100;
		if(window.screen) {
			xPos= (screen.width-width)/2;
			yPos= (screen.height-height)/2;
		}
		features= "status=0,width="+width+",left="+xPos+",height="+height+",top="+yPos+",scrollbars=1,resizable=1,titlebar=1,alwaysRaised=1,modal=1";
		msgWindow= window.open(url,"msgWindow",features);
		msgWindow.focus();
	}
