document.write('<script type="text/javascript" src="http://www.sound-uz.jp/php/js/bbsCreate.js"></script>');

var baseUrl = 'http://www.sound-uz.jp/php/bbs/';

/*
 * BBS Form
 */
var validat = { };
validat.title = function ( value ) {
	if ('' == value) { 
		return 'タイトルが未記入です。';
	}
	if (40 < value.length) {
		return 'タイトルは全角40文字以内で記入下さい。';
	}
	if (-1 != value.search(/[\cx\r\n\r\t\0]/)) {
		return 'タイトルに不正な文字が含まれています。';
	}
	return '';
};
validat.comment = function ( value ) {
	if ('' == value) {
		return 'コメント（本文）が未記入です。';
	}
	if (12000 < value.length) {
		return 'コメントは全角4000文字まで記入下さい。';
	}
	if (-1 != value.search(/[\cx\0]/)) {
		return 'コメントに不正な文字が含まれています。';
	}
	return '';
};
validat.username = function ( value ) {
	if ('' == value) {
		return 'ハンドルネームが未記入です。';
	}
	if (20 < value.length) {
		return 'ハンドルネームは全角20文字以内で記入下さい。';
	}
	if (-1 != value.search(/[\cx\r\n\r\t\0]/)) {
		return 'ハンドルネームに不正な文字が含まれています。';
	}
	return '';	
};
validat.email = function ( value ) {
	if ('' == value) {
		return 'メールアドレスが未記入です。';
	}
	if (-1 != value.search(/[^@\w\.\/-]/)) {
		return 'メールアドレスに不正な文字が含まれています。';
	}
	if (-1 == value.search(/^[\w\.\/-]+@[\w\.\/-]+\.[a-z]{2,4}$/)) {
		return 'メールアドレスの記述を確認してください。';
	}
	return '';
};
validat.password = function ( value ) {
	if ('' == value) {
		return 'パスワードが未記入です。';
	}
	if (-1 != value.search(/[^0-9a-z]/i)) {
		return 'パスワードに不正な文字が含まれています。';
	}
	return '';
};
validat.url = function ( value ) {
	if ('' == value) {
		return false;
	}
	if (-1 != value.search(/[\cx\r\n\r\t\0]/)) {
		return 'ホームページに不正な文字が含まれています。';
	}
	if (-1 == value.search(/^(http|https|ftp):\/\/[\w\.-]+[a-z]{2,4}(\/[^\s]*)*$/i)) {
		return 'ホームページの記述を確認してください。';
	}
	return '';
};
validat.emailsend = function ( value ) {
	return '';
};

function validateBbsForm ( type )
{
	var items;
	if ('reply' == type || 'edit' == type || 'create' == type) {
		items = ['title', 'comment', 'username', 'email', 'password', 'url', 'emailsend'];
	} else if ('remove' == type) {
		items = ['email', 'password'];
	} else {
		alert('不正なリクエスト');
		return false;
	}

	var len = items.length;
	var messages = new Array();
	for (var n = 0; n < len; n++) {
		var field = 'bbsForm_' + items[n];
		Element.setStyle(field, {'backgroundColor':'#ffffff'});
		mess = validat[items[n]]($F(field));
		if ('' != mess) {
			Element.setStyle(field, {'backgroundColor':'#f6f0ff'});
			messages.push(mess);
		}
	}
	if (messages.length) {
		alert(messages.join("\n"));
		return false;
	}
	if ('create' == type) {
		$('bbsForm').action = createFormUrl + '/' + createMethod;
	}
	return true;
}

function openBbsForm ( id, mode )
{
	var bbsFormSpace = $('bbsFormSpace');
	if (!bbsFormSpace) {
		return;
	}
	if ('reply' != mode && 'edit' != mode && 'remove' != mode) {
		return;
	}
	if (0 >= id) {
		return;
	}

	Element.update('bbsFormSpace', '<div class="color-gray"><img src="/php/images/icon-load-w18h18.gif" width="18" height="18" alt="ロード中" align="absmiddle" /> ロード中・・・</div>');
	var contents = '<div class="color-red">フォームの獲得に失敗しました</div>';
	var url = baseUrl + mode + '/open/' + id;
	var a = new Ajax.Updater(
		bbsFormSpace,
		url,
		{
			'method': 'get',
			onSuccess:  function(request) { 
				Element.update('bbsFormSpace', request.responseText);
			},
			onComplete: function(request) {
			},
			onFailure:  function(request) {
				Element.update('bbsFormSpace', contents);
			},
			onException:  function(request) {
				Element.update('bbsFormSpace', contents);
			}
		}
	);
}

function closeBbsForm ( mode )
{
	var bbsFormSpace = $('bbsFormSpace');
	if (!bbsFormSpace) {
		return;
	}
	if ('reply' != mode && 'edit' != mode && 'remove' != mode) {
		return;
	}

	Element.update('bbsFormSpace', '<div class="color-gray"><img src="/php/images/icon-load-w18h18.gif" width="18" height="18" alt="処理中" align="absmiddle" /> 処理中・・・</div>');
	var url = baseUrl + mode + '/close';
	var a = new Ajax.Updater(
		bbsFormSpace,
		url,
		{
			'method': 'get',
			onSuccess:  function(request) { 
				Element.update('bbsFormSpace', '');
			},
			onComplete: function(request) {
				Element.update('bbsFormSpace', '');
			},
			onFailure:  function(request) {
				Element.update('bbsFormSpace', '');
			},
			onException:  function(request) {
				Element.update('bbsFormSpace', '');
			}
		}
	);
}


function redirect ( id, mode )
{
	if ('reply' != mode && 'edit' != mode && 'remove' != mode) {
		return;
	}
	if (0 >= id) {
		return;
	}
	window.location.href = baseUrl + mode + '/' + id;
}


