<!--

/*****************************************************************
JavaScript for SpandauGo / Kontakt
 	- Doc Holiday Data -

created by 	Dr.Holger Maerz 
version: 1.0 
Last Date Changes: 25.02.2007 
edited by: (pls enter name and date)

this class is for proving user input if JavaScript is enabled
bind key events to input fields ; input trimming ; bgColor for mandatory fields ;
switching color after input validation ; enables and disables button ; emailCheck  
*******************************************************************/

/////////////////////////////////////////////////////////////////////////////////////
//News CLASS
/////////////////////////////////////////////////////////////////////////////////////
 	
function ShoutBox(){
	
	this.bgColorError="#E9967A";
	this.bgColor="#587498";
	this.name=document.getElementsByName("sbname")[0];
	this.nr=document.getElementsByName("sbnr")[0];
	this.msg=document.getElementsByName("sbmsg")[0];
	this.ip=document.getElementsByName("sbip")[0];
	this.email=document.getElementsByName("sbmail")[0];
	this.resetBtn=document.getElementsByName("sbreset")[0];
	this.sentBtn=document.getElementsByName("sbsend")[0];
	this.adminPic="images/admin1.png";
	this.userPic="images/world2.png";
	
	
	
	this.init=function(){
	
		try {
				var myObj=this;
								
				//init smilies
				myObj.initSmilies();
				
				//init HelpBox
				myObj.initHelp();
				myObj.addDragDrop("menuebar");
				
				//init allBtns & input fields
				myObj.initButtons();
				
				
				
				
		}	
	    catch(e){		
				alert("JavaScript Exception: ShoutBox.init()\r\n\r\n" +e);
		}		
	}
	
	this.initSmilies=function(){
		
		try{
			
			var parent=document.getElementById("shouts_smilies");
			
			var smilies = parent.getElementsByTagName("input");
			for(var i=0;i<smilies.length;i++){
				smilies[i].onclick=this.setSmilie;
			}
		
		}	
	    catch(e){		
				alert("JavaScript Exception: ShoutBox.initSmilies()\r\n\r\n" +e);
		}
		
	}
	
	this.setSmilie=function(ev){
		
		try{
			
			//get event source
			if (!ev)	source = window.event.srcElement;
			else  source=ev.target;
			
			var txt=" "+source.value+" ";
			
			var sb=new ShoutBox();
			sb.insertAtCaret(sb.msg,txt);
			
		}	catch(e){alert("JavaScript Exception: ShoutBox.insertAtCaret()\r\n\r\n"+ e);	}
	}
	
	
	this.initButtons=function(){
		
		try{
				var myObj=this;
			
								
				//init input name
				myObj.name.onkeyup=myObj.testUI;
								
				//init input msg
				myObj.msg.onkeyup=myObj.testUI;
								
				//init input email
				myObj.email.onkeyup=myObj.testUI;
				
		}	
	    catch(e){		
				alert("JavaScript Exception: ShoutBox.initButtons()\r\n\r\n" +e);
		}	
		
	}
	
	this.testUI=function(ev){
		
		try{
			
			//get event source
			if (!ev)	source = window.event.srcElement;
			else  source=ev.target;
			
			var sb = new ShoutBox();
			var val = sb.trim(source.value);
								
			if(source==sb.name)	
				sb.setErrorColor(source, !(source.value.length>0 && source.value!="Name"));
			
			if(source==sb.msg)	
				sb.setErrorColor(source, !(source.value.length>0 && source.value!="Nachricht"));
			
			if(source==sb.email){
				var okEmail=true;
				if(source.value.length>0 && source.value!="Email") okEmail = sb.checkEmail(source.value);
				sb.setErrorColor(source, !(okEmail));
			}	
					
		}
		catch(e){		
				alert("JavaScript Exception: ShoutBox.testUI()\r\n\r\n" +e);
		}
	}
	
	
	
	
	this.initHelp=function(){
		
		try{
			
			//deaktiviere formular onsubmit
			if(document.getElementsByName("sb_helpclose")[0])
				document.getElementsByName("sb_helpclose")[0].onsubmit=function(){return false};			
			
			//deaktiviere formular onsubmit
			if(document.getElementsByName("sb_helpshow")[0])
				document.getElementsByName("sb_helpshow")[0].onsubmit=function(){return false};		
			
			//deaktiviere formular onsubmit
			if(document.getElementsByName("sb_helpclose2")[0])
				document.getElementsByName("sb_helpclose2")[0].onsubmit=function(){return false};		
							
			
			document.getElementsByName("sb_info_btn")[0].onclick=this.triggerHelp;
			var sbcloseBtn=document.getElementsByName("sbinfo_close_btn")[0];
			var sbcloseBtn2=document.getElementsByName("sbinfo_close")[0];
			
			sbcloseBtn.onclick=this.closeHelp;
			sbcloseBtn2.onclick=this.closeHelp;
			
			
		}	
	    catch(e){		
				alert("JavaScript Exception: ShoutBox.initHelp()\r\n\r\n" +e);
		}	
		
	}
	
	this.closeHelp=function(){
		
		try{
				var sbHelpBox=document.getElementById("shoutbox_info");
				sbHelpBox.style.visibility="hidden";
		}	
	    catch(e){		
				alert("JavaScript Exception: ShoutBox.closeHelp()\r\n\r\n" +e);
		}		
	}
	
	//drag&drop YUI
	this.addDragDrop=function(id) {
		
		try{
			/*uses Yahoo UI Lib for drag&drop of helper window*/		
			function ddParent(id, sGroup, onDragStyleClass, onStopStyleClass) {

				  if (id) {
				    // get parent id (window block element) and store it
				    this.parentId = document.getElementById(id).parentNode.id;
						
				    // invoke init method from superclass with parent id to set parent as
				    // drag and drop element
				    this.init(this.parentId);
						
				    // Set a child element of set element in init method which should be
				    // used to initiate the drag operation. In this case the given id
				    // vi constructor (window head element) should be initiat the drag operation.
				    this.setHandleElId(id);
						
				    
				    }
				}
			
			// extend from class DD
			ddParent.prototype = new YAHOO.util.DD();
			var dragElement = new ddParent(id, "", "", "");
		
		}	
	    catch(e){		
				alert("JavaScript Exception: ShoutBox.addDragDrop()\r\n\r\n" +e);
		}		
	}
		
	this.triggerHelp=function(){
		
		try{
				var sbHelpBox=document.getElementById("shoutbox_info");
				
				if(sbHelpBox.style.visibility=="visible")
					 sbHelpBox.style.visibility="hidden";
				else sbHelpBox.style.visibility="visible";	
		
		}	
	    catch(e){		
				alert("JavaScript Exception: ShoutBox.triggerHelp()\r\n\r\n" +e);
		}	
	}
	
	
	
	
	this.setErrorColor=function(target, error){
		try{
				(error)?target.style.background=this.bgColorError:target.style.background=this.bgColor;
		}
		catch(e){		
				alert("JavaScript Exception: ShoutBox.setErrorColor()\r\n\r\n" +e);
		}
		
	}
		
	
	
	this.trim = function(str){
	
		try{
			
			return(str.replace(/^\s*|\s*$/g, ""));
			
		}catch(e){alert("JavaScript Exception: ShoutBox.trim()\r\n\r\n"+ e);	}
	}
	
	this.checkEmail=function(str){
	
		try{
		
			var reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
	                   '(\\@)([a-zA-Z0-9\\-\\.]+)'+
	                   '(\\.)([a-zA-Z]{2,4})$');
	  		return (reg.test(str));
		
		}catch(e){alert("JavaScript Exception: ShoutBox.checkEmail()\r\n\r\n"+ e);	}
	}
	
	
	
	this.insertAtCaret=function(obj, text){
		
		try{
			if(document.selection) {   
		        // Go the IE way 
		        /* 	First of all, focus the object, we want to work with  
		   			If we do not do so, it is possible, that the selection  
		   			is not, where we expect it to be  
				*/  
				obj.focus();   
		  
				/* Create a TextRange based on the document.selection  
				   This TextRanged can be used to replace the selected  
				   Text with the new one  */  
				var range = document.selection.createRange();   
		  
				/* If the range is not part of our Object (remember the  
				   textarea or input field), stop processing here  */  
				if(range.parentElement() != obj) return false;   
		  
				/*  Save the current value. We will need this value later  
		   			to find out, where the text has been changed */  
				var orig = obj.value.replace(/rn/g, "n");   
		  
				/* Replace the Text */  
				range.text = text;   
		  
				/* Now get the new content and save it into  
				   a temporary variable	*/  
				var actual = obj.value.replace(/rn/g, "n");   
				var tmp	= obj.value.replace(/rn/g, "n");	  
				/* Find the first occurance, where the original differs  
				   from the actual content. This could be the startposition  
				   of our text selection, but it has not to be. Think of the  
				   selection "ab" and replacing it with "ac". The first  
				   difference would be the "c", while the start position  
				   is the "a"	*/  
				for(var diff = 0; diff < orig.length; diff++)    
					    if(orig.charAt(diff) != actual.charAt(diff)) break;   
					   
					 
				/* To get the real start position, we iterate through  
				   the string searching for the whole replacement  
				   text - "abc", as long as the first difference is not  
				   reached. If you do not understand that logic - no  
				   blame to you, just copy & paste it ;)*/ 
				var start=0;    
				for(var index = 0; (tmp = tmp.replace(text, "")) && index <= diff; index = start + text.length) {   
				    start = actual.indexOf(text, index);   
					}  
	          
	    	} else if(obj.selectionStart) {   
		        // Go the Gecko way  
			        /* Find the Start and End Position */  
					var start = obj.selectionStart;   
					var end   = obj.selectionEnd;   
					  
					/* Remember obj is a textarea or input field */  
					obj.value = obj.value.substr(0, start)   
					    + text   
					    + obj.value.substr(end, obj.value.length);  
				  
	    	} if(start != null)  this.setCaretTo(obj, start + text.length);   
	       	else {   
	        	// Fallback for any other browser
	          	obj.value+=' 8-o';  
	    	}   
		}catch(e){alert("JavaScript Exception: ShoutBox.insertAtCaret()\r\n\r\n"+ e);	}	
		
	}
	
	this.setCaretTo=function(obj, pos) {   
	    
	    try{
		    if(obj.createTextRange) {   
		        /* Create a TextRange, set the internal pointer to  
		           a specified position and show the cursor at this  
		           position    */  
		        var range = obj.createTextRange();   
		        range.move("character", pos);   
		        range.select();   
		    } else if(obj.selectionStart) {   
		        /* Gecko is a little bit shorter on that. Simply  
		           focus the element and set the selection to a  
		           specified position      */  
		        obj.focus();   
		        obj.setSelectionRange(pos, pos);   
		    }   
		    
	    }catch(e){alert("JavaScript Exception: ShoutBox.setCaretTo()\r\n\r\n"+ e);	}
	}  
	
}	


//-->
