var runningAjax = false;

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" || value == "berufsstatus" || value == "einkommen" || value == "s_gebtag" || value == "s_gebmonat" || value == "s_gebjahr")
		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";
			
		if($("#" + this.m_Name).val() == "Herr")
			return "Herr";
		
		if($("#" + this.m_Name).val() == "Frau")
			return "Frau";
			
		if($("#" + this.m_Name).val() == "M.")
			return "M.";
		
		if($("#" + this.m_Name).val() == "Mme.")
			return "Mme.";
			
		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;
	}
	
	if(this.m_Name == "messestand_groesse")
	{
		if($("input[name='messestand_groesse']:checked").val() == "unter_20")
			return $("input[name='messestand_groesse']:checked").val();
		else if($("input[name='messestand_groesse']:checked").val() == "20_bis_50")
			return $("input[name='messestand_groesse']:checked").val();
		else if($("input[name='messestand_groesse']:checked").val() == "ueber_50")
			return $("input[name='messestand_groesse']:checked").val();
		else
			return 0;
	}
	
	if(this.m_Name == "erwerbsstatus")
	{
		if($("input[name='erwerbsstatus']:checked").val() == "selbststaendig")
			return $("input[name='erwerbsstatus']:checked").val();
		else if($("input[name='erwerbsstatus']:checked").val() == "angestellt")
			return $("input[name='erwerbsstatus']:checked").val();
		else
			return 0;
	}
	
	if(this.m_Name == "versicherung")
	{
		if($("input[name='versicherung']:checked").val() == "privat")
			return $("input[name='versicherung']:checked").val();
		else if($("input[name='versicherung']:checked").val() == "gesetzlich")
			return $("input[name='versicherung']:checked").val();
		else
			return 0;
	}
	
	if(this.m_Name == "erreichbarkeit")
	{
		if($("input[name='erreichbarkeit']:checked").val() == "tagsueber")
			return $("input[name='erreichbarkeit']:checked").val();
		else if($("input[name='erreichbarkeit']:checked").val() == "ab_18_uhr")
			return $("input[name='erreichbarkeit']:checked").val();
		else if($("input[name='erreichbarkeit']:checked").val() == "ab_20_uhr")
			return $("input[name='erreichbarkeit']:checked").val();
		else
			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;
	
	if(!runningAjax)
	{
		runningAjax = true;
		
		$.ajax
		(
			{
				url: url,
				type: "POST",
				data: data,
				success: function(msg)
				{
					runningAjax = false;
					//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>Entschuldigung, es ist ein Fehler passiert. Bitte &uuml;berpr&uuml;fen Sie Ihre Angaben!</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;
							}
							
							if(errorFieldNames[0] == "facebox_captcha")
							{							
								jQuery.facebox(strErrorMessage);
								
								return;
							}
							
							if(errorFieldNames[0] == "email_domain")
							{							
								jQuery.facebox(strErrorMessage);
								
								return;
							}
							
							if(errorFieldNames[0] == "primary_email")
							{							
								jQuery.facebox(strErrorMessage);
								
								return;
							}
							
							if(errorFieldNames[0] == "validate_phonenumber")
							{							
								jQuery.facebox(strErrorMessage);
								
								return;
							}
						}
						//------------------------------------------
						
						$("input,select,textarea").removeClass("inputerror");
						
						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("<span>" + errorMessagesArr[errorMessageIndex] + "</span>");
									
								//Only increase counter on none grouped fields TODO: add an Array containing the grouped fields
								if
								(
									field.GetName() != "gebmonat" 
									&& field.GetName() != "gebjahr"
								)
								{
									errorMessageIndex++;
								}
								
								$("#" + errorFieldNames[i]).addClass("inputerror");
							}
							else
							{
								if(errorFieldNames[i] == "s_geburtsdatum")
								{
									$("#div_s_geburtsdatum").addClass("ErrorMessageContainerShow_ol");
									$("#div_s_geburtsdatum").html("<span>" + errorMessagesArr[errorMessageIndex] + "</span>");
									
									$("#s_gebtag").addClass("inputerror");
									$("#s_gebmonat").addClass("inputerror");
									$("#s_gebjahr").addClass("inputerror");
								}							
							}
						}
						
						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";
	this.m_Content = "";
}

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.SetContent = function(value)
{
	this.m_Content = value;
}

TefMail.prototype.GetContent = function()
{
	return this.m_Content;
}

TefMail.prototype.Submit = function()
{
	var url = this.m_Url;
	var data = "friends=" + this.m_Friends + "&friends_content=" + this.m_Content;
	
	$.ajax
	(
		{
			url: url,
			type: "POST",
			data: data,
			success: function(msg)
			{
				jQuery.facebox(msg);
			}
		}
	);
}
