var UserProfileFrame = {
	Name: "UserProfileFrame",

  InitFrame: function() {
    jQuery("#UserProfileForm").submit(UserProfileFrame.SubmitUserProfile);
    jQuery("#ChangePasswordForm").submit(UserProfileFrame.SubmitChangePassword);
  },
	
  ShowFrame: function() {
		jQuery('#UserProfileFrame').show();
		
		if (Session.UserProfile != null) {
		  UserProfileFrame.UpdateUserProfileFrameControls();
		}	
		
		document.getElementById("OldPassword").value = '';
		document.getElementById("NewPassword").value = '';
		document.getElementById("NewPassword2").value = '';
		SetWindowKeyPressHandler(UserProfileFrame.OnKeyPress);
	},

  HideFrame: function() {
    jQuery('#UserProfileFrame').hide();
  },

	OnKeyPress: function(event) {
		switch (event.keyCode) {
			case 116:
				return false;
				break;
		}
	},
	
  OnLogoff: function() {

  },
	
  OnFrameResize: function() {

  },

  UpdateUserProfileFrameControls: function() {
    document.UserProfileForm.ReportLanguage.selectedIndex = 0;
    for (i=0 ; i < document.UserProfileForm.ReportLanguage.length ; i++)
    {
      if ( Session.UserProfile.ReportLanguage == document.UserProfileForm.ReportLanguage.options[i].text )
        document.UserProfileForm.ReportLanguage.selectedIndex = i;
    }
	
    document.UserProfileForm.DefaultArchiveDisplayPeriode.value = Session.UserProfile.DefaultArchiveDisplayPeriode;
    document.UserProfileForm.EnableQueueReport.checked = Session.UserProfile.EnableQueueReport;
    document.UserProfileForm.UseHtmlMail.checked = Session.UserProfile.UseHtmlMail;
  },

  SubmitUserProfile: function() {
		i = document.UserProfileForm.ReportLanguage.selectedIndex;

    Session.UserProfile.ReportLanguage = document.UserProfileForm.ReportLanguage.options[i].text;
    Session.UserProfile.DefaultArchiveDisplayPeriode = document.UserProfileForm.DefaultArchiveDisplayPeriode.value;
    Session.UserProfile.EnableQueueReport = document.UserProfileForm.EnableQueueReport.checked;
    Session.UserProfile.UseHtmlMail = document.UserProfileForm.UseHtmlMail.checked; 
		
		// DefaultArchiveDisplayPeriod is currently localy stored
		SetCookie('DefaultArchiveDisplayPeriod', Session.UserProfile.DefaultArchiveDisplayPeriode, 365);
		
    var service = new CoreService(serviceURL, Session.SessionID);
    service.SaveUserProfile(Session.UserProfile,
      {
        success: function(result) {
					jAlert(_('Profile saved.'), _('Information'));
				},
        failure: ErrorHandler
      }
    );
		
		return false;
	},
	
	SubmitChangePassword: function() {
		var OldPW =	trim(document.getElementById("OldPassword").value);
		var NewPW =	trim(document.getElementById("NewPassword").value);
		var NewPW2 =	trim(document.getElementById("NewPassword2").value);
	
	  if (OldPW == '') { jAlert(_('The field must not be empty.'), _('Information'), function() { document.getElementById("OldPassword").focus(); }); return false; }
	  if (NewPW == '') { jAlert(_('The field must not be empty.'), _('Information'), function() { document.getElementById("NewPassword").focus(); }); return false; }
	  if (NewPW2 == '') { jAlert(_('The field must not be empty.'), _('Information'), function() { document.getElementById("NewPassword2").focus(); }); return false; }
	
	  if (NewPW != NewPW2) { jAlert(_('The new passwords are not the same.'), _('Information'), function() { document.getElementById("NewPassword").focus(); }); return false; }
		
    var service = new CoreService(serviceURL, Session.SessionID);
    service.ChangePassword(OldPW, NewPW,
		{
			success: function(result) {
				jAlert(_('Password successfully changed.'), _('Information'),
    				function() {
							document.getElementById("OldPassword").value = '';
							document.getElementById("NewPassword").value = '';
							document.getElementById("NewPassword2").value = '';
						});
			},
			failure: ErrorHandler
		});
		
		return false;
	}
}
