function FormField()
{
	this.m_Name = "";
	this.m_Mandatory = "";
	this.m_ID = "";
	this.m_ErrorID = "";
	this.m_ErrorClass = "";
	this.m_ErrorMessageContainerID = "";
	this.m_LiveDataCheck = false;
	this.m_ProcessDependencies = false;
	this.m_FormType = "input";
}

FormField.prototype.SetName = function(value)
{
	this.m_Name = value;
	
	if(value == "land" || value == "titel" || value == "pers_frage")
		this.m_FormType = "select";
}

FormField.prototype.GetName = function()
{
	return this.m_Name;
}

FormField.prototype.SetValue = function(value)
{
	$("input[name=" + this.m_Name + "]").val(value);
}

FormField.prototype.GetValue = function()
{
	if(this.m_Name == "anrede")
	{
		if($("#anrede_herr").attr("checked"))
			return "Herr";
			
		if($("#anrede_frau").attr("checked"))
			return "Frau";
			
		return "";
	}
	
	if(this.m_Name == "agb")
	{
		if($("input[name=agb]").attr("checked"))
			return 1;
			
		return 0;
	}
	
	if(this.m_Name == "agb2")
	{
		if($("input[name=agb2]").attr("checked"))
			return 1;
			
		return 0;
	}
	
	return $(this.m_FormType + "[name=" + this.m_Name + "]").val();
}

FormField.prototype.SetMandatory = function(value)
{
	this.m_Mandatory = value;
}

FormField.prototype.GetMandatory = function()
{
	return this.m_Mandatory;
}

FormField.prototype.SetID = function(value)
{
	this.m_ID = value;
}

FormField.prototype.GetID = function()
{
	return this.m_ID;
}

FormField.prototype.SetErrorID = function(value)
{
	this.m_ErrorID = value;
}

FormField.prototype.GetErrorID = function()
{
	return this.m_ErrorID;
}

FormField.prototype.SetErrorClass = function(value)
{
	this.m_ErrorClass = value;
}

FormField.prototype.GetErrorClass = function()
{
	return this.m_ErrorClass;
}

FormField.prototype.SetErrorMessageContainerID = function(value)
{
	this.m_ErrorMessageContainerID = value;
}

FormField.prototype.GetErrorMessageContainerID = function()
{
	return this.m_ErrorMessageContainerID;
}

FormField.prototype.SetLiveDataCheck = function(value)
{
	this.m_LiveDataCheck = value;
	
	if(value)
	{
		//TODO: bind the function which performs the live check
	}
	else
	{
		//TODO: unbind the function which perofrms the live check
	}
}

FormField.prototype.GetLiveDataCheck = function()
{
	return this.m_LiveDataCheck;
}

FormField.prototype.DoLiveDataCheck = function()
{

}

FormField.prototype.SetProcessDependencies = function(value)
{
	this.m_ProcessDepedencies = value;
}

FormField.prototype.GetProcessDependencies = function()
{
	return this.m_ProcessDepedencies;
}

FormField.prototype.ProcessDependencies = function()
{
	switch(this.m_Name)
	{
	
		default:
			//Do nothing here
		break;
	}
}

FormField.prototype.SetFormType = function(value)
{
	this.m_FormType = value;
}

FormField.prototype.GetFormType = function()
{
	return this.m_FormType;
}




function FormSetup()
{
	this.m_FieldErrorSeparator = "{form_error_separator}";
	this.m_ErrorMessageSeparator = "{ems}";
	this.m_FieldSeperator = "|";

	this.m_SubmitURL = "formsubmit.php";
	this.m_Fields = new Array();
	this.m_SuccessCallback = null;
	this.m_ErrorCallback = null;
	this.m_SendActivationEmail = 0;
	
	this.m_ShowErrorMessageDialog = true;
	
	this.m_FieldsToIgnore = new Array
								(
									"gebtag", 
									"gebmonat"
								);
}

FormSetup.prototype.SetFieldErrorSeparator = function(value)
{
	this.m_FieldErrorSeparator = value;
}

FormSetup.prototype.GetFieldErrorSeparator = function()
{
	return this.m_FieldErrorSeparator;
}

FormSetup.prototype.SetErrorMessageSeparator = function(value)
{
	this.m_ErrorMessageSeparator = value;
}

FormSetup.prototype.GetErrorMessageSeparator = function()
{
	return this.m_ErrorMessageSeparator;
}

FormSetup.prototype.SetFieldSeparator = function(value)
{
	this.m_FieldSeperator = value;
}

FormSetup.prototype.GetFieldSeperator = function()
{
	return this.m_FieldSeperator;
}

FormSetup.prototype.SetSubmitURL = function(value)
{
	this.m_SubmitURL = value;
}

FormSetup.prototype.GetSubmitURL = function()
{
	return this.m_SubmitURL;
}

FormSetup.prototype.AddField = function(value)
{
	this.m_Fields.push(value);
}

FormSetup.prototype.GetFieldByName = function(value)
{
	for(var i = 0; i < this.m_Fields.length; i++)
	{
		if(this.m_Fields[i].GetName() == value)
		{
			return this.m_Fields[i];
		}
	}
	
	return null;
}

