/* Utility functions */
function junction() {
  return "X4";
}

function junction_login() {
  return "X7";
}

/* Hex encoding af url strenge */
function hexEncode(e) {
  var keyStr = "ABCDEFGHIJKLMNOPQR";
  var i=0;
  var ret = "";
  var c, u, l = 0;
  do {
    c = e.charCodeAt(i);
    u = ((c & 0xf0) >> 4);
    l = (c & 0x0f);
    ret = ret + keyStr.charAt(u);ret = ret + keyStr.charAt(l);i++;
  } while(i < e.length)
  return ret;
}


function WM_readCookie(name) {
    if(document.cookie == '') { // there's no cookie, so go no further
	return false; 
    } else { // there is a cookie
	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
	var NN2Hack = firstChar + name.length;
	if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
	    firstChar += name.length + 1; // skip 'name' and '='
	    lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
	    if(lastChar == -1) lastChar = theBigCookie.length;
	    return unescape(theBigCookie.substring(firstChar, lastChar));
	} else { // If there was no cookie of that name, return false.
	    return false;
	}
    }	
} // WM_readCookie

function WM_killCookie(name) {
  var theValue = WM_readCookie(name); // We need the value to kill the cookie
  if(theValue) {
      document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT'; 
      // set an already-expired cookie
  }
} // WM_killCookie



function redirectError(errorCode, errorText) {
  window.location = "/" + junction() + "/LogonTam/WebSEALError.jsp?errorCode="+escape(errorCode)+"&ERROR_TEXT=" + escape(errorText);
}

function redirect_logout() {
  WM_killCookie("A000816");
  // hack to kill portal session
  deleteCookiesForJunction("X5");
  
  if (document.location.search.indexOf("closeWin") != -1) {
     window.close();
  } else {
	document.location = "/" + junction() + "/wrr/FlsLogon/FlsLogoffServlet?loggetud=true";
  }
}



/*bruges af alle html siderne under /html/C/ */
function redirect() {
  window.location = "/" + junction() + "/";
}

/*bruges af /errors/C/132120c8.html*/
function redirect_login(backUrl, errMsg, errCode) {

  redirect_login_and_goto(document.location.href, backUrl, errMsg, errCode);
}

/* used by redirect_login, login.html, loign_rep.html and some pages in the pd_portal brand */
function redirect_login_and_goto(targetLocation, backUrl, errMsg, errCode) {
  // hack to kill portal session
  deleteCookiesForJunction("X5");
 
  var params = "";
  if (targetLocation != null && targetLocation != "")
  	params += "from="+hexEncode(targetLocation);
  if (backUrl != null && backUrl != '') {
    params += "&backActionUrl=" + escape(backUrl);
  }
  if (errMsg != null && errMsg != '') {
    params += "&error=" + escape(errMsg);
  }
  if (errCode != null && errCode != '') {
    params += "&errorCode=" + escape(errCode);
  }
  window.location = "/" + junction_login() + "/adgang/pc?cmd=entry&" + params;
}

/*bruges af /errors/C/38cf013d.html*/
function redirect_protected_startpage() {
/* Start siden kendes ikke så vi må redirecte til roden. */
  window.location = "https://" + window.location.host + "/";
}

/*bruges af /errors/C/38cf0428.html*/
function redirect_file_not_found() {
  window.location = "/file_not_found.html"
}

/*bruges af /errors/C/38cf08cc.html*/
function redirect_time_restricted_access() {
  window.location =  "/file_not_found.html"
}

/*bruges af /errors/C/38cf0427.html*/
function https_url(linkTekst){
  var https_url = new String(window.location);
  if(https_url.indexOf("http:")==0){
   https_url = "https:" + https_url.substring(5,https_url.length);
  }
  url = "<a href=" + https_url + " class=tabellink_u_indryk>" + linkTekst + "</A>";
  document.write(url);
}

function deleteCookiesForJunction(jct) {
  var all_cookies = document.cookie.split( ';' );
  for ( i = 0; i < all_cookies.length; i++ ) {
    var a_cookie = all_cookies[i];
    // now we'll split apart each name=value pair
    var a_temp_cookie = a_cookie.split( '=' );
			
    // and trim left/right whitespace while we're at it
    var	cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
    if(cookie_name.match("AMWEBJCT!%2F" +jct+"!")) {
      // delete cookie
      document.cookie = cookie_name + "=" + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
  }
}

