//----------------------------------------------------------------------
HandhavingsNetwerkTool = function ( form, container, imagemapUrl, resultContainer ) {
    // IE < 6 is not supported and will not work.
    if ( YAHOO.env.ua.ie && YAHOO.env.ua.ie < 6 ) {
        return null;
    }

    // Fetch DOM elements
    this.form               = YAHOO.util.Dom.get( form      );
    this.container          = YAHOO.util.Dom.get( container );
    this.resultContainer    = resultContainer ? YAHOO.util.Dom.get( resultContainer ) : null;

    // Fetch image map
    this.fetchImagemap( imagemapUrl );
};

//----------------------------------------------------------------------
HandhavingsNetwerkTool.prototype.applyMap = function ( ) {
    // This only works if the elements that are to be replaced are inputs, which they are in this case.
    this.formVarName = this.container.getElementsByTagName( 'INPUT' )[0].name;

    // Replace container contents with the image map.
    this.container.innerHTML = this.imagemap;

    // Fetch all area elements from the image map, and attach onClick handlers to them
    var areas = this.container.getElementsByTagName( 'AREA' );
    for ( var i = 0; i < areas.length; i++ ) {
        YAHOO.util.Event.addListener( areas[i], 'click', this.areaClickHandler, this, true );
    }

};

//----------------------------------------------------------------------
HandhavingsNetwerkTool.prototype.areaClickHandler = function ( e ) {
    var area = YAHOO.util.Event.getTarget( e );

    // Fetch value from area id property.
    var value = area.id.replace( /^.+_/, '' );

    // Add value to the form as a hidden form var...
    // We cannot use this.form[ this.formVarName ] because aparently 
    // IE < 8 doesn't update the form object when inputs are dynamically added.
    var elem = null;
    for ( var i = 0; i < this.form.elements.length; i++ ) {
        if ( this.form.elements[i].name === this.formVarName ) {
            elem = this.form.elements[i];
        }
    }

    //Create element is it doesn't exist yet.
    if ( ! elem ) {
        elem        = document.createElement( 'INPUT' );
        elem.type   = 'hidden';
        elem.name   = this.formVarName;
        this.form.appendChild( elem );
    }
    elem.value  = value;

    // Finally submit the form.
    if ( this.resultContainer && this.resultContainer.id ) {
        // We have a results container, so let's use ajax to fetch the data.
        var callback = {
            scope   : this,
            success : function ( o ) {
                var div = document.createElement( 'DIV' );         
                // Explicitly strip all body tags and everything around it.
                // This is to avoid an IE8 issue
                div.innerHTML = o.responseText
                    .replace(/[\r\n]/g, ' ')
                    .replace( /^.*< *body[^>]*>/i, '')
                    .replace( /<\/body>.*$/,'' )
                ;

                var id      = this.resultContainer.id;
                var results = YAHOO.util.Dom.getElementsBy( function (elem) { return elem.id === id }, '*', div );
                this.resultContainer.innerHTML = results[0].innerHTML;
            }
        };

        this.form.enctype = 'application/x-www-form-urlencoded';

        YAHOO.util.Connect.setForm( this.form );
        YAHOO.util.Connect.asyncRequest( 'POST', this.form.action, callback );
    }
    else {
        // No container on this page. We'll have to use a classic submit();
        this.form.submit();
    }
};

//----------------------------------------------------------------------
HandhavingsNetwerkTool.prototype.fetchImagemap = function ( url ) {
    var callback = {
        scope   : this,
        success : function ( o ) {
            this.imagemap = o.responseText;
            this.mapReady();
        }
    };

    YAHOO.util.Connect.asyncRequest( 'GET', url, callback );
};

//----------------------------------------------------------------------
HandhavingsNetwerkTool.prototype.mapReady = function ( ) {
    if ( YAHOO.util.Event.DOMReady ) {
        this.applyMap();
    }
    else {
        YAHOO.util.Event.onDOMReady( this.applyMap, this, true );
    }
};
