// 文字コード：UTF-8

// 送信処理Pro
function fded_submit_pro()
{
	document.fded_form.field_code.value = fded_get_field_code_pro();
	document.fded_form.submit();
}

// プレビュー画面への送信処理Pro
function fded_preview_pro()
{
	document.fded_preview_form.f.value = fded_get_field_code_pro();
	document.fded_preview_form.submit();
}

// 送信処理Spd
function fded_submit_spd()
{
	document.fded_form.field_code.value = fded_get_field_code_spd();
	document.fded_form.submit();
}

// プレビュー画面への送信処理Spd
function fded_preview_spd()
{
	document.fded_preview_form.f.value = fded_get_field_code_spd();
	document.fded_preview_form.submit();
}

// デッキコードを生成して取得Pro
//	
//	ビッグエンディアンの64進数(6bit)
//		1文字目：
//			1～4bit：0
//			5～6bit：ルール
//		2～4、5～7文字目：
//			 1～ 4bit：マスター
//			 5～ 8bit：マスターのHP
//			 9～10bit：マスターの効果(P,M)
//			11～17bit：ストーン
//		8～15、16～23文字目：
//			 8bitで1枚の手札 × 6枚 × 2
//		24～26、～、45～47文字目：
//			 18bitで1体のモンスター × 8体
//				 1～ 8bit：モンスターID
//				 9～10bit：モンスターのLv
//				11～13bit：モンスターのHP
//				14～17bit：モンスターの効果(P,S,M,!)
//				    18bit：行動済
//		
function fded_get_field_code_pro()
{
	var chars = new Array();
	var char_index = 0;
	
	// ルール
	var fded_rule = parseInt($("fded_rule").value, 10);
	chars[char_index++] = fded_rule;
	
	// 先攻マスター
	var master_type_1 = parseInt($("master_type_1").value, 10);
	var master_hp_1   = parseInt($("master_hp_1").value, 10);
	var effect_p_1m   = document.getElementById("effect_p_1m").checked ? 1 : 0;
	var effect_m_1m   = document.getElementById("effect_m_1m").checked ? 1 : 0;
	var stone_1       = parseInt($("stone_1").value, 10);
	chars[char_index++] = (master_type_1 << 2) + (master_hp_1 >> 2);
	chars[char_index++] = ((master_hp_1 & 3) << 4) + (effect_p_1m << 3) + (effect_m_1m << 2) + (stone_1 >> 5);
	chars[char_index++] = ((stone_1 & 31) << 1);
	
	// 後攻マスター
	var master_type_2 = parseInt($("master_type_2").value, 10);
	var master_hp_2   = parseInt($("master_hp_2").value, 10);
	var effect_p_2m   = document.getElementById("effect_p_2m").checked ? 1 : 0;
	var effect_m_2m   = document.getElementById("effect_m_2m").checked ? 1 : 0;
	var stone_2       = parseInt($("stone_2").value, 10);
	chars[char_index++] = (master_type_2 << 2) + (master_hp_2 >> 2);
	chars[char_index++] = ((master_hp_2 & 3) << 4) + (effect_p_2m << 3) + (effect_m_2m << 2) + (stone_2 >> 5);
	chars[char_index++] = ((stone_2 & 31) << 1);
	
	// 先攻手札
	var hand_1_1 = parseInt($("hand_1_1").value, 10);
	var hand_1_2 = parseInt($("hand_1_2").value, 10);
	var hand_1_3 = parseInt($("hand_1_3").value, 10);
	var hand_1_4 = parseInt($("hand_1_4").value, 10);
	var hand_1_5 = parseInt($("hand_1_5").value, 10);
	var hand_1_6 = parseInt($("hand_1_6").value, 10);
	chars[char_index++] = (hand_1_1 >> 2);
	chars[char_index++] = ((hand_1_1 & 3) << 4) + (hand_1_2 >> 4);
	chars[char_index++] = ((hand_1_2 & 15) << 2) + (hand_1_3 >> 6);
	chars[char_index++] = (hand_1_3 & 63);
	chars[char_index++] = (hand_1_4 >> 2);
	chars[char_index++] = ((hand_1_4 & 3) << 4) + (hand_1_5 >> 4);
	chars[char_index++] = ((hand_1_5 & 15) << 2) + (hand_1_6 >> 6);
	chars[char_index++] = (hand_1_6 & 63);
	
	// 後攻手札
	var hand_2_1 = parseInt($("hand_2_1").value, 10);
	var hand_2_2 = parseInt($("hand_2_2").value, 10);
	var hand_2_3 = parseInt($("hand_2_3").value, 10);
	var hand_2_4 = parseInt($("hand_2_4").value, 10);
	var hand_2_5 = parseInt($("hand_2_5").value, 10);
	var hand_2_6 = parseInt($("hand_2_6").value, 10);
	chars[char_index++] = (hand_2_1 >> 2);
	chars[char_index++] = ((hand_2_1 & 3) << 4) + (hand_2_2 >> 4);
	chars[char_index++] = ((hand_2_2 & 15) << 2) + (hand_2_3 >> 6);
	chars[char_index++] = (hand_2_3 & 63);
	chars[char_index++] = (hand_2_4 >> 2);
	chars[char_index++] = ((hand_2_4 & 3) << 4) + (hand_2_5 >> 4);
	chars[char_index++] = ((hand_2_5 & 15) << 2) + (hand_2_6 >> 6);
	chars[char_index++] = (hand_2_6 & 63);
	
	// 先攻モンスター
	var monster_1a  = parseInt($("monster_1a").value, 10);
	var lv_1a       = parseInt($("lv_1a").value, 10) - 1;
	var hp_1a       = parseInt($("hp_1a").value, 10) - 1;
	var effect_p_1a = document.getElementById("effect_p_1a").checked ? 1 : 0;
	var effect_s_1a = document.getElementById("effect_s_1a").checked ? 1 : 0;
	var effect_m_1a = document.getElementById("effect_m_1a").checked ? 1 : 0;
	var effect_k_1a = document.getElementById("effect_k_1a").checked ? 1 : 0;
	var effect_f_1a = document.getElementById("effect_f_1a").checked ? 1 : 0;
	var monster_1b  = parseInt($("monster_1b").value, 10);
	var lv_1b       = parseInt($("lv_1b").value, 10) - 1;
	var hp_1b       = parseInt($("hp_1b").value, 10) - 1;
	var effect_p_1b = document.getElementById("effect_p_1b").checked ? 1 : 0;
	var effect_s_1b = document.getElementById("effect_s_1b").checked ? 1 : 0;
	var effect_m_1b = document.getElementById("effect_m_1b").checked ? 1 : 0;
	var effect_k_1b = document.getElementById("effect_k_1b").checked ? 1 : 0;
	var effect_f_1b = document.getElementById("effect_f_1b").checked ? 1 : 0;
	var monster_1c  = parseInt($("monster_1c").value, 10);
	var lv_1c       = parseInt($("lv_1c").value, 10) - 1;
	var hp_1c       = parseInt($("hp_1c").value, 10) - 1;
	var effect_p_1c = document.getElementById("effect_p_1c").checked ? 1 : 0;
	var effect_s_1c = document.getElementById("effect_s_1c").checked ? 1 : 0;
	var effect_m_1c = document.getElementById("effect_m_1c").checked ? 1 : 0;
	var effect_k_1c = document.getElementById("effect_k_1c").checked ? 1 : 0;
	var effect_f_1c = document.getElementById("effect_f_1c").checked ? 1 : 0;
	var monster_1d  = parseInt($("monster_1d").value, 10);
	var lv_1d       = parseInt($("lv_1d").value, 10) - 1;
	var hp_1d       = parseInt($("hp_1d").value, 10) - 1;
	var effect_p_1d = document.getElementById("effect_p_1d").checked ? 1 : 0;
	var effect_s_1d = document.getElementById("effect_s_1d").checked ? 1 : 0;
	var effect_m_1d = document.getElementById("effect_m_1d").checked ? 1 : 0;
	var effect_k_1d = document.getElementById("effect_k_1d").checked ? 1 : 0;
	var effect_f_1d = document.getElementById("effect_f_1d").checked ? 1 : 0;
	chars[char_index++] = (monster_1a >> 2);
	chars[char_index++] = ((monster_1a & 3) << 4) + (lv_1a << 2) + (hp_1a >> 1);
	chars[char_index++] = ((hp_1a & 1) << 5) + (effect_p_1a << 4) + (effect_s_1a << 3) + (effect_m_1a << 2) + (effect_k_1a << 1) + effect_f_1a;
	chars[char_index++] = (monster_1b >> 2);
	chars[char_index++] = ((monster_1b & 3) << 4) + (lv_1b << 2) + (hp_1b >> 1);
	chars[char_index++] = ((hp_1b & 1) << 5) + (effect_p_1b << 4) + (effect_s_1b << 3) + (effect_m_1b << 2) + (effect_k_1b << 1) + effect_f_1b;
	chars[char_index++] = (monster_1c >> 2);
	chars[char_index++] = ((monster_1c & 3) << 4) + (lv_1c << 2) + (hp_1c >> 1);
	chars[char_index++] = ((hp_1c & 1) << 5) + (effect_p_1c << 4) + (effect_s_1c << 3) + (effect_m_1c << 2) + (effect_k_1c << 1) + effect_f_1c;
	chars[char_index++] = (monster_1d >> 2);
	chars[char_index++] = ((monster_1d & 3) << 4) + (lv_1d << 2) + (hp_1d >> 1);
	chars[char_index++] = ((hp_1d & 1) << 5) + (effect_p_1d << 4) + (effect_s_1d << 3) + (effect_m_1d << 2) + (effect_k_1d << 1) + effect_f_1d;
	
	// 後攻モンスター
	var monster_2a  = parseInt($("monster_2a").value, 10);
	var lv_2a       = parseInt($("lv_2a").value, 10) - 1;
	var hp_2a       = parseInt($("hp_2a").value, 10) - 1;
	var effect_p_2a = document.getElementById("effect_p_2a").checked ? 1 : 0;
	var effect_s_2a = document.getElementById("effect_s_2a").checked ? 1 : 0;
	var effect_m_2a = document.getElementById("effect_m_2a").checked ? 1 : 0;
	var effect_k_2a = document.getElementById("effect_k_2a").checked ? 1 : 0;
	var effect_f_2a = document.getElementById("effect_f_2a").checked ? 1 : 0;
	var monster_2b  = parseInt($("monster_2b").value, 10);
	var lv_2b       = parseInt($("lv_2b").value, 10) - 1;
	var hp_2b       = parseInt($("hp_2b").value, 10) - 1;
	var effect_p_2b = document.getElementById("effect_p_2b").checked ? 1 : 0;
	var effect_s_2b = document.getElementById("effect_s_2b").checked ? 1 : 0;
	var effect_m_2b = document.getElementById("effect_m_2b").checked ? 1 : 0;
	var effect_k_2b = document.getElementById("effect_k_2b").checked ? 1 : 0;
	var effect_f_2b = document.getElementById("effect_f_2b").checked ? 1 : 0;
	var monster_2c  = parseInt($("monster_2c").value, 10);
	var lv_2c       = parseInt($("lv_2c").value, 10) - 1;
	var hp_2c       = parseInt($("hp_2c").value, 10) - 1;
	var effect_p_2c = document.getElementById("effect_p_2c").checked ? 1 : 0;
	var effect_s_2c = document.getElementById("effect_s_2c").checked ? 1 : 0;
	var effect_m_2c = document.getElementById("effect_m_2c").checked ? 1 : 0;
	var effect_k_2c = document.getElementById("effect_k_2c").checked ? 1 : 0;
	var effect_f_2c = document.getElementById("effect_f_2c").checked ? 1 : 0;
	var monster_2d  = parseInt($("monster_2d").value, 10);
	var lv_2d       = parseInt($("lv_2d").value, 10) - 1;
	var hp_2d       = parseInt($("hp_2d").value, 10) - 1;
	var effect_p_2d = document.getElementById("effect_p_2d").checked ? 1 : 0;
	var effect_s_2d = document.getElementById("effect_s_2d").checked ? 1 : 0;
	var effect_m_2d = document.getElementById("effect_m_2d").checked ? 1 : 0;
	var effect_k_2d = document.getElementById("effect_k_2d").checked ? 1 : 0;
	var effect_f_2d = document.getElementById("effect_f_2d").checked ? 1 : 0;
	chars[char_index++] = (monster_2a >> 2);
	chars[char_index++] = ((monster_2a & 3) << 4) + (lv_2a << 2) + (hp_2a >> 1);
	chars[char_index++] = ((hp_2a & 1) << 5) + (effect_p_2a << 4) + (effect_s_2a << 3) + (effect_m_2a << 2) + (effect_k_2a << 1) + effect_f_2a;
	chars[char_index++] = (monster_2b >> 2);
	chars[char_index++] = ((monster_2b & 3) << 4) + (lv_2b << 2) + (hp_2b >> 1);
	chars[char_index++] = ((hp_2b & 1) << 5) + (effect_p_2b << 4) + (effect_s_2b << 3) + (effect_m_2b << 2) + (effect_k_2b << 1) + effect_f_2b;
	chars[char_index++] = (monster_2c >> 2);
	chars[char_index++] = ((monster_2c & 3) << 4) + (lv_2c << 2) + (hp_2c >> 1);
	chars[char_index++] = ((hp_2c & 1) << 5) + (effect_p_2c << 4) + (effect_s_2c << 3) + (effect_m_2c << 2) + (effect_k_2c << 1) + effect_f_2c;
	chars[char_index++] = (monster_2d >> 2);
	chars[char_index++] = ((monster_2d & 3) << 4) + (lv_2d << 2) + (hp_2d >> 1);
	chars[char_index++] = ((hp_2d & 1) << 5) + (effect_p_2d << 4) + (effect_s_2d << 3) + (effect_m_2d << 2) + (effect_k_2d << 1) + effect_f_2d;
	
	var field_code = "";
	for(var i = 0; i < chars.length; i++){
		field_code += fded_int_to_string64(chars[i]);
	}
	
	return field_code;
}

