$(function(){new order();});

function order()
{
	var self = this;
	this.init.apply(this, arguments);
}

order.prototype =
{
	month : {
		'1' : null,
		'2' : null,
		'3' : null,
		'4' : null,
		'5' : null,
		'6' : null,
		'7' : null,
		'8' : null,
		'9' : null,
		'10' : null,
		'11' : null,
		'12' : null
	},

	init : function()
	{
		var self = this;
	
		var image_click = function()
		{
			var num = this.id.split('_')[2];
			jQuery.facebox('<img src="' + image_path + '/' + images[self.month[num]].filename_large + '" />');
		};

		for ( var i = 1; i < 13; i++ )
		{
			$('select#month_' + i).change(function(e) {
				e.preventDefault();
				var num = this.id.split('_')[1];
				if ( $('#' + this.id).val() == '0' )
				{
					$('#selected_image_' + num).unbind("click", image_click);
					$('#image_' + num).html('');
					self.month[num] = null;
				}
				else
				{
					$('#image_' + num).html(
						'<img id="selected_image_' + num + '" src="' + image_path + '/' + images[$('#' + this.id).val()].filename + '" />'
					);
					$('#selected_image_' + num).bind("click", image_click);
					self.month[num] = $('#' + this.id).val();
				}
				return false;
			});
		}

		for ( var p in already_images_id )
		{
			if ( already_images_id[p] != null )
			{
				$('#selected_image_' + p).bind("click", image_click);
				this.month[p] = already_images_id[p];
			}
		}

		$('#confirm').click(function(e) {
			e.preventDefault();
			self.confirm();
		});

	} ,
	
	confirm : function()
	{
		var selected_images = {};
		for(var p in images)
		{
			selected_images[p] = 0;
		}
	
		// 選択されたカレンダー項目のチェック
		for ( var i = 1; i < 13; i++ )
		{
			if ( all_image_select == 0 )
			{
				if ( $('#month_' + i).val() == '0' )
				{
					alert(i + '枚目の選択がされていません。ご確認ください');
					return false;
				}
			}
			if ( $('#month_' + i).val() != '0' )
			{
				selected_images[$('#month_' + i).val()]++;
			}
		}

		// お客様情報の簡易チェック
		var param = [
			{ param : 'name'   , str : '氏名' },
			{ param : 'zip_1'  , str : '郵便番号' },
			{ param : 'zip_2'  , str : '郵便番号' },
			{ param : 'address', str : '住所' },
			{ param : 'tel_1'  , str : '電話番号' },
			{ param : 'tel_2'  , str : '電話番号' },
			{ param : 'tel_3'  , str : '電話番号' },
			{ param : 'mail'   , str : 'メールアドレス' },
			{ param : 'mail_confirm', str : 'メールアドレス' }
		];
		for ( var i = 0, len = param.length; i < len; i++ )
		{
			if ( $('#' + param[i].param).val() == '' )
			{
				alert(param[i].str + 'が入力されていません');
				$('#' + param[i].param).focus();
				return false;
			}
		}

		// 同じカレンダーの選択がされていれば警告を促す
		var dup = false;
		for(var p in selected_images)
		{
			if ( selected_images[p] > 1 )
			{
				dup = true;
				break;
			}
		}
		if ( dup == true )
		{
			if ( ! confirm('同じイメージを選択しています。続行してもよろしいですか？') )
			{
				return false;
			}
		}

		var f = document.forms[0];
		$('#next').val('confirm');
		f.method = 'post';
		f.submit();
	}
};
