﻿//Common JS Function

$.postJSON = function(url,data,callback){
	$.post(url,data,callback,'json');
}
$.fixEvent = function(e){
	var evt = (typeof e == "undefined") ? window.event : e;
	return evt;
}
$.srcElement = function(e)
{
	if (typeof e == "undefined") e = window.event;
  	var src = document.all ? e.srcElement : e.target;
  	return src;
}
$.isTime = function(val)
{
  	var reg = /^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}$/;
  	return reg.test(val);
}
$.request = function(url, item)
{
	var sValue=url.match(new RegExp("[\?\&]"+item+"=([^\&]*)(\&?)","i"));
	return sValue?sValue[1]:sValue;
}
$.isNumber = function(val)
{
  	var reg = /^[\d|\.|,]+$/;
  	return reg.test(val);
}

$.isInt = function(val)
{
  	if (val == "")
  	{
   		return false;
  	}
  	var reg = /\D+/;
  	return !reg.test(val);
}


function quicksave(obj,url,id)
{
	var tag = obj.firstChild.tagName;
  	if (typeof(tag) != "undefined" && tag.toLowerCase() == "input")
  	{
    	return;
  	}
  	/* 保存原始的内容 */
  	var org = obj.innerHTML;
  	var val = $.browser.msie ? obj.innerText : obj.textContent;

  	/* 创建一个输入框 */
  	var txt = document.createElement("INPUT");
  	txt.value = (val == 'N/A') ? '' : val;
  	txt.style.width = (obj.offsetWidth + 12) + "px" ;

  	/* 隐藏对象中的内容，并将输入框加入到对象中 */
  	obj.innerHTML = "";
  	obj.appendChild(txt);
  	txt.focus();

  	/* 编辑区输入事件处理函数 */
  	txt.onkeypress = function(e)
  	{
    	var evt = $.fixEvent(e);
    	var obj = $.srcElement(e);

    if (evt.keyCode == 13)
    {
      obj.blur();

      return false;
    }

    if (evt.keyCode == 27)
    {
      obj.parentNode.innerHTML = org;
    }
  }

  /* 编辑区失去焦点的处理函数 */
  txt.onblur = function(e)
  {
    if ($.trim(txt.value).length > 0)
    {

	  $.postJSON(url,{val:$.trim(txt.value),id:id},function(data){
			if(data.message){
				alert(data.message);
			}
			obj.innerHTML = (data.error == 0) ? data.content : org;
		});
	}
	else
	{
		obj.innerHTML = org;
	}
  }
}

function scrollNews(selector,Entry,time,StartIndex)
{
	var _self=this;
	this.Selector=selector;
	this.Entry=Entry;
	this.time = time;
	this.i=StartIndex||0;
	this.Count=$(this.Selector+" ul li").length;
	$(this.Selector+" ul li").hide();//全部隐藏
	$(this.Selector+" ul li").eq(this.i).show();//第i个显示
	$(this.Selector).bind("mouseenter",function(){
	    	if(_self.sI){clearInterval(_self.sI);}
	}).bind("mouseleave",function(){
			_self.showIndex(_self.i++);
	})
	/*生成激活OL项目*/
	for(var j=0;j<this.Count;j++)
		$(this.Selector+" .activeOL").append('<li><a onclick="'+this.Entry+'.showIndex('+j+');" href="#"><img src="/images/crystal.gif"></a></li>');
	$(this.Selector+" ol li a").eq(this.i).addClass("active");
	this.sI=setInterval(this.Entry+".showIndex(null)",this.time);
	
	this.GetSelector=function(){return this.Selector;}
	this.showIndex=function(index)
	{
		this.i++;//显示下一个
		if(this.sI){clearInterval(this.sI);}
		this.sI=setInterval(this.Entry+".showIndex()",this.time);
		if (index!=null)
		{
			this.i=index;
		}
		if(this.i==this.Count)
			this.i=0;
		$(this.Selector+" ul li").hide();
		$(this.Selector+" ul li").eq(this.i).slideDown();
		$(this.Selector+" ol li a").removeClass("active");
		$(this.Selector+" ol li a").eq(this.i).addClass("active");
	}
}
/**滚动新闻展示效果
code by CssRain--橘子.
转载请注明:http://www.cssrain.cn
// 参数1：ID    		
// 参数2：实例化对象的名称（跟var 后的相同）
// 参数3：间隔时间      
// 参数4：初始化时，默认哪个先显示
**/

