var RecaptchaOptions = {theme : 'custom', lang: 'en', custom_theme_widget: 'recaptcha'}; 

var Good = '<img class="good_img" src="img/success_16.png" alt=""/>';
var Wrong = '<img class="error_img" src="img/error_16.png" alt=""/>';

var Loading = false;

var Loading1 = '<div class="loading_status ld1"><img class="loading_img" src="img/loading.gif" alt=""/>';
var Loading2 = Loading1 + " Caricamento in corso...";
var Loading3 = '<div class="loading_status ld2"><img class="loading_img" src="img/loading.gif" alt=""/>';
var Loading4 = '<div class="loading_status ld3"><img class="loading_img" src="img/loading.gif" alt=""/>';
Loading1 += "</div>";
Loading2 += "</div>";
Loading3 += "</div>";
Loading4 += "</div>";

var DataCheck = 0;
var Type = 0;

function CheckInputAndOperate(Name, Name2, Action, IsAcc, ID)
{
	if (typeof(IsAcc) == 'undefined')
	{
		IsAcc = 0;
	}

	if (typeof(ID) == 'undefined')
	{
		ID = 0;
	}

	var id = !IsAcc ? "#reg_form" : "#acc_form";

	var el = $(id + ' input[name="' + Name + '"]');
	var el2 = null;
	var Value = el.val();
	var Value2 = "";

	var IsSelect = false;

	if (typeof(Value) == 'undefined')
	{
		el = $(id + ' select[name="' + Name + '"]');

		Value = el.val();

		IsSelect = true;
	}

	if (typeof(IsAcc) == 'undefined')
	{
		IsAcc = 0;
	}

	if ((Name == "rule") || (Name == "privacy") || (Name == "newsletters"))
	{
		Value = el.is(':checked') ? "yes" : "";
	}

	if (typeof(Name2) == 'undefined')
	{
		Name2 = "";
	}

	else
	{
		el2 = $(id + ' input[name="' + Name2 + '"]');
		Value2 = el2.val();
	}

	if (IsSelect)
	{
		if (Name == "tipo")
		{
			Type = Value;

			if (Type == 1)
			{
				$("#piva_row").show();

				$("#piva_row").find("input").removeAttr("disabled");
			}

			else
			{
				$("#piva_row").hide();

				$("#piva_row").find("input").attr("disabled", "disabled");
			}
		}
	}

	$.post(Link + "/utils/AjaxHandler.php", {req: 1, id: ID, name: Name, name2: Name2, value: Value, value2: Value2, isacc: IsAcc}, function(data)
	{
		data = data.split(String.fromCharCode(1));

		var Return = parseInt(data[0]);
		var Message = data[1];

		switch (Action)
		{
			case 0: //Nothing
			{
				

				break;
			}

			case 1: //Display Result
			{
				if (Name2.length > 0)
				{
					Name = Name2;
				}

				else if ((Name == "giorno") || (Name == "mese") || (Name == "anno"))
				{
					Name = "data";

					if (Return)
					{
						DataCheck++;
					}
				}

				if ((Name == "data") && (DataCheck >= 3))
				{
					$("#" + Name).html(Good);

					if (!IsSelect)
					{
						el.css("background-color", "#4FA600");
					}

					DataCheck = 0;
				}

				else if (Return && (Name != "data"))
				{
					$("#" + Name).html(Good);

					if (Name2.length)
					{
						el = el2;
					}

					if (!IsSelect)
					{
						el.css("background-color", "#4FA600");
						el.css("color", "#FFFFFF");
					}
				}

				else
				{
					$("#" + Name).html("<div title=\"" + Message + "\">" + Wrong + "</div>");

					if (Name2.length)
					{
						el = el2;
					}

					if (!IsSelect)
					{
						el.css("background-color", "#d90b24");
						el.css("color", "#FFFFFF");
					}
				}

				break;
			}
		}
	});
}

function CheckCaptcha(Value1, Value2)
{
	if ((Value1.length > 0) && (Value2.length > 0))
	{
		$.post(Link + "/utils/AjaxHandler.php", {req: 3, recaptcha_response_field: Value1, recaptcha_challenge_field: Value2}, function(data)
		{
			data = data.split(String.fromCharCode(1));

			var Return = parseInt(data[0]);
			var Message = data[1];

			if (Return)
			{
				$("#captcha").html(Good);
			}

			else
			{
				$("#captcha").html("<div title=\"" + Message + "\">" + Wrong + "</div>");
			}

			return Return;
		});
	}

	else
	{
		$("#captcha").html("<div title=\"Inserisci il Codice di Sicurezza\">" + Wrong + "</div>");
	}
}

