//------------------------------------------------------------------------------
// Main Menu
//------------------------------------------------------------------------------

function HoverIt(bool_include_submenu)
{
	$("#menu > ul > li > a").each(function()
	{
		/*$(this).hover(function(){
			$(this).fadeTo("normal", 1.0)}, function(){$(this).fadeTo("normal", 0.8);
		});*/

		$(this).click(function(){
			ClearSelect();
			$(this).addClass("selected");
		});
	});

	if (bool_include_submenu == true)
	{
		$("#body ul.header-tabs > li > a").each(function(){
			$(this).hover(function(){
				$(this).fadeTo("normal", 1.0)}, function(){$(this).fadeTo("normal", 0.8);
			});
		});
	}
}


function ClearSelect()
{
	$("#home").removeClass("selected");
	$("#software").removeClass("selected");
	$("#support").removeClass("selected");
	$("#order").removeClass("selected");
	$("#company").removeClass("selected");
}


function ToggleMenuItem(item)
{
	if (item == undefined)
	{
		ClickItem();

		ClickItem("home");
		ClickItem("download");
		ClickItem("order");
		ClickItem("support");
		ClickItem("company");
	}
	else
	{
		$("div.menu > ul > li").each(function(){
			if ("div.menu > ul > li." + this.className !== item)
				$(this).removeClass("selected");
		});
	}
}


function ClickItem(item)
{
	if (item !== undefined)
	{
		$("div.menu > ul > li." + item + " > a").click(function(){

			ToggleMenuItem("div.menu > ul > li." + item + " > a");
			$("div.menu > ul > li." + item + " > a").addClass("selected");
		});
	}
	else
	{
		var absolute_url	= "http://www.bitscoffee.com";
		var where			= document.location.href;
		var where_rx		= new Array();
		var where_tabs		= new Array();

		where_rx[0] = /index.html/i;
		where_rx[1] = /download.html/i;
		where_rx[2] = /purchase.html/i;
		where_rx[3] = /support.html/i;
		where_rx[4] = /company.html/i;

		where_tabs[0] = "div.menu > ul > li.home > a";
		where_tabs[1] = "div.menu > ul > li.download > a";
		where_tabs[2] = "div.menu > ul > li.purchase > a";
		where_tabs[3] = "div.menu > ul > li.support > a";
		where_tabs[4] = "div.menu > ul > li.company > a";

		if (where == absolute_url || where == (absolute_url + "/"))
			$(where_tabs[0]).addClass("selected");

		$.each(where_rx, function(i){
			if (where.match(where_rx[i]))
			{
				if (!$(where_tabs[i]).hasClass("selected"))
					$(where_tabs[i]).addClass("selected");
			}
		});
	}
}

//------------------------------------------------------------------------------
// Index - Tab Scroll Effect
//------------------------------------------------------------------------------

var original_sender = null;

function DisplayContent(sender, element, container)
{
	var content = $(container);
	var target_height = ReturnTabsHeight(element);
	var margin_bottom = content.css("marginBottom");

	ResetTabsState(sender);
	HideTabs(element);

	if (this.original_sender == sender)
	{
		this.original_sender = null;
		$(element).fadeOut(750);
		$(sender).removeClass("selected");

		content.animate({height: "1px"}, 750, function(){
			content.slideUp(250, function(){
				$(element).hide();
			});
		});
	}

	else if (content.is(":hidden"))
	{
		this.original_sender = sender;
		$("a" + sender).addClass("selected");

		content.css("height", target_height);
		content.slideDown(750);

		$(element).fadeIn("normal");
	}

	else if (content.is(":visible") && content.css("height") == target_height)
	{
		this.original_sender = sender;
		$("a" + sender).addClass("selected");
		HideTabs(element);
		$(element).fadeIn("normal");
	}

	else if (content.is(":visible") && content.css("height") !== target_height)
	{
		content.animate().stop();
		content.css("overflow", "visible");
		content.css("margin-bottom", margin_bottom);

		this.original_sender = sender;

		$("a" + sender).addClass("selected");

		content.animate( {height: target_height, marginBottom: "40px"}, 750, function(){
			HideTabs(element);
			$(element).fadeIn("normal");
		});
	}
}


