// =============================================================================
//                                 AJAX SUPPORT
// =============================================================================

if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
  throw new Error( "This browser does not support XMLHttpRequest." )
};


Request = function ( url, init )
{
    this.callback = (init) ? init.callback : null;
    this.request = null;
    this.params = (init && init.params) ? init.params : [];
    this.async = (init) ? init.async : true;
    this.url = url;


    this.Execute = function ( )
    {
        var body = '';
        if ( this.params )
        {
            var count = 0;
            for ( key in this.params )
            {
                if ( count == 0 )
                    body += "&";
                body += escape(key) + "=" + escape(this.params[key]);          
            }
        }

        this.request = new XMLHttpRequest ( );
        this.request.open ( "POST", this.url, this.async );
        this.request.setRequestHeader ( "Content-Type", "application/x-www-form-urlencoded" );
        this.request.setRequestHeader ( "Content-Length", body.length );
        this.request.setRequestHeader ( "Connection", "close" );
        
        var req = this.request;
        var self = this;
        if ( this.async )
        {
            this.request.onreadystatechange = function ( ) { if ( req.readyState == 4 ) { self.callback(self); } };
            this.request.send ( body );
        }
        else
        {
            this.request.send ( body );
            return ( this.request.responseText );                
        }
    };
    
    
    
    this.GetJsonObjects = function ( )
    {
        var text = this.GetResponseText ( );
        if ( text == "" )
            return ( null );
        else            
            return ( Core.Json.Evaluate(text) ); 
    };
    
    
    this.GetResponseText = function ( )
    {
        return ( this.request.responseText );
    };
};


// =============================================================================
//                                 NAVIGATION
// =============================================================================


function ToggleNav ( id, rel, lv )
{
    var img = document.getElementById ( 'nav_img_' + id );
    var container = document.getElementById ( 'nav_con_' + id );
    
    if ( img && container )
    {
        if ( container.style.display == 'none' )
        {
            container.style.display = 'block';
            img.src = rel + 'images/nav' + lv + '_minus.gif';
        }
        else
        {
            container.style.display = 'none';
            img.src = rel + 'images/nav' + lv + '_plus.gif';
        }
    }
}


// =============================================================================
//                        FAVORITES AND SHOPPING BASKET
// =============================================================================


function HT_AddFavorites ( id )
{
    var req = new Request ( relPath + 'favorites.php?action=add', {'params': {'id': id}} );
    req.Execute ( );
    
    location.href = relPath + 'merkliste.html';
}


function HT_RemoveFavorites ( id )
{
    var req = new Request ( relPath + 'favorites.php?action=remove', {'params': {'id': id}} );
    req.Execute ( );
    
    var elem = document.getElementById ( 'fav_' + id );
    if ( elem )
        elem.parentNode.removeChild ( elem );
}


function HT_Basket_Submit_State_Changed ( input )
{
    if ( input.checked )
        document.getElementById('basket_submit').disabled = false;
    else
        document.getElementById('basket_submit').disabled = true;
}



// =============================================================================
// FORM CHECKING
// =============================================================================

Core = function ( ) {};
Core.Form = function ( ) { };

Core.Form.CheckEmail = function ( email ) 
{
    var usr    = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
    var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
    var regex  = "^" + usr + "\@" + domain + "$";
    
    var rgx = new RegExp(regex);
    return rgx.exec(email) ? true : false;
}


Core.Form.CheckEmpty = function ( text )
{
    return ( text && text.replace(/\s+/g, "") != "" );
}



Core.Form.CheckOrderForm = function ( form )
{
    var firstname = Core.Form.CheckEmpty(Core.Form.GetValue(form, 'firstname'));
    Core.Form.SetValid ( 'firstname', firstname );
    var surname = Core.Form.CheckEmpty(Core.Form.GetValue(form, 'surname'));
    Core.Form.SetValid ( 'surname', surname );
    var street = Core.Form.CheckEmpty(Core.Form.GetValue(form, 'street'));
    Core.Form.SetValid ( 'street', street );
    var postalcode = Core.Form.CheckEmpty(Core.Form.GetValue(form, 'postalcode'));
    Core.Form.SetValid ( 'postalcode', postalcode );
    var city = Core.Form.CheckEmpty(Core.Form.GetValue(form, 'city'));
    Core.Form.SetValid ( 'city', city );
    
    var email = Core.Form.CheckEmail(Core.Form.GetValue(form, 'email'));
    Core.Form.SetValid ( 'email', email );
    
    var valid = firstname && surname && street && postalcode && city && email;
    if ( !valid )
    {
        var elem = document.getElementById ( "val_notice" );
        if ( elem )
            elem.style.display = "block";
    }
    
    if ( valid )
        return ( true );
    else
        return ( false );
}



Core.Form.GetValue = function ( form, field )
{
    return ( form.elements["ht_"+field].value );
}


Core.Form.SetValid = function ( field, value )
{
    var elem = document.getElementById ( "val_" + field );
    if ( elem )
    {
        while ( elem.firstChild )
            elem.removeChild ( elem.firstChild );
            
        var img = new Image();
        if ( value )
            img.src = relPathStatic + "/images/valid_true.gif";
        else
            img.src = relPathStatic + "/images/valid_false.gif";
            
        elem.appendChild ( img );        
    }
}