var questions_limit = 100;
var choices_limit = 50;
var uploads_limit = 25;
var statements_limit = 25;
var conditions_limit = 100;

function elems_on(els) { for (i=0; i<els.length; i++) { elem_on(els[i]); }; }
function disps_off(els) { for (i=0; i<els.length; i++) { disp_off(els[i]); }; }
function elems_off(els) { for (i=0; i<els.length; i++) { elem_off(els[i]); }; }

function elem_on(elname) {
	obj = document.getElementById(elname);
	if (obj) { obj.disabled=false; }
}
function elem_off(elname) {
	obj = document.getElementById(elname);
	if (obj) { obj.disabled=true; }
}
function disp_on(elname) {
	obj = document.getElementById(elname);
	if (obj) { obj.style.display='block'; }
}
function disp_off(elname) {
	obj = document.getElementById(elname);
	if (obj) { obj.style.display='none'; }
}


function getDivAttribute(idName, attrName) {
	obj = document.getElementById(idName);
	if (obj) { return parseInt(obj.getAttribute(attrName)); }
	return 0;
}
function setDivAttribute(idName, attrName, val){
	obj = document.getElementById(idName);
	if (obj) { obj.setAttribute(attrName, parseInt(val)); }
}

function change_type(n, newtype) {
	// serve? var old_type = get_type(qNum); serve se devo controllare le transizioni all'interno di 0,1,2; per ora lascio perdere
	var j = 0;
	if (newtype == 3) {
		if (getDivAttribute("question"+n, "created_choices") > 0) {
			setDivAttribute("question"+n, "active_choices", 1);
			for (j=2; j<=getDivAttribute("question"+n, "created_choices"); j++) { 
				elems_off(['choicesText[' + n + '|' + j + ']', 'choicesShorttext[' + n + '|' + j + ']']);
			}
			elem_off('delChoiButt'+n); 
			elem_off('addChoiButt'+n);
			disp_on('choicesButtons'+n);
		}	
		else {
			elem_off('delChoiButt'+n); 
			elem_on('addChoiButt'+n);
			disp_on('choicesButtons'+n);
		}
	}
	if ((newtype == 0) || (newtype == 1) || (newtype == 2)) {
		setDivAttribute("question"+n, "active_choices", getDivAttribute("question"+n, "created_choices"));
		for (j=1; j<=getDivAttribute("question"+n, "created_choices"); j++) { 
			elems_on(['choicesText[' + n + '|' + j + ']', 'choicesShorttext[' + n + '|' + j + ']']);
		}
		elem_on('delChoiButt'+n); 
		if (getDivAttribute("question"+n, "created_choices")<choices_limit) elem_on('addChoiButt'+n);
		disp_on('choicesButtons'+n);
	}
	if (newtype == 99) {
		setDivAttribute("question"+n, "active_choices", 0);
		for (j=1; j<=getDivAttribute("question"+n, "created_choices"); j++) { 
			elems_off(['choicesText[' + n + '|' + j + ']', 'choicesShorttext[' + n + '|' + j + ']']);
		}
		elem_off('delChoiButt'+n); 
		elem_off('addChoiButt'+n);
	}
	setDivAttribute("question"+n, "choices_type", newtype);
}

