//表单验证
function CheckForm(form_id)
{
	var form=$("#"+form_id);
	var elements = form.find('input[require],select[require],textarea[require]');

	var state=true;
	elements.each(function(){
		if($(this).val().length<1)
		{
			if($(this).attr('msg'))
			alert($(this).attr('msg'));
			$(this).focus();
			state=false;
			return false;
		}
	});

	return state;
}

//设置显示模式
function SetDisplayMode(display_mode)
{
	var expires=new Date();
	//这是显示模式的设置
	expires.setTime(expires.getTime()+1000*60*60*24);
	document.cookie='display_mode='+display_mode+';expires='+expires.toGMTString+';path=/';
	//下面是决定是否回馈304的COOKIE，不然用COOKIE的方式控制显示模式将失败，永远停留在一个页面(我好聪明)
	expires.setTime(expires.getTime()+1000*20);//20秒
	document.cookie='refresh_display=1;expires='+expires.toGMTString+';path=/';
	window.location.reload();
}

//以w与h中比较小的一个为标准按比例缩放
//参数losse表示是否根据大小自动忽略设置的图片宽度和高度(最小值的那个): 
//为空表示强制；为true表示松散根据原大小设置(如果设定值太大则忽略之而采用原大小)
function setpicWH(ImgD,w,h,loose)
{
	var temp_w=null;
	var temp_h=null;
	var image=new Image();
	image.src=ImgD.src;
	if(loose)
	{
		if(w>image.width&&h>image.height)
		return;
	}
	if(w<h)
	{
		temp_w=w;
		temp_h=(w/image.width)*image.height;
		while(temp_h>h)
		{
			temp_w--;
			temp_h=(temp_w/image.width)*image.height;
		}
	}
	else
	{
		temp_h=h;
		temp_w=(h/image.height)*image.width;
		while(temp_w>w)
		{
			temp_h--;
			temp_w=(temp_h/image.height)*image.width;
		}
	}
	
	ImgD.style.width=temp_w+'px';
	ImgD.style.height=temp_h+'px';
}

//采用AJAX获取验证号码
function SetValidation()
{
	$('#Validation').html('<img src="images/loading.gif"> 数据加载中... ');
	$.ajax({
		url: "ajax.php",
		type:"post",
		dataType:"html",
		data: "ajax_action=SetValidation",
		success:function(msg){
			$("#Validation").html(msg);
		},
		error:function(msg){
			$("#Validation").html("Ajax错误状态码: "+msg.status);
		}
	});
}

//添加收藏夹
function myAddPanel(title,url)
{
    if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function'))
    {
        window.sidebar.addPanel(title,url,"");
    }
    else
    {
        window.external.AddFavorite(url,title);
    }
}

//判断浏览器是否为IE
function isIE()
{
	if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1)
	return true;
	else
	return false;
}


//显示自定义层，内容标题自定义，使用AJAX获取内容
function ShowAjaxDiv(width,height,url,is_iframe)
{
	//$('embed').hide();
	//$('object').hide();
	if($('#phpme_ajax_div').html())
	{
		//调整高度和宽度
		$('#phpme_ajax_div').css('width',width);
		$('#phpme_ajax_div').css('height',height);
		
		//层居中调整
		var height = document.getElementById("phpme_ajax_div").offsetHeight;
		var bodyHeight = isIE()?document.documentElement.offsetHeight:document.documentElement.clientHeight;
		$('#phpme_ajax_div').css('top',document.documentElement.scrollTop +30+"px");
		$('#phpme_ajax_div').css("left",(($(document).width())/2-(parseInt($('#phpme_ajax_div').width())/2))+"px");
		
		$("#phpme_ajax_div_content").html('<img src="images/loading.gif"> 数据加载中... ');
		$('#phpme_ajax_div').show();
	}
	else
	{
		$('body').append('<div id="phpme_ajax_div" class="jqDnR" style="width:'+width+'px;height:'+height+'px;left:0px;top:30px;">'
		+'<div id="phpme_ajax_div_title" class="jqDrag"><a href="javascript:HideAjaxDiv();">关闭</a></div>'
		+'<div id="phpme_ajax_div_content"><img src="images/loading.gif"> 数据加载中... </div>'
		+'</div>');

		//层居中调整
		var height = document.getElementById("phpme_ajax_div").offsetHeight;
		var bodyHeight = isIE()?document.documentElement.offsetHeight:document.documentElement.clientHeight;
		$('#phpme_ajax_div').css('top',document.documentElement.scrollTop + ((bodyHeight - height)/2)+ "px");
		$('#phpme_ajax_div').css("left",(($(document).width())/2-(parseInt($('#phpme_ajax_div').width())/2))+"px");
		
		$('#phpme_ajax_div').jqDrag('.jqDrag');
	}

	$("#phpme_ajax_div").css('opacity',100);
	
	if(is_iframe)
	{
		$("#phpme_ajax_div_content").html('<iframe src="'+url+'" border="0" frameborder="0" style="border:0px;border-style:none;width:100%;height:100%;"></iframe>');
	}
	else
	{
		$.ajax({
			url: url,
			type:"get",
			dataType:"html",
			success:function(msg){
				$("#phpme_ajax_div_content").html(msg);
			},
			error:function(msg){
				$("#phpme_ajax_div_content").html("Ajax错误状态码: "+msg.status);
			}
		});
	}
	
}
function HideAjaxDiv()
{
	$('#phpme_ajax_div').hide();
	$('embed').show();
	$('object').show();
}

