function addElements(parent, elements, before) {
	for (var i = 0; i < elements.length; i++) {
		var elm = null;
		if (elements[i] instanceof Array) {
			elm = document.createElement(elements[i][0]);
			for (var j = 1; j < elements[i].length; j++) {
				if (elements[i][j] instanceof Array) {
					addElements(elm, elements[i][j], null);
				} else if (elements[i][j].substr(0, 5) == "text:") {
					elm.appendChild(document.createTextNode(elements[i][j].substring(5)));
				} else {
					var index = elements[i][j].indexOf(":");
					if (index != -1) {
						elm.setAttribute(elements[i][j].substring(0, index), elements[i][j].substring(index + 1));
					}
				}
			}
		} else {
			elm = document.createElement(elements[i]);
		}
		if (before != null) {
			parent.insertBefore(elm, before);
		} else {
			parent.appendChild(elm);
		}
	}	
}

var highestPersonId = 0;

function addPerson(optVals, optNames, highestId) {
	if (highestId && highestId > highestPersonId) {
		highestPersonId = highestId;	
	}
	highestPersonId++;
	var id = highestPersonId;
	var options = new Array(optVals.length);
	for (var i = 0; i < options.length; i++) {
		options[i] = ["option", "value:" + optVals[i], "text:" + optNames[i]];	
	}
	var elements = [["div", "class:formGroup", "id:person" + id, 
						[["div", "class:section", 
							[["div", "class:formItem", 
								[["label", "for:personType" + id, "text:Typ:"],
								"br", 
								["select", "name:personType[]", "id:personType" + id, options]]
							],["div", "class:formItem", 
								[["label", "for:personFirstName" + id, "text:Förnamn:"],
								"br", 
								["input", "type:text", "name:personFirstName[]", "id:personFirstName" + id]]
							],["div", "class:formItem", 
								[["label", "for:personLastName" + id, "text:Efternamn:"],
								"br", 
								["input", "type:text", "name:personLastName[]", "id:personLastName" + id]]
							],["div", "class:formItem", 
								[["label", "for:personUrl" + id, "text:Url:"],
								"br", 
								["input", "type:text", "name:personUrl[]", "id:personUrl" + id]]
							]]
						],["div", "class:section center",
							[["p", "class:deletePerson", 
								[["a", "href:javascript:onclick:deletePerson(" + id + ");", 
									[["img", "src:/design/images/minus.png", "alt:Ta bort medarbetare"]]]]
							],["p", "text:Ta bort medarbetare"]]
						]]
					]];
	addElements(document.getElementById('persons'), elements, document.getElementById('addPerson'));	
}

function deletePerson(id) {
	document.getElementById('persons').removeChild(document.getElementById('person' + id));
}

var highestLinkId = 0;

function addLink(optVals, optNames, highestId) {
	if (highestId && highestId > highestLinkId) {
		highestLinkId = highestId;	
	}
	highestLinkId++;
	var id = highestLinkId;
	var options = new Array(optVals.length);
	for (var i = 0; i < options.length; i++) {
		options[i] = ["option", "value:" + optVals[i], "text:" + optNames[i]];	
	}
	var elements = [["div", "class:formGroup", "id:link" + id, 
						[["div", "class:section",
							[["div", "class:formItem", 
								[["label", "for:linkType" + id, "text:Typ:"],
								"br", 
								["select", "name:linkType[]", "id:linkType" + id, options]]
							],["div", "class:formItem", 
								[["label", "for:linkUrl" + id, "text:Url:"],
								"br", 
								["input", "type:text", "name:linkUrl[]", "id:linkUrl" + id]]
							]]
						],["div", "class:section",
							[["div", "class:deleteLink", 
								[["a", "href:javascript:onclick:deleteLink(" + id + ");", 
									[["img", "src:/design/images/minus.png", "alt:Ta bort länk"]]],
								["p", "text:Ta bort länk"]]
							]]
						]]
					]];
	addElements(document.getElementById('links'), elements, document.getElementById('addLink'));
}

function deleteLink(id) {
	var links = document.getElementById('links');
	links.removeChild(document.getElementById('link' + id));
}

var highestLangId = 0;

function addLanguage(optVals, optNames, highestId) {
	if (highestId && highestId > highestLangId) {
		highestLangId = highestId;	
	}
	highestLangId++;
	var id = highestLangId;
	var options = new Array(optVals.length);
	for (var i = 0; i < options.length; i++) {
		options[i] = ["option", "value:" + optVals[i], "text:" + optNames[i]];	
	}
	var elements = [["div", "class:formGroup", "id:language" + id, 
						[["div", "class:formItem", 
							[["select", "name:languageType[]", "id:languageType" + id, options]
							,["a", "href:javascript:deleteLanguage(" + id + ");",
								[["img", "src:/design/images/minus.png", "alt:Ta bort språk"],["span", "class:afterLarge", "text: Ta bort språk"]]]]
						]]
					]];
	addElements(document.getElementById('languages'), elements, document.getElementById('addLanguage'));
}

function deleteLanguage(id) {
	var langs = document.getElementById('languages');
	langs.removeChild(document.getElementById('language' + id));
}

var highestDimId = -1;

