//
// dynamicforms.js
//
// This script contains functions that are called upon events raised
// from within a web form that has been created by the XSD driven
// stylesheet edit.xsl. We make some assumption on the structure of
// such web forms. So be careful to keep this script and edit.xsl in
// sync.
//
// strauss, 2005-09-30.
//



function gettr(elem) {
  return elem.parentNode.parentNode;
}



function getvarname(tr) {
    // assume 4th <td> contains the input element
    var name = null;

    try {
	name = getname(tr);
    } catch(e) {
	;
    }

    if (name != null) {
	name = name.replace(/_\d+$/,"");
	name = name.replace(/^[^_]+_/,"");
    } else {
	name = "";
    }

    return name;
}



function getbasename(tr) {
    // assume 4th <td> contains the input element
    var name;

    name = getname(tr);

    if (name) {
	return name.replace(/_\d+$/,"");
    } else {
	return "";
    }
}



function getname(tr) {
    // assume 4th <td> contains the input element
    var name;

    try {
	if (tr.childNodes[3]) {
	    name = tr.childNodes[3].childNodes[0].getAttribute("name");
	}
    } catch (e) {
	;
    }
    if (! name) {
	try {
	    name = tr.childNodes[0].childNodes[0].getAttribute("name");
	} catch (e2) {
	    ;
	}
    }
    if (! name) {
	try {
	    trs = tr.childNodes[3].childNodes[0].childNodes[0].childNodes;
	    tr2 = trs[trs.length - 1];
	    name = tr2.childNodes[0].childNodes[0].getAttribute("name");
	} catch (e3) {
	    ;
	}
    }

    return name;
}



function getfullname(basename, pos) {
    if (pos == 1) {
	return basename;
    } else {
	pos2 = "000" + pos;
	pos2 = pos2.substr(pos2.length - 3, 3);
	return basename + "_" + pos2;
    }
}



function getcount(tr) {
    var varname = getvarname(tr);
    var n = 1;
    var tr2;

    for (tr2 = tr;
	 (tr2.nextSibling) && (getvarname(tr2.nextSibling) == varname);
	 tr2 = tr2.nextSibling) {
	n++;
    }
    for (tr2 = tr;
	 (tr2.previousSibling) && (getvarname(tr2.previousSibling) == varname);
	 tr2 = tr2.previousSibling) {
	n++;
    }

    return n;
}



function setname(tr, name) {
    var oldname;

    try {
	if (tr.childNodes[3]) {
	    oldname = tr.childNodes[3].childNodes[0].getAttribute("name");
	    tr.childNodes[3].childNodes[0].setAttribute("name", name);
	}
    } catch (e) {
	;
    }
    if (! oldname) {
	try {
	    oldname = tr.childNodes[0].childNodes[0].getAttribute("name");
	    tr.childNodes[0].childNodes[0].setAttribute("name", name);
	} catch (e2) {
	    ;
	}
    }
    if (! oldname) {
	try {
	    trs = tr.childNodes[3].childNodes[0].childNodes[0].childNodes;
	    tr2 = trs[trs.length - 1];
	    oldname = tr2.childNodes[0].childNodes[0].getAttribute("name");
	    tr2.childNodes[0].childNodes[0].setAttribute("name", name);
	} catch (e3) {
	    ;
	}
    }
}



function getpos(tr) {
    var s = tr.childNodes[3].childNodes[0].getAttribute("name");
    var regexp = /^.*_(\d*)/; regexp.exec(s); s = RegExp.$1;
    var pos = parseInt(s);
    if (pos >= 2) return pos;
    return 1;
}



function add(elem, max) {
    var tr;
    
    oldtr = gettr(elem);
    
    if (getcount(oldtr) == max) {
	alert("No more than " + max + " instance(s) of this field!");
	return;
    } 
    
    basename = getbasename(oldtr);
    varname = getvarname(oldtr);
    newtr = oldtr.cloneNode(true);
    newtr.childNodes[3].childNodes[0].value = "";
    oldtr.parentNode.insertBefore(newtr, oldtr.nextSibling);

    for (tr = newtr; getvarname(tr) == varname; tr = tr.nextSibling) {
	if (! add_incnames(tr, basename)) {
	    setname(tr, getfullname(basename, 0 + getpos(tr) + 1));
	}
	try {
	    tr.childNodes[1].childNodes[0].setAttribute("style", "visibility:visible");
	} catch (e) {
	    ;
	}
    }
    for (tr = newtr; getvarname(tr) == varname; tr = tr.previousSibling) {
	try {
	    tr.childNodes[1].childNodes[0].setAttribute("style", "visibility:visible");
	} catch (e) {
	    ;
	}
    }
}



function add_incnames(parent, base) {
    base = base.replace(/^[^_]+_/,"");
    trs = parent.getElementsByTagName("tr");
    var done = 0;
    for (i = 0; i < trs.length; i++) {
	tr = trs[i];
	oldname = getname(tr);
	if (oldname) {
	    newname = oldname.substr(0, oldname.indexOf(base) + base.length);
	    num = oldname.substr(newname.length + 1);
	    rest = num.replace(/^[^_]+/,"");
	    num = num.replace(/_.+$/,""); 
	    if (num <= 1) { num = 1; }
	    num++;
	    pos2 = "000" + num;
	    pos2 = pos2.substr(pos2.length - 3, 3);
	    newname = newname + "_" + pos2 + rest;
	    // alert(base + ": " + oldname + " -> " + newname);
	    setname(tr, newname);
	    done = 1;
	}
    }
    return done;
}


