/*if (document.domain.indexOf("qq.com") > 0)
{
	document.domain = "qq.com";
}
*/

/*
获得URL参数
*/
function GetUrlParms()
{
    var args=new Object();
    var query=location.search.substring(1);//获取查询串
    var pairs=query.split("&");//在逗号处断开
    for(var i=0; i < pairs.length; i++ )   
    {   
        var pos=pairs[i].indexOf('=');//查找name=value   
            if(pos==-1)   continue;//如果没有找到就跳过   
            var argname=pairs[i].substring(0,pos);//提取name   
            var value=pairs[i].substring(pos+1);//提取value   
            args[argname]=unescape(value);//存为属性   
    }
    return args;
}

/*
获得URL参数，将指定的的URL参数设置到Cookie里面去。
*/
function SetUrlParaToCookie(strKey)
{
    var objArgs = GetUrlParms();
    if (typeof objArgs[strKey] != "undefined")
    {
        chSetCookie(strKey, objArgs[strKey]);   
    }
}


// cookie操作
function chGetCookie(name)
{
	var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr != null)
	{
		return unescape(arr[2]);
	}
	return "";
}
function chSetCookie(name, value)
{
	document.cookie = name + "=" + value + "; path=/; domain=qq.com";
}
function chDeleteCookie(name)
{
	chSetCookie(name, "");
}

// 得到字符串的长度
function chGetLength(value)
{
	var len = 0;
	for(var i = 0; i < value.length; i++)
	{
		if (value.charCodeAt(i) > 255) // 双字节
		{
			len += 2;
		}
		else
		{
			++len;
		}
	}
	return len;
}

// 转换为int
function chStrToInt(str, failint)
{
	var i = parseInt(str);
	return (isNaN(i) ? (failint ? failint : 0) : i);
}

// 从cookie获取QQ号
function chGetUin()
{
	var aRet = chGetCookie('uin').match(/\d+/);
	return aRet ? parseInt(aRet[0], 10) : -1;
}
// 根据cookie简单判断是否登录
function chIsLogin()
{
	var uin = chGetUin();
	var skey = chGetCookie('skey');
	if (uin <= 10000 || skey.length != 10)
	{
		return false;
	}
	return true;
}
// 退出登录
function chLogout(jumpurl, topjump)
{
	chDeleteCookie("uin");
	chDeleteCookie("skey");	
	chDeleteCookie("zzpaneluin");
	chDeleteCookie("zzpanelkey");
    chDeleteCookie("dna_result_key");
    chDeleteCookie("PcacheTime");	
	if (jumpurl)
	{
		if (topjump)
		{
			top.location = jumpurl;
		}
		else
		{
			location = jumpurl;
		}
	}
}

function chId(id)
{
	return document.getElementById(id);
}
function chName(name)
{
	return document.getElementsByTagName(name);
}

function chBrowser()
{
	var ua, s, i;
	this.isIE = false;
	this.isNS = false;
	this.isOP = false;
	this.isSF = false;
	ua = navigator.userAgent.toLowerCase();
	s = "opera";
	if ((i = ua.indexOf(s)) >= 0)
	{
		this.isOP = true;
		return;
	}
	s = "msie";
	if ((i = ua.indexOf(s)) >= 0) 
	{
		this.isIE = true;
		return;
	}
	s = "netscape6/";
	if ((i = ua.indexOf(s)) >= 0)
   	{
   		this.isNS = true;
   		return;
   	}
   	s = "gecko";
 	if ((i = ua.indexOf(s)) >= 0) 
   	{
   		this.isNS = true;
   		return;
   	}
   	s = "safari";
 	if ((i = ua.indexOf(s)) >= 0) 
   	{
   		this.isSF = true;
   		return;
   	}
}