//处理用户自定义的列表显示
var working=false;
function DealUserCustom()
{
	if(working)
	{
		alert('操作处理中，请稍侯...');
		return;
	}
	working=true;
	var temp=$('#custom_oper').html();
	var custom_type=$('#custom_type').val();
	var custom_size=$('#custom_size').val();
	$('#custom_oper').html('<img src="images/loading.gif"> 请稍候 - 操作处理中...　')
	$.ajax({
		url: "ajax.php",
		type:"post",
		dataType:"html",
		data: "ajax_action=DealUserCustom&custom_type="+custom_type+"&custom_size="+custom_size,
		success:function(msg){
			if(confirm('设置已保存，如果浏览器有缓存，你可以刷新页面查看效果...'))
			window.location.reload();
			else
			$('#custom_oper').html('设置已保存，如果浏览器有缓存，你可以刷新页面查看效果...　');
			working=false;
		},
		error:function(msg){
			working=false;
			alert('处理过程中遇到错误，请刷新页面后重新设置...');
			$('#custom_oper').html("Ajax错误状态码: "+msg.status+'　 ');
		}
	});
}


//复制到剪贴板函数，兼容不同的浏览器
function CopyToClipboard(txt) 
{
    if(window.clipboardData) 
    {
            window.clipboardData.clearData();   
            window.clipboardData.setData("Text", txt);   
    }
    else if(navigator.userAgent.indexOf("Opera") != -1) 
    {
         window.location = txt;   
    }
    else if (window.netscape) 
    {
         try 
         {   
              netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");   
         } 
         catch (e) 
         {
              alert("剪贴板复制操作被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'"); 
              return false;  
         }   
         var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); 
         if (!clip) return false;   
         var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);   
         if (!trans) return fasle;   
         trans.addDataFlavor('text/unicode');   
         var str = new Object();   
         var len = new Object();   
         var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);   
         var copytext = txt;   
         str.data = copytext;   
         trans.setTransferData("text/unicode",str,copytext.length*2);   
         var clipid = Components.interfaces.nsIClipboard;   
         if (!clip)return false;   
         clip.setData(trans,null,clipid.kGlobalClipboard);
    }
    return true;  
}

//获得引用通告地址
function GetTrackbackURL()
{
	var url=$('#TrackbackURL').text();
	if(CopyToClipboard(url))
	alert('引用通告地址已复制到了剪贴板...');
}













//增加并显示浏览数
function ShowClicked(id,cat_mode)
{
	$('#clicked').html('<img src="images/loading.gif">')
	$.ajax({
		url: "ajax.php",
		type:"post",
		dataType:"html",
		data: "ajax_action=ShowClicked&id="+id+"&cat_mode="+cat_mode,
		success:function(msg){
			$('#clicked').html(msg);
		},
		error:function(msg){
			alert("Ajax错误状态码: "+msg.status);
			$('#clicked').html('<font color="red">错误</font>')
		}
	});
}

//显示处理时间
function ShowExecutedTime()
{
	$('#ExecutedTime').html('<img src="images/loading.gif">');

	$.ajax({
		url: "ajax.php",
		type:"post",
		dataType:"html",
		data: '&ajax_action=ShowExecutedTime',
		success:function(msg){
			$('#ExecutedTime').html(msg);
		},
		error:function(msg){
			$('#ExecutedTime').html('Ajax错误状态码: '+msg.status);
		}
	});
}

//添加访问信息,处理网站访客统计信息
function DealClientInfo()
{
	$.ajax({ 
		url: "ajax.php",
		type:"post",
		dataType:"html",
		data: "ajax_action=DealClientInfo"
	});
}


//取得项名称为offset的cookie值 
function GetCookieVal (offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
//取得名称为name的cookie值 
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return GetCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}
//删除名称为name的Cookie
function DeleteCookie (name) 
{   
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
//清除COOKIE
function ClearCookies()
{
	if(confirm('你确定要清空所有COOKIE吗？'))
	{
		var temp=document.cookie.split(";");
		var ts;
		for (var i=0;;i++)
		{
			if(!temp[i])break;
			ts=temp[i].split("=")[0];
			DeleteCookie(ts);
		}
		alert('COOKIE已清除！');
	}
}

//查找网页内宽度和高度太大的图片进行缩放，自定义最大宽和高
//通过var image=new Image();image.width获取的图象的宽和高是原图象大小;
//通过$(this).width()获取的图象的宽和高是经过属性或者CSS设置后的大小,所以用$(this).width()比较适合
function ReImgSize(max_w,max_h){
	//去掉属性的宽和高，这种属性设置有时会使IE的图象变的张牙舞爪
	//并且如果太大的图片设置了小的宽度和高度则看不到原图象
	$('.Content img').removeAttr('width');
	$('.Content img').removeAttr('height');

	var src=null;
	var width=null;
	var height=null;
	var image=new Image();
	$('.Content img').each(function() {//不使用load方法而改用each
		src=$(this).attr('src');
		width=$(this).width();
		height=$(this).height();
		if(width<1||height<1)
		{
			image.src=$(this).attr('src');
			width=image.width;
			height=image.height;
		}
		
		if(width>max_w)
		{
			$(this).width(max_w);
			$(this).wrap('<a href="'+src+'" target="_blank" title="在新窗口打开图片"></a>');//包裹
		}
		else if(height>max_h)
		{
			$(this).height(max_h);
			$(this).wrap('<a href="'+src+'" target="_blank" title="在新窗口打开图片"></a>');//包裹
		}
	});
}

$(document).ready(function(){
	DealClientInfo();//执行
	ReImgSize(650,800);
});