﻿$(document).ready(function() {
	
	$("html").ready(function() {
		$("#sendactuality_email").val("email");
		$("#sendact_msg").hide();
		$("#details").hide();
		if ($('#blizsi_info_select :selected').attr("class") != "show_podrobneinfodiv") {
			$("#podrobneinfodiv").hide();
		}
	})

	$("#sendactuality_email").click(function() {
		if ($("#sendactuality_email").val() == "email" ) {
			$("#sendactuality_email").val("");
		}
	})

	$("#news_arrow").hover(function() {
		if ($(this).attr("src") == "../img/bannery/news_arrow.png") {
			$(this).attr("src", "../img/bannery/news_arrow1.png");
		}
		if ($(this).attr("src") == "img/bannery/news_arrow.png") {
			$(this).attr("src", "img/bannery/news_arrow1.png");
		}
    }, function(){
        if ($(this).attr("src") == "../img/bannery/news_arrow1.png") {
			$(this).attr("src", "../img/bannery/news_arrow.png");
		}
		if ($(this).attr("src") == "img/bannery/news_arrow1.png") {
			$(this).attr("src", "img/bannery/news_arrow.png");
		}
    })
	
	$("#news_arrow").click(function(){
		if (!isEmailAddress($("#sendactuality_email").val())) {
			$("#sendact_msg").html("<td><span id=\"sendacterror\" style=\"color: red; margin-left:4px;\">Vyplňte prosím platný email</span></td>");
		} else {
			var url = "/test/php/actuality.php?useremail="+$("#sendactuality_email").val();
//			alert(url);
			$.ajax({
				type: "GET",
				url: ""+url,
				dataType: "html",
				data:"",
				success:function(g){
					if (g == "ok") {
						$("#sendactuality_email").val("");
						$("#sendact_msg").html("<td><span id=\"sendacterror\" style=\"color: green; margin-left:4px;\">Odesláno</span></td>");
						$("#sendact_msg").fadeOut(2500);
					} else {
						$("#sendact_msg").html("<td><span id=\"sendacterror\" style=\"color: red; margin-left:4px;\">" + g + "</span></td>");
					}
				},
				error:function (xhr, ajaxOptions, thrownError){
					$("#sendact_msg").html("<td><span id=\"sendacterror\" style=\"color: red; margin-left:4px;\">Chyba při kontaktu serveru. Zkuste to prosím znovu.</span></td>");
				}
			});
		}
		$("#sendact_msg").show();
	})
	
	$("#sendactuality_email").keyup(function() {
		if ($(this).val().length > 5 && !isEmailAddress($(this).val())) {
			$(this).css("color", "red");
		} else {
			$(this).css("color", "black");
			$("#sendact_msg").fadeOut();
		}
	})

	$("#more_info").click(function() {
		if ($(this).attr("class") == "details_hidden") {
			$("#details").show();
			$(this).attr("class", "details_shown");
			$("#more_info").val("Skrýt");
		} else {
			$(this).attr("class", "details_hidden");
			$("#details").hide();
			$("#more_info").val("Zadat více informací");
		}
	})
	
	$("#blizsi_info_select").change(function() {
		if ($('#blizsi_info_select :selected').attr("class") == "show_podrobneinfodiv") {
			$("#podrobneinfodiv").show();
		} else {
			$("#podrobneinfoarea").val("");
			$("#podrobneinfodiv").hide();
		}
	})

	$("#email").keyup(function() {
		if ($(this).val().length > 5 && !isEmailAddress($(this).val())) {
			$(this).css("color", "red");
		} else {
			$("#mailerror").fadeOut();
			$(this).css("color", "black");
		}
	})
	
	
})

function validateForm() {
	if ($("#email").val() == "") {
		$("#email").after("<span id=\"mailerror\" style=\"color: red; margin-left:4px;\">Vyplňte prosím email</span>");
		$("#email").focus();
		alert("1");
		return false;
	}

    if (!isEmailAddress($("#email").val())) {
		$("#email").after("<span id=\"mailerror\" style=\"color: red; margin-left:4px;\">Vyplňte prosím platný email</span>");
		$("#email").focus();
		alert("2");
		return false;
	}
	return true;
}

function isEmailAddress (email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var address = email;

    if(reg.test(address) == false) {
        return false;
    }
    return true;
}

