/**********************************************************************
* /js/component/dictionary-creation-create-dictionary.js
* Copyright (C) 2007-2008 Kyoto University
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-
* 1301  USA
***********************************************************************/

//* クラスの定義
var DeployDictionaryService = Class.create();
var DownloadDictionaryService = Class.create();

//* 各種定数の定義
var ID_MESSAGE_AREA_DEPLOY = 'deploy-dictionary-service-message-area';
var ID_MESSAGE_AREA_DOWNLOAD = 'download-dictionary-service-message-area';

var ID_DICTIONARY_NAME_DEPLOY = 'deploy-dictionary-service-name';
var ID_DICTIONARY_ID_DEPLOY = 'deploy-dictionary-service-id';
var ID_DICTIONARY_ID_DOWNLOAD = 'download-dictionary-service-id';

var ID_CREATE_DICT_BUTTONS = 'deploy-dictinary-create-buttons';

var ID_OK_DEPLOY = 'deploy-dictionary-service-ok';
var ID_OK_DOWNLOAD = 'download-dictionary-service-ok';

//* 辞書デプロイエリアの大枠のクラス
DeployDictionaryService.prototype = {

	//* 初期化処理
	initialize: function(){

		//* 各種パーツのDOMオブジェクトを取得
		this.messageArea = $(ID_MESSAGE_AREA_DEPLOY);
		this.dictionaryServiceName = $(ID_DICTIONARY_NAME_DEPLOY);
		this.dictionaryServiceID = $(ID_DICTIONARY_ID_DEPLOY);
		this.createButtons = $(ID_CREATE_DICT_BUTTONS);

		//* "Deploy"ボタンにイベントを割り当て
		Event.observe($(ID_OK_DEPLOY), 'click', this.callDeploy.bind(this));
		//* メッセージエリアを隠す
		this.hideLoading();

		// eip kawauchi add start 20100118
		// Download Dictionary Serviceのテキストボックスで
		// Enterキーを押すと遷移してしまうバグを修正
		Event.observe($(ID_DICTIONARY_ID_DOWNLOAD), "keypress", this.cancelSubmit.bind(this));
		// eip kawauchi add end 20100118


	},

	// eip kawauchi add start 20100118
	// 押されたキーがenterなら、イベントをキャンセル
	cancelSubmit: function(evt){
		var enterCode = 13;
		if (evt.keyCode == enterCode) {
			if (window.execScript) event.returnValue = false;
			else evt.preventDefault();
		}
	},
	// eip kawauchi add end 20100118

	callDeploy: function(){

		var id = this.dictionaryServiceID.value;
		//* 既に同IDの辞書がデプロイされていた場合，上書きするか確認
		var table = undeployDicitonaryService.dataTable.cellsArray;
		for(var i=0; i<table.length; i++){
			if(table[i][1] == id && !confirm('"' + id + '" already exists on the Playground server.\n\nOverwrite dictionary service?'))
			return;
		}

		downloadDictionaryService.finishLoading("");
		undeployDicitonaryService.finishLoading("");
		this.startLoading("Now deploying...");

		//* 実際のデプロイを行うメソッドに渡す変数を準備
		var name = this.dictionaryServiceName.value;
		var id = this.dictionaryServiceID.value;
		var dataTable = editDictionary.getDictioanryAsMatrix(); //* 見出し含め、テーブル全体を二次元配列で表したもの
		var languages = dataTable[0];
		var data = dataTable.slice(1, dataTable.length);
		var filename = editDictionary.getFileName();
		var makeicon = this.createButtons.checked ? true : false;
		//* 入力する値のチェック
		if (id.length < 1 || name.length < 1) {
			// mod start kitajima 20090821
//			this.finishLoading('<font style="color:#FF0000">Dictionaly service name/id cannot be empty.</font>');{
			this.finishLoading('<font style="color:#FF0000">Dictionary service name/id cannot be empty.</font>');
			// mod end kitajima 20090821
			return;
		} else if(!this.isValidDictName(name)){
			// mod start kitajima 20090821
//			this.finishLoading('<font style="color:#FF0000">Dictionaly service name can contain only(one-byte)English characters,numerals,undersore "_", hyphen "-" and space " ".</font>');
			this.finishLoading('<font style="color:#FF0000">Dictionary service name can contain only(one-byte)English characters,numerals,undersore "_", hyphen "-" and space " ".</font>');
			// mod end kitajima 20090821
			return;
		} else if(!this.isValidDictID(id)){
			// mod start kitajima 20090821
//			this.finishLoading('<font style="color:#FF0000">Dictionaly service id can contain only(one-byte)English characters,numerals,undersore "_" and hyphen "-".</font>');
			this.finishLoading('<font style="color:#FF0000">Dictionary service id can contain only(one-byte)English characters,numerals,undersore "_" and hyphen "-".</font>');
			// mod end kitajima 20090821
			return;
		} else if(data.length < 1){
			// mod start kitajima 20090821
//			this.finishLoading('<font style="color:#FF0000">Dictionaly cannot be empty.</font>');
			this.finishLoading('<font style="color:#FF0000">Dictionary cannot be empty.</font>');
			// mod end kitajima 20090821
			return;
		}

		try {
			var wsdlAddress = DictionaryManager.deployDictionary(name, id, languages, data, filename, makeicon);


			//* 長い文字列が途中で改行されるように、
			//* 全ての文字の間に<wbr>を挿入する。
			var breakableTxt = "";
			for(var charCount=0; charCount<wsdlAddress.length; charCount++)
				breakableTxt += wsdlAddress.charAt(charCount) + "<wbr />";

			this.finishLoading('Dictionary service was successfully deployed as<br />'
				+ '<font style="color:#000080">' + breakableTxt + '</font>');
			undeployDicitonaryService.refreashDictionaryTable();
		} catch(e){
			if(e == 'User has not login status.'){
				this.finishLoading("<font style='color:#FF0000'>You should login before deploying dictionary service.</font>");
			} else {
				this.finishLoading("<font style='color:#FF0000;'>Playground Error: </font>The dictionary was not deployed correctly. Please try again later.\n"
					+ "Description: " + e);
			}
		}
	},

	//* 表示用
	startLoading : function( msg ) {
		this.messageArea.show();
		var area = this.messageArea;
		while ( area.firstChild ) area.removeChild( area.firstChild );
		if ( msg ) {
			area.innerHTML = msg;
		}
		var img = new Image();
		img.src = 'img/anime/ajax-loader3.gif';
		area.appendChild( img );
	},

	finishLoading : function( msg ) {
		this.messageArea.show();
		var area = this.messageArea;
		while ( area.firstChild ) area.removeChild( area.firstChild );
		if ( msg ) {
			area.innerHTML = msg;
		}
	},

	hideLoading : function(){
		this.messageArea.hide();
	},

	isValidDictName: function(fileName){

		var pattern = /^([a-zA-Z0-9_-]| )+$/; //* 半角英数字および"-","_"," "

		if (fileName.toString().match(pattern) == null)
			return false;
		else
			return true;
	},

	isValidDictID: function(fileName){

		var pattern = /^[a-zA-Z0-9_-]+$/; //* 半角英数字および"-","_"

		if (fileName.toString().match(pattern) == null)
			return false;
		else
			return true;
	}
};

