var Faq = {
	initialized: false,
	show: function(id) {
		if (!this.initialized) {
			var x = $("faq-dialog");
			x.parentNode.removeChild(x);
			$("body").appendChild(x);
			this.initialized = true;
		}
		request.json("./help/one/id/"+id,function(rs) {
			$("faq-category").innerHTML = rs.category;
			$("faq-title").innerHTML = rs.title;
			$("faq-content").innerHTML = rs.content.rplace("\n","<br/>");
			showandcenter($("faq-dialog"));
			$("faq-dialog").style.left = (parseInt($("faq-dialog").style.left)+100)+"px";
			$("faq-dialog").style.top = "230px";
		});
	},
	cancel: function() {
		$("faq-dialog").style.display = "none";
	}
}

var Q = {
	pageid: 0,
	questionid: 0,
	addpageid: 0,
	breakpageid: 0,
	breakquestionid: 0,
	breakid: 0,
	addquestionid: 0,
	addposivive: 0,
	maxa: 0,
	answers: { },
	answers_extended: { },
	addpage: function() {
		this.pageid = 0;
		showandcenter($("page-dialog"));
		$("page-name").value = "";
		$("page-name").focus();
	},
	editpage: function(id) {
		this.pageid = id;
		showandcenter($("page-dialog"));
		$("page-name").value = $("page-name-"+id).innerHTML;
		$("page-name").focus();
	},
	savepage: function() {
		var vars = {
			id: this.pageid,
			name: $("page-name").value
		};
		request.post("/"+language+"/adminq/savepage", vars, function(rs) {
			Q.cancelpage();
			Q.reloadpage(rs.id);
			Q.pageid = 0;
		});
	},
	cancelpage: function() {
		$("page-dialog").style.display = "none";
	},
	reloadpage: function(id) {
		request.html("/"+language+"/adminq/page/id/"+id, function(h) {
			if ($("page-"+id))
				$("page-"+id).innerHTML = h;
			else
				$("pages").innerHTML += "<div class=\"qpage\" id=\"page-"+id+"\">"+h+"</div>";
		});
	},
	uppage: function(id) {
		request.json("/"+language+"/adminq/uppage/id/"+id, function(rs) {
			if (rs.peer) {
				var x = $("page-"+id);
				var p = x.parentNode;
				p.removeChild(x);
				p.insertBefore(x, $("page-"+rs.peer));
			}
		});
	},
	downpage: function(id) {
		request.json("/"+language+"/adminq/downpage/id/"+id, function(rs) {
			if (rs.peer) {
				var x = $("page-"+rs.peer);
				var p = x.parentNode;
				p.removeChild(x);
				p.insertBefore(x, $("page-"+id));
			}
		});
	},
	upquestion: function(id) {
		request.json("/"+language+"/adminq/upquestion/id/"+id, function(rs) {
			if (rs.id)
				Q.reloadpage(rs.id);
		});
	},
	downquestion: function(id) {
		request.json("/"+language+"/adminq/downquestion/id/"+id, function(rs) {
			if (rs.id)
				Q.reloadpage(rs.id);
		});
	},
	movequestion: function(id) {
		var nw = prompt("Enter the ID of the question to","");
		if (!nw)
			return;
		request.json("/"+language+"/adminq/movequestion/id/"+id+"/by/"+nw, function(rs) {
			Q.reloadpage(rs.id);
		});
	},
	duplicatequestion: function(id, positive) {
		request.json("/"+language+"/adminq/duplicate?id="+id+"&positive="+positive, function(rs) {
			Q.reloadpage(rs.id);
		});
	},
	addbreak: function(pageid, questionid) {
		this.breakpageid = pageid;
		this.breakquestionid = questionid;
		showandcenter($("question-break-dialog"));
		$("question-break-name").value = "";
		$("question-break-name").focus();
	},
	editbreak: function(id) {
		this.breakid = id;
		request.json("/"+language+"/adminq/question/id/"+id, function(rs) {
			showandcenter($("question-break-dialog"));
			$("question-break-name").value = rs.text;
			$("question-break-name").focus();
		});
	},
	savebreak: function() {
		if (this.breakid) {
			request.post("/"+language+"/adminq/editbreak", { id: this.breakid, name: $("question-break-name").value }, function(rs) {
				Q.cancelbreak();
				Q.reloadpage(rs.id);
			});
		} else if (this.breakpageid) {
			request.post("/"+language+"/adminq/addbreak", { page: this.breakpageid, name: $("question-break-name").value }, function(rs) {
				Q.cancelbreak();
				Q.reloadpage(rs.id);
			});
		} else {
			request.post("/"+language+"/adminq/addbreak", { question: this.breakquestionid, name: $("question-break-name").value }, function(rs) {
				Q.cancelbreak();
				Q.reloadpage(rs.id);
			});
		}
	},
	cancelbreak: function() {
		$("question-break-dialog").style.display = "none";
	},
	addquestion: function(id, questionid, positive) {
		this.answers = { };
		this.answers_extended = { };
		this.addpageid = id;
		this.addquestionid = questionid;
		this.addpositive = positive;
		this.questionid = 0;
		$("question-answers-tr").style.display = "none";
		$("question-combo-tr").style.display = "none";
		$("question-ispagebreak").checked = false;
		showandcenter($("question-dialog"));
		selectSelect($("question-type"), "text");
		selectSelect($("question-mandatory"), 0);
		$("question-question").value = "";
		$("question-question").focus();
		dropAllElements($("question-answers"));
	},
	editquestion: function(id) {
		this.answers = { };
		this.answers_extended = { };
		this.questionid = id;
		request.json("/"+language+"/adminq/question/id/"+id, function(rs) {
			showandcenter($("question-dialog"));
			$("question-ispagebreak").checked = rs.pagebreak;
			selectSelect($("question-type"), rs.type);
			selectSelect($("question-mandatory"), rs.mandatory);
			$("question-question").value = rs.text;
			$("question-question").focus();
			dropAllElements($("question-answers"));
			if (rs.type=="radio" || rs.type=="check") {
				$("question-answers-tr").style.display = "table-row";
				$("question-combo-tr").style.display = "none";
				for (var i=0; i<rs.answers.length; i++) {
					var d = document.createElement("div");
					d.id = "answer-"+rs.answers[i].id;
					d.innerHTML = "<input type=\"text\" value=\""+rs.answers[i].text+"\" onchange=\"Q.answers["+rs.answers[i].id+"]=this.value\"/>"+
						"<a id=\"extend-link-"+rs.answers[i].id+"\" class=\"adminbutton\" "+(rs.answers[i].extended?"style=\"background-color:#ccffcc\"":"")+" href=\"javascript:void(0)\" onclick=\"Q.extendanswer('"+rs.answers[i].id+"');return false;\">e</a> "+
						"<a class=\"adminbutton\" href=\"javascript:void(0)\" onclick=\"Q.dropanswer('"+rs.answers[i].id+"');return false;\">x</a>";
					Q.answers[rs.answers[i].id] = rs.answers[i].text;
					Q.answers_extended[rs.answers[i].id] = rs.answers[i].extended;
					$("question-answers").appendChild(d);
				}
			} else if (rs.type=="combo") {
				$("question-answers-tr").style.display = "none";
				$("question-combo-tr").style.display = "table-row";
				$("question-combo-0").value = rs.answers[0].text;
				$("question-combo-1").value = rs.answers[1].text;
				Q.answers[0] = rs.answers[0].text;
				Q.answers[1] = rs.answers[1].text;
			} else {
				$("question-answers-tr").style.display = "none";
				$("question-combo-tr").style.display = "none";
			}
		});
	},
	updateanswers: function() {
		if ($("question-type").value=="text" || $("question-type").value=="area") {
			$("question-answers-tr").style.display = "none";
			$("question-combo-tr").style.display = "none";
		} else if ($("question-type").value=="combo") {
			$("question-answers-tr").style.display = "none";
			$("question-combo-tr").style.display = "table-row";
			$("question-combo-0").value = "";
			$("question-combo-1").value = "";
			this.answers[0] = "";
			this.answers[1] = "";
		} else {
			$("question-answers-tr").style.display = "table-row";
			$("question-combo-tr").style.display = "none";
		}
		showandcenter($("question-dialog"));
	},
	addanswer: function() {
		if (!$("question-new-answer").value.trim())
			return;
		var aid = ++this.maxa;
		var d = document.createElement("div");
		d.id = "answer-x"+aid;
		d.innerHTML = "<input type=\"text\" value=\""+$("question-new-answer").value.rplace("\"","&quot;")+"\"/> "+
			"<a id=\"extend-link-x"+aid+"\" class=\"adminbutton\" href=\"javascript:void(0)\" onclick=\"Q.extendanswer('x"+aid+"');return false;\">e</a> "+
			"<a class=\"adminbutton\" href=\"javascript:void(0)\" onclick=\"Q.dropanswer('x"+aid+"');return false;\">x</a>";
		this.answers["x"+aid] = $("question-new-answer").value;
		$("question-answers").appendChild(d);
		$("question-new-answer").value = "";
		showandcenter($("question-dialog"));
	},
	savequestion: function() {
		if (this.questionid) {
			var vars = {
				"id": this.questionid,
				"text": $("question-question").value,
				"type": $("question-type").value,
				"mandatory": $("question-mandatory").value,
				"pagebreak": $("question-ispagebreak").checked?"1":"0"
			};
			if (vars.type=="radio" || vars.type=="check") {
				var a, idx = 0;
				for (a in this.answers) {
					vars["answer["+(++idx)+"]"] = this.answers[a];
					vars["answerextend["+idx+"]"] = this.answers_extended[a] ? "1" : "0";
				}
			} else if (vars.type=="combo") {
				vars["answer[0]"] = this.answers[0];
				vars["answer[1]"] = this.answers[1];
			}
			request.post("/"+language+"/adminq/savequestion", vars, function(rs) {
				Q.cancelquestion();
				Q.reloadpage(rs.id);
			});
		} else {
			var vars = {
				"pageid": this.addpageid,
				"questionid": this.addquestionid,
				"ifpositive": this.addposivive,
				"text": $("question-question").value,
				"type": $("question-type").value,
				"mandatory": $("question-mandatory").value,
				"ifpositive": this.addpositive ? "1" : "0",
				"pagebreak": $("question-ispagebreak").checked?"1":"0"
			};
			if (vars.type=="radio" || vars.type=="check") {
				var a, idx = 0;
				for (a in this.answers) {
					vars["answer["+(++idx)+"]"] = this.answers[a];
					vars["answerextend["+idx+"]"] = this.answers_extended[a] ? "1" : "0";
				}
			} else if (vars.type=="combo") {
				vars["answer[0]"] = this.answers[0];
				vars["answer[1]"] = this.answers[1];
			}
			request.post("/"+language+"/adminq/savequestion", vars, function(rs) {
				Q.cancelquestion();
				Q.reloadpage(rs.id);
			});
		}
	},
	extendanswer: function(id) {
		if (this.answers_extended[id]) {
			this.answers_extended[id] = false;
			$("extend-link-"+id).style.backgroundColor = "";
		} else {
			this.answers_extended[id] = true;
			$("extend-link-"+id).style.backgroundColor = "#ccffcc";
		}
	},
	droppage: function(id) {
		if (!confirm("Are you sure you want to delete this page?"))
			return;
		request.json("/"+language+"/adminq/droppage/id/"+id, function(rs) {
			dropElement($("page-"+id));
		});
	},
	dropquestion: function(id) {
		if (!confirm("Are you sure you want to delete this question?"))
			return;
		request.json("/"+language+"/adminq/dropquestion/id/"+id, function(rs) {
			Q.reloadpage(rs.id);
		});
	},
	dropanswer: function(id) {
		dropElement($("answer-"+id));
		this.answers[id] = null;
	},
	cancelquestion: function() {
		$("question-dialog").style.display = "none";
	},
	switchquestions: function(id) {
		if ($("question-switchable-"+id).style.display=="none") {
			$("question-switchable-"+id).style.display = "block";
			$("question-switcher-"+id).innerHTML = "&rarr;";
		} else {
			$("question-switchable-"+id).style.display = "none";
			$("question-switcher-"+id).innerHTML = "&darr;";
		}
	}
};