function chScreenConvert()
{
	var browser = new chBrowser();
	var objScreen = chId("ScreenOver");
	if (!objScreen)
	{
		var objScreen = document.createElement("div");
	}
	objScreen.id = "ScreenOver";

	var oS = objScreen.style;
	oS.display = "block";
	oS.top = oS.left = oS.margin = oS.padding = "0px";

	if (document.body.clientHeight)
	{
		var wh = document.body.clientHeight + "px";
	}
	else if (window.innerHeight)
	{
		var wh = window.innerHeight + "px";
	}
	else
	{
		var wh = "100%";
	}
	oS.width = "100%";
	oS.height = wh;
	oS.position = "absolute";
	oS.zIndex = "3";
	
	if ((!browser.isSF) && (!browser.isOP))
	{
		oS.background = "#cccccc";
	}
	else
	{
		oS.background = "#cccccc";
	}
	oS.filter = "alpha(opacity=40)";
	oS.opacity = 40/100;
	oS.MozOpacity = 40/100;
	document.body.appendChild(objScreen);
	
	var allselect = chName("select");
	for (var i=0; i<allselect.length; ++i)
	{
		allselect[i].style.visibility = "hidden";
	}
}

function chDialogLoc(alignH, offsetH, alignV, offsetV, widthW, heightW)
{
	var dde = document.documentElement;
	if (window.innerWidth)
	{
		var ww = window.innerWidth;
		var wh = window.innerHeight;
		var ox = window.pageXOffset;
		var oy = window.pageYOffset;
	}
	else
	{
		var ww = dde.offsetWidth;
		var wh = dde.offsetHeight;
		var ox = dde.scrollLeft;
		var oy = dde.scrollTop;
	}
	this.posTop = 0;
	this.posLeft = 0;
	if (alignH == "center")
	{
		this.posLeft = ox + (ww - widthW) / 2 + offsetH;
	}
	else if (alignH == "right")
	{
		this.posLeft = ox + (ww - widthW) + offsetH - 23;
	}
	else
	{
		this.posLeft = ox + offsetH;
	}
	
	if (alignV == "center")
	{
		this.posTop = oy + (wh - heightW) / 2 + offsetV;
	}
	else if (alignV == "bottom")
	{
		this.posTop = oy + (wh - heightW) + offsetV - 33;
	}
	else
	{
		this.posTop = oy + offsetV;
	}
}

function chDialogShow(showdata,ot,ol,w,h)
{
	var objDialog = chId("DialogMove");
	if (!objDialog)
	{
		objDialog = document.createElement("div");
	}
	
	objDialog.id = "DialogMove";
	var oS = objDialog.style;

	oS.display = "block";
	oS.top = ot + "px";
	oS.left = ol + "px";
	oS.margin = "0px";
	oS.padding = "0px";
	oS.width = w + "px";
	oS.height = h + "px";
	oS.position = "absolute";
	oS.zIndex = "9999";
	oS.background = "#FFFFFF";
	objDialog.innerHTML = showdata;
	document.body.appendChild(objDialog);
}

function chDialogHide()
{
	chScreenClean();
	var objDialog = chId("DialogMove");
	if (objDialog)
	{
		objDialog.style.display = "none";
	}
}
function chScreenConvert()
{
	var browser = new chBrowser();
	var objScreen = chId("ScreenOver");
	if (!objScreen)
	{
		var objScreen = document.createElement("div");
	}
	var oS = objScreen.style;
	objScreen.id = "ScreenOver";
	oS.display = "block";
	oS.top = oS.left = oS.margin = oS.padding = "0px";
	if (document.body.clientHeight)	
	{
		var wh = document.body.clientHeight + "px";
	}
	else if (window.innerHeight)
	{
		var wh = window.innerHeight + "px";
	}
	else
	{
		var wh = "100%";
	}
	oS.width = "100%";
	oS.height = wh;
	oS.position = "absolute";
	oS.zIndex = "3";
	if ((!browser.isSF) && (!browser.isOP))
	{
		oS.background = "#cccccc";
	}
	else
	{
		oS.background = "#cccccc";
	}
	oS.filter = "alpha(opacity=40)";
	oS.opacity = 40/100;
	oS.MozOpacity = 40/100;
	document.body.appendChild(objScreen);
	var allselect = chName("select");
	for (var i=0; i<allselect.length; i++)
	{
  		allselect[i].style.visibility = "hidden";
  	}
}

