 $(document).ready(function(){
	$("#form_email").focus(function(){
		if( typeof $('body').data('email') == 'undefined' ) {
			$('body').data('email','edited');
			$("#form_email").val("");
		}
	});
	$("#form_telefon").focus(function(){
		if( typeof $('body').data('telefon') == 'undefined' ) {
			$('body').data('telefon','edited');
			$("#form_telefon").val("");
		}
	});
	$("#form_text").focus(function(){
		if( typeof $('body').data('text') == 'undefined' ) {
			$('body').data('text','edited');
			$("#form_text").val("");
		}
	});
	$("#form_email").focusout(function(){
                if( $("#form_email").val() == '' ) {
			$("#form_email").val("váš e-mail");
                        $('body').removeData('email');
		}
	});
	$("#form_telefon").focusout(function(){
                if( $("#form_telefon").val() == '' ) {
			$("#form_telefon").val("vaše telefonní číslo");
                        $('body').removeData('telefon');
		}
	});
	$("#form_text").focusout(function(){
		if( $("#form_text").val() == '' ) {
			$("#form_text").val("text zprávy...");
                        $('body').removeData('text');
		}
	});
	$("#sendmail").click(function(){
		var valid = '';
		var countErr = 0;
		var mail = $("#form_email").val();
		var telefon = $("#form_telefon").val();
		var text = $("#form_text").val();
                var intRegex = /[0-9 -()+]+$/;

		text = text.trim();
		mail = mail.trim();


		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			valid += '<ul><li>Email je ve špatném tvaru.</li></ul>';
		}
		if (telefon == 'vaše telefonní číslo') {
			telefon = '';
		}
		else if(telefon.length<9 || !intRegex.test(telefon)) {
			valid += '<ul><li>Telefoní číslo má špatný tvar.</li></ul>';
		}
		if (text.length<1 || text == 'text zprávy...') {
			valid += '<ul><li>Musíte zadat text poptávky.</li></ul>';
		}
		if (valid!='') {
			$("#response").fadeIn("slow");
			if(countErr==1)$("#response").html("Nastala chyba:"+valid);
			else $("#response").html("Nastaly chyby:"+valid);
		}
		else {
			//var datastr ='mail=' + mail + '&telefon=' + telefon + '&text=' + text;
			var datastr = $("#formail").serialize();
			$("#response").css("display", "block");
			$("#response").html("Odesílám poptávku... ");
			$("#response").fadeIn("slow");
			setTimeout("send('"+datastr+"')",5000);
		}
		return false;
	});
});
function send(datastr){
	$.ajax({
		type: "POST",
		url: "web_files/scripts/mail.php",
		data: datastr,
		cache: false,
		success: function(html){
			$("#response").fadeIn("slow");
			$("#response").html(html);
			setTimeout('$("#response").fadeOut("slow")',5000);
			$("#form_email").val("váš e-mail");
			$("#form_telefon").val("vaše telefonní číslo");
			$("#form_text").val("text zprávy...");
			$('body').removeData('email');
			$('body').removeData('telefon');
			$('body').removeData('text');
		}
	});
}