// デッキコードを生成して取得Spd
//	
//	ビッグエンディアンの64進数(6bit)
//		1文字目：
//			1～4bit：0
//			5～6bit：ルール
//		2、3文字目：
//			 1～3bit：ストーンゲージ数
//			 4～6bit：使用済みストーン数
//		4～13、14～23文字目：
//			 8bitで1枚の手札 × 7枚 × 2
//		24～26、～、33～35文字目：
//			 18bitで1体のモンスター × 8体
//				 1～ 8bit：モンスターID
//				 9～10bit：モンスターのLv
//				11～13bit：モンスターのHP
//				14～16bit：モンスターの効果(P,S,M)
//				    17bit：0
//				    18bit：行動済
//		
function fded_get_field_code_spd()
{
	var chars = new Array();
	var char_index = 0;
	
	// ルール
	chars[char_index++] = parseInt(0, 10);
	
	// 先攻ストーン
	var gauge_1 = parseInt($("gauge_1").value, 10);
	var used_1  = parseInt($("used_1").value, 10);
	chars[char_index++] = (gauge_1 << 3) + used_1;
	
	// 後攻ストーン
	var gauge_2 = parseInt($("gauge_2").value, 10);
	var used_2  = parseInt($("used_2").value, 10);
	chars[char_index++] = (gauge_2 << 3) + used_2;
	
	// 先攻手札
	var hand_1_1 = parseInt($("hand_1_1").value, 10);
	var hand_1_2 = parseInt($("hand_1_2").value, 10);
	var hand_1_3 = parseInt($("hand_1_3").value, 10);
	var hand_1_4 = parseInt($("hand_1_4").value, 10);
	var hand_1_5 = parseInt($("hand_1_5").value, 10);
	var hand_1_6 = parseInt($("hand_1_6").value, 10);
	var hand_1_7 = parseInt($("hand_1_7").value, 10);
	chars[char_index++] = (hand_1_1 >> 2);
	chars[char_index++] = ((hand_1_1 & 3) << 4) + (hand_1_2 >> 4);
	chars[char_index++] = ((hand_1_2 & 15) << 2) + (hand_1_3 >> 6);
	chars[char_index++] = (hand_1_3 & 63);
	chars[char_index++] = (hand_1_4 >> 2);
	chars[char_index++] = ((hand_1_4 & 3) << 4) + (hand_1_5 >> 4);
	chars[char_index++] = ((hand_1_5 & 15) << 2) + (hand_1_6 >> 6);
	chars[char_index++] = (hand_1_6 & 63);
	chars[char_index++] = (hand_1_7 >> 2);
	chars[char_index++] = ((hand_1_7 & 3) << 4);
	
	// 後攻手札
	var hand_2_1 = parseInt($("hand_2_1").value, 10);
	var hand_2_2 = parseInt($("hand_2_2").value, 10);
	var hand_2_3 = parseInt($("hand_2_3").value, 10);
	var hand_2_4 = parseInt($("hand_2_4").value, 10);
	var hand_2_5 = parseInt($("hand_2_5").value, 10);
	var hand_2_6 = parseInt($("hand_2_6").value, 10);
	var hand_2_7 = parseInt($("hand_2_7").value, 10);
	chars[char_index++] = (hand_2_1 >> 2);
	chars[char_index++] = ((hand_2_1 & 3) << 4) + (hand_2_2 >> 4);
	chars[char_index++] = ((hand_2_2 & 15) << 2) + (hand_2_3 >> 6);
	chars[char_index++] = (hand_2_3 & 63);
	chars[char_index++] = (hand_2_4 >> 2);
	chars[char_index++] = ((hand_2_4 & 3) << 4) + (hand_2_5 >> 4);
	chars[char_index++] = ((hand_2_5 & 15) << 2) + (hand_2_6 >> 6);
	chars[char_index++] = (hand_2_6 & 63);
	chars[char_index++] = (hand_2_7 >> 2);
	chars[char_index++] = ((hand_2_7 & 3) << 4);
	
	// 先攻モンスター
	var monster_1a  = parseInt($("monster_1a").value, 10);
	var lv_1a       = parseInt($("lv_1a").value, 10) - 1;
	var hp_1a       = parseInt($("hp_1a").value, 10) - 1;
	var effect_p_1a = document.getElementById("effect_p_1a").checked ? 1 : 0;
	var effect_s_1a = document.getElementById("effect_s_1a").checked ? 1 : 0;
	var effect_m_1a = document.getElementById("effect_m_1a").checked ? 1 : 0;
	var effect_f_1a = document.getElementById("effect_f_1a").checked ? 1 : 0;
	var monster_1b  = parseInt($("monster_1b").value, 10);
	var lv_1b       = parseInt($("lv_1b").value, 10) - 1;
	var hp_1b       = parseInt($("hp_1b").value, 10) - 1;
	var effect_p_1b = document.getElementById("effect_p_1b").checked ? 1 : 0;
	var effect_s_1b = document.getElementById("effect_s_1b").checked ? 1 : 0;
	var effect_m_1b = document.getElementById("effect_m_1b").checked ? 1 : 0;
	var effect_f_1b = document.getElementById("effect_f_1b").checked ? 1 : 0;
	chars[char_index++] = (monster_1a >> 2);
	chars[char_index++] = ((monster_1a & 3) << 4) + (lv_1a << 2) + (hp_1a >> 1);
	chars[char_index++] = ((hp_1a & 1) << 5) + (effect_p_1a << 4) + (effect_s_1a << 3) + (effect_m_1a << 2) + effect_f_1a;
	chars[char_index++] = (monster_1b >> 2);
	chars[char_index++] = ((monster_1b & 3) << 4) + (lv_1b << 2) + (hp_1b >> 1);
	chars[char_index++] = ((hp_1b & 1) << 5) + (effect_p_1b << 4) + (effect_s_1b << 3) + (effect_m_1b << 2) + effect_f_1b;
	
	// 後攻モンスター
	var monster_2a  = parseInt($("monster_2a").value, 10);
	var lv_2a       = parseInt($("lv_2a").value, 10) - 1;
	var hp_2a       = parseInt($("hp_2a").value, 10) - 1;
	var effect_p_2a = document.getElementById("effect_p_2a").checked ? 1 : 0;
	var effect_s_2a = document.getElementById("effect_s_2a").checked ? 1 : 0;
	var effect_m_2a = document.getElementById("effect_m_2a").checked ? 1 : 0;
	var effect_f_2a = document.getElementById("effect_f_2a").checked ? 1 : 0;
	var monster_2b  = parseInt($("monster_2b").value, 10);
	var lv_2b       = parseInt($("lv_2b").value, 10) - 1;
	var hp_2b       = parseInt($("hp_2b").value, 10) - 1;
	var effect_p_2b = document.getElementById("effect_p_2b").checked ? 1 : 0;
	var effect_s_2b = document.getElementById("effect_s_2b").checked ? 1 : 0;
	var effect_m_2b = document.getElementById("effect_m_2b").checked ? 1 : 0;
	var effect_f_2b = document.getElementById("effect_f_2b").checked ? 1 : 0;
	chars[char_index++] = (monster_2a >> 2);
	chars[char_index++] = ((monster_2a & 3) << 4) + (lv_2a << 2) + (hp_2a >> 1);
	chars[char_index++] = ((hp_2a & 1) << 5) + (effect_p_2a << 4) + (effect_s_2a << 3) + (effect_m_2a << 2) + effect_f_2a;
	chars[char_index++] = (monster_2b >> 2);
	chars[char_index++] = ((monster_2b & 3) << 4) + (lv_2b << 2) + (hp_2b >> 1);
	chars[char_index++] = ((hp_2b & 1) << 5) + (effect_p_2b << 4) + (effect_s_2b << 3) + (effect_m_2b << 2) + effect_f_2b;
	
	var field_code = "";
	for(var i = 0; i < chars.length; i++){
		field_code += fded_int_to_string64(chars[i]);
	}
	
	return field_code;
}

