var pdq;
var allquest = 0;
var direction = '';

/*
* getFldVal : store fld value before appendChild, because for MSIE we need to restore it 
*/
function getFldVal(dv) {
	var c = dv.getElementsByTagName('INPUT');
	z = new Array();
	for(var y=0;y<c.length;y++) z[y] = c[y].checked;
	return z;
}

/*
* setFldVal : restore fld value after appendChild, because MSIE won't save it 
*/
function setFldVal(dv, z) {
	var c = dv.getElementsByTagName('INPUT');
	for(var y=0;y<c.length;y++) c[y].checked = z[y];
	return true;
}

function dispquest(qe,dir) {
	ProcessWaitLabel(true);
	setTimeout("ProcessQuestionDiv("+qe+",'"+dir+"')",200);
}

function ProcessWaitLabel(mode) {
	if(mode) {
		document.getElementById('wait_div').style.display='block';
	} else {
		document.getElementById('wait_div').style.display='none';
	}
}

function ProcessQuestionDiv(qe,dir) {
	var frm = document.getElementById('thesurvey');
	
	direction = dir;
	var x = dq;
	if(x > tq) x = tq;
	
	var tdiv,xdiv;
	var c,y,z;
	var cq;
	var tqq;
	
	pdq = Math.max(1,dq);
	
	if (qe) {
		dq = parseInt(qe);
	} else if (qe == 0 || typeof(qe) == undefined) {
		dq = 1;
	} else {
		dq = Math.max(1,(dir == 'back') ? dq-1 : dq+1);
	}

	if((dq > 1) && (dir != 'back') && x) {
		tdiv = document.getElementById('q'+x);
		z = getFldVal(qdiv);
		setFldVal(tdiv, z);
		cq = document.getElementById('q'+x);
		tqq = cq.getAttribute('q');
		if(!QuestionWellFilled(frm, QuestionElements(frm, "q["+tqq+"][answer]"))) dq = x;
	}

	dq = GetNextQuestion(frm, dq, dir);
	if (dq > tq) {
		var submit_ok = SubmitSurvey();
		ProcessWaitLabel(false);
		return submit_ok;
	}

	if(dq >= tq) {
		if (r = getCssRule('.questnav1')) r.style.display = 'block';
		if (r = getCssRule('.questnav2')) r.style.display = 'none';
		if (r = getCssRule('.questnav3')) r.style.display = 'block';
	} else {
		if (r = getCssRule('.questnav1')) r.style.display = 'block';
		if (r = getCssRule('.questnav2')) r.style.display = 'block';
		if (r = getCssRule('.questnav3')) r.style.display = 'none';
	}

	if(dq < 1) dq = 1;
	if(dq != x) {

		if (x > 0) {
			tdiv = document.getElementById('q'+x);
			z = getFldVal(qdiv);
			tdiv.appendChild(qdiv.firstChild);
			setFldVal(tdiv, z);
		}

		if (!allquest) {
			xdiv = document.getElementById('q'+dq);
			if (xdiv) {
				if (document.getElementById('cursub')) {
					document.getElementById('cursub').innerHTML = xdiv.getAttribute('cat');
					document.getElementById('cursubtext').innerHTML = xdiv.getAttribute('oms');
				}
				if ( xdiv.firstChild ) {
					if (xdiv.firstChild.getAttribute('ondisplay')) {
						eval(xdiv.firstChild.getAttribute('ondisplay'));
					}
		
					z = getFldVal(xdiv);
					qdiv.appendChild(xdiv.firstChild);
					x = xdiv.childNodes.length;
					if (x>0) {
						var tmp = xdiv.childNodes[0];
						xdiv.removeChild(tmp);
					}
					setFldVal(qdiv,z);
				}
			}
		}
	}
	UpdateProgress();
	ProcessWaitLabel(false);
}


function GetNextQuestion(frm, curq, dir) {
	cq = document.getElementById('q'+curq);
	if (cq) {
		tqq = cq.getAttribute('q');
		while (!QuestionOnlyIf(frm, tqq)) {
			ClearQuestion(frm, QuestionElements(frm,"q["+tqq+"]"));
			curq = Math.max(0,(dir == 'back') ? curq-1 : curq+1);
			cq = document.getElementById('q'+curq);
			if (cq) {
				tqq = cq.getAttribute('q');
			} else {
				break;
			}
		}
	}
	return curq;
}

function UpdateProgress() {
}

function getCssRule(seltext) {
	var stylesheets = document.styleSheets;
	var s,r,sheetrules,currule,curstate;
	for (s=0;s<stylesheets.length;s++) {
		if (stylesheets[s].href != '' && stylesheets[s].href != location.href) continue;
		sheetrules = stylesheets[s].cssRules ? stylesheets[s].cssRules : stylesheets[s].rules;
		for (r=0;r<sheetrules.length;r++) {
			currule = sheetrules[r];
			if (currule.selectorText.toLowerCase() == seltext) return currule;
		}
	}
	return false;
}

