
function change_active(check_id, id){
    var el = document.getElementById(id);
    var ch = document.getElementById(check_id);

    if (ch.checked){
        el.disabled = false;
    }else{
        el.disabled = true;
    }
}

var deadline_val = '';
function deadline_form(check_id,id){
    var ch = document.getElementById(check_id);
    var innerStr = '';
    if (ch.checked){
        innerStr += '<div class=\"form-background\">\n';
        innerStr += '    <input size=\"100\" class=\"input-text\" name=\"deadline_on\" id=\"deadline_on\" value=\"\" type=\"text\" onFocus=\"change_active_and_cal_id(\'is_deadline\', \'deadline_on\', \'calVote\')\"/>\n';
        innerStr += '    <div id=\"calVote\"></div>\n';
        innerStr += '</div>\n';

        if (! document.getElementById(id)){
            document.getElementById("deadlineForm").innerHTML = innerStr;
            document.getElementById(id).value = deadline_val;
            document.getElementById(id).focus();
        }
    }else{
        if (! document.getElementById(id)){
            deadline_val = '';
        }else{
            deadline_val = document.getElementById(id).value;
        }
        document.getElementById("deadlineForm").innerHTML = innerStr;
    }
}


var max_count_val = '';
function max_count_form(check_id,id){
    var ch = document.getElementById(check_id);
    var innerStr = '';

    if (ch.checked){
        innerStr += '<div class=\"form-background\">\n';
        innerStr += '     総投票数の上限を入力（999999まで設定が可能です）<br>\n';
        innerStr += '     <input size=\"100\" class=\"input-text\" name=\"max_count\" id=\"max_count\" value=\"\" type=\"text\">\n';
        innerStr += '</div>\n';

        if (! document.getElementById(id)){
            document.getElementById("maxCount").innerHTML = innerStr;
            document.getElementById(id).value = max_count_val;
            document.getElementById(id).focus();
        }
    }else{
        if (! document.getElementById(id)){
            max_count_val = '';
        }else{
            max_count_val = document.getElementById(id).value;
        }
        document.getElementById("maxCount").innerHTML = innerStr;
    }
}

var limited_url_val = '';
function limited_url_form(check_id, id){
    var ch = document.getElementById(check_id);
    var innerStr = '';

    if (ch.checked){
        innerStr += '<div class=\"form-background\">';
        innerStr += '    貼り付けを許可するブログのURLを入力（前方一致）<br />';
        innerStr += '    <input type=\"text\" size=\"100\" class=\"input-text\" name=\"limited_url\" id=\"limited_url\" value=\"\">';
        innerStr += '</div>';

        if (! document.getElementById(id)){
            document.getElementById("limited").innerHTML = innerStr;
            document.getElementById(id).value = limited_url_val;
            document.getElementById(id).focus();
        }
    }else{
        if (! document.getElementById(id)){
            limited_url_val = '';
        }else{
            limited_url_val = document.getElementById(id).value;
        }
        document.getElementById("limited").innerHTML = innerStr;
    }
}

function active_box(ans_num){
    var max_item = 6;
    var el_id    = "answer" + ans_num;
    var td_id    = "tds" + ans_num;
    var tab_id   = ans_num + 1;

    var innerStr1 = "<input type='text' size='30' class='input-float' name='" + el_id + "' id='" + el_id + "' tabindex='" + tab_id + "'>";
    var innerStr2 = "<span class='btn-minus'><a href='javascript:delete_box(" + ans_num + ")'></a></span>";
    document.getElementById(td_id).innerHTML = innerStr1 + innerStr2;

    if (ans_num == 2){
        var val = document.getElementById("answer1").value;

        document.getElementById("tds1").innerHTML += 
                                       "<span class='btn-minus'><a href='javascript:delete_box(1)'></a></span>";
        document.getElementById("answer1").value = val;
    }

    var nextnum   = ans_num + 1;
    var tb_id     = "ans_table";
    var nextTd_id = "tds" + nextnum;
    var nextTr_id = "trs" + nextnum;
    var act_tab_id = nextnum + 1;
    if (nextnum <= max_item && !document.getElementById(nextTd_id)){
        var nextStr1 = "選択肢" + nextnum + "&nbsp\n";
        var nextStr2 = "<span class='btn-plus'>\n" +
                       "<a href=\"#\" onclick=\"active_box(" + nextnum +
                       ");id=setTimeout('" +
                       "document.forms[\\\'vote_form\\\'].answer" + nextnum + ".focus();clearTimeout('+id+');', 100);" +
                       "\" tabindex=\"" + act_tab_id +
                       "\"></a>\n" +
                       "</span>\n";

        var rowObj;
        var cellObj;
        rowObj  = document.getElementById(tb_id).insertRow(ans_num);
        rowObj.id = nextTr_id;

        callObj = document.createElement("TD");
        rowObj.appendChild(callObj).innerHTML = nextStr1;
    
        callObj = document.createElement("TD");
        callObj.id = nextTd_id;
        rowObj.appendChild(callObj).innerHTML = nextStr2;
    }
}

