﻿function submitBibleOptions(elem)
{
    
	var selectElements = $("aspnetForm").getElementsByTagName("select");
	for(i=selectElements.length-1; i >= 0; i--)
	{
		if(elem.name != selectElements[i].name)
			selectElements[i].options[0].selected = true;
		else
			break;
	}
	$("aspnetForm").folioNo[$("aspnetForm").folioNo.selectedIndex].value = "";
	$("aspnetForm").quireNo[$("aspnetForm").quireNo.selectedIndex].value = "";
	normalizeURL();
}

function submitFolioOptions()
{
	var selectElements = $("bibleOptions").getElementsByTagName("select");
	for(i=selectElements.length-1; i >= 0; i--)
	{   
			selectElements[i].options[0].selected = true;
	}
	normalizeURL();
}

function normalizeURL()
{
	var formElems = getAllFormElements($("aspnetForm"));
	var params = new Array();
	
	var paramString = ""

	for(i=0;i<formElems.length;i++)
	{
		if(formElems[i].value!="" && !formElems[i].value.match(/anfrage/i))
		{
			paramString = formElems[i].name+"="+formElems[i].value;
			params.push(paramString);
		}
	}
	params.sort();
	window.location.href = window.location.pathname + "?" +params.join("&");	
}

function navigate(dir)
{
	var selectElements = $("bibleOptions").getElementsByTagName("select");
	for(i=selectElements.length-1; i >= 0; i--)
	{
			selectElements[i].options[0].selected = true;
	}
	$("aspnetForm").dir.value=dir;
	normalizeURL();				
}


function getAllFormElements( parent_node ) 
{
    if( parent_node == undefined ) 
    {
        parent_node = document;
    }
   
    var out = new Array();
    
    formInputs = parent_node.getElementsByTagName("input");
    for (var i = 0; i < formInputs.length; i++)
        out.push( formInputs.item(i) );
    
    formInputs = parent_node.getElementsByTagName("textarea");
    for (var i = 0; i < formInputs.length; i++)
        out.push( formInputs.item(i) );
    
    formInputs = parent_node.getElementsByTagName("select");
    for (var i = 0; i < formInputs.length; i++)
        out.push( formInputs.item(i) );
    
    formInputs = parent_node.getElementsByTagName("button");
    for (var i = 0; i < formInputs.length; i++)
        out.push( formInputs.item(i) );
    return out;
}


/* Returns the flattened tree (Array) of the given node.
 * An optional filterFunc(Node) determines whether to return an element or not.
 * Another optional recurseFunc(Node) determines whether to recurse into an element or not.
 */
function getFlatTree(/* DomNode */node, /* function(DomNode) */filterFunc/* = null */, /* function(DomNode) */recurseFunc/* = null */) {
	var result;
	if (!filterFunc || filterFunc(node)) result = [node];
	for (var i=0; i<node.childNodes.length; i++) {
		if (!recurseFunc || recurseFunc(node.childNodes[i])) {
			var arr = getFlatTree(node.childNodes[i], filterFunc, recurseFunc, true); // allow no return value
			if (arr && result) result = result.concat(arr);
			else if (arr) result = arr;
		}
	}
	if (!arguments[3] && !result) return []; // ensure at least empty array as final return value
	else return result;
}

/* Returns all elements with the given attribute, contained by el.
 */
function getDescendantElementsByAttribute(/* DomNode */el, /* string */name, /* string */value) {
	return getFlatTree(el, function(el) { return el.nodeType == 1 && el.getAttribute(name) == value; });
}


  String.prototype.leftTrim = function () {
    return (this.replace(/^\s+/,""));
  };
  String.prototype.rightTrim = function () {
    return (this.replace(/\s+$/,""));
  };
//kombiniert "leftTrim" und "rightTrim";
  String.prototype.basicTrim = function () {
    return (this.replace(/\s+$/,"").replace(/^\s+/,""));
  };
//dampft leerzeichen(-sequenzen) innerhalb einer zeichenkette auf ein einzelnes "space" ein;
  String.prototype.superTrim = function () {
    return(this.replace(/\s+/g," ").replace(/\s+$/,"").replace(/^\s+/,""));
  };

//zugabe: entfernt alle leerzeichen aus einer zeichenkette;
  String.prototype.removeWhiteSpaces = function () {
    return (this.replace(/\s+/g,""));
  };