var actChoice = "invitationForm";
var destination = "contentContainer";

var warningColor = "#f58320";
var regularColor = "#ffffff";

var agbClicked = false;

//menu
 function request() 
 { 	
      xhr = http();
	  var suffix = ".php";
	  
      xhr.open("GET", actChoice+suffix, true);
      xhr.onreadystatechange = contentOutput;
      xhr.send(null);
 };

function contentOutput() 
{
   if (xhr.readyState == 4) 
   {
     var rueckgabe = xhr.responseText;
     document.getElementById(destination).innerHTML = rueckgabe;
	 
	 //for ajax browser forward / backward, writes actchoice to history
	 window.location.hash = actChoice;	
	}
}



function checkMail()
{
	var ok = true;
	var agbOK = true;
	
	if ($('firstName').value == "")
	{
		ok = false;
		$('firstName').setStyle('background-color', warningColor);
	}
	else
	{
		$('firstName').setStyle('background-color', regularColor);
	}
	
	if ($('lastName').value == "")
	{
		ok = false;
		$('lastName').setStyle('background-color', warningColor);
	}
	else
	{
		$('lastName').setStyle('background-color', regularColor);
	}
	
	if ($('address').value == "")
	{
		ok = false;
		$('address').setStyle('background-color', warningColor);
	}
	else
	{
		$('address').setStyle('background-color', regularColor);
	}
	
	if ($('city').value == "")
	{
		ok = false;
		$('city').setStyle('background-color', warningColor);
	}
	else
	{
		$('city').setStyle('background-color', regularColor);
	}
	
	if ($('zip').value == "")
	{
		ok = false;
		$('zip').setStyle('background-color', warningColor);
	}
	else
	{
		$('zip').setStyle('background-color', regularColor);
	}
	
	/*if ($('fon').value == "")
	{
		ok = false;
		$('fon').setStyle('background-color', warningColor);
	}
	else
	{
		$('fon').setStyle('background-color', regularColor);
	}*/
	
	if ($('email').value == "" || $('email').value.indexOf("@") == -1 || $('email').value.indexOf(".") == -1)
	{
		ok = false;
		$('email').setStyle('background-color', warningColor);
	}
	else
	{
		$('email').setStyle('background-color', regularColor);
	}
	
	//check if ticket is available, because the same routine is used for the traders reservation
	if ($('ticket') != null)
	{
		if ($('ticket').value == "")
		{
			ok = false;
			$('ticket').setStyle('background-color', warningColor);
		}
		else
		{
			$('ticket').setStyle('background-color', regularColor);
		}
	}
	
	//for the trader stand
	if ($('stand') != null)
	{
		if ($('stand').value == "")
		{
			ok = false;
			$('stand').setStyle('background-color', warningColor);
		}
		else
		{
			$('stand').setStyle('background-color', regularColor);
		}
	}
		
	if ($('agbCB').checked == false)
	{
		agbOK = false;
		$('warning').set('html', 'Sie müssen die Nutzungsbedingungen akzeptieren. <br /> You must accept the terms and conditions.');
	}
	else
	{
		$('warning').set('text', '');
	}
	
	if (ok)
	{
		if (agbOK) 
		{
			$('warning').set('text', '');
			
			if($('category').value == "cosplay")
			{
				//for cosplay open a different mailform
				mailRequest('mailformCosplay.php');
			}
			else
			{
				mailRequest('mailform.php');
			}
		}
	}
	else
	{
		$('warning').set('html', 'Bitte die orange markierten Felder ausfüllen. <br /> Please fill out the orange marked fields.');	
	}
}

