/* 
* Module Name : ajax_interface.js
* Description :  ajax interface module
* Written By : Anusorn Pholpak
* Create Date : 26-Oct-2006
* Fixed list
* 2-Mar-2007 - update xml dom for cross-browser
*/
var ajax_output = null;
function ajax_request(url, args, content) {
	if (typeof content!='undefined') {
		ajax_output = content;
	}
	var ajax_req = false;
	var d = new Date();
	var ajax_url = url+"?"+args+"&tm="+d.getTime();
	if (window.XMLHttpRequest) {
		ajax_req = new XMLHttpRequest();
		if (ajax_req.overrideMimeType) {
			ajax_req.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try
		{
			ajax_req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				ajax_req = new ActiveXObject("Microsoft.XMLHTTP");		
			}
			catch (e)
			{
			}
		}
	}
	if (!ajax_req) 
	{
		ajax_status("Can't create xmlhttp request.");
		return false;
	}
	ajax_req.onreadystatechange = function() {
		ajax_callback(ajax_req);
	};
	ajax_req.open("GET", ajax_url, true);
	ajax_req.send(null);
}

function ajax_post(url, args, content) {
	if (typeof content!='undefined') {
		ajax_output = content;
	}
	var ajax_req = false;
	var d = new Date();
	var ajax_url = url;
	var ajax_parameters = args+"&tm="+d.getTime();
	if (window.XMLHttpRequest) {
		ajax_req = new XMLHttpRequest();
		if (ajax_req.overrideMimeType) {
			//ajax_req.overrideMimeType('text/xml');
            ajax_req.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) {
		try
		{
			ajax_req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				ajax_req = new ActiveXObject("Microsoft.XMLHTTP");		
			}
			catch (e)
			{
			}
		}
	}
	if (!ajax_req) 
	{
		ajax_status("Can't create xmlhttp request.");
		return false;
	}
	ajax_req.onreadystatechange = function() {
		ajax_callback(ajax_req);
	};
	ajax_req.open('POST', ajax_url, true);
	ajax_req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajax_req.setRequestHeader("Content-length", ajax_parameters.length);
	ajax_req.setRequestHeader("Connection", "close");
	ajax_req.send(ajax_parameters);
}

function ajax_status(msg) {
		if (ajax_output==null) {
			window.status = msg;
		} else {
			ajax_output.innerHTML = msg;
		}
}

function ajax_message(msg) {
		if (ajax_output==null) {
			if (typeof msg!='undefined' && msg.length > 0)
			{
				alert(msg);
			}
		} else {
			ajax_output.innerHTML = msg;
		}
}

function ajax_callback(ajax_req) {
	if (ajax_req.readyState == 1) {
		window.status = "กำลังส่งข้อมูลให้ระบบ...";
	} else if (ajax_req.readyState == 4) {
		if (ajax_req.status == 200){
			window.status = "ได้การตอบสนองจากระบบแล้ว...";
			if (ajax_req.responseText)
			{
				status_code = parseXML(ajax_req.responseXML.documentElement);	
				if (status_code == 0) {
					window.status = "เสร็จสิ้น";
				} else if (status_code > 0) {
					window.status = "เสร็จสิ้น ด้วยสถานะ "+status_code;
				} else {
					window.status = "มีข้อผิดพลาดเกิดขึ้น...";
				}
			}
		} else {
			window.status = "ไม่ได้การตอบสนองจากระบบ...";
		}
	}
}

function parseXML(obj) {
	var str_tmp = '';
	var str_response = '';
	var status_code = -1;
	if (obj.hasChildNodes()) {
		if(typeof obj.tagName!='undefined' && obj.tagName=='ajax') {
			var nodes = obj.childNodes.length;
			for(var i = 0; i < obj.childNodes.length; i++) {
				if (obj.childNodes[i].tagName == 'status')
				{
					status_code = parseInt(obj.childNodes[i].childNodes[0].nodeValue);
				}
				if (obj.childNodes[i].tagName == 'message')
				{
					str_tmp = obj.childNodes[i].childNodes[0].nodeValue;
					if (str_tmp != 'none')
					{
						ajax_message(""+str_tmp)
					}
				}
				if (obj.childNodes[i].tagName == 'response')
				{
					str_response = obj.childNodes[i].childNodes[0].nodeValue;
				}
				if (obj.childNodes[i].tagName == 'nextaction')
				{
					if (obj.childNodes[i].childNodes[0].nodeValue == 'close')
					{
						if (typeof window.parent.parent!='undefined') window.parent.parent.close();
						else if (typeof window.parent!='undefined') window.parent.close();
						else window.close();
					}
					else if (obj.childNodes[i].childNodes[0].nodeValue == 'reload')
					{
						window.location.reload();
					}
					else if (obj.childNodes[i].childNodes[0].nodeValue == 'parent_reload')
					{
						window.parent.location.reload();
					}
					else if (obj.childNodes[i].childNodes[0].nodeValue == 'opener_reload')
					{
						window.opener.location.reload();
					}
					else if (obj.childNodes[i].childNodes[0].nodeValue == 'opener_focus')
					{
						window.opener.focus();
					}
					else if (obj.childNodes[i].childNodes[0].nodeValue == 'ajax_success')
					{
						ajax_success();
					}
					else if (obj.childNodes[i].childNodes[0].nodeValue == 'ajax_response')
					{
						ajax_response(str_response);
					}
				}
			}
		}
	}
	return status_code;
}
