/*
  www.IsraelWein.de
*/

	SaveChanges = {
	
		handler : {
		
			onfocus : function( elem ) {
			
				// value is already changed: nothing to do
				if ( SaveChanges.$$ischanged )
					return;
				
				// counter = 1;
				var compare = function() {
					// document.getElementById('stderr').innerHTML = counter++;
					if ( elem.value != elem.$$initvalue ) {
						SaveChanges.$$ischanged = true;
						SaveChanges.registerChange();
						window.clearInterval( elem.$$timeout );
					}
				};
					
				elem.$$initvalue = elem.value;
				elem.$$timeout = window.setInterval( compare, 50 );
				
			},
		
			onblur : function( elem ) {
				if ( elem.$$timeout )
					window.clearInterval( elem.$$timeout );
				elem.$$timeout = null;
			}
			
		},
	
		registerChange : function() {
			window.onbeforeunload = SaveChanges.unloadHandler;
			// alert( 'ON - SaveChanges.registerChange' );
		},
		
		unregisterChange : function() {
			window.onbeforeunload = null;
			SaveChanges.$$ischanged = null;
			// alert( 'OFF - SaveChanges.unregisterChange' );
		},

		isregisteredChange : function() {
			return window.onbeforeunload == SaveChanges.unloadHandler;
		},
		
		unloadHandler : function() {
			return "Die noch nicht gespeicherten Änderungen gehen dadurch unwiderruflich verloren.";
		},
		
		confirmClose : function() {
			var leave = confirm(
				"Soll diese Seite wirklich verlassen werden?\n\n" +
				"Die noch nicht gespeicherten Änderungen gehen dadurch unwiderruflich verloren.\n\n" +
				"Wählen Sie OK, um fortzufahren, oder Abbrechen, um auf der aktuellen Seite zu bleiben."
			);
			if ( leave )
				SaveChanges.unregisterChange();
			return leave;
		}
		
		
	};

	

	window.onload = function() {
	
		// Abbrechen, wenn nicht "israelwein.de" und nicht ".israelwein.de" im Hostname vorkommt
		// -- für externe Domains wichtig!
		with ( window.location ) {
			if ( hostname != 'israelwein.de' && hostname.indexOf('.israelwein.de') == -1 )
				return;
		}
	
		var div = document.getElementById('contentdiv');
		var links = div.getElementsByTagName( 'a' );
		// var links = document.getElementsByTagName( 'a' );
		for ( var i = 0; i < links.length; i++ ) {
			var href = links[i].href;
				// if ( links[i].onclick == null &&
				// if ( href.match(/^http:/) && ! href.match(/http:\/\/www.pilkowski.de/) ) {
				
			if ( href.match(/^http:/) && href.indexOf(window.location.hostname) == -1 ) {
			
				links[i].onmousedown = function() {
					this.$$down = true; // "this" is the anchor element
				};

				// logging
				links[i].onmouseup = function() {
					if ( this.$$down ) {
						// alert( 'Logging request for "' + this.href + '" into database.' );
						var img = new Image();
						img.src = "_user_linkcounter.php?url=" + encodeURIComponent(this.href);
					}
				};
				
				// external link icon
				var img = links[i].getElementsByTagName('img');
				if ( ! img[0] ) {
					links[i].style.background = 'url(style/link_remote.png) left center no-repeat';
					links[i].style.paddingLeft = '10px';
				}
				
				// links[i].target = '_blank';
				
			}
		}
		
	};

	
/*	
	function $( id ) {
		return document.getElementById( id );
	}
*/	
	function isHTMLElement( elem ) {
		if ( document.all )
			return !!( elem && elem.nodeType );
		else
			return elem instanceof HTMLElement;
		// return elem != null && elem.nodeType == Node.ELEMENT_NODE;
	}
	
	
	
	function openPopup( url, newwindow ) {
		var wname = newwindow ? '_blank' : 'popup';
		popup = window.open( url, wname, "width=720,height=550,scrollbars=yes,location=yes,resizable=yes,toolbar=yes,menubar=yes" );
		popup.focus();
	}


	function flashBackground( id, firstcolor, midcolor ) {
/*	
		if ( firstcolor == null ) firstcolor = '#fff';
		if ( midcolor == null ) midcolor = '#fdb';
	
		var i = 0;
		for ( i = 0; i < 5; i++ ) {
			window.setTimeout( 'document.getElementById("'+id+'").style.background = "'+firstcolor+'";', 300 + i*300 );
			window.setTimeout( 'document.getElementById("'+id+'").style.background = "'+midcolor+'";', 300 + i*300 + 150);
		}
		window.setTimeout( 'document.getElementById("'+id+'").style.background = "'+firstcolor+'";', 300 + i*300 );
*/	
	}
	
	
	
