var initText = "请在此留言,我们会及时联系您!!";
var initMobile="请输入您的手机号码!!";
var initEmpty = "";
var beginDateTime;

window.onload = getMsg; 
//返回在页面停留时间，并提交统计数据
document.body.onunload = submitCountData;

var divTop,divLeft,divWidth,divHeight,docHeight,docWidth,objTimer,i = 0; 
function getMsg() 
{ 
	try
	{
	    var param = GetScriptArgs().split('&')[1].split('=')[1];//0,1,2,3
	    //在页面停留开始时间
	    beginDateTime = new Date();
	} 
	catch(e)
	{ } 
} 

function $(obj)
{
    return document.getElementById(obj);
}

/*
获取系统信息(操作系统、浏览器、时区等)
*/
function submitCountData()
{
    ClientAgent = new Agent();
    var isReturn = 1;
    
    //读取Cookies
    if(readCookies() != null)
    {
        //曾经有访问过本站
        var visitDate = new Date(readCookies());
        if(((((new Date() - visitDate)/1000)/60)/60) > 12)
        {
            //距离上次访问已经超过十二小时（即记为一次回访）
            isReturn = 2;
        }
        else
        {
            isReturn = 3;
        }
    }
    var stayTime = new Date() - beginDateTime;//(毫秒)
    var param = "&StayTime="+stayTime+"&IsReturn="+escape(isReturn)+"&System="+escape(ClientAgent.System) +"&Browser="+escape(ClientAgent.Brower)+"&Screen="+escape(screen.width/16 +"*"+ screen.height/8) +"&Color="+escape(screen.colorDepth)+"&Gmt="+escape(Getgmt())+"&Referer="+escape(document.referrer)+"&SessionId="+GetScriptArgs().split('&')[0].split('=')[1]+"&Site="+GetScriptArgs().split('&')[2].split('=')[1];
    loadScript('http://www.pangod.com/Manage/RequestDeal/RequestDeal.ashx?Active=AddCounter'+param+'',false);
    writeCookies();
}

/*返回GMT时间*/
var mydt;
function Getgmt()
{
    var dt = new Date();
    var tzone=dt.getTimezoneOffset();
    var gmtfh=(tzone>=0)?"0":"1";
    tzone=(tzone>=0)?tzone:-tzone;
    var gmthour=tzone/60;
    var gmtmin=tzone%60;
    gmthour=(gmthour>=10)?gmthour:"0"+gmthour;
    gmtmin=(gmtmin>=10)?gmtmin:"0"+gmtmin;
    gmt=gmtfh+""+gmthour+""+gmtmin;
    return gmt;
}

/*客户端系统与使用浏览器*/
function Agent()
{
    var System;
    var agent_arr;
    var bctype;
    var agent=navigator.userAgent;
    var Mozilla=agent.substr(0,agent.indexOf("/"));
    agent_arr=agent.split(";");
    switch (Mozilla)
    {
        case "Mozilla":
            System=agent_arr[2].replace(")","");
            bctype=agent_arr[1];
            if (bctype.indexOf("MSIE")>0)
            {
                Brower=bctype;
                //s!!!
                Brower=Brower.replace("MSIE","")
            }
            else if (bctype.indexOf("U")>0)
            {
                var ucstrl=agent.substr(0,agent.lastIndexOf("/"));
                var ucstrr=agent.substring(agent.lastIndexOf("/")+1,agent.length);
                ucstrl=ucstrl.substring(ucstrl.lastIndexOf(" ")+1,ucstrl.length);
                ucstrr=ucstrr.substr(0,ucstrr.indexOf(" ")>0?ucstrr.indexOf(" "):ucstrr.length);
                Brower=ucstrl+" "+ucstrr;
                Brower="!"+Brower;
            }
            break;
        case "Opera":
            Brower=agent_arr[0].substr(0,agent_arr[0].indexOf(" ") ) ;
            Brower=Brower.replace("/"," ");
            Brower="!"+Brower;
            System=agent_arr[0].substr(agent_arr[0].indexOf("(")+1, agent_arr[0].length);
            break;
      default:
            break;
            }
            if (System.indexOf("Win")>0)
            {
                System=System.replace("Windows","");
                System=System.replace("NT","");
                System=System.replace("Win","");
            }
            else 
            {
                System="!"+System
            }
    this.Brower=Brower;
    this.System=System;
}

//向客户端写入Cookies
function writeCookies()
{
    var curDate = new Date();
    //将当前日期存入Cookies
    //Cookies有效期保存一年
    document.cookie="www.pangod.com="+(curDate.getFullYear()+"/"+(curDate.getMonth()+1)+"/"+curDate.getDate()+" "+curDate.getHours()+":"+curDate.getMinutes()+":"+curDate.getSeconds())+";expires="+curDate;
}

