
var surveyID = 0;

$(document).ready(function(){
    //loading
    $('#loadingContainer').ajaxStart(function(){$(this).show();}).ajaxStop(function(){$(this).hide();});	
	setInterval(function(){
        $('div.sub_container_content').css('height',$(window.parent.frames.right).height()-$('div.sub_container_title').height()-$('div.sub_container_operation').height()-$('div.sub_container_listhead').height()-$('div.sub_container_page').height()-10); //设置内容区域的高度
    },100);
    surveyID = getQueryStringRegExp('SurveyID')
    $('#btnAddQuestion').click(function(){
        var formAdd = $('#formAdd').clone(true).show().attr('id','formAdd_'+$('form').length);
        bindFormEvent(formAdd,true);
        $(this).before(formAdd);
    });//end btnAddQuestion click
    $('input[@type=text]').change(function(){
        $(this).next('span').html($(this).val());
    });
    
    $('form[@id^=form_]').each(function(){
        var form = $(this);
        form.find('input[@type=text]').hide().end().find('span.textContent').show();
        bindFormEvent(form,false);
    });
});//end ready


function bindFormEvent(formAdd,isAdd){
    if(isAdd){formAdd.find('input[@name^=txtQuestion_]').attr('name','txtQuestion');};
    formAdd.find('input[@name^=txtOption][@name!=txtOption_hide]').attr('name','txtOption_0');
    var choicetypeNum = $('input[@name=rdChoiceType]').length-2;
    formAdd.find('#rdChoiceType_0').attr('id','rdChoiceType_'+(choicetypeNum+1)).next().attr('for','rdChoiceType_'+(choicetypeNum+1));
    formAdd.find('#rdChoiceType_1').attr('id','rdChoiceType_'+(choicetypeNum+2)).next().attr('for','rdChoiceType_'+(choicetypeNum+2));
    formAdd.find('input[@name=btnAddMoreOption]').click(function(){
        var optionNum = $('input[@name^=txtOption_][@name!=txtOption_hide]',formAdd).length;
        var option = $(this).siblings('.txtOption:eq(0)').clone(true);
        option.find('input[@name^=txtOption]').show().attr('name','txtOption_'+optionNum).val('').nextAll('span').hide().eq(0).html('');
        option.find('span[@class*=deleteOption]').addClass('hide');
        $(this).before(option);
        $(this).next('input[@name=hiddenOptionNum]').val(optionNum + 1);
    });
    if(!isAdd){
        var surveyItemID = $('input[@name=SurveyItemID]',formAdd).val();
        var optionIDs = '';
        formAdd.find('span[@class*=deleteOption][@class*=hide]').each(function(){
            optionIDs += $(this).attr('data') + ',';
        });
        if(optionIDs != ''){optionIDs = optionIDs.substring(0,optionIDs.length-1)};
        bindInputEvent(formAdd,surveyItemID,surveyItemID + '|' + optionIDs);
    };
    formAdd.submit(function(){
        var optionNum = $('input[@name^=txtOption_][@name!=txtOption_hide]',formAdd).length;
        $(this).next('input[@name=hiddenOptionNum]').val(optionNum);
        
        $.ajax({
            url:'SurveyItem.aspx?NoCopyRight=1&type=ajax&Action=AddQuestion&SurveyID='+surveyID,
            type:'post',
            data:formAdd.serialize(),
            success:function(data){
                if(!checkLoginStatus(data)){return false;}
                var surveyItemID = data.split('|')[0];  //data = 1|1,2,3
                bindInputEvent(formAdd,surveyItemID,data);
            },//end success
            error:function(a,b){alert('error');alert(b);}
        });//end ajax
        return false;
    });// end submit
};

