if (typeof UPOC == "undefined" || !UPOC) {
    var UPOC = {};
}

// login management functions
UPOC.Auth = {};

//verifies user login status
UPOC.Auth.isLoggedIn = function(){
  if(user.handle){
    return true;
  } else{
    return false;
  }
};

UPOC.Auth.loginSuccess = function(){

       $('searchbarLoggedIn').setHTML('Welcome, ' + user.handle + "! <a href='#' onclick='UPOC.Auth.logoutAttempt();'>Log Out</a>");
       $('searchbarJoin').setStyle('display','none');
       $('searchbarLoggedIn').setStyle('display','inline');
       //if(messages.user.profilePictureThumbnailUrl){
       	//	user.thumbUrl = messages.user.profilePictureThumbnailUrl;
       	//}

		//pageLogIn frequently gets fired twice since alcdUpdate fires it if the alcd has changed
       user.alcdUpdate();

    if((typeof pageLogIn) != "undefined"){ pageLogIn(); }
    //alert(typeof pageLogIn);
};

//submits
UPOC.Auth.loginAttempt = function(successFn){
  $('signInForm').submit();
/*
    UPOC.Xhr.request("/j_acegi_security_check.json",
		null,
		"j_username=" + $('signInUsername').get('value') + "&j_password=" + $('signInPassword').get('value'),
		function(msgObj){
			user.handle = $('signInUsername').get('value');
			UPOC.Auth.loginSuccess();
			UPOC.Popup.close();
            //successFn();
            //alert("made it!");
            if((typeof successFn) === "function"){ setTimeout(successFn,1000); }
        },
		function(text){$('signInErrorMsg').setHTML(text);}
	);
*/
};

UPOC.Auth.logoutSuccess = function(){
		user.handle = null;
		user.alcd = null;
		user.thumbUrl = null;
		user.alcdUpdate();

        if((typeof pageLogout) != "undefined"){ pageLogout(); }

        if(window.location.pathname != "/index.htm"){
            location = "/";
        }
};

UPOC.Auth.logoutAttempt = function(){
	UPOC.Xhr.request("/logout.json","GET",null, function(msgObj){ UPOC.Auth.logoutSuccess(); })
};



// code for login popup
UPOC.Auth.loginFormClear = function(){
    if(($('signInUsername').get('value') == "Username") && ($('signInPassword').get('value') == "password")){
        $('signInUsername').setProperty('value',"");
        $('signInPassword').setProperty('value',"");
    }
};

UPOC.Auth.loginPopup = function(){
    UPOC.Popup.open('signIn');
    $('signInPassword').setProperty('value','password');
};