function HideTabs(sender)
{
	$.main = $(sender).parent().parent();

	$("#" + $.main.get(0).id + " > div.slot-main > div").each(function(){
		if("#" + this.id !== sender)
			$("#" + this.id).hide();
	});
}


function ReturnTabsHeight(element)
{
	return $(element).css("height");
}


//------------------------------------------------------------------------------
// Tabs
//------------------------------------------------------------------------------

// ShowTab - Afisaza tab-ul selectat si le ascunde pe celelalte
function ShowTab(sender, element)
{
	if (sender !== null)
	{
		ResetTabsState();
		$(sender).addClass("selected");
	}

	if ($("#tab1").is(":visible"))
		$("#tab1").hide();
	if ($("#tab2").is(":visible"))
		$("#tab2").hide();
	if ($("#tab3").is(":visible"))
		$("#tab3").hide();
	if ($("#tab4").is(":visible"))
		$("#tab4").hide();
	if ($("#tab5").is(":visible"))
		$("#tab5").hide();

	$(element).fadeIn(300);
	SetHeight(element);

	if ($.toggle_right_tabs == true)
	{
		var right_tab = $(element + "-right");

		if (right_tab !== null)
			right_tab.show();
	}
}


function ResetTabsState(sender)
{
	if (sender == undefined)
	{
		$("li.tab1 a").removeClass("selected");
		$("li.tab2 a").removeClass("selected");
		$("li.tab3 a").removeClass("selected");
		$("li.tab4 a").removeClass("selected");
		$("li.tab5 a").removeClass("selected");
		
		if ($.toggle_right_tabs == true)
		{
			var right1 = $("#tab1-right");
			var right2 = $("#tab2-right");
			var right3 = $("#tab3-right");
			var right4 = $("#tab4-right");
			var right5 = $("#tab5-right");

			if (right1 !== null)
				right1.hide();

			if (right2 !== null)
				right2.hide();

			if (right3 !== null)
				right3.hide();
			
			if (right4 !== null)
				right4.hide();
			
			if (right5 !== null)
				right5.hide();
		}
	}
	else
	{
		$.ul = $(sender).parent().parent();

		$("#" + $.ul.get(0).id + " > li > a").each(function(){
			if ("#" + this.id !== sender)
				$("#" + this.id).removeClass("selected");
		});
	}
}


function SetHeight(tab)
{
	// - container (acelas height cu tab1, deoarece tab1 este afisat default)
	//		- slot-main
	//			- tabX (height fix)
	//		- slot right (height fix, deoarece fara mosteneste height 100% de la clasa)
	
	var a = parseInt($("div.container > div.slot-main > " + tab).height());
	var b = parseInt($("div.container > div.slot-right").height());
	var x = ( a >= b ? a : b );
	
	$("div.container").css("height", x.toString() + "px");
	$("div.container > div.slot-main > " + tab).css("height", x.toString() + "px");
}


//------------------------------------------------------------------------------
// Link direct cater un anumit tab
//------------------------------------------------------------------------------

function CheckAndSwitchTo()
{
	var tab_rx		= /\?tab=/i;
	var where		= document.location.href;

	if (where.match(tab_rx))
	{
		var items		= where.split(tab_rx);
		var tab_name	= "#tab" + items[1];
		var tab_btn		= "#tab" + items[1] + "-btn";

		try
		{	
			ResetTabsState();
			ShowTab(tab_btn, tab_name);
		}
		catch (ex) { }
	}
}


//------------------------------------------------------------------------------
// Contact Form
//------------------------------------------------------------------------------