// 数値を64進数に変換
function fded_int_to_string64(val)
{
	return ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","]"][val];
}

// 64進数を数値に変換
function fded_string64_to_int(val)
{
	return "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ[]".indexOf(val,0);
}

// 指定IDのセレクトボックスに指定の値があれば選択状態にする
function fded_init_selectbox(obj_id, select_value)
{
	var obj = document.getElementById(obj_id);
	if(obj){
		for(var i = 0; i < obj.options.length; i++){
			if(obj.options[i].value == select_value){
				obj.selectedIndex = i;
				break;
			}
		}
	}
}

// 指定IDのチェックボックスを (val != 0) なら選択状態にする
function fded_init_checkbox(obj_id, val)
{
	if(val == 0){
		return;
	}
	
	var obj = document.getElementById(obj_id);
	if(obj){
		obj.checked = true;
	}
}

// 初期化処理Pro
function fded_init_pro(field_code)
{
	if(field_code.length != 47){
		return;
	}
	
	var codes = new Array();
	for(var i = 0; i < 47; i++){
		codes[i] = 0;
	}
	for(var i = 0; i < field_code.length; i++){
		codes[i] = fded_string64_to_int(field_code.charAt(i));
	}
	
	// ルール
	fded_init_selectbox("fded_rule", (codes[0] & 3));
	
	// 先攻マスター
	fded_init_selectbox("master_type_1", (codes[1] >> 2));
	fded_init_selectbox("master_hp_1",   ((codes[1] & 3) << 2) + (codes[2] >> 4));
	fded_init_checkbox("effect_p_1m",    ((codes[2] >> 3) & 1));
	fded_init_checkbox("effect_m_1m",    ((codes[2] >> 2) & 1));
	fded_init_selectbox("stone_1",       ((codes[2] & 3) << 5) + (codes[3] >> 1));
	
	// 後攻マスター
	fded_init_selectbox("master_type_2", (codes[4] >> 2));
	fded_init_selectbox("master_hp_2",   ((codes[4] & 3) << 2) + (codes[5] >> 4));
	fded_init_checkbox("effect_p_2m",    ((codes[5] >> 3) & 1));
	fded_init_checkbox("effect_m_2m",    ((codes[5] >> 2) & 1));
	fded_init_selectbox("stone_2",       ((codes[5] & 3) << 5) + (codes[6] >> 1));
	
	// 先攻手札
	fded_init_selectbox("hand_1_1", ((codes[7]  & 63) << 2) + (codes[8]  >> 4));
	fded_init_selectbox("hand_1_2", ((codes[8]  & 15) << 4) + (codes[9]  >> 2));
	fded_init_selectbox("hand_1_3", ((codes[9]  &  3) << 6) + (codes[10] >> 0));
	fded_init_selectbox("hand_1_4", ((codes[11] & 63) << 2) + (codes[12] >> 4));
	fded_init_selectbox("hand_1_5", ((codes[12] & 15) << 4) + (codes[13] >> 2));
	fded_init_selectbox("hand_1_6", ((codes[13] &  3) << 6) + (codes[14] >> 0));
	
	// 後攻手札
	fded_init_selectbox("hand_2_1", ((codes[15] & 63) << 2) + (codes[16] >> 4));
	fded_init_selectbox("hand_2_2", ((codes[16] & 15) << 4) + (codes[17] >> 2));
	fded_init_selectbox("hand_2_3", ((codes[17] &  3) << 6) + (codes[18] >> 0));
	fded_init_selectbox("hand_2_4", ((codes[19] & 63) << 2) + (codes[20] >> 4));
	fded_init_selectbox("hand_2_5", ((codes[20] & 15) << 4) + (codes[21] >> 2));
	fded_init_selectbox("hand_2_6", ((codes[21] &  3) << 6) + (codes[22] >> 0));
	
	// 先攻モンスター
	fded_init_selectbox("monster_1a", (codes[23] << 2) + (codes[24] >> 4));
	fded_init_selectbox("lv_1a",      ((codes[24] >> 2) & 3) + 1);
	fded_init_selectbox("hp_1a",      (((codes[24] & 3) << 1) + (codes[25] >> 5)) + 1);
	fded_init_checkbox("effect_p_1a", ((codes[25] >> 4) & 1));
	fded_init_checkbox("effect_s_1a", ((codes[25] >> 3) & 1));
	fded_init_checkbox("effect_m_1a", ((codes[25] >> 2) & 1));
	fded_init_checkbox("effect_k_1a", ((codes[25] >> 1) & 1));
	fded_init_checkbox("effect_f_1a", ((codes[25] >> 0) & 1));
	fded_init_selectbox("monster_1b", (codes[26] << 2) + (codes[27] >> 4));
	fded_init_selectbox("lv_1b",      ((codes[27] >> 2) & 3) + 1);
	fded_init_selectbox("hp_1b",      (((codes[27] & 3) << 1) + (codes[28] >> 5)) + 1);
	fded_init_checkbox("effect_p_1b", ((codes[28] >> 4) & 1));
	fded_init_checkbox("effect_s_1b", ((codes[28] >> 3) & 1));
	fded_init_checkbox("effect_m_1b", ((codes[28] >> 2) & 1));
	fded_init_checkbox("effect_k_1b", ((codes[28] >> 1) & 1));
	fded_init_checkbox("effect_f_1b", ((codes[28] >> 0) & 1));
	fded_init_selectbox("monster_1c", (codes[29] << 2) + (codes[30] >> 4));
	fded_init_selectbox("lv_1c",      ((codes[30] >> 2) & 3) + 1);
	fded_init_selectbox("hp_1c",      (((codes[30] & 3) << 1) + (codes[31] >> 5)) + 1);
	fded_init_checkbox("effect_p_1c", ((codes[31] >> 4) & 1));
	fded_init_checkbox("effect_s_1c", ((codes[31] >> 3) & 1));
	fded_init_checkbox("effect_m_1c", ((codes[31] >> 2) & 1));
	fded_init_checkbox("effect_k_1c", ((codes[31] >> 1) & 1));
	fded_init_checkbox("effect_f_1c", ((codes[31] >> 0) & 1));
	fded_init_selectbox("monster_1d", (codes[32] << 2) + (codes[33] >> 4));
	fded_init_selectbox("lv_1d",      ((codes[33] >> 2) & 3) + 1);
	fded_init_selectbox("hp_1d",      (((codes[33] & 3) << 1) + (codes[34] >> 5)) + 1);
	fded_init_checkbox("effect_p_1d", ((codes[34] >> 4) & 1));
	fded_init_checkbox("effect_s_1d", ((codes[34] >> 3) & 1));
	fded_init_checkbox("effect_m_1d", ((codes[34] >> 2) & 1));
	fded_init_checkbox("effect_k_1d", ((codes[34] >> 1) & 1));
	fded_init_checkbox("effect_f_1d", ((codes[34] >> 0) & 1));
	
	// 後攻モンスター
	fded_init_selectbox("monster_2a", (codes[35] << 2) + (codes[36] >> 4));
	fded_init_selectbox("lv_2a",      ((codes[36] >> 2) & 3) + 1);
	fded_init_selectbox("hp_2a",      (((codes[36] & 3) << 1) + (codes[37] >> 5)) + 1);
	fded_init_checkbox("effect_p_2a", ((codes[37] >> 4) & 1));
	fded_init_checkbox("effect_s_2a", ((codes[37] >> 3) & 1));
	fded_init_checkbox("effect_m_2a", ((codes[37] >> 2) & 1));
	fded_init_checkbox("effect_k_2a", ((codes[37] >> 1) & 1));
	fded_init_checkbox("effect_f_2a", ((codes[37] >> 0) & 1));
	fded_init_selectbox("monster_2b", (codes[38] << 2) + (codes[39] >> 4));
	fded_init_selectbox("lv_2b",      ((codes[39] >> 2) & 3) + 1);
	fded_init_selectbox("hp_2b",      (((codes[39] & 3) << 1) + (codes[40] >> 5)) + 1);
	fded_init_checkbox("effect_p_2b", ((codes[40] >> 4) & 1));
	fded_init_checkbox("effect_s_2b", ((codes[40] >> 3) & 1));
	fded_init_checkbox("effect_m_2b", ((codes[40] >> 2) & 1));
	fded_init_checkbox("effect_k_2b", ((codes[40] >> 1) & 1));
	fded_init_checkbox("effect_f_2b", ((codes[40] >> 0) & 1));
	fded_init_selectbox("monster_2c", (codes[41] << 2) + (codes[42] >> 4));
	fded_init_selectbox("lv_2c",      ((codes[42] >> 2) & 3) + 1);
	fded_init_selectbox("hp_2c",      (((codes[42] & 3) << 1) + (codes[43] >> 5)) + 1);
	fded_init_checkbox("effect_p_2c", ((codes[43] >> 4) & 1));
	fded_init_checkbox("effect_s_2c", ((codes[43] >> 3) & 1));
	fded_init_checkbox("effect_m_2c", ((codes[43] >> 2) & 1));
	fded_init_checkbox("effect_k_2c", ((codes[43] >> 1) & 1));
	fded_init_checkbox("effect_f_2c", ((codes[43] >> 0) & 1));
	fded_init_selectbox("monster_2d", (codes[44] << 2) + (codes[45] >> 4));
	fded_init_selectbox("lv_2d",      ((codes[45] >> 2) & 3) + 1);
	fded_init_selectbox("hp_2d",      (((codes[45] & 3) << 1) + (codes[46] >> 5)) + 1);
	fded_init_checkbox("effect_p_2d", ((codes[46] >> 4) & 1));
	fded_init_checkbox("effect_s_2d", ((codes[46] >> 3) & 1));
	fded_init_checkbox("effect_m_2d", ((codes[46] >> 2) & 1));
	fded_init_checkbox("effect_k_2d", ((codes[46] >> 1) & 1));
	fded_init_checkbox("effect_f_2d", ((codes[46] >> 0) & 1));
}

