window.addEvent('domready', function(){

/* Script for Tipps and input field effects  */

var Tipp1 = new Tips($$('.tipp1'), {
	initialize:function(){
		this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
	},
	onShow: function(toolTip) {this.fx.start(1);},
	onHide: function(toolTip) {this.fx.start(0);}
});



/* Add events to the input fields */


$('editform').getElements('input').each(function(el) {

		if (el.className != "submit" && el.className != "noedit") {
			if (el.className == "doctext") {
				el.onfocus = function() { el.className = "doctexton"; }
				el.onblur = function() { el.className = "doctext"; }
			} else {
				el.onfocus = function() { el.className = "on"; }
				el.onblur = function() { el.className = ""; }
			}
		}
});


$('editform').getElements('textarea').each(function(el) {

		el.onfocus = function() { el.className = "on"; }
		el.onblur = function() { el.className = ""; }
});


$('editform').getElements('select').each(function(el) {

		el.onfocus = function() { el.className = "on"; }
		el.onblur = function() { el.className = ""; }
});

$('entryform').addEvent('submit', function() {
			document.getElementById('submit').style.display = "none";
	});

});


/* General Scripts for Calendar and toggle  */

function toggle(link, id, showtext, hidetext ) {
	if (document.getElementById) {
		obj = document.getElementById(id);
		if (obj.style.display == "none") {
			obj.style.display = "block";
			link.innerHTML = hidetext;
		} else {
			obj.style.display = "none";
			link.innerHTML = showtext;
		}
	}
	return false;
}

function show_cal(id, show_link_id, hide_link_id) {
	var fadeCalInFx = new Fx.Styles(id);
	fadeCalInFx.start({
		'opacity':[0,1],
		'height':[0,240],
		'width': [0,180]
	});

	document.getElementById(show_link_id).style.display = "none";
	document.getElementById(hide_link_id).style.display = "inline";

}

function hide_cal(id, show_link_id, hide_link_id) {
	var fadeCalOutFx = new Fx.Styles(id);
	fadeCalOutFx.start({
		'opacity':[1,0],
		'height':[240,0],
		'width': [180,0]
	});

	document.getElementById(hide_link_id).style.display = "none";
	document.getElementById(show_link_id).style.display = "inline";

}

    
/* EE Scripts for setting the fields for Link insert  */
    
var selField  = false;
var selMode   = "normal";
   		
  		//  Dynamically set the textarea name


function clear_state()
        {
            if (usedarray[0])
            {
                while (usedarray[0])
                {
                    clearState = arraypop(usedarray);
                    eval(clearState + " = 0");
                    document.getElementById(clearState).className = 'htmlButtonA';
                }
                
				if (document.getElementById('close_all').className == 'htmlButtonB')
				{
					document.getElementById('close_all').className = 'htmlButtonA';
				}            
            }	
}
        

function setFieldName(which) {
	if (which != selField) {
		selField = which;
		clear_state();
		tagarray  = new Array();
		usedarray = new Array();
		running	  = 0;
	}
}

var no_cursor     = "Bitte klicken Sie zuerst in ein Eingabefeld!";
var url_text      = "Bitte die Link URL eingeben";
var webpage_text  = "Bitte den Titel des Links eingeben";
var title_text	  = "Optional: Ein Attribut eingeben";
var image_text    = "Bitte die Bild-URL angeben";
var email_text    = "Bitte die E-Mailadresse eingeben";
var email_title   = "Den Titel des links eingeben (oder Freilassen um die Adresse anzuzeigen)";
var enter_text    = "Enter the text you want to format";


