//浏览器类型
var FBrowser=new Object();
FBrowser.isIE=((navigator.userAgent.indexOf('MSIE')==-1)?false:true);
FBrowser.isIE7=((FBrowser.isIE && window.XMLHttpRequest)?true:false);
FBrowser.isIE6=((FBrowser.isIE && !window.XMLHttpRequest && window.ActiveXObject)?true:false);
FBrowser.isFirefox=((navigator.userAgent.indexOf('Firefox')==-1)?false:true);
FBrowser.isOpera=((navigator.userAgent.indexOf('Opera')==-1)?false:true);

String.prototype.lTrim=function(){return this.replace(/^\s*/, "");}

String.prototype.rTrim=function(){return this.replace(/\s*$/, "");}

String.prototype.trim=function(){return this.rTrim().lTrim();}

String.prototype.hasChinese=function(){return /[^\x00-\xff]/g.test(this);}

String.prototype.onlyChinese=function(){return /^[\u0391-\uFFE5]+$/g.test(this);}

String.prototype.onlyNumber=function(){return /^[1-9]\d*$/.test(this);}

//取得字符串实际长度(汉字算两个字节,英文字母算一个字节)
String.prototype.getLength=function()
{
  return this.replace(/[^\x00-\xff]/gi,'xx').length;
}

//处理包含中文的字符串
String.prototype.cut=function(start,len)
{
	var n=0;
	var s="";
	for(var i=0;i<this.length;i++)
	{
		if(i<start) continue;
		n+=(this.charCodeAt(i)>128)?2:1;
		if(n>len) return s;
		s+=this.charAt(i);
	}
	return s;
}

function Farray_exist(d,v)
{
	for(var i=0;i<d.length;i++)
	{
		if(d[i]==v) return true;
	}
	return false;
}

window.clearRunInterval=window.clearInterval;
window.clearRunTimeout=window.clearTimeout;

/**
 * @param fn  函数的引用,函数不能用字符串引起来
 * @param dt  间隔时间
 * 
 * @return bool   正确返回true,否则返回false
 * 例子:setRunTimeout(fun,1000,1,2,4)
 */
window.setRunTimeout=function(fn,dt)
{
	if(typeof(fn)!='function') return false;
	var p=new Array();
  var c=arguments.length;
	if(c>2)
	{
		for(var i=2;i<c;i++)	p[i-2]=arguments[i];
	}
	var f=function(){fn.apply(null,p)}
	return window.setTimeout(f,dt);
}

/**
 * @param fn   函数的引用,函数不能用字符串引起来
 * @param dt  间隔时间
 * 
 * @return bool  正确返回true,否则返回false
 * 例子:setRunInterval(fun,1000,1,2,4)
 */
window.setRunInterval=function(fn,dt)
{
	if(typeof(fn)!='function') return false;
	var p=new Array();
	var c=arguments.length;
	if(c>2)
	{
		for(var i=2;i<c;i++)	p[i-2]=arguments[i];
	}
	var f=function(){fn.apply(null,p)}
	return window.setInterval(f,dt);
}

//document.getElementById的简写
function Fid(id)
{
	return document.getElementById(id);
}

//document.getElementById的简写
function Fname(name)
{
	return document.getElementsByName(name);
}

function FtagName(name)
{
	return document.getElementsByTagName(name);
}

function Fempty(v){
	if(v!=null && (typeof(v)=='object' || typeof(v)=='function')) return false;
	return ((""==v || undefined==v || null==v)?true:false);
}

//字符串过滤的函数
function FxmlEncode(s)
{
	return s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\'/g,"&apos;").replace(/\"/g,"&quot;");
}

//FxmlEncode 的反函数
function FxmlDecode(s)
{
	return s.replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&apos;/g,"'").replace(/&quot;/g,"\"");
}

//对URL参数的分析,需要进行详细的分析
function FgetURLArgs()
{
	var q=location.search.substring(1).replace("&amp;","&").split("&");
	var p=new Object();
	var c=q.length;
	for(var i=0;i<c;i++)
	{
		var pos=q[i].indexOf('=');
		if (-1==pos) continue;
		p[q[i].substring(0,pos)]=unescape(q[i].substring(pos+1));
	}
	return p;
}

