/**********************************************************

	「艦これ提督ｂｏｔの設定保存」Ver.1.1.1

					Produced by sakura

	艦これ提督ｂｏｔの現在の設定を、デフォルトの設定に保存します。

**********************************************************/

/* 設定 */
var constrSettingFileName = 'kcsSettingJSON.txt';

function Main(){
	/* ダイアログ表示 */
	function PopupMsg(d_text, d_time, d_type){
		var objWSHShell, n;
		objWSHShell = new ActiveXObject('WScript.Shell');
		n = objWSHShell.Popup(d_text, d_time, WScript.ScriptName.replace(/.js$/i,''), d_type);
		objWSHShell = null;
	}

	/* カレントディレクトリを取得 */
	function GetCurrentDirectory(){
		var objWSHShell;
		objWSHShell = new ActiveXObject('WScript.Shell');
		return(objWSHShell.CurrentDirectory);
	}

	/* 任意の文字コードでテキストファイルの全文字列を取得 */
	function adoLoadText(filename, charset) {
		var stream, text, adTypeText, adReadAll;
		adTypeText = 2;
		adReadAll = -1;
		stream = new ActiveXObject("ADODB.Stream");
		stream.type = adTypeText;
		stream.charset = charset;
		stream.open();
		stream.loadFromFile(filename);
		text = stream.readText(adReadAll);
		stream.close();
		stream = null;
		return(text);
	}

	/* 文字列をByte配列に変換 */
	function ConvertStrToByteArray(fstr, fstrsendenc){
		var stream, adTypeText, adTypeBinary, adReadAll, bytetemp;
		adTypeText = 2;
		adTypeBinary = 1;
		adReadAll = -1;
		stream = new ActiveXObject("ADODB.stream");
		stream.Type = adTypeText;
		stream.Charset = fstrsendenc;
		stream.Open();
		stream.WriteText(fstr);
		stream.Position = 0;
		stream.Type = adTypeBinary;
		if (fstrsendenc == 'UTF-8'){
			stream.Position = 3;
		}
		bytetemp = stream.Read(adReadAll);
		stream.Close();
		stream = null;
		return(bytetemp);
	}

	/* バイナリデータをファイルに保存 */
	function SaveToBinaryFile(fbinary, fstrpath){
		var stream, adTypeBinary, adSaveCreateOverwrite, adModeReadWrite, fe;
		stream = new ActiveXObject("ADODB.stream");
		adTypeBinary = 1;
		adSaveCreateOverwrite = 2;
		adModeReadWrite = 3;
		stream.Type = adTypeBinary;
		stream.Mode = adModeReadWrite;
		stream.Open();
		stream.Write(fbinary);
		try {
			stream.SaveToFile(fstrpath, adSaveCreateOverwrite);
		} catch (fe){
			stream.Close();
			stream = null;
			return(false);
		}
		stream.Close();
		stream = null;
		return(true);
	}

	function GetWindowObject(fstrUri){
		var fobjShell, e, fstrLocationURL
		fobjShell = WScript.CreateObject('Shell.Application');
		for (fi=0; fi<fobjShell.Windows().Count; fi++){
			try {
				fobjShell.Windows().Item(fi).FullName;
			} catch (e){
				continue;
			}
			if ((/iexplore\.exe$/i).test(fobjShell.Windows().Item(fi).FullName)){
				fstrLocationURL = fobjShell.Windows().Item(fi).LocationURL;
				if (fstrLocationURL == fstrUri){
					return(fobjShell.Windows().Item(fi));
				}
			}
		}
		return(null);
	}
	
	function JsonStringify(obj){
		var dic_e = new Object();
		dic_e['\b'] = '\\b';
		dic_e['\f'] = '\\f';
		dic_e['\n'] = '\\n';
		dic_e['\r'] = '\\r';
		dic_e['\t'] = '\\t';
		dic_e['\"'] = '\\"';
		dic_e['\\'] = '\\\\';

		function stringify(obj){
			var i;
			var k;
			var s;
			var r;
			var num;
			var type = typeof(obj);
			if(type == "object"){
				if(!obj){
					return "null";

				// 配列
				}else if(obj.constructor == Array){
					s = "[";
					num = obj.length;
					for(i=0;i < num;i++){
						if(i)	s += ",";
						s += stringify(obj[i]);
					}
					return s + "]";

				// オブジェクト
				}else{
					r = false;
					s = "{";
					for(k in obj){
						if(r)	s += ",";
						else	r = true;
						s += stringify(k) + ":" + stringify(obj[k]);
					}
					return s + "}";
				}

			// 文字列
			}else if(type == "string"){
				s = '"';
				num = obj.length;
				for(i=0;i < num;i++){
					if(dic_e[obj.charAt(i)] != undefined){
						s += dic_e[obj.charAt(i)];
					}else{
						s += obj.charAt(i);
					}
				}

				return s + '"';

			// 数値
			}else if(type == "number"){
				if(isNaN(obj)) return "null";
				if(obj == Number.POSITIVE_INFINITY) return "null";
				if(obj == Number.NEGATIVE_INFINITY) return "null";
				return obj.toString();

			}else if(type == "boolean"){
				return obj.toString();
			}else if(type == "null" || type == "undefined"){
				return "null";
			}
			return null;
		}

		return stringify(obj);
	}
	
	var strCurrentDirectory, strSetting, objSetting, window, document, objBotJSON, strJSONText;
	strCurrentDirectory = GetCurrentDirectory();
	strSetting = adoLoadText(strCurrentDirectory + '\\' + constrSettingFileName, 'Shift_JIS');
	objSetting = eval('\(' + strSetting + '\)');
	window = GetWindowObject('file\:\/\/\/' + strCurrentDirectory.replace(/\\/g, '\/').replace(/ /g, '%20') + '\/' + objSetting.BotFileName);
	if (window != null){
		document = window.document;
		//艦これ提督ｂｏｔの現在の設定読み込む
		objBotJSON = {};
		objBotJSON.autostoponoff = document.getElementById('autostoponoff').checked;
		objBotJSON.autostopday = document.getElementById('autostopday').selectedIndex;
		objBotJSON.autostophour = document.getElementById('autostophour').selectedIndex;
		objBotJSON.keizokukantai2 = document.getElementById('keizokukantai2').checked;
		objBotJSON.keizokukantai3 = document.getElementById('keizokukantai3').checked;
		objBotJSON.keizokukantai4 = document.getElementById('keizokukantai4').checked;
		objBotJSON.kirani = document.getElementById('kirani').checked;
		objBotJSON.kirasan = document.getElementById('kirasan').checked;
		objBotJSON.kirayon = document.getElementById('kirayon').checked;
		objBotJSON.kirabattletime = document.getElementById('kirabattletime').value;
		objBotJSON.kirabattleyuragi = document.getElementById('kirabattleyuragi').value;
		objBotJSON.kirahidan = document.getElementById('kirahidan').selectedIndex;
		objBotJSON.kirahiroudo = document.getElementById('kirahiroudo').value;
		objBotJSON.kiranyukyobattle = document.getElementById('kiranyukyobattle').checked;
		objBotJSON.kiranyukyo = document.getElementById('kiranyukyo').selectedIndex;
		objBotJSON.kirabaketsu = document.getElementById('kirabaketsu').selectedIndex;
		objBotJSON.levelexection = document.getElementById('levelexection').checked;
		objBotJSON.levelingkotei1 = document.getElementById('levelingkotei1').checked;
		objBotJSON.levelingkotei2 = document.getElementById('levelingkotei2').checked;
		objBotJSON.levelingkotei3 = document.getElementById('levelingkotei3').checked;
		objBotJSON.levelingkotei4 = document.getElementById('levelingkotei4').checked;
		objBotJSON.levelingkotei5 = document.getElementById('levelingkotei5').checked;
		objBotJSON.levelingkotei6 = document.getElementById('levelingkotei6').checked;
		objBotJSON.levelingkan1 = document.getElementById('levelingkan1').value;
		objBotJSON.levelingkan2 = document.getElementById('levelingkan2').value;
		objBotJSON.levelingkan3 = document.getElementById('levelingkan3').value;
		objBotJSON.levelingkan4 = document.getElementById('levelingkan4').value;
		objBotJSON.levelingkan5 = document.getElementById('levelingkan5').value;
		objBotJSON.levelingkan6 = document.getElementById('levelingkan6').value;
		objBotJSON.leveledid1 = document.getElementById('leveledid1').value;
		objBotJSON.leveledid2 = document.getElementById('leveledid2').value;
		objBotJSON.leveledid3 = document.getElementById('leveledid3').value;
		objBotJSON.leveledid4 = document.getElementById('leveledid4').value;
		objBotJSON.leveledid5 = document.getElementById('leveledid5').value;
		objBotJSON.leveledid6 = document.getElementById('leveledid6').value;
		objBotJSON.leveledaimlv1 = document.getElementById('leveledaimlv1').value;
		objBotJSON.leveledaimlv2 = document.getElementById('leveledaimlv2').value;
		objBotJSON.leveledaimlv3 = document.getElementById('leveledaimlv3').value;
		objBotJSON.leveledaimlv4 = document.getElementById('leveledaimlv4').value;
		objBotJSON.leveledaimlv5 = document.getElementById('leveledaimlv5').value;
		objBotJSON.leveledaimlv6 = document.getElementById('leveledaimlv6').value;
		objBotJSON.levelformation = document.getElementById('levelformation').selectedIndex;
		objBotJSON.levelbattletime = document.getElementById('levelbattletime').value;
		objBotJSON.levelbattleyuragi = document.getElementById('levelbattleyuragi').value;
		objBotJSON.levelbattlehason = document.getElementById('levelbattlehason').selectedIndex;
		objBotJSON.levelnyukyobattle = document.getElementById('levelnyukyobattle').checked;
		objBotJSON.levelnyukyo = document.getElementById('levelnyukyo').selectedIndex;
		objBotJSON.levelbaketsu = document.getElementById('levelbaketsu').selectedIndex;
		objBotJSON.lvendnenryo = document.getElementById('lvendnenryo').value;
		objBotJSON.lvenddanyaku = document.getElementById('lvenddanyaku').value;
		objBotJSON.lvendkouzai = document.getElementById('lvendkouzai').value;
		objBotJSON.lvendbauxite = document.getElementById('lvendbauxite').value;
		objBotJSON.lvendbaketsu = document.getElementById('lvendbaketsu').value;
		objBotJSON.lvendhiroudo = document.getElementById('lvendhiroudo').value;
		objBotJSON.lvendbattlecount = document.getElementById('lvendbattlecount').value;
		objBotJSON.lvendattaingoal = document.getElementById('lvendattaingoal').checked;
		objBotJSON.lvendshiplimit = document.getElementById('lvendshiplimit').checked;
		objBotJSON.twinkleid1 = document.getElementById('twinkleid1').value;
		objBotJSON.twinkleid2 = document.getElementById('twinkleid2').value;
		objBotJSON.twinkleid3 = document.getElementById('twinkleid3').value;
		objBotJSON.twinkleid4 = document.getElementById('twinkleid4').value;
		objBotJSON.twinkleid5 = document.getElementById('twinkleid5').value;
		objBotJSON.twinkleid6 = document.getElementById('twinkleid6').value;
		objBotJSON.twinkleid7 = document.getElementById('twinkleid7').value;
		objBotJSON.twinkleid8 = document.getElementById('twinkleid8').value;
		objBotJSON.twinkleid9 = document.getElementById('twinkleid9').value;
		objBotJSON.twinkleid10 = document.getElementById('twinkleid10').value;
		objBotJSON.twinkleid11 = document.getElementById('twinkleid11').value;
		objBotJSON.twinkleid12 = document.getElementById('twinkleid12').value;
		objBotJSON.twinkleid13 = document.getElementById('twinkleid13').value;
		objBotJSON.twinkleid14 = document.getElementById('twinkleid14').value;
		objBotJSON.twinkleid15 = document.getElementById('twinkleid15').value;
		objBotJSON.twinkleid16 = document.getElementById('twinkleid16').value;
		objBotJSON.twinkleid17 = document.getElementById('twinkleid17').value;
		objBotJSON.twinkleid18 = document.getElementById('twinkleid18').value;
		objBotJSON.twinkleid19 = document.getElementById('twinkleid19').value;
		objBotJSON.twinkleid20 = document.getElementById('twinkleid20').value;
		objBotJSON.twinkleid21 = document.getElementById('twinkleid21').value;
		objBotJSON.twinkleid22 = document.getElementById('twinkleid22').value;
		objBotJSON.twinkleid23 = document.getElementById('twinkleid23').value;
		objBotJSON.twinkleid24 = document.getElementById('twinkleid24').value;
		objBotJSON.twinkleexection = document.getElementById('twinkleexection').checked;
		objBotJSON.twinklebattletime = document.getElementById('twinklebattletime').value;
		objBotJSON.twinklebattleyuragi = document.getElementById('twinklebattleyuragi').value;
		objBotJSON.twinklehidan = document.getElementById('twinklehidan').selectedIndex;
		objBotJSON.twinklehiroudo = document.getElementById('twinklehiroudo').value;
		objBotJSON.twinklehiroudomin = document.getElementById('twinklehiroudomin').value;
		objBotJSON.twinklenyukyobattle = document.getElementById('twinklenyukyobattle').checked;
		objBotJSON.twinklenyukyo = document.getElementById('twinklenyukyo').selectedIndex;
		objBotJSON.twinklebaketsu = document.getElementById('twinklebaketsu').selectedIndex;
		//設定をJSON文字列に変換する
		strJSONText = JsonStringify(objBotJSON);
		//JSON文字列をファイルに保存する
		if (SaveToBinaryFile(ConvertStrToByteArray(strJSONText, 'Shift_JIS'), strCurrentDirectory + '\\' + objSetting.BotJSONFileName)){
			PopupMsg('成功', 1, 64);
		} else {
			PopupMsg('設定の保存に失敗しました。', 0, 16);
		}
	} else {
		PopupMsg('「' + objSetting.BotFileName + '」\r\nが IE で開かれていません。', 0, 16);
	}
}

Main();
