$(function()
{
    var varName         = editData.varName;
    var type            = editData.type;
    var distribution    = editData.distribution;
    var exportY         = editData.exportY;
    var forecastYname   = editData.forecastYname;
    var forecastName    = editData.forecastName;
    var isCondition     = editData.isCondition;
    var formula         = editData.formula;
    var decision        = editData.decision || 'false';
    var decisionXname   = editData.decisionXname;
    var decisionName    = editData.decisionName;
    var decisionVar     = editData.decisionVar;
    var targetYname     = editData.targetYname;
    var target          = editData.target;
    var statistic       = editData.statistic;
    var sensitivity     = editData.senstivitity;
    var displayPlans    = editData.displayPlans

    var reqVar       = editData.reqVar || [];
    var reqStat      = editData.reqStat || [];
    var reqCondition = editData.reqCondition || [];
    var reqValue     = editData.reqValue || [];

    formData = {hypo: [], forecast: [], decisionStus: decision, sensitivity: sensitivity, displayPlans: displayPlans};
    buildHypo(varName, type, distribution);
    buildForecast(exportY, forecastYname, forecastName, isCondition, formula);
    buildDecision(decision, decisionXname, decisionName, decisionVar);
    buildTarget(decision, target, targetYname, statistic);
    buildRequest(decision, reqVar, reqStat, reqCondition, reqValue);
    render();
});

/**
 * Build hypo data.
 *
 * @param string varName
 * @param string type
 * @param string distribution
 * @access public
 * @return void
 */
function buildHypo(varName, type, distribution)
{
    for(var variable in varName)
    {
        formData.hypo.push(
        {
            variable: variable,
            varName: varName[variable],
            type: type[variable],
            distribution: distribution[variable]
        });
    }
}

/**
 * Build forecast data.
 *
 * @param string forecastYname
 * @param string forecastName
 * @param string formula
 * @access public
 * @return void
 */
function buildForecast(exportY, forecastYname, forecastName, isCondition, formula)
{
    for(var index in forecastYname)
    {
        formData.forecast.push(
        {
          exportY: exportY == forecastYname[index] ? 'checked' : '',
          variable: forecastYname[index],
          forecastName: forecastName[index],
          isCondition: isCondition[index],
          formula: formula[index]
        });
    }
}

/**
 * Build decision data.
 *
 * @param string decision
 * @param string decisionXname
 * @param string decisionName
 * @param string decisionVar
 * @access public
 * @return void
 */
function buildDecision(decision, decisionXname, decisionName, decisionVar)
{
    formData.decision = [];
    if(decision == 'true' && decisionXname)
    {
        for(var index in decisionXname)
        {
            formData.decision.push(
            {
                variable: decisionXname[index],
                decisionName: decisionName[index],
                decisionVar: decisionVar[index]
            });
        }
    }
}

/**
 * Build target data.
 *
 * @param string decision
 * @param string target
 * @param string targetYname
 * @param string statistic
 * @access public
 * @return void
 */
function buildTarget(decision, target, targetYname, statistic)
{
    formData.target = [];
    if(decision == 'true' && target)
    {
        for(var index in targetYname)
        {
            formData.target.push(
            {
                variable: targetYname[index],
                target: target[index],
                statistic: statistic[index]
            });
        }
    }
}

/**
 * Build request data.
 *
 * @param array reqVar
 * @param array reqStat
 * @param array reqCondition
 * @param array reqValue
 * @access public
 * @return void
 */
function buildRequest(decision, reqVar, reqStat, reqCondition, reqValue)
{
    formData.request = [];
    if(decision == 'true' && reqVar.length > 0)
    {
        reqVar.forEach(function(v, index)
        {
            formData.request.push(
            {
                reqVar: reqVar[index],
                reqStat: reqStat[index],
                reqCondition: reqCondition[index],
                reqValue: reqValue[index],
            });
        });
    }
}
