// (c) 2006 Heise Zeitschriften Verlag GmbH & Co KG
// $Id: select_cat.js,v 1.9 2007/06/20 10:24:03 sn Exp $

function changeCheck(a,v) {
    if (document.kat_suche.suchbereich[a].checked == false) {
        document.kat_suche.suchbereich[a].checked = true;
        document.kat_suche.suchbereich[v].checked = false;       
    }
}

function selectCat(i) {
    if (i == 0) {
        var j = 1;
        if (document.kat_suche.kat_auswahl.selectedIndex > 8) {
            document.kat_suche.kat_auswahl.selectedIndex = 0;
	    }
    } else {
        j = 0;
        if (document.kat_suche.kat_auswahl.selectedIndex < 9) {
            document.kat_suche.kat_auswahl.selectedIndex = 9;
	    }
    }
    changeCheck(i,j);        
}

function changeCat() {

    var n = document.kat_suche.kat_auswahl.selectedIndex;
    var x, y;
    var s1 = document.kat_suche.kat_auswahl.options[n].value.charAt(0);
    var s2 = 'a';
    
    if (s1 == s2) {
        x = 0;
        y = 1;	
	} else {
        x = 1;
        y = 0;
    }

    changeCheck(x,y); 
}

// die Veranstaltungskategorie ueber die SelectBox veraendern
function vChangeCat(c) {
    var oldloc = window.location.protocol + '//' + window.location.hostname
               + (window.location.port ? ':' + window.location.port : '') + window.location.pathname;
    var params = window.location.search;
    oldloc = oldloc + "/";
    oldloc = oldloc.replace(/\/\/$/,"/");
    var newloc = oldloc.replace(/kat-\d+\/*/,'');
    if (c) {
        newloc = newloc + "kat-" + c + "/";
    }
    window.location = newloc + params;
}

// die Startzeit ueber die SelectBox veraendern
function vChangeStartZeit(s,e) {
    var oldloc = window.location.protocol + '//' + window.location.hostname
               + (window.location.port ? ':' + window.location.port : '') + window.location.pathname;
    var params = window.location.search;
    oldloc = oldloc + "/";
    oldloc = oldloc.replace(/\/\/$/,"/");
    var newloc = oldloc.replace(/zeitvon-\d+\/*/,'');
    if (s && s > e) {
        alert("Fehler: die Startzeit muss vor der Endzeit liegen.");
        s = 0;
    }
    if (s && s <= e) {
        newloc = newloc + "zeitvon-" + s + "/";
    }
    window.location = newloc + params;
}