function drawimage(ImgD,iwidth,iheight)
{ 
	var image=new Image(); 
	if(ImgD.length > 0 )
	{
		for(var i=0;i<ImgD.length;i++)
		{
			ImgDc = ImgD[i];
			image.src=ImgDc.src;
			if(image.width>0 && image.height>0){ 
				if(image.width/image.height>= iwidth/iheight){ 
					if(image.width>iwidth){   
						ImgDc.width = iwidth; 
						ImgDc.height = (image.height*iwidth)/image.width; 
						$('#bl').text(ForDight(image.width/iwidth));
					}else{ 
						ImgDc.width = image.width;   
						ImgDc.height = image.height; 
						$('#bl').text('1');
					} 
				} 
				else{ 
					if(image.height>iheight){   
						ImgDc.height = iheight; 
						ImgDc.width= (image.width*iheight)/image.height;   
						$('#bl').text(ForDight(image.height/iheight));
					}else{ 
						ImgDc.width= image.width;   
						ImgDc.height = image.height; 
						$('#bl').text('1');
					} 
				} 
			} 
		}
	}
	else
	{
		image.src=ImgD.attr('src');
		if(image.width>0 && image.height>0){ 
			if(image.width/image.height>= iwidth/iheight){ 
				if(image.width>iwidth){   
					ImgD.attr('width',iwidth); 
					ImgD.attr('height',(image.height*iwidth)/image.width); 
					$('#bl').text(ForDight(image.width/iwidth));
				}else{ 
					ImgD.attr('width',image.width);   
					ImgD.attr('height',image.height); 
					$('#bl').text('1');
				} 
			} 
			else{ 
				if(image.height>iheight){   
					ImgD.attr('height',iheight); 
					ImgD.attr('width',(image.width*iheight)/image.height);   
					$('#bl').text(ForDight(image.height/iheight));
				}else{ 
					ImgD.attr('width',image.width);   
					ImgD.attr('height',image.height); 
					$('#bl').text('1');
				} 
			} 
		} 
	}
}
function ForDight(Dight)   
{   var How = 4
    Dight = Math.round(Dight*Math.pow(10,How))/Math.pow(10,How);   
    return Dight;   
} 
function ForDight_0(Dight)   
{   var How = 0
    Dight = Math.round(Dight*Math.pow(10,How))/Math.pow(10,How);   
    return Dight;   
}
function ShowSiteList()
{
	$('#sitelist').show();	
}
function HideSiteList()
{
	$('#sitelist').hide();	
}
function ShowAreaList()
{
	if($('#poparea').css('display')=='none')
	$('#poparea').show();
	else
	HideAreaList();
}
function HideAreaList()
{
	SetCookie('areaurl',$('input[@name=a][@checked]').val(),7);
	$('#poparea').hide();
}
function GoArea()
{
	var url = $('input[@name=a][@checked]').val();	
	var curl = $('#ca').val();
	SetCookie('areaurl',url);
	if(url == curl)
	{
		$('#poparea').hide();	
	}
	else
	{
		window.location.href = url;	
	}
}
var areaurl = getCookie('areaurl');

function len(s) { 
	var l = 0; 
	var a = s.split(""); 
	for (var i=0;i<a.length;i++) { 
	if (a[i].charCodeAt(0)<299) { 
	l++; 
	} else { 
	l+=2; 
	} 
	} 
	return l; 
}
function setfail(tipid,showmsg)
{
	var tip = $("#"+tipid);
	tip.removeClass();
	tip.addClass("onError");
	tip.html(showmsg);
}
function RndNum(n){
	var rnd="";
	for(var i=0;i<n;i++)
	rnd+=Math.floor(Math.random()*10);
	return rnd;
}
function SetCookie(name,value,day)//两个参数，一个是cookie的名子，一个是值
{
	name = cookiepre+name;
	var argc = SetCookie.arguments.length;
	var argv = SetCookie.arguments;
	var secure = (argc > 5) ? argv[5] : false;
	if(day==null || day==0) day=1;
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + day*24*60*60*1000);
	document.cookie = name + "=" + escape(value) + ("; path=" + cookiepath) + ((cookiedomain == '') ? "" : ("; domain=" + cookiedomain)) + ((secure == true) ? "; secure" : "") + ";expires="+exp.toGMTString();
	for(i=0;i<domainlist.length;i++)
	{
		document.cookie = name + "=" + escape(value) + ("; path=" + cookiepath) + ((domainlist[i] == '') ? "" : ("; domain=" + domainlist[i])) + ((secure == true) ? "; secure" : "") + ";expires="+exp.toGMTString();
	}
}
function getCookie(name)//取cookies函数        
{
	name = cookiepre+name;
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
	name = cookiepre+name;
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