//* 辞書ダウンロードエリアの大枠のクラス
DownloadDictionaryService.prototype = {

	//*初期化する際に、辞書のリストと対応言語を取得
	initialize: function(){

		//* 各種パーツのDOMオブジェクトを取得
		this.messageArea = $(ID_MESSAGE_AREA_DOWNLOAD);
		this.dictionaryServiceID = $(ID_DICTIONARY_ID_DOWNLOAD);

		//* "Download"ボタンにイベントを割り当て
		Event.observe($(ID_OK_DOWNLOAD), 'click', this.callDownload.bind(this));

		//* メッセージエリアを隠す
		this.hideLoading();
	},

	callDownload: function(){

		deployDictionaryService.finishLoading("");
		undeployDicitonaryService.finishLoading("");
		this.startLoading("Preparing for download.");

		//* 実際のダウンロードを行うメソッドに渡す変数を準備
		var id = this.dictionaryServiceID.value;
		var dataTable = editDictionary.getDictioanryAsMatrix(); //* 見出し含め、テーブル全体を二次元配列で表したもの
		var languages = dataTable[0];
		var data = dataTable.slice(1, dataTable.length);

		if (id.length < 1) {
			// mod start kitajima 20090821
//			this.finishLoading('<font style="color:#FF0000">Dictionaly service name/id cannot be empty.</font>');
			this.finishLoading('<font style="color:#FF0000">Dictionary service name/id cannot be empty.</font>');
			// mod end kitajima 20090821
			return;
		} else if(!this.isValidDictName(id)){
			// mod start kitajima 20090821
//			this.finishLoading('<font style="color:#FF0000">Dictionaly service name can contain only(one-byte)English characters,numerals,undersore "_", hyphen "-" and space " ".</font>');
			this.finishLoading('<font style="color:#FF0000">Dictionary service name can contain only(one-byte)English characters,numerals,undersore "_", hyphen "-" and space " ".</font>');
			// mod end kitajima 20090821
			return;
		} else if(data.length < 1){
			// mod start kitajima 20090821
//			this.finishLoading('<font style="color:#FF0000">Dictionaly cannot be empty.</font>');
			this.finishLoading('<font style="color:#FF0000">Dictionary cannot be empty.</font>');
			// mod end kitajima 20090821
			return;
		}

		try {
			DictionaryManager.downloadDictionary(id, languages, data);
			this.finishLoading('');
		} catch(e){
			if(e == 'User has not login status.'){
				this.finishLoading("<font style='color:#FF0000'>You should login before downloading dictionary service.</font>");
			} else {
				this.finishLoading("<font style='color:#FF0000;'>Playground Error: </font>The dictionary was not downloaded correctly. Please try again later.\n"
					+ "Description: " + e);
			}
		}
	},

	//* 初期化処理
	startLoading : function( msg ) {
		this.messageArea.show();
		var area = this.messageArea;
		while ( area.firstChild ) area.removeChild( area.firstChild );
		if ( msg ) {
			area.innerHTML = msg;
		}
		var img = new Image();
		img.src = 'img/anime/ajax-loader3.gif';
		area.appendChild( img );
	},

	finishLoading : function( msg ) {
		this.messageArea.show();
		var area = this.messageArea;
		while ( area.firstChild ) area.removeChild( area.firstChild );
		if ( msg ) {
			area.innerHTML = msg;
		}
	},

	hideLoading : function(){
		this.messageArea.hide();
	},

	isValidDictName: function(fileName){

		var pattern = /^[a-zA-Z0-9_-]+$/; //* 半角英数字および"-","_"

		if (fileName.toString().match(pattern) == null)
			return false;
		else
			return true;
	}
};

