var xmlhttp = false;
//Check if we are using IE.
try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) {
	try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (E) {xmlhttp = false; }
}

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { xmlhttp = new XMLHttpRequest(); }

function overlay(form, type, entry, container, appendix)
{
	var targetelement = container;
	var theObject = document.getElementById(targetelement);
	var cursid = document.getElementById('seitenid').value;
	theObject.style.visibility = "visible";
	var objID = targetelement;
	var serverPage = window.location.protocol+"//"+document.domain+form+"?"+type+"="+entry + appendix;
	var obj = document.getElementById(objID);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() 
	{
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
		{
			obj.innerHTML = xmlhttp.responseText
		}
	}
	xmlhttp.send(null);
	//evtl. noch offenes Popup Navi schließen
	var last_popup = document.getElementById('popup_open').value;
	if (last_popup != '')
	{
		try {	document.getElementById(last_popup).setAttribute("src","/images/buttons/FE_Button_"+last_popup+"_Normal.png",1); }
		catch (E) {}
	}
	document.getElementById('popup_open').value = entry;
	try {	document.getElementById(cursid).setAttribute("src","/images/buttons/FE_Button_"+cursid+"_Normal.png",1); }
		catch (E) {}
	document.getElementById('hauptcontent').style.visibility = 'visible';
	try { document.getElementById(entry).setAttribute("src","/images/buttons/FE_Button_"+entry+"_Hot.png",1); }
	catch (E) {}
}

function show_article(artikel,heftinfo)
{
	//Überprüfen. Haben wir einen IE???
	var browsername = navigator.userAgent.toLowerCase()
	var IE = (browsername.indexOf("msie")>-1)
	var erfolg = false;
	
	var artikelbox = document.getElementById('artikelbox');
	var serverPage = window.location.protocol+'//'+document.domain+"/article.php?a="+artikel+"&info="+heftinfo;
	//alert(serverPage);
	xmlhttp.open('GET',serverPage);
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
		{
			artikelbox.innerHTML = xmlhttp.responseText;
			
			erfolg = true;
			//	Artikelliste anpassen
			oldlink = getElementsByClass('list_active')[0];
			if (IE)
				oldlink.setAttribute('className','list');
			else
				oldlink.setAttribute('class','list');
			document.getElementById('rd_'+oldlink.id.slice(4)).innerHTML='&nbsp;';
			
			if (IE)
				document.getElementById('lnk_'+artikel).setAttribute('className','list_active');
			else
				document.getElementById('lnk_'+artikel).setAttribute('class','list_active');
			document.getElementById('rd_'+artikel).innerHTML = '<img src="/images/roter_ball.gif" alt="#">';
		}
	}
	xmlhttp.send(null);
	return erfolg;
}

function warenkorb(artid)
{
	var countbox = document.getElementById('warencount');
	var serverPage = window.location.protocol+'//'+document.domain+"/waren.php?aid="+artid;
	//alert(serverPage);
	xmlhttp.open('GET',serverPage);
	antwort = false;
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
		{
			countbox.innerHTML = xmlhttp.responseText;
			antwort = true;
		}
	}
	xmlhttp.send(null);
	
	return antwort;
}

function close_popup(id)
{
	var cursid = document.getElementById('seitenid').value;
	try {	document.getElementById(cursid).setAttribute("src","/images/buttons/FE_Button_"+cursid+"_Hot.png",1); }
	catch (E) {}
	
	try {	document.getElementById(id).setAttribute("src","/images/buttons/FE_Button_"+id+"_Normal.png",1); }
	catch (E) {}
	
	document.getElementById('popup').style.visibility = 'hidden';
	document.getElementById('hauptcontent').style.visibility = 'hidden';
}

function edit(id)
{
	var ganzahl = document.getElementById('geschenkanzahl').value;
	
	for(j=1;j<=ganzahl;j++)
	{
		if(j!=id)
		{
			if (close_addr(j))
			{
				j=999;
			}
		}
	}
	
	if(j<999)
	{
		document.getElementById('GESCHENK_'+id+'_1').style.visibility = 'hidden';
		document.getElementById('GESCHENK_'+id+'_1').style.display = 'none';
		document.getElementById('GESCHENK_'+id+'_1').style.height = '0px';
		document.getElementById('GESCHENK_'+id+'_2').style.visibility = 'visible';
		document.getElementById('GESCHENK_'+id+'_2').style.display = 'block';
		document.getElementById('GESCHENK_'+id+'_2').style.height = '229px';
	}
}

function close_addr(id)
{
	var error = false;
	//	nur weitermachen, wenn das Eingabefeld auch tatsächlich aufgeklappt ist
	if (document.getElementById('GESCHENK_'+id+'_2').style.display != 'none')
	{
		error = validate(id);
		
		if (!error)
		{
			document.getElementById('GESCHENK_'+id+'_2').style.visibility = 'hidden';
			document.getElementById('GESCHENK_'+id+'_2').style.display = 'none';
			document.getElementById('GESCHENK_'+id+'_2').style.height = '0px';
			document.getElementById('GESCHENK_'+id+'_1').style.visibility = 'visible';
			document.getElementById('GESCHENK_'+id+'_1').style.display = 'block';
			document.getElementById('GESCHENK_'+id+'_1').style.height = '19px';
		}
	}
	
	return error;
}