function PressedRegForm(Name, Name2)
{
	if (typeof(Name2) == 'undefined')
	{
		Name2 = "";
	}

	CheckInputAndOperate(Name, Name2, 1, 0, "");
}

var GeneralDur = 500;
var GeneralDur2 = GeneralDur / 2;

function LoadContent(Request, Index, Name, Parameter)
{
	if (Loading)
	{
		return false;
	}

	if (typeof(Parameter) == 'undefined')
	{
		Parameter = -1;
	}

	Loading = true;

	var el = $("#" + Name + "_content");

	el.show();
	el.html(Loading1);

	$(".general_section_menu ul li").removeClass("item_active");
	$(".general_section_menu ul #" + Name + "_" + Index).addClass("item_active");

	$(".general_section_menu ul #" + Name + "_" + Index).each(function() { OverBackgroundFunction(this) });
	$(".general_section_menu ul #" + Name + "_" + Index + " a").each(function() { OverTextFunction(this) });

	$(".general_section_menu ul li").each(function() { OutBackgroundFunction(this) });
	$(".general_section_menu ul li a").each(function() { OutTextFunction(this) });

	$.post(Link + "/utils/AjaxHandler.php", {req: Request, index: Index, param: Parameter}, function(data)
	{
		/*el.css("margin-left", "-100%");
		el.animate({"margin-left": "0"}, GeneralDur);*/

		el.hide();
		el.fadeIn(GeneralDur, function() { Loading = false; });

		el.html(data);

		Shadowbox.clearCache();
		Shadowbox.setup();

		if (Name == "profilo")
		{
			$("#center_sx").css("background-image", "url('img/profilo_" + (Index + 1) + ".jpg')");
			$("#center_sx").css("background-repeat", "no-repeat");
			$("#center_sx").css("background-position", "right bottom");
		}

		$(".dynamic_right.pro ul li").click(function()
		{
			$(this).children("div").slideToggle();
		});
	});

	return true;
}

function LoadProfilo(Index)
{
	return LoadContent(0, Index, "profilo");
}

function LoadProdotti(Index)
{
	return LoadContent(7, Index, "prodotti");
}

function LoadReference(Index, Reference)
{
	return LoadContent(1, Index, "references", Reference);
}

function LoadVideo(Index, Reference)
{
	return LoadContent(9, Index, "videos", Reference);
}

function LoadVideoDescription(Index)
{
	var el = $("#v_description");

	el.html(Loading1);

	$.post(Link + "/utils/AjaxHandler.php", {req: 10, index: Index}, function(data)
	{
		el.html(data);
	});

	return false;
}

function LoadNews(Index, Reference)
{
	return LoadContent(2, Index, "news", Reference);
}

function LoadDownload(Index, Reference)
{
	return LoadContent(3, Index, "download", Reference);
}

function OpenProductDetails(Code, Return, Already)
{
	if (typeof(Already) == 'undefined')
	{
		Already = false;
	}

	if (!Already)
	{
		DisplayScheda(Loading3);
	}

	else
	{
		SetScheda(Loading3);
	}

	$.post(Link + "/utils/AjaxHandler.php", {req: 5, code: Code}, function(data)
	{
		SetScheda(data);
	});

	if (typeof(Return) == 'undefined')
	{
		Return = true;
	}

	if (Return)
	{
		return false;
	}
}

function OpenProductGallery(Code)
{
	SetScheda(Loading3);

	$.post(Link + "/utils/AjaxHandler.php", {req: 6, code: Code}, function(data)
	{
		SetScheda(data);
	});

	return false;
}

function OpenProductColours(Code)
{
	SetScheda(Loading3);

	$.post(Link + "/utils/AjaxHandler.php", {req: 8, code: Code}, function(data)
	{
		SetScheda(data);
	});

	return false;
}

function OpenProductColoursScroller()
{
	$(".product_colours").show();
}

function OpenEngineering()
{
	//Shadowbox.open({player: "swf", content: "engineering.swf", width: 1000, height: 550});
	document.location = "engineering";
}

function RemoveLoading()
{
	if ($("#loading_full").length)
	{
		$("#loading_full").remove();
	}
}
