function encodeSafe(str) {
    return encodeURIComponent(str).replace(/%/g, ",");
}
function buildBaseUrl() {
    var here = new cm4all.HttpURL(document.location.href);
    var herePort = '';
    if (here.port != -1) {
        herePort = ':' + here.port;
    }
    return here.protocol + '://' + here.host + herePort;
}
function buildPath() {
    var here = new cm4all.HttpURL(document.location.href);
    var path = here.path;
    if (path) {
        path = path.replace(/;.+$/, "");
    }
    if (!path || path == "/") {
        path = loginPagePath;
    }
    if (!path || path == "/") {
        path = "/notfound/1,auth,8,1";
    }
    return path;
}
function testForLoginFailure() {
    if (document.location.href.search('Xhutzzz7') != -1) {
        pollLogin(showLogin, true);
    }
    return false;
}
function alreadyPolled() {
    return false;
}
function saveAlreadyPolled() {
}
function pollLogin(onError, onErrorArg) {
    if (alreadyPolled()) {
        if (onError) {
            onError(onErrorArg);
        }
    } else if (!loginPolling) {
        loginPolling = true;
        window.setTimeout("loginPolling=false", 5000);
        var wtgt = buildBaseUrl() + '/poll/1,auth,8,1;translate='
                + encodeURIComponent('auth=login') + '?txnid=_TXID_';
        var url = loginU4BaseUrl + '/loginExists.action?redirectURL='
                + encodeURIComponent(wtgt);
        var req = $('pollIFrame');
        waitOnLoginResult(req, function() {
            var url = buildBaseUrl() + buildPath();
            if (url == document.location.href) {
                document.location.reload();
            } else {
                document.location.href = url;
            }
        }, function() {
            saveAlreadyPolled();
            if (onError) {
                onError(onErrorArg);
            }
        });
        req.src = url;
    }
    return false;
}
function showLoginSymbol() {
    $('newPassword').hide();
    $('login').hide();
    $('loginSymbol').show();
    return false;
}
function showLogin(showMsg) {
    $('loginSymbol').hide();
    $('newPassword').hide();
    if (showMsg) {
        $('loginMessage').show();
    } else {
        $('loginMessage').hide();
    }
    $('login').show();
    return false;
}
function showNewPassword(showMsg) {
    var ls = $('loginSymbol');
    if(ls){ls.hide();}
    var l = $('login');
    if(l){l.hide();}
    var npe = $('newpasswordemail');
    if(npe){npe.value = '';}
    var np = $('newPassword'); 
    if(np){np.show();}
    var cpm = $('initPasswordMessage');
    if (cpm) {
        if (showMsg) {
            cpm.select(".err").invoke('hide');
            cpm.show();
            cpm.select(showMsg).invoke('show');
        } else {
            cpm.hide();
        }
    }
    return false;
}
function newPasswordRequest(finishStyle) {
    var baseUrl = buildBaseUrl();
    var path = buildPath();
    var url = baseUrl + '/newpass/' + encodeSafe(loginMyWidgetId) + '/'
            + encodeSafe($F($('newpasswordemail'))) + '/' + encodeSafe(baseUrl)
            + '/' + encodeSafe(path) + '/1,auth,8,1';
    var req = $('newPasswordIFrame');
    req.src = url;
    if (finishStyle == "link") {
        showLoginSymbol();
    } else {
        showLogin();
    }
	alert ("Eine E-Mail zum Setzen eines neuen Passwortes wurde an Sie versendet.");
    return false;
}
function samLogin() {
    var hereEncoded = encodeSafe(buildBaseUrl() + buildPath());
    var there = buildBaseUrl() + '/sam/' + hereEncoded + '/1,auth,8,1';
    document.location.href = there;
    return false;
}
function login() {
    var wtgt = loginWTGT;
    if (!wtgt) {
        wtgt = buildPath();
    }
    var wdycf = buildBaseUrl() + wtgt + ';translate='
            + encodeURIComponent('auth=login') + '?Xhutzzz7=5&txnid=_TXID_';
    $('loginredirecturl').value = wdycf;
    $('loginForm').submit();
    return false;
}
function showChangePassword(showMsg) {
    var cps = $('changePasswordSuccess');
    if(cps){cps.hide();}
    var lob = $('logoutButton');
    if(lob){lob.hide();}
    var cpm = $('changePasswordMessage');
    if(cpm){
        if (showMsg) {
            cpm.select(".err").invoke('hide');
            cpm.show();
            cpm.select(showMsg).invoke('show');
        } else {
            cpm.hide();
        }
    }
    $('changepasswordpass').value = '';
    $('changepasswordnewpass').value = '';
    $('changepasswordnewpassconfirm').value = '';
    $('changePassword').show();
    return false;
}
function showChangePasswordSuccess() {
    $('logoutButton').hide();
    $('changePassword').hide();
    $('changePasswordSuccess').show();
    return false;
}
function showLogoutButton() {
    $('changePasswordSuccess').hide();
    $('changePassword').hide();
    $('logoutButton').show();
    return false;
}
function changePassword() {
    var message = checkPassword($('changePasswordForm'));
    if (message) {
        showChangePassword(message);
        return false;
    }
    var wtgt = buildBaseUrl() + '/result/_TXID_/1,auth,8,1';
    $('changepasswordredirecturl').value = wtgt;
    var req = $('changePasswordIFrame');
    waitOnLoginResult(req, showChangePasswordSuccess, function() {
        showChangePassword(".err.generic");
    });
    $('changePasswordForm').target = 'changePasswordIFrame';
    $('changePasswordForm').submit();
    return false;
}
function waitOnLoginResult(req, success, failure) {
    req
            .observe(
                    'load',
                    function() {
                        req.stopObserving();
                        var tryEvaluate = function(i) {
                            try {
                                if (req.contentWindow
                                        && req.contentWindow.document
                                        && req.contentWindow.document
                                                .getElementById('loginResult')
                                        && req.contentWindow.document
                                                .getElementById('loginResult').firstChild
                                        && req.contentWindow.document
                                                .getElementById('loginResult').firstChild.nodeValue) {
                                    success();
                                } else {
                                    failure();
                                }
                                return false;
                            } catch (e) {
                            }
                            if (10 < i++) {
                                failure();
                            } else {
                                window.setTimeout( function() {
                                    tryEvaluate(i)
                                }, 100);
                            }
                            return false;
                        };
                        tryEvaluate(0);
                    });
    req.observe('error', function() {
        req.stopObserving();
        failure();
    });

}
function logout() {
    document.location.href = buildBaseUrl() + buildPath() + ';translate='
            + encodeURIComponent('auth=logout');
    return false;
}
function newPassword() {
    var message = checkPassword($('newPasswordForm'));
    if (message) {
        showNewPassword(message);
        return false;
    }
    var wdycf = buildBaseUrl() + loginWTGT + ';translate='
            + encodeURIComponent('auth=login') + '?txnid=_TXID_';
    $('newpasswordredirecturl').value = wdycf;
    $('newPasswordForm').submit();
    return false;
}
function initPassword() {
    var message = checkPassword($('initPasswordForm'));
    if (message) {
        showNewPassword(message);
        return false;
    }
    var wdycf = buildBaseUrl() + loginWTGT + ';translate='
            + encodeURIComponent('auth=login') + '?txnid=_TXID_';
    $('initpasswordredirecturl').value = wdycf;
    $('initPasswordForm').submit();
    return false;
}
function checkPassword(form) {
    var passwords = form.select("input[type='password']");
    var l = passwords.length;
    if (l >= 2) {
        if (passwords[l - 2].value.length < 6) {
            return ".err.length";
        }
        if (passwords[l - 2].value != passwords[l - 1].value) {
            return ".err.confirm";
        }
    }
}

