//------------------------------------------------------------------------------
// 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)
{
	$(document).ready(function(){
		$("div.menu > ul > li").each(function(){
			$(this).removeClass("selected");
		});
	});

	if (item == undefined)
	{
		ClickItem("home");
		ClickItem("download");
		ClickItem("order");
		ClickItem("support");
		ClickItem("company");

		ClickItem();
	}
}


function ClickItem(item)
{
	if (item !== undefined)
	{
		$(document).ready(function(){
			$("div.menu > ul > li." + item + " > a").click(function(){
				ToggleMenuItem("div.menu > ul > li." + item + " > a");
			});
		});
	}
}


//------------------------------------------------------------------------------
// 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 url	= document.location.href;
	var tab_rx	= /tab=/i;
	// http://www.w3schools.com/jsref/jsref_obj_regexp.asp

	// Daca avem parametrul tab=x in URL
	if ( url.match( tab_rx ) )
	{
		// index.html?tab=2&s=cnet
		// index.html?s=cnet&tab=3
		var items1		= url.split( tab_rx );
		var items2		= items1[1].split( /\&/ );
		var tab_id		= items2[0];

		var tab_name	= "#tab" + tab_id;
		var tab_btn		= "#tab" + tab_id + "-btn";

		if ( !url.match( /index.html/ ) )
		{
			try
			{
				ResetTabsState();
				ShowTab(tab_btn, tab_name);
			}
			catch (ex) { }
		}
		// Tab-ul se afla pe pagina /index.html
		else
		{
			DisplayContent("#batchphoto-tab" + tab_id + "-btn", "#batchphoto-tab" + tab_id, "#batchphoto");
		}
	}
}


//------------------------------------------------------------------------------
// Contact Form
//------------------------------------------------------------------------------

function ContactUs()
{
	var url = "/script/php/sendmail.php?module=support";
	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);
		
		if ( $("div.slot-main > div.contentbox")[1] )
			$($("div.slot-main > div.contentbox")[1]).height(199);
		else
			$($("div.slot-main > div.contentbox")[0]).height(199);
		
		$.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');
}


//------------------------------------------------------------------------------
// RetrieveData
//------------------------------------------------------------------------------

function RetrieveData( param1, div_id, err )
{
	var email		= $("#email");
	var orderno_or_code = $("#" + param1 );
	var error		= $(err);
	var selected	= 0;

	if (IsValidMail(email.val(), false))
		selected++;

	if (orderno_or_code.val().length > 0)
		selected++;

	if (selected == 0)
		error.html("<span style=\"color: red;\">Please specify a value.</span>");

	else if (selected > 0)
	{
		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() +
									"&" + param1 + "=" + orderno_or_code.val();
				retrieve_data += param1 == 'orderno' ? "&action=code" : "&action=download";

				var retrieve_response = $.ajax({
					type: "POST",
					url: "/script/php/retrieve-code.php",
					data: retrieve_data,
					async: false
				}).responseText;

				$(div_id).html(retrieve_response);
				$(div_id).css("height", "auto");
				$("#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);
	}
}


//------------------------------------------------------------------------------
// Uninstall Survey
//------------------------------------------------------------------------------

function CheckCaptcha(captcha)
{
	var response = $.ajax({
		type: "GET",
		url: "../script/php/sendmail.php?module=get-captcha",
		async: false
	}).responseText;

	response	= response.toLowerCase();
	captcha		= captcha.toLowerCase();

	if (captcha == false || captcha.length == 0)
		return false;
	
	else if (captcha == undefined)
		return false;

	else if (response == captcha)
		return true;

	else
		return false;
}

function SendSurvey(div)
{
	var email	= $("#senderEmail").val();
	var message = $("#message").val();
    var os		= $("#os").val();
    var captcha	= $("#code").val();
    var errors	= 0;
	
    var data_string =	"emailAddress=" + email +
						"&message=" + message +
						"&os=" + os +
						"&captcha=" + captcha;

	if (!IsValidMail(email, false))
    {
         $("#emailAddressErrorHolder").html("<span style=\"color: red;\">Please enter a valid email address.</span>");
         errors++;
    }

    if (IsEmpty(message))
    {
         $("#messageErrorHolder").html("<span style=\"color: red;\">Please enter your message.</span>");
         errors++;
    }

    if (IsEmpty(os))
    {
         $("#osErrorHolder").html("<span style=\"color: red;\">Please select a Windows version.</span>");
         errors++;
    }

    if (!CheckCaptcha(captcha))
    {
		$("#captchaErrorHolder").html("<span style=\"color: red;\">Human verification failed.</span>");
		errors++;
    }
	
	if (errors == 0)
	{
		var response = $.ajax({
			type: "POST",
			url: "../script/php/sendmail.php?module=sendsurvey",
			data: data_string,
			async: false
          }).responseText;

		  $("#" + div).html(response);
     }
}


//------------------------------------------------------------------------------
// Functii Utilitare
//------------------------------------------------------------------------------

// De fiecare data cand se lanseaza un download se vor executa si actiunele de
// mai jos: tracking si redirect
function DownloadActions()
{
	pageTracker._trackPageview('/download/batchphoto.exe');

	//var url = "http://www.batchphoto.com/download-start.html";
	//setTimeout( "window.location='" + url + "'", 2000 );
	return true;
}


function IsEmpty(input)
{
	if ($(input).val() == false || input == false)
		return true;
	else if (input.length == 0)
		return true;
	else
		return false;
}

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 emailPattern = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		return emailPattern.test(email);

		// Expresia regex nu merge pt adrese de genul name@site-name.com
		//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(field, error)
{
	if (typeof(field) == "object")
		field = $(field).val();
	
	if (!IsValidMail(field, false))
		$(error).html("<span style=\"color: red;\">Invalid email address.</span>")
	else
		$(error).html("");
}

function CheckField(field, error)
{
	if (IsEmpty(field))
		$(error).html("<span style=\"color: red;\">Please specify a value.</span>")
	else
		$(error).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>");
	}
}

function ShowSurvey(el)
{
	$(el).modal();
}

function CloseSurveyForm(el)
{
	$(el).close();
}


//------------------------------------------------------------------------------
// Sitemap Styler v0.1
// written by Alen Grakalic, provided by Css Globe (cssglobe.com)
// visit http://cssglobe.com/lab/sitemap_styler/
//------------------------------------------------------------------------------

function treelist()
{
	window.onload = sitemapstyler;
}

this.sitemapstyler = function()
{
	var sitemap = document.getElementById("treelist")
	if(sitemap)
	{
		this.listItem = function(li){
			if(li.getElementsByTagName("ul").length > 0){
				var ul = li.getElementsByTagName("ul")[0];
				ul.style.display = "block";
				var span = document.createElement("span");
				span.className = "expanded";
				span.onclick = function(){
					ul.style.display = (ul.style.display == "none") ? "block" : "none";
					this.className = (ul.style.display == "none") ? "collapsed" : "expanded";
				};
				li.appendChild(span);
			};
		};

		var items = sitemap.getElementsByTagName("li");
		for(var i=0;i<items.length;i++){
			listItem(items[i]);
		};

	};
};