// die Endzeit ueber die SelectBox veraendern
function vChangeEndZeit(e,s) {
    var oldloc = window.location.protocol + '//' + window.location.hostname
               + (window.location.port ? ':' + window.location.port : '') + window.location.pathname;
    var params = window.location.search;
    oldloc = oldloc + "/";
    oldloc = oldloc.replace(/\/\/$/,"/");
    var newloc = oldloc.replace(/zeitbis-\d+\/*/,'');
    if (e && e < s) {
        alert("Fehler: die Startzeit muss vor der Endzeit liegen.");
        e = 24;
    }
    if (e && e >= s) {
        newloc = newloc + "zeitbis-" + e + "/";
    }
    window.location = newloc + params;
}

//  #   #   #   #   #   #   #
// HTTP.Request 0.03 von OpenJSAN.org
//  #   #   #   #   #   #   #

if ( typeof( Method ) == "undefined" ) {
    Method = {};
}

if ( typeof( Method["bind"] ) == "undefined" ) {
    Method.bind = function ( method, object ) {
        return function() {
            method.apply(object, arguments);
        }
    };
}

if ( typeof( HTTP ) == "undefined" ) {
    HTTP = {};
}

if ( typeof( HTTP.Request ) == "undefined" ) {
    HTTP.Request = function ( options ) {
        if ( !options ) options = {};

        this.options = {};
        for ( var i in options ) {
            this.setOption( i, options[i] );
        }

        if ( this.getOption( "method" ) == undefined ) {
            this.setOption( "method", "post" );
        }

        if ( this.getOption( "asynchronous" ) == undefined ) {
            this.setOption( "asynchronous", true );
        }

        if ( this.getOption( "parameters" ) == undefined ) {
            this.setOption( "parameters", "" );
        }

        if ( this.getOption( "transport" ) == undefined ) {
            this.setOption( "transport", HTTP.Request.Transport );
        }

        if ( this.getOption( "uri" ) )
            this.request();
    };

    HTTP.Request.EventNames = [
        "uninitialized"
       ,"loading"
       ,"loaded"
       ,"interactive"
       ,"complete"
    ];

    HTTP.Request.prototype.getOption = function( name ) {
        if ( typeof( name ) != "string" ) {
            return;
        }
        return this.options[name.toLowerCase()];
    };

    HTTP.Request.prototype.setOption = function( name, value ) {
        if ( typeof( name ) != "string" ) {
            return;
        }

        name = name.toLowerCase();

        this.options[name] = value;

        if ( name == "method" ) {
            if ( ! ( this.options.method == "get" || this.options.method == "post" || this.options.method == "head" ) ) {
                this.options.method = "post";
            }
        }

        if ( name == "transport" ) {
            if ( typeof( value ) != "function" ) {
                this.options.transport = HTTP.Request.Transport;
            }
        }
    };

    HTTP.Request.prototype.request = function ( uri ) {
        if ( ! uri ) uri = this.getOption( "uri" );
        if ( ! uri ) return;

        var parameters = this.getOption( "parameters" );
        // XXX Why?
        if (parameters.length > 0) parameters += "&_=";

        var method = this.getOption( "method" );
        if ( method == "get" ) {
            uri += "?" + parameters;
        }

        this.transport = new (this.getOption( "transport" ))();

        var async = this.getOption( "asynchronous" );
        this.transport.open( method ,uri ,async );

        if ( async ) {
            this.transport.onreadystatechange = Method.bind(
                this.onStateChange, this
            );

            setTimeout(
                Method.bind(
                    function() { this.respondToReadyState(1) }
                   ,this
                )
               ,10
           );
        }

        this.setRequestHeaders();

        if ( method == "post" ) {
            var body = this.getOption( "postbody" );
            if ( ! body ) body = parameters;

            this.transport.send( body );
        }
        else {
            this.transport.send( null );
        }
    };

    HTTP.Request.prototype.setRequestHeaders = function() {
        this.transport.setRequestHeader( "X-Requested-With", "HTTP.Request" );
        this.transport.setRequestHeader( "X-HTTP-Request-Version", HTTP.Request.VERSION );

        if (this.getOption( "method" ) == "post") {
            this.transport.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );

            /* Force "Connection: close" for Mozilla browsers to work around
             * a bug where XMLHttpReqeuest sends an incorrect Content-length
             * header. See Mozilla Bugzilla #246651.
             */
            if (this.transport.overrideMimeType) {
                this.transport.setRequestHeader( "Connection", "close" );
            }
        }

/* TODO Add support for this back in later
        if (this.options.requestHeaders)
            requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);
*/
    };

    // XXX This confuses me a little ... how are undefined and 0 considered a success?
    HTTP.Request.prototype.isSuccess = function () {
        return this.transport.status == undefined
            || this.transport.status == 0
            || (this.transport.status >= 200 && this.transport.status < 300);
    };

    HTTP.Request.prototype.onStateChange = function() {
        var readyState = this.transport.readyState;
        if (readyState != 1) {
            this.respondToReadyState( this.transport.readyState );
        }
    };

    HTTP.Request.prototype.respondToReadyState = function( readyState ) {
        var event = HTTP.Request.EventNames[readyState];

        if (event == "complete") {
            var func = this.getOption( "on" + this.transport.status );
            if ( ! func ) {
                if ( this.isSuccess() ) {
                    func = this.getOption( "onsuccess" );
                }
                else {
                    func = this.getOption( "onfailure" );
                }
            }

            if ( func ) {
                ( func )( this.transport );
            }
        }

        if ( this.getOption( "on" + event ) )
            ( this.getOption( "on" + event ) )( this.transport );

        /* Avoid memory leak in MSIE: clean up the oncomplete event handler */
        if (event == "complete") {
            this.transport.onreadystatechange = function (){};
        }
    };

    HTTP.Request.VERSION = 0.03;
}

