//do NOT call this routing like so: window.onload = changeLoginLinks;

function changeLoginLinks() {
	try {
		var ll = document.getElementById("loginLinks");
		ll.innerHTML = '<a href="' + loginLink() + '">' + loginLabel() + '</a>';
		var rl = document.getElementById("regisLinks");
		rl.innerHTML = '<a href="' + regLink() + '">' + regLabel() + '</a>';
	}
	catch (e){}
}
function getBasePath(){
	var basePath = basePath = document.getElementById("dbpath").innerHTML;
	return "/" + leftBack( basePath, "/") + "/";
}

function QSearch(){
	try {
		basePath = getBasePath();
		var fld = document.getElementById( "srchFld" );
		if ( trim(fld.value) != "") {
			setCookie( "autoSearch", fld.value, 1, "/");
			window.location = basePath + "wappJPO.nsf/JS?OpenForm";
		}
	} 
	catch (e) {}
}

function isLoggedIn() {
	return (document.getElementById("authName").innerHTML != "Anonymous");
}

function regLink(){
	basePath = getBasePath();
	URL = basePath + "wappuib.nsf/pl?openform";
	if (!isLoggedIn()) URL = basePath + "wappuib.nsf/newacc?openform";
	return URL;	
}

function regLabel(){
	LABEL = "Dashboard";
	if (!isLoggedIn()) LABEL = "Register";
	return LABEL;	
}

function loginLink(){
	basePath = getBasePath();
	URL = basePath + "wappUIB.nsf?Logout&Redirectto=/";
	if (!isLoggedIn()) URL = basePath + "wappuib.nsf/pl?openform&login";
	return URL;	
}

function loginLabel(){
	LABEL = "Logout";
	if (!isLoggedIn()) LABEL = "Login";
	return LABEL;
}

/* =====================================  Auto Login ========================== */
function userLogin(uname, upass, gotoURL ){
	basePath = getBasePath();	
	basePath = basePath + "wappuib.nsf/";
	var logReq = createXHTMLHttpRequest() ;
	var poststring = "RedirectTo=" + escape(basePath +'($loginok)?openagent') + "&Username=" + uname + "&password=" + upass;
	logReq.open("POST", "/names.nsf?Login" , false);
	logReq.send(poststring);

	if (logReq.status == 200){
		if (logReq.responseText == "***IAMIN***\n") {
			if (gotoURL!="") window.location.href = gotoURL;
			return true;
		}
	} else
		alert("Cannot login you in, sorry.  Please check seesion based authentication for this domain.");
	
	return false;
}

function createXHTMLHttpRequest() {
	try { return new ActiveXObject("Msxml2.XMLHTTP") ; } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP") ; } catch (e) {}
	try { return new XMLHttpRequest() ; } catch (e) {}
	alert("XMLHttpRequest is not supported on this browser!");
	return null;
}