function validate(id)
{
	pflichtfelder = new Array('anrede','vorname','name','strasse','haus','plz','ort','land');
	var error = false;
	var alleleer = true;
	var errorname = '';
	var language = document.getElementById('language').value;
	//alert('validate '+id)
	
	//Überprüfen. Haben wir einen IE???
	var browsername = navigator.userAgent.toLowerCase()
	var ie = (browsername.indexOf("msie")>-1)
	
	for(i=0;i<=7;i++)
	{
		if(pflichtfelder[i]=='haus')
			errorname='strasse';
		else if(pflichtfelder[i]=='plz')
			errorname='ort';
		else
			errorname=pflichtfelder[i];
		
		if(trim(document.getElementsByName('g_'+id+'_'+pflichtfelder[i])[0].value)=='')
		{
			error = true;
			//alert('ERROR '+errorname);
			if(ie)	document.getElementById('error_'+id+'_'+errorname).setAttribute('className','text_red');
			else	document.getElementById('error_'+id+'_'+errorname).setAttribute('class','text_red');
		}
		else
		{
			alleleer = false;
			//alert('OK '+errorname);
			if(ie)	document.getElementById('error_'+id+'_'+errorname).setAttribute('className','body');
			else	document.getElementById('error_'+id+'_'+errorname).setAttribute('class','body');
		}
	}
	if(error)
	{
		document.getElementById('ERR_'+id).src='/images/fehler.gif';
		document.getElementById('error').style.visibility='visible';
		document.getElementById('tabelle_'+id).style.borderColor = '#D70316';
		document.getElementById('titelzeile1_'+id).style.backgroundColor = '#D70316';
		document.getElementById('titelzeile2_'+id).style.backgroundColor = '#D70316';
	}
	else
	{
		document.getElementById('ERR_'+id).src='/images/ok.gif';
		document.getElementById('error').style.visibility='hidden';
		document.getElementById('tabelle_'+id).style.borderColor = '#004998';
		document.getElementById('titelzeile1_'+id).style.backgroundColor = '#004998';
		document.getElementById('titelzeile2_'+id).style.backgroundColor = '#004998';
	}
	if(alleleer)
	{
		if(language=="fr")
			var bezeichnung = 'Compléter';
		else
			var bezeichnung = 'Daten eingeben';
	}
	else
	{
		if(language=="fr")
			var bezeichnung = 'Aperçu / Modifier';
		else
			var bezeichnung = 'Daten ansehen / ändern';
	}
	var Node = document.createTextNode(bezeichnung);
	AnzahlZeichen = document.getElementById('LINK_'+id).firstChild.nodeValue.length;
	document.getElementById('LINK_'+id).firstChild.deleteData(0,AnzahlZeichen);
	document.getElementById('LINK_'+id).firstChild.appendData(Node.nodeValue);
	
	var Text = document.getElementsByName('g_'+id+'_vorname')[0].value+' '+document.getElementsByName('g_'+id+'_name')[0].value;
	var TextNode = document.createTextNode(Text);
	element = document.getElementById('NAME_'+id);
	if(element!=null)
	{
		AnzahlZeichen = document.getElementById('NAME_'+id).firstChild.nodeValue.length;
		document.getElementById('NAME_'+id).firstChild.deleteData(0,AnzahlZeichen);
		document.getElementById('NAME_'+id).firstChild.appendData(TextNode.nodeValue);
	}
	
	return error;
}

function preselect(id)
{
	if(id=="karte")
	{
		document.getElementById(id).checked="checked";
		document.getElementById('bank').checked="";
	}
	else
	{
		document.getElementById(id).checked="checked";
		document.getElementById('karte').checked="";
	}
	select_art(id);
}

function select_art(id)
{
	if(id=='karte')
	{
		document.getElementsByName('koinhaber')[0].setAttribute('readonly','readonly',1);
		document.getElementsByName('konto')[0].setAttribute('readonly','readonly',1);
		document.getElementsByName('blz')[0].setAttribute('readonly','readonly',1);
		document.getElementsByName('bank')[0].setAttribute('readonly','readonly',1);
		
		document.getElementsByName('kartenart')[0].removeAttribute('readonly');
		document.getElementsByName('kainhaber')[0].removeAttribute('readonly');
		document.getElementsByName('karte')[0].removeAttribute('readonly');
		document.getElementsByName('code')[0].removeAttribute('readonly');
		document.getElementsByName('monat')[0].removeAttribute('readonly');
		document.getElementsByName('jahr')[0].removeAttribute('readonly');
	}
	else
	{
		document.getElementsByName('kartenart')[0].setAttribute('readonly','readonly',1);
		document.getElementsByName('kainhaber')[0].setAttribute('readonly','readonly',1);
		document.getElementsByName('karte')[0].setAttribute('readonly','readonly',1);
		document.getElementsByName('code')[0].setAttribute('readonly','readonly',1);
		document.getElementsByName('monat')[0].setAttribute('readonly','readonly',1);
		document.getElementsByName('jahr')[0].setAttribute('readonly','readonly',1);
		
		document.getElementsByName('koinhaber')[0].removeAttribute('readonly');
		document.getElementsByName('konto')[0].removeAttribute('readonly');
		document.getElementsByName('blz')[0].removeAttribute('readonly');
		document.getElementsByName('bank')[0].removeAttribute('readonly');
	}
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	{
		while(1)
		{
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	}
	else if(obj.y)
	{
		curtop += obj.y;
	}
	return curtop;
}

function scrollHeftarchiv(id)
{
	var YPos = findPosY(document.getElementById('lnk_'+id));
	if (YPos>600)
	{
		document.getElementById('artikelliste').scrollTop=(YPos-600);
	}
}

function echeck(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}
	
	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }
	
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }
	
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }
	
	 return true					
}

function getElementsByClass( searchClass, domNode, tagName) 
{
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++) {
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1)
			el[j++] = tags[i];
	}
	return el;
}
function trim (zeichenkette)
{
	zeichenkette = zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
	return zeichenkette;
}


