//#########################################
//####### Ajax Functions ##################
//#########################################

function ToggleGuests() {
	var TheForm = $('TheForm');
	if (TheForm.YouHaveGuest[0].checked){
		if(document.getElementById('Guests')) document.getElementById('Guests').style.display = "";
	} else{
		if(document.getElementById('Guests')) document.getElementById('Guests').style.display = "none";
	}
}


function showTickets(originalRequest){
	if ($('remain_tickets')) $('remain_tickets').innerHTML = originalRequest.responseText;
}

function CheckTickets_ajax(){
     var url = 'PHPfunctions.php';
   	 var pars = 'ToDo=CheckTickets';
     var myAjax = new Ajax.Request(
					url, 
					{
						method: 'post', 
						parameters: pars, 
						onComplete: showTickets
					});
}

function showReciept(originalRequest){
	if ($('noTickets')) $('noTickets').innerHTML = "";
	switch (originalRequest.responseText){
		case "noTickets":
			if ($('noTickets')) $('noTickets').innerHTML = "<h3><font color=\"red\">Sorry, There are not enough remaining tickets to process this request.</font></h3>";
			CheckTickets_ajax();
			break;
		default:
			if ($('EventTexasHoldem')) $('EventTexasHoldem').innerHTML = originalRequest.responseText;
			CheckTickets_ajax();
			break;
	}
}

function Insert_ajax(){
	 var formvals = Form.serialize($('TheForm'));
     var url = 'PHPfunctions.php';
     var pars = 'ToDo=InsertUser&' + formvals;
     var myAjax = new Ajax.Request(
					url, 
					{
						method: 'post', 
						parameters: pars, 
						onComplete: showReciept
					});
}	

function showResponse(originalRequest){
	if ($('Email_Match_error')) $('Email_Match_error').innerHTML = "";
	if ($('Email_error')) $('Email_error').innerHTML = "";
	if ($('Guest1Email_error')) $('Guest1Email_error').innerHTML = "";
	if ($('Guest2Email_error')) $('Guest2Email_error').innerHTML = "";
	if ($('Guest3Email_error')) $('Guest3Email_error').innerHTML = "";
	if ($('Guest4Email_error')) $('Guest4Email_error').innerHTML = "";
	//alert(originalRequest.responseText);
	
	var testarray = originalRequest.responseText.split("|");
	var j = testarray.length;
	var i = 0;
	
	for(i=0;i<j;i++){
		switch (testarray[i]){
			case "Matching":
				if ($('Email_Match_error')) $('Email_Match_error').innerHTML = "<span class=\"validation-advice\">Sorry email addresses cannot match.</span>";
				break;
			case "Taken1":
				if ($('Email_error')) $('Email_error').innerHTML = "<span class=\"validation-advice\">Sorry But this email has already been used.</span>";
				break;
			case "Taken2":
				if ($('Guest1Email_error')) $('Guest1Email_error').innerHTML = "<span class=\"validation-advice\">Sorry But this email has already been used.</span>";
				break;
			case "Taken3":
				if ($('Guest2Email_error')) $('Guest2Email_error').innerHTML = "<span class=\"validation-advice\">Sorry But this email has already been used.</span>";
				break;
			case "Taken4":
				if ($('Guest3Email_error')) $('Guest3Email_error').innerHTML = "<span class=\"validation-advice\">Sorry But this email has already been used.</span>";
				break;
			case "Taken5":
				if ($('Guest4Email_error')) $('Guest4Email_error').innerHTML = "<span class=\"validation-advice\">Sorry But this email has already been used.</span>";
				break;
			case "Valid":
				Insert_ajax();
				break;
			case "":
				break;
			default:
				break;
		}
	}		
}

function CheckEmail_ajax(TheForm){
	 var formvals = Form.serialize(TheForm);
     var url = 'PHPfunctions.php';
     var pars = 'ToDo=CheckEm&' + formvals;
     var myAjax = new Ajax.Request(
					url, 
					{
						method: 'post', 
						parameters: pars, 
						onComplete: showResponse
					});
}