//customizingbereich - einstellbare werte
var inputfeld="fullinput";
//bei den farben leerzeichen nachm komma!
var colordefault=""; //standardfarbe wenn feld aktivierbar
var coloractive="rgb(0, 255, 255)"; //farbe für aktivierte felder
var colorsoon="rgb(255, 255, 102)"; //farbe fuer zufuegestart
var colordel="rgb(255, 102, 102)"; //farbe fuer loeschstart

function RGBtoHex(R,G,B)
	{return toHex(R)+toHex(G)+toHex(B)}
function toHex(N)
	{
	if (N==null) return "00";
	N=parseInt(N); if (N==0 || isNaN(N)) return "00";
	N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
	return "0123456789abcdef".charAt((N-N%16)/16)
	     + "0123456789abcdef".charAt(N%16);
	}

// !!!!!!!!! do not edit below this line !!!!!!!  nichts unterhalb veraenfdern !!!!!!!!!
var anf="";
var end="";
var anftemp="";
var endtemp="";
var modus="zuf"; //zuf weg
var modustemp="";

var colordefaults=colordefault.split(" ").join("");
var coloractives= coloractive.split(" ").join("");
var colorsoons=   colorsoon.split(" ").join("");
var colordels=    colordel.split(" ").join("");

try {var colteiledefault=colordefault.split("(")[1].split(")")[0].split(" ").join("").split(",");} catch(err) {var colteiledefault=Array("","","");}
try {var colteileactive= coloractive.split("(")[1].split(")")[0].split(" ").join("").split(","); } catch(err) {var colteileactive =Array("","","");}
try {var colteilesoon=   colorsoon.split("(")[1].split(")")[0].split(" ").join("").split(",");   } catch(err) {var colteilesoon   =Array("","","");}
try {var colteiledel=    colordel.split("(")[1].split(")")[0].split(" ").join("").split(",");    } catch(err) {var colteiledel    =Array("","","");}

var colordefaulto='#'+RGBtoHex(colteiledefault[0],colteiledefault[1],colteiledefault[2]);
var coloractiveo= '#'+RGBtoHex(colteileactive[0] ,colteileactive[1] ,colteileactive[2]);
var colorsoono=   '#'+RGBtoHex(colteilesoon[0]   ,colteilesoon[1]   ,colteilesoon[2]);
var colordelo=    '#'+RGBtoHex(colteiledel[0]    ,colteiledel[1]    ,colteiledel[2]);

var statetemp=Array();
var statetempdo=false;

function addEvent(element, handler, callback, capture){
	
	try {
		element[handler + callback] = callback;
		element.addEventListener(handler, element[handler + callback], capture);
	}catch(e){
		
		element[handler + callback] = function()
		{
			callback.call(element);
		}
		element.attachEvent('on' + handler, element[handler + callback]);
	}
}

function removeEvent(element, handler, callback, capture){
	
	try{
		element.removeEventListener(handler, element[handler + callback], capture);
	}catch(e){
		if(element.detachEvent)
			element.detachEvent('on' + handler, element[handler + callback]);
	}
	element[handler + callback] = null;
}
/*
function getElementsByClassName(class_name)
{
  var all_obj,ret_obj=new Array(),j=0,teststr;

  if(document.all)all_obj=document.all;
  else if(document.getElementsByTagName && !document.all)
    all_obj=document.getElementsByTagName("*");

  for(i=0;i<all_obj.length;i++)
  {
    if(all_obj[i].className.indexOf(class_name)!=-1)
    {
      teststr=","+all_obj[i].className.split(" ").join(",")+",";
      if(teststr.indexOf(","+class_name+",")!=-1)
      {
        ret_obj[j]=all_obj[i];
        j++;
      }
    }
  }
  return ret_obj;
}
*/
/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

function exportfieldstoinput(inputfeld){
	
	document.getElementById(inputfeld).value='';
	var obj = getElementsByClassName("calendarField");

	var xcv = "";
	
	var xc = "";

	for(i = 0; i < obj.length; i++){
		
		// *** Farben in das richtige Format, da manche Browser RGB andere Hexadezimal zurückgeben ***
		if(coloractive && document.getElementById("getColor")){
			
			document.getElementById("getColor").style.backgroundColor = coloractive;
			coloractive = document.getElementById("getColor").style.backgroundColor;
			document.getElementById("getColor").style.display = "none";
			
		} else if(coloractives && document.getElementById("getColor")){
			
			document.getElementById("getColor").style.backgroundColor = coloractives;
			coloractives = document.getElementById("getColor").style.backgroundColor;
			document.getElementById("getColor").style.display = "none";
		}
		// *** ENDE: Farben in das richtige Format, da manche Browser RGB andere Hexadezimal zurückgeben ***
			
		if ((obj[i].style.backgroundColor == coloractive) || (obj[i].style.backgroundColor == coloractives)){
			
			var tempvallu=obj[i].id.split("-");
			
			document.getElementById(inputfeld).value += String(tempvallu[1]) + '-' + String(tempvallu[2]) +'-'+ String(tempvallu[3]) +';'; //0 waere foobar
		}
	}
}

