/*
 * $Id: form.js 65888 2009-03-09 21:10:26Z rmcginty $
 * $URL: https://build/subversion/DestinationSearch/wdie/tags/wdieds-2.21.5.4/src/main/webapp/js/form.js $
 * DS functions
 */

LMI.Form = function(){
    this.action = '';
    this.params = {};
    this.method = 'post';
    this.postCallback = null;
};
LMI.Form.prototype = ( function() {
    return {
        getAction: function() {
            return this.action;
        },
        setAction: function( a ) {
            this.action = this.parseUrl( a );
        },
        getMethod: function() {
            return this.method;
        },
        setMethod: function( m ) {
            this.method = m;
        },
        setPostCallback: function( callback ) {
            this.postCallback = callback;
        },
        removeVar: function(name) {
            if( this.params[name] ) { delete this.params[name]; }
        },
        setVar: function( name, val, append, remove ) {
            var added = 0;

            // create the array if it doesn't exist
            if( ! this.params[name] ) { this.params[name] = []; }

            // handle null values
            if( val === null) { val = ''; }

            var cur = -1;

            // find the index of the given k/v pair
            for( var i=0; i<this.params[name].length; ++i ) {
                if( this.params[name][i] == val ) { cur = i; }
            }

            if( cur > -1 ) {
                if( remove ) {
                    this.params[name].splice( cur, 1 );

                    // remove empty array
                    if( this.params[name].length === 0 ) { delete this.params[name]; }

                    return;
                }
            }

            // only modify an existing-named param if append it set
            if( ! append ) {
                this.params[name] = [];
                this.params[name].push( val );
            }
            added++;

            if( append || !added ) {
                this.params[name].push( val );
            }
        },
        getVar: function( name ) {
            return( this.params[name] ? this.params[name] : [] );
        },
        SUBMIT_NORMAL: 0,
        SUBMIT_NOREFRESH: 1,
        SUBMIT_SESSION: 2,
        submit: function( submitType ) {
            /* submitType should be:
             * 0 - normal (default)
             * 1 - norefresh
             * 2 - send sessionid manually
             */
            var url,
                body = document.getElementsByTagName( 'body' )[0],
                f = LMI.Element.create( 'form', body, { action:this.action, method:this.method, style:'display:none;position:absolute;top-4000px;width:0;height:0' } );

            for( var i in this.params ) {
            	if( this.params.hasOwnProperty( i ) ){
	                for( var j=0; j<this.params[i].length; ++j ) {
	                    if( submitType == this.SUBMIT_NOREFRESH ) {
	                        // build up a string of param k/v pairs for norefresh use
	                        url += i + "=" + this.params[i][j] + "&";
	                    } else {
	                        LMI.Element.create( 'input', f, { name: i, value: this.params[i][j], style: 'display:none;' } );
	                    }
	                }
            	}
            }

            if( submitType == this.SUBMIT_NOREFRESH ) {
                // append the sessionId, since IE doesn't send cookies with image requests
                ( document.createElement( 'img' ) ).src = this.action + ";jsessionid=" + LMI.Lang.getObject( "LMI.Data.state.sessionId" ) + "?" + url + "noresponse=1";
            } else if( submitType == this.SUBMIT_SESSION ) {
                f.action = this.action + ";jsessionid=" + LMI.Lang.getObject( "LMI.Data.state.sessionId" );
                f.submit();
            } else {
                f.submit();
            }
        },
        go: function( action, submitType, name, val ) {
            /*
             * dsform.go takes any number of name and val parameters (after action and submitType)
             * now can tage a uri fragment ('#')
             */
            var frag;

            for( var i=2; i<arguments.length; i++ ) {
                if( arguments[i] == '#' ) {
                    frag = arguments[++i];
                    continue;
                }
                this.setVar( arguments[i], arguments[++i] );
            }

            if( action ) {
                this.setAction( frag ? action + '#' + frag : action );
            }
            this.submit( submitType );
            return false;
        },
        postUrl: function( url, allowDups ) {
            var location = this.parseUrl( url, allowDups );
            if( this.postCallback ) {
                location = this.postCallback( location );
            }
            this.go( location );
        },
        parseUrl: function( url, allowDups ) {
            var u = new LMI.Url( url );
            var location = u.getLocation();
            var ps = u.getParamString();
            if( ps ) {
                location += ';' + ps;
            }
            var keys = u.getQueryNames();
            for( var i = 0; i < keys.length; ++i ) {
                var v = u.getQueryValues( keys[i] );
                for( var j = 0; j < v.length; ++j ) {
                    this.setVar( keys[i], v[j], allowDups, false );
                }
            }
            return location;
        },
        postLink: function( evt, allowDups ) {
            /*
             * parse a the href of a clicked link and submit it as a form
             * with all the other params already in the form
             */
        	var e;
            if( evt && typeof evt.event != 'undefined' ) {
                e = evt; // already a LMI.BrowserEventObject
            } else {
                e = new LMI.BrowserEventObject( evt, window.event, this ); // raw event
            }

            if( e.getAltKey() || e.getCtrlKey() ) {
                return true;
            }
            var t = e.getCurrentTarget() || e.getTarget();
            while( t.nodeName != 'A' && t.parentNode ) { t = t.parentNode; }
            this.postUrl( t.href, allowDups );
            if( LMI.Browser.browser == 'Safari' ) {
                t.href = '#'; // safari won't preventDefault
            }
            //e.stopPropagation();
            /* Testing this core file to see what happens after removing stop propagation */
            e.preventDefault();
            return false;
        },
        copy: function( form ) {
            /*
             * copy the given form's properties into our own form
             */
            if( form.action ) { this.setAction( form.action ); }
            var dsform = this;
            LMI.Lang.forEach( form.elements, function( o ) {
                dsform.setVar( o.name, o.value );
            } );
        }
    };
} )();