//判断是否对象是否是指定的tagName
function FisTagName(e,tagName)
{
	return ((e.tagName.toUpperCase()==tagName.toUpperCase())?true:false);
}

function FaddOptionToSelect(id,txt,v,selected)
{
	var e=Fid(id);
	if(Fempty(e) || !FisTagName(e,'select')) return false;
	var s=((undefined==selected || true!=selected)?false:true);
	e.options[e.options.length]=new Option(txt,v,s,false);
	return true;
}

//清空一个或多个或全部
function FclearOptionsOfSelect(id)
{
	var e=Fid(id);
	if(Fempty(e) || !FisTagName(e,'select')) return false;
	var p=e.options;
	var c=p.length;
	for(var i=c;i>=0;i--)
		p[i]=null;
	
}

//注意单选和多选,如果v为空,所有的option都设置成state,否则
function FsetValuesOfSelect(id,v,stat)
{
	var e=Fid(id);
	var v1=new Array();
	if(Fempty(e) || !FisTagName(e,'select')) return false;
	if(typeof(v)!='object'){
		v1[0]=v;
	}else{
		v1=v;
	}

	for(var i=0;i<e.options.length;i++)
	{
		e.options[i].selected=false;
		if(Fempty(v1))
			e.options[i].selected=stat;
		else if(Farray_exist(v1,e.options[i].value))
			e.options[i].selected=stat;
	}
}

//注意单选和多选
function FgetValuesOfSelect(id)
{
	var e=Fid(id);
	if(Fempty(e) || !FisTagName(e,'select')) return null;
	var v=new Array();
	var p=e.options;
	var c=p.length;
	for(var i=0,j=0;i<c;i++)
	{
		if(true==p[i].selected)	
			v[j++]=p[i].value;
	}
	return ((1==v.length)?v[0]:v)
}

//考虑全选
function FsetValuesOfCheckbox(name,v,stat)
{
	var e=Fname(name);
	if('Array'!=typeof(v) && !Fempty(v)) v=new Array(v);
	var c=e.length;
	for(var i=0;i<c;i++)
	{
		if(Fempty(e[i]) || e[i].type!='checkbox') continue;
		e[i].checked=false;
		if(Fempty(v))
			e[i].checked=stat;
		else if(Farray_exist(v,e[i].value))
			e[i].checked=stat;
	}
}

function FgetValuesOfCheckbox(name)
{
	var e=Fname(name);
	var v=new Array();
	var c=e.length;
	for(var i=0;i<c;i++)
	{
		if(Fempty(e[i]) || e[i].type!='checkbox') continue;
		if(e[i].checked==true) 
			v[v.length]=e[i].value;
	}
	return v;
}

function FsetValueOfRadio(name,v)
{
	var e=Fname(name);
	var c=e.length;
	for(var i=0;i<c;i++)
	{
		if(Fempty(e[i]) || e[i].type!='radio') continue;
		if(e[i].value==v) e[i].checked=true;
	}
}

function FgetValueOfRadio(name)
{
	var e=Fname(name);
	var c=e.length;
	for(var i=0;i<c;i++)
	{
		if(e[i]!='radio') continue;
		if(e[i].checked==true) return e[i].value;
	}
	return null;
}

//对cookie的操作
function FgetCookie(name)
{
	var r=new RegExp("(^|;|\\s+)"+name+"=([^;]*)(;|$)");
	var m=document.cookie.match(r);
	return(!m?"":m[2]);
}

function FaddCookie(name,v,path,expire,domain){
	var s=name+"="+escape(v);
	if(!Fempty(path)) path="/";
	if(expire>0){
		var d=new Date();
		d.setTime(d.getTime+expire*1000);
		if(!Fempty(domain))
			s=s+"; path="+path+"; domain="+domain+" expire="+d.toGMTString();
		else
			s=s+"; path="+path+"; expire="+d.toGMTString();
	}
	document.cookie=s;
}

function FdeleteCookie(name,domain)
{
	if(!Fempty(domain))
		document.cookie=name+"=;path=/;domain="+domain+";expires="+(new Date(0)).toGMTString();
	else
		document.cookie=name+"=;path=/;expires="+(new Date(0)).toGMTString();
}