function bindInputEvent(formAdd,surveyItemID,data){
    formAdd.find('input[@name^=txtQuestion]').attr('name','txtQuestion_hide').hide().siblings('span.textContent').show();
    formAdd.find('[@name=SurveyItemID]').val(surveyItemID);
    formAdd.find('input[@name^=txtOption_]').attr('name','txtOption_hide').hide().siblings('span.textContent').show();  
    
    //删除SurveyItem
    formAdd.find('#deleteQuestion').click(function(){
        if(confirm('确定要删除吗?')){
            $.ajax({
                url:'SurveyItem.aspx?NoCopyRight=1&type=ajax&Action=DeleteQuestion&SurveyItemID='+surveyItemID,
                success:function(data){
                    if(!checkLoginStatus(data)){return false;}
                    if(data>0){formAdd.remove();};
                },
                error:function(){alert('error');}
            });
        };
    })
    .prev().unbind().toggle(function(){
        $(this).html('[保存]').siblings('input[@name^=txtQuestion]').show().next().hide();
        formAdd.find('input[@type=submit]').attr('disabled','disabled');
    },function(){
        var objCurrModify = $(this);
        var objtxtQuestion = objCurrModify.siblings('input[@name^=txtQuestion]');
        $.ajax({
            url:'SurveyItem.aspx?NoCopyRight=1&type=ajax&Action=UpdateQuestionTitle&SurveyItemID='+surveyItemID+'&QuestionName='+escape(objtxtQuestion.val()),
            cache:false,
            async:false,
            success:function(data){
                if(!checkLoginStatus(data)){return false;}
                objtxtQuestion.hide().next().show();
                objCurrModify.html('[修改]');
            },
            error:function(){alert('error');}
        });
        formAdd.find('input[@type=submit]').removeAttr('disabled');
    });
    
    formAdd.find('input[@name=rdChoiceType]').click(function(){
        var obj = $(this);
        $.ajax({
            url:'SurveyItem.aspx?NoCopyRight=1&type=ajax&Action=UpdateChoiceType&SurveyItemID='+surveyItemID+'&ChoiceType='+obj.val(),
            cache:false,
            success:function(data){if(!checkLoginStatus(data)){return false;}},
            error:function(){alert('error');}
        });
    });
    
    var arryOptionIDs = data.split('|')[1].split(',');
    var objDeleteOptions = formAdd.find('span[@class*=deleteOption][@class*=hide]');
    $.each(objDeleteOptions,function(i,n){
        var objCurrDelete = $(this);
        var optionID = arryOptionIDs[i];
        if(i<arryOptionIDs.length && optionID != 0){
            objCurrDelete.unbind().click(function(){
                if(confirm('确定要删除吗?')){
                    $.ajax({
                        url:'SurveyItem.aspx?NoCopyRight=1&type=ajax&Action=DeleteOption&OptionID='+optionID,
                        cache:false,
                        async:false,
                        success:function(data){
                            if(!checkLoginStatus(data)){return false;}
                            if(data > 0){
                                objCurrDelete.parent().remove();//移除一项option
                            }
                        },
                        error:function(){alert('error');}
                    });
                };
            }).removeClass('hide')
            .prev().unbind().toggle(function(){
                $(this).html('[保存]').siblings('input[@name=txtOption_hide]').show().next().hide();
                formAdd.find('input[@type=submit]').attr('disabled','disabled');
            },function(){
                var objCurrModify = $(this);
                var objtxtOption = objCurrModify.siblings('input[@name=txtOption_hide]');
                $.ajax({
                    url:'SurveyItem.aspx?NoCopyRight=1&type=ajax&Action=UpdateOption&OptionID='+optionID+'&OptionName='+escape(objtxtOption.val()),
                    cache:false,
                    async:false,
                    success:function(data){
                        if(!checkLoginStatus(data)){return false;}
                        objtxtOption.hide().next().show();
                        objCurrModify.html('[修改]');
                    },
                    error:function(){alert('error');}                                    
                });
                formAdd.find('input[@type=submit]').removeAttr('disabled');
            });
        }else{
            objCurrDelete.parent().remove();//移除未填写的框
        };
    });//end each
};




function checkLoginStatus(data){
    var result = true;
    if(data.indexOf('<!DOCTYPE') != -1){
        result = false;
        alert('身份已过期,请重新登陆!');
    };
    return result;
};