function promptTag(which) {
            if ( ! selField)
            {
                alert(no_cursor);
                return;
            }
        
            if ( ! which)
                return;
                
            var theSelection = "";  
            eval("var theField = document.getElementById('entryform')." + selField + ";");
            
            if (document.selection)
            {	
                if (document.selection.createRange().text)
                {
                	theSelection = document.selection.createRange().text; 
                }
            }
            else if ( ! isNaN(theField.selectionEnd))
			{
				var selLength = theField.textLength;
				var selStart = theField.selectionStart;
				var selEnd = theField.selectionEnd;
				if (selEnd <= 2)
					selEnd = selLength;
				
				var s1 = (theField.value).substring(0,selStart);
				var s2 = (theField.value).substring(selStart, selEnd)
				var s3 = (theField.value).substring(selEnd, selLength);
				theSelection = s2;
			}
            
                
        
            if (which == "link")
            {
                var URL = prompt(url_text, "http://");
                        
                if ( ! URL || URL == 'http://' || URL == null)
                    return; 
                
                var Name = prompt(webpage_text, theSelection);
                
                if (Name == null)
                {
                	return;
                }
                
                if ( ! Name)
                {
                	Name = URL;
                }
         
                var Title = Name;
            
                var Link = '<a class="link_extern" href="' + URL + '" title="' + Title + '" target="_blank">' + Name + '<'+'/a>';
            }
            
            
            if (which == "link2")
            {
                var URL = prompt(url_text, "http://");
                        
                if ( ! URL || URL == 'http://' || URL == null)
                    return; 
                
                var Name = prompt(webpage_text, theSelection);
                
                if (Name == null)
                {
                	return;
                }
                
                if ( ! Name)
                {
                	Name = URL;
                }
         
                var Title = Name;
            
                var Link = '<a class="link_extern2" href="' + URL + '" title="' + Title + '" target="_blank">' + Name + '<'+'/a>';
            }
            
      
            if (which == "image")
            {
                var URL   = prompt(image_text, "http://");
                
                if ( ! URL || URL == null)
                    return; 
            
                var Link = '<img src="' + URL + '" />';
            }
        	
        	if (document.selection) 
            {		            
            	theField.focus();
            
            	document.selection.createRange().text = Link;
			}
            else if ( ! isNaN(theField.selectionEnd))
			{
				var newStart = s1.length + Link.length;
				theField.value = s1 + Link + s3;
				
				theField.focus();
				theField.selectionStart = newStart;
				theField.selectionEnd = newStart;
				return;
			}
			else
			{
            	eval("document.getElementById('entryform')." + selField + ".value += Link");		
            }
            
            theSelection = '';         
            theField.blur();        
            theField.focus();
            return;
       }
       
       


        // Insert tag
        function taginsert(item, tagOpen, tagClose)
        {
            // Determine which tag we are dealing with
        
            var which = eval('item.name');
            
            if ( ! selField)
            {
                alert(no_cursor);
                return false;
            }
            
            var theSelection = false;  
            var result		 = false
            eval("var theField = document.getElementById('entryform')." + selField + ";");
            
            if (selMode == 'guided')
            {
                data = prompt(enter_text, "");
                
                if ((data != null) && (data != ""))
                {
                    result =  tagOpen + data + tagClose;			
                }
            }
        
        
            // Is this a Windows user?
            // If so, add tags around selection
        
            if (document.selection) 
            {
            	theSelection = document.selection.createRange().text;
            	
            	theField.focus();
            
            	if (theSelection)
            	{
                	document.selection.createRange().text = (result == false) ? tagOpen + theSelection + tagClose : result;
                }
                else
                {
                	document.selection.createRange().text = (result == false) ? tagOpen + tagClose : result;
                }
                
                theSelection = '';
                
                theField.blur();
                theField.focus();
                
                return;
            }
            else if ( ! isNaN(theField.selectionEnd))
			{
				var scrollPos = theField.scrollTop;
				var selLength = theField.textLength;
				var selStart = theField.selectionStart;
				var selEnd = theField.selectionEnd;
				if (selEnd <= 2)
					selEnd = selLength;

				var s1 = (theField.value).substring(0,selStart);
				var s2 = (theField.value).substring(selStart, selEnd)
				var s3 = (theField.value).substring(selEnd, selLength);
				
				if (result == false)
				{
					var newStart = selStart + tagOpen.length + s2.length + tagClose.length;
				
					theField.value = (result == false) ? s1 + tagOpen + s2 + tagClose + s3 : result;
				}
				else
				{
					var newStart = selStart + result.length;
				
					theField.value = s1 + result + s3;
				}
				
				theField.focus();
				theField.selectionStart = newStart;
				theField.selectionEnd = newStart;
				theField.scrollTop = scrollPos;
				return;
			}
			
            // Add single open tags
            
            if (item == 'other')
            {
            	eval("document.getElementById('entryform')." + selField + ".value += tagOpen");
            }
            else if (eval(which) == 0)
            {
                var result = tagOpen;
                
                eval("document.getElementById('entryform')." + selField + ".value += result");			
                eval(which + " = 1");
                
                arraypush(tagarray, tagClose);
                arraypush(usedarray, which);
                
                running++;
                               
                styleswap(which);
            }
            else
            {
                // Close tags
            
                n = 0;
                
                for (i = 0 ; i < tagarray.length; i++ )
                {
                    if (tagarray[i] == tagClose)
                    {
                        n = i;
                        
                        running--;
                        
						while (tagarray[n])
						{
							closeTag = arraypop(tagarray);
							eval("document.getElementById('entryform')." + selField + ".value += closeTag");			
						}
						
						while (usedarray[n])
						{
							clearState = arraypop(usedarray);
							eval(clearState + " = 0");
							document.getElementById(clearState).className = 'htmlButtonA';
						}						
                    }
                }
                 
				if (running <= 0 && document.getElementById('close_all').className == 'htmlButtonB')
				{
					document.getElementById('close_all').className = 'htmlButtonA';
				}                
                
            }
            
            curField = eval("document.getElementById('entryform')." + selField);
            curField.blur();
            curField.focus();	
        }


        function nullo()
        {
            return;
        }
      
  /* ############# */
  
function quick_save(url) {
	document.getElementById('entryform').return_url.value= url;
	document.getElementById('entryform').submit();

}

function quick_del(url, input, val) {
	var the_form = document.getElementById('entryform');
	the_form.elements[input].value = val;
	if (confirm("Die hochgeladene Datei wirklich löschen?")) {
		document.getElementById('entryform').return_url.value= url;
		document.getElementById('entryform').submit();
	}
}

/* ##############  Formularfelder Überprüfung ################ */

function news_checkdata() {
	
	if (document.getElementById('entryform').title.value == "") {
		alert("Bitte einen Titel eingeben");
		document.getElementById('entryform').title.focus();
		return false;
	}
	if (document.getElementById('entryform').field_id_2.value == "") {
		alert("Bitte einen Inhalt eingeben");
		document.getElementById('entryform').field_id_2.focus();
		return false;
	}
	
	if (document.getElementById('entryform').field_id_6) {
		if (document.getElementById('entryform').field_id_6.value != "" && document.getElementById('entryform').field_id_7.value == "") {
			alert("Bitte eine Beschreibung zum Dokument 1 eingeben");
			document.getElementById('entryform').field_id_7.focus();
			return false;
		}
	}
	if (document.getElementById('entryform').field_id_8) {
		if (document.getElementById('entryform').field_id_8.value != "" && document.getElementById('entryform').field_id_9.value == "") {
			alert("Bitte eine Beschreibung zum Dokument 2 eingeben");
			document.getElementById('entryform').field_id_9.focus();
			return false;
		}
	}
	
	document.getElementById('entryform').submit();
}
