function doSelectedBoard( sel )
{
	document.location = 'index.php?module=webboard&wbid=' + sel.value;
};

function doWebboardPost( pForm )
{
	var req = new GForm( pForm );
	req.inintLoading( 'wait' , true );
	req.submit(function(xhr){
		var datas = req.toJSON( xhr.responseText );
		if ( datas[0].error != '' )
		{
			alert( decodeURIComponent( datas[0].error ) );
			if ( datas[0].input != '' )
			{
				new Highlight( datas[0].input ).play();
				document.getElementById( datas[0].input ).focus();
			};
		}
		else
		{
			var req2 = new GAjax();
			req2.inintLoading( 'wait' , true );
			req2.send( 'getcontent.php' , 'module=webboard/webboard&wbid=' + datas[0].id , function(xhr){
				var data = xhr.responseText;
				var el = $( 'content' ).elem;
				el.innerHTML = data;
				Object.evalScript( data );
				alert( decodeURIComponent( datas[0].alert ) );
			});
		};
	});
	return true;
};

function doWebboardReply( pForm )
{
	var req = new GForm( pForm );
	req.inintLoading( 'wait' , true );
	req.submit(function(xhr){
		var datas = req.toJSON( xhr.responseText );
		if ( datas[0].error != '' )
		{
			alert( decodeURIComponent( datas[0].error ) );
			if ( datas[0].input != '' )
			{
				new Highlight( datas[0].input ).play();
				document.getElementById( datas[0].input ).focus();
			};
		}
		else
		{
			var req2 = new GAjax();
			req2.inintLoading( 'wait' , true );
			req2.send( 'getcontent.php' , 'module=webboard/view&visited=1&id=' + datas[0].id , function(xhr){
				var data = xhr.responseText;
				var el = $( 'content' ).elem;
				el.innerHTML = data;
				Object.evalScript( data );
				alert( decodeURIComponent( datas[0].alert ) );
			});
		};
	});
	return true;
};

function doSticky( e )
{
	var req = new GAjax();
	req.send( 'webboard/action.php' , 'action=sticky&id=' + e.className , function(xhr) {
		var datas = req.toJSON( xhr.responseText );
		if ( datas[0].error != '' )
		{
			alert( datas[0].error );
		}
		else
		{
			var img = $( 'S_' + e.className ).elem;
			img.src = datas[0].pin;
			img.title = datas[0].pintitle;
			var img = $( 'I_' + e.className ).elem.className = datas[0].icon;
		};
	});
};

function doForumDeleteQuestion( id )
{
	if ( confirm( 'คุณต้องการจะลบคำถามนี้ และคำตอบของคำถามนี้ทั้งหมด ?' ) )
	{
		var req = new GAjax();
		req.send( 'webboard/action.php' , 'action=deleteq&id=' + id , function(xhr) {
			var datas = req.toJSON( xhr.responseText );
			if ( datas[0].error != '' )
			{
				alert( datas[0].error );
			}
			else
			{
				$( 'B_' + id ).remove();
			};
		});
	};
};

var temp = new Array;
var contentW = 515;
var imgs;

function WebboardAutoResizeImg( objstr )
{
	var content = document.getElementById( objstr );
	imgs = content.getElementsByTagName( 'img' );
	for ( var i = 0 ; i < imgs.length ; i++ )
	{
		var name = imgs[i].src;
		if ( typeof temp[name] == "undefined" )
		{
			temp[name] = new Image();   
			temp[name].src = name;
		};
		WebboardPreload( i , name );
	};
};

function WebboardPreload( id , name )
{
	if ( temp[name].complete )
	{
		if ( temp[name].width > contentW )
		{
			imgs[id].height = ( ( parseFloat( temp[name].height ) * contentW ) / parseFloat( temp[name].width ) );
			imgs[id].width = contentW;
		};
	}
	else
	{
		 setTimeout( "WebboardPreload(" + id + ",'" + name + "')" , 50 );
	};
};

function doWebboardDeleteReply( id )
{
	if ( confirm( 'คุณต้องการที่จะลบคำตอบนี้' ) )
	{
		var req = new GAjax();
		req.inintLoading( 'wait' , true );
		req.send( 'webboard/action.php' , 'action=deleter&id=' + id , function(xhr){
			var datas = req.toJSON( xhr.responseText );
			if ( datas[0].error != '' )
			{
				alert( datas[0].error );
			}
			else
			{
				$( 'FR_' + id ).remove();
			};
		});
	};
};
