// JavaScript Document// JavaScript Document
//questionnaire form
var qsForm = Class.create({
	
	//initialise function
	initialize: function(id, outIDYes, outIDNo, qsSubmit, qsResubmit,nextStep) {
        if(!$(id)) throw("Attempted to initalize questionnaire with id: "+ id + " which was not found.");
		this.qsDiv = $(id);
		this.outDivY = $(outIDYes);
		this.outDivN = $(outIDNo);
		this.qsSubmit = $(qsSubmit);
		this.qsResubmit = $(qsResubmit);
		this.nextStep = $(nextStep);
			
		//hide all
		this.outDivY.hide();
		this.outDivN.hide();
		this.nextStep.hide();
		this.qsResubmit.hide();
		
		
		
		//submiting the form
		this.qsDiv.observe('submit', function(e) {
			//temporary stop the form.... don't quite understand but its needed
			Event.stop(e);
			this.qsresults = 0;
			this.noans = 0;
			for (var i=1; i<10; i++) {
				
				var tv = this.rf('q'+i);
				if (tv == 'yes') {
					this.qsresults++;
				}
				else if (tv != 'no') {
					this.noans++;
				}
			}
			
			if (this.noans<=0 && this.qsresults>=3) {
				this.outDivY.show();
				this.outDivN.hide();
				this.qsSubmit.hide();
				this.qsResubmit.show();
				this.nextStep.show();
			}
			else if (this.noans<=0 && this.qsresults<=3) {
				this.outDivY.hide();
				this.outDivN.show();
				this.qsSubmit.hide();
				this.qsResubmit.show();
				this.nextStep.show();
			}
			else {
				this.outDivY.hide();
				this.outDivN.hide();
				alert ("請完成全部診斷問題!");

			}
				
			
		}.bindAsEventListener(this));
		
		//resubmit
		this.qsDiv.observe ('reset', function(e) {
				
				//alert('cancel');
				this.outDivY.hide();
				this.outDivN.hide();
				this.qsSubmit.show();
				this.nextStep.hide();
				this.qsResubmit.hide();
		}.bindAsEventListener(this));
		
	},
	
	rf : function (el, radioGroup) {
		if($(el).type && $(el).type.toLowerCase() == 'radio') {
			var radioGroup = $(el).name;
			var el = $(el).form;
		} else if ($(el).tagName.toLowerCase() != 'form') {
			return false;
		}
	
		var checked = $(el).getInputs('radio', radioGroup).find(
			function(re) {return re.checked;}
		);
		return (checked) ? $F(checked) : null;
	}
	
});

document.observe("dom:loaded", function(){
qsform = new qsForm ('treatment-planner-question','questionnaire-results-yes','questionnaire-results-no','question_submit', 'question_resubmit','question_nextstep');	
})