if ( typeof( HTTP.Request.Transport ) == "undefined" ) {
    if ( window.XMLHttpRequest ) {
        HTTP.Request.Transport = window.XMLHttpRequest;
    }
    // This tests for ActiveXObject in IE5+
    else if ( window.ActiveXObject && window.clipboardData ) {
        var msxmls = new Array(
            "Msxml2.XMLHTTP.5.0"
           ,"Msxml2.XMLHTTP.4.0"
           ,"Msxml2.XMLHTTP.3.0"
           ,"Msxml2.XMLHTTP"
           ,"Microsoft.XMLHTTP"
        );
        for ( var i = 0; i < msxmls.length; i++ ) {
            try {
                new ActiveXObject(msxmls[i]);
                HTTP.Request.Transport = function () {
                    return new ActiveXObject(msxmls[i]);
                };
                break;
            }
            catch(e) {
            }
        }
    }

    if ( typeof( HTTP.Request.Transport ) == "undefined" ) {
        // This is where we add DIV/IFRAME support masquerading as an XMLHttpRequest object
    }

    if ( typeof( HTTP.Request.Transport ) == "undefined" ) {
        throw new Error("Unable to locate XMLHttpRequest or other HTTP transport mechanism");
    }
}


//  #   #   #   #   #   #   #
// Ajax Routinen
// (c) 2006 Heise Zeitschriften Verlag GmbH & Co KG
//  #   #   #   #   #   #   #

if (window.XMLHttpRequest || 
    (window.ActiveXObject && window.clipboardData)) {
    // faehiger Browser, Cookie entsprechend setzen
    document.cookie = 'heise-treff-acceptcookie=ajax' + ';path=/';
}

var req, captcha_id;

function get_captcha () {
    if (captcha_id)
        return;
    req = new HTTP.Request({
        uri: '/',
        postbody: 'rm=ajaxcaptcha',
        onSuccess: function (trans) {
            captcha_id = trans.responseText;
            // in hidden form field eintragen
            var captcha_hidden = document.getElementById('captcha_session');
            captcha_hidden.value = captcha_id;
            // captcha bild laden
            var captcha_bild = document.getElementById('captcha_img');
            captcha_bild.onerror = function () {
                alert("Das Captcha-Bild konnte nicht geladen werden.\n" +
                    "Bitte laden Sie die Seite neu und versuchen Sie es " +
                    "erneut.");
            }
            captcha_bild.src='/?rm=showcaptcha&CGISESSID=' + captcha_id;
        },
        onFailure: function () {
            alert("XMLHttpRequest Problem beim Captcha.\n" +
                  "Bitte laden Sie die Seite neu und versuchen Sie es " +
                  "erneut.");
            }
    });
}


// Bei Veranstaltungen Minuten mit Nullen vorbelegen, wenn Stunde vorhanden
function v_zero_fill(hour_field) {
    var minute_field_id = hour_field.id.replace( /_stunde$/, '_minute' ); // Zugehoeriges Minutenfeld
    var minute_field = document.getElementById(minute_field_id);
    if ( hour_field.value.length > 0 && minute_field.value.length == 0 ) {
        minute_field.value = '00';
    }
}

//***
//*** Cookies komfortabel nutzen
//***

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

// Aus Framesets befreien
if (window != top) top.location.href = location.href;

// vim:tabstop=4:shiftwidth=4:expandtab:
//eof
