$(document).ready(function() {
	
	$("#countries_popup_x, #countries_popup_x_selectable").click(function(){
	
		$(".countries_popup_wrap").fadeOut();
		$.mask.close();
	});
	
	$("#map_popup_x").click(function(){
		
	//	$(".map_popup_wrap").hide();
		//alert ("asd");
		$(".map_popup_wrap").fadeOut();
		$.mask.close();
	});

	$("#video_popup_x").click(function(){
		
		$(".video_popup_wrap").fadeOut();
		$.mask.close();
	});
	
	$("#header_top_menu_login_caption, #login_link_btn").click(function(){
		
		open_login_popup();
	});
	
	$(".forget_pass_opener").click(function(){
		open_forget_password_popup();
	});
	
	$(".login_forget_pass_link").click(function(){
		$("#login_layer").hide();
		$("#recover_layer").show();
	});
	
	$("#login_popup_x").click(function(){
		$(".login_popup_wrap").fadeOut();
		$.mask.close();
	});
	
	$(".login_popup_layer#login_layer input").keyup(function(e){
	
		// ENTER key pressed
		if (e.keyCode == 13) {
			$("#login_popup_login_btn").click();
		}
	});
	
	$(".login_popup_layer#recover_layer input").keyup(function(e){
	
		// ENTER key pressed
		if (e.keyCode == 13) {
			$("#login_popup_login_btn").click();
		}
	});
	
	$(".login_popup_layer#recover_layer input").keyup(function(e){
	
		// ENTER key pressed
		if (e.keyCode == 13) {
			$(".recover_send_btn").click();
		}
	});
	
	$("#contact_us_popup_x").click(function(){
		$(".contact_us_popup_wrap").fadeOut();
		$.mask.close();
	});
	
	$("#cancelation_popup_x").click(function(){
		$(".cancelation_popup_wrap").fadeOut();
		$.mask.close();
	});
	
	$("#video_popup_x").click(function(){
		$(".video_popup_wrap").fadeOut();
		$.mask.close();
	});
	
	$("#after_cancelation_popup_x").click(function(){
		$(".after_cancelation_popup_wrap").fadeOut();
		$.mask.close();
	});
	
	
	$(".login_popup_tryagain").click(function(){
		$("#error_layer, #wrongpass_layer, #error_inactive").hide();
		$("#login_layer").show();
	});
	
	$("#login_popup_login_btn").click(function(){
		
		$(".login_popup_wrap .error").remove();
		$("#login_layer").hide();
		$(".login_popup_wrap .loader").show();
		$("#login_layer .subtitle").html("Processing Login Request..");
		
		var email = $("#login_form_email").val();
		
		var password = $("#login_form_password").val();
		var valid = true;
		
		if ($.trim(email) == "") {
			addErrorBox("login_form_email", "This field is mandatory");
			valid = false;
		} else if (!validate_email_format($.trim(email))){
			addErrorBox("login_form_email", "Please enter valid Email address");
			valid = false;
		}
		
		if ($.trim(password) == "") {
			addErrorBox("login_form_password", "This field is mandatory");
			valid = false;
		}
		
		if (valid) {
			var login_res = $.ajax({url: "/ajax/login/",
									type: "POST",
									data: "password="+password+"&email=" +email,
									async:false
								}).responseText;
								
			if (login_res == "1") {
				window.location = "/account";
			} else if (login_res == "2") {
				$(".login_popup_wrap .loader").hide();
				$("#login_layer").hide();
				$("#login_layer .subtitle").html("Login");
				$("#error_inactive").show();
			} else {
				$(".login_popup_wrap .loader").hide();
				$("#login_layer").hide();
				$("#login_layer .subtitle").html("Login");
				$("#wrongpass_layer").show();
			}
		} else {
			$(".login_popup_wrap .loader").hide();
			$("#login_layer .subtitle").html("Login");
			$("#login_layer").show();
		}
	});
	
	$(".recover_send_btn").click(function(){
		$(".login_popup_wrap .login_popup_layer").hide();
		$(".login_popup_wrap .loader").show();
		
		$(".login_popup_wrap .error").remove();
		var email = $("#login_form_forgotten_email").val();
		var valid = true;
		
		if ($.trim(email) == "") {
			addErrorBox("login_form_forgotten_email", "This field is mandatory");
			valid = false;
		}else if (!validate_email_format($.trim(email))){
			addErrorBox("login_form_forgotten_email", "Please enter valid Email address");
			valid = false;
		}
		
		if (valid) {
			var responseText = 
				$.ajax({url: "/ajax/recover_password/",
						type: "POST",
						data: "email=" +email,
						async:false
					}).responseText;
			
			if (responseText == "1") {
				$(".login_popup_wrap .loader").hide();
				$("#recover_thanks_layer").show();
				setTimeout("close_login_popup()", 2000);
				/*$("#contact_us_popup_x").click();*/
			} else {
				$(".login_popup_wrap .loader").hide();
				$("#forgot_pass_error_email").html(email);
				$("#error_layer").show();
			}
		} else {	
			$(".login_popup_wrap .loader").hide();
			$(".login_popup_wrap #recover_layer").show();
		}
	});
	
	$("#header_top_menu_logout_caption").click(function(){
		var login_res = $.ajax({url: "/ajax/logout/",
								type: "POST",
								async:false
							}).responseText;
		window.location = "/";
	});
	
	$("#contact_us_popup_submit").click(function(){
		validate_contact_us_popup();
	});
	
	$("#cancelation_popup_submit").click(function(){
		validate_cancelation_popup();
	});
	
	
	$(".contact_us_opener").click(function(){
		open_contact_us_popup();
	});
	
	$(".cancelation_opener").click(function(){
		open_cancelation_popup();
	});
	
	$(".video_popup_opener").click(function(){
		open_video_popup();
	});
	
	$(".map_popup_opener").click(function(){
		open_map_popup();
	});
	
	$(".locations_popup_opener").click(function(){
		open_locations_popup();
	});
});

