var w3c=(document.getElementById)? true: false;
var agt=navigator.userAgent.toLowerCase();
var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));

function IeTrueBody()
{
	return (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body;
}

function GetScrollTop()
{
	return ie ? IeTrueBody().scrollTop : window.pageYOffset;
}
function getPageScroll()
{
	var yScroll;
	if (self.pageYOffset) yScroll = self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop) yScroll = document.documentElement.scrollTop;
	else if (document.body) yScroll = document.body.scrollTop;
	arrayPageScroll = new Array('',yScroll);
	return arrayPageScroll;
}

function getPageSize()
{
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY)
	{
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else
	{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight)
	{  // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight) pageHeight = windowHeight;
	else pageHeight = yScroll;
	if(xScroll < windowWidth) pageWidth = windowWidth;
	else pageWidth = xScroll;
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,xScroll,yScroll);
	return arrayPageSize;
//return pageHeight;
}


$(document).ready(function () {
	// 上传
	if ($('#AddAttach').length)
	{
	
		new Ajax_upload('#AddAttach', {
			action : '/index.php/admin/Filesystem/upload',	// 上传文件的服务器端
			name : 'myfile',	// 上传文件字段名
			onSubmit : function (file, ext) {
				var errstr		= '不允许上传该类型的文件';
				var oat_num		= document.getElementsByName('attaches')[0].value.split(',').length;
				if(oat_num>3) errstr	= '您一次上传的文件不能超过3个!';
				if (! (ext && /^(jpg|png|jpeg|gif|bmp)$/.test(ext)) || oat_num>3)
				{
					$('#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
					{
						//$('#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();
			}
		});
	}


	var $trs=$(".tr_list"); //选择所有行
	$trs.filter(":even").css("background","#ffffff"); 
	
	
	// 为表单中的提交按钮添加事件
	$('.tform').unbind('submit');
	$('.tform').submit(function () {
		if(isEmail())
			postPageContent($('.tform').attr('action'), $('.tform').formSerialize());
		else{
			alert("提示\n\n邮箱格式不正确");
			$("#email").select();
		}
		return false;
		
	});
	
	function isEmail() {
		if($("#email").val() != "" && $("#email")[0] != undefined) {
			var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
			return flag = reg.test($("#email").val());
		}
		return true;
	};
	
		// 关闭信息框
	var closeMessageWindows = function () {
		$('#MaskLayer').hide();
		$('#StatusLayer').hide();
		$('#WaitingLayer').hide();
		return true;
	};
	
	/**
		 * 显示等待内容
		 */
	var showWaitingLayer = function () {
		//$('#StatusLayer').hide();
		$('#MaskLayer').height($(document).height());
		$('#MaskLayer').show();
		page_size = getPageSize();
		$('#WaitingLayer').css('top', GetScrollTop() + (page_size[3] - $('#WaitingLayer').height()) / 2 + 'px');
		$('#WaitingLayer').show();
	};
	
	// AJAX GET方式获取内容
	var getPageContent = function (url, showSuccess) {
		//显示等待
		showWaitingLayer();
		$.ajax({
			type : 'GET',
			url : url,
			dataType : 'html',
			error: showErrorMessage,
			success : showSuccess
		});
	};
	
		// AJAX POST 方式获取内容
	var postPageContent = function (url, data) {
		showWaitingLayer();
		$.ajax({
			type : 'POST',
			url : url,
			data : data,
			dataType : 'html',
			error: showErrorMessage,
			success : showStatusLayer
		});
	};
	// 显示错误信息
	var showErrorMessage = function (XMLHttpRequest, textStatus, errorThrown) {
		showStatusLayer(XMLHttpRequest.responseText);
	};
	
	var showStatusLayer = function (html_data) {
			$('#WaitingLayer').hide();
			$('#MaskLayer').height($(document).height());
			$('#MaskLayer').show();
			$('#StatusLayer').html(html_data);
			page_size = getPageSize();
			$('#StatusLayer').css('top', GetScrollTop() + (page_size[3] - $('#StatusLayer').height()) / 2 + 'px');
			$('#StatusLayer').show();
		};
		
	$('#CloseButton').click(closeMessageWindows);
	$('.CloseButton').click(closeMessageWindows);

})