///////////////////////////////////////////////////////////
function questionsRowAdd() {
	var ql = document.getElementById("questionsList");
	var activeQuestions = getDivAttribute("questionsList", "active_questions");
	var createdQuestions = getDivAttribute("questionsList", "created_questions");
	activeQuestions++;
	setDivAttribute("questionsList", "active_questions", activeQuestions);
	
	if (activeQuestions > createdQuestions) {
		var question0 = document.getElementById("question0");
		var cloneQuestion = question0.cloneNode(true);
		cloneQuestion.setAttribute("id", "question"+activeQuestions);
		cloneQuestion.setAttribute("style", "padding-top: 5px;");
		//var childTR = cloneQuestion.firstChild.nextSibling.firstChild.nextSibling.firstChild;
		//var childSelectType = childTR.childNodes[3].childNodes[1];
		var TDList = cloneQuestion.firstChild.nextSibling.firstChild.nextSibling.firstChild.getElementsByTagName('td');
		var childSelectType = TDList[1].childNodes[1];
		childSelectType.setAttribute("id", "type"+activeQuestions);	
		childSelectType.setAttribute("name", "type"+activeQuestions);	
		childSelectType.setAttribute("onchange", "change_type("+activeQuestions+", this.value)");	
		//var childInputText = childTR.childNodes[7].childNodes[1];
		var childInputText = TDList[3].childNodes[1];
		childInputText.setAttribute("id", "text"+activeQuestions);	
		childInputText.setAttribute("name", "text"+activeQuestions);	
		//var childInputShorttext = childTR.childNodes[11].childNodes[1];
		var childInputShorttext = TDList[5].childNodes[1];
		childInputShorttext.setAttribute("id", "shorttext"+activeQuestions);	
		childInputShorttext.setAttribute("name", "shorttext"+activeQuestions);	
		ql.appendChild(cloneQuestion);
		setDivAttribute("questionsList", "created_questions", activeQuestions);
		
		var choicesList0 = document.getElementById("choicesList0");
		var cloneChoicesList = choicesList0.cloneNode(true);
		cloneChoicesList.setAttribute("id", "choicesList"+activeQuestions);
		cloneChoicesList.setAttribute("style", "padding-top: 5px;");
		ql.appendChild(cloneChoicesList);
		
		var choicesButtons0 = document.getElementById("choicesButtons0");
		var cloneChoicesButtons = choicesButtons0.cloneNode(true);
		cloneChoicesButtons.setAttribute("id", "choicesButtons"+activeQuestions);
		var INPUTList = cloneChoicesButtons.getElementsByTagName('input');
		//var childAddButt = cloneChoicesButtons.childNodes[0];
		var childAddButt = INPUTList[0];
		childAddButt.setAttribute("id", "addChoiButt"+activeQuestions);	
		childAddButt.setAttribute("onclick", "choicesRowAdd("+activeQuestions+")");	
		//var childDelButt = cloneChoicesButtons.childNodes[2];
		var childDelButt = INPUTList[1];
		childDelButt.setAttribute("id", "delChoiButt"+activeQuestions);	
		childDelButt.setAttribute("onclick", "choicesRowDel("+activeQuestions+")");	
		ql.appendChild(cloneChoicesButtons);
	}
	else elems_on(['text' + activeQuestions, 'shorttext' + activeQuestions, 'type' + activeQuestions]);
	if (activeQuestions > 0) {
		elem_on('delQuestButt');
		if (activeQuestions >= questions_limit) elem_off('addQuestButt');
	}	
}

function questionsRowDel() {
	var activeQuestions = getDivAttribute("questionsList", "active_questions");
	elems_off(['text' + activeQuestions, 'shorttext' + activeQuestions, 'type' + activeQuestions]);
	activeQuestions--;
	setDivAttribute("questionsList", "active_questions", activeQuestions);
	if (activeQuestions <= 0) elem_off('delQuestButt'); 
	if (activeQuestions < questions_limit) elem_on('addQuestButt');
}

function choicesRowAdd(n) {
	var cl = document.getElementById("choicesList"+n);
	var activeChoices = getDivAttribute("question"+n, "active_choices");
	var createdChoices = getDivAttribute("question"+n, "created_choices");
	activeChoices++;
	setDivAttribute("question"+n, "active_choices", activeChoices);
	if (activeChoices > createdChoices) {
		var choice00 = document.getElementById("choice[0|0]");
		var cloneChoice = choice00.cloneNode(true);
		cloneChoice.setAttribute("id", "choice["+n+"|"+activeChoices+"]");
		cloneChoice.setAttribute("style", "padding-top: 5px;");
		//var childChoiceTR = cloneChoice.firstChild.nextSibling.firstChild.nextSibling.firstChild;
		//var childChoiceInputText = childChoiceTR.childNodes[3].childNodes[1];
		var childChoiceTDList = cloneChoice.firstChild.nextSibling.firstChild.nextSibling.firstChild.getElementsByTagName('td');
		var childChoiceInputText = childChoiceTDList[1].childNodes[1];
		childChoiceInputText.setAttribute("id", "choicesText["+n+"|"+activeChoices+"]");	
		childChoiceInputText.setAttribute("name", "choicesText["+n+"|"+activeChoices+"]");	
		//var childChoiceInputShorttext = childChoiceTR.childNodes[7].childNodes[1];
		var childChoiceInputShorttext = childChoiceTDList[3].childNodes[1];
		childChoiceInputShorttext.setAttribute("id", "choicesShorttext["+n+"|"+activeChoices+"]");	
		childChoiceInputShorttext.setAttribute("name", "choicesShorttext["+n+"|"+activeChoices+"]");	
		cl.appendChild(cloneChoice);
		setDivAttribute("question"+n, "created_choices", activeChoices);
	}	
	else elems_on(['choicesText[' + n + '|' + activeChoices + ']', 'choicesShorttext[' + n + '|' + activeChoices + ']']);
	
	var type = getDivAttribute("question"+n, "choices_type");
	if (type == 3) 
		if (activeChoices > 0) {
			elem_off('delChoiButt'+n); 
			elem_off('addChoiButt'+n);
		}	
	if (type == 0 || type == 1 || type == 2) 
		if (activeChoices > 1) {
			elem_on('delChoiButt'+n); 
			if (activeChoices >= choices_limit) elem_off('addChoiButt'+n); 
		}
}
function choicesRowDel(n) {
	var activeChoices = getDivAttribute("question"+n, "active_choices");
	elems_off(['choicesText[' + n + '|' +activeChoices + ']', 'choicesShorttext[' + n + '|' +activeChoices + ']']);
	activeChoices--;
	setDivAttribute("question"+n, "active_choices", activeChoices);
	var type = getDivAttribute("question"+n, "choices_type");
	if (type == 0 || type == 1 || type == 2) {
		if (activeChoices <= 1) elem_off('delChoiButt'+n);
		if (activeChoices < choices_limit) elem_on('addChoiButt'+n);
	}	
}