function ContactUs()
{
	var url = "script/php/sendmail.php?mod=send";
	var counter_errors = 0;

	var issue		= $("#issue").val();
	var title		= $("#title").val();
	var name		= $("#name").val()
	var email		= $("#email").val();
	var message		= $("#message").val();
	var os			= $("#os").val();
	var captcha		= $("#captcha").val();

	if (issue == "")
	{
		counter_errors++;
		$("#issue_error").html("<span style=\"color: red;\">Please select a issue.</span>");
	}

	if (title == "")
	{
		counter_errors++;
		$("#title_error").html("<span style=\"color: red;\">Please specify a title.</span>");
	}

	if (name == "")
	{
		counter_errors++;
		$("#name_error").html("<span style=\"color: red;\">Please specify a title.</span>");
	}

	if (!IsValidMail(email, false))
	{
		counter_errors++;
		$("#email_error").html("<span style=\"color: red;\">Please specify a valid email.</span>");
	}
	
	if (message == "")
	{
		counter_errors++;
		$("#message_error").html("<span style=\"color: red;\">Please specify a message.</span>");
	}
	
	if (os == "")
	{
		counter_errors++;
		$("#os_error").html("<span style=\"color: red;\">Please select a version.</span>");
	}

	if (captcha == "" || captcha.length > 4)
	{
		counter_errors++;
		$("#captcha_error").html("<span style=\"color: red;\">Please specify a valid captcha.</span>");
	}

	if (counter_errors == 0)
	{
		var data =	"issue=" + issue +
					"&title=" + title +
					"&name=" + name +
					"&email=" + email +
					"&message=" + message +
					"&os=" + os +
					"&captcha=" + captcha;

		$.response = $.ajax({
			type: "POST",
			url: url,
			data: data,
			async: false
		}).responseText;

		if ($("#subscribe-to-newsletter").attr("checked") == true)
		{
			var newsletter_data =	"email=" + $("#email").val() +
									"&ref=support";
			$.ajax({
				type: "POST",
				url: "script/php/newsletter.php?mod=send",
				data: newsletter_data
			});
		}

		$("#contact-us").html($.response);
	}
}


//------------------------------------------------------------------------------
// Newsletter
//------------------------------------------------------------------------------

function Newsletter(unsubscribe)
{
	$.url = unsubscribe == 'unsubscribe' ? "script/php/newsletter.php?mod=unsubscribe" : "script/php/newsletter.php?mod=send";
	$.container = $("#newsletter");
	$.email = $("#newsletter-email");
	$.error = $("#newsletter-error");

	if (!IsValidMail($.email.val(), false))
	{
		$("#newsletter-error").height(19);
		$($("div.slot-main > div.contentbox")[1]).height(199);
		$("div.container").height(519);
		$.error.html("<span style=\"color: red;\">Please specify a valid email</span>");
	}
	else
	{
		$.data = "email=" + $.email.val() + "&ref=download";

		$.response = $.ajax({
			type: "POST",
			url: $.url,
			data: $.data,
			async: false
		}).responseText;

		$.container.html($.response);
	}
}


function NewsletterAction(unsubscribe)
{
	if (unsubscribe == undefined)
	{
		if (document.location.href.match(/download.html/i) || document.location.href.match(/support.html/))
		{
			if (document.location.href.match(/confirmation=valid/i))
			{
				$("#newsletter").height($("#newsletter").height());
				$("#newsletter").html("<span style=\"color: green;\">Thank you for subscribing to our newsletter</span>");

				if (document.location.href.match(/support.html/))
				{
					$("#newsletter-subscribed").css("display", "block");
					
					ResetTabsState();
					ShowTab("#tab3-btn", "#tab3");
				}
			}
			else if (document.location.href.match(/action=unsubscribe/i))
			{
				var items	= $("div.container > div.slot-main > div.contentbox");
				var item	= $(items[1]);

				var html	=	"<h3>Newsletter Unsubscribe</h3>" +
								"<hr/>" +
								"<p>In order to unsubscribe please type your email adress</p>" +
								"<div id=\"newsletter\" class=\"center-text\" style=\"margin-bottom:20px;\">" +
								"<input id=\"newsletter-email\" name=\"newsletter-email\" type=\"text\" size=\"30\" onfocus=\"Clear('#newsletter-error');\" onchange=\"CheckEmail(this, '#newsletter-error');\" />" +
								"<input id=\"newsletter-send\" name=\"newsletter-send\" type=\"button\" value=\"Submit\" onclick=\"NewsletterAction('unsubscribe');\" />" +
								"<div id=\"newsletter-error\" style=\"display: block; margin-top: 10px;\"></div>" +
								"</div>";

				item.html(html);
			}
		}
	}
	else
		Newsletter('unsubscribe');
}