function close_login_popup() {
	$("#contact_us_popup_x").click();
}
function validate_contact_us_popup() {
	$("#contact_us_popup_form .error").remove();
	var valid = true;
	var email = $.trim($("#contact_us_popup_form input[name=email]").val());
	if ($.trim($("#contact_us_popup_form input[name=last_name]").val()) == "")
	{
		addErrorBox("contact_us_popup_name", "This field is mandatory");
		valid = false;
	}
	
	if (email == "")
	{
		addErrorBox("contact_us_popup_email", "This field is mandatory");
		valid = false;
	}else if (!validate_email_format(email)){
		addErrorBox("contact_us_popup_email", "Please enter valid Email address");
		valid = false;
	}
	
	if ($.trim($("#contact_us_popup_form textarea[name=msg]").val()) == "")
	{
		addErrorBox("contact_us_popup_msg", "This field is mandatory");
		valid = false;
	}
	
	if (valid) {
		
		var type = $("#contact_popup_form_type").val();
		
		if (typeof _gaq !== 'undefined'){
			if (type == "Sales") 		
				_gaq.push(['_trackPageview', '/Goal/Contact/sales']);
			if (type == "Technical Issue") 
				_gaq.push(['_trackPageview', '/Goal/Contact/technical']);
			if (type == "Billing") 
				_gaq.push(['_trackPageview', '/Goal/Contact/billing']);
			if (type == "Feature Request") 
				_gaq.push(['_trackPageview', '/Goal/Contact/feature']);
			if (type == "Other") 
				_gaq.push(['_trackPageview', '/Goal/Contact/other']);
		}
		
		var dataString = $("#contact_us_popup_form").serialize();
		
		$.ajax ({
			async: false,
			url: "/ajax/send_contact_us/",
			type: "POST",
			data: dataString
		});
		
		
		
		$(".contact_us_popup_wrap .content").hide();
		$(".contact_us_popup_wrap .thankyou_layer").show();
		
		if (type == "Sales") {
			$("input[name=00N20000002xNTF]").each(function(){
				$(this).val($.trim($("#contact_us_popup_form textarea[name=msg]").val()));
			});
			
			var t=setTimeout("$(\"#contact_us_popup_form\").submit();",3000);
		
		}
		
		$(".contact_us_popup_wrap .close_btn").click(function(){
			$(".contact_us_popup_wrap").fadeOut();
			$.mask.close();
		});
		
		/*
		$(".contact_us_popup_wrap").fadeOut();
		$.mask.close();
		*/
	}
	
	return valid;
}

function validate_cancelation_popup() {
	
	$("#cancelation_popup_form .error").remove();
	var valid = true;
	
	if ($.trim($("#cancelation_popup_msg").val()) == "")
	{
		addErrorBox("cancelation_popup_msg", "This field is mandatory");
		valid = false;
	} else if ($("#cancelation_popup_form_reason_type").val() == "0")
	{
		addErrorBox("cancelation_popup_form_reason_type", "This field is mandatory");
		valid = false;
	}
	
	if (valid) {
		
		$(".contact_us_popup .content").hide();
		$(".contact_us_popup .loader").show();
	
		var dataString = $("#cancelation_popup_form").serialize();
		
		var responseText = $.ajax ({
			async: false,
			url: "/ajax/cancel_account/",
			type: "POST",
			data: dataString
		}).responseText;
		
		//$(".contact_us_popup .loader").hide();
		
		var errorCode = "";
		if (responseText.length < 5) {
			errorCode = "(Error Code: "+responseText+")";
		}
		
		if (responseText != "1") {
			
			var mailData = "to=support@biscience.com, roy.asoolin@biscience.com&subject=Cancelation Error&body=response: " + responseText + "\n\nuser:" + $("#cancelation_popup_form_user_id").val();
			
			var responseText = $.ajax ({
			async: false,
			url: "/ajax/send_mail/",
			type: "POST",
			data: mailData
			}).responseText;
			
			alert ("There was an error during cancelling your account. Please contact our support team. " + errorCode);
			$(".cancelation_popup_wrap").fadeOut();
			$.mask.close();
		} else {
			window.location = "/home/action/deactivated";
		}
	}
	
	return valid;
}
