﻿



var PopupHtmlWriter = new Class({
    Implements: Options,
    options: {
        ressource: null,
        roomobj: null,
        eventobj: null

    },
    initialize: function(options) {
        this.setOptions(options);
    },


    createHtml: function() {
        if (this.options.ressource != null) {

            this.options.ressource.DescriptionHTML = this.options.ressource.DescriptionHTML.replace(/\r\n/gi, "<br/>");

            if (this.options.ressource.Pictures.length > 0) {
                var imgcode = "";

                for (var i = 0; i < this.options.ressource.Pictures.length; i++) {
                    imgcode += "<img src=\"" + g_urlresizepath + "?w=200&h=0&url=" + this.options.ressource.Pictures[i].ImageUrl + "\" class=\"listimg\"><br/>";
                }

                return "<table><tr><td valign=\"top\" style=\"width:100%\">" + this.options.ressource.DescriptionHTML + "</td><td  valign=\"top\">" + imgcode + "</td></tr></table>";

            } else {
                return this.options.ressource.DescriptionHTML;
            }

        }

        return "";
    },

    writeEventDetail: function() {
        if (this.options.eventobj != null) {

            var html = "";

            this.options.eventobj.Description = this.options.eventobj.Description.replace(/\r\n/gi, "<br/>");

            if (this.options.eventobj.LastsAllDay == true) {
                html += "<div style=\"font-weight:bold\">" + formatDate(this.options.eventobj.StartDate, true) + " &nbsp; &nbsp; ganztags</div>";
            } else {
                html = "<table>";
                html += "<tr><td width=40px><b>Von</b></td><td width=140px><b>" + formatDate(this.options.eventobj.StartDate, true) + "</b></td><td><b>" + formatTime(this.options.eventobj.StartDate) + " Uhr</b></td></tr>";
                html += "<tr><td><b>Bis</b></td><td><b>" + formatDate(this.options.eventobj.EndDate, true) + "</b></td><td><b>" + formatTime(this.options.eventobj.EndDate) + " Uhr</b></td></tr>";
                html += "</table>";
            }


            html += "<div style=\"margin-top:12px\">" + this.options.eventobj.Description + "</div>";


            /*
            for (var i = 0; i < this.options.eventobj.Rooms.length; i++)
            html += this.options.eventobj.Rooms[i].Name + "<br>";
            */
            return html;
        }

        return "";
    }
    ,

    writeRoomDetail: function(room) {

        if (room == null)
            room = this.options.rooomobj;

        if (room != null) {

            var html = "<table>";

            if (room.Street.length > 0)
                html += "<tr><td>Adresse:&nbsp; " + room.Street + " </td></tr>";

            html += "<tr><td>Fläche:&nbsp; " + room.Capacity + " m²</td></tr>";
            html += "<tr><td>" + room.HTML.replace(/\r\n/gi, "<br/>") + "</td></tr>";
          
            if (room.Pictures.length > 0) {

                var imgcode = "";

                for (var i = 0; i < room.Pictures.length; i++) {
                    imgcode += "<img src=\"" + g_urlresizepath + "?w=100&h=0&url=" + room.Pictures[i].ImageUrl + "\" class=\"listimg\"><br/>";
                }

                html += imgcode;

            }

            html += "</table>";


            return html;
        }

        return "";
    }


});