function delete_box(ans_num){
    var max_item   = 6;
    var tbIDName   = "ans_table";
    var tdIDName   = "tds"
    var inputIDName= "answer"
    
    // 値入れ替え処理
    var childCount = document.getElementById(tbIDName).getElementsByTagName("TR").length;
    for (i = ans_num; i < childCount; i++){
        var nextStr = document.getElementById(tdIDName + (i + 1)).innerHTML;
        // 次のTDタグがインプットボックスか確認
        if (/input/.test(nextStr)){
            document.getElementById(inputIDName + i).value = document.getElementById(inputIDName + (i + 1)).value;
            if ((i + 1) == max_item){
                i++;
            }
        }else{
            // 非表示選択肢のがあった場合以降全て削除
            for (j = childCount - 1 ; i <= j; j--){
                document.getElementById(tbIDName).deleteRow(j);
                childCount--;
            }
        }
    }
    if (ans_num == max_item){
        i++;
    }

    i--;
    var tab_id = i + 1;
    var innerStr = "<span class='btn-plus'><a href='javascript:active_box(" + i + ")'' tabindex='" + tab_id + "'></a></span>";
    document.getElementById(tdIDName + i).innerHTML = innerStr;
    
    // 表示ボックスが1つの場合の制御
    if(document.getElementById(tbIDName).getElementsByTagName("TR").length == 2){
        var val = document.getElementById(inputIDName + "1").value;
        
        document.getElementById(tdIDName + "1").innerHTML = 
                      "<input type='text' size='30' class='input-float' name='" + inputIDName + "1" + "' id='" + inputIDName + "1" + "' tabindex='2'>";
        document.getElementById(inputIDName + "1").value = val;
    }
}

function valCheck(){
    document.getElementById("Submit").disabled = true;

    var rules = [];
    rules.push({id:'id_question', action:'isStringInRange',  message_key:'質問',    param:{Smax:45}});
    rules.push({id:'answer1',     action:'isStringInRange',  message_key:'選択肢1', param:{Smax:45}});
    rules.push({id:'answer2',     action:'isStringInRange',  message_key:'選択肢2', param:{Smax:45, notNull:false}});
    rules.push({id:'answer3',     action:'isStringInRange',  message_key:'選択肢3', param:{Smax:45, notNull:false}});
    rules.push({id:'answer4',     action:'isStringInRange',  message_key:'選択肢4', param:{Smax:45, notNull:false}});
    rules.push({id:'answer5',     action:'isStringInRange',  message_key:'選択肢5', param:{Smax:45, notNull:false}});
    rules.push({id:'answer6',     action:'isStringInRange',  message_key:'選択肢6', param:{Smax:45, notNull:false}});
    rules.push({id:'template_id', action:'isChecked',        message_key:'デザイン'});
    rules.push({id:'category_id', action:'isNotEmpty',       message_key:'カテゴリ'});
    rules.push({id:'deadline_on', action:'isFutureDate',     message_key:'投票の終了日'});
    rules.push({id:'max_count',   action:'isIntegerInRange', message_key:'総投票数の上限', param:{Nmin:1, Nmax:999999}});

    rules.push({id:'anonymity_key', action:'isNotEmpty', message_key:'削除用パスワード'     });
    rules.push({id:'captcha_code',  action:'isNotEmpty', message_key:'スパム防止の為の認証' });

    var prefix   = "<div class='error'><h5>入力にエラーがありました</h5><ul>";
    var suffix   = "</ul></div>";
    var r_prefix = "<li>";
    var r_suffix = "</li>";

    var v = new VoteCheckForm();
    v.setRules(rules);
    var flg = v.setChecktoHTML("error", prefix, suffix, r_prefix, r_suffix);
    
    document.getElementById("Submit").disabled = false;
    y = document.getElementById('return_top').offsetTop;
    scrollTo( 0, y ); 

    return flg;
}

function change_active_and_cal_id(check_id, id, cal_id) {
    change_active(check_id, id);
    check_cal_id(check_id, id, cal_id)
}

function check_cal_id(check_id, id, cal_id){
    var ch = document.getElementById(check_id);

    if (ch.checked){
        new viewCalender(id,0);
    }else{
        new _setDate(cal_id,'');
    }
}