FormSetup.prototype.Submit = function()
{
	var data = "";
	var mandatory = "";
	
	for(var i = 0; i < this.m_Fields.length; i++)
	{
		var field = this.m_Fields[i];
		
		data += escape(field.GetName()) + "=" + escape(field.GetValue()) + "&";
		data += escape("mandatory_" + field.GetName()) + "=" + (field.GetMandatory() ? "1" : "0") + "&";
	}
	
	data += "interface_send_activation_mail=" + this.m_SendActivationEmail;
	
	var url = this.m_SubmitURL;
	var errorSeparator = this.m_FieldErrorSeparator;
	var errorMessageSeparator = this.m_ErrorMessageSeparator;
	var fieldSeparator = this.m_FieldSeperator;
	var this_obj = this;
	
	$.ajax
	(
		{
			url: url,
			type: "POST",
			data: data,
			success: function(msg)
			{
				//unmark all errornous fields
				for(var i = 0; i < this_obj.m_Fields.length; i++)
				{
					var field = this_obj.m_Fields[i];
					$("#" + field.GetErrorID()).removeClass(field.GetErrorClass());
					
					if(field.GetErrorMessageContainerID())
						$("#" + field.GetErrorMessageContainerID()).html("");
				}
			
				if(msg)
				{
					var split = msg.split(errorSeparator);
					
					var strFieldErrors = split[0];
					var strErrorMessage = split[1];
					var errorMessagesArr = split[2].split(errorMessageSeparator);
					
					
					var errorFieldNames = strFieldErrors.split(fieldSeparator);
					var errorMessageIndex = 0;
					
					//Blacklist Message here
					//------------------------------------------
					if(errorFieldNames.length >= 1)
					{
						if(errorFieldNames[0] == "blacklist_error")
						{
							var blacklistErrorHtml = '<div style="width:500px;padding:10px;text-align:center;">';
							blacklistErrorHtml += '<span style="color:red">';
							blacklistErrorHtml += '<b>Sie sind nicht berechtigt teilzunehmen</b>';
							blacklistErrorHtml += '</span>';
							blacklistErrorHtml += '<div style="width:100%;text-align:center;padding-top:20px;">';
							blacklistErrorHtml += '<input type="button" id="error_ok" value="Ok" style="width:100px;" onclick="jQuery.facebox.close()" />';
							blacklistErrorHtml += '</div>';
							blacklistErrorHtml += '</div>';
							
							jQuery.facebox(blacklistErrorHtml);
							
							return;
						}
					}
					//------------------------------------------
					
					for(var i = 0; i < errorFieldNames.length; i++)
					{
						var field = this_obj.GetFieldByName(errorFieldNames[i]);
						if(field)
						{
							if(field.GetErrorID())
								$("#" + field.GetErrorID()).addClass(field.GetErrorClass());
							
							if(field.GetErrorMessageContainerID())
								$("#" + field.GetErrorMessageContainerID()).html(errorMessagesArr[errorMessageIndex]);
								
							//Only increase counter on none grouped fields TODO: add an Array containing the grouped fields
							if
							(
								field.GetName() != "gebmonat" 
								&& field.GetName() != "gebjahr"
							)
							{
								errorMessageIndex++;
							}
						}
					}
					
					if(this_obj.GetShowErrorMessageDialog())
						jQuery.facebox(strErrorMessage);
						
					if(this_obj.m_ErrorCallback)
						this_obj.m_ErrorCallback(strFieldErrors);
						
					return;
				}
				
				if(this_obj.m_SuccessCallback)
				{
					this_obj.m_SuccessCallback();
				}
			}
		}
	);
}

FormSetup.prototype.SetSuccessCallback = function(value)
{
	this.m_SuccessCallback = value;
}

FormSetup.prototype.SetErrorCallback = function(value)
{
	this.m_ErrorCallback = value;
}

FormSetup.prototype.SetSendActivationEmail = function(value)
{
	this.m_SendActivationEmail = value;
}

FormSetup.prototype.SetShowErrorMessageDialog = function(value)
{
	this.m_ShowErrorMessageDialog = value;
}

FormSetup.prototype.GetShowErrorMessageDialog = function()
{
	return this.m_ShowErrorMessageDialog;
}

function TefMail()
{
	this.m_Friends = "";
	this.m_Url = "tef.php";
}

TefMail.prototype.SetFriends = function(value)
{
	this.m_Friends = value;
}

TefMail.prototype.GetFriends = function()
{
	return this.m_Friends;
}

TefMail.prototype.SetUrl = function(value)
{
	this.m_Url = value;
}

TefMail.prototype.GetUrl = function()
{
	return this.m_Url;
}

TefMail.prototype.Submit = function()
{
	var url = this.m_Url;
	var data = "friends=" + this.m_Friends;
	
	$.ajax
	(
		{
			url: url,
			type: "POST",
			data: data,
			success: function(msg)
			{
				jQuery.facebox(msg);
			}
		}
	);
}