//取得事件发生的元素
function FgetEventTarget(evt)
{
	return evt.target || evt.srcElement;
}

//屏蔽浏览器的差别
function FgetEvent(evt)
{
	return (!evt)?window.event:evt;
}

//判断是否左键被按下
function FisLeftKeyDown(evt)
{
	return (((evt.which) && (evt.which==1)) || ((evt.button) && (evt.button==1)));
}

//给对象增加一个事件,格式target必须是对象，evt格式"click"
function FaddEvent(e,evt,fn,isID)
{
	if(isID==true) e=Fid(e);
	if(!Fempty(e.attachEvent) && (typeof(e.attachEvent)=="function" || typeof(e.attachEvent)=="object"))
		e.attachEvent("on"+evt,fn);
	else if(!Fempty(e.addEventListener) && (typeof(e.addEventListener)=="function" || typeof(e.addEventListener)=="object"))
		e.addEventListener(evt,fn,false);
}

function FremoveEvent(e,evt,fun,isID)
{
	if(isID==true) e=Fid(e);
	if(!Fempty(e.detachEvent) && (typeof(e.detachEvent)=="function" || typeof(e.detachEvent)=="object"))
		e.detachEvent("on"+evt,fun);
	else if(!Fempty(e.removeEventListener) && (typeof(e.removeEventListener)=="function" || typeof(e.removeEventListener)=="object"))
		e.removeEventListener(evt,fun,false);
}

//阻止事件向上传递
function FstopEventTransfer(evt)
{
	if(evt.preventDefault){
		evt.stopPropagation();
		evt.preventDefault();
	}else{
		evt.returnValue=false;
		evt.cancelBubble=true;
	}
}

//禁止指定对象指定事件的传播,格式target必须是对象，evts格式"click,mousedown,mousedown"
function FstopObjectEventTransfer(e,evts)
{
	if(Fempty(e) || Fempty(evts)) return;
	var l=evts.split(",");
	var c=l.length;
	for(var i=0;i<c;i++)
	{
		var evt=l[i].trim();
		if(Fempty(evt)) continue;
		var fn=function(event){event=FgetEvent(event);FstopEventTransfer(event);}
		FaddEvent(e,evt,fn);
	}
}

//设置事件捕获
function FsetEventCapture(target)
{
	if(target.setCapture)
		target.setCapture();
	else{
	 if(!FBrowser.isFirefox && document.captureEvents)
		document.captureEvents(Event.MouseMove|Event.MouseUp);
	}
}

//释放事件捕获
function FreleaseEventCapture(target)
{
	if(target.releaseCapture)
		target.releaseCapture();
	else{
		if(!FBrowser.isFirefox && document.releaseEvents)
			document.releaseEvents(Event.MouseMove|Event.MouseUp);
	}
}

//获得页面窗口的大小
function FgetWindowSize()
{
	if(FBrowser.isOpera)
		return {width:document.body.clientWidth,height:document.body.clientHeight};
	else
		return {width:document.documentElement.clientWidth,height:document.documentElement.clientHeight};
}

//获得页面窗口的大小
function FgetPageSize()
{
	return {width:document.documentElement.scrollWidth,height:document.documentElement.scrollHeight};
}

//获得滚动的大小,此函数在IE FF中在刚刷新页面的时候不能取到真正的值，因为那时候IE FF正在初试化 opera下是正确的
function FgetScrollPostion()
{

		return {left:document.documentElement.scrollLeft,top:document.documentElement.scrollTop};
}

//需要区分页面位置和屏幕位置 还需要区分xhtml html
//获得当前指针在页面上的位置 有问题 在点击的时候要锁定滚动条
//网页是不是body?
function FgetPointerPostion(evt)
{
    if(evt.pageX || evt.pageY)  return {x:evt.pageX,y:evt.pageY};
    return {x:evt.clientX+document.documentElement.scrollLeft-document.documentElement.clientLeft,y:evt.clientY+document.documentElement.scrollTop-document.documentElement.clientTop};
}

