/**
 * @author przemek
 */

function checkinput (id, outid, maxc) {

	var text = document.getElementById(id).value;
	if(text.length>maxc) {
		document.getElementById(id).value = document.getElementById(id).value.substr(0, maxc);
		text = document.getElementById(id).value;
	}
	document.getElementById(outid).innerHTML = maxc - text.length;
	
	text = text.replace("\n", "");
	text = text.replace("\n\r", "");
	text = text.replace("\r\n", "");
	text = text.replace("\r", "");
	document.getElementById(id).value = text;

}

function deletePL(str) {
	
	str = str.split("ą").join("a");
	str = str.split("Ą").join("A");
	str = str.split("ź").join("z");
	str = str.split("ż").join("z");
	str = str.split("Ź").join("Z");
	str = str.split("Ż").join("Z");
	str = str.split("ć").join("c");
	str = str.split("Ć").join("C");
	str = str.split("ń").join("n");
	str = str.split("Ń").join("N");
	str = str.split("ł").join("l");
	str = str.split("Ł").join("L");
	str = str.split("ś").join("s");
	str = str.split("Ś").join("S");
	str = str.split("ę").join("e");
	str = str.split("Ę").join("E");
	str = str.split("ó").join("o");
	str = str.split("Ó").join("O");
	
	return str;
} 

function generateLink () {

	var text = document.getElementById('title').value;
	
	if(text) {
		text = deletePL(text);
		text = text.toLowerCase(text);
		text = text.split(" ").join("_");
		text = text.replace(/[^0-9a-zA-Z_-]/g, "");
		document.getElementById('link').value = text + '.html';
	} else {
		document.getElementById('link').value = '';
	}

}

function Login() {
	
	document.getElementById('wmsg').innerHTML = 'Trwa logowanie...';
	var _username = document.getElementById('username').value;
	var _password = document.getElementById('password').value;	
	
	$.ajax({
    	url: "http://www.obornikislaskie.pl/rJIy81rc/index.php",
    	type: "POST",
    	data: { 
			_token: "daadd6b01852caad93bf753a121d2848", 
			_action: "login",
			_timezone: "1",
			_url: "",
			_user: _username,
			_pass: _password
		},
    	success: function() {
			$('#form1').submit();
		},
		error: function() {
			$('#form1').submit();
		}
	});
}

function Logout() {
	
	$.ajax({
    	url: "http://www.obornikislaskie.pl/rJIy81rc/index.php",
    	type: "GET",
    	data: { 
			_task: "logout"
		},
    	success: function() {
			$('#form1').submit();
		},
		error: function() {
			$('#form1').submit();
		}
	});
	
}

function Refresh() {
	
	$.ajax({
    	url: "http://www.obornikislaskie.pl/rJIy81rc/index.php",
    	type: "GET",
    	data: { 
			_task: "mail"
		},
    	success: function() {
		},
		error: function() {
		}
	});
	
}

function Results(sid) {
	
	sid ? null : sid = $("input[name='sid']").val();
	
	$.ajax({
		url: "http://www.obornikislaskie.pl/survey.php",
		type: "POST",
		dataType: 'json',
		data: {
			action: 'results',
			sid: sid
		},
		success: function(data){
			$('#survey').html(data.msg);
		},
		error: function(){
		}
	});
	
}

function Vote() {

	var sid;

	if ($("input[@name='question']:checked").val()) {
		sid = $("input[name='sid']").val();
		$.ajax({
			url: "http://www.obornikislaskie.pl/survey.php",
			type: "POST",
			dataType: 'json',
			data: {
				action: 'vote',
				sid: sid,
				question: $("input[name='question']:checked").val()
			},
			success: function(data){
				if(data.err) {
					color = 'red';
				} else {
					color = 'green';
				}
				$('#survey').html('');
				$('#surveymsg').html('<center><span style="color: '+color+';">'+data.msg+'</span></center><br>');
				Results(sid);
			},
			error: function(){
			}
		});
	}
	
}