var _soGlobalHandle;

function soLoginTools(AOwnerDiv) {
	
	if (!AOwnerDiv) {
		var missingArgumentError = new Error("No owner div passed!");
		throw missingArgumentError;
	} else if (typeof(AOwnerDiv) != "object") {
		var invalidArgumentError = new Error("Argument passed is of incorrect type!");
		throw invalidArgumentError;
	} else {
		var _loginObj = new Object();
		
		_loginObj.OwnerDiv = AOwnerDiv;
		
		_loginObj.URIPrefix = "/apps/pbcs.dll/";
		
		_soGlobalHandle = _loginObj;

		// User definable settings		
		_loginObj.labelUsername = "Username";
		_loginObj.labelPassword = "Password";
		_loginObj.labelLogin    = "Login";
		_loginObj.labelSignup   = "Sign up";
		_loginObj.signupURI   = "/userprofile";
		_loginObj.inputLength = 30;

		_loginObj.getXmlHttpRequest = function() {
			if (window.XMLHttpRequest) {
				return new XMLHttpRequest();
			} else if (window.ActiveXObject) {
				try {
					return new ActiveXObject("Msxml2.XMLHTTP");
					} catch (e) {
						try {
							return new ActiveXObject("Microsoft.XMLHTTP");
							} catch (E) {
								return null;
						}
				}
			}	else {
				return null;
			}
		};

		// Method to display login dialog.		
		_loginObj.showLoginDialog = function(AStatus) {
			var _html = "\
				<div id=\"soLoginBox\" class=\"soLoginBox\">\
					<form id=\"soLoginForm\">\
						<fieldset>\
							<legend>Login</legend>\
							<p>\
								<label for=\"username\">" + this.labelUsername + "</label>\
								<input type=text name=\"username\" size=" + this.inputLength + ">\
							</p>\
							<p>\
								<label for=\"pwd\">" + this.labelPassword + "</label>\
								<input type=password name=\"pwd\" size=" + this.inputLength + ">\
							</p>\
							<p class=\"submit\">\
								<input type=button value=\"" + this.labelLogin + "\" onClick=\"_soGlobalHandle.performLogin(this.form);\">\
								<input type=button value=\"" + this.labelSignup + "\" onClick=\"_soGlobalHandle.performSignup();\">\
							</p>\
						</fieldset>\
					</form>\
				</div>";

			if (AStatus == -1)  {
				_html += "<div id=\"soLoginMsgBox\" class=\"soLoginMsgBox\" align=\"center\">Invalid Username or Password.  Please try again.</div>";
			}
			
			this.OwnerDiv.innerHTML = _html;
		}

		// Method to handle login.
		_loginObj.performLogin = function(AForm) {
			if (AForm.username.value == "") {
				alert("Username is required");
			} else if (AForm.pwd.value == "") {
				alert("Password is required");
			} else {
			  var req = this.getXmlHttpRequest();
				  req.open("POST",this.URIPrefix + "exec?Name=LoginUser.js",true);
   				req.onreadystatechange = function() {
		  			if (req.readyState==4) { _loginObj.checkLoginStatus(req.responseText); }
			  	};
  				req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	  			req.send("username=" + encodeURIComponent(AForm.username.value) + "&" + "pwd=" + encodeURIComponent(AForm.pwd.value));
		  }
		}
		
		// Method to handle signup.
		_loginObj.performSignup = function() {
			location.href = this.signupURI;
		}
		
		// Method to handle signup.
		_loginObj.checkLoginStatus = function(AStatus) {
			if (parseInt(AStatus) > 0) {
				this.showUserProfile()
			} else {
				this.showLoginDialog(AStatus);
			}
		}
		
		// Method will show profile of user.
		_loginObj.showUserProfile = function() {
			var req = this.getXmlHttpRequest();
			req.open("GET",this.URIPrefix + "exec?Name=ShowUserprofile.js",true);
			req.onreadystatechange = function() {
				if (req.readyState==4) { _loginObj.OwnerDiv.innerHTML = req.responseText; }
			};
			req.send(null);
		}
		
		// Method will show profile or present login box if not logged in.
		_loginObj.showUser = function() {
			var req = this.getXmlHttpRequest();
			req.open("GET",this.URIPrefix + "exec?Name=IsLoggedIn.js",true);
			req.onreadystatechange = function() {
				if (req.readyState==4) { _loginObj.checkLoginStatus(req.responseText); }
			};
			req.send(null);
		}

		// Method to check if value of field is available.
		_loginObj.checkAvailable = function(AField, AValue) {
			var req = this.getXmlHttpRequest();
			req.open("GET",this.URIPrefix + "exec?Name=IsAvailable.js&Field=" + AField + "&Value=" + encodeURIComponent(AValue),false);
			req.setRequestHeader("Connection", "close");
			req.send();
			return (parseInt(req.responseText) == 1);
		}
		
		// Method to check if value of field is available.
		_loginObj.logout = function() {
			var req = this.getXmlHttpRequest();
			req.open("GET",this.URIPrefix + "exec?Name=IsLoggedIn.js&Reset=1",false);
			req.setRequestHeader("Connection", "close");
			req.send();
			location.href = "/";
		}
		
	}
	
	return _loginObj;
}



