$('.contact_form').ready(
	function(){
		
		
		//TODO: checkboxes
		$('#contact_phone').click(
			function(){

			}
		)
		$('#contact_email').click(
			function(){
			
			}
		)
		
		
		$('.kontakt_button').click(
			function(){
				var _parent = $(this).parent();
				
				var errors = 0;
				
				$(document).find('.required_input_check').each(
					function(){
						if( $(this).val().length == 0){
							errors++;
							$('.'+$(this).attr('handle') ).addClass('red');
						}else{
							$( '.'+$(this).attr('handle') ).removeClass('red');
						} 
					}
				)
				$(document).find('.required_textarea_check').each(
					function(){
						if( $(this).val().length == 0){
							errors++;
							$('.'+$(this).attr('handle') ).addClass('red');
						}else{
							$( '.'+$(this).attr('handle') ).removeClass('red');
						} 
					}
				)
				
				//send
				if(errors == 0){
					_parent.html( $('#loading').html() );
					
					//create msg
					var msg = "";
					msg += 'Jméno: '+$('input[name="name"]').val()+"\r\n";
					if( $('input[name="email"]').val().length != 0 ){
						msg += 'E-mail: '+$('input[name="email"]').val()+"\r\n";
					}	
					if( $('input[name="phone"]').val().length != 0 ){
						msg += 'Telefon: '+$('input[name="phone"]').val()+"\r\n";
					}	
					msg += 'Zpráva: '+$('textarea[name="msg"]').val()+"\r\n\r\n";
					if( $('#contact_phone').is(':checked') ){
						msg += 'Prosím o kontaktování telefonicky.'+"\r\n";
					}
					if( $('#contact_email').is(':checked') ){
						msg += 'Prosím o kontaktování e-mailem.'+"\r\n";
					}
					
					
					
					var processUrl = $(document).find('form').attr('action');
					$.ajax({ 
            		    type: "POST", 
		                url : processUrl, 
		                cache: false, 
		                processData: false, 
		                data: "msg=" + encodeURIComponent(msg), 
		                success: sendSuccess(_parent)
			        }); 	
				}else{
					$('.required_error').show();
				}
			}
		)
	}
);

function sendSuccess(_parent)  {
	_parent.html( '<span class="blue">Vaše zpráva <br>byla úspěšně odeslána.</span>' );
}


