﻿// -----------------------------------------------
// WebUI.js
// ©2008, Peter Laws p.laws@relaix.info
// -----------------------------------------------

var ca = null;
var contentObjectId = "content";
var loginInfo = "logininfo";
var userName = "";
var password = "";
var onlinePool = "";

window.onload = function() {
    start();
}

function start() {

    Type.registerNamespace('WebUI');

    WebUI.Panel = function (id, pid) {

        var _id = id;
        var _pid = pid;
        var _p = $get(pid);

        this.id = function () { return _id; }
        this.pid = function () { return _pid; }

        // -------------------------------------
        //  init menu customer area
        // -------------------------------------

        this.initMCAComplete = function (result) {
            if (result[0] == "ERROR") {
                alert("ERROR " + result[1]);
            } else {
                _p.innerHTML = result[1];
            }
        }

        this.initMCATimeout = function (result) {
            //window.alert("initMCA Timeout " + result);
        }

        this.initMCAError = function (result) {
            window.alert("initMCA Error " + result);
        }

        this.initMCA = function () {
            WebServiceAccess.InitMCA(
                this.initMCAComplete,
                this.initMCATimeout,
                this.initMCAError
                );
        }

        // -------------------------------------
        //  login
        // -------------------------------------

        this.loginComplete = function (result) {
            if (result[0] == "Error") {
                alert("Fehler: " + result[1]);
            } else if (result[0] == "Info") {
                var loginInfoObj = $get(loginInfo);
                loginInfoObj.innerHTML = result[1];
            } else if (result[0] == "OK") {
                onlinePool = result[1];
                /*
                Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback(OnLoginCompleted);
                Sys.Services.AuthenticationService.set_defaultLogoutCompletedCallback(OnLogoutCompleted);
                Sys.Services.AuthenticationService.set_defaultFailedCallback(OnLoginFailed);
                Sys.Services.AuthenticationService.login(userName, password, false, null, null, null, null, "User Context");
                */

		//window.alert(userName);

                if (onlinePool == "True")
                    document.location.href = "Authenticate.aspx?ReturnUrl=pool.aspx&userName=" + userName;
                else
                    document.location.href = "Authenticate.aspx?ReturnUrl=" + document.URL + "&userName=" + userName;

                userName = "";
                password = "";

/*
                if (onlinePool == "True")
                    document.location.href = "pool.aspx";
                else
                    document.location.href = document.URL;
*/
            }
        }

        this.loginTimeout = function (result) {
            //window.alert("login Timeout " + result);
        }

        this.loginError = function (result) {
            window.alert("login Error " + result);
        }

        this.login = function (userName, password) {
            WebServiceAccess.Login(
                userName,
                password,
                this.loginComplete,
                this.loginTimeout,
                this.loginError
                );
        }

        // -------------------------------------
        //  logout
        // -------------------------------------

        this.logoutComplete = function (result) {
            if (result[0] == "Error") {
                alert("Error " + result[1]);
            } else {
                onlinePool = "";
                document.location = "Default.aspx";
                //eval(id + ".initMCA()");
            }
        }

        this.logoutTimeout = function (result) {
            //window.alert("login Timeout " + result);
        }

        this.logoutError = function (result) {
            window.alert("login Error " + result);
        }

        this.logout = function () {
            WebServiceAccess.Logout(
                this.logoutComplete,
                this.logoutTimeout,
                this.logoutError
                );
        }
    }

    initPanels();
}

function OnLoginCompleted(validCredentials,userContext,methodName) {
    if (validCredentials == true) {
        if (onlinePool == "True")
            document.location.href = "pool.aspx";
         else
            document.location.href = document.URL;
    } else {
        window.alert("Beim Login ist ein technisches Problem aufgetreten.\n\nBitte informieren Sie umgehend unseren technischen Support unter\n\n+49 (0241) 36925.");
    }
}

function OnLogoutCompleted(result) {
    ;
}

function OnLoginFailed(error,userContext,methodName) {
    window.alert(error.get_message() + " | " + error.get_timedOut() + " | " + error.get_statusCode());
}

function initPanels() {
    ca = new WebUI.Panel("ca","CustomerArea");
    ca.initMCA();
}

function login(userNameObjectId, passwordObjectId) {
    if (ca == null) {
        alert("Error in WebUI/login(): object customer area not initialized. Login process aborted.");
        return false;
    } else {
        var userNameObj = $get(userNameObjectId);
        var passwordObj = $get(passwordObjectId);
        if(userNameObj == null || passwordObj == null) {
            alert("Error in WebUI/login(): username/password objects not found. Login process aborted.");
            return false;
        } else {
            userName = new String(userNameObj.value);
            userName = userName.trim();
            if(userName == "") {
                return false;
            } else {
                password = new String(passwordObj.value);
                password = password.trim();
                if (password == "") {
                    return false;
                } else {
                    ca.login(userName, password);
                    return false;
                }
            }        
        }
    }
}

