/*
 * survey.js - Ajax style survey submissions
 *
 *  This file provides a function "signup" you can associate with the submit
 *  event of your survey form. It take two arguments:
 *
 *   signup( jQuery reference to the form, on success callback )
 *
 *  On error, it will try to highlight errors if there are any, and will 
 *  call show() on element with the id 'error-message'.
 *
 *  On successs, the default callback replace the form with the text 
 *  'Thanks for signing up!'. You can pass in a callback to do something
 *  more interesting. You call back will get a reference to the form and 
 *  the results of the submit.
 *
 *  Example:
 *
 * <script type="text/javascript" src="/js/survey.js"></script>
 * <script type="text/javascript">
 *                 
 *      function succesful_signup(f_signup, result) {
 *                             
 *          $('#signup').hide()
 *          $('#taf').show('slow')
 *                                                 
 *      }               
 *                                                                 
 *      function initialize() {
 *          $('#signup-form').submit( 
 *              function() { 
 *                 signup($('#signup-form'), successfull_signup); 
 *                 return false; 
 *                 } 
 *          )
 *      }
 *
 *      $(document).ready(initialize);
 *
 *  </script>
 *
 *
 */
function highlight_errors(f_signup, fields) {

    if (fields == undefined)
        return;

    for (var i=0;i<fields.length;i++) {
        var selectors = [
            ( '#l' + fields[i] ),
            ( '#l_' + fields[i] ),
            ( ':input[name=' + "'" + fields[i] + "'" + ']' )
        ]
        re = /^user_/
        if ( re.test(fields[i]) ) {
            f = fields[i].substring(5,fields[i].length)
            selectors.push(
                ( '#l' + f ),
                ( '#l_' + f )
            )
        }
        for (var s=0;s<selectors.length;s++) {
            f_signup
                .find(selectors[s])
                .each(function(x) { $(this).addClass('error') })
        }
    }

}

function clear_errors() {
    $('#error-message').hide()
    $('.error').removeClass('error')
}

function signedup(f_signup, result, on_success) {
    
    if ( result.success ) {
        if ( on_success ) {
            on_success(f_signup, result)
        }
        else {
            f_signup.html('<p>Thanks for signing up!</p>')
        }
    }
    else {
        highlight_errors(f_signup, result.missing)
        highlight_errors(f_signup, result.invalid)

        var err = f_signup.find('#error-message')
        err.addClass('error')
        err.show()
    }

}

function signup(f_signup, f_callback, f_url) {
    if ( f_callback ) {
        callback = function(result) { signedup(f_signup, result, f_callback) }
    }
    else {
        callback = function(result) { signedup(f_signup, result) }
    }

    if (!f_url) {
        f_url = '/survey/process.pl';
    }

    clear_errors()
    $.post(
        f_url,
        f_signup.serialize(), 
        callback,
        "json"
    )

    return false
}