//计算出对象在页面上的位置以及宽和高
function FgetPostion(e,isID)
{
	if(isID==true) e=Fid(e);
	var left=0,top=0,w=e.offsetWidth,h=e.offsetHeight;
	do{ 
		top+=e.offsetTop || 0; 
		left+=e.offsetLeft || 0; 
		e=e.offsetParent; 
	}while(e); 

	return {x:left,y:top,width:w,height:h};
}

//需要经过充分的测试，设置元素的页面位置 w=-1 h=-1不会设置这两个属性
function FsetPostion(e,x,y,w,h,isID){
	if(isID==true) e=Fid(e);
	var s=e.style;

	if(s.position=="absolute")
	{
		s.left=x+"px";
		s.top=y+"px";
	}else if(s.position=="relative")
	{
		var p=FgetPostion(e.offsetParent);
		s.left=(x-p.x)+"px";
		s.top=(y-p.y)+"px";
	}
	if(w>=0) s.width=w+"px";
	if(h>=0) s.height=h+"px";
}

//获得元素1相对与元素2的位置，在显示的时候没有改变
function FgetOffsetPostion(e1,e2)
{
	var p1=FgetPostion(e1);
	var p2=FgetPostion(e2);
	return {x:(p1.x-p2.x),y:(p1.y-p2.y)};
}

//把指定的元素移动到相对元素的位置
function FsetOffsetPostion(e1,e2,x,y,isID)
{
	if(isID==true){
		e1=Fid(e1);
		e2=Fid(e2);
	}
	var p=FgetPostion(e2);
	FsetPostion(e1,x+p.x,y+p.y);
}

//设置元素到另一个元素的指定位置，按比例
function FsetOffsetPostionByRate(e1,e2,nx,ny,isID)
{
	if(isID==true){
		e1=Fid(e1);
		e2=Fid(e2);
	}
	var s1=FgetPostion(e1);
	var s2=FgetPostion(e2);
	FsetPostion(e1,(s2.x+(s2.width-s1.width)/nx),(s2.y+(s2.height-s1.height)/ny),-1,-1);
}

//设置元素相对窗口的位置
function FsetOffsetWindowPostion(e,x,y,isID)
{
	if(isID==true) e=Fid(e);
	var p=FgetScrollPostion();
	FsetPostion(e,x+p.left,y+p.top,-1,-1);
}

//移动元素到窗口的指定位置，只按比例
function FsetOffsetWindowPostionByRate(e,nx,ny,isID)
{
	if(isID==true) e=Fid(e);
	var s=FgetWindowSize();
	FsetOffsetWindowPostion(e,(s.width-e.offsetWidth)/nx,(s.height-e.offsetHeight)/ny);
}

//判断两个元素是否有相同的父节点
function FhasSameParent(e1,e2,isID)
{
	if(isID==true)
	{
		e1=Fid(e1);
		e2=Fid(e2);
	}
	if(Fempty(e1) || Fempty(e2)) return false;
	return (e1.parentNode==e2.parentNode);
}

//设置元素css float的属性
function FsetStyleFloat(e,v,isID)
{
	if(isID==true) e=Fid(e);
	var s=e.style;
	if(s.styleFloat!=undefined)
		s.styleFloat=v;
	else
		s.cssFloat=v;
}
//获得元素的属性值
function FgetAttr(e,isID,name)
{
	if(isID==true) e=Fid(e);
	return e.getAttribute(name);
}

//Ajax代理,主要解决跨域问题
//封装请求
function FrequestAgent(callback,method,url,param,proxy)
{
	this.callback=callback;
	this.method=method.toUpperCase();
	this.url=url;
	//是否需要判断一下是否<=IE6的版本
	this.param=param;
	this.proxy=proxy;
	this.errCount=0;
}

//Request合法性检验
FrequestAgent.prototype.checkValid=function()
{
	return ((typeof(this.callback)=="function" && typeof(this.url)=="string" && (this.method=="POST" || this.method=="GET"))?true:false);
}

