var popwin;

// PopUps aufrufen
function PopUp(src,w,h)
{
	oversize = 0;
	if(popwin) popwin.close();

	if(w > screen.width-100)
	{
		oversize = 1;
		// h = Math.round((screen.width-100)/w*h); // Verhältnis
		h = parseInt(h)+20; // Scrollbars
		w = (screen.width-100);
	}
	if(h > screen.height-150)
	{
		oversize = 1;
	    	// w = Math.round((screen.height-150)/h*w); // Verhältnis
	    	w = parseInt(w)+20; // Scrollbars
		h = (screen.height-150);
	}

	breite = Math.round((screen.width - w) / 2);
	hoehe = Math.round((screen.height - h) / 2);

	if(oversize == 1)
		popwin = window.open(src,"popwin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width="+w+",height="+h+",top="+hoehe+",left="+breite);
	else
		popwin = window.open(src,"popwin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width="+w+",height="+h+",top="+hoehe+",left="+breite);
}

// Druckansicht
function PrintView(src,w,h)
{
	var breite = (screen.width - w) / 2;
	var hoehe = (screen.height - h) / 2;
	window.open(src,"printview","toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width="+w+",height="+h+",top="+hoehe+",left="+breite);
}

// Formulare leeren
function ClearForm(Formular)
{
	document.forms[Formular].reset(); // Zuerst reset!
	var x = 0;
	while(document.forms[Formular].elements[x])
	{
		if(document.forms[Formular].elements[x].type == "checkbox" || document.forms[Formular].elements[x].type == "radio")
		{
			// Checkboxen + Radiobuttons werden nicht angehakt
			document.forms[Formular].elements[x].checked = false;
		}
		else if(document.forms[Formular].elements[x].type != "hidden" && document.forms[Formular].elements[x].type != "submit" && document.forms[Formular].elements[x].type != "reset" && document.forms[Formular].elements[x].type != "button")
		{
			// Alle, ausser versteckte Felder, dürfen geleert werden
			document.forms[Formular].elements[x].value = "";
		}
		x++;
	}
}

// Zum Anfang der Seite scrollen
function ScrollToTop()
{
	var y = 0;

	if(window.pageYOffset) y = window.pageYOffset;
	else
	if(document.body && document.body.scrollTop) y=document.body.scrollTop;

	while(y > 0)
	{
		setTimeout("window.scrollBy(0,-20)",10);
		y = y-5;
	}
}

// Seite zu den Favoriten
function MakeFavorite(title,url)
{
	if((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4))
	{
		window.external.AddFavorite(url,title)
	}
	else if(navigator.appName == "Netscape")
	{
		alert("Unsere Homepage können Sie manuell oder\nmit den Tasten STRG+D zu den Favoriten hinzufügen.");
	}
}

// Bilder vorladen, mit PreloadImages('bild1.jpg','bild2.gif'); aufrufen
function PreloadImages()
{
	document.Vorladen = new Array();
	if(document.images)
	{
		for(var i=0; i<PreloadImages.arguments.length; i++)
		{
			document.Vorladen[i] = new Image();
			document.Vorladen[i].src = PreloadImages.arguments[i];
		}
	}
}

// Liefert ein Object anhand ID oder Name
function gObj(id)
{
	if(typeof document.getElementById(id) == 'object' && document.getElementById(id) != null)
	{
		return document.getElementById(id);
	}
	else if(document.getElementsByName)
	{
		var namedObjects = document.getElementsByName(id);
		if(typeof namedObjects != 'undefined' && namedObjects.length)
		{
		    return namedObjects[0];
		}
	}
}

// Reiternavigation
function showTab(num)
{
	for(i = 1; i <= 10; i++)
	{
		if(gObj('tab'+i) != undefined) gObj('tab'+i).className = 'hidden';
		if(gObj('tabDiv'+i) != undefined) gObj('tabDiv'+i).style.display = 'none';
	}
	if(gObj('tab'+num) != undefined) gObj('tab'+num).className = '';
	if(gObj('tabDiv'+num) != undefined) gObj('tabDiv'+num).style.display = 'block';
	if(gObj('tab')) gObj('tab').value = num;
}

// Sprachreiter
function langTab(num,name)
{
	for(i = 0; i <= 5; i++)
	{
		if(gObj(name+i) != undefined) gObj(name+i).className = '';
		if(gObj(name+'Div'+i) != undefined) gObj(name+'Div'+i).style.display = 'none';
	}
	if(gObj(name+num) != undefined)
	{
		gObj(name+num).className = 'on';
		gObj(name+num).blur();
	}
	if(gObj(name+'Div'+num) != undefined) gObj(name+'Div'+num).style.display = 'block';
}

