var Polls;
var Site;
var Validation;



// wait for page to load
$(document).ready(function() {
    Polls = new PollMiniUpdate("/images/load.gif");
    Polls.updatePolls();
    /*
    Site = new SiteScripts();
    Site.Init();
    Policies();
    Validation = new ValidationUtils();
    */
});  // end google callback

// Object to handle updating any pollmini's
function PollMiniUpdate(AjaxImageURL) {
    this.updateInProgress = false;
    this.rootDivs = null;
    this.divIndex = null;
    this.ajaxReq = null;
    this.ajaxDiv = null;
    this.ajaxDoc = null;
    this.ajaxImage = AjaxImageURL;
    this.pollMiniFrm = null;

    this.processPolls = function() {
        var T = this;
        if (T.divIndex < T.rootDivs.length) {
            try {
                T.ajaxDiv = $("div#" + T.rootDivs[T.divIndex].id).children("div:first");
                T.pollMiniFrm = $("div#" + T.rootDivs[T.divIndex].id).children("form:first");
                var pollMiniURL = T.pollMiniFrm[0].url.value;
                $.ajax({
                    type: "GET",
                    url: pollMiniURL,
                    dataType: "html",
                    cache: false,
                    beforeSend: function() { T.ajaxDiv.mask("Please wait..."); },
                    success: function(pollData) {
                        T.ajaxDiv.unmask();
                        T.ajaxDiv.html(pollData);
                    },
                    complete: function() {
                        T.divIndex++;
                        T.processPolls();
                    }
                });
            }
            catch (e) {
                alert(e);
            }
        }
    }

    this.updatePolls = function() {
        if ($) {
            if (!this.updateInProgress) {
                this.updateInProgress = true;
                this.rootDivs = $("div[name='pollMini']");
                if (this.rootDivs.length == 0) {
                    var divs = $("div");
                    for (var i = 0; i < divs.length; i++) {
                        if (divs[i].name == "pollMini") { this.rootDivs.push(divs[i]); }
                    }
                }
                if (this.rootDivs.length > 0) {
                    this.divIndex = 0;
                    this.processPolls();
                } else {
                    this.updateInProgress = false;
                }
            }
        } else {
            alert('jQuery is required to process AJAX Requests');
        }
    }

    this.processResult = function(aURL, aDivTag, parameters) {
        var T = this;
        try {
            T.ajaxDiv = aDivTag;
            T.ajaxReq = $.ajax({
                type: "POST",
                url: aURL,
                processData: false,
                data: parameters,
                dataType: "html",
                beforeSend: function() { T.ajaxDiv.mask("Please wait..."); },
                success: function(pollResultData) {
                    T.ajaxDiv.unmask();
                    T.ajaxDiv.html(pollResultData);
                }
            });
        }
        catch (e) {
            alert(e);
        }
    }


    this.CheckForm = function(aFormId, aDivTagId) {
        var formErrors = true;
        var jqF = "form#" + aFormId;
        var aForm = $(jqF);
        var aDivToUpdate = $("div#" + aDivTagId);
        $(jqF + " input[name='q1']").each(
            function() {
                if (this.checked) { formErrors = false; }
                return formErrors;
            }
        );
        if (formErrors) {
            alert('You must choose an answer');
        }
        else {
            var getStr = "";
            $(jqF + " :input").each(
                function() {
                    if ($(this).is(":text")) { getStr += this.name + "=" + this.valueOf + "&"; }
                    if ($(this).is(":checkbox")) {
                        (this.checked) ? getStr += this.name + "=" + this.value + "&" : getStr += this.name + "=&";
                    }
                    if ($(this).is(":radio")) {
                        (this.checked) ? getStr += this.name + "=" + this.value + "&" : getStr += "";
                    }
                    if ($(this).is("[tagName='select']")) {
                        alert(this);
                        getStr += this.name + "=" + this.options[this.selectedIndex].value + "&";
                    }
                    if ($(this).is("input:hidden")) {
                        getStr += this.name + "=" + this.value + "&";
                    }
                }
            );
            var postUrlValue = aForm[0].PostUrl.value;
            this.processResult(postUrlValue, aDivToUpdate, getStr);
        }
    }       
}
