// JavaScript Document
var ObjMain = window.document.getElementById('main');
var timeoutId, request,queryString,State,ErrorFlag;
function htmlSelected(url,id,sid){
	State=id;
    httpRequest("GET",url+".php?mid="+id+"&sid="+sid,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(State);
		if(State<=6){
			xmlReturnVal = request.responseText;
			document.all['list'+State].innerHTML=xmlReturnVal;
		}
     } else {
		//ObjMsg.innerHTML='';
		if(ErrorFlag!="yes"){
			ErrorFlag="yes";
			alert("系統發生錯誤 \n錯誤代碼:"+request.status+" \n\n請洽系統管理員");
		}
		  return false;
    }
  }//end outer if
}
 
function warn(){
		if(ErrorFlag!="yes"){
			ErrorFlag="yes";
			request.abort();
			//ObjMsg.innerHTML='';
			alert("因網路延遲造成錯誤 \n\n請檢查您的網路連線或稍候再試!");
		}
  return false;
}
 
/* 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!");
	}
}

function postData(url,no){
	State=url;
    setQueryString(no);
    httpRequest("POST",url,true);
}


function setQueryString(no){
    queryString="";
	if(document.all['fid'+no]!=null && document.all['mid'+no]!=null && document.all['zstore'+no]!=null){
		queryString += "mid2="+encodeURIComponent(document.all['fid'+no].value)+"&";
		queryString += "mid3="+encodeURIComponent(document.all['mid'+no].value)+"&";
		queryString += "zstore="+encodeURIComponent(document.all['zstore'+no].value);
		document.all['fid'+no].value='';
		document.all['mid'+no].value='';
		document.all['zstore'+no].value='';
	}
}
