function ajax_xmlhttp()
{
	//XMLHttpRequest 連結
	var xmlhttp = false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}

	return xmlhttp;
}


function ajax_status(data, action, id, status)
{
   if(data.readyState==4)
	{
	   document.getElementById("process_status").innerHTML="完成～～";
	   if(data.status==200){
		   var $ = function(a){return document.getElementById(a)}
		   if(status==0){
			   if(data.responseText!=''){
				  $('B1').disabled = true;
				  alert(data.responseText);
				  $('B1').disabled = false;
			   }else{
				  $('B1').disabled = true;
				  ajax_process(action, id);
			   }
		   }else{
		     str=data.responseText;
		     var tmp=str.split('@#');
			 if(tmp[1]){
		     alert(tmp[1]);
			 }
		     if(tmp[0]!='no-path'){
			 location=tmp[0];
		     }else{
			 $('B1').disabled = false;
		     }
		   }
	   }else{
	       alert("ajax error!");
	   }
	}
}

function ajax_post(URL, action, id, status, Forum)
{   
	var data = ajax_xmlhttp();
    if(Forum!='')
	{
		if(document.forms[''+Forum+'']!='[object HTMLCollection]'){
		  var cForm = document.forms[''+Forum+''];
		}else{
		  var cForm = document.forms[0];
		}
        var sBody=getRequestBody(cForm);
	}else{
	    var sBody=null;
	}

    data.open('POST', "../config/ajax_check.php?"+URL, true);
	data.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	data.onreadystatechange=function(){ ajax_status(data, action, id, status);process_status(data);
	};
    data.send(sBody);
}


function ajax_status_no(data, kind)
{
   if(data.readyState==4)
	{
	   if(data.status==200){
           //alert(data.responseText);
		   document.getElementById(kind).innerHTML=data.responseText;
	   }else{
	       alert("ajax error!");
	   }
	}

}


function ajax_post_no(URL, kind)
{   
	var data = ajax_xmlhttp();
    data.open('GET', "../config/ckaction.php?"+URL+"&kid="+kind, true);
	data.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	data.onreadystatechange=function(){ajax_status_no(data, kind);
	};
    data.send();
}


function ajax_post_sort(URL, kind)
{   
	var data = ajax_xmlhttp();
    data.open('GET', "../config/ajax_process_sort.php?"+URL, true);
	data.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	data.onreadystatechange=function(){ajax_status_no(data, kind);
	};
    data.send();
}

function getRequestBody(oForm){
	var sParams=new Array();
	for(var i=0;i<oForm.elements.length;i++){
	if(oForm.elements[i].type!='checkbox' && oForm.elements[i].type!='radio'){
				var sParam=encodeURIComponent(oForm.elements[i].name);
	sParam+="=";
	sParam+=encodeURIComponent(oForm.elements[i].value);
	sParams.push(sParam);
	}
	if(oForm.elements[i].type=='checkbox' && oForm.elements[i].checked){
				var sParam=encodeURIComponent(oForm.elements[i].name);
	sParam+="=";
	sParam+=encodeURIComponent(oForm.elements[i].value);
	sParams.push(sParam);
	}

	if(oForm.elements[i].type=='radio' && oForm.elements[i].checked){
				var sParam=encodeURIComponent(oForm.elements[i].name);
	sParam+="=";
	sParam+=encodeURIComponent(oForm.elements[i].value);
	sParams.push(sParam);
	}
	
	}
	return sParams.join("&");
}

function process_status(data)
{
   display_on("process_status");
   if(data.readyState==1)
	{
	   document.getElementById("process_status").innerHTML="<img src='http://loveweb.yafoon.com/19-1.gif'><br>載入中～～";
	}
   if(data.readyState==2)
	{
	   document.getElementById("process_status").innerHTML="<img src='http://loveweb.yafoon.com/19-1.gif'><br>完成載入～～";
	}
   if(data.readyState==3)
	{
	   document.getElementById("process_status").innerHTML="<img src='http://loveweb.yafoon.com/19-1.gif'><br>解析交換～～";
	}
   if(data.readyState==4)
	{
	   document.getElementById("process_status").innerHTML="<img src='http://loveweb.yafoon.com/19-1.gif'><br>完成～～";
	   //sleep(1);
	   display_off("process_status");
	}
}