function addDimension(optVals, optNames, highestId) {
	if (highestId && highestId > highestDimId) {
		highestDimId = highestId;	
	}
	highestDimId++;
	var id = highestDimId;
	var options = new Array(optVals.length);
	for (var i = 0; i < options.length; i++) {
		options[i] = ["option", "value:" + optVals[i], "text:" + optNames[i]];	
	}
	var del = null;
	if (id == 0) {
		del = ["input", "type:hidden", "name:delimiters[]", "value:"];
	} else {
		del = ["select", "name:delimiters[]", "id:delimiter" + id, 
					[["option", "value:&&", "text:och"], 
					["option", "value:||", "text:eller"]]];
	}
	var elements = [["div", "class:formGroup", "id:dimension" + id, 
						[["div", "class:formItem", 
							[del, ["select", "name:dimensions[]", "id:dimensions" + id, options], 
							["input", "type:text", "name:restrictions[]", "id:restriction" + id]]
						],["div", "class:formItem", 
							[["img", "src:/design/images/minus.png", "alt:Ta bort", "onclick:deleteDimension(" + id + ");"]]
						]]
					]];
	addElements(document.getElementById('dimensions'), elements, document.getElementById('addDimension'));
}

function deleteDimension(id) {
	var langs = document.getElementById('dimensions');
	langs.removeChild(document.getElementById('dimension' + id));
}

var highestCatId = 0;

function addCategory(optVals, optNames, highestId) {
	if (highestId && highestId > highestCatId) {
		highestCatId = highestId;	
	}
	highestCatId++;
	var id = highestCatId;
	var options = new Array(optVals.length);
	for (var i = 0; i < options.length; i++) {
		options[i] = ["option", "value:" + optVals[i], "text:" + optNames[i]];	
	}
	var elements = [["div", "class:formGroup", "id:categoryGroup" + id, 
						[["div", "class:formItem", 
							[["label", "for:category" + id, "text:Kategori:"],
							["br"], 
							["select", "name:category[]", "id:category" + id, options],
							["input", "type:hidden", "name:newCategory" + id, "id:newCategory" + id, "value:0"],
							["a", "id:newCatLink" + id, "href:javascript:newCategory(" + id + ")","text:Ny kategori"],
							["input", "type:text", "name:categoryName" + id, "id:categoryName" + id, "class:hidden"]]
						],["div", "class:formItem",
							[["p", "class:deleteCategory", 
								[["a", "href:javascript:onclick:deleteCategory(" + id + ");", 
									[["img", "src:/design/images/minus.png", "alt:Ta bort kategori"]]]]
							]]
						]]
					]];
	addElements(document.getElementById('categories'), elements, document.getElementById('addCategory'));	
}

function deleteCategory(id) {
	document.getElementById('categories').removeChild(document.getElementById('categoryGroup' + id));
}

function newCategory(id) {
	hideId("category" + id);
	hideId("newCatLink" + id);
	showId("categoryName" + id);
	document.getElementById("newCategory" + id).setAttribute('value',1);
}

var highestAuthorId = 0;

function addAuthor(highestId) {
	if (highestId && highestId > highestAuthorId) {
		highestAuthorId = highestId;	
	}
	highestAuthorId++;
	var id = highestAuthorId;
	var elements = [["div", "class:formItem", "id:" + id + "Author", 
						[["div", "class:section", 
							[["label", "for:firstname" + id, "text:Förnamn:"],
							"br", 
							["input", "type:text", "name:firstname[]", "id:firstname" + id]]
						],["div", "class:section", 
							[["label", "for:lastname" + id, "text:Efternamn:"],
							"br", 
							["input", "type:text", "name:lastname[]", "id:lastname" + id]]
						],["div", "class:section center", 
							[["a", "href:javascript:deleteAuthor(" + id + ")", 
								[["img", "src:/design/images/minus.png", "alt:-"],
								"br",
								["span", "text:Ta bort skribent"]]
							]]
						]]
					]];
	addElements(document.getElementById('authors'), elements, document.getElementById('addAuthor'));	
}

function deleteAuthor(id) {
	document.getElementById('authors').removeChild(document.getElementById(id + "Author"));
}

function hideId(id) {
	document.getElementById(id).style.display = "none";
}

function showId(id, displ) {
	displ = (typeof displ == 'undefined') ? 'block' : displ;
	document.getElementById(id).style.display = displ;
}

function invertVisible(id) {
	var elm = document.getElementById(id);
	if (elm.style.display == "none") {
		elm.style.display = "block";	
	} else {
		elm.style.display = "none";	
	}
}

function showIds(ids, disp) {
	for (var i = 0; i < ids.length; i++) {
		showId(ids[i], disp);	
	}	
}

function hideIds(ids) {
	for (var i = 0; i < ids.length; i++) {
		hideId(ids[i]);	
	}	
}

function invertIds(ids) {
	for (var i = 0; i < ids.length; i++) {
		invertVisible(ids[i]);	
	}	
}

function truncate(id, len) {
	var elm = document.getElementById(id);
	if (elm != null && elm.firstChild.data.length > len) {
		var content = elm.firstChild.data;
		while (elm.hasChildNodes()) {
			elm.removeChild(elm.lastChild);	
		} 	
		elm.appendChild(document.createTextNode(content.substring(0,len)));
		var a = document.createElement("a");
		a.href = "javascript:untruncate('" + elm.id + "', '" + content + "', " + len + ")";
		a.appendChild(document.createTextNode("..."));
		elm.appendChild(a);
	}
}

function untruncate(id, fulltext, len) {
	var elm = document.getElementById(id);
	if (elm != null) {
		while (elm.hasChildNodes()) {
			elm.removeChild(elm.lastChild);	
		}
		elm.appendChild(document.createTextNode(fulltext));	
		var a = document.createElement("a");
		a.href = "javascript:truncate('" + elm.id + "', " + len + ")";
		a.appendChild(document.createTextNode("<<"));
		elm.appendChild(a);
	}	
}

function checkAll(checkboxes, check) {
	for (var i = 0; i < checkboxes.length; i++) {
		checkboxes[i].checked = check;	
	}
}

function popup(url, height, width) {
	var newwindow=window.open(url,'name','height=' + height + ',width=' + width);
	if (window.focus) {newwindow.focus()}
}

