//alert(document.compatMode);
function get_web_client() {
    var browser     = new Jurl();
    browser.init_h  = function(http,jurl) {
        if('POST' == jurl.method)
            http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    };

    browser.error_h = function(http, jurl) {
        alert('error occurred:\n' + http.getAllResponseHeaders() );
    };

    return browser;
}


function add_pd(del_me) {
    var f = $('forma');
    var del_me = (del_me == null) ? "" : del_me;
    var browser        = get_web_client();
    browser.response_h = function(http,jurl) {
        //var json = http.responseText.parseJSON();
        //alert(http.responseText);
        $('fa-add-pd').innerHTML = http.responseText;
    }

    var query = new StringBuffer();
    var got_faqpd5 = false;
    for(i=0; i<f.elements.length; i++)
    {
        var matched = f.elements[i].name.match(/(^faq5(pd|date|num)(\d+)$)/);
        if( matched == null) continue;

        got_faqpd5 = true;
        query.append(f.elements[i].name);
        query.append('=');
        query.append(encodeURI(document.getElementById(matched[1]).value));
        query.append('&');
    }

    if(!got_faqpd5) del_me = '+1';
    query.append('rm=_fa_add_pd&add=');
    query.append(Jurl.encode(del_me));
    //alert(query.toString());
    browser.post('index.php',query.toString());
}


function frequency_other(x) {
    var sidx = x.selectedIndex;

    if(x.options[sidx].value == 'Other')
    {
        var buf = new StringBuffer();
        var span = $('s-'+x.name);
        buf.append('<input size="9" maxlength="20" type="text" name="'+ x.name +'" />');
        buf.append('<img alt="" align="top" onclick="frequency_list(\''+ x.name + '\')" style="border: 0;" src="static/images/downarrow.png" />');
        span.innerHTML = buf.toString();

        setTimeout("document.forms[0]['"+x.name+"'].focus()", 100);

    }
}


function frequency_list(name) {
    var buf = new StringBuffer();
    var span = $('s-'+name);

    buf.append('<select name="'+ name +'" id="'+ name +'" onchange="frequency_other(this);" />');

    for( i in frequency )
    {
        buf.append('<option value="'+ i +'">' + i + '</option>');
    }

    buf.append('</select>');
    span.innerHTML = buf.toString();
}


function faq5_other(x) {
    var sidx = x.selectedIndex;

    if(x.options[sidx].value == 'Other')
    {
        var buf = new StringBuffer();
        var span = $('s-'+x.name);
        buf.append('<input size="37" maxlength="39" type="text" name="'+ x.name +'" id="'+ x.name +'" />');
        buf.append('<img alt="" align="top" onclick="faq5_list(\''+ x.name + '\')" style="border: 0;" src="static/images/downarrow.png" />');
        span.innerHTML = buf.toString();

        setTimeout("document.forms[0]['"+x.name+"'].focus()", 100);

    }
}


function faq5_list(name) {
    var buf = new StringBuffer();
    var span = $('s-'+name);

    buf.append('<select name="'+ name +'" id="'+ name +'" onchange="faq5_other(this);" />');
    //for(i=0; i<profdev.length; i++)
    for( i in profdev )
    {
        //buf.append('<option value="'+ i +'">' + profdev[i] + '</option>');
        buf.append('<option value="'+ i +'">' + i + '</option>');
    }
    buf.append('</select>');
    span.innerHTML = buf.toString();
}


function submitForm(form) {
    //alert(form.rm.value);
    form.submit();

    return true;
}

/**************************** Start util functions ****************************/

function $(x) {
    if (typeof x == "string") return document.getElementById(x);
        return x;
}


/**
 * StringBuffer
 *
 * var buf = new StringBuffer();
 * buf.append("hello");
 * buf.append("world");
 * alert(buf.toString());
 */

function StringBuffer() {
    this.buffer = [];
}

StringBuffer.prototype.append = function append(string) {
    this.buffer.push(string);
    return this;
};

StringBuffer.prototype.toString = function toString() {
    return this.buffer.join("");
};

/*
 * runOnLoad.js: portable registration for onload event handlers.
 *
 * This module defines a single runOnLoad( ) function for portably registering
 * functions that can be safely invoked only when the document is fully loaded
 * and the DOM is available.
 *
 * Functions registered with runOnLoad( ) will not be passed any arguments when
 * invoked. They will not be invoked as a method of any meaningful object, and
 * the this keyword should not be used. Functions registered with runOnLoad( )
 * will be invoked in the order in which they were registered. There is no
 * way to deregister a function once it has been passed to runOnLoad( ).
 *
 * In old browsers that do not support addEventListener( ) or attachEvent( ),
 * this function relies on the DOM Level 0 window.onload property and will not
 * work correctly when used in documents that set the onload attribute
 * of their <body> or <frameset> tags.
 */
function runOnLoad(f) {
    if (runOnLoad.loaded) f( );    // If already loaded, just invoke f( ) now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run( ) more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad( ) to register another function.
runOnLoad.run = function( ) {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i]( ); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }

    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run( ) as the onload event handler
// for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;

