﻿function getElement(aID)
{
  return (document.getElementById) ? document.getElementById(aID) : document.all[aID];
}
function getAllElement()
{
  return (document.getElementsByTagName) ? document.getElementsByTagName("*") : document.all;
}
function getParentElement(elm)
{
	return (elm.parentElement) ? elm.parentElement : elm.parentNode
}

function attach(o,e,f){
  if (document.attachEvent)
    o.attachEvent("on"+e,f);
  else if (document.addEventListener)
    o.addEventListener(e,f);
}

function Trim(s) {
 var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
 return (m == null) ? "" : m[1];
}

var Drag={
        "obj":null,
	"init":function(a, aRoot,objDiv){
	        objDiv = objDiv == undefined ? "MsgDiv" : objDiv;
	        var docW=(document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.clientWidth;
	        var divW=document.getElementById(objDiv).offsetWidth;
	        var docH=(document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight;
	        var divH=document.getElementById(objDiv).offsetHeight;
	        if(parseInt(docW)<=0){docW=document.body.clientWidth}
	        if(parseInt(docH)<=0){docH=document.body.clientHeight}
	        var Lp=((parseInt(docW)-parseInt(divW))/2).toString()+"px";
	        var Tp=(parseInt(document.documentElement.scrollTop)+parseInt((parseInt(docH)-parseInt(divH))/2)).toString()+"px";
			a.onmousedown=Drag.start;
			a.root = aRoot;
			if(isNaN(parseInt(a.root.style.left)))a.root.style.left=Lp;
			if(isNaN(parseInt(a.root.style.top)))a.root.style.top=Tp;
			a.root.onDragStart=new Function();
			a.root.onDragEnd=new Function();
			a.root.onDrag=new Function();
		},
	"start":function(a){	
			var b=Drag.obj=this;
			a=Drag.fixE(a);
			var c=parseInt(b.root.style.top);
			var d=parseInt(b.root.style.left);
			b.root.onDragStart(d,c,a.clientX,a.clientY);
			b.lastMouseX=a.clientX;
			b.lastMouseY=a.clientY;
			document.onmousemove=Drag.drag;
			document.onmouseup=Drag.end;
			return false;
		},	
	"drag":function(a){
			a=Drag.fixE(a);
			var b=Drag.obj;
			var c=a.clientY;
			var d=a.clientX;
			var e=parseInt(b.root.style.top);
			var f=parseInt(b.root.style.left);
			var h,g;
			h=f+d-b.lastMouseX;
			g=e+c-b.lastMouseY;
			b.root.style.left=h+"px";
			b.root.style.top=g+"px";			
			b.lastMouseX=d;
			b.lastMouseY=c;
			b.root.onDrag(h,g,a.clientX,a.clientY);
			return false;
		},
	"end":function(){			
			document.onmousemove=null;
			document.onmouseup=null;
			Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style.left),parseInt(Drag.obj.root.style.top));
			Drag.obj=null;
		},
	"fixE":function(a){
			if(typeof a=="undefined")a=window.event;
			if(typeof a.layerX=="undefined")a.layerX=a.offsetX;
			if(typeof a.layerY=="undefined")a.layerY=a.offsetY;
			return a;
		}
};
   
function getRange() {
      var top     = document.documentElement.scrollTop;
      var left    = document.documentElement.scrollLeft;
      var height  = document.documentElement.clientHeight;
      var width   = document.documentElement.clientWidth;

      if (top==0 && left==0 && height==0 && width==0) {
        top     = document.body.scrollTop;
        left    = document.body.scrollLeft;
        height  = document.body.clientHeight;
        width   = document.body.clientWidth;
      }

      return  {top:top  ,left:left ,height:height ,width:width } ;
}

//Ajax處理結果事件方法顯示
function CreateMsg(obj)
    {        
        if(obj!=null&&obj.value!=null)
        {
            var W=getWidth(),H=getHeight();
            var newOver = document.createElement('div');
            newOver.id='OverDiv';
            newOver.className = 'OverDiv';
            newOver.style.width=W;
            newOver.style.height = H;
            newOver.innerHTML="<iframe style=\"width:"+W+"; height:"+H+";filter:alpha(opacity=0);\"></iframe>";
            document.body.appendChild(newOver);
            var newElement = document.createElement('div');
            newElement.id='MsgDiv';            
            newElement.className = 'MsgDiv';
            newElement.innerHTML=obj.value;
            document.body.appendChild(newElement);
            initbox('MsgDiv');
        }
    }
    
