
	//object used to bring back userProfile
	var userData;
	
	//process a forget your pasword
	function forgotPassword() {
		window.location="/account/login.html?returnUrl="+window.location.pathname;
	}
	//log a user in based on the form
	function logUserIn(formId) {	
		var returnUrl = new String(document.location).toQueryParams().returnUrl;
		var comments = new String(document.location).toQueryParams().comments;
		var username = formId.username.value;
		var password = formId.password.value;
		UserLogin.getUserLogin(username,password, {
			callback: function(data) {
				//process cookie
				if(data == true) {
					Cookie.set('IFWUSER', username, 2000, '/');
					//hide form
					Effect.SwitchOff('content');
					Effect.SwitchOff('notLoggedIn');
					$('ifwuser').innerHTML = "<a href='/account/account.html'>"+username+"</a>";
					Form.reset('loginForm');
					Effect.Appear('loggedIn', {duration: .5});
					
					//next check to see if the article comment stuff is there, if so, update that as well, bascially, call the exposeCommentsForm
					//TODO: clean this up
					if($('commentsDiv') != null)
						exposeCommentsForm();

					if(returnUrl != null && returnUrl.length > 0 && returnUrl != 'undefined') {
						if(comments != null && comments.length > 0 && comments != 'undefined') {
						//is this the best way to support login and then add comment?
						ArticleComments.insertActiveComment(Cookie.get('IFWUSER'), comments, returnUrl, {
							callback:function(data) {
								//only redirect if no error occured.
								window.location=returnUrl;
							},
							errorHandler:function(message) {alert(message);}
						});

						}
						window.location=returnUrl;
						
					}	
				} else {
					//send to  page with error message
					if(returnUrl != null && returnUrl.length > 0 && returnUrl != 'undefined')
						window.location="/account/login.html?errorMsg=Incorrect%20Username%20or%20Password&returnUrl="+returnUrl+"&comments="+escape(comments);
					else
						window.location="/account/login.html?errorMsg=Incorrect%20Username%20or%20Password&returnUrl="+window.location.pathname+"&comments="+escape(comments);
				}
			},
			errorHandler: function(message) {
				alert(message);
			}
		});

		//this is hard-wired for a  test


	}
	
	function loginRequired() {
		var user = Cookie.get('IFWUSER');
		if(user == null) {
			//redirect to login page
			window.location="/account/login.html?errorMsg="+escape("Not Logged In");
		}
	}

	function sendToRegisterFromTop() {
		var queryParam = new String(document.location).toQueryParams();
//		var returnUrl = queryParam.returnUrl;
		var returnUrl = window.location.pathname;
		var comments = queryParam.comments;
		window.location = "/account/register.html?returnUrl="+returnUrl+"&comments="+escape(comments);
	}

	//retrieves a user profile based on a cookie
	function getProfile() {
		var user = Cookie.get('IFWUSER');
		if(typeof(username) != 'undefined') { 
			if(username != null && username.length > 0 && username != 'undefined')
				user = username;
		}
		if(user == null) {
			//see if the username is set
			//redirect to login page, if not logged in
			window.location="/account/login.html?errorMsg=Not Logged In";
		} else {
			UserLogin.getProfile(user, {
			     callback: function(data) {
				//store this locally so we have access to it
				userData = data;
				Form.Element.deserialize($('email'), data.email || "");
				//profile form
				Form.Element.deserialize($('firstName'), data.firstName || "");
				Form.Element.deserialize($('lastName'), data.lastName || "");
				Form.Element.deserialize($('address'), data.address || "");
				Form.Element.deserialize($('address1'), data.address1 || "");
				Form.Element.deserialize($('city'), data.city || "");
				Form.Element.deserialize($('state'), data.state || "");
				Form.Element.deserialize($('country'), data.country || "");
				Form.Element.deserialize($('addressType'),data.addressType || "");
				Form.Element.deserialize($('zip'), data.zip || "");
				Form.Element.deserialize($('company'), data.company || "");
				Form.Element.deserialize($('jobFunction'), data.jobFunction || "");
				Form.Element.deserialize($('jobTitle'), data.jobTitle || "");
				Form.Element.deserialize($('industry'), data.industry || "");
				Form.Element.deserialize($('organizationSize'), data.organizationSize || "");
				Form.Element.deserialize($('optIn'), data.optIn || "");
			     }
			});
		}
	}

	function updateUserProfile() {
		if(userData == null) {
			//handle this case, but leave blank for now
			alert("no user data");
		} else {
			var user = userData.userName;
			userData.firstName = $F('firstName');
			userData.lastName = $F('lastName');
			userData.address = $F('address');
			userData.address1 = $F('address1');
			userData.city = $F('city');
			userData.state = $F('state');
			userData.country = $F('country');
			userData.zip = $F('zip');
			userData.addressType = $F('addressType');
			userData.company = $F('company');
			userData.jobFunction = $F('jobFunction');
			userData.jobTitle = $F('jobTitle');
			userData.industry = $F('industry');
			userData.organizationSize = $F('organizationSize');
			userData.optIn = $F('optIn');

			UserLogin.updateUserProfile(user, userData, {
				callback: function (data) {
					//write out a message i suppose
		       			$(errorMsg).innerHTML = "Profile updated successfully";
					Element.show('error');
					Element.scrollTo('error');
				},
				errorHandler:function(message) {
					if($('errorMsg') != null) {
			       			$(errorMsg).innerHTML = message;
						Element.show('error');
					} else {
						alert(message);
					}
				}
			});
		}
		return false;
		
	}
	
	function updateUserEmail() {
		if($F('email').length > 0) {
			//update the emails
			var user = Cookie.get('IFWUSER');
			UserLogin.updateEmail(user, $F('email'), {
				callback: function (data) {
					//write out a message i suppose
		       			$(errorMsg).innerHTML = "Email updated successfully";
					Element.show('error');
					Element.scrollTo('error');
				},
				errorHandler:function(message) {
					if($('errorMsg') != null) {
			       			$(errorMsg).innerHTML = message;
						Element.show('error');
					} else {
						alert(message);
					}
				}
			});
		}
	}
		
	function viewProfile() {
		window.location = '/account/account_profile.html?'+Form.serialize('changeEmail')+'&'+Form.serialize('updateProfile');
	}

	function updatePassword() {
		var user = Cookie.get('IFWUSER');
		var oldPassword = $F('oldPassword');	//look at the data object
		var newPassword = $F('newPassword');
		var confirmNewPassword = $F('confirmNewPassword');
		if(newPassword != confirmNewPassword) {
			$(errorMsg).innerHTML = "Your new passwords do not match";
			Element.show('error');
			Element.scrollTo('error');
		} else if(oldPassword != userData.password) {
			$(errorMsg).innerHTML = "Your current password is not correct";
			Element.show('error');
			Element.scrollTo('error');
		} else {
			UserLogin.updatePassword(user, newPassword, oldPassword, {
				callback: function (data) {
					//write out a message i suppose
		       			$(errorMsg).innerHTML = "Password updated successfully";
					Element.show('error');
					Element.scrollTo('error');
					userData.password=newPassword;
				},
				errorHandler:function(message) {
					if($('errorMsg') != null) {
			       			$(errorMsg).innerHTML = message;
						Element.show('error');
					} else {
						alert(message);
					}
				}
			});
		}

		
	}


	function activateUser() {
		var queryValues = new String(document.location).toQueryParams();
		var values = queryValues.r;
		var returnUrl = queryValues.url;
		UserLogin.activateUser(values, {
			callback: function(data) {
				//send to thankyou page
				//set a cookie too
				var split = values.split("ZZZR@@");
				if(split.length == 3) {
					//log the user in
					Cookie.set('IFWUSER', split[2], 2000, '/');
				}
				window.location="/account/thankyou.html?returnUrl="+returnUrl;
			},
			errorHandler: function(message) {
				alert("an error has occured: " + message);
			}
		});
	}

	function retrievePassword() {
		UserLogin.emailPassword($F('retrieveusername') || "", $F('email') || "", {
			callback: function (data) {
				//write out a message i suppose
	       			$(errorMsg).innerHTML = "Your password has been sent to your email";
				Element.show('error');
				//Element.scrollTo('error');
			},
			errorHandler:function(message) {
				if($('errorMsg') != null) {
					if(message == 'null')
						$(errorMsg).innerHTML = "That username or e-mail address cannot be located within our system. Please check your information and try again.";
					else
		       				$(errorMsg).innerHTML = message;
					Element.show('error');
				} else {
					alert(message);
				}
			}
		});		
	}

	function registerUser(){
		var thirdParty = "N";
		if(document.register.elements['thirdParty'].type =="checkbox" && document.register.elements['thirdParty'].checked)
			thirdParty = "Y";
		try {
		if(checkTosPP() && validate() == true) {
			var comments = "";
			if($("commentsField") == null)
				comments = "";
			else
				comments = $("commentsField").value;

			var returnUrl = new String(document.location).toQueryParams().returnUrl;
			var comments =  new String(document.location).toQueryParams().comments;
			UserLogin.insertProfileAndComment($F('signupUsername'), $F('signupEmail'), $F('signupPassword'), $F('challenge'), comments, returnUrl, {
				callback: function(data) {
					//after a successful insert, log the user in, and set their cookie
					Cookie.set('IFWUSER', $F('signupUsername'), 2000, '/');
					window.location="/account/addProfileInfo.html?thirdParty=" + thirdParty + "&status=Success&newEmail="+$F('signupEmail')+"&username="+$F('signupUsername')+"&returnUrl="+returnUrl;
				},
				errorHandler:function(message) {
					//if there an error
					//window.location="/account/register.html?errorMsg="+message;
					//alert("error: " + message);
					if(message == "DUPLICATE_USERNAME")
						alert("We're sorry.  That Username is already in use on this site.  Please register using another username, or Login to access InfoWorld.com");
					else if(message == "DUPLICATE_EMAIL")
						alert("We're sorry.  That E-mail is already in use on this site.  Please register using another E-mail, or Login to access InfoWorld.com");
					else
						alert(message);
				}
			});
		} 
		} catch (e) {
			alert(e);
		}

	}

	//if there are any error messages show it here
	function checkErrorMsg() {
		var queryParam = new String(window.location).toQueryParams().errorMsg;
		if(queryParam != undefined && queryParam.length > 0) {
			if($('errorMsg') != null ) {
				$(errorMsg).innerHTML = queryParam;
				Element.show('error');
			}
		}
	}

	function checkRegPermission() {
		var cookievalue = Cookie.get('IFWUSER');
		if(cookievalue != null && cookievalue.length > 0) {
			//no need to register again, take them to their profile page
			window.location="/account/account.html";			
		}
	}
	
	function determineLoginStatus() {
		var cookievalue = Cookie.get('IFWUSER');
		if(cookievalue != null && cookievalue.length > 0) {
			Element.hide('notLoggedIn');
			$('ifwuser').innerHTML = "<a href='http://www.infoworld.com/account/account.html'>"+cookievalue+"</a>";
			Element.show('loggedIn');			
			
		}
	}
	
	function logout() {
		Cookie.erase('IFWUSER', '/');
		Element.hide('loggedIn');
		Effect.Appear('notLoggedIn', {duration: .5});
		//next check to see if the article comment stuff is there, if so, update that as well, bascially, call the exposeCommentsForm
		if($('commentsDiv') != null)
			exposeCommentsForm();
	}

function checkTosPP(frm) {
	var fields = document.register.elements;
	var requiredFields = [
		["toc", "Please check the box to agree to the Terms of Service and Privacy Policy."]
	];

	for ( var i=0; i<requiredFields.length; i++ ) {
		if( (fields[requiredFields[i][0]].type == "checkbox" ) && !fields[requiredFields[i][0]].checked ) {
			alert( requiredFields[i][1] );
			return false;
		}
	}

   	return true;

}