function mailRequest(form)
{
	xhr = http();
	xhr.open('POST', form, true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.onreadystatechange = mailOutput;
	
	var category = $('category').value;
	var firstName = $('firstName').value;
	var lastName = $('lastName').value;
	var address = $('address').value;
	var city = $('city').value;
	var zip = $('zip').value;
	var fon = $('fon').value;
	var email = $('email').value;
	var ticket = ($('ticket') != null) ? $('ticket').value : "";
	var starguest = ($('starguest') != null) ? $('starguest').value : "";
	var starDate = ($('date') != null) ? $('date').value : "";
	var delivery = "";
	var stand = ($('stand') != null) ? $('stand').value : "";
	
	//eintrittskarten vorbestellung
	var typeofticket = ($('typeofticket') != null) ? $('typeofticket').value : "";
	
	//if radiobuttons for delivery are used fill the delivery var 
	if ($('radioButtons') != null)
	{
		var temp = $('radioButtons').getElements('input[name=delivery]');
		for (i = 0; i< temp.length; i++)
		{
			if(temp[i].checked)
			{
				delivery = temp[i].value;
			}
		}
	}
	xhr.send('category='+category+'&firstName='+firstName+'&lastName='+lastName+'&address='+address+'&city='+city+'&zip='+zip+'&email='+email+'&fon='+fon+'&ticket='+ticket+'&stand='+stand+'&starguest='+starguest+'&stardate='+starDate+'&delivery='+delivery+'&typeofticket='+typeofticket); 
}

function mailOutput() 
{
   if (xhr.readyState == 4) 
   {
     var rueckgabe = xhr.responseText;
     document.getElementById("warning").innerHTML = rueckgabe;
	 resetFields();
   }
}

//felder im kontaktformular leeren
function resetFields()
{
	$('firstName').set('value','');
	$('lastName').set('value','');
	$('email').set('value','');
	$('city').set('value','');
	$('fon').set('value','');
	$('address').set('value','');
	$('zip').set('value','');
	$('agbCB').checked = false;
	
	if($('stand') != null)
	{
		$('stand').set('value','');
	}
	
	if($('ticket') != null)
	{
		$('ticket').set('value','');
	}
}

function menClick(id)
{
	actChoice = id;
	
	//$(actChoice).setProperty('id', 'current');
	request();
}

//newsletter
function subscribe()
{
	if ($('newsletter').value != "" || $('newsletter').value.indexOf("@") != -1 || $('newsletter').value.indexOf(".") != -1)
	{
		newsletterRequest("anmelden");
	}
}

function unsubscribe()
{
	if ($('newsletter').value != "" || $('newsletter').value.indexOf("@") != -1 || $('newsletter').value.indexOf(".") != -1)
	{
		newsletterRequest("abmelden");
	}
}

function newsletterRequest(type)
{
	xhr = http();
	xhr.open('POST', 'newsletter.php', true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.onreadystatechange = newsletterOutput;
	
	var email = $('newsletter').value;
	
	xhr.send('type='+type+'&email='+email); 
}

function newsletterOutput() 
{
   if (xhr.readyState == 4) 
   {
     $('newsletter').value = "";
	}
}

//newsletter end

//for ajax browser prev and next button --> works not in IE
var recentHash = "";
   
   function pollHash() {
  
     if (window.location.hash==recentHash) 
	 {
       return; // Nothing's changed since last polled.
     }
     recentHash = window.location.hash;
 		
		menClick(recentHash.substr(1));
     
	 
  
   }
   
 //checkFields for the press application
function checkFields()
{
	var ok = true;
	
	if ($('redaktion').value == "")
	{
		ok = false;
		$('redaktion').setStyle('background-color', warningColor);
	}
	else
	{
		$('redaktion').setStyle('background-color', regularColor);
	}
	
	if ($('ansprechpartner').value == "")
	{
		ok = false;
		$('ansprechpartner').setStyle('background-color', warningColor);
	}
	else
	{
		$('ansprechpartner').setStyle('background-color', regularColor);
	}
	
	if ($('fon').value == "")
	{
		ok = false;
		$('fon').setStyle('background-color', warningColor);
	}
	else
	{
		$('fon').setStyle('background-color', regularColor);
	}
	
	if ($('email').value == "" || $('email').value.indexOf("@") == -1 || $('email').value.indexOf(".") == -1)
	{
		ok = false;
		$('email').setStyle('background-color', warningColor);
	}
	else
	{
		$('email').setStyle('background-color', regularColor);
	}
	
	if (ok)
	{
		$('warning').set('text', '');
		
		pressMailRequest('pressMailform.php');
		
	}
	else
	{
		$('warning').set('html', 'Bitte die orange markierten Felder ausf&uuml;llen. <br /> Please fill out the orange marked fields.');	
	}
}

function pressMailRequest(form)
{
	xhr = http();
	xhr.open('POST', form, true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.onreadystatechange = pressMailOutput;
	
	var redaktion = $('redaktion').value;
	var ansprechpartner = $('ansprechpartner').value;
	var fon = $('fon').value;
	var fax = $('fax').value;
	var email = $('email').value;
	var bemerkung = $('bemerkung').value;
	
	xhr.send('redaktion='+redaktion+'&ansprechpartner='+ansprechpartner+'&fon='+fon+'&fax='+fax+'&email='+email+'&bemerkung='+bemerkung); 
}

function pressMailOutput() 
{
   if (xhr.readyState == 4) 
   {
     var rueckgabe = xhr.responseText;
     document.getElementById("warning").innerHTML = rueckgabe;
	 resetPressFields();
   }
}

//felder im presseformular leeren
function resetPressFields()
{
	$('redaktion').set('value','');
	$('ansprechpartner').set('value','');
	$('email').set('value','');
	$('fon').set('value','');
	$('fax').set('value','');
	$('bemerkung').set('value','');
	
}