var ConfirmationPopup = new Class({
    Implements: Options,
    options: {
        headline: "&nbsp;",
        question: "&nbsp;",
        questionstyle: "text-align:center",
        onYesClick: null,
        onNoClick: null,
        ydelta: 0,
        xdelta: 0
    },
    initialize: function(dockelem, options) {
        this.setOptions(options);

        this.dockelem = dockelem

        var html = "<div class=\"question\" style=\"" + this.options.questionstyle + "\">" + this.options.question + "</div>";

        var btyes = new Element("img", { "class": "btyes", "src": "../grafik/icons/btn_ja_off.gif" });
        var btno = new Element("img", { "class": "btno", "src": "../grafik/icons/btn_nein_off.gif" });


        btyes.addEvent("mouseenter", function(ev) {
            ev.target.src = "../grafik/icons/btn_ja_on.gif";
        });

        btyes.addEvent("mouseleave", function(ev) {
            ev.target.src = "../grafik/icons/btn_ja_off.gif";
        });

        btno.addEvent("mouseenter", function(ev) {
            ev.target.src = "../grafik/icons/btn_nein_on.gif";
        });

        btno.addEvent("mouseleave", function(ev) {
            ev.target.src = "../grafik/icons/btn_nein_off.gif";
        });

        btyes.store("cpop", this);
        btno.store("cpop", this);

        btyes.addEvent("click", function(ev) {
            var cpop = ev.target.retrieve("cpop");

            if (cpop.options.onYesClick != null)
                cpop.options.onYesClick(cpop.dockelem);
        });

        btno.addEvent("click", function(ev) {
            var cpop = ev.target.retrieve("cpop");

            if (cpop.options.onNoClick != null)
                cpop.options.onNoClick();

            cpop.popup.clear();
        });


        this.popup = new mooPopup({ dockelem: dockelem, xdelta: this.options.xdelta, ydelta: this.options.ydelta, dockelempos: 'center', headline: this.options.headline, content: html });

        this.popup.addEvent("OnPopupCleared", this.onPopupCleared.bind(this));

        var td = this.popup.popupcon.getElement('#p_inhalt');

        td.appendChild(btno);
        td.appendChild(btyes);

    },

    onPopupCleared: function() {
  
        if (this.options.onNoClick != null)
            this.options.onNoClick();

  
    }
});

var LoginPopup = new Class({
    Implements: [Options, Events],
    options: {
        headline: "&nbsp;",
        question: "&nbsp;",
        questionstyle: "text-align:center",
        onLoggedIn: null
    },
    initialize: function(dockelem, options) {

        this.setOptions(options);

        this.dockelem = dockelem

        var html = "<div class=\"question\" style=\"" + this.options.questionstyle + "\">" + this.options.question + "</div>";

        html += "<table style=\"margin:5px 0px 0px 0px;\">";
        html += "<colgroup>";
        html += "    <col width=\"90px\" />";
        html += "    <col width=\"*\" />";
        html += "</colgroup>";
        html += "<tr>";
        html += "    <td align=\"left\">Benutzername</td>";
        html += "    <td><input type=\"text\" id=\"txtUser\"  class=\"textbox\" style=\"width:160px\"></input></td>";
        html += "</tr>";
        html += "<tr>";
        html += "    <td align=\"left\">Passwort</td>";
        html += "    <td><input ID=\"txtPass\" class=\"textbox\" style=\"width:160px\" type=\"password\"></input></td>";
        html += "</tr>";
        html += "<tr>";
        html += "    <td>&nbsp;</td>";
        html += "    <td style=\"text-align:right\"><div class=\"link\" id=\"divSend\">Anmelden</a></td>";
        html += "</tr>";
        html += "<tr>";
        html += "    <td colspan=\"2\" style=\"text-align:right;padding:25px 0px 0px 0px\"><a href=\"registration.aspx\">Ich bin noch nicht registriert</a><br /><a href=\"passlost.aspx\">Ich habe mein Passwort vergessen</a></td>";
        html += "</tr>";
        html += "</table>";

        this.popup = new mooPopup({ dockelem: dockelem, dockelempos: 'center', headline: this.options.headline, content: html, closeexisting: false });


        var divSend = this.popup.popupcon.getElement(' #divSend');
        // var divCancel = this.popup.popupcon.getElement('#p_inhalt #divCancel');

        divSend.addEvent("click", this.onLoginClick.bind(this));
        // divCancel.addEvent("click", this.onCancelClick.bind(this));


    },

    onLoginClick: function(e) {
        var ev = new Event(e);

        var txtuser = $('txtUser');
        var txtpass = $('txtPass');

        if (txtuser == null || txtpass == null)
            return;

        var jsonRequest = new Request.JSON({ url: "../ajax/ajaxservice.aspx?action=doLogin&username=" + txtuser.value + "&passwort=" + txtpass.value + "&y=" + new Date().getMilliseconds(), onComplete: this.onLoginResponse.bind(this)}).send();    
        
    },


    onLoginResponse: function(res) {
        
        if (res.login == true) {
            this.popup.clear();
            setUserLoggedIn(res.userid);
            this.fireEvent("OnLoginPassed");
        } else {
            alert("Falscher Benutzername oder Passwort. \r\n \r\n Benutzerzugänge sind frühestens 24 Stunden nach der Registrierung aktiv.");
        }
    }

    
});