function del(elem, min) {
    oldtr = gettr(elem);

    basename = getbasename(oldtr);

    oldcount = getcount(oldtr);

    if (oldcount == min) {
	if (min == 1) {
	    alert("This field is mandatory!");
	} else {
	    alert("No less than " + min + " instance(s) of this field!");
	}
	// return;
    }

    addnode = oldtr.childNodes[0].childNodes[0];
    delnode = oldtr.childNodes[1].childNodes[0];
    inputnode = oldtr.childNodes[3].childNodes[0];

    if (oldcount == 1) {
	// don't remove the row, if it is the last one, so that
	// a new entry can be entered. however, we have to hide
	// the "del" button and clear the value.
	delnode.setAttribute("style", "visibility:hidden");
	// if it's an input element...
	if (inputnode.getAttributeNode("value")) {
	    inputnode.setAttribute("value","");
	    inputnode.value = "";
	}
	// if it's a textarea...
	if (inputnode.nodeName == "TEXTAREA") {
	    n = inputnode.childNodes.length;
	    inputnode.value = "";
	    for (i = n-1; i >= 0; i--) {
		if ((inputnode.childNodes[i].nodeType == 1) ||
		    (inputnode.childNodes[i].nodeType >= 3)) {
		    inputnode.removeChild(inputnode.childNodes[i]);
		}
	    }
	}
	// if it's a select box...
	if (inputnode.nodeName == "SELECT") {
	    n = inputnode.childNodes.length;
	    for (i = n-1; i >= 0; i--) {
		if (inputnode.childNodes[i].getAttributeNode("selected")) {
		    inputnode.childNodes[i].
			removeAttributeNode(inputnode.childNodes[i].
					    getAttributeNode("selected"));
		}
	    }
	    s = document.createAttribute("selected");
	    s.nodeValue = "selected";
	    inputnode.childNodes[n-1].setAttributeNode(s);
	    inputnode.selectedIndex = n-1;
	}

    } else {

	/*
	if (oldtr.nextSibling) {
	    for (tr = oldtr.nextSibling; (tr.nextSibling) && (getbasename(tr) == basename); tr = tr.nextSibling) {
		setname(tr, getfullname(basename, 0 + getpos(tr) - 1));
	    }
	}
	*/

	//oldtr.parentNode.removeChild(oldtr);
	remove_subtree(oldtr.parentNode, oldtr);
	inputnode.name = "";
	
	/*
	if ((oldcount - 1) <= min) {
	    for (tr in parent.childNodes) {
		if (getbasename(tr) == basename) {
		    tr.childNodes[1].childNodes[0].setAttribute("style", "visibility:hidden");
		}
	    }
	}
	*/

    }
}



function change(elem, min) {
    tr = gettr(elem);
    value = elem.value;
    
    if (value == "") {
	tr.childNodes[1].childNodes[0].setAttribute("style", "visibility:hidden");
    } else {
	tr.childNodes[1].childNodes[0].setAttribute("style", "visibility:visible");
    }

    custom_change(elem, min);
}



function remove(file, backurl, l) {
    if (l == "de") {
	q = "Soll dieser Datensatz wirklich gelöscht werden?";
    } else {
	q = "Are you sure to remove this record?";
    }
    check = confirm(q);
    if (check) {
	if (backurl.match(/\?/)) {
	    backurl = backurl + ";";
	} else {
	    backurl = backurl + "?";
	}
	backurl = location.protocol + "//" + location.hostname + backurl + "action=remove;argument=" + file;
	location.href = backurl;
    }
}



function create(template, schema, backurl, l) {
    if (l == "de") {
	q = "ID des neuen Datensatzes? (Bitte einen kurzen prägnanten Namen - im Zweifelsfall mit englischem Wortstamm - in Kleinbuchstaben und ohne Leer- und Sonderzeichen wählen, z.B. \"testlab-ns2\".)";
    } else {
	q = "ID of the new record? (Please choose a concise lowercase name without blanks and special characters, e.g., \"testlab-ns2\".)";
    }
    oldname = "?";
    name = template;
    name = name.replace(/^.*\/([^\/]*)$/,"$1");
    name = name.replace(/.xml$/,"");
    name = name.replace(/[^a-zA-Z0-9\-\.]/g,"");

    // increment number, if it's a library item
    if (name.match(/^D\.[0-9][0-9]\.3[0-9][0-9][0-9]$/)) {
	n = name.substr(5);
	// do we need this "+1" ?! (steinb, 2011-08-29)
	n = parseInt(n) + 1;
	name = name.substr(0,5) + n;
    }

    // loop until an acceptable ID was entered
    while (name == "" || name != oldname) {
	oldname = name;
	name = prompt(q, name);
	if ((name == "null") || (name == "")) return;
	oldname = name;
	name = name.replace(/.xml$/,"");
	name = name.replace(/[^a-zA-Z0-9\-\.]/g,"");
    }

    newuri = template;
    newuri = newuri.replace(/\/[^\/]*$/,"");
    newuri = newuri + "/" + name + ".xml";
    newuri2 = newuri.replace(/^[^\/]*\/\/[^\/]*/,"");
    schemauri = schema;
    schemauri = schemauri.replace(/^[^\/]*\/\/[^\/]*/,"");
    targeturi = newuri + "?xslt=/xsl/edit.xsl&action=create&argument=" + newuri2 + "&argument2=" + schemauri + "&lang=" + l + "&backurl=" + backurl;
    location.href = targeturi;
}



function go(url) {
    url = url.replace(/&amp;/g,"&");
    url = url.replace(/&#38;/g,"&");
    location.href = url;
}


function submit() {
    // f = document.getElementsByName("Editform")[0];
    f = document.getElementById("Editform");
    f.submit();
}


function remove_subtree(parent, node) {
    for (n = parent.firstChilde; n; n = n.nextSibling) {
	remove_subtree(node, n);
    }
    parent.removeChild(node);
}

