/* statick class to create an okAmiss object */
function getOkAmiss(params)
{
    return getOkAmiss.initialize(params);
}
getOkAmiss.amiss = []
getOkAmiss.initialize = function(params)
{
    if (typeof(params)=='undefined') params = {};
    params.name = 'getOkAmiss.amiss['+getOkAmiss.amiss.length+']';
    return getOkAmiss.amiss[getOkAmiss.amiss.length] = new okAmiss(params);
}
/* -------------------------------------------------------------------------- */

function okAmiss(params)
{
    this.initialize(params);
}
okAmiss.names = [];
/* -------------------------------------------------------------------------- */
/**
 * Manages the Notify Us form on the details page
 */
okAmiss.prototype = {
    name            : "amiss",
    apiUrl          : "",
    formId          : "amissForm",
    reasonId        : "amissReason",
    msgId           : "amissMsg",
    url             : "",
    from            : "amiss@wimgo.com",
    to              : "WimgoCorrections@oklahoman.com",
    //to              : "ryan.5and2@gmail.com",
    msg             : "We appreciate you letting us know about your concern.  We will review your report and take the appropriate action .  Please be aware that circumstances may prevent us from taking immediate action.",
    params          : null,

    initialize : function(params)
    {
        this.params = params;

        okAmiss.names[params.name] = this;
        if(typeof(params.name)          != 'undefined') { this.name         = params.name;          }
        if(typeof(params.apiURL)        != 'undefined') { this.apiURL       = params.apiURL;        }
        if(typeof(params.formId)        != 'undefined') { this.formId       = params.formId;        }
        if(typeof(params.reasonId)      != 'undefined') { this.reasonId     = params.reasonId;      }
        if(typeof(params.msg)           != 'undefined') { this.msg          = params.msg;           }
        if(typeof(params.url)           != 'undefined') { this.url          = params.url;           }
    },

    /**
    *   resets the form fields
    */
    reset : function()
    {
        //clear the form to defaults.
        document.getElementById(this.reasonId).value = '';
    },

    /**
    *   Attempts to send the report.
    *   Expects to find hidden form fields with the module's id and module's type.
    */
    submit : function()
    {
        var reason = document.getElementById(this.reasonId).value;
        if(reason.length == 0){
            alert('Please tell us what is amiss and we will do our best to make the necesarry corrections.');
            document.getElementById(this.reasonId).focus();
            return;
        }

        //var msg = "We appreciate you letting us know about your concern.  We will review your report and take the appropriate action .  Please be aware that circumstances may prevent us from taking immediate action.";
        
        document.getElementById(this.msgId).innerHTML = this.msg;

        //code to send report
        queryString  = "method="        + "EmailThis.Send";
        queryString += "&from="         + this.from; 
        queryString += "&to="           + this.to; 
        queryString += "&msg="          + encodeURIComponent("A user is reporting something is amiss on " + this.url+"\n"+reason);
        loadJSON(this.apiURL,'callback='+this.name+".display&"+queryString,'script');
    },
    display : function(data)
    {
        var msg = document.getElementById(this.msgId)

        if(data.success){
            exec = "";

            this.reset();

            if(msg != undefined){
                msg.innerHTML = this.msg;
                exec += "document.getElementById('"+this.msgId+"').innerHTML='';";
            }
            if(exec.length) { var t=setTimeout(exec, 6000); }
        }else{
                msg.innerHTML = "There was a problem with submiting the form."
        }
    }
}
