//currently  it is not clear why we need this workaround
//originally there was a simple form which pushed the 
// parameters to some php file (index.php)
// however this did not work (because the calling form
// was built by java script? )
// this is why we end up having this cookie workaround
// which surpringly works. PHP can read the cookie.
function sender(idName)
{
	var replyTo=document.getElementById("replyToId").value;
	var message=document.getElementById("messageId").value.split('\n').join(" // ");
	//var message=document.getElementById("messageId").value;
	//alert(message);
	
	writeCookie("replyTo","");
	writeCookie("message","");
	//let the cookie expire in 5 seconds. this will be enough to survive the refresh
	// and short enough to not interfere with a next mail
	var expiration =new Date();
  var exprirationValue=new Date(expiration.getTime() + 5000).toGMTString();
	
	//document.cookie = "replyTo="+replyTo+"; expires="+exprirationValue+"; path=/";
	//document.cookie = "message="+message+"; expires="+exprirationValue+"; path=/";
	writeCookie("replyTo",replyTo,5);
	writeCookie("message",message,5);
	window.location.href="index.php?contact="+idName;
}

function writeCookie(name, value, secsUntilExpiration)
{
	var expirationValue="";
	if (secsUntilExpiration)
	{
		var expiration =new Date();
		expirationValue=new Date(expiration.getTime() + (secsUntilExpiration * 1000)).toGMTString();	
		expirationValue = " expires="+expirationValue+";";
	}
	document.cookie = name+"="+value+";"+expirationValue+" path=/";
	
}

//from head first
// cookies is a semi-colon separated list of key=value pairs
// the code expects those cookies to be served as an array containing
// each key-value pair in its owen item.  
// it iterates this list by producing some old-fashioned trim() function
// and then returning the value of the key whitch matches the provided name
function readCookie(name, cookies)
{
	var searchName=name+"=";
	
	for (var i=0; i< cookies.length;i++)
	{
		var c = cookies[i];
		
		while(c.charAt(0) == ' ')
			c=c.substring(1, c.length);
		
		if (c.indexOf(searchName) == 0)
			return c.substring(searchName.length, c.length);
	}
	return null;
}

// replace the [contact] button with the mail form
function contactMe(idName)
{
	var parentNode=document.getElementById("contactSpace"); 
	if (parentNode)
	{
		while (parentNode.firstChild)
		{
			parentNode.removeChild(parentNode.firstChild);
		}
	}
	
	parentNode=document.getElementById("contactButtonId"); 
	if (parentNode)
	{
		while (parentNode.firstChild)
		{
			parentNode.removeChild(parentNode.firstChild);
		}
	}	
	//add new things
	
	spanner=document.createElement("p");
	spanner.appendChild(document.createTextNode("Meldung "));
	spanner.appendChild(document.createElement("br"));
	tempItem=document.createElement("textarea");
	tempItem.setAttribute("rows","5");
	tempItem.setAttribute("cols","50");
	tempItem.setAttribute("class","themaItem");
	tempItem.setAttribute("id","messageId");
	spanner.appendChild(tempItem);
	parentNode.appendChild(spanner);
	
	spanner=document.createElement("p");
	spanner.appendChild(document.createTextNode("Antwort an "));
	spanner.appendChild(document.createElement("br"));
	tempItem=document.createElement("input");
	tempItem.setAttribute("size","50");
	tempItem.setAttribute("class","themaItem");
	tempItem.setAttribute("name", "replyTo");
	tempItem.setAttribute("id","replyToId");
	spanner.appendChild(tempItem);
	parentNode.appendChild(spanner);
	
	button=document.createElement("input");
	button.setAttribute("type","button");
	button.setAttribute("onclick","sender(\""+idName+"\");");
	button.setAttribute("value","Abschicken");
	parentNode.appendChild(button);
}

// injects a button which can be used to send an emali
// we do it this way, because to generated a button using JavaScript
// makes sure that JavaScript is enabled in the browser. (which is a prerequisite
// for the entire mailng stuff)
function injectContact(idName)
{
	var parentNode=document.getElementById("contactSpace"); 
	if(parentNode)
	{
		while (parentNode.firstChild)
		{
			parentNode.removeChild(parentNode.firstChild);
		}

		button=document.createElement("input");
		button.setAttribute("type","button");
		button.setAttribute("onclick","contactMe(\""+idName+"\");");

		button.setAttribute("value","Kontakt");
		parentNode.appendChild(button);
	}
}