//	var timeouts = new Object(); // assoc array


	function incBoxAnimated( id, destheight ) {
	
		// var div = document.getElementById( id );
		var div = isHTMLElement(id) ? id : document.getElementById( id );
		if ( div ) {
			var curheight = parseInt( div.style.height );
		    if ( curheight < destheight ) {
				var newheight = Math.ceil( curheight * 2 );    // logarithmic growing
				newheight = Math.max( newheight, curheight+10 ); // min 10 pixels each step
				newheight = Math.min( destheight, newheight );   // not more than maxheight
				div.style.height = newheight + 'px';
				// div.style.opacity = Math.min( parseFloat(div.style.opacity)+0.1, 1 );
				// div.style.opacity = parseFloat(newheight) / div.scrollHeight;
				div.style.opacity = parseFloat(newheight) / destheight;
			}
			else {
				div.style.height = 'auto';
				div.style.overflow = 'visible';
				div.style.display = 'block';
				div.style.opacity = 1;
				window.clearInterval( div.ajaxtimeout );
			}
		}
		
    }

	function decBoxAnimated( id, destheight ) {
	
		// var div = document.getElementById( id );
		var div = isHTMLElement(id) ? id : document.getElementById( id );
		if ( div ) {
			var curheight = parseInt( div.style.height );
		    if ( curheight > destheight ) {
				var newheight = Math.ceil( curheight * 0.5 );    // logarithmic growing
				newheight = Math.min( newheight, curheight-10 ); // min 10 pixels each step
				newheight = Math.max( destheight, newheight );   // not more than maxheight
				div.style.height = newheight + 'px';
				// div.style.opacity = Math.max( parseFloat(div.style.opacity)-0.1, 0 );
				div.style.opacity = parseFloat(newheight) / div.scrollHeight;
			}
			else {
				div.style.height = '0px';
				div.style.overflow = 'hidden';
				div.style.display = 'none';
				div.style.opacity = 0;
				window.clearInterval( div.ajaxtimeout );
			}
		}
	
    }

	function openBoxAnimated( id, html ) {
	
		//$(id).html( html ).fadeIn('slow');
		//return;
	
		var div = isHTMLElement(id) ? id : document.getElementById( id );
		if ( div ) {
		
			div.style.height = '0px';
			div.style.overflow = 'hidden';
			div.style.display = 'block';
			div.style.opacity = 0;

			if ( html != null ) {
				div.innerHTML = html;
			}
			
			var destheight = div.scrollHeight;
			if ( destheight > 1000 )
				destheight = 1000;
			//alert( destheight );
			window.clearInterval( div.ajaxtimeout );
			div.ajaxtimeout = window.setInterval( function() { incBoxAnimated(id,destheight); }, 20 );
		}
		else {
			alert( 'Element "' + id + '" not found.' );
		}
	
	}

	function closeBoxAnimated( id, html ) {

		// var div = document.getElementById( id );
		var div = isHTMLElement(id) ? id : document.getElementById( id );
		if ( div ) {
			div.style.height = parseInt(div.scrollHeight) + 'px';
			div.style.overflow = 'hidden';
			div.style.display = 'block';
			div.style.opacity = 1;
			
			if ( html != null ) {
				div.innerHTML = html;
			}
			
			window.clearInterval( div.ajaxtimeout );
			div.ajaxtimeout = window.setInterval( function() { decBoxAnimated(id,0); }, 20 );
		}
		else {
			alert( 'Element "' + id + '" not found.' );
		}

	}
	
	
	function toggleBoxAnimated( id ) {
	
		var div = isHTMLElement(id) ? id : document.getElementById( id );
		if ( div ) {
			if ( div.style.display == 'none' )
				openBoxAnimated( div );
			else
				closeBoxAnimated( div ); 
		}
	
	}

	
	