function disp_all_questions() {
	allquest = 1;
	dispquest(dq);
	if (r = getCssRule('.dvimg')) r.style.display = 'none';
	if (r = getCssRule('.vraag')) r.style.height = 'auto';
	if (r = getCssRule('.questnav1')) r.style.display = 'none';
	if (r = getCssRule('.questnav2')) r.style.display = 'none';
	if (r = getCssRule('.questnav3')) r.style.display = 'block';
	if (r = getCssRule('.dvvraag')) r.style.display = 'block';
	if (r = getCssRule('.tbltoelichting')) r.style.backgroundColor = '#d1e2e5';
	if (r = getCssRule('.dvanswer')) r.style.backgroundColor = '#f5f8f9';
}

function SubmitFormEnquete(frm) {
	obj = document.forms[frm];
	if(obj && CheckEnquete(obj)) {
		return submitForm(obj);
	}
	return false;
}

function CheckEnquete(frm) {
	if (ql_name) {
		for(key in ql_name) {
			if (key != '') {
				var id = ql_name[key];
				if(!QuestionMandatory(frm, id)) continue;
				if(!QuestionOnlyIf(frm, id)) ClearQuestion(frm, QuestionElements(frm, "q[" + id + "]"));
			}
		}
	}
	return true;
}

function SubmitSurvey() {
	return SubmitFormEnquete('thesurvey');
}

function GetElementType(elm) {
	if(elm.length) {
		return elm[0].type;
	} else {
		return (elm.type.substring(0,6) == "select") ? "select" : elm.type;
	}
}

function QuestionElements(frm, id) {
	var QE = new Array();
	QE['name'] = id;
	
	var fe = frm.elements;
	if (fe[id]) {
		QE['object'] = fe[id];
		QE['type'] = GetElementType(QE['object']);
	} else {
		var obj = new Array();
		var len = id.length;
		for (i=0;i<fe.length;i++) {
			if (fe[i].name.substring(0,len) == id) {
				if(fe[i].name.length > len) {
					if(!obj[fe[i].name]) obj[fe[i].name] = fe[i];
				}
			}
		}
		QE['object'] = new Array();
		var x = 0;
		for (z in obj) QE['object'][x++] = QuestionElements(frm, obj[z].name);
		QE['type'] = "collection";
	}
	return QE;
}

function GetQuestionValue(frm, elm) {
	if(!elm['type']) return false;
	
	var value = '';
	if(elm['type'] == 'collection') {
		value = new Array();
		for(i=0;i<elm['object'].length;i++) {
			value[i] = GetQuestionValue(frm, elm['object'][i]);
		}
		return value;
	}

	var fe = frm.elements;
	var obj = new Array();
	var x = 0;
	for (i=0;i<fe.length;i++) {
		if (fe[i].name == elm['name']) obj[x++] = fe[i];
	}
	if(!obj.length) return '';

	if(elm['type'] == 'radio') {
		for (i=0;i<obj.length;i++) {
			if(obj[i].checked) return obj[i].value;
		}
		return '';
	}

	if(elm['type'] == 'select') {
		if( obj[0].type == "select-one" && !obj[0].container ){
			return (obj[0].selectedIndex == -1) ? "" : obj[0][obj[0].selectedIndex].value;
		} else {
			for( var i=0; i < obj[0].options.length; i++ ){
				if( (obj[0].options[i].selected || obj[0].container) && (!obj[0].dummyContainer) ) {
					if(value == '') value = new Array();
					value[value.length] = obj[0].options[i].value;
				}
			}
			return value;
		}
	}

	if(elm['type'] == 'checkbox') return (obj[0].checked ? obj[0].value : '');
	return (elm['type'] ? obj[0].value : '');
}

function SetQuestionValue(frm, elm, value) {
	if(!elm['type']) return false;
	if(elm['type'] == 'collection') return false;

	var fe = frm.elements;
	var obj = new Array();
	var x = 0;
	for (i=0;i<fe.length;i++) {
		if (fe[i].name == elm['name']) obj[x++] = fe[i];
	}
	if(!obj.length) return false;

	if(elm['type'] == 'radio') {
		for (i=0;i<obj.length;i++) {
			if(obj[i].value == value) {
				obj[i].checked = true;
				return true;
			}
		}
		return false;
	}

	if(elm['type'] == 'select') {
		for( var i=0; i < obj[0].options.length; i++ ){
			if(obj[0].options[i].value == value) {
				obj[0].options[i].selected = true;
				return true;
			}
		}
		return false;
	}

	if(elm['type'] == 'checkbox') {
		obj[0].checked = value ? true : false;
		return true;
	}
	obj[0].value = value;
	return true;
}

