﻿/* ------------------------------- */
/* CONSTANTS */
/* ------------------------------- */
var busyloading = "<div class='ajaxloading_outer'><div class='ajaxloading_inner'><img src='/_images/ajaxloading.gif' alt='Loading...'><br />Searching...</div></div>";

/* ------------------------------- */
/* EVENT TRAPS */
/* ------------------------------- */
//  When the window loads
$(document).ready(function() {
    /* ------------------------------------------- */
    /* Preload the AJAX spinning wheeel image      */
    /* ------------------------------------------- */
    if (document.images) {
        preload_image = new Image();
        preload_image.src = "/_images/ajaxloading.gif";
    }
    user_pageload_logic();
});

/* ------------------------------------------- */
/* Update web content using AJAX               */
/* ------------------------------------------- */
function ajax_update(target_div, content_url, showbusy) {
    /* ------------------------------------------- */
    /* target_div is the 'ID' value of the containing <DIV> */
    /*    This is WHERE on the web page you want content replaced */
    /* content_url is a URL that returns the HTML to inject into the web page */
    /* showbusy is true or false indicating if you want a "loading" message */
    /*     as temporary content while waiting for the server to return the results */
    /*     If the containing <DIV> is very small, then use false since there is no */
    /*     room for the busy message */
    /* NOTE: This function uses the JQuery Javascript Framework */
    /* ------------------------------------------- */
    /* ------------------------------------------- */
    /* Throw up optional 'I am busy' message while waiting on real content */
    /* ------------------------------------------- */
    if (showbusy) {
        $(target_div).html(busyloading);
    }
    /* ------------------------------------------- */
    /* Add a time value to the query arguments of the URL to prevent caching by IE */
    /* ------------------------------------------- */
    var kvp = content_url.substr(1).split('&');
    var random_arg = "ms=" + new Date().getTime();
    if (kvp == "") {
    } else {
        random_arg = "&" + random_arg;
    }
    var new_url = content_url + random_arg;

    /* ------------------------------------------- */
    /* replace the content of the web site */
    /* ------------------------------------------- */
    $(target_div).load(new_url);
    /* ------------------------------------------- */
    /* Always return to false to cancel any <A> link action */
    /* ------------------------------------------- */
    return false;
}
/* ------------------------------------------- */
/* JQUERY.Form Submission Debug                */
/* ------------------------------------------- */
// pre-submit callback 
function showRequest(formData, jqForm, options) {
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData);

    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 

    alert('About to submit: \n\n' + queryString);

    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true;
}

// post-submit callback 
function showResponse(responseText, statusText) {
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 

    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 

    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 

    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.');
} 