/* toggle menu item
	function toggleMenuItem( id, boldElem ) {
	
		var div = isHTMLElement(id) ? id : document.getElementById( id );
		if ( div.style.display == 'none' ) {
			boldElem.getElementsByTagName('img')[0].src = 'style/button_down.png';
		}
		else {
			boldElem.getElementsByTagName('img')[0].src = 'style/button_right.png';
		}
		
		toggleBoxAnimated( id );
		
	}
*/

	
	// assoc array: one xmlhttpobject for each targetid
	var request = new Object();
	
	
	
	function getHTML_CloseButton( targetid ) {
		return '<div style="position:absolute; top:5px; right:5px; text-align:right;">'
			// + '<a href="#" onclick="document.getElementById(\'' + targetid + '\').style.display=\'none\';return false">'
			+ '<a href="#" onclick="if (!SaveChanges.isregisteredChange() || SaveChanges.confirmClose()) closeBoxAnimated(\'' + targetid + '\'); return false">'
			+ '<img src="style/icon_img_delete.png" border="0"></a></div>';
	}

	// GetAsyncData sends a request to read the fifo.
	function GetAsyncData( url, params, targetid, closable, onsuccessHTML ) {
	
		// load default values if required
		params = params==null ? '' : params;
		targetid = targetid==null ? 'configdiv' : targetid;
		closable = closable==null ? true : closable;

		var target = document.getElementById( targetid );
		
		// check if SaveChanger is active and avoid unloading unsafed data
		if ( targetid == 'configdiv' ) {
			if ( SaveChanges.isregisteredChange() ) {
				if ( ! SaveChanges.confirmClose() ) {
					return;
				}
			}
		}
		
		/*
		// if params is a form object fetch its values now
		if ( params && params.length && params.elements ) {
			params = getFormParams( params );
		}
		*/
		
		// build html fragment that indicates the running request
		if ( target ) {
			var htmlwait = '';
			if ( closable )
				htmlwait += getHTML_CloseButton( targetid );
			if ( targetid.match(/^tiny/) )
				htmlwait += '<img src="style/ajax_loading_tiny.gif">';
			else
				htmlwait += '<p style="padding-left:10px"><img src="style/ajax_loading.gif"></p>';
			// document.getElementById( targetid ).innerHTML = htmlwait;
			//document.getElementById( targetid ).style.display = 'block';
			//document.getElementById( targetid ).style.height = 'auto';
			openBoxAnimated( targetid, htmlwait );
		}
		
	    // branch for native XMLHttpRequest object and IE/ActiveX version
		if ( request[targetid] == null ) {
		    if ( window.XMLHttpRequest )
				request[targetid] = new XMLHttpRequest();
			else if ( window.ActiveXObject )
				request[targetid] = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		var r = request[targetid];
		if ( r ) {

			// Timeout after 15 sec!
			var requestTimer = setTimeout( function() {
				r.abort();
				GotAsyncData(targetid,closable,onsuccessHTML); 
			}, 15000 );
		
			// "open" before "onreadystatechange" due to reusability of XMLHttpRequest object
			r.open( "POST", url, true );
			// r.onreadystatechange = new Function("",'GotAsyncData("'+targetid+'",'+closable+')');
			r.onreadystatechange = function() { 
				if (r.readyState != 4)  { return; }
				clearTimeout(requestTimer);
				GotAsyncData(targetid,closable,onsuccessHTML); 
			};
			
			// send proper POST header along with the request
			r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			r.setRequestHeader("Content-length", params.length);
			r.setRequestHeader("Connection", "close");
			r.send( params );

		}
		
	}

	// GotAsyncData is the callback for the above XMLHttpRequest() call.
	// This routine is not executed until data arrives from the request.
	// We update the "targetid" area on the page when data does arrive.
	function GotAsyncData( targetid, closable, onsuccessHTML ) {
	
		var target = document.getElementById( targetid );
		var closebutton = closable ? getHTML_CloseButton(targetid) : '';
		var r = request[targetid];
	
	    // only if request shows "loaded"
	    if ( r && r.readyState == 4 ) {
			if ( r.status == 200 ) {
			
				// if ( target )
				//	target.style.background = targetid == 'configdiv' ? "#fe7" : "#fff";
				
				// if there is a <script> tag as the beginning of the request,
				// execute it as javascript function
				/*
				var text = r.responseText;
				var script = '';
				if ( text.indexOf("<scr"+"ipt>") == 0 ) {
					var end = text.indexOf("</scr"+"ipt>");
					if ( end >= 0 ) {
						script = text.substring( 8, end ); // 8 == strlen("<scr"+"ipt>")
						text = text.substring( end + 9 );  // 9 == strlen("</scr"+"ipt>")
					}
				}
				*/
				
				// search for script-tags and extract for later execution
				var text = r.responseText;
				text = text.replace( /\r/g, '' );
				text = text.replace( /\n/g, '\uffff' );
				var script = new Array();
				var result = text.match( /\<script\>(.*?)\<\/script\>/g );
				if ( result ) {
					for ( var i = 0; i < result.length; ++i )
						scripttext = result[i].replace( /\uffff/g, '\n' );
						scripttext = scripttext.substring( 8, scripttext.length-9 );
						script.push( scripttext );
				}
				text = text.replace( /\<script\>(.*?)\<\/script\>/g, '' );
				text = text.replace( /\uffff/g, '\n' );
				
				
				if ( target ) {
					if ( onsuccessHTML ) {
						openBoxAnimated( targetid, onsuccessHTML );
					}
					else if ( text.length ) {
						text = closebutton + text;
						// document.getElementById( targetid ).innerHTML = text;
						// document.getElementById( targetid ).style.display = 'block';
						openBoxAnimated( targetid, text );
						if ( text.match(/class="error"/) ) {
							flashBackground( targetid );
						}
					}
					else {
						document.getElementById( targetid ).innerHTML = '';
						document.getElementById( targetid ).style.display = 'none';
					}
				}
				
				try {
					if ( script.length ) {
						for ( var i = 0; i < script.length; i++ ) {
							// alert( script[i] );
							var func = new Function( '', script[i] );
							func();
						}
					/*
						var func = new Function("",script);
						func();
					*/
					}
				}
				catch ( e ) {
					alert( "Exeption caught in GotAsyncData: " + e );
				}

			}
			else {
				if ( target ) {
					var msg = '<h2>Fehlermeldung</h2><p>' + r.status + ' ' + r.statusText + '</p>';
					// document.getElementById( targetid ).innerHTML = closebutton + msg;
					// document.getElementById( targetid ).style.display = 'block';
					openBoxAnimated( targetid, closebutton + msg );
				}
			}
			
			if ( targetid == 'configdiv' )
				window.scrollTo( 0, 0 );
			
	    }
		else {
			// Timeout!
			if ( target ) {
				// document.getElementById( targetid ).innerHTML = closebutton + msg;
				var msg = '<h2>Fehlermeldung</h2><p>Zeitüberschreitung. Der Server hat keine Rückmeldung gegeben. Entweder braucht der Server zu lange für die Bearbeitung dieser Anfrage oder er hat sie verschlafen...</p>';
				// document.getElementById( targetid ).style.display = 'block';
				openBoxAnimated( targetid, closebutton + msg );
			}
		}

	}

	
	
	/**
	 *
	 * Funktionen für Sitemap
	 *
	 */
	Sitemap = {
	
		create : function( id ) {
			var backup = document.getElementById('tinycreate'+id).innerHTML;
			document.getElementById('tinycreate'+id).innerHTML = '<img src="style/ajax_loading_tiny.gif" width="13" height="13">';
			
			var name = prompt( "Bitte gib den neuen Namen ein:", "" );
			if ( name != null ) {
				name = encodeURIComponent( name );
				GetAsyncData( '_config_sitemap.php', 'action=new&parent=' + id + '&name=' + name, 'tinycreate'+id, false, backup );
			}
			else {
				document.getElementById('tinycreate'+id).innerHTML = backup;
			}
		},
		
		createCallOnSuccess : function( id, name, filename, parentid ) {
		
			var li = document.createElement('li');
			li.innerHTML = '<span style="color:#c00">' + name + '</span> <span style="color:#999">[' + id + ']' + '</span> <a href="#" onclick="document.location.reload(); return false;" style="font-size:11px">&raquo; Sitemap muss neu geladen werden</a>';
			
			var parentli = document.getElementById( 'name' + parentid ).parentNode;
			var parentul = parentli.getElementsByTagName('ul')[0];
			if ( parentul ) 
				parentul.appendChild( li );
			else {
				var ul = document.createElement('ul');
				ul.appendChild( li );
				parentli.appendChild( ul );
			}
		
		},
	
		rename : function( id ) {
			var backup = document.getElementById('tinyrename'+id).innerHTML;
			document.getElementById('tinyrename'+id).innerHTML = '<img src="style/ajax_loading_tiny.gif" width="13" height="13">';
			
			var old = document.getElementById( 'name' + id ).innerHTML;
			var name = prompt( "Bitte gib den neuen Namen ein:", old );
			if ( name != null ) {
				name = encodeURIComponent( name );
				GetAsyncData( '_config_sitemap.php', 'action=rename&id=' + id + '&name=' + name, 'tinyrename'+id, false, backup );
			}
			else {
				document.getElementById('tinyrename'+id).innerHTML = backup;
			}
		},
		
		renameCallOnSuccess : function( id, name, filename ) {
			var tag = document.getElementById( 'name' + id ); // is an <a> or <span> tag
			if ( tag ) {
				tag.innerHTML = name;
				if ( tag instanceof HTMLAnchorElement )
					tag.href = filename;
			}
		},
	
		edit : function( id ) {
			GetAsyncData( '_config_pageedit.php','id=' + id );
		},

		remove : function( id ) {
			var backup = document.getElementById('tinyremove'+id).innerHTML;
			document.getElementById('tinyremove'+id).innerHTML = '<img src="style/ajax_loading_tiny.gif" width="13" height="13">';

			var name = document.getElementById( 'name' + id ).innerHTML;
			var ok = confirm( "\"" + name + "\" wirklich löschen?" );
			if ( ok ) {
				GetAsyncData( '_config_sitemap.php', 'action=delete&id=' + id, 'tinyremove'+id, false, backup );
			}
			else {
				document.getElementById('tinyremove'+id).innerHTML = backup;
			}
		},
		
		removeCallOnSuccess : function( id ) {
			
			var tag = document.getElementById( 'name' + id );
			if ( tag ) {
				var li = tag.parentNode;
				return li.parentNode.removeChild( li );
			}
			
		},
		
		moveup : function( id ) {
			var backup = document.getElementById('tinymoveup'+id).innerHTML;
			document.getElementById('tinymoveup'+id).innerHTML = '<img src="style/ajax_loading_tiny.gif" width="13" height="13">';
			
			GetAsyncData( '_config_sitemap.php', 'action=up&id=' + id, 'tinymoveup'+id, false, backup );
		},

		movedown : function( id ) {
			var backup = document.getElementById('tinymovedown'+id).innerHTML;
			document.getElementById('tinymovedown'+id).innerHTML = '<img src="style/ajax_loading_tiny.gif" width="13" height="13">';
			
			GetAsyncData( '_config_sitemap.php', 'action=down&id=' + id, 'tinymovedown'+id, false, backup );
		},
		
		
		moveCallOnSuccess : function( id, newparentid, n ) {
		
			// alert( 'insert as ' + n + 'th item of ' + newparentid );
		
			// delete first
			var listitem = Sitemap.removeCallOnSuccess( id );
			
			// and insert as n-th entry of newparent
			//alert( newparentid );
			var parentname = document.getElementById( newparentid==1 ? 'sitemap' : 'name'+newparentid );
			var parentli = parentname.parentNode;
			var parentul = parentli.getElementsByTagName('ul')[0];
			if ( parentul ) {
			
				var sibling = parentul.firstChild;
				var cnt = newparentid==1 ? -1 : 0;
				while ( sibling && cnt < n ) {
					sibling = sibling.nextSibling;
					if ( sibling && sibling.nodeName == 'LI' )
						cnt++;
				}
				
				if ( sibling ) {
					parentul.insertBefore( listitem, sibling );
				}
				else {
					parentul.appendChild( listitem );
				}
				
			}
			else {
				// no <ul> found: must be first item in new list
				var ul = document.createElement('ul');
				ul.appendChild( listitem );
				parentli.appendChild( ul );
			}
		
		}
		
/*
		toggle : function( prefix ) {
		
			var trs = document.getElementById('TABLE_sitemap').getElementsByTagName('tr');
			for ( var i = 0; i < trs.length; i++ ) {
			
				if ( trs[i].id.substring(0,prefix.length+1) == prefix+'-' ) {
					with ( trs[i].style ) {
						display = display=='none' ? '' : 'none';
					}
				}
			}
		
		},
*/
	};

	
	
	
	
	// Check/Uncheck/Reset all checkboxes of a form (value = true/false/null)
	function formCheckAll( myform, value ) {
		for ( var i = 0; i < myform.length; i++ ) {
			if ( myform[i].type == 'checkbox' )
				myform[i].checked = value==null ? myform[i].defaultChecked : value;
		}
	}
	
	function getFormParams( myform ) {
		var params = new Array();
		for ( var i = 0; i < myform.length; i++ ) {
			// myform[i].readOnly = true;
			// myform[i].style.background = "#eee";
			// myform[i].style.color = "#999";
			if ( myform[i].type == 'checkbox' && ! myform[i].checked )
				continue; // skip unchecked checkboxes
			if ( myform[i].name != null && myform[i].name.length )
				params.push( encodeURIComponent(myform[i].name) + '=' + encodeURIComponent(myform[i].value) );
				
		}
		// alert( params.join("\n") );
		return params.join( '&' );
	}
	
	//function adjustTextareaHeight( id ) {
	//	document.getElementById( id ).style.height = document.getElementById( id ).scrollHeight + "px";
	//}

	
	ArticleEdit = {
	
		createMyInputElement : function( type, id, name, value, textalign, width ) {
			var input = document.createElement('INPUT');
			input.type = type;
			input.id = id;
			input.name = name;
			input.value = value;
			input.style.textAlign = textalign;
			input.style.width = width;
			
			input.onfocus = function() { SaveChanges.handler.onfocus(input); };
			input.onblur = function() { SaveChanges.handler.onblur(input); };
			
			return input;
		},
	
		addDATA : function() {
			var table = document.getElementById('TABLE_data');
			var count = table.rows.length - 2;
			
			var inputNUMBER = ArticleEdit.createMyInputElement( 'text', 'number'+count, 'number[]', '', 'center', '100px' );
			var inputSIZE = ArticleEdit.createMyInputElement( 'text', 'size'+count, 'size[]', '', 'center', '100px' );
			var inputCONTENT = ArticleEdit.createMyInputElement( 'text', 'content'+count, 'content[]', '', 'center', '50px' );
			var inputPRICE = ArticleEdit.createMyInputElement( 'text', 'price'+count, 'price[]', '', 'center', '50px' );
			var inputOLDPRICE = ArticleEdit.createMyInputElement( 'text', 'oldprice'+count, 'oldprice[]', '', 'center', '50px' );
			var inputSCALEDPRICE = ArticleEdit.createMyInputElement( 'text', 'scaledprice'+count, 'scaledprice[]', '', 'center', '100px' );
			var inputDELIVERY = ArticleEdit.createMyInputElement( 'text', 'delivery'+count, 'delivery[]', '', 'center', '25px' );
			var inputBOTTLES = ArticleEdit.createMyInputElement( 'text', 'bottles'+count, 'bottles[]', '', 'center', '25px' );
			
			var row = table.insertRow( count+1 );
			row.style.background = count%2 ? '#eee' : '#fff';
			row.insertCell(0).appendChild(inputNUMBER);
			row.insertCell(1).appendChild(inputSIZE);
			row.insertCell(2).appendChild(inputCONTENT);
			row.insertCell(3).appendChild(inputPRICE);
			row.insertCell(4).appendChild(inputOLDPRICE);
			row.insertCell(5).appendChild(inputSCALEDPRICE);
			row.insertCell(6).appendChild(inputDELIVERY);
			row.insertCell(6).appendChild(inputBOTTLES);
			
		},

		addLINK : function() {
			var table = document.getElementById('TABLE_link');
			var count = table.rows.length - 2;
			
			var inputNUM = ArticleEdit.createMyInputElement( 'text', 'linknum'+count, 'linknum[]', '', 'center', '100%' );
			var inputTXT = ArticleEdit.createMyInputElement( 'text', 'linktxt'+count, 'linktxt[]', '', 'left', '500px' );

			var row = table.insertRow( count+1 );
			row.style.background = count%2 ? '#eee' : '#fff';
			row.insertCell(0).appendChild(inputNUM);
			row.insertCell(1).appendChild(inputTXT);
		},

		updateLogoInput : function() {
		
			var logobox = document.getElementById('BOX_logo');
			var divs = logobox.getElementsByTagName('div');
			
			var logoids = new Array();
			
			for ( var i = 0; i < divs.length; i++ ) {
				if ( divs[i].style.opacity != 0.25 ) {
					var title = divs[i].title;
					if ( title.search(/^\d+ /) != -1 ) {
						logoids.push( parseInt(title) );
					}
				}
			}
			
			document.articleform.logos.value = logoids.join(',');
			
		},
		
		addLogoBox : function( pid, pname, pfile, pdimx, pdimy ) {
		
			try {
				var logobox = document.getElementById('BOX_logo');
				var firstdiv = logobox.getElementsByTagName('div')[0];
				var logonode = firstdiv.cloneNode(true);
				
				logonode.title = pid + ' ' + pname;
				logonode.style.opacity = 1;
				logonode.style.display = 'block';
				logonode.getElementsByTagName('img')[1].src = 'style/icon_img_delete.png';
				logonode.getElementsByTagName('span')[0].innerHTML = pid;
				logonode.getElementsByTagName('span')[1].innerHTML = pname;

				var img = logonode.getElementsByTagName('img')[0];
				img.src = pfile;
				img.width = pdimx;
				img.height = pdimy;
				
				var logoend = document.getElementById('BOX_logo_end');
				logoend.parentNode.insertBefore( logonode, logoend );
				
				ArticleEdit.updateLogoInput();
			}
			catch ( e ) {
			}

		},
		
		deleteLogoBox : function( delButton ) {
		
			var div = delButton.parentNode;
			if ( div.style.opacity == 0.25 ) {
				div.style.opacity = 1;
				div.getElementsByTagName('img')[1].src = 'style/icon_img_delete.png';
			}
			else {
				div.style.opacity = 0.25;
				div.getElementsByTagName('img')[1].src = 'style/icon_img_restore.png';
			}
			ArticleEdit.updateLogoInput();
			
		},

		updateImageInput : function() {
		
			var logobox = document.getElementById('BOX_image');
			var divs = logobox.getElementsByTagName('div');
			
			var logoids = new Array();
			
			for ( var i = 0; i < divs.length; i++ ) {
				if ( divs[i].style.opacity != 0.25 ) {
					var title = divs[i].title;
					var pos = title.indexOf(' ');
					if ( pos > 0 ) {
						title = title.substring( 0, pos );
						logoids.push( title );
					}
				}
			}
			
			document.articleform.images.value = logoids.join(',');
			
		},
		
		addImageBox : function( pid, pname, pfile, pdimx, pdimy ) {
		
			try {
				var logobox = document.getElementById('BOX_image');
				var firstdiv = logobox.getElementsByTagName('div')[0];
				var logonode = firstdiv.cloneNode(true);
				
				logonode.title = pid + ' ' + pname;
				logonode.style.opacity = 1;
				logonode.style.display = 'block';
				logonode.getElementsByTagName('img')[1].src = 'style/icon_img_delete.png';
				logonode.getElementsByTagName('span')[0].innerHTML = pid;
				logonode.getElementsByTagName('span')[1].innerHTML = pname;

				var img = logonode.getElementsByTagName('img')[0];
				img.src = pfile + '&' + (new Date()).getTime();
				//img.width = pdimx;
				//img.height = pdimy;
				
				var logoend = document.getElementById('BOX_image_end');
				logoend.parentNode.insertBefore( logonode, logoend );
				
				ArticleEdit.updateImageInput();
			}
			catch ( e ) {
				alert( e );
			}

		},

		deleteImageBox : function( delButton ) {
		
			var div = delButton.parentNode;
			if ( div.style.opacity == 0.25 ) {
				div.style.opacity = 1;
				div.getElementsByTagName('img')[1].src = 'style/icon_img_delete.png';
			}
			else {
				div.style.opacity = 0.25;
				div.getElementsByTagName('img')[1].src = 'style/icon_img_restore.png';
			}
			ArticleEdit.updateImageInput();
			
		}
		
	};



	/**
	*
	* Tooltips
	*
	**/
	Tooltips = {
	
	
    /* getElementByClass */

		// var allHTMLTags = new Array();

		init : function() {

			// Create Array of All HTML Tags
			var allHTMLTags = document.getElementsByTagName("*");

			// Loop through all tags using a for loop
			for ( i = 0; i < allHTMLTags.length; i++ ) {
				elem = allHTMLTags[i];

				// Get all tags with the specified class name.
				if ( elem.className == 'tooltipped' ) {
					elem.onmouseover = Tooltips.show;
					elem.onmouseout  = Tooltips.hide;
					elem.onmousemove = Tooltips.move;
				}
			}
		},	
	
		load : function( elem ) {
			var tooltip = document.createElement('div');
			tooltip.className = "tooltip";
			tooltip.innerHTML = elem.title;
			
			elem.removeAttribute('title');
			document.getElementsByTagName('body')[0].appendChild( tooltip ); // insert into DOM
			elem.$$tooltip = tooltip;    // save reference for later access
		},
		
		show : function( event ) {
			var elem = this;
			if ( ! elem.$$tooltip )
				Tooltips.load( elem );
			elem.$$tooltip.style.display = 'block';
		},
		
		hide : function( event ) {
			var elem = this;
			if ( elem.$$tooltip )
				elem.$$tooltip.style.display = 'none';
		},
		
		move : function( event ) {
			var elem = this;
			if ( elem.$$tooltip ) {
				
				var docwidth = window.innerWidth-15;
				var docheight = window.innerHeight-18;
				var twidth = elem.$$tooltip.offsetWidth;
				var theight = elem.$$tooltip.offsetHeight;
				var tipx = event.pageX+10; // paddings
				var tipy = event.pageY+10;
				
				var marginLeft = (docwidth - document.body.offsetWidth) / 2;
				tipx -= marginLeft;
				
				tipx = (event.clientX+twidth>docwidth)? tipx-twidth-(2*10) : tipx; //account for right edge
				tipy = (event.clientY+theight>docheight)? tipy-theight-(2*10) : tipy; //account for bottom edge
				// $tooltip.css({left: tipx, top: tipy})
				
				// elem.$$tooltip.innerHTML += ' ' + tipx + 'x' + tipy;
				
				elem.$$tooltip.style.left = tipx + 'px';
				elem.$$tooltip.style.top = tipy + 'px';
			}
		}
		
	};



	

	/**
	*
	*  File upload via AJAX
	*
	*  AJAX IFRAME METHOD (AIM)
	*  http://www.webtoolkit.info/
	*
	**/
	AIM = {

		frame : function(c) {

			var n = 'f' + Math.floor(Math.random() * 99999);
			var d = document.createElement('DIV');
			d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
			document.body.appendChild(d);

			var i = document.getElementById(n);
			if (c && typeof(c.onComplete) == 'function') {
				i.onComplete = c.onComplete;
			}

			return n;
		},
		
		form : null,

//		form : function(f, name) {
//			f.setAttribute('target', name);
//		},

		submit : function(f, c) {
			// AIM.form(f, AIM.frame(c));
			
			AIM.form = f;
			
			c.onStart();
			
			var name = AIM.frame(c);
			f.setAttribute('target', name);
			f.action = "_config_fileupload.php";
			f.onsubmit = function() { return true; };
			
			if (c && typeof(c.onStart) == 'function') {
				return true; // c.onStart();
			} else {
				return true;
			}
		},

		loaded : function(id) {
			var i = document.getElementById(id);
			if (i.contentDocument) {
				var d = i.contentDocument;
			} else if (i.contentWindow) {
				var d = i.contentWindow.document;
			} else {
				var d = window.frames[id].document;
			}
			if (d.location.href == "about:blank") {
				return;
			}

			if (typeof(i.onComplete) == 'function') {
				i.onComplete(d.body.innerHTML);
			}
		},
		
		startCallback : function() {
			// make something useful before submit (onStart)
			htmlwait = '<img src="style/ajax_loading.gif">';
			openBoxAnimated( 'fileuploadresponse', htmlwait );
			// with ( document.fileuploadform.submitbutton ) {
			with ( AIM.form.upload ) {
				disabled = true;
				style.color = '#999';
				value = "Bitte warten...";
			}
			return true;
		},
		
		completeCallback : function(response) {
			// make something useful after submit (onComplete)
			openBoxAnimated( 'fileuploadresponse', response );
			// with ( document.fileuploadform.submitbutton ) {
			with ( AIM.form.upload ) {
				disabled = false;
				style.color = null;
				value = "Upload";
			}
			
			if ( AIM.form.getAttribute('Name') == 'articleform' ) {
			
				var regex = /upload(\d+).jpg, (\d+)x(\d+) Pixel/;
				var matches = regex.exec( response );
				if ( matches ) {
					
					var articleid = parseInt( AIM.form.id.value );
					var uploadnum = parseInt( matches[1] );
					var uploadname = 'upload' + uploadnum + '.jpg';
					var file = '_shop_articleedit.php?id=' + articleid + '&getimg=' + (uploadnum-1);
					ArticleEdit.addImageBox( uploadnum, uploadname, file );
					
				}
			
				
			}
			
		}

	};
	
	
	/**
	*
	*  
	*
	**/
/*
	MyCookie = {
	
		toggleCookie : function( group ) {
			var c = MyCookie.getCookie();
			if ( c && c.length ) {
				var hash = new Object();
				
				var groups = c.split(',');
				for ( var i = 0; i < groups.length; i++ ) {
					hash[groups[i]] = true;
				}
								
				if ( hash[group] )
					delete( hash[group] );
				else
					hash[group] = true;
				
				var newcookie = '';
				for ( var key in hash )
					newcookie += ',' + key;
				newcookie = newcookie.substr( 1 );
				document.cookie = 'opengroups=' + newcookie;
			}
			else
				document.cookie = 'opengroups=' + group;
				
			// alert(c);
		},
		
		setCookie : function( group ) {
			var c = MyCookie.getCookie();
			if ( c && c.length )
				document.cookie = "opengroups=" + c + ',' + group;
			else
				document.cookie = "opengroups=" + group;
		},
		
		getCookie : function() {
			var parts = document.cookie.split('; ');
			for ( var i = 0; i < parts.length; i++ ) {
				var part = parts[i];
				var keyval = part.split( '=', 2 );
				if ( keyval[0] == 'opengroups' )
					return keyval[1];
			}
			return '';			
			
		},		
		
	};
*/


	SSLverify = {
	
		// url : 'https://www.thawte.com/core/process?process=public-site-seal-cert-details&public-site-seal-cert-details.referer=http://www.israelwein.de',
		url : 'https://sealinfo.thawte.com/thawtesplash?form_file=fdf/thawtesplash.fdf&dn=WWW.ISRAELWEIN.DE&lang=en',
		popup : '',

		view : function() {

			// if popup already open, reload url
			if ( !SSLverify.popup.closed && SSLverify.popup.location )
				SSLverify.popup.location.href = SSLverify.url;
			
			else // open new popup window otherwise
				SSLverify.popup = window.open( SSLverify.url, 'sslverifywindow', 'height=400,width=516,scrollbars=yes,status=1,location=yes' );
			
			SSLverify.popup.focus(); // bring popup to front
			return false; // ignore href due to js action
			
		},
		
		print : function() {

			// document.write('<a href="'+SSLverify.url+'" target="sslverifywindow" onClick="return SSLverify.view()"><img border="0" src="style/ssl_secured_by_thawte.gif" alt="Click to verify" width="80" height="80" style="margin:0px auto"></a>');
			document.write('<a href="'+SSLverify.url+'" target="sslverifywindow" onClick="return SSLverify.view()"><img border="0" src="style/ssl_secured.png" alt="SSL-Zertifikat anzeigen" width="120" height="120" style="margin:0px auto"></a>');
			
		}

	};

	

	Helper = {
	
		showArchiveWeek: function( weekid ) {
			document.getElementById('weekarchive'+weekid).style.display = 'none';
			document.getElementById('weekheader'+weekid).style.display = 'block';
			document.getElementById('weektable'+weekid).style.display = 'block';
		}
		
	};
