
    /*
     * Author: Karl O'Leary
     * Company: Emagine Media, http://www.emagine.ie
     * Date: 2009-05-05
     * Description: Library of common functions
     *
     * Change Log;
     * - 20090619.1106, Karl, added the conatins_a_number function
     * - 20090511.1518, Karl, added the submit_before, submit_after, validate_email, logger, error and message functions
     * - 2009-05-05, Karl, created
     */



    function submit_before(formData, jqForm, form_options) {
        $("*").css("cursor", "wait");
        logger("submit_before, formData = "+formData);
		logger("submit_before, jqForm = "+jqForm);
		logger("submit_before, form_options = "+form_options);
    }


    function submit_after(responseText, statusText) {
        $("*").css("cursor", "auto");
        logger("submit_after, responseText = "+responseText);
        logger("submit_after, statusText = "+statusText);        

        $("#returned_data").html(responseText);
        $("#returned_data div.response > *").each(function(i) {
            try {
                if ($(this).attr("class")=="function")
                    eval($(this).attr("name")+"("+$(this).text()+")");

                else if ($(this).attr("class")=="content") {
                    var selector = $(this).attr("selector");
                    var road = $(this).attr("road");
                    var content = $(this).html();

                    if (road=="replace") $(selector).html(content);
                    else if (road=="append") $(selector).append(content);
                    else if (road=="delete") $(selector).remove();
                }

                else
                    message("unrecognised element found, "+this.tagName+", class = "+$(this).attr("class")+", id = "+$(this).attr("id")+", body = "+$(this).text());
            }
            catch(err) {
                error("submit_after error caught ["+i+"]: "+err);
            }
        });
    }


    /*
     * Display the logged in bar
     */
    function display_loggedin_bar(customer_reference, balance) {
        if (!isNaN(balance))
            balance = "&euro;"+balance;

        if (balance.indexOf(".")==-1)
            balance = balance+".00";

        $("#content_top1").html("");
        $("#content_top1").append('<div class="form_box1a"></div>');
        $("#content_top1").append('<div class="form_box2a"></div>');
        $("#content_top1 .form_box1a").html('Welcome <span id="username">'+customer_reference+'</span>. Your Account Balance is: <strong>'+balance+'</strong>');
        $("#content_top1 .form_box2a").html('<a href="/cashier/statement.asp" class="toplink2">My Account</a><span class="space_margin">|</span><a href="/home.asp?Login=false" class="link">Logout</a>');
    }


    /*
     * UTILITY: Debugging function
     * - if the firebug plugin is installed log messages will be sent to console
     * - if not the error will be caught and not lock the browser
     */
    function logger(message) {
        try {
            console.log(new Date+": "+message);
        }
        catch(err) {}
    }


    /*
     * UTILITY: Debugging function
     */
    function error(message) {
        log("ERROR: "+message);
    }


    /*
     * UTILITY: Debugging function
     */
	function message(message) {
        log("MESSAGE: "+message);
    }


    /*
     * Run email throught this regular expression and retrun boolean result
     *
     * TODO: ensure that there will not be a conflict with other JS file when this is integrated with main system
     */
    function validate_email(eee) {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return filter.test(eee);
    }


    /*
     * UTILITY: Used in verifying that a password contains a number
     */
    function contains_a_number(str) {
        if (str.indexOf("0")!=-1)
            return true;
        else if (str.indexOf("1")!=-1)
            return true;
        else if (str.indexOf("2")!=-1)
            return true;
        else if (str.indexOf("3")!=-1)
            return true;
        else if (str.indexOf("4")!=-1)
            return true;
        else if (str.indexOf("5")!=-1)
            return true;
        else if (str.indexOf("6")!=-1)
            return true;
        else if (str.indexOf("7")!=-1)
            return true;
        else if (str.indexOf("8")!=-1)
            return true;
        else if (str.indexOf("9")!=-1)
            return true;
        else
            return false;
    }

    /*
     * UTILITY: submit a form defined by the css selector after a time understandable by the jquery.timers plugin
     */
    function submit_after_pause(idee, lenny) {
        $(idee).oneTime(lenny, function() {
            $(idee).submit();
        });
    }