FrequestAgent.prototype.sendRequest=function()
{
	if(!this.checkValid())
	{
		window.status="ERROR:Request parameter error!";
		return;
	}
	var ajax=FId('$_ajax_agent_ifrm_$');
	if(Fempty(ajax))
	{
		var ifrm=document.createElement("iframe");
		ifrm.id='$_ajax_agent_ifrm_$';
		ifrm.src=this.proxy;
		ifrm.scrolling="No";
		with(ifrm.style){
			border="0",frameborder="0",width=0,height=0;
		}
		document.body.appendChild(ifrm);
	}
	var tmpReq=this;
	var fn=function()
	{
		var ajax=FId('$_ajax_agent_ifrm_$').contentWindow._AjaxProxyAgent;
		if(Fempty(ajax) && tmpReq.errCount<10)
		{
			setRunTimeout(fn,100);
			return;
		}
		ajax.requestQueue.put(tmpReq);
		ajax.handlerRequest();
	}
	setRunTimeout(fn,10);
}

//动态加载js,异步执行的，也就是说，在加载这些脚本的同时，主页面的脚本继续运行,为了节约内存可以考虑在使用后删除js对象
function FloadJS(url,sucfn,failfn,isdel)
{
	var js=document.createElement("script");
	js.type="text/javascript";
	js.src=url;
	var h=FtagName('HEAD').item(0);
	h.appendChild(js);
	if(FBrowser.isIE)
	{
		js.onreadystatechange=function()
		{
			//什么情况下是complete loaded,this.$funExeced:说明函数已经被执行了，由于ie下可能出现两次调用的问题
			if(this.readyState.toLowerCase()!="complete" && this.readyState.toLowerCase()!="loaded")
				return;
			if(this.$funExeced!=true && !Fempty(sucfn) && 'function'==typeof(sucfn)){
				this.$funExeced=true;
				sucfn();
			}
		}
	}else if(FBrowser.isOpera)
	{
		if(!Fempty(sucfn) && 'function'==typeof(sucfn))
		sucfn();
	}else{
		js.onload=function()
		{
			if(!Fempty(sucfn) && 'function'==typeof(sucfn))
			sucfn();
		}
	}
	js.onerror=function(){
		if(!Fempty(failfn) && 'function'==typeof(failfn)) 
			failfn();
	}
}






function FstartDrag(evt,wid)
{
	function FDrag(et)
	{
		et=FgetEvent(et);
		if(!FisLeftKeyDown(et)) return;

		var pw=Fid(wid);
		if(Fempty(pw.$clientX)) pw.$clientX=et.clientX;
		if(Fempty(pw.$clientY)) pw.$clientY=et.clientY;

		//需要考虑滚动条的位置，非常有意思
		var sp=FgetScrollPostion();
		var x=pw.offsetLeft+et.clientX-pw.$clientX+sp.left-pw.$scrollLeft;
		var y=pw.offsetTop+et.clientY-pw.$clientY+sp.top-pw.$scrollTop;
		if(x<=0) x=0;
		if(y<=0) y=0;
		var ps=pw.style;
		ps.left=x+"px";
		ps.top=y+"px";
		pw.$scrollTop=sp.top;
		pw.$scrollLeft=sp.left;
		pw.$clientX=et.clientX;
		pw.$clientY=et.clientY;
		//想想为什么？呵呵
		FstopEventTransfer(et);
		return false;
	}

	function FstopDrag(et)
	{
		et=FgetEvent(et);
		var ph=Fid(wid+"_head");
		var pw=Fid(wid);
		pw.style.zIndex=pw.$zIndex;

		FremoveEvent(document,'mousemove',FDrag);
		FremoveEvent(document,'mouseup',FstopDrag);
		ph.onmousemove=null;
		ph.onmouseup=null;
		FreleaseEventCapture(ph);
		//想想为什么？呵呵
		FstopEventTransfer(et);
		return false;
	}

	evt=FgetEvent(evt);
	if(!FisLeftKeyDown(evt)) return;
	var w=Fid(wid);
	var h=Fid(wid+"_head");
	var s=w.style;
	var p=FgetScrollPostion();
	w.$scrollTop=p.top;
	w.$scrollLeft=p.left;
	w.$clientX=evt.clientX;
	w.$clientY=evt.clientY;
	w.$zIndex=s.zIndex;
	s.zIndex=999;

	FsetEventCapture(h);
	FaddEvent(document,'mousemove',FDrag);
	FaddEvent(document,'mouseup',FstopDrag);
	h.onmouseup=FstopDrag;
	h.onmousemove=FDrag;
	//想想为什么？呵呵
	FstopEventTransfer(evt);
	return false;
}

