/**
 *
 * @version $Id: events.js 387 2009-02-10 03:34:00Z midget $
 *
 * @license http://www.midworld-networks.com/licenses/outsoursing.html
 * @author Dario Minnucci <dario.minnucci@midworld-networks.com>
 * @copyright Copyright 2008, Dario Minnucci <dario.minnucci@midworld-networks.com>
 *
 * @package midworld.net
 * @subpackage javascript
 * @access public
 *
 */
/**
 * Usage example:
 *
 *	function exec(){
 *		var p = new Post();
 *		p.debug = 0;
 *		p.addForm('frm');
 *		p.addAction('events.html');
 *		p.addParam('foo', 'bar'); 	// Ad param 'foo' and value 'bar' to the existing cache
 *		//p.showSettings(); 				// Shows the current settings
 *		//p.showCache(); 						// Shows the current cache content
 *		p.doPost();
 *	}
 */
/**
 * Post class
 */
function Post(){
    //
    // Properties
    //
    this.debug = 0;
    this.paramDelimiter = '{equals}';
    this.cacheDelimiter = '{del}';
    this.form = "";
    this.action = "";
    this.cache = "";
    //
    // Methods
    //
    this.addForm = addForm;
    this.addAction = addAction;
    this.addExisting = addExisting;
    this.showSettings = showSettings;
    this.showCache = showCache;
    this.addParam = addParam;
    
    this.doPost = doPost;
}

//
// Add the form parameter
//
function addForm(f){

    if (this.debug) 
        alert("Adding form: " + f);
    
    with (this) this.form = trim(f);
}

/**
 * Add action
 * @param string a
 */
function addAction(a){

    if (this.debug) 
        alert("Adding action: " + a);
    
    with (this) this.action = trim(a);
}


/**
 * Add existing form elements ( real elements )
 * return void
 */
function addExisting(){

    if (this.debug) 
        alert('Adding existing inf form: ' + this.form);
    
    with (this) {
    
        var x = document.getElementById(this.form);
        var i, key, val;
        for (i = 0; i < x.length; i++) {
        
            key = x.elements[i].name;
            val = x.elements[i].value;
            
            if (this.debug) 
                alert('Adding param: ' + key + this.paramDelimiter + val);
            
            this.addParam( key, val );
        }
        
    }
}


//
// Add parameter to the cache
//
/**
 * Add given data to the cache
 * @param string k
 * @param string v
 */
function addParam(k, v){
    with (this) {
        //alert( 'Adding key: '+ k +' | val :'+ v );
        // Cleanup parameters and add them to the cache
        if (this.cache) {
            this.cache += this.cacheDelimiter + trim(k) + this.paramDelimiter + trim(v);
        }
        else {
            this.cache = trim(k) + this.paramDelimiter + trim(v);
        }
    }
}

/**
 * Show cache content
 * @return void
 */
function showCache(){
    with (this) alert(this.cache);
}

/**
 * Show current settings
 * @return void
 */
function showSettings(){
    with (this) {
        var settings;
        settings = "Current settings: \n\n";
        settings += " Form: " + this.form + "\n";
        settings += " Action: " + this.action + "\n";
        settings += " Cache: " + splitCache(this.cache, this.cacheDelimiter) + "\n";
        alert(settings);
    }
}

/**
 * Substitute delimiter for \n to make it pretty ( is buggy )
 * @param string s
 * @param string d
 * @return string bucket
 */
function splitCache(s, d){

    var arr_cache = s.split(d);
    var bucket;
    
    for (var i = 0; i < arr_cache.length; i++) {
        temp = arr_cache[i];
        bucket += temp.replace(d, "\n");
    }
    
    return bucket;
}

/**
 * Trim the given string
 * @param string value
 * @return string temp
 */
function trim(value){
    var temp = value;
    var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
    if (obj.test(temp)) {
        temp = temp.replace(obj, '$2');
    }
    var obj = / +/g;
    temp = temp.replace(obj, " ");
    if (temp == " ") {
        temp = "";
    }
    return temp;
}


/**
 * Build the form elements and do the post itself
 * @return void
 */
function doPost(){

    with (this) {
    
        var f = this.form;
        var a = this.action;
        var c = this.cache;
        var cd = this.cacheDelimiter;
        var pd = this.paramDelimiter;
        
        var arr_params = c.split( cd );
        var arr_key_val = new Array();
        
        timestamp = new Date();
        
        for (i = 0; i < arr_params.length; i++) {
        
            arr_key_val = arr_params[i].split( pd );
            
            if ( arr_params ) {
            
                for ( s = 0; s < arr_key_val.length; s++ ) {
                
                    new_element = document.createElement( "input" );
                    new_element.setAttribute( "type", "hidden" );
                    new_element.setAttribute( "id", "_" + arr_key_val[0] );
                    new_element.setAttribute( "name", "_" + arr_key_val[0] );
                    new_element.setAttribute( "value", arr_key_val[1] );
                    document.forms[f].appendChild( new_element );
                    
                }
                
            }
            
        }
        
        //
        // Add form_name param
        //
        new_element = document.createElement( "input" );
        new_element.setAttribute( "type", "hidden" );
        new_element.setAttribute( "id", "_form_name" );
        new_element.setAttribute( "name", "_form_name" );
        new_element.setAttribute( "value", this.form );
        document.forms[f].appendChild( new_element );
        //
        // Add timestamp param
        //
        new_element = document.createElement( "input" );
        new_element.setAttribute( "type", "hidden" );
        new_element.setAttribute( "id", "_timestamp" );
        new_element.setAttribute( "name", "_timestamp" );
        new_element.setAttribute( "value", timestamp.getTime() );
        document.forms[f].appendChild( new_element );
        
        fs = document.getElementById( f );
        fs.action = a;
        fs.submit();
        
    }
}
