
$(function()
{
	// 上传
	if ($('#AddAttach').length)
	{
		new Ajax_upload('#AddAttach', {
			action : '/index.php/Filesystem/upload',	// 上传文件的服务器端
			name : 'myfile',	// 上传文件字段名
			onSubmit : function (file, ext) {
				var errstr		= '不允许上传该类型的文件';
				var oat_num		= document.getElementsByName('attaches')[0].value.split(',').length;
				if(oat_num>8) errstr	= '您一次上传的文件不能超过8个!';
				if (! (ext && /^(jpg|png|jpeg|gif|bmp)$/.test(ext)) || oat_num>8)
				{
					$('#UploadInfo').html(errstr);
					$('#UploadInfo').addClass('Error');
					$('#UploadInfo').fadeIn();
					return false;
				}
			},
			onComplete: function(file, response) {
				tmp_str = response.split('|');
				$('#xx').show();
				$('#UploadInfo').show();
				if (tmp_str.length < 2)
				{
					$('#UploadInfo').html(response);
					$('#UploadInfo').addClass('Error');
				}
				else if (tmp_str[0] != 'TRUE')
				{
					$('#UploadInfo').html(tmp_str[1]);
					$('#UploadInfo').addClass('Error');
				}
				else
				{
					$('#UploadInfo').html(file + '上传成功！');
					if ($('#AddAttach').attr('class') == 'Single')
					{
						$('#AttachmentList').empty();
						$('#AttachmentList').append('<li><img src="' + tmp_str[1] + '" border="0" /></li>');
						$('#Attachments').val(tmp_str[1]);
					}
					else
					{
						//alert(tmp_str[3]);
						//$('#AttachmentList').append('<li><img src="' + tmp_str[1] + '" border="0" /></li>');
						$('#AttachmentList').append('<li id="'+tmp_str[3].replace('\r', '').replace('\n', '')+'">"'+file+'"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="'+tmp_str[3].replace('\r', '').replace('\n', '')+'_sub" onclick="delattach(\''+tmp_str[1].replace('\r', '').replace('\n', '')+'\', \''+tmp_str[2].replace('\r', '').replace('\n', '')+'\', \''+tmp_str[3].replace('\r', '').replace('\n', '')+'\')" style="cursor:pointer;text-decoration:underline; color:#0e6000">删除附件</span></li>');
						$('#Attachments').val($('#Attachments').val() + ',' + tmp_str[1]);
					}
				}
				$('#UploadInfo').fadeIn();
			}
		});
	}

	// 如果等待层不存在
	if (!$('#Waiting').length) $('body').append('<div id="Waiting"></div><div id="ProgressMessage"></div>');

	$('#btnError').live('click', function () { showAnt(); $('.tform').removeAttr('disable'); $('.img_vertify').click();});
	$('#btnNotice').live('click', function () { showAnt(); $('.tform').removeAttr('disable'); $('.img_vertify').click();dourl();});
	$('#btnConfirmOk').live('click', function () { showAnt(); $('.tform').removeAttr('disable');window.location.href="/index.php/first/register" });
	$('#btnConfirmcancel').live('click', function () { showAnt(); $('.tform').removeAttr('disable'); window.location.href="/index.html" });

	$('#checkboxSelectAll').live(
		'click',
		function () {
			form_name = $(this).attr('target');
			if (!$(this).attr('checked')) $('form[name=' + form_name + '] :checkbox').attr('checked', false);
			else $('form[name=' + form_name + '] :checkbox').attr('checked', true);
		}
	);

	// 装载图片文件
	var loading_image = '/images/loading.gif';
	/**
	 * 显示隐藏状态层
	 * @param {String} progress_mesg 处理状态描述。支持HTML
	 */
	var showOrHideWaitingLayer = function (progress_title, progress_mesg)
	{
		$('#Waiting').height(Math.max($('body').height(), $(window).height()));
		mesg = '<p class="title">' + progress_mesg + '</p><p align="center"><img src="' + loading_image + '" border="0" /></p><p align="center">' + progress_mesg + '</p>';
		$('#ProgressMessage').html(mesg);
		$('#ProgressMessage').css('top', ($(window).height() - $('#ProgressMessage').height()) / 2 + 'px');
		$('#ProgressMessage').css('left', ($(window).width() - $('#ProgressMessage').width()) / 2 + 'px');
		showAnt();
	};
	/**
	 * 显示浮动层内容
	 **/
	var showMessage = function (type, title, mesg)
	{
		button_list = '';
		mesg_print = '';
		switch (type)
		{
			case 'Error': button_list += '<button id="btnError">确定</button>'; break;
			case 'Notice': button_list += '<button id="btnNotice">确定</button>'; break;
			case 'Notice1': button_list += '<br/><a style="text-decoration:underline" href="/index.php/homepage/login/buy30"><font color="#fff">现在登录</font></a>&nbsp;&nbsp;<a style="text-decoration:underline" href="/index.php/homepage/register/buy30"><font color="#fff">现在注册</font></a>'; break;
			case 'Notice2': button_list += '<br/><a style="text-decoration:underline" href="/index.php/homepage/login/buy43"><font color="#fff">现在登录</font></a>&nbsp;&nbsp;<a style="text-decoration:underline" href="/index.php/homepage/register/buy43"><font color="#fff">现在注册</font></a>'; break;
			case 'Confirm': button_list += '<button class="btn1" id="btnConfirmOk">我已了解并同意，现在开始填写</button> <button class="btn2" id="btnConfirmcancel">我已了解并同意，暂不填写</button>'; break;
		
			
		}
		if ('' != title) mesg_print = '<div class="title">' + title + '</div>';
		mesg_print += '<div style="text-align:left;padding:2%;border:1px solid #CC6633;">' + mesg + '<p align="right">' + button_list + '</p></div>';
		$('#ProgressMessage').html(mesg_print);
	};
	
	var dourl =function()
	{
		if($('#returnurl').length)
		{
			window.location.href=$('#returnurl').val();
		}
		else
		{
			return false;
		}
	}
	/**
	 * 显示效果
	 **/
	var showAnt = function ()
	{
		if ($('#Waiting').css('display') != 'none')
		{
			$('select').show();
			$('#Waiting').hide();
		}
		else
		{
			$('select').hide();
			$('#Waiting').show();
		}
		if ($('#ProgressMessage').css('display') != 'none')
		{
			$('#ProgressMessage').fadeOut('normal');
		}
		else $('#ProgressMessage').fadeIn('fast');
	}

	
		$('.tform').submit(function ()
	{
		if ($(this).attr('disable') != 'disable')
		{
			$(this).attr('disable', 'disable');
			showOrHideWaitingLayer($(this).attr('title'), '正在处理表单，请等待...');
			$.ajax({
				type: $(this).attr('method'),
				url: $(this).attr('action'),
				dataType: 'html',
				data:$(this).formSerialize(),
				success: function (html_data)
				{
					tmp_str = html_data.split('|');
					if (tmp_str.length > 1)
					{
						if ('TRUE' != tmp_str[0]) showMessage('Error', '提交失败', tmp_str[1]);
						else
						{
							showMessage('Notice', '提交成功', tmp_str[1]);
							if ($('#tjbutton').length)
							$('#tjbutton').hide();
							if ($('#zclinks').length)
							$('#zclinks').show();
						}
					}
					else showMessage('Error', '提交失败', html_data);
				},
				error: function (XMLHttpRequest, textStatus, errorThrown)
				{
					showMessage('Error', '提交失败', textStatus);
				},
				cache: false,
				complete: function (XMLHttpRequest, textStatus)
					{
						//showOrHideWaitingLayer('');
					}
			});
		}
		return false;
	});

	// 所有ajax表单
	$('.tform1').submit(function ()
	{
		if ($(this).attr('disable') != 'disable')
		{
			showOrHideWaitingLayer($(this).attr('title'), '正在处理表单，请等待...');
			$.ajax({
				type: $(this).attr('method'),
				url: $(this).attr('action'),
				dataType: 'html',
				data:$(this).formSerialize(),
				success: function (html_data)
				{
					tmp_str = html_data.split('|');
					if (tmp_str.length > 1)
					{
						if ('TRUE' != tmp_str[0]) showMessage('Error', '提交失败', tmp_str[1]);
						else
						{
							showMessage('Confirm', '提交成功', tmp_str[1]);
						}
					}
					else showMessage('Error', '提交失败！', html_data);
				},
				error: function (XMLHttpRequest, textStatus, errorThrown)
				{
					showMessage('Error', '提交失败', textStatus);
				},
				cache: false,
				complete: function (XMLHttpRequest, textStatus)
					{
						//showOrHideWaitingLayer('');
						
					}
			});
		}
		return false;
	});
	
	$('.linkface').click(function ()
	{
		showOrHideWaitingLayer($(this).attr('title'), '正在提交数据，请等待');
		//showAnt();
		showMessage('Notice1', '温馨提示', '请先登录后再购买此商品！谢谢！');
		return true;
	});
	
	$('#linkface1').click(function ()
	{
		showOrHideWaitingLayer($(this).attr('title'), '正在提交数据，请等待');
		//showAnt();
		showMessage('Notice', '提示', '敬请关注优惠购买网站开通');
		return true;
	});
	
	$('.linkface3').click(function ()
	{
		showOrHideWaitingLayer($(this).attr('title'), '正在提交数据，请等待');
		//showAnt();
		showMessage('Notice2', '温馨提示', '请先登录后再购买此商品！谢谢！');
		return true;
	});
	
});

