/*
 * ※このスクリプトファイルはプログラムによって自動生成されています。
 * Created by exAD::LpCommon::getMailFormFreeHtml();
 * Created date 2010/12/07
 */
var valid_1 = '';
var valid_2 = '';
var valid_3 = '';
var valid_4 = '';
var valid_7 = '';
var valid_8 = '';

window.onload = function() {

	valid_1 = new Spry.Widget.ValidationTextField('check_1', 'none', {validateOn:['change'], useCharacterMasking:false, isRequired:true});
	valid_2 = new Spry.Widget.ValidationTextField('check_2', 'none', {validateOn:['change'], useCharacterMasking:false, isRequired:true});
	valid_3 = new Spry.Widget.ValidationTextField('check_3', 'email', {validateOn:['change'], useCharacterMasking:true, isRequired:true});
	valid_4 = new Spry.Widget.ValidationTextField('check_4', 'none', {validateOn:['change'], useCharacterMasking:false, isRequired:true});
	valid_7 = new Spry.Widget.ValidationTextField('check_7', 'none', {validateOn:['change'], useCharacterMasking:false, isRequired:true});
	valid_8 = new Spry.Widget.ValidationTextField('check_8', 'none', {validateOn:['change'], useCharacterMasking:false, isRequired:true});

}

function check_input() {

	var bln = true;

	bln *= valid_1.validate();
	bln *= valid_2.validate();
	bln *= valid_3.validate();
	bln *= valid_4.validate();
	bln *= valid_7.validate();
	bln *= valid_8.validate();

	return bln;

}

$(function() {
	$('#btn_confirm').click(function(ev) {
		// 入力チェック
		if (!check_input()) {
			return false;
		}

		$('#complete').hide();
		$('#error').hide();
		$('#confirm').show();

		for (i = 1; i <= 12; i++) {
			switch($('#control_' + i).val()) {
				case '01':
					reply_info = '';
					if ($('#reply_' + i).val() == '1' && $('#input_' + i).val() != '') {
						reply_info += '&nbsp;このメールアドレスに自動返信メールが送信されます。';
					}
					$('#confirm_' + i).html($('#input_' + i).val() + '&nbsp;' + reply_info);
					break;
				case '02':
					$('#confirm_' + i).html($("input[@name='radio_" + i + "']:checked").val() + '&nbsp;');
					break;
				case '03':
					$('#confirm_' + i).html($('#input_' + i).val() + '&nbsp;');
					break;
				case '04':
					$('#confirm_' + i).html($('#ddl_' + i).val() + '&nbsp;');
					break;
				case '11':
					$('#confirm_' + i).html($('#input_' + i).val() + '&nbsp;');
					break;
				case '12':
					$('#confirm_' + i).html('<pre>' + $('#input_' + i).val() + '</pre>' + '&nbsp;');
					break;
				case '13':
					$('#confirm_' + i).html($("input[@name='radio_" + i + "']:checked").val() + '&nbsp;');
					break;
				case '14':
					$ckb_confirm = '';
					$("input:checkbox[@name^='ckb_" + i + "']").each(function(){
						if ($(this).attr("checked") == true) {
							$ckb_confirm += $(this).val() + "<br />\n";
						}
					});
					$('#confirm_' + i).html($ckb_confirm);
					break;
				case '15':
					$('#confirm_' + i).html($('#ddl_' + i).val() + '&nbsp;');
					break;
			}
		}

		$('#move').click();

	});

	$('#btn_close').click(function(ev) {
		tb_remove();
	});

	$('#send').click(function(ev) {
		var json = '';
		json += "{";
		json += "\"site_id\" : \"" + $('#site_id').val() + "\",\n";
		json += "\"site_title\" : \"" + $('title').html() + "\",\n";
		json += "\"send_email\" : \"" + $('#send_email').val() + "\",\n";
		json += "\"mff_mail_title\" : \"" + $('#mff_mail_title').val() + "\",\n";
		json += "\"mf_title\" : \"" + $('#step1 > h2').text() + "\",\n";
		json += "\"item_count\" : \"12\",\n";
		for (i = 1; i <= 12; i++) {
			json += "\"value_" + i + "[type]\" : [\"" + $('#control_' + i).val() + "\"],\n";
			json += "\"value_" + i + "[title]\" : [\"" + $('#item_title_' + i).val() + "\"],\n";
			switch($('#control_' + i).val()) {
				case '01':
					json += "\"value_" + i + "[data]\" : [\"" + $('#input_' + i).val() + "\"],\n";
					json += "\"value_" + i + "[reply]\" : [\"" + $('#reply_' + i).val() + "\"]\n";
					break;
				case '02':
					json += "\"value_" + i + "[data]\" : [\"" + $("input[@name='radio_" + i + "']:checked").val() + "\"]\n";
					break;
				case '03':
					json += "\"value_" + i + "[data]\" : [\"" + $('#input_' + i).val() + "\"]\n";
					break;
				case '04':
					json += "\"value_" + i + "[data]\" : [\"" + $('#ddl_' + i).val() + "\"]\n";
					break;
				case '11':
					json += "\"value_" + i + "[data]\" : [\"" + $('#input_' + i).val() + "\"]\n";
					break;
				case '12':
					if ($('#input_' + i).val() != '') {
						var text = $('#input_' + i).val();
						text = text.replace(/\r\n/g, "\\n");
						text = text.replace(/(\n|\r)/g, "\\n");
					}
					json += "\"value_" + i + "[data]\" : [\"" + text + "\"]\n";
					break;
				case '13':
					json += "\"value_" + i + "[data]\" : [\"" + $("input[@name='radio_" + i + "']:checked").val() + "\"]\n";
					break;
				case '14':
					ckb = '';
					$("input:checkbox[@name^='ckb_" + i + "']").each(function(){
						if ($(this).attr("checked") == true) {
							if (ckb != '') {
								ckb += ',';
							}
							ckb += "\"" + $(this).val() + "\"";
						}
					});
					json += "\"value_" + i + "[data][]\" : [" + ckb + "]\n";
					break;
				case '15':
					json += "\"value_" + i + "[data]\" : [\"" + $('#ddl_' + i).val() + "\"]\n";
					break;
				default:
					break;
			}

			if (i < 12) {
				json += ",";
			}
		}

		json += "}";

		$.ajax({
			type : "POST",
			url : "../options/mailformfree.php",
			data : eval("(" + json + ")"),
			dataType : "text", 
			scriptCharset : "UTF-8", 
			async : false,
			contentType: "application/x-www-form-urlencoded; charset=UTF-8", 
			success : function(data, status, msg) {
				$('#confirm').hide();
				$('#error').hide();
				$('#complete').show();
			}, 
			error : function(data, status, msg) {
				$('#confirm').hide();
				$('#complete').hide();
				$('#error').show();
			}
			
		});

	});
});

