function doWebboardSubmit(xhr) {
	try {
		var datas = xhr.responseText.toJSON();
		for (var prop in datas[0] ) {
			val = datas[0][prop];
			if (prop == 'error') {
				alert(val);
			} else if (prop == 'location') {
				window.location = val.replace(/&amp;/g, '&');
			} else if (prop == 'input' && $E(val)) {
				$E(val).focus();
				new GHighlight(val).play();
			};
		};
	}catch(e){
		alert(xhr.responseText);
	};
};

function intWebboard(elem , imgsize) {
	var patt = /delete-([0-9]+)-([0-9]+)/
	forEach($E(elem).getElementsByTagName('a'), function(item){
		if(hs = patt.test(item.id)) {
			$G(item).addEvent('click', doWebboardDelete);
		};
	});
	if (imgsize > 0) {
		$G(elem).Ready(function(){
			forEach($E(elem).getElementsByTagName('img'), function(item){
				if (item.className == 'picture') {
					new preload(item, function(){
						if (this.width > imgsize) {
							this.height = ((parseFloat(this.height) * imgsize) / parseFloat(this.width));
							this.width = imgsize;
						};
					});
				};
			});
		});
	};
};

var doWebboardDelete = function(event) {
	GEvent.stop(event);
	var patt = /delete-([0-9]+)-([0-9]+)/
	var hs = patt.exec(GEvent.element(event).getAttribute('id'));
	if (hs) {
		if (hs[2] == 0 && confirm(WEBBOARD_COMFIRM_DELETE_QUESTION)) {
			var query = 'action=delete&qid=' + hs[1];
		} else if (hs[2] > 0 && confirm(WEBBOARD_COMFIRM_DELETE_ANSWER)) {
			var query = 'action=delete&qid=' + hs[1] + '&rid=' + hs[2];
		} else {
			return false;
		};
		var req = new GAjax();
		req.inintLoading('wait', true);
		req.send('modules/webboard/action.php' , query , function(xhr) {
			var ds = xhr.responseText.split( '|' );
			if (ds.length > 1) {
				if (ds[1] == 0) {
					$G('R_' + ds[0]).remove();
				} else {
					$G('R_' + ds[1]).remove();
				};
			} else {
				alert(ds[0]);
			};
		});
	};
};

var last_news;
function webboardNews(id, time) {
	last_news = 0;
	var news_interval = time || 60; // auto refresh news every 1 miniute
	var news_board = new GAjax()
	news_board.autoupdate('modules/webboard/getnews.php', news_interval, function(){return 'time=' + last_news}, function(xhr){
		var ds = xhr.responseText.split('|');
 		if ($E(id)) {
	 		if (ds.length == 2) {
	 			var news = $E(id);
		 		news.innerHTML = ds[0];
		 		last_news = ds[1];
		 		if (loader) {
		 			loader.inint(news);
		 		};
		 	};
 		} else {
 			news_board.abort();
 		};
	});
};
