// 建立ajax物件
function ajaxCore()
{
	this.notSupported=false; // 預設支援ajax
	this.ajaxCount=0; // 第幾個ajaxCount
	this.outputDiv=""; // 輸出的Div

	this.sendURL=""; // 目標位置
	this.retryCount=0; // 重試次數

	// /// 建立request物件
	if(window.XMLHttpRequest)
	{
		this.req=new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		try
		{
			this.req=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				this.req=new ActiveXObject("Micorsoft.XMLHTTP");
			}
			catch(e)
			{
				this.notSupported=true;
			}
		}
	}

	// ///  判斷瀏覽器支援Ajax
	if(this.req==null||typeof this.req=="undefined")
	{
		alert("not support ajax");
		this.notSupported=true;
	}
	else // 確定支援
	{
	}

	// 將自己加入Window物件陣列中
	var ajaxCoreCount=window["ajaxCoreCount"];

	if(typeof ajaxCoreCount=="undefined")
	{
		this.ajaxCoreCount=0;
		window["ajaxCoreCount"]=this.ajaxCoreCount;
	}
	else
	{
		this.ajaxCoreCount++;
		window["ajaxCoreCount"]=this.ajaxCoreCount;
	}
	
	// /// 設定返回函式
	this.req.onreadystatechange=new Function( "window[\"ajaxCore"+this.ajaxCoreCount+"\"].callback();" ); // 建立新函式

	// /// 返回函式
	this.callback=function()
	{
		//alert("callback");

		if(this.req.readyState==4) // 判斷物件狀況
		{
			//alert(this.req.status);

			if(this.req.status==200) // 資訊已成功反回
			{
				div=document.getElementById(this.outputDiv);

				if(div)
				{
					// / 去除div下所有的標籤
					removeAllChild(div);

					//alert(this.req.responseText);
					div.innerHTML=this.req.responseText;
				}
				else
				{
					alert("div "+inDiv+" not found!");
				}

			}
			else
			{
				div=document.getElementById(this.outputDiv);

				this.retryCount+=1; // 重試次數加1

				if(div)
				{
					// / 去除div下所有的標籤
					removeAllChild(div);

					div.innerHTML=""+this.req.status+"<a href=\"javascript:void(0)\" onclick=\"ajaxClientWindow('"+this.sendURL+"','"+this.outputDiv+"','')\" > retry </a>";
				}
				else
				{
					alert("div "+inDiv+" not found!");
				}
				
			} // 200
		}
	}

	// /// 初始及加入物件
	this.init=function ()
	{
		window["ajaxCore"+this.ajaxCoreCount]=this; // 將物件加入到Window中,這樣callback才能運作
	}

	// 測試函式
	this.test=function ()
	{
		alert("17");
	}
}

// 刪除所有的子節點標籤
function removeAllChild(elem)
{
	while(elem.firstChild)
	{
		elem.removeChild(elem.firstChild);
	}
}

// ajax子視窗功能
function ajaxClientWindow(inURL,inDiv,inMessage)
{
	var div=document.getElementById(inDiv);
	
	if(div) // 標籤存在
	{
		removeAllChild(div);

		
		if(inMessage.length>0)
		{
			div.innerHTML=inMessage;
		}
		else
		{
			div.innerHTML="Loading...";
		}

		var ac=new ajaxCore();

		if(ac)
		{
			ac.outputDiv=""+inDiv;
			ac.init();
			ac.sendURL=inURL;

			// / 產生亂數,讓ajax不會被快取
			var rnd = Math.round(Math.random()*10000);

			try
			{
				ac.req.open("GET",inURL+"?"+rnd,true);
				ac.req.send(null);
			}
			catch(e)
			{
				//alert("ajax Error");
			}
		}
		else
		{
			alert("ajax Error");
			
		}
	}
	else
	{
		alert("div "+inDiv+" not found!");
	}
}

// ajaxPost子視窗功能
function ajaxPostClientWindow(inURL,inDiv,inMessage,inData)
{
	var div=document.getElementById(inDiv);
	
	if(div) // 標籤存在
	{
		removeAllChild(div);

		
		if(inMessage.length>0)
		{
			div.innerHTML=inMessage;
		}
		else
		{
			div.innerHTML="Loading...";
		}
		

		var ac=new ajaxCore();

		if(ac)
		{
			ac.outputDiv=""+inDiv;
			ac.init();
			ac.sendURL=inURL;

			// / 產生亂數,讓ajax不會被快取
			var rnd = Math.round(Math.random()*10000);

			try
			{
				ac.req.open("POST",inURL+"?"+rnd,true);
				ac.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				ac.req.send(inData);
			}
			catch(e)
			{
				//alert("ajax Error");
			}
		}
		else
		{
			alert("ajax Error");
			
		}
	}
	else
	{
		alert("div "+inDiv+" not found!");
	}
}