$('document').ready(function(){
	
	var options = {
	
		url: '/products/sendreports',
		type: 'POST',
		success: showResponse
	};
	
	$('#getReport').validate({
		
		focusCleanup: false,
		
		submitHandler: function(form) { 
				
			$('.getReports').hide();
			if ($('.reportsBlock').css('display') == 'block') {
				
				$(form).ajaxSubmit(options);
				return false;
			}
			else {
			
				$('.reportsBlock').css('display', 'block');
			}
		},
		
		rules: {
			name: { required: true },
			email: { required: true, email: true }
		},
		errorPlacement: function(error, element) {
			
			var er = element.attr("name");
		    element.parent().find('#' + er + 'Error').html(error.html());
		},
		messages: {
			name: "Laukas privalomas",
			email: { required: "Laukas privalomas", email: "Blogas el. paštas" }
		},
		success: function(label) { }
	});
	
	$('#callme').click(function(){
		
		if ($(this).is(':checked')){
		
			$('#telephoneTr').show();
		}
		else {
		
			$('#telephoneTr').hide();
		}
		
	});
	
	var optionsIndex = {
		url: '/ajax/addemail',
		type: 'POST',
		success: showResponseIndex
	};
	
	$('#newsletter').validate({
		
		focusCleanup: false,
		
		submitHandler: function(form) {
		
				$(form).ajaxSubmit(optionsIndex); 
				
				return false;
			},
		
		rules: {
			email: {
				required: true,
				email: true
			}
			
			
		},
		errorPlacement: function(error, element) {
		      var er = element.attr("name");
		      
		      $('#formError').html(error.html());
		    //  error.appendTo( element.parent().parent().find('.errorBlock') );
	              
		 },
		 success: function(label){

		      $('#formError').html('');
         }
	});
	
});
function showResponse(responseText, statusText, xhr, $form){
	
	$('.reportsBlock').hide();
	$('#responseText').css('padding-bottom', '20px');
	$('#responseText').html(responseText);
	 
	
}
function showResponseIndex(responseText, statusText, xhr, $form){
	
	$('#formError').html(responseText);
	
}
