 /**
  * JavaScript for CallBackFormular on www.carpe-diem-events.com
  *
  * @author Steffen Friedrich (steffen.friedrich@pingping.ag, www.pingping.ag)
  ***/

function CallbackForm()
{	
	this.link = document.getElementById('callback');
	this.link = this.link.getElementsByTagName('a');
	this.link = this.link[0];
	
	this.openImage = this.link.getElementsByTagName('img')[0];
	
	this.formularSeite = this.link.href;
	this.formularSeite = this.formularSeite.replace(/0\.html/,'1.html');
	
	this.link.href = '#';
	this.link.onclick = function()
	{
		return false;
	}
	
	this.openImage.empfaenger = this;
	this.openImage.onclick = function() 
	{	
		this.empfaenger.open();
	}

	this.open = function()
	{	// Öffnet CallBack-Formular und blendet Fenster aus
		this.screen();
	}

	this.screen = function()
	{
		if(!this.screendiv)
		{
			this.screendiv = document.createElement('div');
			this.screendiv.className = 'screen';
			var body = document.getElementsByTagName('body')[0];
			body.insertBefore(this.screendiv,body.firstChild);
			
			this.closeImage = document.createElement('img');
			
			this.closeImage.style.position = 'absolute';
			this.closeImage.style.zIndex = '2020';
			this.closeImage.style.top = '-82px';
			this.closeImage.style.left = '0';
			this.closeImage.style.cursor = 'pointer';
			
			this.closeImage.src = this.openImage.src;
			this.link.insertBefore(this.closeImage,this.openImage);
			
			this.closeImage.empfaenger = this;
			this.closeImage.onclick = function()
			{
				this.empfaenger.unscreen();
			}
			
			this.ajax = new AJAX();
			this.ajaxsurround = document.createElement('div');
			this.ajaxsurround.className ='ajaxsurroundcontainer';
			
			this.ajaxdiv = document.createElement('div');
			this.ajaxdiv.className = 'ajaxcontainer';
			
			this.ajaxdiv.empfaenger = this;
			this.ajaxdiv.receive = function(pack)
			{	
				this.innerHTML = pack.request.responseText;
				this.empfaenger.manipulateForm();
			}
			
			body.insertBefore(this.ajaxsurround,body.firstChild);
			this.ajaxsurround.appendChild(this.ajaxdiv);
		
		}
		else 
		{
			this.screendiv.style.display = 'block';
			this.closeImage.style.display = 'block';
			this.ajaxsurround.style.display = 'block';
		}
		
		this.ajax.request(this.ajaxdiv,this.formularSeite);
	}
	
	this.unscreen = function()
	{
		this.screendiv.style.display = 'none';
		this.closeImage.style.display = 'none';
		this.ajaxsurround.style.display = 'none';
	}
	
	this.manipulateForm = function()
	{
		this.closeButton = document.createElement('button');
		this.closeButton.className = 'cancel';
		this['closeButton'].innerHTML = 'Schließen';
		
		this.closeButton.empfaenger = this;
		
		this.closeButton.onclick = function()
		{
			this.empfaenger.unscreen();
			return false;
		}
		
		var sendButton = this.ajaxdiv.getElementsByTagName('button')[0];
		
		if(sendButton)
			sendButton.parentNode.insertBefore(this.closeButton,this.sendButton);
		else this.ajaxdiv.appendChild(this.closeButton);	
		
		var formular = this.ajaxdiv.getElementsByTagName('form')[0];
		formular.empfaenger = this;
		formular.onsubmit = function()
		{
			this.empfaenger.senden();
			return false;
		}
	}
	
	this.getDataByTagName = function(tagN, data)
	{
		var tags = this.ajaxdiv.getElementsByTagName(tagN);
		
		for(var i = 0; i < tags.length; i++)
		{
			data[ tags[i].name ] = tags[i].value;
		}
		
	}
	
	this.senden = function()
	{
		var data = new Object();

		this.getDataByTagName('input', data);
		this.getDataByTagName('select', data);
		this.getDataByTagName('textarea', data);
		
		this.ajax.send(this.ajaxdiv,this.formularSeite,data);
	}
	
}
