/*
**************** Epiphayny ***********************
**************** VN Team *************************
**************** NguyenTrungHa ******************

The 'ajaxSendingRequest' function is main function. It will receive arguments but it must have four main parameters
1. target 		: name of div tag. It's a place where the result will shown
2. targetType 	: type of tag. There are three main type
					+ 'div'		: tag's type is div
					+ 'text'	: tag's type is controls such as textbox
					+ 'div+'	: add more content into the div tag
					+ 'td'		: tag's type is td
3. parameters	: It is a flag. View examples, you will understand
4. url			: the path of file which contains content shown

Example :  
"index.php"
<html>
<script src="jsAjax.js" language="javascript" type="text/javascript"></script>
<body>
	<div id = 'showAjax'>
	</div>
	<input type='button' value='Show Ajax' onclick="javascript:"ajaxSendingRequest('showAjax','div','ajax_type=show','ajaxResponse.php');">
</body>
</html>

"ajaxResponse.php"
<?php
$ajax_type = $_POST["ajax_type"];
$html = "";
switch($ajax_type)
{
	case "show":
			$html = "con khi kho";
			echo $html;
			break;
	case "hide":
			$html = "&nbsp;";
			echo $html;
			break;
	case "add":
			$html = "con heo map";
			echo $html;
			break;
	default:
			$html = "dry monkey";
			echo $html;
} 
?>
*/

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
	  	// Firefox, Opera 8.0+, Safari
	  	xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	  	// Internet Explorer
	  	try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  	catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
function showResult(xmlHttp,target,targetType,paramaters)
{
  if(xmlHttp.readyState==4)
	try
	{
		switch (targetType)
		{
			case "div":
					document.getElementById(target).innerHTML = xmlHttp.responseText;
					break;
			case "text":
					document.getElementById(target).value = xmlHttp.responseText;
					break;
			case "div+":
					document.getElementById(target).innerHTML +=  xmlHttp.responseText;
					break;
			case "td":
					document.getElementById(target).innerHTML =  xmlHttp.responseText;
					break;
			default:
					document.getElementById(target).value = xmlHttp.responseText;
		}
	}catch(e) {;}
}
// main function
function ajaxSendingRequest()
{
	var func = ajaxSendingRequest;
	var count = func.arguments.length;
	var target,targetType,parameters,defaultURL;
	temp = new Array();
	for(i=0;i<count;i++)
	{
		switch (i)
		{
			case 0 : target = func.arguments[i];			break;
			case 1 : targetType = func.arguments[i];		break;
			case 2 : parameters = func.arguments[i];		break;
			case 3 : defaultURL = func.arguments[i];		break; 
			default:
					temp[i] = func.arguments[i];
		}
	}
	//alert(target);
	//alert(targetType);
	//alert(parameters);
	//alert(defaultURL);
	//window.open(defaultURL);
	
	
	var xmlHttp = GetXmlHttpObject();
	var url = "ajaxResponse.php";
	if (defaultURL != "") url = defaultURL;
	xmlHttp.onreadystatechange=function()
	{
		//if(xmlHttp.readyState==4)
		//{
		//  	document.getElementById(target).innerHTML = xmlHttp.responseText;
		//}
		showResult(xmlHttp,target,targetType,parameters);
	}
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  	xmlHttp.setRequestHeader("Content-length", parameters.length);
  	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
}