function ClearQuestion(frm, elm) {
	if(!elm || !elm['type']) return false;
	
	if(elm['type'] == 'collection') {
		for(i=0;i<elm['object'].length;i++) {
			if(!ClearQuestion(frm, elm['object'][i])) return false;
		}
		return true;
	}
	
	var fe = frm.elements;
	var obj = new Array();
	var x = 0;
	for (i=0;i<fe.length;i++) {
		if (fe[i].name == elm['name']) obj[x++] = fe[i];
	}
	if(!obj.length) return false;
	
	if(elm['type'] == "select") {
		obj[0].selectedIndex = -1;
		for (var i=0; i<obj[0].options.length; i++ ) obj[0].options[i].selected = false;
		return true;
	}
	if( (elm['type'] == "checkbox") || (elm['type'] == "radio") ) {
		if (obj.length > 1) {
			for(var i=0; i<obj.length;i++) obj[i].checked = false;
		} else {
			obj[0].checked = false;
		}
		return true;
	}
	if(elm['type'] == "textarea") {
		obj[0].value = '';
		return true;
	}

	if(obj.length>1){
		for(var i=0; i<obj.length;i++) obj[i].value = '';
	} else {
		obj[0].value = '';
	}
	return true;
}

function QuestionHasValue(frm, elm) {
	if(!elm || !elm['type']) {
		alert("HasValue error");
		return false;
	}
	if(elm['type'] == 'collection') {
		for(var i=0;i<elm['object'].length;i++) {
			if(QuestionHasValue(frm, elm['object'][i])) return true;
		}
	} else {
		var qv = GetQuestionValue(frm, elm).toString();
		if(elm['type'] == 'radio') {
			if(qv && (qv != '0') ) return true;
		} else {
			if(qv) return true;
		}
	}
	return false;
}

function QuestionWellFilled(frm, elm, alert_off) {
	if(!QuestionHasValue(frm, elm)) {
		if(QuestionMandatory(frm, tqq)) {
			if(!alert_off) alert('Maak a.u.b. een keuze!');
			return false;
		}
	} else {
		if(elm['type'] == 'collection') {
			for(var i=0;i<elm['object'].length;i++) {
				var qv = GetQuestionValue(frm, elm['object'][i]);

				qcid = GetChoiceId(elm['object'][i]['name']);
				var qo = QuestionElements(frm, "q["+tqq+"][other]["+qcid+"]");
				
				if(elm['object'][i]['type'] == 'radio') {
					if(qv == '0') {
						if(qo['type'] == 'text') {
							if(QuestionHasValue(frm, qo)) {
								if(!alert_off) alert("Question not fully answered");
								return false;
							}
						} else {
							if(!alert_off) alert("Question not fully answered");
							return false;
						}
					}
				}

				
				if( (qv != '0') && (qv != '') ) {
					if(qo['type'] == 'text') {
						if(!QuestionHasValue(frm, qo)) {
							if(!alert_off) alert("Question's textfield needs to be answered");
							return false;
						}
					}
				}
			}
		} else {
			var qv = GetQuestionValue(frm, elm);
			/*
			var qo = QuestionElements(frm, "q["+tqq+"][other]["+qv+"]");
			if(qo['type'] == 'text') {
				if(!QuestionHasValue(frm, qo)) {
					if(!alert_off) alert("Question's textfield needs to be answered");
					return false;
				}
			}
			*/
		}
	}
	return true;
}

function CheckQuestion(frm, elm, value) {
	if(!elm || !elm['type']) return false;
	if(elm['type'] == 'collection') return false;
	return ( (GetQuestionValue(frm, elm) == value) ? true : false);
}

function QuestionOnlyIf(frm, id) {

	// Format: <question>.<choice>=<value>|<value>|<va.. , <question>.<choice>=<value>|<value>|<va..
	var only_if = ql_onlyif[id];
	if(!only_if || !only_if.length || (only_if == "-")) return true;
	var ifcount = only_if.split(',');
	for(var i=0;i<ifcount.length;i++) {
		
		var ifparts = ifcount[i].split('=');
		if(ifparts.length < 2) continue;

		var question = ifparts[0].split('.');
		var values = ifparts[1].split('|');
		if(!question.length || !values.length) continue;
		
		for(var check=0;check<values.length;check++) {
			if(!question[1]) {
				if(CheckQuestion(frm, QuestionElements(frm, "q[" + ql_name[question[0]] + "][answer]"), values[check])) return true;
			} else {
				if(CheckQuestion(frm, QuestionElements(frm, "q[" + ql_name[question[0]] + "][answer][" + question[1] + "]"), values[check])) return true;
			}
		}
	}
	return false;
}

function QuestionMandatory(frm, id) {
	if(!ql_onlyif[id]) return true;
	return ( (ql_onlyif[id] == "-") ? false : true);
}

function GetQuestionId(qname) {
	for(i=1; i<=tq; i++) {
		question = document.getElementById('q'+i);
		if (question) {
			tqq = question.getAttribute('q');
			if(tqq == ql_name['' + qname + '']) return i;
		}
	}
	return 1;
}

function GetChoiceId(qname) {
	var result = '';
	for(var i=qname.length-1; i>=0;i--) {
		if(qname.charAt(i) == '[') break;
		if(qname.charAt(i) != ']') result = qname.charAt(i) + result;
	}
	return result;
}

