/* JavaScript 變數定義區 */
/* Ajax */
var data        ="";
var ajax_request_type="GET";

/* JavaScript 函數區 */
//*****************************************************************************
//字串空白處理
//*****************************************************************************
String.prototype.trim = function()
{
    return this.replace(/(^[\s]*)|([\s]*$)/g, "");
}
String.prototype.lTrim = function()
{
    return this.replace(/(^[\s]*)/g, "");
}
String.prototype.rTrim = function()
{
    return this.replace(/([\s]*$)/g, "");
}
//*****************************************************************************
// 初始化-OK
//*****************************************************************************
function ajax_init_object()
{
	var A;
	if (window.XMLHttpRequest)
	{
		A=new XMLHttpRequest();
		if (A.overrideMimeType)
		{
			A.overrideMimeType("text/xml");
		}
	}
	else if (window.ActiveXObject)
	{
		try 
		{
			A=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				A=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	if (!A)
	{
		window.alert("無法建立 XMLHttpRequest 物件。");
		return false;
	}
	return A;
}
//*****************************************************************************
//ajax:檔案目錄-OK
//*****************************************************************************
function ajax_send(url,block_id,url2)
{
	var post_url;
	var post_data;
	var p;
	var str;
	var request_type;

	request_type=ajax_request_type;

	if (request_type=="GET")
	{
		post_data=null;
	}
	else
	{
		p=url.indexOf("?");
		if (p==-1)
		{
			request_type="GET";
			post_data=null;
		}
		else
		{
			str=url.split("?");
			post_url=str[0];
			post_data=str[1];
		}
	}

	x=ajax_init_object();
	if (request_type=="GET") x.open(request_type,url,true);
	else                     x.open(request_type,post_url,true);
	x.onreadystatechange=function()	
	{
		if (x.readyState==4)
		{
			if (x.status==200)
			{
				var data=x.responseText;
				show_status(false);
				if (data !="~~Err~~")
				{
					try
					{
						if (block_id != "")
						{
							if (block_id == "logout")
							{
								window.location="logout.php";
							}
							else
							{
								if (block_id=='refresh')
								{
									window.location=url2;
								}
								else
								{
									document.getElementById(block_id).innerHTML=data;
								}
							}
						} 
					}
					catch (e) {}
				}
				delete x;
			}
			else
			{
				show_status(false);
				alert("您所請求的頁面有問題。");
			}
		}
		else
		{
			//
			return;
		}
	}
	if (request_type=="POST")
	{
		x.setRequestHeader("Method","POST "+post_url+" HTTP/1.1");
		x.setRequestHeader("Content-Length",post_data.length);
		x.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	}
	//強制重讀資料
    x.setRequestHeader("If-Modified-Since","0");
	x.send(post_data);
}

//*****************************************************************************
//顯示更新狀態-OK
//*****************************************************************************
function show_status(ac)
{
	if (ac)
	{
		document.getElementById("status").className = "show";
		//document.getElementById("status").style.left = (screen.width-200)/2;
		//document.getElementById("status").style.top = document.body.scrollTop+240;
	}
	else
	{
		document.getElementById("status").className = "hide";
	}
}

//*****************************************************************************
//重新統計數量
//*****************************************************************************
function calcProduct(url,url2)
{
	show_status(true);
	ajax_send(url,'refresh',url2);
}