
var ajaxObj = new Array();
var isIE = false;

/*
* @Class Ajax
* @version 2.0 
* @author vasupat chantakeaw
* @email thaiajax@gmail.com
*/

function ajax(){
	
	this.xmlhttp;
	this.formElement;
	this.requestFile;
	this.URLString;
	this.innerDivID;
	this.method 		= 'GET';
	this.responseType 	= 'Text'; /*Text , XML*/
	this.loadingType 	= 0;
	this.async 			= true;
	this.onCompletion 	= function() { };
	this.onProcess 		= function() { };
	this.loading 		= function(v){
		var v;
		if(this.loadingType>0){
			if(this.loadingType==1){
				if(!g("loading")) {this.createLoaddingDiv();}
			}
			if(v==1){
				if(this.loadingType==1){ g("loading").style.visibility = "visible";}
			}else{
				if(this.loadingType==1){ g("loading").style.visibility = "hidden";}
			}
		}
	}
	
	this.ConnXmlHttp = function(){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				isIE = true;
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				xmlhttp = false;
			}
		}
	
		if(!xmlhttp && document.createElement){
			xmlhttp = new XMLHttpRequest();
		}
		
		return xmlhttp;
	}
	
	this.getRequestBody = function(myForm) {
		var aParams = new Array();  
			for (var i=0 ; i < myForm.elements.length; i++) {
				
				var formElement = myForm.elements[i];
				if(formElement.type=='checkbox' && !formElement.checked){ continue;} 
            	var sParam = encodeURIComponent(myForm.elements[i].name);
               	sParam += "=";
               	sParam += encodeURIComponent(myForm.elements[i].value);
               	aParams.push(sParam);
           	}     
           	return aParams.join("&");        
	}
	
	this.loadXMLDoc = function() {
	
		var self = this;
		this.xmlhttp = this.ConnXmlHttp();

		
		if (this.method == "GET") {
			this.xmlhttp.open(this.method, this.requestFile, this.async);
		}else{
			this.URLString = this.getRequestBody(this.formElement);
			this.xmlhttp.open(this.method,this.requestFile, this.async);
		}
		if (this.method == "POST"){
  			try {
				this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')  
			} catch (e) {}
		}
		this.xmlhttp.onreadystatechange = function(){
			if(self.xmlhttp.readyState==4){
				if(self.loadingType==1){ self.loading(0);}
				if (self.xmlhttp.status == 200) {
					if(self.responseType == "Text"){
						self.response = self.xmlhttp.responseText;
					}else{
						self.response = self.xmlhttp.responseXML;
					}	
					
					self.onCompletion();
				}
			}else{
				if(self.loadingType==1){ self.loading(1);}
				if(self.loadingType==2){ 
					self.response = ""; //"<img src='ajax-loader.gif'>";
					self.onCompletion();
				}
			}
		}
		this.xmlhttp.send(this.URLString);
	}
	
	/*
	* Show Loading
	*/
	
	this.createLoaddingDiv = function(){
	
		var connLoading = document.createElement("DIV")
			connLoading.id = "loading";
			connLoading.style.position="absolute";
			connLoading.style.left="0px";
			connLoading.style.top="0px";
			connLoading.style.width="185px";
			connLoading.style.height="15px";
			connLoading.style.zIndex=99999;
			connLoading.style.backgroundColor="#FF0000";
			connLoading.style.visibility = "visible";
			connLoading.style.font = '11px Lucida Sans Unicode';
			connLoading.style.filter = 'alpha(opacity=60)';
			connLoading.innerHTML = " working on your request...";
		document.body.appendChild(connLoading);
		
	}
}

function getRootNode(xmlHttpReq,tagName) {
	return xmlHttpReq.getElementsByTagName(tagName);
}
/*
* ex getNodeValue("","html_content","root[0]",0)
*/

function getNodeValue(prefix, local, parentElem, index) {

	try{
		var result = "";
		if (prefix && isIE) {
			/* IE/Windows way of handling namespaces */
			result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
		} else {
			result = parentElem.getElementsByTagName(local)[index];
		}
		if (result) {
			/* get text, accounting for possible */
			if (result.childNodes.length > 1) {
				return result.childNodes[1].nodeValue;
			} else {
				return result.firstChild.nodeValue;    		
			}
		} else {
			return "n/a";
		}
	}
	catch(e){
		alert("xml error");
	}
}
/* function gelElement */
function g(namex){if (document.getElementById){return document.getElementById(namex);}
else if (document.all){return document.all[namex];}else{return null;}}

/* เรียกใช้งาน Class Ajax แบบ Get*/
function getAjaxGetUrl(index,requestFile){
	var index; var requestFile;
	ajaxObj[index] = new ajax();
	ajaxObj[index].method="GET";
	ajaxObj[index].responseType="Text";
	ajaxObj[index].loadingType = 2;
	ajaxObj[index].requestFile=requestFile;
	ajaxObj[index].onCompletion = function(){ showAjaxGetUrl(index);};
	ajaxObj[index].loadXMLDoc();
	
}
function getAjaxGetUrl2(index,requestFile){
	var index; var requestFile;
	ajaxObj[index] = new ajax();
	ajaxObj[index].method="GET";
	ajaxObj[index].responseType="Text";
	ajaxObj[index].loadingType = 2;
	ajaxObj[index].requestFile=requestFile;
	ajaxObj[index].onCompletion = function(){ showAjaxGetUr2(index);};
	ajaxObj[index].loadXMLDoc();
	
}
/* แสดงข้อมูล */

function showAjaxGetUrl(index){
//	g('showDataDiv').innerHTML = ajaxObj[index].response;
	g(index).innerHTML = ajaxObj[index].response;
}
function showAjaxGetUrl2(index){
window.refresh;
}
function changeStatus(blogid,status,id)
{
	var returndata = 'showstatus'+id;
	var url = 'changestatus.php?blogid='+blogid+'&status='+status+'&id='+id;
	getAjaxGetUrl(returndata,url);
}
function ShoutMsg()
{
if(document.frm2.msg.value==""){ alert("กรุณาใส่ข้อความด้วยค่ะ");  return false;}
var result5;
var 	UsrName =document.getElementById('usrname').value;
var 	msg = document.frm2.msg.value;
	var returndata = 'shoutshout';
	var url = '/addon/shoutbox/Shoutbox3.php?UsrName='+ UsrName + '&msg='+msg; 
	getAjaxGetUrl(returndata,url);
	document.getElementById('shoutMsg').value ="";
}

function ShoutMsg1()
{
	var returndata = 'shoutshout';
	var url = '/addon/shoutbox/Shoutbox3.php'
	getAjaxGetUrl(returndata,url);
	document.getElementById('shoutMsg').value ="";
	document.getElementById('shoutshout').innerHTML =result;
}

