/**
 * Functions for gathering client-side information and sending it back
 * to the server via (hidden) form fields
 */

/**
 * The master function, calls all the rest.  This will automatically
 * detect the form itself and will properly set values for fields with
 * the following names:
 *
 * <ul>
 *   <li>timeOffset</li>
 *   <li>screenHeight</li>
 *   <li>screenWidth</li>
 *   <li>platform</li>
 *   <li>cookiesEnabled</li>
 * </ul>
 */
function sendClientInfo (form) {
    form = form ? form : document.forms[0];
    if (form) {
        sendTimezoneOffset(form);
        sendScreenHeight(form);
        sendScreenWidth(form);
        sendPlatform(form);
        sendCookiesEnabled(form);
    }
}

function sendTimezoneOffset (form) {
    if (form.timeOffset) {
        var d = new Date();
        var tzOffset = 0;
        if (d.getTimezoneOffset) {
            // this value is in minutes
            var tzOffset = d.getTimezoneOffset();
        } 
        form.timeOffset.value = tzOffset;
        // alert("timezone offset = " + form.timeOffset.value);
    }
}

function sendScreenHeight (form) {
    maybeSetFormField(form, "screenHeight", window.screen.height);
}

function sendScreenWidth (form) {
    maybeSetFormField(form, "screenWidth", window.screen.width);
}

function sendPlatform (form) {
    maybeSetFormField(form, "platform", window.navigator.platform);
}

function sendCookiesEnabled (form) {
    maybeSetFormField(form, "cookiesEnabled", window.navigator.cookieEnabled);
}

function maybeSetFormField (form, name, value) {
    if (form[name]) {
        form[name].value = value;
    }
}