//------------------------------------------------------------------------------
// Retrieve Code
//------------------------------------------------------------------------------

function RetrieveCode()
{
	var email		= $("#email");
	var orderno		= $("#orderno");
	var error		= $("#retrieve-code-error");
	var selected	= 0;

	if (IsValidMail(email.val(), false))
		selected++;

	if (orderno.val().length > 0)
		selected++;

	if (selected == 0)
		error.html("<span style=\"color: red;\">Please specify a value.</span>");

	else if (selected == 1)
	{
		var challenge	= $("#recaptcha_challenge_field");
		var response	= $("#recaptcha_response_field");
		var recaptcha_data	=	"recaptcha_challenge_field=" + challenge.val() +
								"&recaptcha_response_field=" + response.val();

		if (response.val().length > 0)
		{
			var recaptcha_validation = $.ajax({
				type: "POST",
				url: "script/extern/recaptcha.php",
				data: recaptcha_data,
				async: false
			}).responseText;

			if (recaptcha_validation == "CAPTCHA_VALID")
			{
				var retrieve_data = "email=" + email.val() +
									"&orderno=" + orderno.val();

				var retrieve_response = $.ajax({
					type: "POST",
					url: "script/php/retrieve-code.php",
					data: retrieve_data,
					async: false
				}).responseText;

				$("#retrieve-code").html(retrieve_response);
				$("#retrieve-code").css("height", "100px");
				$("#content > div.container").height(140);
			}
			else
			{
				error.html("<span style=\"color: red;\">The validation string is not correct</span>");				Recaptcha.reload();
			}
		}
		else
			error.html("<span style=\"color: red;\">Please enter the validation string</span>");
	}
}


function Clear(element)
{
	if ($(element).html() !== null)
		$(element).html("");

	if (document.location.href.match(/download.html/i))
	{
		$("#newsletter-error").height(0);
		$($("div.slot-main > div.contentbox")[1]).height(180);
		$("div.container").height(500);
	}
}


//------------------------------------------------------------------------------
// Functii Utilitare
//------------------------------------------------------------------------------

function IsEmpty(selector)
{
	if ($(selector).val() !== "")
		return false;
	else
		return true;
}

function IsEmail(input)
{
	if (!IsEmpty(input))
	{
		if (IsValidMail(input))
			return true;
	}
	return false;
}

function IsValidMail(email, bool_input_only)
{
	if ((email == null || email.length == 0) && bool_input_only == true)
		return true;

	else if ((email == null || email.length == 0) && bool_input_only == false)
		return false;

	else if ((email != null || email.length > 0) && bool_input_only == false)
	{
		var regex = new RegExp("^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$");
		return regex.test(email);
	}

	else if (email == null && bool_input_only == undefined)
		return false;

	else
		return false;
}

function CheckEmail(email_field, eror_holder)
{
	if (!IsValidMail($(email_field).val(), false))
		$(eror_holder).html("<span style=\"color: red;\">Invalid email address.</span>")
	else
		$(eror_holder).html("");
}

function CheckField(field, eror_holder)
{
	if (IsEmpty(field))
		$(eror_holder).html("<span style=\"color: red;\">Please specify a value.</span>")
	else
		$(eror_holder).html("");
}

function IsSelected(item)
{
	if ($(item).val() !== "")
		return true;
	else
		return false;
}

function CheckSelect(item, error)
{
	if (item !== undefined && item !== null)
	{
		$(error).html("");

		if (!IsSelected(item))
			$(error).html("<span style=\"color: red;\">Please select a value.</span>");
	}
}