//读取Cookies
function readCookies()
{
    var redCook = document.cookie.split(';');
    var result = null;
    
    for(var count =0;count<redCook.length;count++)
    {
        var Cookies = redCook[count].split('=');
        if(Cookies[0] == "www.pangod.com")
        {
            result = Cookies[1];
        }
    }
    return result;
}

function clearContent(Ctrl)
{
	var txtValue = $(Ctrl);
	if(txtValue.value == initText || txtValue.value==initMobile)
	{
		txtValue.value = initEmpty;
	}
}

function setInitValue(Ctrl,num)
{
	var text = document.getElementById(Ctrl);
	if(text.value.length == 0)
	{
		switch(num)
		{
			case 1:
			{
				text.value = initText;
				break;
			}
			case 2:
			{
				text.value = initMobile;
				break;
			}
		}
	}
}

//获取包含Script标签，并检索其后的参数
function GetScriptArgs()
{
    var script = document.getElementsByTagName("Script");
    var paramsArr = script[script.length -1].src.split('?')[1];
    //扁历参数
    return paramsArr;
}

function validInput()
{
	var content = $("pangod_message_content");
	var mobile = $("pangod_message_mobile");
	
	if(content.value == initText || mobile.value == initMobile)
	{
		alert("请输入留言内容.\n请输入手机号码.");
		return false;
	}
	var param = "&Text="+escape($("pangod_message_content").value) +"&Tel="+$("pangod_message_mobile").value+"&SessionId="+GetScriptArgs().split('&')[0].split('=')[1]+"";
	loadScript('http://www.pangod.com/Manage/RequestDeal/RequestDeal.ashx?Active=SubmitBBS'+param+'',true);
}

/*
实现跨域数据提交
*/
function loadScript(url,active)
{
    try
    {
	var lastScript;
	var h=document.getElementsByTagName("head")[0];
        var f=document.createElement("script");
        var d=new Date().getTime();
        f.type="text/javascript";
        f.id=d;
        f.src=url+'&d='+d;
        h.appendChild(f);
        lastScript=d;
        if(lastScript && $(lastScript))
        {
            $(lastScript).parentNode.removeChild($(lastScript));
        }
        if(active)
        {
            alert("提交数据成功!!");
        }
    }
    catch(e)
    {alert("提交数据失败!!");}
}

function tab(mod,cursor,n)
{
	for(i=1;i<=n;i++)
	{
		var nav=$(mod+i);
		var cont=$(mod+"_"+"cont"+i);
		cont.style.display=i==cursor?"block":"none";
		if(cursor==2) 
		{
			$("popup2").style.display= "none";
			$("popup1").style.display= "block";			
		}
		else 
		{
			$("popup1").style.display= "none";
			$("popup2").style.display= "block";			
		}
	}
}
function closediv (){
	$("blogPop").style.display="none";
}

with (document) {
    write("<link rel=\"stylesheet\" href=\"http://www.pangod.com/skins/css/popup.css\" type=\"text/css\" />");
    write("<div id=\"blogPop\" class=\"blogPop\">");
    write("		<div class=\"popTitle\">");
    write("		<img onclick=\"closediv()\" src=\"http://www.pangod.com/skins/image/close.gif\" title=\"关闭\" />");
    write("		<span id=\"popup1\" onclick=\"tab('popup',1,2)\" style=\"display:none\"><img src=\"http://www.pangod.com/skins/image/resize.gif\" title=\"还原\" /></span>");
    write("		<span id=\"popup2\" onclick=\"tab('popup',2,2)\"><img src=\"http://www.pangod.com/skins/image/min.gif\" title=\"最小化\" /></span>");
    write("</div>");
    write("<div id=\"popup_cont1\" class=\"popContent\">");
    write("		<textarea id=\"pangod_message_content\" onclick=\"clearContent(this.id)\" onblur=\"setInitValue(this.id,1)\">" + initText + "</textarea><br />");
    write("		电话：");
    write("		<span><input type=\"text\" onblur=\"setInitValue(this.id,2)\" id=\"pangod_message_mobile\" onclick=\"clearContent(this.id)\" value='" + initMobile + "' /></span>");
    write("		<span><input type=\"button\" value=\"发送\" style=\ width:50px;\" onclick=\"validInput()\" /></span>");
    write("		<span>&nbsp;&nbsp;<a href=\"http://www.pangod.com\" target=\"_blank\">成都盘古网络提供技术支持</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>");
    write("</div>");
    write("<div id=\"popup_cont2\" style=\"display:none;\">");
    write("</div>");
}