function FenableDrag(wid)
{
	var w=Fid(wid),h=Fid(wid+"_head");
	if(w.style.position!="absolute") return;
	h.onmousedown=function(evt){FstartDrag(evt,wid);};
}

//打开模态窗口,分为两种：一种是整个屏幕，一种是对指定元素
function FsetModal(e,isID,wid)
{
	if(!Fempty(e) && isID==true) e=Fid(e);
	FunsetModal(wid);
	var p=0;
	if(Fempty(e)){
		p=FgetPageSize();
		p.x=0,p.y=0;
	}else{
		p=FgetPostion(e);
	}
	if(Fempty(wid)) wid="$_modal_$";
	var w=Fid(wid);
	if(Fempty(w))
	{
		w=document.createElement('DIV');
		w.id=wid;
		var s=w.style;
		with(s){
			position="absolute",filter="alpha(opacity=20);-moz-Opacity:0;Opacity:0;",zIndex=99,background='#666666';
		}
		if(FBrowser.isIE)s.background="#666666";
		if(FBrowser.isFirefox) {
			s.background = "#000000";
			s.opacity = 0.2;
		}
		document.body.insertBefore(w, document.body.firstChild);

	}
	FsetPostion(w,p.x,p.y,p.width,p.height);
	if(FBrowser.isIE6)
		w.innerHTML='<iframe scrolling="No" style="z-index:99" border="0" frameborder="0" width="'+p.width+'" height="'+p.height+'"></iframe>';
	else if(FBrowser.isOpera)
		w.innerHTML='<img src="/image/b.gif" onMouseDown="return false;" galleryimg="no" style="z-index:99" width="'+p.width+'" height="'+p.height+'"/>';
}

//关闭模态窗口,分为两种：一种是整个屏幕，一种是对指定元素
function FunsetModal(wid)
{
	var e=(Fempty(wid)?Fid('$_modal_$'):Fid(wid));
	if(!Fempty(e)) 
		document.body.removeChild(e);
}






document.domain='qq.com';
function ptlogin2_onResize(width,height){
	var login_div = Fid('login_div');
	if(login_div){
		login_div.style.width = width+'px';
		login_div.style.height = height+'px';
		login_div.style.display = 'none';
		login_div.style.display = 'block';
	}
}

function ptlogin2_onClose(){
	var login_div = Fid('login_div');
	if(login_div){
		login_div.style.display = 'none';
	}
	FunsetModal();
}


function qq_login_form(laid)
{
	if(!(/\d+/.test(laid)))
		return;
	var urlPt = /^(https?:\/\/)?([a-z]([a-z0-9\-]*\.)+qq\.com)(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&amp;]*)?)?(#[a-z][a-z0-9_]*)?$/i;
	var args = arguments;
	var sucUrl = (typeof args[1]) == 'string' ? (urlPt.test(args[1]) ? args[1] : "http://qzone.qq.com") : window.location.href;
	var failUrl = (typeof args[2]) == 'string' && urlPt.test(args[2]) ? args[2] : "loginerroralert";
	var targetWin = (typeof args[3]) == 'string' && /^[^_][a-z0-9_\-]*$/i.test(args[3]) ? 'self' : "top";
	
	var d = Fid('login_div');
	if(!d){
		d = document.createElement('div');
		d.id = 'login_div';
		d.style.position = 'absolute';
		d.style.zIndex = '200';
		d.innerHTML = '<iframe name="login_iframe" id="login_iframe" frameborder="0" scrolling="no" width="100%" height="100%" src="about:blank"></iframe>';
		document.body.insertBefore(d, document.body.firstChild);
	}
	d.style.width = '400px';
	d.style.height = '300px';
	d.style.display = 'block';
	FsetOffsetWindowPostionByRate(d,2,3);
	FsetModal();
	Fid('login_iframe').src = 'http://ui.ptlogin2.qq.com/cgi-bin/login?link_target=blank&appid='+laid+'&s_url='+escape(sucUrl)+'&f_url='+escape(failUrl)+'&target='+escape(targetWin) + '&qlogin_jumpname=jump&qlogin_param=u1%3D' + escape(sucUrl);
}