function logout() {
    if (ca == null) {
        alert("Error in WebUI/logout(): object customer area not initialized. Login process aborted.");
    } else {
        //Sys.Services.AuthenticationService.logout(null,null,null,null);
        ca.logout();
    }
}

// -------------------------------------
//  Registration
// -------------------------------------

registrationComplete = function(result) {
    if (result[0] == "Error") {
        alert("Error " + result[1]);
    } else {
        var contentObj = $get(contentObjectId);
        contentObj.innerHTML = result[1];
    }
}

registrationTimeout = function(result) {
    window.alert("registration Timeout " + result);
}

registrationError = function(result) {
    window.alert("registration Error " + result);
}

registrationForm = function() {
    WebServiceAccess.Registration(
        this.registrationComplete,
        this.registrationTimeout,
        this.registrationError
        );
}


// -------------------------------------
//  Helper
// -------------------------------------

function checkKeycode(e) {
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    return keycode;
}

function invokeSave(objId) {
    var _o = eval(objId);
    var _d = _o.d();
    var _t = _o.tid();

    var txtFields = _d.getElementsByTagName('input');
    var selFields = _d.getElementsByTagName('select');
    var tarFields = _d.getElementsByTagName('textarea');

    var mrtf = false;
    var savearray = new Array();

    var k = 0;

    for(var i=0; i<txtFields.length; i++) {
        var txtFieldType = new String(txtFields[i].type);
        if (txtFieldType.toLowerCase() != "hidden") {
            var value = new String(txtFields[i].value);
            value = value.trim();
            txtFields[i].value = value;
            var fieldIds = txtFields[i].id.split("_");
            var name = txtFields[i].name.split("_");
            if (name[0] == "rtxt" || name[0] == "txt")
                ctxt(txtFields[i]);
            if (name[0] == "rdate" || name[0] == "date")
                cdate(txtFields[i]);
            if (name[0] == "rint" || name[0] == "int")
                cint(txtFields[i]);
            savearray[k] = new Array(2);
            savearray[k][0] = fieldIds[1];
            if (txtFields[i].type == "checkbox")        
                savearray[k][1] = txtFields[i].checked;
            else
                savearray[k][1] = txtFields[i].value;            
            k++;
            if (txtFields[i].className == "required" || txtFields[i].className == "alert") {
                if (txtFields[i].value == "" || ((name[0] == "rdate" || name[0] == "rint") && txtFields[i].className == "alert")) {
                    txtFields[i].className = "alert";
                    mrtf = true;
                } else {
                    if (name[0] == "rtxt" || name[0] == "txt")
                        ctxt(txtFields[i]);
                    if (name[0] == "rdate" || name[0] == "date")
                        cdate(txtFields[i]);
                    if (name[0] == "rint" || name[0] == "int")
                        cint(txtFields[i]);
                        
                    if (txtFields[i].className == "alert")
                        mrtf = true;
                    else
                        txtFields[i].className = "required";
                }
            }
        }
    }

    for(var i=0; i<tarFields.length; i++) {
        var value = new String(tarFields[i].innerHTML);
        var fieldIds = tarFields[i].id.split("_");
        savearray[k] = new Array(2);
        savearray[k][0] = fieldIds[1];
        savearray[k][1] = tarFields[i].innerHTML;
        k++;
        if (tarFields[i].className == "required" || tarFields[i].className == "alert") {
            if (tarFields[i].value == "") {
                tarFields[i].className = "alert";
                mrtf = true;
            } else {
                tarFields[i].className = "required";
    }   }   }

    var mrsf = false;
    var selection = false;

    for(var i=0; i<selFields.length; i++) {
        //window.alert(selFields[i].id);
        var fieldIds = selFields[i].id.split("_");
        selection = false;
        for(var j=0; j<selFields[i].options.length; j++) {
            if (selFields[i].options[j].selected) {
                savearray[k] = new Array(2);
                savearray[k][0] = fieldIds[1];
                savearray[k][1] = selFields[i].options[j].value;
                k++;
                selection = true;
                if (selFields[i].className == "alert")
                    selFields[i].className = "required";
        }   }
        if (selection == false) {
            if (selFields[i].className == "required" || selFields[i].className == "alert") {
                selFields[i].className = "alert";
                mrsf = true;
            } else {
                //window.alert(fieldIds[1] + " no selection");
                savearray[k] = new Array(2);
                savearray[k][0] = fieldIds[1];
                savearray[k][1] = "";
                k++;
            }
        }
    }

    // Pflichtfeld
    if (mrtf == true || mrsf == true)
    {
        alert("Bitte füllen Sie alle Pflichtfelder aus oder korrigieren Sie ihre Eingaben.");
        return;
    }
    else
    {
        _o.setPropertyData(savearray);
    }
}