function uploadsRowAdd() {
	var ul = document.getElementById("uploadsList");
	var activeUploads = getDivAttribute("uploadsList", "active_uploads");
	var createdUploads = getDivAttribute("uploadsList", "created_uploads");
	activeUploads++;
	setDivAttribute("uploadsList", "active_uploads", activeUploads);
	
	if (activeUploads > createdUploads) {
		var upload0 = document.getElementById("upload0");
		var cloneUpload = upload0.cloneNode(true);
		cloneUpload.setAttribute("id", "upload"+activeUploads);
		cloneUpload.setAttribute("style", "padding-top: 5px;");
		//var childTR = cloneUpload.firstChild.nextSibling.firstChild.nextSibling.firstChild;
		//var childName = childTR.childNodes[3].childNodes[1];
		var TDList = cloneUpload.firstChild.nextSibling.firstChild.nextSibling.firstChild.getElementsByTagName('td');
		var childName = TDList[1].childNodes[1];
		childName.setAttribute("id", "upload_name"+activeUploads);	
		childName.setAttribute("name", "upload_name"+activeUploads);	
		//var childSelectMandatory = childTR.childNodes[7].childNodes[1];
		var childSelectMandatory = TDList[3].childNodes[1];
		childSelectMandatory.setAttribute("id", "upload_mandatory"+activeUploads);	
		childSelectMandatory.setAttribute("name", "upload_mandatory"+activeUploads);	
		//var childInstructions = childTR.childNodes[11].childNodes[1];
		var childInstructions = TDList[5].childNodes[1];
		childInstructions.setAttribute("id", "upload_help"+activeUploads);	
		childInstructions.setAttribute("name", "upload_help"+activeUploads);	
		//var childSelectLocked = childTR.childNodes[15].childNodes[1];
		var childSelectLocked = TDList[7].childNodes[1];
		childSelectLocked.setAttribute("id", "upload_locked"+activeUploads);	
		childSelectLocked.setAttribute("name", "upload_locked"+activeUploads);	
		childSelectLocked.setAttribute("onchange", "uploadsChangeLocked("+activeUploads+");");	
		//var childInstructions = childTR.childNodes[19].childNodes[1];
		var childInstructions = TDList[9].childNodes[1];
		childInstructions.setAttribute("id", "upload_instructions"+activeUploads);	
		childInstructions.setAttribute("name", "upload_instructions"+activeUploads);	
		ul.appendChild(cloneUpload);
		setDivAttribute("uploadsList", "created_uploads", activeUploads);
	}
	else {
		elems_on(['upload_name' + activeUploads, 'upload_mandatory' + activeUploads, 'upload_locked' + activeUploads, 'upload_help' + activeUploads]);
		if (document.getElementById("upload_locked" + activeUploads).value == 1) elem_on(['upload_instructions' + activeUploads]);
	}
	if (activeUploads > 0) {
		elem_on('delUplButt');
		if (activeUploads >= uploads_limit) elem_off('addUplButt');
	}	
}

function uploadsRowDel() {
	var activeUploads = getDivAttribute("uploadsList", "active_uploads");
	elems_off(['upload_name' + activeUploads, 'upload_mandatory' + activeUploads, 'upload_locked' + activeUploads, 'upload_instructions' + activeUploads, 'upload_help' + activeUploads]);
	activeUploads--;
	setDivAttribute("uploadsList", "active_uploads", activeUploads);
	if (activeUploads <= 0) elem_off('delUplButt'); 
	if (activeUploads < uploads_limit) elem_on('addUplButt');
}

function uploadsChangeLocked(n) {
	if (document.getElementById("upload_locked" + n).value == 1) 
		elem_on(['upload_instructions' + n])
	else 
		elem_off(['upload_instructions' + n]);	
}

