$(function(){	
	$("#juitterContainer").tweet({
          join_text: "auto",
          username: "kmgroup",
          avatar_size: 48,
          count: 5,
          auto_join_text_default: "we said,", 
          auto_join_text_ed: "we",
          auto_join_text_ing: "we were",
          auto_join_text_reply: "we replied",
          auto_join_text_url: "we were checking out",
          loading_text: "Загрузка сообщений..."
        });
})
function UserForm(lang){	
	$.ajax({
		url: '/register/renderUserForm',		
		data: 'lang='+lang,
		type:'POST',
		success: function(response){
			$("#add_anchor").remove();
			$('#register_user').append(response);
		}
	})
}

function deleteUserFrom(code,lang){	
	$("#"+code).remove();
	var count = $(".user_from").size();
	if(count == 0){
		$("#register_info").prepend('<a id="add_anchor" style="margin-left:212px" onclick="UserForm(\''+lang+'\')">Добавить участника</a>');
	}
}

function ProcessRegistration(){
	$("#lock").val('false');
	$(".error_message").hide();
	$(".fio").each(function(index){		
		ValidateProcess(this);
	})
	$(".post").each(function(index){		
		ValidateProcess(this);
	})
	$(".work_phone").each(function(index){		
		ValidateProcess(this);
	})
	$(".mobile_phone").each(function(index){		
		ValidateProcess(this);
	})
	$(".email").each(function(index){		
		ValidateProcess(this);
	})
	var lock = $("#lock").val();
	if(lock == 'false'){	
	var form =  $("#register_form");
		$.ajax({
			url: form.attr('action'),
			type: form.attr('method'),
			data: form.serialize(),
			beforeSend: function(){
				
			},
			success: function(response){
				var data = eval('('+response+')');			
				if(data.status == 'ok'){				
					$(".registration_form").html(data.message);				
				}
			}
		})
	}
}

function ValidateProcess(el){
	if($(el).val() == ''){
			$(el).addClass('error_input').focus().next().show();			
			$(el).prev().addClass('error_label');
			$(".error_message").show();
			$("#lock").val('true');
		}else {
			$(el).removeClass('error_input').prev().removeClass('error_label');
		}
}
function Register(el){
	var form = $('#register').serialize();
	var name = $('#name');
	var surname = $('#surname');
	var phone = $('#phone');
	var email = $('#email');
	
	if(email.val() == ''){		
		email.focus();
	}else{
		if(check_email(email.val())){
			$.ajax({
				type: 'POST',
				url :'/home/register',
				data: form,
				beforeSend: function(){
					$('#submit').hide();
					$(el).after('<img id="ajax_indicator" style="margin-right:141px" src="/km_style/css/images/register_ajax.gif">');	
				},
				
				error: function(){
					$("#ajax_indicator").remove();
				},
				success: function(response){
					if(response == true){
						$("#ajax_indicator").remove();
						$('.register').text('Ваша заявка добавлена в базу');
					}else $('.register').text('Произошла ошибка.');
				}
			})
		}
		else email.focus();
	}
}

function check_email(email) {
	var template = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z])+$/;
	email = drop_spaces(email); //функцию drop_spaces() см. выше
	if (template.test(email)) {
		return true;
	}
	return false; 
}

function drop_spaces(str) {
	var newstr = trim(str); 
	return newstr.replace(/(\s)+/g, ""); 
}
				
function trim(str) {
	var newstr = str.replace(/^\s*(.+?)\s*$/, "$1");
	if (newstr == " ") {
		return "";
	}
	return newstr;
}
