/* Author:

 */
function validateEmail($email) {
	var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(!emailReg.test($email)) {
		return false;
	} else {
		return true;
	}
}

function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}


$(document).ready(function() {

	/*
	 $('#subscribe_email_field').blur(function() {
	 $(this).val('Informujte ma o spustení na tento e-mail');
	 });
	 */

	$('#subscribe_email_field').focus(function() {
		$(this).val('');
	});

	//$('#extended_info').hide();
	$('#how_it_works_button').click(function() {
		$('#extended_info').slideDown('slow');
		$('html, body').animate({
			scrollTop : $("#anchor").offset().top
		}, 1000);

	})

	$('#notify_button').click(function() {
		$('.form_subscribe').children().fadeOut(400);
		return false;
	});
	
	$('#how_it_works_button').click(function() {
		$('#how_it_works_pressed').val(1);
	});

	$('.inform_me').click(function() {
		
		var category = $(this).attr('id');
		
		$('#customer_category').val(category);
		
		var data = $('#notify_form').serialize();

		var email = $("#subscribe_email_field").val();
		
		if(validateEmail(email)) {
			
			var data = $('#notify_form').serialize();
			
			$.post('/core/signin', data, function(data, status, jqxhr) {
				$("#subscribe_email_field").val('Ďakujeme za prejavený záujem !');
			});
			
			
			
		} else {
			alert("Zadajte platnú emailovú adresu !");
		}

	});
	
	$('#notify_form').submit(function() {
		return false;
	});

	$('.send_question_button').click(function() {
		var email = $('#email_question').val();

		if(email != null && validateEmail(email)) {

			var data = $('#contact_form').serialize();
			var message = $('#message_text').val();

			if(message != null && trim(message) != '') {
				$.post('/core/contact', data, function (data, textStatus, jqXHR) {
					confirm("Vaša tázka bola odoslaná.");
					$('#contact_form')[0].reset();	
				});
				
			} else {
				alert('Zadajte znenie správy !');
			}
		} else {
			alert("Zadajte platnú emailovú adresu !");
		}

		return false;
	});
})