function statementsRowAdd() {
	var sl = document.getElementById("statementsList");
	var activeStatements = getDivAttribute("statementsList", "active_statements");
	var createdStatements = getDivAttribute("statementsList", "created_statements");

	activeStatements++;
	setDivAttribute("statementsList", "active_statements", activeStatements);
	
	if (activeStatements > createdStatements) {
		var statement0 = document.getElementById("statement0");
		var cloneStatement = statement0.cloneNode(true);
		cloneStatement.setAttribute("id", "statement"+activeStatements);
		cloneStatement.setAttribute("style", "padding-top: 5px;");
		var TDList = cloneStatement.firstChild.nextSibling.firstChild.nextSibling.firstChild.getElementsByTagName('td');
		var childStatement = TDList[1].childNodes[1];
		childStatement.setAttribute("id", "statement_text"+activeStatements);	
		childStatement.setAttribute("name", "statement_text"+activeStatements);	
		sl.appendChild(cloneStatement);
		setDivAttribute("statementsList", "created_statements", activeStatements);
	}
	else {
		elem_on('statement_text' + activeStatements);
	}
	if (activeStatements > 0) {
		elem_on('delStatButt');
		if (activeStatements >= statements_limit) elem_off('addStatButt');
	}	
}

function statementsRowDel() {
	var activeStatements = getDivAttribute("statementsList", "active_statements");
	elem_off('statement_text' + activeStatements);
	activeStatements--;
	setDivAttribute("statementsList", "active_statements", activeStatements);
	if (activeStatements <= 0) elem_off('delStatButt'); 
	if (activeStatements < statements_limit) elem_on('addStatButt');
}

function conditionsRowAdd() {
	var cl = document.getElementById("conditionsList");
	var activeConditions = getDivAttribute("conditionsList", "active_conditions");
	var createdConditions = getDivAttribute("conditionsList", "created_conditions");
	activeConditions++;
	setDivAttribute("conditionsList", "active_conditions", activeConditions);
	
	if (activeConditions > createdConditions) {
		var condition0 = document.getElementById("condition0");
		var cloneCondition = condition0.cloneNode(true);
		cloneCondition.setAttribute("id", "condition"+activeConditions);
		cloneCondition.setAttribute("style", "padding-top: 5px;");
		//var childTR = cloneCondition.firstChild.nextSibling.firstChild.nextSibling.firstChild;
		//var childConditionField = childTR.childNodes[3].childNodes[1];
		var TDList = cloneCondition.firstChild.nextSibling.firstChild.nextSibling.firstChild.getElementsByTagName('td');
		var childConditionField = TDList[1].childNodes[1];
		childConditionField.setAttribute("id", "condition_field"+activeConditions);	
		childConditionField.setAttribute("name", "condition_field"+activeConditions);	
		//var childConditionMandatory = childTR.childNodes[7].childNodes[1];
		var childConditionMandatory =  TDList[3].childNodes[1];
		childConditionMandatory.setAttribute("id", "condition_mandatory"+activeConditions);	
		childConditionMandatory.setAttribute("name", "condition_mandatory"+activeConditions);	
		//var childConditionValue = childTR.childNodes[11].childNodes[1];
		var childConditionValue =  TDList[5].childNodes[1];
		childConditionValue.setAttribute("id", "condition_value"+activeConditions);	
		childConditionValue.setAttribute("name", "condition_value"+activeConditions);	
		//var childConditionDisplay = childTR.childNodes[15].childNodes[1];
		var childConditionDisplay =  TDList[7].childNodes[1];
		childConditionDisplay.setAttribute("id", "condition_display"+activeConditions);	
		childConditionDisplay.setAttribute("name", "condition_display"+activeConditions);	
		//var childConditionHelp = childTR.childNodes[19].childNodes[1];
		var childConditionHelp =  TDList[9].childNodes[1];
		childConditionHelp.setAttribute("id", "condition_help"+activeConditions);	
		childConditionHelp.setAttribute("name", "condition_help"+activeConditions);	
		cl.appendChild(cloneCondition);
		setDivAttribute("conditionsList", "created_conditions", activeConditions);
	}
	else {
		elems_on(['condition_field' + activeConditions, 'condition_mandatory' + activeConditions, 'condition_value' + activeConditions, 'condition_display' + activeConditions, 'condition_help' + activeConditions]);
	}
	if (activeConditions > 0) {
		elem_on('delCondButt');
		if (activeConditions >= conditions_limit) elem_off('addCondButt');
	}	
}

function conditionsRowDel() {
	var activeConditions = getDivAttribute("conditionsList", "active_conditions");
	elems_off(['condition_field' + activeConditions, 'condition_mandatory' + activeConditions, 'condition_value' + activeConditions, 'condition_display' + activeConditions, 'condition_help' + activeConditions]);
	activeConditions--;
	setDivAttribute("conditionsList", "active_conditions", activeConditions);
	if (activeConditions <= 0) elem_off('delCondButt'); 
	if (activeConditions < conditions_limit) elem_on('addCondButt');
}