/* erhält session */

var sk_count = 0; //Number of Reconnects
var sk_max = 50; //Maximum reconnects setting

function Reconnect() {

    sk_count++;
    if (sk_count < sk_max) {
        window.status = 'Session Refreshed ' + sk_count.toString() + ' time(s)';

        var img = new Image(1, 1);
        img.src = '../refreshsession.aspx?xxx=' + escape(new Date().getMilliseconds());

    }
}


function isUserLoggedIn() {

    var hcid = $('ctl00_hCustomerId');

    if (hcid != null)
        if (hcid.value != "" && hcid.value != "0")
            return true;

        return false;
}

function setUserLoggedIn(userid) {
 var hcid = $('ctl00_hCustomerId');

 if (hcid != null)
     hcid.value = userid;
}

function getTimeDiff(date1, date2) { dt1 = new Date(date1); dt2 = new Date(date2); var diff; if (dt1 > dt2) { diff = new Date(dt1 - dt2); } else { diff = new Date(dt2 - dt1); } return diff; }

/* 27.08.2009 */
function formatDate(date, full) {
    if (full == true)
        return  cutText(getWeekname(date.getDay()), 2) + ", " + preZero(date.getDate()) + ". " + cutText(getMonthname(date.getMonth()), 3) + ". " + date.getFullYear();
    else
        return preZero(date.getDate()) + "." + preZero(date.getMonth() + 1) + "." + date.getFullYear();
}

/* 17:30 */
function formatTime(date) {
    return preZero(date.getHours()) + ":" + preZero(date.getMinutes());
}


/* Januar */
function getMonthname(month)
{
    switch(month){
        case 0: return "Januar";
        case 1: return "Februar";
        case 2: return "März";
        case 3: return "April";
        case 4: return "Mai";
        case 5: return "Juni";
        case 6: return "Juli";
        case 7: return "August";
        case 8: return "September";
        case 9: return "Oktober";
        case 10: return "November";
        case 11: return "Dezember";
    }
    
    return "";
}

/* Montag */
function getWeekname(weekday)
{
    switch(weekday){
        case  0: return "Sonntag";
        case 1: return "Montag";
        case 2: return "Dienstag";
        case 3: return "Mittwoch";
        case 4: return "Donnerstag";
        case 5: return "Freitag";
        case 6: return "Samstag";
        case 7: return "Sonntag";
    }
    
    return "";
}

/* 7 -> 07 */
function preZero(nr) {
    if (nr.toString().length == 1)
        return "0" + nr;

    return nr;
}

/* cut text */
function cutText(text, maxchars) {
    if (text.length > maxchars)
        return text.substring(0,maxchars);
       
    return text;
}

/* verzögerter alert */
function dalert(msg) {
    window.setTimeout("alert('" + msg + "');", 500);
}

/* verzögerter alert mit eigener zeitangabe */
function dalert(msg, ms) {
    window.setTimeout("alert('" + msg + "');", ms);
}


// mouseover effekt bei buttons/bildern
var isOutActive = true;

function overButton(img) {
    if (img != null) {
        img.src = img.src.toLowerCase().replace("_off.gif", "_on.gif");
    }
}

function outButton(img) {
    if (img != null && isOutActive) {
        img.src = img.src.toLowerCase().replace("_on.gif", "_off.gif");
    }
}

// mouseover effekt bei tr-zeilen

var lastbgcolor;

function overRow(tr) {
    if (tr != null) {
        lastbgcolor = tr.style.backgroundColor;
        tr.style.backgroundColor = '#C9E8FF';

        for (var i = 0; i < tr.childNodes.length; i++) {
            var td = tr.childNodes[i];
            td.style.backgroundColor = '#C9E8FF';

        }
    }
}

function outRow(tr) {
    if (tr != null && isOutActive) {
        tr.style.backgroundColor = lastbgcolor;

        for (var i = 0; i < tr.childNodes.length; i++) {
            var td = tr.childNodes[i];
            td.style.backgroundColor = "";
        }
    }
}
