/*    $(document).ready(function() {
    	if($("#teamAddForm")){
       $("#teamAddForm").validationEngine({
    	   
    	   promptPosition: "topRight", // OPENNING BOX POSITION, IMPLEMENTED: topLeft, topRight, bottomLeft,  centerRight, bottomRight
    	   success :  false,
    	   failure : function() {}
       })
    	}

      })
      */


 function get(targetUrl,loadingId) {
	/*
	 if (!loadingId) loadingId = 'ajaxLoading';
		if (targetUrl) {		
		//showAjaxLoader(loadingId);
		new Ajax.Request(targetUrl, {
			method:'get',
			onSuccess: function(transport){
			
			$(loadingId).innerHTML = transport.responseText;		
				
		    },

			onFailure: function(){ alert('Something went wrong...'); }
		});
	}*/
	
	$.ajax({
		  url: targetUrl,
		  success: function(data) {
		
		    $('#'+loadingId).html(data);
		    
		  }
		});

		
}
 
 
 function addslashes(str) {
	 str=str.replace(/\\/g,'\\\\');
	 str=str.replace(/\'/g,'\\\'');
	 str=str.replace(/\"/g,'\\"');
	 str=str.replace(/\0/g,'\\0');
	 return str;
	 }
    
    function addRecepient(id,username, recepientDiv, targetId, formfield){
    	
    	username = addslashes(username);
    	if(id>0){
    		
    		
    		formFieldValue = dojo.byId(formfield).value;   		
    	
    		if(formFieldValue.length>0){
    			
    			arr = formFieldValue.split(",");
	    		
    			if(arr.length>0){
		    		drin = false;
		    		for(i=0; i<arr.length; i++){
		    			if(arr[i] == id){
		    				//schon drin
		    				drin = true;
		    			}
		    		}
		    		
		    		if(!drin){
		    			
		    			arr.push(id); //id zum Array hinzufügen
		    			
		    			
		    			
		    			dojo.byId(recepientDiv+id).hide(); //suchergebnis ausblenden
		    			//targetDivElement = dojo.create("li",{innerHTML:"<a href='#' onclick='removeRecepient("+id+", \""+username+"\", \""+ recepientDiv +"\",\""+ formfield +"\">"+username+"</a>"}); //neues empfänger li
		    			targetDivElement = dojo.create("li",{innerHTML:"<a href='#' onclick='removeRecepient("+ id +",\""+username+"\",\"user_receipient_res_\",\""+ recepientDiv +"\",\""+ formfield +"\");return false'>" + username + " - </a>", id:"user_receipient_res_"+ id}); //neues empfänger li
		        		dojo.byId(targetId).appendChild(targetDivElement); //empfängerli hinzufügen
		    			
		        		dojo.byId(formfield).value = arr.join(",");//wert in Recepient-Feld schreiben (für POST)
		    		}
		    		
		    		
	    		}
    		}else{
    			dojo.byId(recepientDiv+id).hide();
    			targetDivElement = dojo.create("li",{innerHTML:"<a href='#' onclick='removeRecepient("+ id +",\""+username+"\",\"user_receipient_res_\",\""+ recepientDiv +"\",\""+ formfield +"\");return false'>" + username + " - </a>",id:"user_receipient_res_"+ id}); //neues empfänger li
    			//targetDivElement = dojo.create("li",{innerHTML:"<a href='#' onclick='removeRecepient("+id+", \'"+username+"\', \""+ recepientDiv +"\",\""+ formfield +"\")'>"+username+"</a>"});
        		dojo.byId(targetId).appendChild(targetDivElement);
    			dojo.byId(formfield).value = id;
    		}
    		
    		
    	}
    	
    }
    
    function removeRecepient(id, username,recepientDiv, searchDiv, formfield){
    	
    	
    	
    	//dojo.byId(formfield).value += addToValue;
    	//Remove Div and put it again to the Recepient list
    	try{
    		dojo.byId(searchDiv+id).show();
    	}catch(error){
    		targetDivElement = dojo.create("li",{innerHTML:"<a href='#' onclick='addRecepient("+ id +",\""+username+"\",\"user_search_res_\",\"recepients_list\",\"recepients\");return false'>" + username + "</a>", id:"user_search_res_"+ id}); //neues empfänger li
    		dojo.byId('searchList').appendChild(targetDivElement);
    	}
    	
    	
    	dojo.byId(recepientDiv+id).remove();
    	// Remove ID from Form-Input-Field
    	
    	formFieldValue = dojo.byId(formfield).value;   		
    	
		if(formFieldValue.length>0){
			
			arr = formFieldValue.split(",");
			
    		position = '';
			if(arr.length>0){
				
	    		drin = false;
	    		for(i=0; i<arr.length; i++){
	    			if(arr[i] == id){
	    				//schon drin
	    				drin = true;
	    				position = i;
	    			}
	    		}
	    		if(drin){
	    			
	    		
	    			if(position == 0){
	    				// alert("position == 0");
	    				// cut from front
	    				if(arr.length > 1){
	    					arr=arr.slice(1, arr.length-1);
	    				}else{
	    					arr = "";
	    				}
	    			}else if(position == arr.length-1){
	    				//alert("position == arr.length");
	    				// cut from back
	    				arr.pop();
	    			}else{
	    				//alert("position == " + position);
	    				cuta1 = arr.slice(0,position);
		    			cuta2 = arr.slice(position+1,arr.length);
		    			//alert("Cuta1 = " + cuta1 + " -- Cuta2 = " + cuta2);
		    			for(i=0; i<cuta2.length; i++){
		    				cuta1.push(cuta2[i]);
		    			}
		    			arr = cuta1;
		    			//alert("Array = " + arr);
	    			}
	    		}	
	    		if(arr.length>0){
	    			dojo.byId(formfield).value = arr.join(",");//wert in Recepient-Feld schreiben (für POST)
	    		}else{
	    			dojo.byId(formfield).value = "";
	    		}
			}else{
				
				if(formFieldValue == id){
					dojo.byId(formfield).value = "";//wert in Recepient-Feld schreiben (für POST)
				}
			}
			
		}
	        		
    	
    }
    
    function updateJokerTipps(className, element){
    	$$('.'+className).each(function(elmt){
    		elmt.checked = false;
    	})
    	
    	element.checked = true;
    }
