/*
 * reorder-error.js
 *
 * create:
 * update:
 *
 */

(function($) {
	$(function() {
		// ------------------------------------------------------------------------
		// 枚数の自動計算
		// ------------------------------------------------------------------------
		// 初回
		var total_count = 0;
		setSizeNum();
		
		// 一定間隔で処理
		setInterval(setSizeNum, 500);
		
		// 枚数計算関数
		function setSizeNum() {
			total_count = 0;
			$(".size_count").each(function(){
				var n = $(this).val();
				if ((n.match(/[^0-9]/g) == null) && (n != "")) {
					total_count += parseInt($(this).val(),10);
				}
			});
			$("#total_count").val(total_count);
			$("#total_count_text").text(total_count);
		}
		
		
		// ------------------------------------------------------------------------
		// ロールオーバー処理
		// ------------------------------------------------------------------------
		$("#button-confirm").hover(function(){
			$(this).css("background-image", "../img/common/btn-confirm-over.gif");
		},function(){
			$(this).css("background-image", "../img/common/btn-confirm.gif");
		});
		
		
		// ------------------------------------------------------------------------
		// クリック時
		// ------------------------------------------------------------------------
		$("#form_r").submit(function(){
			var error = new Array();
			var anchorlink = new Array();
			var i = 0;
			
			// ------------------------------------------------
			// エラーを非表示
			// ------------------------------------------------
			$("#ordernum_error").css("display","none");
			$("#product_name_error").css("display","none");
			$("#product_color_error").css("display","none");
			$("#shirt_size01_error").css("display","none");
			$("#shirt_count01_error").css("display","none");
			$("#size_count_error").css("display","none");
			$("#name_error").css("display","none");
			$("#kana_error").css("display","none");
			$("#tel_error").css("display","none");
			$("#email_error").css("display","none");
			$("#zipcode_error").css("display","none");
			$("#address_error").css("display","none");
			$("#s_others_payment_error").css("display","none");
			$("#addressee_zip_error").css("display","none");
			$("#addressee_tel_error").css("display","none");
			
			// ------------------------------------------------
			// エラーチェック
			// ------------------------------------------------
			
			// 注文番号の入力チェック
			if ($("#ordernum").val() == "") {
				error[i] = "注文番号は必須項目です。";
				anchorlink[i] = "#ordernum_th";
				
				// エラーを表示
				$("#ordernum_error").css("display","block");
				$("#ordernum_error em").text("【!!】注文番号は必須項目です。");
				
				i++;
			}
			
			// 商品名の入力チェック
			if ($("#product_name").val() == "") {
				error[i] = "商品名は必須項目です。";
				anchorlink[i] = "#product_name_th";
				
				// エラーを表示
				$("#product_name_error").css("display","block");
				$("#product_name_error em").text("【!!】商品名は必須項目です。");
				
				i++;
			}
			
			// 商品カラー名の入力チェック
			if ($("#product_color").val() == "") {
				error[i] = "商品カラー名は必須項目です。";
				anchorlink[i] = "#product_color_th";
				
				// エラーを表示
				$("#product_color_error").css("display","block");
				$("#product_color_error em").text("【!!】商品カラー名は必須項目です。");
				
				i++;
			}
			
			// サイズの入力エラーチェック
			if ($("#shirt_size01").val() == "") {
				error[i] = "サイズは必須項目です。";
				anchorlink[i] = "#shirt_size01_th";
				
				// エラーを表示
				$("#shirt_size01_error").css("display","block");
				$("#shirt_size01_error em").text("【!!】サイズは必須項目です。");
				
				i++;
			}
			
			// 枚数の入力エラーチェック
			if ($("#shirt_count01").val() == "") {
				error[i] = "枚数は必須項目です。";
				anchorlink[i] = "#shirt_size01_th";
				
				// エラーを表示
				$("#shirt_count01_error").css("display","block");
				$("#shirt_count01_error em").text("【!!】枚数は必須項目です。");

				i++;
			}
			
			// 枚数の入力数チェック
			var sc = 0;
			$(".size_count").each(function(){
				var n = $(this).val();
				if ((n.match(/[^0-9]/g) == null) && (n != "")) {
					sc += parseInt($(this).val(),10);
				}
			});
			if (sc < 1) {
				error[i] = "枚数は合計1以上で入力してください。";
				anchorlink[i] = "#shirt_size01_th";
				
				// エラー表示
				$("#size_count_error").css("display","block");
				$("#size_count_error em").text("【!!】枚数は合計1以上で入力してください。");

				i++;
			}
			
			// お名前の入力エラーチェック
			if ($("#name").val() == "") {
				error[i] = "お名前は必須項目です。";
				anchorlink[i] = "#name_th";
				
				// エラーを表示
				$("#name_error").css("display","block");
				$("#name_error em").text("【!!】お名前は必須項目です。");
				
				i++;
			}
			
			// フリガナの入力エラーチェック
			if ($("#kana").val() == "") {
				error[i] = "フリガナは必須項目です。";
				anchorlink[i] = "#kana_th";
				
				//エラーを表示
				$("#kana_error").css("display","block");
				$("#kana_error em").text("【!!】フリガナは必須項目です。");
				
				i++;
			}
			
			// 電話番号の入力エラーチェック
			if ($("#tel").val() == "") {
				error[i] = "電話番号は必須項目です。";
				anchorlink[i] = "#tel_th";
				
				// エラーを表示
				$("#tel_error").css("display","block");
				$("#tel_error em").text("【!!】電話番号は必須項目です。");
				
				i++;
			// 電話番号の入力内容チェック
			} else if (!$("#tel").val().match(/^[0-9]+$/)) {
				error[i] = "電話番号は半角数値で入力してください。";
				anchorlink[i] = "#tel_th";
				
				// エラーを表示
				$("#tel_error").css("display","block");
				$("#tel_error em").text("【!!】電話番号は半角数値で入力してください。");
				
				i++;
			}
			
			// メールアドレスの入力エラーチェック
			if ($("#email").val() == "") {
				error[i] = "メールアドレスは必須項目です。";
				anchorlink[i] = "#email_th";
				
				// エラーを表示
				$("#email_error").css("display","block");
				$("#email_error em").text("【!!】メールアドレスは必須項目です。");
				
				i++;

			//　メールアドレスの入力方法エラーチェック
			} else if (!$("#email").val().match(/[!#-9A-~]+@+[a-z0-9]+.+[^.]$/)) {
				error[i] = "メールアドレスは正しく入力してください。";
				anchorlink[i] = "#email_th";
				
				// エラーを表示
				$("#email_error").css("display","block");
				$("#email_error em").text("【!!】メールアドレスは正しく入力してください。");
			
				i++;
			}
			
			// 郵便番号の入力エラーチェック
			if ($("#zipcode").val() == "") {
				error[i] = "郵便番号は必須項目です。";
				anchorlink[i] = "#zipcode_th";
				
				// エラーを表示
				$("#zipcode_error").css("display","block");
				$("#zipcode_error em").text("【!!】郵便番号は必須項目です。");
				
				i++;
			//　郵便番号の入力文法エラーチェック
			} else if (($("#zipcode").val() != "") && (!$("#zipcode").val().match(/^[0-9]+$/))) {
				error[i] = "郵便番号は半角数値で入力してください。";
				anchorlink[i] = "#zipcode_th";
				
				// エラーを表示
				$("#zipcode_error").css("display","block");
				$("#zipcode_error em").text("【!!】郵便番号は半角数値で入力してください。");
				
				i++;
			}
			
			// ご住所の入力エラーチェック
			if ($("#address").val() == "") {
				error[i] = "ご住所は必須項目です。";
				anchorlink[i] = "#address_th";
				
				// エラーを表示
				$("#address_error").css("display","block");
				$("#address_error em").text("【!!】ご住所は必須項目です。");
				
				i++;
			}
			
			// お支払い方法の入力エラーチェック
			if ($("#s_others_payment").val() == "") {
				error[i] = "お支払い方法は必須項目です。";
				anchorlink[i] = "#s_others_payment_th";
			
				// エラーを表示
				$("#s_others_payment_error").css("display","block");
				$("#s_others_payment_error em").text("【!!】お支払い方法は必須項目です。");
				
				i++;
			}
			
			// 配送先郵便番号の入力エラーチェック
			if (($("#addressee_zip").val() != "") && (!$("#addressee_zip").val().match(/^[0-9]+$/))) {
				error[i] = "配送先郵便番号は半角数値で入力してください。";
				anchorlink[i] = "#addressee_zip_th";
				
				// エラーを表示
				$("#addressee_zip_error").css("display","block");
				$("#addressee_zip_error em").text("【!!】配送先郵便番号は半角数値で入力してください。");
				
				i++;
			}
			
			// 配送先電話番号の入力エラーチェック
			if (($("#addressee_tel").val() != "") && (!$("#addressee_tel").val().match(/^[0-9]+$/))) {
				error[i] = "配送先電話番号は半角数値で入力してください。";
				anchorlink[i] = "#addressee_tel_th";
				
				// エラーを表示
				$("#addressee_tel_error").css("display","block");
				$("#addressee_tel_error em").text("【!!】配送先電話番号は半角数値で入力してください。");
				
				i++;
			}
			
			
			// ------------------------------------------------
			// エラー判別
			// ------------------------------------------------
			if (error.length > 0) {
				//エラーがある場合
				alert(error.join("\n"));
				location.href = anchorlink[0];
				return false;
			}
		});
	});
})(jQuery);
