//****************************************************************
//
//  FUNCTION:	nodelink
//  PARAMS:		nodeId of content server page
//				
//  RETURNS:	none
//  PURPOSE:	Parse the nodeId into a URL to link to.
//
//		Called from top havigation.
//
//****************************************************************
function nodelink(nodeId) {
	var newUrl = "";
	newUrl += document.location.protocol + "//";
	newUrl += document.location.host;
	newUrl += "/ws/cs/idcplg?IdcService=SS_GET_PAGE&";
	newUrl += "nodeId=" + nodeId;

	// Navigate to the new url
	window.location.href = newUrl;
}

//****************************************************************
//
//  FUNCTION:	getCookie
//  PARAMS:		name of cookie to parse
//				
//  RETURNS:	string value containing URL to redirect to.
//  PURPOSE:	Parse the value of the cookie into a
//				valid URL.
//
//		Called from redirect to return parsed value of cookie
//
//****************************************************************
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf(prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

//****************************************************************
//
//  FUNCTION:	redirect
//  PARAMS:		
//				
//  RETURNS:	void
//  PURPOSE:	Redirect user to value in cookie if it exists or
//		        to welcome screen.
//
//		Called from login_success.html, typicaly after user
//      has successfully logged into WebSEAL.
//
//****************************************************************
function redirectFromCookie() {
	var target = getCookie("IdcTarget");
	
	document.cookie = "IdcTarget" + "=" + target + "; expires=Thu, 01-Jan-1970 00:00:01 GMT";

	if (target != null)
	{
		window.location.replace(target);
	} else {
		var newUrl = "";
		
		newUrl += document.location.protocol + "//";
		newUrl += document.location.host;
		newUrl += "/ws/cs/idcplg?IdcService=SS_GET_PAGE&nodeId=6032";	
		
		window.location.replace(newUrl);
	}
}

//****************************************************************
//
//  FUNCTION:	redirectToLogout
//  PARAMS:		
//						
//  RETURNS:	void
//  PURPOSE:	Redirects to the 'you have signed off' page
//		
//
//		Called from pkmslogout, typically after user clicks 
//		Sign Off
//
//****************************************************************
function redirectToLogout() {

	//clear out the IntradocLoginState cookie so Content Server does not think it needs user credentials
	document.cookie = "AMWEBJCT!%2Fws!IntradocLoginState=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
		
	var newUrl = "";
	
	newUrl += "https://";
	newUrl += document.location.host;
	newUrl += "/innapps/fedapp/healthinternet/invalidate.jsp?replace=%2Fws%2Fcs%2Fidcplg%3FIdcService%3DSS_GET_PAGE%26nodeId%3D6123";

	window.location.replace(newUrl);
}

//****************************************************************
//
//  FUNCTION:	forgotPassword
//  PARAMS:		none
//				
//  RETURNS:	void
//  PURPOSE:	Redirects to the selfcare forgot password page
//		
//		
//		Called from login.html
//
//****************************************************************
function forgotPassword() {
	var newUrl = "";
	
	newUrl += document.location.protocol + "//";
	newUrl += document.location.host;
	newUrl += "/intapps/fedapp/selfcare/ForgotPasswordLinkRequest.do?userId="+document.loginForm.username.value;

	if ( document.loginForm.username.value.length > 0 ) {
		window.location.href = newUrl;
	} else {
		alert( "Please enter your user id and press the \"Forgot your password?\" link" );
	}
}

//****************************************************************
//
//  FUNCTION:	registerNewUser
//  PARAMS:		none 
//				
//  RETURNS:	void
//  PURPOSE:	Redirect to selfcare enroll index page
//
//		Called from login.html
//
//****************************************************************
function registerNewUser() {
	var newUrl = "";
	
	newUrl += document.location.protocol + "//";
	newUrl += document.location.host;
	newUrl += "/intapps/fedapp/selfcare/index.html";


	window.location.href = newUrl;
}

//****************************************************************
//
//  FUNCTION:	completeRegisterNow
//  PARAMS:		none 
//				
//  RETURNS:	void
//  PURPOSE:	Redirect to selfcare compelete enrollment
//
//		Called from login.html
//
//****************************************************************
function completeRegisterNow() {
	var newUrl = "";
	
	newUrl += document.location.protocol + "//";
	newUrl += document.location.host;
	newUrl += "/intapps/fedapp/selfcare/ConfirmEnrollmentNowLinkRequest.do";


	window.location.href = newUrl;
}

//****************************************************************
//								
//  FUNCTION:	openHelpWindow					
//  PARAMS:		targetUrl - relative url of html page to open
//				width - width of html page to open
//				height - height of html page to open					
//  RETURNS:	void						
//  PURPOSE:	Opens the target URL window with the specified
//				height and width.
//		
//				Typically called to open a help_?.html file.
//									
//****************************************************************
function openHelpWindow(targetUrl,width,height){
	windowName=window.open(targetUrl,"helpwindow","width="+width+",height="+height+",toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1",true);
	windowName.focus();
}

//****************************************************************
//
//  FUNCTION:	setFocus
//  PARAMS:	tabNumber - parameter to indicate the tabIndex to look 
//						for when setting the focus.  Nullable
//  RETURNS:	void
//  PURPOSE:	Sets the focus to the first document element
//		whose tabIndex value is set to the passed
//		parameter.  No focus is set by this function if null 
//		parameter.
//
//		Normally called as an "onLoad" event handler.
//
//****************************************************************
function setFocus(tabNumber) {

	if (tabNumber != null) {
 		var elementList;
 	
 		// Loop through all forms on the page
 		for (var i=0; i < document.forms.length; i++) {
 			elementList = document.forms[i].elements;
 		
 			// Loop through all elements in the current form
 			for (var j=0; j < elementList.length; j++) {
 				if (elementList[j].tabIndex == tabNumber) {
 					elementList[j].focus();
 					break;
 				}
 			}
 		}
  	}
}

//****************************************************************
//
//  FUNCTION:	redirectToLogon
//  PARAMS:		none 
//				
//  RETURNS:	void
//  PURPOSE:	Redirect to logon
//
//		Called from action_completed page
//
//****************************************************************
function redirectToLogon() {
	var newUrl = "";
	
	newUrl += "https://";
	newUrl += document.location.host;
	newUrl += "/pkmslogin";


	document.location = newUrl;
}

//****************************************************************
//
//  FUNCTION:	redirectToMyAccount
//  PARAMS:		none 
//				
//  RETURNS:	void
//  PURPOSE:	Redirect to logon
//
//		Called from action_completed page
//
//****************************************************************
function redirectToMyAccount() {
	nodelink(6032);
}
