setTimeout(function(){
	checkForPasswordChange();
},3000);

/**
 * function checkForPasswordChange()
 * checks if user-password was changed. If so, all subscriptions
 * will be broken and old password is needed once to decrypt 
 * subscription-credentials on backend.
 */

function checkForPasswordChange(){

	// successhandler for initial request
	var successHandler = function(e){
		if(e.data){		
			if (e.data.secretWorks == false){
				
				// workaround to assure the alert-window stays open
				// when clicking on OK
				globalalert.leaveOpen = true;
				
				// on-ok function
				var onOk = function(){
					
					// get enterd password
					var oldPass = jQuery("#oldKeywordField").val();
					if(oldPass !== ""){
						
						var migrateSuccess = function(e,f){
							if (e.data == 1){
								// returned 1 so decryption of subscriptions on backend
								// was successfull
								delete (globalalert.leaveOpen);
								jQuery("#oldPasswordWarn").empty();
								jQuery("#oldPasswordWarn").fadeIn("<b>" + _("Subscriptions successfully migrated")+"</b>","slow");
								setTimeout(function(){AlertPopup.close();},1000);
							}else{
								// error, the old password was wrong
								jQuery("#oldPasswordWarn").empty();
								jQuery("#oldPasswordWarn").append("<b>" + _("Old Password is wrong.")+"</b>");
								
							}
							
						};
						// second request to migrate subscriptions from old to new password
						jQuery.ajax({
					         url: AjaxRoot + "/recovery/secret?action=migrate&session="+session+"&password="+oldPass,
					         success: migrateSuccess,
					         error: function(XMLHttpRequest, textStatus, errorThrown){
							
							 triggerEvent("OX_New_Error", 2,
					                //#. HTTP Errors from the server
					                //#. %1$s is the numeric HTTP status code
					                //#. %2$s is the corresponding HTTP status text
								    //#, c-format
					                format(_("Error: %1$s - %2$s"), textStatus, errorThrown));
							 },
					         dataType: "json",
					         type:"get"
					     });
					}else{
						// field was emtpy, display error
						jQuery("#oldPasswordWarn").empty();
						jQuery("#oldPasswordWarn").append("<b>" + _("Field may not be empty")+"</b>");
					}
				};
				
				// create new alert window without text with ok and cancel
				// button
				newConfirm(_("Password has changed"),"", 3, onOk);
				
				jQuery("#oldPasswordWarn").empty(); 
				
				// Fill alertwindow manualy with text
				jQuery("#alert_window_text").append(
					jQuery("<div>").append(
						_("Your password changed. For a future use of your subscriptions and additional E-Mail accounts enter your old password. This only has to be done once. If you now cancel the action you have to newly set up all subscriptions and additional E-Mail accounts.") + "<br>"	
					  ).append(
						'<p style="text-align:center"><label for="keyword">'+ _("Your old password") + '</label>'+
						'&nbsp;<input name="keyword" id="oldKeywordField" type="password" size="16">'+
						'</p><div id="oldPasswordWarn" style="text-align:center"><br></div>')
				);
				
				centerPopupWindow(jQuery("#alert_window"));
				jQuery("#alert_window").css({"display":"block"});
				
			}
		}
	};
  
    jQuery.ajax({

         url: AjaxRoot + "/recovery/secret?action=check&session="+session,
         success: successHandler,
         error: function(XMLHttpRequest, textStatus, errorThrown){
		
		triggerEvent("OX_New_Error", 2,
               //#. HTTP Errors from the server
               //#. %1$s is the numeric HTTP status code
               //#. %2$s is the corresponding HTTP status text
			    //#, c-format
               format(_("Error: %1$s - %2$s"), textStatus, errorThrown));
		 },
         dataType: "json",
         type:"get"
     });
}