var FQ = {
	currentpage: "",
	breakquestion: { },
	triggers: [ ],
	showKids: function(id, mode) {
		if (mode=="0") {
			$("question-kids-"+id+"-negative").innerHTML = "Loading...";
			$("question-kids-"+id+"-negative").style.display = mode=="0" ? "block" : "none";
			$("question-kids-"+id+"-positive").style.display = mode=="1" ? "block" : "none";
		} else {
			$("question-kids-"+id+"-positive").innerHTML = "Loading...";
			$("question-kids-"+id+"-negative").style.display = mode=="0" ? "block" : "none";
			$("question-kids-"+id+"-positive").style.display = mode=="1" ? "block" : "none";
		}
		request.html("/"+language+"/questionnaire/partial?id="+id+"&positive="+mode, function(h) {
			var script = "";
			var x1 = h.explode("<script>");
			for (var i=1; i<x1.length; i++) {
				var x2 = x1[i].explode("</script>");
				script += x2[0];
			}
			if (mode=="0")
				$("question-kids-"+id+"-negative").innerHTML = h;
			else
				$("question-kids-"+id+"-positive").innerHTML = h;
			eval(script);
			$("content").style.height = ($("content").clientHeight+(mode=="0"?$("question-kids-"+id+"-negative").clientHeight:(mode=="1"?$("question-kids-"+id+"-positive").clientHeight:0)))+"px";
			if (FQ.breakquestion[id])
				for (var i=0; i<FQ.breakquestion[id].length; i++)
					$("question-break-"+FQ.breakquestion[id][i]).style.display = "block";
	
			if (FQ.triggers.contains(id)) {
				FQ.triggers = FQ.triggers.removevalue(id);
				if (FQ.triggers.length==0)
					$("submit-button").style.display = "block";
			}
	
			window.onload();
		});
//		window.xload["init"]();
	},
	validate: function(mandatory, types, depends, positive) {
		var result = true;
		
		var f = document.forms.questionnaire;
		for (var i=0; i<mandatory.length; i++) {
			if (depends[mandatory[i]]) {
				if (f.elements["answer["+depends[mandatory[i]]+"]"].value=="" || positive[mandatory[i]] && f.elements["answer["+depends[mandatory[i]]+"]"].value=="0" || !positive[mandatory[i]] && f.elements["answer["+depends[mandatory[i]]+"]"].value=="1")
					continue;
			}
			var failed = false;
			if (types[mandatory[i]]=="text" && !f.elements["answer["+mandatory[i]+"]"].value.trim())
				failed = true;
			else if (types[mandatory[i]]=="area" && !f.elements["answer["+mandatory[i]+"]"].value.trim())
				failed = true;
			else if (types[mandatory[i]]=="combo" && !f.elements["answer["+mandatory[i]+"]"].value.length)
				failed = true;
			else if (types[mandatory[i]]=="check") {
				var x = f.elements["answer["+mandatory[i]+"][]"];
				var gotit = false;
				for (var j=0; j<x.length; j++)
					if (x[j].checked) {
						gotit = true;
						break;
					}
				if (!gotit)
					failed = true;
			} else if (types[mandatory[i]]=="radio") {
				var x = f.elements["answer["+mandatory[i]+"]"];
				var gotit = false;
				for (var j=0; j<x.length; j++)
					if (x[j].checked) {
						gotit = true;
						break;
					}
				if (!gotit)
					failed = true;
			}
			if (failed) {
				alert("Please answer fill all mandatory fields\n\n"+$("question-question-"+mandatory[i]).innerHTML.stripTags());
				result = false;
				break;
			}
		}
		
		if (result==false && this.currentpage) {
			$(this.currentpage).style.display = "none";
//			$("question-page-0").style.display = "block";
			$("question-question-"+mandatory[i]).parentNode.parentNode.style.display = "block";
		}
		
		return result;
	},
	showSubpage: function(id) {
		this.currentpage = "question-page-"+id;
		$("question-break-"+id).parentNode.style.display = "none";
		$("question-page-"+id).style.display = "block";

		resizeitall(Math.max(600, $("content").clientHeight, $("question-page-"+id).clientHeight+100));
	},
	submitTrigger: function(id) {
		this.triggers.push(id);
		window.xload["submit-button"] = function() {
			window.xload["submit-button"] = null;
			$("submit-button").style.display = "none";
		}
	},
	connectBreakToQuestion: function(breakid,questionid) {
		if (this.breakquestion[questionid])
			this.breakquestion[questionid].push(breakid);
		else
			this.breakquestion[questionid] = [ breakid ];
		$("question-break-"+breakid).style.display = "none";
	}
};

function resizeitall(h) {
	$("left").style.height = h+"px";
	$("content").style.height = h+"px";
	$("menu").style.height = (h-78-73-13)+"px";
	$("content-left").style.height = h+"px";
	$("content-right").style.height = h+"px";
	$("bottommenu").style.top = (h+86)+"px";
}


