$(document).ready(function() {
    $('#nick-btn').click(function(){
		getData();
    });
	$("#nickname-input").keyup(function(event){
		if(event.keyCode == 13){
			getData();
		}
	});
});

// TODO Доделать!


function getData() {
	if (($('#nickname-input').val() == '')) {
			alert("Не указан ник!");
	} else {
		var username = $('#nickname-input').val().trim();						
		$.ajax({
			url:"main/templates/getData1.php",			
			type: 'POST',
			data: {user: username},
			success: function(msg){ 
				if (!(msg == '')) {								
					$('#wt1').empty();		
					$('#wt1').css("display","block");
					$('#wt1').css("display","block");
					$('#wt1').append(msg);
				} else {
					console.log(msg);
					alert('Пользователь не найден!');
					return;
				}
			}
		});
		$.ajax({
			url:"main/templates/getData2.php",			
			type: 'POST',
			data: {user: username},
			success: function(msg2){ 
				if (!(msg2 == '')) {
					$('#wt2').empty();		
					$('#wt2').css("display","block");					
					$('#wt2').append(msg2);
				} else {
					console.log(msg2);
					alert('Пользователь не найден!');
					return;
				}
			}
		});
	}		
	window.setTimeout(function(){TodayIsTheDay();}, 2000);
}

function TodayIsTheDay(){
	var now = new Date(); 
	var sNow = now.toString();
	let nowDate = parseDate(sNow);
	let headers = document.getElementsByClassName('day-name');
	for (let i = 0; i < headers.length; i++){
		let sDate = getDate(headers[i].textContent);
		if (sDate === nowDate) {
			headers[i].setAttribute('style', 'text-shadow: 0 0 10px #609A21,0 0 10px #609A21,0 0 30px #609A21,0 0 10px #609A21;	');
		}
	}
}

function getDate(sText){
	var textC = Array.from(sText);
	var sDate = "";
	var record = false;
	for (var i = 0; i < textC.length; i++){
		if (textC[i] === '['){
			record = true;
		} else if (textC[i] === ']') {
			record = false;
		}
		if (record){
			sDate = sDate + textC[i];
		}
	}
	sDate = sDate.replace('[', '');
	sDate = sDate.replace(']', '');
	return sDate;
}

function parseDate(sDate){
	var dateArr = sDate.split(' ');
	var day = dateArr[2];
	var sMonth = dateArr[1];
	var month = '';
	if (sMonth == 'Jan') {
		month = '01';
	} else if (sMonth == 'Jan') {
		month = '01';
	} else if (sMonth == 'Feb') {
		month = '02';
	} else if (sMonth == 'Mar') {
		month = '03';
	} else if (sMonth == 'Apr') {
		month = '04';
	} else if (sMonth == 'May') {
		month = '05';
	} else if (sMonth == 'Jun') {
		month = '06';
	} else if (sMonth == 'Jul') {
		month = '07';
	} else if (sMonth == 'Aug') {
		month = '08';
	} else if (sMonth == 'Sep') {
		month = '09';
	} else if (sMonth == 'Oct') {
		month = '10';
	} else if (sMonth == 'Nov') {
		month = '11';
	} else if (sMonth == 'Dec') {
		month = '12';
	} else {
		month = '01';
	}
	var year = dateArr[3];
	var date = day + '.' + month + '.' + year;
	return date;
}