
/**
 * @author elabdesign
 */

var EmailRegex = /^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/;
var PhoneRegex = /^[0-9\-\ \(\)\+]{6,20}$/;
var NumbersRegex = /^[0-9]+$/;
var IE = /*@cc_on!@*/false;

function initialize() {
	
}

$(document).ready(function(){

	$('#SearchInput') . keyup(function(e){
		if(e.keyCode == 13)
			document.location = "/?type=search&q="+$(this).val();
	});
	$('#search_ex') . click(function(){
		var Ex = $('#search-bar') . find('input[name=exercise]') . val();
		var Day = $('#search-bar') . find('input[name=day]') . val();
		
		document.location = "/?type=search-exercise&day="+Day+"&ex="+Ex; 
	});

	$('ul.choise') . children('li') . click(function(){
		
		var Id = $(this) . attr('name');
		var Name = $(this) . html();
		
		$(this) . parent() . parent() . find('span') . html(Name);
		$(this) . parent() . parent() . find('input') . val(Id);
		$(this) . parent() . fadeOut();
	});

	$('.dropdown') . click(function(){
		$(this) . find('ul') . show();
		$(this) . find('li') . show();
	});

	$('ul.choise') . hover(function(){}, function(){
		$(this) . parent() . find('ul') . hide();
	});
		
	var border_height = $("#content") . height() + 20;

	$("#leftmain") . height(border_height)
	$("#rightmain") . height(border_height)

	$("#LoginButton") . click(function(){
		if($("input[name=email]") . val() == "" || $("input[name=password]") . val() == ""){
			alert("Всички полета са задължителни");
			return;
		}
		Email = $("input[name=email]") . val();
		Password = $("input[name=password]") . val();
		if (!EmailRegex.test(Email)) {
			alert("Невалиден email!");
			return;
		}
		
		$.ajax({
			type: "POST",
			url: "/php/action.php",
			data: {
				action: "Login",
				email: Email,
				password: Password
			}, success: function(result){						
				if(result == 0)
					alert("Възникна Грешка!");
				else
					document.location.reload(true);
			}
		});
	});
	
	$(".login") . select() . change(function(){
		var Type = $(this) .find('option:selected') . attr('id');
		switch(Type)
		{
			case "getUsers":
				document.location = "/?type=getUsers";
				break;
			case "getAdverts":
				document.location = "/?type=getAdverts";
				break;
			case "addPage":
				document.location = "/?type=addPage";
				break;
		}
	});
	
	$("#Logout") . click(function(){
		$.ajax({
			type: "POST",
			url: "/php/action.php",
			data: {
				action: "Logout"
			}, success: function(result){
				document.location.reload(true);
			}
		});
	});
	
	$("input[name=subscribe]") . click(function(){
		var Email = $('input[name=email_subscr]') . val();
		var Name = $('input[name=name_subscr]') . val();
		if($('input[name=subscribed]') . is(':checked')){
			var Subscr = 0;
		} else {
			var Subscr = 1;
		}
		$.ajax({
			type: "POST",
			url: "/php/action.php",
			data: {
				action: "subscribe",
				email: Email,
				name: Name,
				subscribed: Subscr
			}, success: function(r){
				if(r == 0){
					document.location.reload(true);
					alert('Вие се абонирахте за email-и с промоции!');
				}else{
					$('#error_subsribe') . html(r);
					$('#error_subsribe') . show();
				}
					
			}
		})
	});
	
	$(".rm") . click(function(){
		if (confirm("Сигурни ли сте, че желате да изтриете това?")) {
			$.ajax({
			type: "POST",
			url: "/php/action.php",
			data: {
				action: "rm",
				id: $(this) . attr('id'),
				type: $(this) . attr('name')
			}, success: function(result){
				if(!result)
					alert("Възникна Грешка!");
				else
					document.location.reload(true);
			}
		});
		}
	});
	
});
