AppraisalURL = location.protocol + '//' +document.location.hostname + "/Appraisal/";

Questionnaire = function() {
	this.Name;
	this.Description;
	this.Id = null;
	this.Questions = new Array();
	
	this.load = load;
	function load(oid) {
		this.Id=oid;
		var dataQuestionnaire;
		$.ajax({
			async:false,
			type: 'GET',
			url: AppraisalURL+"Response/Get.php",
			data: { Service:"Questionnaire",Action:"load",Questionnaire_OID: this.Id},
			success: function(data){
				if(data.error) {
					alert(data.error);
				}
				dataQuestionnaire = data;
			  },
			  dataType: "json"
		});
		
		for(x in dataQuestionnaire) {
			switch(x) {
				case 'Name':
					this.Name = dataQuestionnaire[x];
					break;
				case 'Description':
					this.Description = dataQuestionnaire[x];
					break;
				case 'Questions':
					var position = 1;
					for(y in  dataQuestionnaire[x]) {
						var question = new Question();
						question.Id = y;
						question.Position = position;
						question.Statement = dataQuestionnaire[x][y].Statement;
						for(z in dataQuestionnaire[x][y].Answers) {
							var answer = new Answer();
							answer.Id = z;
							answer.isEven = question.isEvenRow();
							answer.Statement = dataQuestionnaire[x][y].Answers[z][0];
							answer.Value = dataQuestionnaire[x][y].Answers[z][1];
							answer.QuestionId = y;
							question.Answers.push(answer);
						}
						position += 1;
						this.Questions.push(question);
					};
					break;
				
			}
		}
	};

	getAsHTMLEntity = function(){
		var div = document.createElement("div");
		div.setAttribute("id","questionnaire_"+this.Id);
		
		var divTitle = document.createElement('div');
		divTitle.innerHTML = this.Name;
		divTitle.setAttribute('class',"QuestionnaireTitle");
		divTitle.className = "QuestionnaireTitle";
		var divDescription = document.createElement("div");
		divDescription.className = "QuestionnaireDescription";
		divDescription.innerHTML = this.Description;
		divTitle.appendChild(divDescription);
		div.appendChild(divTitle);
		var form = document.createElement('form');
		form.setAttribute("id","questionnaire_form_"+this.Id);
		for(x=0;x<this.Questions.length;x++) {
			form.appendChild(this.Questions[x].getAsHTMLEntity());
			for(y=0;y<this.Questions[x].Answers.length;y++) {
				form.appendChild(this.Questions[x].Answers[y].getAsHTMLEntity());
			}
		}
		div.appendChild(form);
		/**try {
			input = document.createElement("<input type=\"button\" id=\""+this.Id+"\" class=\"ComputeButton\" value=\"Compute\" onclick=\"QuestionnaireHelper.submit("+this.Id+")\" />")
		}
		catch(err) {
			input = document.createElement("input");
			input.setAttribute("type","button");
			input.setAttribute("id",this.Id);
			input.setAttribute("name",this.Id);
			input.setAttribute("class","ComputeButton");
			input.className = "ComputeButton";
			input.value ="Compute";
			Tools.attachEvent(input,'click',function() {
				QuestionnaireHelper.submit(this.id);
			});
		}
		var inputDiv = document.createElement('div');
		inputDiv.setAttribute("class","buttonContainer");
		inputDiv.appendChild(input);
		
		div.appendChild(inputDiv);**/
		return div;
	};
	this.getAsHTMLEntity = getAsHTMLEntity;
};
