// JavaScript Document
var ObjMain = window.document.getElementById('main');
var timeoutId, request,queryString,State,returnDIV;
function htmlSelected(url,Type,DivName,MainID,ID){
	if(url!="" && DivName!=""){
		State=url;
		returnDIV=document.all[DivName];
		httpRequest("GET",url+"?MainID="+MainID+"&ID="+ID+"&Type="+Type,true);
	}
}

 /* Initialize a Request object that is already constructed */
function initRun(reqType,url,bool){
   /* Specify the function that will handle the HTTP response */
	request.onreadystatechange=handleCheck;
	//alert(hostName+url+pattern);
	request.open(reqType,url,bool);
	//alert(hostName+url+pattern);
	timeoutId = setTimeout(warn,10000);
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	request.send(queryString);
}


//event handler for XMLHttpRequest
function handleCheck(){
  if(request.readyState == 4){
    clearTimeout(timeoutId);
    if(request.status == 200){
      //Implement document object in DOM
		 // alert(returnDIV);
		returnDIV.innerHTML = request.responseText;
     } else {
		//ObjMsg.innerHTML='';
      alert("系統發生錯誤 \n錯誤代碼:"+request.status+" \n\n請洽系統管理員");
    }
  }//end outer if
}
 
function warn(){
	request.abort();
    alert("因網路延遲造成錯誤 \n\n請檢查您的網路連線或稍候再試!");
}
 
/* Wrapper function for constructing a Request object.
 Parameters:
  reqType: The HTTP request type such as GET or POST.
  url: The URL of the server program.
  asynch: Whether to send the request asynchronously or not. */
function httpRequest(reqType,url,asynch){
    //Mozilla-based browsers
    if(window.XMLHttpRequest){
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject){
		try{
			request=new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			try{
				request=new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				alert('您的瀏覽器版本太舊,請更新至最新版本!');
			}
		}
	}

	//the request could still be null if neither ActiveXObject
    //initializations succeeded
    if(request){
       initRun(reqType,url,asynch);
    }else{
        alert("Your browser does not permit the use of all "+
        "of this application's features!");
	}
}