function CreateMsgContent(strTitle,strContent)
    {
        if(strContent!="")
        {
            var strHtml = "";
            strHtml+="<ul id=\"uldiv\">\n";
            strHtml += "<li id=\"draghead\">\n";
            strHtml += strTitle;
            strHtml += "\n</li>\n<li id=\"MsgContent\">\n";
            strHtml += strContent;
            strHtml += "\n</li>\n</ul>";
            var W=getWidth(),H=getHeight();
            var newOver = document.createElement('div');
            newOver.id='OverDiv';
            newOver.className = 'OverDiv';
            newOver.style.width=W;
            newOver.style.height = H;
            newOver.innerHTML="<iframe style=\"width:"+W+"; height:"+H+";filter:alpha(opacity=0);\"></iframe>";
            document.body.appendChild(newOver);
            var newElement = document.createElement('div');
            newElement.id='MsgDiv';            
            newElement.className = 'MsgDiv';
            newElement.innerHTML=strHtml;            
            document.body.appendChild(newElement);                 
            initbox('MsgDiv');
        }
    }
    

function CreateTiMsg(strTitle,strMsg,closFun)
{        
        var strHtml = "";
        strHtml+="<ul>\n";
        strHtml += "<li id=\"draghead\">\n";
        strHtml += strTitle;
        strHtml += "\n</li>\n<li id=\"MsgContent\">\n";
        strHtml += strMsg;
        strHtml += "\n</li>\n<li id=\"MsgButton\">\n";
        strHtml += "<input type=\"button\" onclick=\"";
        strHtml += closFun==undefined?"closediv('MsgDiv');":"closediv('MsgDiv');eval("+closFun+");";
        strHtml += "\" value=\"確 定\"/>";
        strHtml += "\n</li>\n</ul>";
        //alert(argObj);
        if(strHtml!=null && strHtml.length>0)
        {
            var W=getWidth(),H=getHeight();        
            var newOver = document.createElement('div');
            newOver.id='OverDiv';
            newOver.className = 'OverDiv';
            newOver.style.width=W;
            newOver.style.height = H;
            newOver.innerHTML="<iframe style=\"width:"+W+"; height:"+H+";filter:alpha(opacity=0);\"></iframe>";
            document.body.appendChild(newOver);
            var newElement = document.createElement('div');
            newElement.id='MsgDiv';            
            newElement.className = 'MsgDiv';
            newElement.innerHTML=strHtml.toString();            
            document.body.appendChild(newElement);
            document.body.scroll="no";         
            initbox('MsgDiv');
        }
}

function CreateTiMsgTo(strTitle,strMsg,strUrl)
{
        var strHtml = "";
        strHtml+="<ul>\n";
        strHtml += "<li id=\"draghead\">\n";
        strHtml += strTitle;
        strHtml += "\n</li>\n<li id=\"MsgContent\">\n";
        strHtml += strMsg;
        strHtml += "\n</li>\n<li id=\"MsgButton\">\n";
        strHtml += "<input type=\"button\" onclick=\"JavaScript:linkPage('"+strUrl+"');\" value=\" 確 定 \"/>";
        strHtml += "\n</li>\n</ul>";
       var W=getWidth(),H=getHeight();
       var newOver = document.createElement('div');
       newOver.id='OverDiv';
       newOver.className = 'OverDiv';
       newOver.style.width=W;
       newOver.style.height = H;
       newOver.innerHTML="<iframe style=\"width:"+W+"; height:"+H+";filter:alpha(opacity=0);\"></iframe>";
       document.body.appendChild(newOver);
       var newElement = document.createElement('div');
       newElement.id='MsgDiv';            
       newElement.className = 'MsgDiv';
       newElement.innerHTML=strHtml;            
       document.body.appendChild(newElement);
       initbox('MsgDiv');
}