// Formatiert Formulareingaben
function formatFieldValue(field,fieldtype)
{
	if(field.value != "")
	{
		if(fieldtype == "alpha")
	    {
			field.value = field.value.replace(/([^a-zA-Z])/g,"");
		}
		if(fieldtype == "alphanum")
	    {
			field.value = field.value.replace(/([^a-zA-Z0-9])/g,"");
		}
		if(fieldtype == "alphanum_x")
	    {
			field.value = field.value.replace(/([^a-zA-Z0-9-_])/g,"");
		}
		if(fieldtype == "int" || fieldtype == "num" || fieldtype == "year")
	    {
			field.value = field.value.replace(/([^0-9])/g,"");
			if(field.value > 0)
			{
				if(fieldtype == "int") field.value = parseInt(field.value);
				if(fieldtype == "year") field.value = (field.value > 2038 ? 2038 : parseInt(field.value.substr(0,4))); // 2038 wegen UNIX TIMESTAMP
			}
		}
	    if(fieldtype == "float")
	    {
	        if(field.value <= 0) field.value = "";
	        field.value = field.value.replace(/,/,".");
	        field.value = field.value.replace(/([^0-9\.])/g,"");
	        if(!isNaN(field.value) && field.value != '' && field.value != null)
			{ 
				field.value = parseFloat(field.value);
			}
			field.value = field.value.replace(/\./,",");
		}
	    if(fieldtype == "float2")
	    {
	        if(field.value <= 0) field.value = "";
	        field.value = field.value.replace(/,/,".");
	        field.value = field.value.replace(/([^0-9\.])/g,"");
			if(field.value > 0) field.value = parseFloat(field.value).toFixed(2);
			field.value = field.value.replace(/\./,",");
		}
	}

    if(field.onblur == null)
    {
        field.onblur = new Function("formatFieldValue(this,'"+fieldtype+"')");
		if(fieldtype == "int" || fieldtype == "num" || fieldtype == "year" || fieldtype == "float" || fieldtype == "float2")
		{
			field.style.textAlign = "center";
			field.onfocus = new Function("this.select()");
		}
    }
}

// Formatiert Formulareingaben aller Formularfelder
function initProductForm()
{
	if(document.forms['formproduct'])
	{
	    proForm = document.forms['formproduct'];

		for(i = 0; i < proForm.elements.length; i++)
		{
		    if(proForm.elements[i].type == 'text' && proForm.elements[i].onfocus != null && proForm.elements[i].onfocus != undefined)
		    {
		    	if(document.all)
		    	{
		    		var search = /'(\w+)'/g;
		    		formatFieldValue(proForm.elements[i], search.exec(proForm.elements[i].onfocus)[1]);
		    	}
		    	else
		    	{
					ofE = proForm.elements[i].getAttribute('onfocus');
		    		eval(ofE.replace('this', 'document.forms[\'formproduct\'].elements[' + i + ']'));
		    	}
			}

			if(proForm.elements[i].value <= 0.00) proForm.elements[i].value = "";
		}
	}
}

//Definition beider Arrays außerhalb der Funktion!
var uid_childs;
var uid_parent;

function handleSearchCheck(checkboxElement)
{
	if(typeof checkboxElement != 'undefined' && checkboxElement != null)
	{
		//Checkbox ist wohl eine Checkbox und kann verwendet werden.
		var uid = parseInt(checkboxElement.value);
		var childs = uid_childs[uid];
		var parent = uid_parent[uid];
		if(typeof childs != 'undefined')
		{
			for(var ixc = 0; ixc < childs.length; ixc++)
			{
				var cid = childs[ixc];
				gObj('cat' + cid).checked = checkboxElement.checked;
				if(typeof uid_childs[cid] != 'undefined') handleSearchCheck(gObj('cat'+cid));
			}
		}
		
		if(!checkboxElement.checked) uncheckTree(uid); //Aufwärts gehen um alle oberen Haken zu entfernen!
		else checkParents(uid);
	}
}

//Entfernt alle Haken oberhalb der angegeben UID im Baum
function uncheckTree(uid)
{
	gObj('cat'+uid).checked = false;
	if(typeof uid_parent[uid] != 'undefined')
	{
		uncheckTree(uid_parent[uid]);	
	}
}

//Überprüft die Eltern-Elemente und schaut, ob alle "Kindelemente" angehakt sind, wenn ja -> anhaken!
function checkParents(uid)
{
	//alert(uid);
	if(typeof uid_parent[uid] != 'undefined' && uid != 0)
	{
		var pid = uid_parent[uid];
		if(typeof uid_childs[pid] != 'undefined')
		{
			for(var ixc = 0; ixc < uid_childs[pid].length; ixc++)
			{
				var cid = uid_childs[pid][ixc];
				if(!gObj('cat'+cid).checked)
				{
					if(arguments.length > 1)
					{
						gObj('cat'+pid).checked = false;
						checkParents(pid, true);
					}
					return false;
				}
			}
			gObj('cat'+pid).checked = true;
			checkParents(pid);
		} //else alert('No childs found for ID #' + pid); #### DEBUG !
	} //else alert('No parent found for ID #' + uid); ##### DEBUG !
}

