function refresher() {
    var a = parseInt($('#refresh').val());
    if (!/[0-9]{1,2}/.test(a) || a < 5) alert('Invalid refresh rate. Please enter a number between 5 and 99.');
    else {
        sendSay('refresh', 0);
        setTimeout("refresher()", a * 1000)
    }
};

function start() {
    $('#nick').focus(function () {
        if (this.value == 'nick') this.value = ''
    });
    $('#message').focus(function () {
        if (this.value == 'type your message') this.value = ''
    });
    $('#refresh').change(function () {
        refresher()
    });
    $('#width').change(function () {
        chWidth()
    });
    $('#height').change(function () {
        chHeight()
    });
    $('form').attr('autocomplete', 'off');
 //   var a = prompt("Enter your name:");
 //   if (a) {
 //       sendSay('CitiTeamSpeak', '<i>' + a + ' has joined the channel</i>');
 //       $('#nick').val(a)
 //   } else {
 //       sendSay('CitiTeamSpeak', '<i>Unknown has joined the channel</i>')
 //   }
    refresher();
    window.onunload = function () {
//        sendSay('CitiTeamSpeak', '<i>' + $('#nick').val() + ' has left the channel</i>')
    }
};

function chWidth() {
    var w = parseInt($('#width').val());
    if (!/[0-9]{3}/.test(w) || w < 300) alert('Invalid width. Please enter a number between 300 and 999.');
    else {
        $('#output').css('width', w);
        $('form').css('width', w);
        $('#message').css('width', (w - 100))
    }
};

function chHeight() {
    var w = parseInt($('#height').val());
    if (!/[0-9]{3}/.test(w) || w < 100) alert('Invalid height. Please enter a number between 100 and 999.');
    else {
        $('#output').css('height', w);
        sendSay('refresh', 0)
    }
};

function say() {
    if (!$('#nick').val() || $('#nick').val() == 'nick' || $('#nick').val() == 'undefined' || !/[a-zA-Z0-9]{3,20}/.test($('#nick').val())) {
        alert('Enter your nick. 3 chars minimum. Allowed chars: a-z,A-Z,0-9');
        $('#nick').focus();
        return false
    }
    if (!$('#message').val() || $('#message').val() == 'type your message') {
        alert('Enter your message');
        $('#message').focus();
        return false
    }
    sendSay($('#nick').val(), $('#message').val());
    $('#message').val('');
    $('#message').focus();
    return false
};

function updater(a) {
    if (a) {
        $('#output').html(a);
        o = document.getElementById('output');
        o.scrollTop = (o.offsetHeight * 5 + 200)
    }
};

function sendSay(b, c) {
	if (b != "") {
		var d = new Date().getTime();
		$.post("timspik.php?" + d, {
			nick: b,
			msg: c
		}, function (a) {
			updater(a)
		})
	}
};
$(function () {
    start()
});