function LoginEventHandler(nextLayerObject)
{
    var promptLayer = document.getElementById('notice_bar');
    
    var strUserID = document.getElementById('in_username').value;
    var strPass = document.getElementById('in_password').value;
    if(Trim(strUserID) == "")
    {
        promptLayer.innerText = "用戶名不能為空！";
        return false;
    }
    
    if(Trim(strPass) == "")
    {
        promptLayer.innerText = "密碼不能為空！";
        return false;
    }
    
    promptLayer.innerText = "正在驗證用戶,請等候......";
    
    //必須引用AjaxMethods類
    //promptLayer.innerText = AjaxMethods.OnLogin(strUserID,strPass).value;
    var timeobj = window.setTimeout("AjaxLoginHandle(this,'"+strUserID+"','"+strPass+"','" + promptLayer.id+"','"+nextLayerObject+"')",1000);
}

function AjaxLoginHandle(timeobj,strUserName,strPassword,promptObj,nextLayerObject)
{
    var strData = "uname="+strUserName+"&pwd="+strPassword;    
    $.ajax({
           type: "POST",
           url: "http://ads.chinayes.com/Ajax.aspx?Module=CheckLogin",
           data: strData,
           success: function(msg){
                var returnString = msg.split(',');
                document.getElementById(promptObj).innerText = returnString[0].toString();
                if(Number(returnString[1]) == 1)
                {
                    window.setTimeout("closediv('qLoginDiv')",500);
                    location.href = location.href.replace(/\?(\s|\S)+/,'');
                    return true;
                }
                else
                {
                    return false;
                }
                window.clearTimeout(timeobj);
           }
        });
}

function CreateLoginWindow(nextLayerObject)
{   
    var strHtml = "";
    //strHtml += "<DIV class=\"g_lay_com g_win_4 g_f_shw\" id=qLoginDiv style=\"Z-INDEX: 99999; LEFT: 459px; WIDTH: 500px; POSITION: absolute; TOP: 240px; HEIGHT: 265px\">";
    strHtml += "    <DIV class=titlebar id=qLoginDiv_system_bar>";
    strHtml += "        <DIV>";
    strHtml += "            <SPAN class=\"r i_ i124\" id=qLoginDiv_system_bar_close title=關閉 onclick=\"closediv('qLoginDiv');\">&nbsp;</SPAN>";
    strHtml += "            <SPAN class=\"handle_class g_c_move\" id=\"draghead\" style=\"DISPLAY: block\">登入兩岸網</SPAN>";
    strHtml += "        </DIV>";
    strHtml += "    </DIV>";
    strHtml += "    <DIV class=content id=qLoginDiv_panel>";
    strHtml += "        <DIV>";
    strHtml += "            <FORM id=frmLogin name=frmLogin method=post>";
    strHtml += "                <DIV class=g_tab_btn03>";
    strHtml += "                </DIV><!-- 通行證 -->";
    strHtml += "                <DIV class=case>";
    strHtml += "                    <DIV class=g_h_25>";
    strHtml += "                        <LABEL for=text0>帳　號：</LABEL><INPUT class=\"g_w_60 g_h_ipt\" id=in_username name=in_username></DIV>";
    strHtml += "                    <DIV class=g_h_25>";
    strHtml += "                        <LABEL for=text1>密　碼：</LABEL><INPUT class=\"g_w_60 g_h_ipt\" id=in_password type=password value=\"\" name=in_password>";
    strHtml += "                    </DIV>";
    strHtml += "                    <DIV class=\"mgntxt clr02\" id=notice_bar style=\"display-left: 0px\">請輸入通行證用戶名和密碼登錄</DIV>";
    strHtml += "                    <DIV class=mgntxt><!--<INPUT id=setCookieCheck type=checkbox value=\"\" name=setCookieCheck><LABEL for=ck0>自動登錄</LABEL>--></DIV>";
    strHtml += "                    <DIV class=\"g_h_30 mgn\">";
    strHtml += "                        <DIV class=\"btn g_f_hov\" id=qLoginButt onclick=\"javascript:LoginEventHandler('"+nextLayerObject+"')\"></DIV>";
    strHtml += "                            &nbsp;&nbsp;<a class=\"a_a d_d\" id=reg_add target=\"_self\" href=\"http://www.chinayes.com/Member/Register_state.html\"><font color=\"#3366CC\">註冊兩岸網會員 --&gt;</font></a>";
    strHtml += "                    </DIV>";
    strHtml += "                </DIV>";
    strHtml += "                <DIV class=\"g_h_30 g_c_vmgin\">";
    strHtml += "                    <SPAN class=\"a_a d_d\"> <a id=getPass_add target=\"_self\" href=\"http://www.chinayes.com/Member/ForgetPass.aspx\"><font color=\"#3366CC\">忘了密碼?</font></a></SPAN>";
    strHtml += "                    <SPAN class=g_c_mvlft>客服信箱：</SPAN>";
    strHtml += "                    <a class=\"a_a d_d\" target=\"_blank\" href=\"mailto:Service@chinayes.com\"><font color=\"#3366CC\">Service@chinayes.com</font></a>";
    strHtml += "                </DIV>";
    strHtml += "            </FORM>";
    strHtml += "        </DIV>";
    strHtml += "    </DIV>";
    //strHtml += "</DIV>";

    var W=getWidth(),H=getHeight();
    var newOver = document.createElement('div');
    newOver.id='OverDiv';
    newOver.className = 'OverDiv';
    newOver.style.width=W;
    newOver.style.height = H;
    newOver.innerHTML="<iframe style=\"width:"+W+"; height:"+H+";filter:alpha(opacity=0);\"></iframe>";
    document.body.appendChild(newOver);
    var newElement = document.createElement('div');
    newElement.id='qLoginDiv';            
    newElement.className = 'g_lay_com g_win_4 g_f_shw';
    //alert(newElement.innerHTML);
    newElement.innerHTML=strHtml;            
    document.body.appendChild(newElement);
    initbox('qLoginDiv');

}
    