// 初期化処理Spd
function fded_init_spd(field_code)
{
	if(field_code.length != 35){
		return;
	}
	
	var codes = new Array();
	for(var i = 0; i < 35; i++){
		codes[i] = 0;
	}
	for(var i = 0; i < field_code.length; i++){
		codes[i] = fded_string64_to_int(field_code.charAt(i));
	}
	
	// ルール
	fded_init_selectbox("fded_rule", (codes[0] & 3));
	
	// 先攻ストーン
	fded_init_selectbox("gauge_1", (codes[1] >> 3));
	fded_init_selectbox("used_1",  (codes[1] & 7));
	
	// 後攻ストーン
	fded_init_selectbox("gauge_2", (codes[2] >> 3));
	fded_init_selectbox("used_2",  (codes[2] & 7));
	
	// 先攻手札
	fded_init_selectbox("hand_1_1", ((codes[3]  & 63) << 2) + (codes[4]  >> 4));
	fded_init_selectbox("hand_1_2", ((codes[4]  & 15) << 4) + (codes[5]  >> 2));
	fded_init_selectbox("hand_1_3", ((codes[5]  &  3) << 6) + (codes[6]  >> 0));
	fded_init_selectbox("hand_1_4", ((codes[7]  & 63) << 2) + (codes[8]  >> 4));
	fded_init_selectbox("hand_1_5", ((codes[8]  & 15) << 4) + (codes[9]  >> 2));
	fded_init_selectbox("hand_1_6", ((codes[9]  &  3) << 6) + (codes[10] >> 0));
	fded_init_selectbox("hand_1_7", ((codes[11] & 63) << 2) + (codes[12] >> 4));
	
	// 後攻手札
	fded_init_selectbox("hand_2_1", ((codes[13] & 63) << 2) + (codes[14] >> 4));
	fded_init_selectbox("hand_2_2", ((codes[14] & 15) << 4) + (codes[15] >> 2));
	fded_init_selectbox("hand_2_3", ((codes[15] &  3) << 6) + (codes[16] >> 0));
	fded_init_selectbox("hand_2_4", ((codes[17] & 63) << 2) + (codes[18] >> 4));
	fded_init_selectbox("hand_2_5", ((codes[18] & 15) << 4) + (codes[19] >> 2));
	fded_init_selectbox("hand_2_6", ((codes[19] &  3) << 6) + (codes[20] >> 0));
	fded_init_selectbox("hand_2_7", ((codes[21] & 63) << 2) + (codes[22] >> 4));
	
	// 先攻モンスター
	fded_init_selectbox("monster_1a", (codes[23] << 2) + (codes[24] >> 4));
	fded_init_selectbox("lv_1a",      ((codes[24] >> 2) & 3) + 1);
	fded_init_selectbox("hp_1a",      (((codes[24] & 3) << 1) + (codes[25] >> 5)) + 1);
	fded_init_checkbox("effect_p_1a", ((codes[25] >> 4) & 1));
	fded_init_checkbox("effect_s_1a", ((codes[25] >> 3) & 1));
	fded_init_checkbox("effect_m_1a", ((codes[25] >> 2) & 1));
	fded_init_checkbox("effect_f_1a", ((codes[25] >> 0) & 1));
	fded_init_selectbox("monster_1b", (codes[26] << 2) + (codes[27] >> 4));
	fded_init_selectbox("lv_1b",      ((codes[27] >> 2) & 3) + 1);
	fded_init_selectbox("hp_1b",      (((codes[27] & 3) << 1) + (codes[28] >> 5)) + 1);
	fded_init_checkbox("effect_p_1b", ((codes[28] >> 4) & 1));
	fded_init_checkbox("effect_s_1b", ((codes[28] >> 3) & 1));
	fded_init_checkbox("effect_m_1b", ((codes[28] >> 2) & 1));
	fded_init_checkbox("effect_f_1b", ((codes[28] >> 0) & 1));
	
	// 後攻モンスター
	fded_init_selectbox("monster_2a", (codes[29] << 2) + (codes[30] >> 4));
	fded_init_selectbox("lv_2a",      ((codes[30] >> 2) & 3) + 1);
	fded_init_selectbox("hp_2a",      (((codes[30] & 3) << 1) + (codes[31] >> 5)) + 1);
	fded_init_checkbox("effect_p_2a", ((codes[31] >> 4) & 1));
	fded_init_checkbox("effect_s_2a", ((codes[31] >> 3) & 1));
	fded_init_checkbox("effect_m_2a", ((codes[31] >> 2) & 1));
	fded_init_checkbox("effect_f_2a", ((codes[31] >> 0) & 1));
	fded_init_selectbox("monster_2b", (codes[32] << 2) + (codes[33] >> 4));
	fded_init_selectbox("lv_2b",      ((codes[33] >> 2) & 3) + 1);
	fded_init_selectbox("hp_2b",      (((codes[33] & 3) << 1) + (codes[34] >> 5)) + 1);
	fded_init_checkbox("effect_p_2b", ((codes[34] >> 4) & 1));
	fded_init_checkbox("effect_s_2b", ((codes[34] >> 3) & 1));
	fded_init_checkbox("effect_m_2b", ((codes[34] >> 2) & 1));
	fded_init_checkbox("effect_f_2b", ((codes[34] >> 0) & 1));
}

// 初期化処理
function fded_init()
{
	var param = document.location.href.split("#");
	if( (param.length > 1) && ((param[1].length == 47) || (param[1].length == 35)) ){
		var field_code = param[1];
		var code = fded_string64_to_int(field_code.charAt(0));
		var rule = code & 3;
		if(rule != 0){
			fded_init_pro(field_code);
		} else {
			fded_init_spd(field_code);
		}
	}
}

window.onload = fded_init;
