var http_request;
var contentdiv;

//-------------------------------------------------------------------------------------
/**
* Ham danh cho kieu du lieu truyen vao la 1 url
*
* @Param string data	-	du lieu duoc truyen duoi dang cac tham so nhu: &x=1&y=2&z=3
* @Param string url		-	duong dan den trang xu ly yeu cau nhu: process.php
* @Param string method	-	kieu truyen du lieu di la POST hay GET
*/
function AjaxByID(id_value,url,method)
{
	data = '?id=' + escape(id_value);
	GetPage(data,url,method);		
}
//-------------------------------------------------------------------------------------

function AjaxURL(url,method,form_name){
	data=ConvertFormValue(form_name);
	data='?' + data;	
	GetPage(data,url,method);	
}

//-------------------------------------------------------------------------------------
/**
* Ham danh cho kieu du lieu truyen vao la 1 form
*
* @Param string url			-	duong dan den trang xu ly yeu cau nhu: process.php
* @Param string method		-	kieu truyen du lieu di la POST hay GET
* @Param string form_name	-	ten cua form
*/
function AjaxForm(url,method,form_name){
	data=ConvertFormValue(form_name);
	GetPage(data,url,method);	
}
//-------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------
/**
* Ham convert du lieu tu cac textbox, checkbox, va radio thanh chuoi du lieu. 
  Vi du: username=wanderedman&fullname=hoquan&age=23
*/
function ConvertFormValue(form_name)
{
	var param="";
	var frmlength=document.forms[form_name].length;
	for(var i=0;i<frmlength;i++)
	{
		if(document.forms[form_name].elements[i].type=="checkbox" || document.forms[form_name].elements[i].type=="radio")
		{
			if(document.forms[form_name].elements[i].checked==true)
			{
				param=param + document.forms[form_name].elements[i].name + "=" + document.forms[form_name].elements[i].value + "&";
			}
		}
		else if(document.forms[form_name].elements[i].type!="button" && document.forms[form_name].elements[i].value!="")
		{
			param=param + document.forms[form_name].elements[i].name + "=" + document.forms[form_name].elements[i].value + "&";
		}
	}
	param=param.substr(0,param.length-1);
	//alert(param);	
	return param;
	
}	
//-------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------
/**
* Ham thuc hien xu ly cua Ajax
*
* @Param string data	-	du lieu duoc truyen duoi dang cac tham so nhu: &x=1&y=2&z=3
* @Param string url		-	duong dan den trang xu ly yeu cau nhu: process.php
* @Param string method	-	kieu truyen du lieu di la POST hay GET
*/
function GetPage(data,url,method)
{
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		//http_request = new ActiveXObject("Microsoft.XMLHTTP");	
		try{
			// Try to create XMLHttpRequest in later versions of Internet Explorer
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
    	}catch (e1){
			 // Failed to create required ActiveXObject
			try{
				// Try version supported by older versions of Internet Explorer
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e2){
				// Unable to create an XMLHttpRequest with ActiveX
				http_request = null;
				//document.location = "http://volam.com.vn/";
			}
      	}
	}

	http_request.onreadystatechange = DisplayResponse;
	if(method=='POST')  //kieu truyen du lieu di la POST
	{
		http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; ");
		http_request.send(data);
	}
	else //kieu truyen du lieu di la GET
	{
		http_request.open("GET", url + data, true);
		http_request.send(null);
	}	
}
//-------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------
/**
* Ham hien thi ket qua tra ve tu trang xu ly phia server
*/
function DisplayResponse()
{
	contentdiv = document.getElementById('content');
	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
			contentdiv.innerHTML = http_request.responseText;
			//contentdiv.innerHTML = 'Ban da dang ky thang cong';
		}
		else
		{
			contentdiv.innerHTML = '<div style="text-align:center; color:#636669; padding:15px; background-color:#E3E6E9;">Error while downloading data.Please try again...</div>';
		}
	}
	else //http_request.readyState == 3 // waiting state
	{
		contentdiv.innerHTML = '<div style="text-align:center; color:#636669; padding:15px; background-color:#E3E6E9;"><img src="images/progress.gif" border="0">&nbsp;waiting...</div>';
	}
}
//-------------------------------------------------------------------------------------