function datetozahl(date)
{
  var ztd=date.split("-");
  var zneuobid=ztd[3] + ztd[2] + ztd[1];
  return parseInt(zneuobid);
}
  
function mymouseup()
  {
  modus="zuf";
  anf="";
  statetempdo=false;
  anftemp="";
  var obj=getElementsByClassName("calendarField");
  for(i=0;i<obj.length;i++)
    {
	if ((obj[i].style.backgroundColor==colordel) || (obj[i].style.backgroundColor==colordels))
	  {
	  obj[i].style.backgroundColor=coloractive;
	  }
	if ((obj[i].style.backgroundColor==colorsoon) || (obj[i].style.backgroundColor==colorsoons))
	  {
	  obj[i].style.backgroundColor=colordefault;
	  }	
	/*
	if (!((obj[i].style.backgroundColor==coloractive) || (obj[i].style.backgroundColor==coloractives)) )
	  {
	  if (!((obj[i].style.backgroundColor==colordel) || (obj[i].style.backgroundColor==colordels)))
	    {
	    obj[i].style.backgroundColor=colordefault;
		}
	  else
	    {
		obj[i].style.backgroundColor=coloractive;
		}
	  }*/
    }
  setTimeout("exportfieldstoinput('"+inputfeld+"')",50);	
  }
  
function dateover(date){
	  

	if ((anf) && (!statetempdo)){
		
		var obj=getElementsByClassName("calendarField");
		statetempdo=true;
		for(i=0;i<obj.length;i++){
			
			statetemp[i]=obj[i].style.backgroundColor;
		}
		anftemp=anf;
		endtemp=end;
		modustemp=modus;
	}
	if ((anftemp) && (statetempdo)){
	  
		anf=anftemp;
		end=endtemp;
		modus=modustemp;
		var obj=getElementsByClassName("calendarField");
		for(i=0;i<obj.length;i++){
			
			obj[i].style.backgroundColor=statetemp[i];
		}
		sendDate(date,"");
	}
}
  
function dateup(date){
	
	sendDate(date, "");
	setTimeout("exportfieldstoinput('"+inputfeld+"')",10);
}
  
function datedown(date){
	 
	if (!coloractive){
		  
		try{
			alert(noperswarn);
		}
		catch(err){
			
			alert('Please choose color/persons!');
		}
	}else{
    	sendDate(date, "");
	}
}

function sendDate(date, donkno){
	
  var meme=document.getElementById(date).style.backgroundColor;

	if (!anf){
	  
		anf=date;
		//aktuelles makieren
		if ((meme==colordefault) || (meme==colordefaults) || (!meme) ){
			
			document.getElementById(date).style.backgroundColor=colorsoon;
			modus="zuf";
		  
		}else{
			
			document.getElementById(date).style.backgroundColor=colordel;
			modus="weg";
		}
	}else{
		
		end=date;
		var obj=getElementsByClassName("calendarField");
		for(i=0;i<obj.length;i++){
			
			if (datetozahl(anf) < datetozahl(end)){
				
				if ((datetozahl(obj[i].id) >= datetozahl(anf)) && (datetozahl(obj[i].id) <= datetozahl(end))){
					
		  			if (modus=="weg"){
						obj[i].style.backgroundColor=colordefault;
					}
		  		else
		    	{
					obj[i].style.backgroundColor=coloractive;
				}
		  	}
		}
	  else //falschrum makiert
	    {
		if ((datetozahl(obj[i].id) <= datetozahl(anf)) && (datetozahl(obj[i].id) >= datetozahl(end)))
	      {
	      if (modus=="weg") 
		    {obj[i].style.backgroundColor=colordefault;
			}
		  else
		    {obj[i].style.backgroundColor=coloractive;
			}
		  }
		}
	  }
	anf="";
	}
  }

addEvent(document.body,"mouseup", mymouseup, false);
function disableFF3shit()
	{
	var obj=getElementsByClassName("calendarField");
	for(i=0;i<obj.length;i++)
		{
		//addEvent(obj[i],"mousedown", myantidragdrop, false);
		/*obj[i].onmousedown=function(event){
			if(event.preventDefault)
				{
				event.preventDefault();
				}
			}
			*/
		}
	}

function myantidragdrop(event)
	{
	if(event.preventDefault)
		{
		event.preventDefault();
		}
	}
	
setTimeout("disableFF3shit();",1000);	
	
