/***********************************************************************
 * loginForm.js
 * Copyright 2008 CCH, a Wolters Kluwer business
 ***********************************************************************/
function siLoginForm( )
{
	if (window[this.winId])
	{
		return window[this.winId];
	}
	this.username = window.siAuthUser;
	window[this.winId] = this;
}
siLoginForm.prototype.winId = "siLoginForm1938";
siLoginForm.prototype.userNameId = "siLoginUserName";
siLoginForm.prototype.passwordId = "siLoginPassword";
siLoginForm.prototype.rememberId = "siLoginRemember";
siLoginForm.prototype.FormAttachTarget = "siGradientRight";
siLoginForm.prototype.wkEmailPattern = new RegExp("[^@]+@wolterskluwer\\.com", "i");
siLoginForm.prototype.initialize = function( )
{
	this.createForm();
	if (window.siAuthUser != "")
	{
		this.statusLoggedIn();
	}
};
siLoginForm.prototype.loginSuccessResponse = function(rData)
{
	if ($("success", rData).length > 0)
	{
		var success = $("success", rData).text();
		if (success == "true")
		{
			location.reload();
		}
		else
		{
			$("#siLoginWaitDiv").hide();
			$("#siLoginFormDiv").show();
			$("#"+this.passwordId).val("");
			if (!this.wkEmailPattern.test($("#"+this.userNameId).val()))
			{
				alert("You could not be logged on with those credentials.\nPlease try again.");
			}
			else
			{
				alert("Please use your NA password to sign in.");
			}
		}
	}
	else if ($("redirect", rData).length > 0)
	{
		var url = unescape($("redirect", rData).text());
		location.href = url;
	}
	else
	{
		alert("Login XML value not understood");
		location.href = "/login/login.aspx";
	}
};
siLoginForm.prototype.loginErrorResponse = function(rData)
{
	alert("An error occurred trying to log in.  Please try again later.");
	$("#siLoginWaitDiv").hide("normal");
};
siLoginForm.prototype.submitForm = function( )
{
	var userName = document.getElementById(this.userNameId).value;
	var password = document.getElementById(this.passwordId).value;
	var remember = document.getElementById(this.rememberId).checked;
	
	this.username = userName; // In case the login was successful.
		
	var errorList = new Array();
	if (userName == "") errorList.push("Please specify an ID");
	if (password == "") errorList.push("Please specify a password");
	
	if (errorList.length > 0)
	{
		alert("Please correct the following:\n   *" + errorList.join("\n   *"));
		return;
	}
	
	$.ajax({
		url: "/login/loginHandler.aspx",
		dataType: "xml",
		type: "POST",
		success: new Function("rData", "window[\"" + this.winId + "\"].loginSuccessResponse(rData);"),
		error: new Function("rData", "window[\"" + this.winId + "\"].loginErrorResponse(rData);"),
		data: {username: userName, password: password, rememberme: (remember?"true":"false")}
	});
	
	$("#siLoginFormDiv").hide();
	$("#siLoginWaitDiv").show();
};
siLoginForm.prototype.createForm = function( )
{
	var formDiv = $("<div id=\"siLoginFormDiv\"></div>").addClass("siLoginFormDiv");
	var loginForm = document.createElement("form");
	loginForm.setAttribute("id", "siLoginInfoForm");
	loginForm.setAttribute("method", "GET");
	loginForm.setAttribute("action", "about:blank");
	$(loginForm).appendTo(formDiv);
	var tbl = document.createElement("table");
	loginForm.appendChild(tbl);
	var tbody = document.createElement("tbody");
	tbl.appendChild(tbody);
	
	tbody.appendChild(this.createUserNameRow());
	tbody.appendChild(this.createPasswordRow());
	tbody.appendChild(this.createRememberRow());
	
	formDiv.appendTo($("#" + this.FormAttachTarget));
	
	formDiv.hide();
	
	//$("#" + this.FormAttachTarget + " a").click(function(event){event.preventDefault();$("#siLoginFormDiv").show("normal");});
	$("#siLoginSubmit").click(new Function("event", "window[\"" + this.winId + "\"].submitForm();event.preventDefault();"));
	this.statusLoggedOut();
	
	var waitDiv = $("<div id=\"siLoginWaitDiv\">Logging in...</div>").addClass("siLoginFormDiv");
	waitDiv.appendTo($("#" + this.FormAttachTarget));
	waitDiv.hide();
	this.getEmailGuid();
	$("#" + this.userNameId).change(new Function("eventObject", "window[\"" + this.winId + "\"].userIdChangeEvent(eventObject);"));
};
siLoginForm.prototype.userIdChangeEvent = function (eventObject)
{
	if (this.wkEmailPattern.test($("#"+this.userNameId).val()))
	{
		$("#" + this.rememberId).attr("checked", true);
	}
}
siLoginForm.prototype.createUserNameRow = function( )
{
	var rVal = document.createElement("tr");
	var cell = document.createElement("td");
	var lbl = document.createElement("label");
	lbl.setAttribute("for", this.userNameId);
	lbl.className = "siLoginLabel";
	lbl.appendChild(document.createTextNode("E-Mail:"));
	cell.appendChild(lbl);
	rVal.appendChild(cell);
	
	cell = document.createElement("td");
	rVal.appendChild(cell);
	
	var txt = document.createElement("input");
	txt.setAttribute("name", this.userNameId);
	txt.setAttribute("type", "text");
	txt.setAttribute("id", this.userNameId);
	txt.setAttribute("size", "30");
	cell.appendChild(txt);
	
	rVal.appendChild(document.createElement("td"));
	
	return rVal;
};
siLoginForm.prototype.createPasswordRow = function( )
{
	var rVal = document.createElement("tr");
	var cell = document.createElement("td");
	var lbl = document.createElement("label");
	lbl.setAttribute("for", this.passwordId);
	lbl.className = "siLoginLabel";
	lbl.appendChild(document.createTextNode("Password:"));
	cell.appendChild(lbl);
	rVal.appendChild(cell);
	
	cell = document.createElement("td");
	rVal.appendChild(cell);
	
	var txt = document.createElement("input");
	txt.setAttribute("name", this.passwordId);
	txt.setAttribute("type", "password");
	txt.setAttribute("id", this.passwordId);
	txt.setAttribute("size", "30");
	txt.setAttribute("autocomplete", "off");
	cell.appendChild(txt);
	
	cell = document.createElement("td");
	var btn = document.createElement("input");
	btn.setAttribute("type", "image");
	btn.setAttribute("id", "siLoginSubmit");
	btn.setAttribute("name", "siLoginSubmit");
	btn.setAttribute("src", "/images/SignInBtn.gif");
	
	cell.appendChild(btn);
	
	rVal.appendChild(cell);
	
	return rVal;
};
siLoginForm.prototype.createRememberRow = function( )
{
	// What's going on here?  Create the row and a
	// single cell with colspan 2.  Then add the
	// checkbox, and then the label.  It's just
	// all jumbled because it's a cut-and-paste
	// job of the previous two functions.
	var rVal = document.createElement("tr");
	rVal.appendChild(document.createElement("td"));
	var cell = document.createElement("td");
	cell.setAttribute("align", "left");
	var txt = document.createElement("input");
	txt.setAttribute("name", this.rememberId);
	txt.setAttribute("type", "checkbox");
	txt.setAttribute("id", this.rememberId);
	cell.appendChild(txt);
	var lbl = document.createElement("label");
	lbl.setAttribute("for", this.rememberId);
	lbl.className = "siLoginLabel";
	lbl.appendChild(document.createTextNode("Keep Me Logged In"));
	cell.appendChild(lbl);
	rVal.appendChild(cell);	
	
	rVal.appendChild(document.createElement("td"));
	
	return rVal;
};
siLoginForm.prototype.getEmailGuid = function( )
{
	var cookies = new siCookieParser(document.cookie);
	if (cookies.getValue("wkssrm"))
	{
		$.ajax({
			url: "/login/loginHandler.aspx",
			dataType: "xml",
			type: "POST",
			success: new Function("rData", this.buildWindowCall("emailGuidTranslation") + "(rData);"),
			data: {rememberEmailGuid: cookies.getValue("wkssrm")}
		});
	}
};
siLoginForm.prototype.emailGuidTranslation = function(rData)
{
	var email = $("email", rData).text();
	$("#" + this.userNameId).val(email);
	if (this.wkEmailPattern.test(email))
	{
		$("#" + this.rememberId).attr("checked", true);
	}

}
siLoginForm.prototype.buildWindowCall = function(funcName)
{
	return "window[\"" + this.winId + "\"]." + funcName;
};
siLoginForm.prototype.showLoginForm = function( )
{
	$("#siLoginFormDiv").fadeIn("slow");
	this.statusLoginFormDisplay();
};
siLoginForm.prototype.hideLoginForm = function( )
{
	$("#siLoginFormDiv").fadeOut("slow");
	this.statusLoggedOut();
}
siLoginForm.prototype.statusLoggedOut = function( )
{
	$("#siLoginName").html("Welcome Guest");
	$("#" + this.FormAttachTarget + " a").unbind("click").click(new Function("event", "event.preventDefault();" + this.buildWindowCall("showLoginForm") + "();")).html("Login");
};
siLoginForm.prototype.statusLoginFormDisplay = function( )
{
	$("#" + this.FormAttachTarget + " a").unbind("click").click(new Function("event", "event.preventDefault();" + this.buildWindowCall("hideLoginForm") + "();"));
};
siLoginForm.prototype.statusLoggedIn = function( )
{
	var welcomeText = "Welcome";
	if (this.username != "")
	{
		welcomeText = welcomeText + " " + this.username.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
	}
	$("#siLoginName").html(welcomeText);
	$("#" + this.FormAttachTarget + " a").unbind("click").click(new Function("event", "event.preventDefault();window[\"" + this.winId + "\"].logout();")).html("Logout");
};
siLoginForm.prototype.logout = function( )
{
	if (confirm("Are you sure that you wish to log out?"))
	{
		$.ajax({
			url: "/login/loginHandler.aspx",
			dataType: "xml",
			type: "POST",
			data: {logout: "true"},
			success: function(rData) {location.href = "/login/login.aspx?returnUrl=" + encodeURIComponent(location.pathname);}
		});
	}
};
$(document).ready(function() {var lf = new siLoginForm();lf.initialize();});