function initbox(objDiv){
    objDiv = objDiv == undefined ? "MsgDiv" : objDiv;
//	var range=getRange();
	WfloatBox=getElement(objDiv);
//	WfloatBox.ox=(range.width-400)/2;
//	WfloatBox.oy=(range.height-300)/2;
	
	//載入拖動事件
	Drag.init(getElement("draghead"),WfloatBox,objDiv);

	WfloatBox.onDragEnd=function(x,y){
	WfloatBox.ox=x-getRange().left;
	WfloatBox.oy=y-getRange().top;	
	}
	document.getElementById(objDiv).getElementsByTagName("input")[0].focus();
	//document.write(document.getElementById(objDiv).outerHTML);
}

function closediv(objDiv)
{
        objDiv=objDiv==undefined?"MsgDiv":objDiv;    
        document.body.removeChild(document.getElementById(objDiv));
        document.body.removeChild(document.getElementById('OverDiv'));
}

function getOs()//流覽器類型
{
   if(navigator.userAgent.indexOf("MSIE")>0)return 1; //IE
   if(isFirefox=navigator.userAgent.indexOf("Firefox")>0)return 2;//Firefox
   if(isSafari=navigator.userAgent.indexOf("Safari")>0)return 3;   
   if(isCamino=navigator.userAgent.indexOf("Camino")>0)return 4;
   if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0)return 5;
   return 0;
}
function getWidth()
{
    var iW=getOs()!=2?parseInt(document.body.clientWidth):parseInt(document.body.clientWidth);
    var tW=(document.body.clientWidth)?iW:(self.innerWidth?self.innerWidth:0);
    return (tW<document.documentElement.clientWidth)?document.documentElement.clientWidth+"px":(tW==0?"100%":tW+"px");
}
function getHeight()
{
    var iH=getOs()!=2?parseInt(document.body.clientHeight):parseInt(document.body.clientHeight); 
    var tH=(document.body.clientHeight)?iH:(self.innerHeight?self.innerHeight:0);
    return (tH<document.documentElement.clientHeight)?document.documentElement.clientHeight+"px":(tH==0?"100%":tH+"px");
}
function linkPage(url) { if(window.location.href) { window.location.href  =url; } else { window.location  =url; } }