function chScreenClean()
{
	var objScreen = chId("ScreenOver");
	if (objScreen)
	{
		objScreen.style.display = "none";
	}
	var allselect = chName("select");
	for (var i=0; i<allselect.length; ++i)
	{
		allselect[i].style.visibility = "visible";
	}
}
function doGet()
{
	new Ajax.Request(
			"http://game-web.qq.com/cgi-bin/check_login_json", 
			{
				method: "post", 
				parameters: "", 
				onSuccess: onGetSuccess
			});
}

function onGetSuccess(transport)
{
	var ans = transport.responseText.evalJSON();
	if (ans.result != 0)
	{
		alert(ans.resultstr);
		return;
	}
	if(ans.islogin == 1)
	{
		$("Logined").style.display = "block";
		$("NeedLogin").style.display = "none";
	}
}
// alginH:  横向偏移计算方式
// offsetH: 横向偏移量
// alignV:  纵向偏移计算方式
// offsetV: 纵向偏移量
function chNeedLogin(alignH, offsetH, alignV, offsetV)
{
	chScreenConvert();
	var AJAX_LoginBody = "<div><table width=\"351\" height=\"273\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"351\" height=\"37\"><img src=\"http:\/\/qqgamecdnimg.qq.com\/login\/images\/lg_03.gif\" width=\"312\" height=\"37\" alt=\"\"><img src=\"http:\/\/qqgamecdnimg.qq.com\/login\/images\/lg_04.gif\" alt=\"\" name=\"AjaxMainCancle\" width=\"39\" height=\"37\" id=\"AjaxMainCancle\" onClick=\"return chDialogHide();\" style=\"cursor:hand\"><\/td><\/tr><tr><td height=\"236\"><iframe id=\"login_frame\" src=\"\/login\/lg.html\" frameborder=\"0\" scrolling=\"no\" width=\"351\" height=\"236\"><\/iframe><\/td><\/tr><\/table><\/div><div id=\"DialogTitleText\"><\/div>";
	var strAlignH = (!alignH ? "center" : alignH);
	var strAlignV = (!alignV ? "center" : alignV);
	var iOffsetH = (!offsetH ? 0 : offsetH);
	var iOffsetV = (!offsetV ? 0 : offsetV);
	var loc = new chDialogLoc(strAlignH, iOffsetH, strAlignV, iOffsetV, 214, 224);
	chDialogShow(AJAX_LoginBody, loc.posTop, loc.posLeft, 214, 224);
}


// 获取浏览器的窗口尺寸信息
function chWindowSizeInfo(aobjDoc)
{
//    var objDoc = aobjDoc || top.document;
    var objDoc = top.document;
    var obj = (objDoc.compatMode == "CSS1Compat" ? objDoc.documentElement : objDoc.body);
    this.clientWidth = obj.clientWidth;
    this.clientHeight = obj.clientHeight;
    this.scrollLeft = obj.scrollLeft;
    this.scrollTop = obj.scrollTop;
    this.scrollWidth = obj.scrollWidth;
    this.scrollHeight = obj.scrollHeight;
}


// 显示覆盖层
function chShowCoverDiv(iOffsetLeft, iHeight)
{
	var objCover = chId("chCoverDiv");
    if (!objCover)
	{

		objCover = top.document.createElement("div");
    	objCover.id = "chCoverDiv";
    try
    {
		top.document.body.appendChild(objCover);
    }
    catch (e)
    {
    }

	}

	var objStyle = objCover.style;
	objStyle.display = "block";
	objStyle.margin = "0px";
	objStyle.padding = "0px";

	objStyle.top = "100px";
	objStyle.left = "100px";
	objStyle.width = 100 + "px";
	objStyle.height = 100 + "px";
/*
	objStyle.top = "0px";
	objStyle.left = "0px";
	objStyle.width = iOffsetLeft + "px";
	objStyle.height = iHeight + "px";
*/
	objStyle.position = "absolute";
	objStyle.zIndex = "3";
	objStyle.background = "#000000";
	objStyle.filter = "alpha(opacity=40)";
	objStyle.opacity = 40/100;
	objStyle.MozOpacity = 40/100;

	var allselect = chName("select");
	for (var i=0; i<allselect.length; ++i)
	{
		allselect[i].style.visibility = "hidden";
	}

}

