var Login = new Object();
    Login = {
		curUserID:"",
		curUserErrorSpan:"",
		curUserErrorMessage:"",
		passwordID:"",
		passwordErrorSpan:"",
		passwordErrorMessage:"",
		summaryID:"",
		responsePanelID:"",
		buttonID:"",
		curLoginPage:"",
		requestString:"",
		recoverySummaryID:"",
		recoveryEmailID:"",
		recoveryEmailErrorSpan:"",
		recoveryEmailErrorMessage:"",
		recoveryRequestButton:"",
		recoveryRequestString:"",
		recoveryStatusMsg:"",
		curRecoveryPage:"",
		closeAfterAuthFlag:true,
        userId:null,
        firstName:null,
        clickFunction:null,
        init:function()
        {
        
        },
        logout:function()
        {
		    //window.location = "/callback/logout.aspx?redirectURL=" + window.location;
			if(this.userId)
            {
                var data = "action=logout";
                AjaxObject.startRequest("logout",data,null);
            }
			this.forceToggle();
        },
		forceToggle:function()
		{
			if($("MainForm"))	Utility.swap("MainForm","LoginForm");
			if($("RegistrationLoginFormScreen1"))	Utility.swap("RegistrationLoginFormScreen2","RegistrationLoginFormScreen1");
			if($("UpdateAccountForm")) { Utility.swap("UpdateAccountForm","UpdateLoginForm"); }
			Utility.swap("widgetSubmitComment","widgetInputContainer");
			/*
			if($())	Utility.swap("","");
			if($())	Utility.swap("","");
			if($())	Utility.swap("","");
			*/
		},
        handleLogoutSuccess:function(o)
        {
            //$("LoginStatus").value = "Logged Out";
            //alert("logged out");
			     Utility.turnOn('page_top_login_link');
			     Utility.turnOff('page_top_personalization');
				 Utility.turnOff('page_top_general_error');
				 Utility.turnOff("page_top_login_layer");
				 Utility.turnOff("page_top_account_error");
				 Utility.turnOff("commentForm");
			     LoginStatus.reset();
        },
        attempt:function(section)
        {
			switch(section)
			{
				case "registration":
					this.curLoginPage = "registration";
					this.summaryID = "regLoginSummary";
					this.curUserID = "regEmail";
					this.curUserErrorSpan = "regEmailValidationSpan";
					this.curUserErrorMessage = "Please enter your email address.";
					this.passwordID = "regPassword";
					this.passwordErrorSpan = "regPasswordValidationSpan";
					this.passwordErrorMessage = "Please enter your password.";
					this.responsePanelID = "regLoginResponsePanel";
					this.buttonID = "regLoginButton";
					this.requestString = "login";
					this.permLogin = "login_rememberme";
					this.loginStatusMsg = "";
				break;
				case "realpeople":
					this.curLoginPage = "realpeople";
					this.summaryID = "regLoginSummary";
					this.curUserID = "regEmail";
					this.curUserErrorSpan = "regEmailValidationSpan";
					this.curUserErrorMessage = "Please enter your email address.";
					this.passwordID = "regPassword";
					this.passwordErrorSpan = "regPasswordValidationSpan";
					this.passwordErrorMessage = "Please enter your password.";
					this.responsePanelID = "regLoginResponsePanel";
					this.buttonID = "regLoginButton";
					this.requestString = "login";
					this.permLogin = "login_rememberme";
					this.loginStatusMsg = "";
				break;
				case "user":
					this.curLoginPage = "user";
					this.summaryID = "regLoginSummary";
					this.curUserID = "regEmail";
					this.curUserErrorSpan = "regEmailValidationSpan";
					this.curUserErrorMessage = "Please enter your email address.";
					this.passwordID = "regPassword";
					this.passwordErrorSpan = "regPasswordValidationSpan";
					this.passwordErrorMessage = "Please enter your password.";
					this.responsePanelID = "regLoginResponsePanel";
					this.buttonID = "regLoginButton";
					this.requestString = "login";
					this.permLogin = "user_rememberme";
					this.loginStatusMsg = "";
				break;
				case "widget":
					this.curLoginPage = "widget";
					this.summaryID = "widgetSummaryText";
					this.curUserID = "widgetLoginID";
					this.curUserErrorSpan = "widgetLoginIDErrorSpan";
					this.curUserErrorMessage = "Please enter your email address.";
					this.passwordID = "widgetPassword";
					this.passwordErrorSpan = "widgetPasswordErrorSpan";
					this.passwordErrorMessage = "Please enter your password.";
					this.responsePanelID = "widgetLoginResponsePanel";
					this.buttonID = "widgetSubmitButton";
					this.requestString = "login";
					this.permLogin = "";
					this.loginStatusMsg = "";
				break;
				case "registration_inline":
					this.curLoginPage = "registration_inline";
					this.summaryID = "registrationStep2Summary";
					this.curUserID = "EmailAddressFinal";
					this.curUserErrorSpan = "emailValidationSpan";
					this.curUserErrorMessage = "Please enter your email address.";
					this.passwordID = "desiredPassword";
					this.passwordErrorSpan = "passwordValidationSpan";
					this.passwordErrorMessage = "Please enter your password.";
					this.responsePanelID = "registrationResponsePanel";
					this.buttonID = "register_button";
					this.requestString = "login";
					this.permLogin = "";
					this.loginStatusMsg = "";
				break;
				case "main_redirect":
					this.curLoginPage = "main_redirect";
					this.summaryID = "summary_txt";
					this.curUserID = "loginID";
					this.curUserErrorSpan = "loginIDErrorSpan";
					this.curUserErrorMessage = "Please enter your email address.";
					this.passwordID = "password";
					this.passwordErrorSpan = "passwordErrorSpan";
					this.passwordErrorMessage = "Please enter your password.";
					this.responsePanelID = "loginResponsePanel";
					this.buttonID = "globalLoginButton";
					this.requestString = "login";
					this.permLogin = "remember_me";
					this.loginStatusMsg = "";
				break;
				default:
					this.curLoginPage = "main";
					this.summaryID = "summary_txt";
					this.curUserID = "loginID";
					this.curUserErrorSpan = "loginIDErrorSpan";
					this.curUserErrorMessage = "Please enter your email address.";
					this.passwordID = "password";
					this.passwordErrorSpan = "passwordErrorSpan";
					this.passwordErrorMessage = "Please enter your password.";
					this.responsePanelID = "loginResponsePanel";
					this.buttonID = "globalLoginButton";
					this.requestString = "login";
					this.permLogin = "remember_me";
					this.loginStatusMsg = "";
				break;
			}
			
			//alert("curUserID="+this.curUserID+",val: "+$(this.curUserID).value+"  ,  passwordID="+this.passwordID+",val: "+$(this.passwordID).value);
            var myValidate = new Validate(this.summaryID);
            // control to validate : reporting Node ID (typically a span) : message
            myValidate.isEmail(this.curUserID,this.curUserErrorSpan,this.curUserErrorMessage);
            myValidate.isEmpty(this.passwordID,this.passwordErrorSpan,this.passwordErrorMessage);

            if(myValidate.isValid())
            {
                var data =  "action=login" +
                            "&username=" + $(this.curUserID).value +
                            "&password=" + $(this.passwordID).value;
							if(this.permLogin != "") 
							{
								if($(this.permLogin).checked)
								{
									data += "&permLogin=true";
								}
								else
								{
									data += "&permLogin=false";
								}
							}
                //$("loginButton").disabled = true;
				this.clickFunction = $(this.buttonID).onclick;
				Utility.turnOn(this.responsePanelID);
                //alert("this.requestString=" + this.requestString + ", data=" + data + ", this.responsePanelID=" + this.responsePanelID);
                AjaxObject.startRequest(this.requestString,data,this.responsePanelID);
                myValidate = null;
                return;
            }
            else
            {
                myValidate.yield(false);
                return;
            }
        },
        loginResultsSchema: {
            rowtag:"result_set" 
        },
        handleSuccess:function(o)
        {
            //alert(o.responseText);
            var firstName = new String();
            // create an XML doc out of the response text
            var loginResultXML = XML.parse(o.responseText);
            // get the first set of nodes 
            var xmlrows = loginResultXML.getElementsByTagName(this.loginResultsSchema.rowtag);
            // get the first row
            var xmlrow = xmlrows[0];
            // get the <result/> tag's contents
            var result = xmlrow.getElementsByTagName("result")[0];
            var error = xmlrow.getElementsByTagName("error")[0];
            var message = xmlrow.getElementsByTagName("message")[0];
            if(xmlrow.getElementsByTagName("user_id")[0].firstChild) this.userId = xmlrow.getElementsByTagName("user_id")[0].firstChild.data;
            if(xmlrow.getElementsByTagName("first_name")[0].firstChild) firstName = xmlrow.getElementsByTagName("first_name")[0].firstChild.data;
            //this.userName = xmlrow.getElementsByTagName("user_name")[0].firstChild.data;
            Utility.turnOff(this.responsePanelID);
            $(this.buttonID).onclick = this.clickFunction;
            if(error.firstChild.data == "false")
            {
                if(result.firstChild.data == "true")
                {
					LoginStatus.isLoggedIn = true;
					LoginStatus.firstName = firstName;
					LoginStatus.UserId = this.userId;
					$("loginID").value = "";
					$("password").value = "";
					if($("widgetSubmitComment")) 
					{
						Utility.swap("widgetInputContainer","widgetSubmitComment");
					}
					switch(this.curLoginPage)
					{
						case "registration":
							/*global login*/
							Utility.turnOn("page_top_personalization");
							Utility.turnOff("page_top_login_layer");
							Utility.turnOff("page_top_general_error");
							Utility.turnOff("page_top_login_link");
							Utility.turnOff("page_top_account_error");
							$("welcomeName").innerHTML = LoginStatus.firstName;
							/*reg login*/
							Utility.turnOn("RegistrationLoginFormScreen2");
							Utility.turnOff("RegistrationLoginFormScreen1");
							$("regWelcomeName").innerHTML = LoginStatus.firstName;
							
							if (document.getElementById('returnURL'))
							{
							    var redirect = document.getElementById('returnURL').value;
						        if (redirect != '')
							    {
							        setTimeout("window.location = \"" + redirect + "\"",2000);
							    }
							}
							
						break;
                        case "realpeople":
							/*global login*/
							Utility.turnOn("page_top_personalization");
							Utility.turnOff("page_top_login_layer");
							Utility.turnOff("page_top_general_error");
							Utility.turnOff("page_top_login_link");
							Utility.turnOff("page_top_account_error");
							$("welcomeName").innerHTML = LoginStatus.firstName;
							/*reg login*/
							Utility.turnOn("MainForm");
							Utility.turnOff("LoginForm");
							//$("regWelcomeName").innerHTML = LoginStatus.firstName;
						break;
						case "user":
							window.location = window.location;
							/*global login
							Utility.turnOn("page_top_personalization");
							Utility.turnOff("page_top_login_layer");
							Utility.turnOff("page_top_login_link");

							$("welcomeName").innerHTML = LoginStatus.firstName;
							*/
							/*reg login
							Utility.swap("UpdateLoginForm","UpdateAccountForm");
							$("regWelcomeName").innerHTML = LoginStatus.firstName;
							*/
						break;
						case "widget":
							/*global login*/
							Utility.turnOn("page_top_personalization");
							Utility.turnOff("page_top_login_layer");
							Utility.turnOff("page_top_login_link");

							$("welcomeName").innerHTML = LoginStatus.firstName;
							/**/
                            				Utility.swap("widgetInputContainer","widgetSubmitComment");
							Utility.turnOn("commentForm");
						    break;
						case "registration_inline":
							/*global login*/
							Utility.turnOn("page_top_personalization");
							Utility.turnOff("page_top_login_layer");
							Utility.turnOff("page_top_login_link");

							$("welcomeName").innerHTML = LoginStatus.firstName;
							/**/
						    break;
						case "main_redirect":
						    if (document.getElementById('returnURL')) 
						    {
						        var redirect = document.getElementById('returnURL').value;
						        if (redirect == '')
							    {
							        setTimeout("window.location = \"/en/real_people/submissions/\"",1000);
							    } else {
							        setTimeout("window.location = \"" + redirect + "\"",1000);
							    }
							 }
							 else {
							    setTimeout("window.location = \"/en/real_people/submissions/\"",1000);
							 }
						break;
						case "main":
							/*
							if($("loginOptions"))
							{
								$("loginOptions").className = "hidden";
								$("registrationOptions").className = "hidden";
								$("submitOptions").className = "visible";
							}
							*/
							/*global login*/
							Utility.turnOn("page_top_personalization");
							Utility.turnOff("page_top_login_layer");
							Utility.turnOff("page_top_login_link");

							$("welcomeName").innerHTML = LoginStatus.firstName;
							/**/
							if($("MainForm")) 
							{
								Utility.swap("LoginForm","MainForm");
							}
							if($("RegistrationLoginFormScreen1"))
							{
								if(!$("UpdateLoginForm"))
								{
								/*reg login*/
								Utility.turnOn("RegistrationLoginFormScreen2");
								Utility.turnOff("RegistrationLoginFormScreen1");
								}
								$("regWelcomeName").innerHTML = LoginStatus.firstName;
							}
							if($("RegistrationLoginText")&&$("UpdateLoginForm"))
							{
								//alert("registation text");
								window.location = window.location;
							}
						break;
					}
                }
                else
                {
                    //document.getElementById("loginStatusMessage").innerHTML = message.firstChild.data;
                    //document.getElementById("loginButton").disabled = false;
    					
					switch(this.curLoginPage)
					{
						case "registration":
							$("regLoginStatusMessage").innerHTML = message.firstChild.data;
                    		$("regLoginButton").disabled = false;
						break;
						case "user":
							$("regLoginStatusMessage").innerHTML = message.firstChild.data;
                    		$("regLoginButton").disabled = false;
						break;
						case "widget":
						    $("widgetLoginResponseMessage").innerHTML = message.firstChild.data;
						    $("widgetSubmitButton").disabled = false;
						break;
						case "registration_inline":
						    $("widgetLoginResponseMessage").innerHTML = message.firstChild.data;
						    $("widgetSubmitButton").disabled = false;
						break;
						default:
							Utility.swap("page_top_login_layer","page_top_account_error");
						break;
					}
                }
            }
            else
            {
                //document.getElementById("loginStatusMessage").innerHTML = message.firstChild.data;
                //if(Login.closeAfterAuthFlag) setTimeout("YAHOO.generic.modal.win.hide();",5000);
                // ERROR HERE
                Utility.turnOn("page_top_general_error");
                Utility.turnOff("page_top_login_layer");
            }
        },
        sendPassRecoveryRequest:function(page)
        {
            switch(page)
			{
				case "registration":
					this.curRecoveryPage = "registration";
					this.recoverySummaryID = "regPassRecoverySummary";
					this.recoveryStatusMsg = "regPassRecoveryLoginStatusMessage";
					this.recoveryEmailID = "regEmailPasswordRecovery";
					this.recoveryEmailErrorSpan = "regPasswordRecoverylValidationSpan";
					this.recoveryEmailErrorMessage = "Invalid email address.";
					this.recoveryRequestButton = "regPasswordRecoveryButton";
					this.recoveryLoaderID = "regPassRecoveryResponsePanel";
					this.recoveryRequestString = "password";
				break;
				default:
					this.curRecoveryPage = "popup";
					this.recoverySummaryID = "passwordRecoverySummary";
					this.recoveryStatusMsg = "passwordStatusMessage";
					this.recoveryLoaderID = "passwordResponsePanel";
					this.recoveryEmailID = "emailAddress";
					this.recoveryEmailErrorSpan = "passRequestValidationErrorSpan";
					this.recoveryEmailErrorMessage = "Invalid email address.";
					this.recoveryRequestButton = "submitPassordRequest_button";
					this.recoveryRequestString = "password";
				break;
			}
			var myVal = new Validate(this.recoverySummaryID);
			myVal.isEmail(this.recoveryEmailID,this.recoveryEmailErrorSpan,this.recoveryEmailErrorMessage);
			if( myVal.isValid() )
            {
	            var data =  "action=password" +
                "&username=" + document.getElementById(this.recoveryEmailID).value;
                document.getElementById(this.recoveryRequestButton).disabled = true;
                Utility.turnOnInline(this.recoveryLoaderID);
                AjaxObject.startRequest(this.recoveryRequestString,data,this.recoverySummaryID);
            }
            else
            {
				myVal.yield(true);
            }
			myVal = null;
			
        },
        handlePassRecoverySuccess:function(o)
        {
            var loginResultXML = XML.parse(o.responseText);
            var xmlrows = loginResultXML.getElementsByTagName(this.loginResultsSchema.rowtag);
            var xmlrow = xmlrows[0];
            var result = xmlrow.getElementsByTagName("result")[0];
            var error = xmlrow.getElementsByTagName("error")[0];
            var message = xmlrow.getElementsByTagName("message")[0];
            Utility.turnOff(this.recoveryLoaderID);
            if(error.firstChild.data == "false")
            {
                if(result.firstChild.data == "true")
                {
                    document.getElementById(this.recoveryStatusMsg).innerHTML = message.firstChild.data;
                    document.getElementById(this.recoveryRequestButton).disabled = false;
                }
                else
                {
                    document.getElementById(this.recoveryStatusMsg).innerHTML = message.firstChild.data;
                    document.getElementById(this.recoveryRequestButton).disabled = false;
                }
            }
        },
        
  
        sendUnsubscribeRequest:function(page)
        {
            switch(page)
			{
				case "unsubscribe":
					this.curRecoveryPage = "unsubscribe";
					this.UnsubscribeSummaryID = "regUnsubscribeSummary";
					this.UnsubscribeStatusMsg = "regUnsubscribeStatusMessage";
					this.UnsubscribeEmailID = "regEmailUnsubscribe";
					this.UnsubscribeEmailErrorSpan = "regUnsubscribeValidationSpan";
					this.UnsubscribeEmailErrorMessage = "Invalid email address.";
					this.UnsubscribeRequestButton = "regUnsubscribeButton";
					this.UnsubscribeLoaderID = "regUnsubscribeResponsePanel";
					this.UnsubscribeRequestString = "password";
				break;
				default:
					this.curRecoveryPage = "popup";
					this.UnsubscribeSummaryID = "UnsubscribeSummary";
					this.UnsubscribeStatusMsg = "UnsubscribeStatusMessage";
					this.UnsubscribeLoaderID = "UnsubscribeResponsePanel";
					this.UnsubscribeEmailID = "emailAddress";
					this.UnsubscribeEmailErrorSpan = "UnsubscribeValidationErrorSpan";
					this.UnsubscribeEmailErrorMessage = "Invalid email address.";
					this.UnsubscribeRequestButton = "submitUnsubscribe_button";
					this.UnsubscribeRequestString = "password";
				break;
			}
			var myVal = new Validate(this.UnsubscribeSummaryID);
			myVal.isEmail(this.UnsubscribeEmailID,this.UnsubscribeEmailErrorSpan,this.UnsubscribeEmailErrorMessage);
			if( myVal.isValid() )
            {
	            var data =  "action=password" +
                "&username=" + document.getElementById(this.UnsubscribeEmailID).value;
                document.getElementById(this.UnsubscribeButton).disabled = true;
                Utility.turnOnInline(this.UnsubscribeLoaderID);
                AjaxObject.startRequest(this.UnsubscribeRequestString,data,this.UnsubscribeSummaryID);
            }
            else
            {
				myVal.yield(true);
            }
			myVal = null;
			
        },
        handleUnsubscribeSuccess:function(o)
        {
            var loginResultXML = XML.parse(o.responseText);
            var xmlrows = loginResultXML.getElementsByTagName(this.loginResultsSchema.rowtag);
            var xmlrow = xmlrows[0];
            var result = xmlrow.getElementsByTagName("result")[0];
            var error = xmlrow.getElementsByTagName("error")[0];
            var message = xmlrow.getElementsByTagName("message")[0];
            Utility.turnOff(this.recoveryLoaderID);
            if(error.firstChild.data == "false")
            {
                if(result.firstChild.data == "true")
                {
                    document.getElementById(this.recoveryStatusMsg).innerHTML = message.firstChild.data;
                    document.getElementById(this.recoveryRequestButton).disabled = false;
                }
                else
                {
                    document.getElementById(this.recoveryStatusMsg).innerHTML = message.firstChild.data;
                    document.getElementById(this.recoveryRequestButton).disabled = false;
                }
            }
        },
        
        
        
        
        
        getPasswordForm:function()
        {
            Utility.swap("loginOptions","forgotPasswordField");
        },
        getDefaultLogin:function()
        {
            Utility.swap("forgotPasswordField","loginOptions");
        },
        getUnsubscribe:function()
        {
            Utility.swap("unsubscribeField","loginOptions");
        },
        getUserId:function()
        {
            return this.userId;
        }
    };