function displayAddAnswerButton(select,id) {
	switch (select.value) {
	case '1':
	case '2':
		$(id+'-add-answer-button').show();
		break;
	case '3':
	case '4':
	case '':
			$(id+'-add-answer-button').hide();
			var answercontainer = document.getElementById('magesurvey-answer-container-top-'+id);
			if ( answercontainer.hasChildNodes() )
			{
				while ( answercontainer.childNodes.length >= 1 )
				{
					answercontainer.removeChild( answercontainer.firstChild );       
				} 
			}

		break;
	}	
}

var secondStepTemplate = '<div id="survey_question_{{question_id}}_answer_{{id}}" class="answer_section_{{question_id}}" style="padding:10px 0 0 0;width:100%">'+
            '<input type="hidden" id="survey_question_{{question_id}}_answer_{{id}}_is_delete" name="survey[questions][{{question_id}}][answer][{{id}}]" value="" />'+
			'<input type="hidden" id="answer_item_count" name="survey[questions][{{question_id}}][answer][{{id}}][answer_item_count]" value="{{answeritemCount}}" />'+
			'<input type="hidden" id="answer_id" name="survey[questions][{{question_id}}][answer][{{id}}][answer_id]" value="0" />'+
			//'<input type="text" style="width:71%;margin:0 10px 0 0;" class="required-entry input-text" id="question_{{question_id}}_answer_{{id}}" name="survey[questions][{{question_id}}][answer][{{id}}][title]" value="">'+
			'<textarea style="width:71%;margin:0 10px 0 0;" class="required-entry input-text" id="question_{{question_id}}_answer_{{id}}" name="survey[questions][{{question_id}}][answer][{{id}}][title]" ></textarea>'+
			'<button style="" onclick="removeAnswer({{question_id}},{{id}})" class="scalable delete remove-answer " type="button" id=""><span>Remove</span></button>'+
			'</div>';

function addAnswer(id){
	data = {};
	if ($('answer_item_count') && ($('answer_item_count').value !='')) {
		var answer_item_count = $('answer_item_count').value;
		data.id  = parseInt(answer_item_count) + 1;
	} else { 
		var answer_item_count = 1;
		data.id = 0;	
	}
	data.question_id = id;
	data.answeritemcount = answer_item_count;
	//alert(data.id);
	var template = new Template(secondStepTemplate, /(^|.|\r|\n)({{(\w+)}})/);
	Element.insert('magesurvey-answer-container-top-'+id, {'bottom':template.evaluate(data)});
	$('answer_item_count').value = parseInt(answer_item_count) + 1;
}

function removeAnswer(question_id,answer_id) {
	$('survey_question_'+question_id+'_answer_'+answer_id).hide();
	$('survey_question_'+question_id+'_answer_'+answer_id+'_is_delete').value = 1;
}

function toggleValueElements(checkbox, container){
    if(container && checkbox){
        //var elems = container.select('select', 'input');
        var elems = Element.select(container, ['textarea', 'input', 'select', 'button', 'img']);
        elems.each(function (elem) {
            if(elem!=checkbox) {
                elem.disabled=checkbox.checked;
                if (checkbox.checked) {
                    elem.addClassName('disabled');
                } else {
                    elem.removeClassName('disabled');
                }
                if(elem.tagName == 'IMG') {
                    checkbox.checked ? elem.hide() : elem.show();
                }
            };
        })
    }
}

function toggleValueElementsforQuestion(checkbox, container){
    if(container && checkbox){
        //var elems = container.select('select', 'input');
        var elems = Element.select(container, ['textarea', 'button', 'img']);
        elems.each(function (elem) {
            if(elem!=checkbox) {
                elem.disabled=checkbox.checked;
                if (checkbox.checked) {
                    elem.addClassName('disabled');
                } else {
                    elem.removeClassName('disabled');
                }
                if(elem.tagName == 'IMG') {
                    checkbox.checked ? elem.hide() : elem.show();
                }
            };
        })
    }
}

//-------------Front end--------------------
function starHover(num, sender, questionID) {
	var brightStar = document.getElementById('brightstar').value;
	var greyStar = document.getElementById('greystar').value;
	
	var nextStar;
	
	for(i = 1; i < 6; i++){
		nextStar = document.getElementById('question_' + questionID + '_' + i);
		
		if(i <= num) {
			nextStar.src = brightStar;
		}
		else {
			nextStar.src = greyStar;
		}
	}
}

function starClick(num, questionID) {
	document.getElementById('question_' + questionID).value = num;
}

function starLeave(questionID) {
	num = document.getElementById('question_' + questionID).value;
	
	starHover(num, null, questionID);
}

function getAnswer(questionID,input) {
	
}