// 创建对话框层
// 因为可能需要调整兑换框的大小，所以先设为不可见
function chCreateDialogDiv(strID, strHtmlData, iOffsetTop, iOffsetLeft, iWidth, iHeight)
{
	var objDialog = chId(strID);
	if (!objDialog)
	{
		objDialog = top.document.createElement("div");
    	objDialog.id = strID;
        try{
    		top.document.body.appendChild(objDialog);
        }
        catch (e)
        {
        }	
	}

	var objStyle = objDialog.style;
	objStyle.display = "block";
	objStyle.margin = "0px";
	objStyle.padding = "0px";
	
	objStyle.top = 100 + "px";
	objStyle.left = 100 + "px";
	objStyle.width = 100 + "px";
	objStyle.height = 100 + "px";


/*
	objStyle.top = iOffsetTop + "px";
	objStyle.left = iOffsetLeft + "px";
	objStyle.width = iWidth + "px";
	objStyle.height = iHeight + "px";
*/	
	objStyle.position = "absolute";
	objStyle.zIndex = "5";
	objStyle.background = "#FFFFFF";
	objStyle.border = "solid #00D2FF 1px";
	objStyle.visibility = "hidden";	// 因为可能需要调整兑换框的大小，所以先设为不可见
	objDialog.innerHTML = strHtmlData;
}



// 显示对话框
function chShowDialog(strID, strHtmlData, iWidth, iHeight, strAlignH, iOffsetH, strAlignV, iOffsetV)
{
	strAlignH = strAlignH || "center";
	strAlignV = strAlignV || "center";
	iWidth = iWidth || 0;
	iHeight = iHeight || 0;
	iOffsetH = iOffsetH || 0;
	iOffsetV = iOffsetV || 0;
	
	
	// 计算偏移量
	var objSizeInfo = new chWindowSizeInfo();
	var iOffsetTop = 0;
	var iOffsetLeft = 0;
	if (strAlignH == "center")
	{
		iOffsetLeft = objSizeInfo.scrollLeft + (objSizeInfo.clientWidth - iWidth) / 2 + iOffsetH;
	}
	else if (alignH == "right")
	{
		iOffsetLeft = objSizeInfo.scrollLeft + (objSizeInfo.clientWidth - iWidth) + iOffsetH - 23;
	}
	else
	{
		iOffsetLeft = objSizeInfo.scrollLeft + iOffsetH;
	}
	
	if (strAlignV == "center")
	{
		iOffsetTop = objSizeInfo.scrollTop + (objSizeInfo.clientHeight - iHeight) / 2 + iOffsetV;
	}
	else if (strAlignV == "bottom")
	{
		iOffsetTop = objSizeInfo.scrollTop + (objSizeInfo.clientHeight - iHeight) + iOffsetV - 33;
	}
	else
	{
		iOffsetTop = objSizeInfo.scrollTop + iOffsetV;
	}
	// 显示覆盖层
	chShowCoverDiv(objSizeInfo.scrollWidth, objSizeInfo.scrollHeight);
	// 显示对话框层
	chCreateDialogDiv(strID, strHtmlData, iOffsetTop, iOffsetLeft, iWidth, iHeight);
}


// 显示对话框层
function chShowDialogDiv(strID)
{
	var objDialog = chId(strID);
	if (objDialog)
	{
		objDialog.style.display = "block";
		objDialog.style.visibility = "visible";
	}
}

function chNeedVerify(strUrl, strAlignH, iOffsetH, strAlignV, iOffsetV)
{
    var str = '<iframe id="verify_iframe" allowTransparency="true" marginwidth="0" marginheight="0" hspace="0"' + 
              ' vspace="0" frameborder="0" scrolling="no" style="border:none;width:373px;height:290px" ' + 
              'src="' + strUrl+'"></iframe>';
    chShowDialog("dialog_verify", str, 373, 284, strAlignH, iOffsetH, strAlignV, iOffsetV);
    chShowDialogDiv("dialog_verify");
}


//设置个账秘宝的cookie
SetUrlParaToCookie("dna_result_key");
SetUrlParaToCookie("PcacheTime");

