﻿function ReplaceAll(iStr, v1, v2)
{
	var i = 0, oStr = '', j = v1.length;
	while (i < iStr.length)
	{
		if (iStr.substr(i, j) == v1)
		{
			oStr+=v2;
			i+=j
		}
		else
		{
			oStr+=iStr.charAt(i);
			i++;
		}
	}
	return oStr;
}


// declare a global  XMLHTTP Request object
var XmlHttpObjSearch;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObjSearch()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObjSearch = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObjSearch = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObjSearch = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	/* if(!XmlHttpObjSearch && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObjSearch = new XMLHttpRequest();
	} */
	if (window.XMLHttpRequest)
	{
		XmlHttpObjSearch = new XMLHttpRequest();
	}
}

// called from onChange or onClick event of the continent dropdown list
function CreateAjaxGetSearch(query)   // url chi la ten file VD : dangky.asp
{
	CreateXmlHttpObjSearch();
//  verify XmlHttpObjSearch variable was successfully initialized	
	if(XmlHttpObjSearch)
	{
		XmlHttpObjSearch.onreadystatechange = NhanPhanHoiSearch;
		//XmlHttpObjSearch.open("POST", url,  true); // url chi la ten file VD : dangky.asp
		//XmlHttpObjSearch.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		//XmlHttpObjSearch.send(getquerystringSearch(theform));	
		//url=query //url+ "?" + query;	
		XmlHttpObjSearch.open("GET",query,  true);
		XmlHttpObjSearch.send(null);
	}
}

function CreateAjaxPostSearch(theform,url)   // url chi la ten file VD : dangky.asp
{
  	CreateXmlHttpObjSearch();
// verify XmlHttpObjSearch variable was successfully initialized
	if(XmlHttpObjSearch)
	{
		XmlHttpObjSearch.onreadystatechange = NhanPhanHoiSearch;
		XmlHttpObjSearch.open("POST", url,  true); // url chi la ten file VD : dangky.asp
		XmlHttpObjSearch.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		XmlHttpObjSearch.send(getquerystringSearch(theform));
	}
}

function getquerystringSearch(theform) {
	var theElements = theform.elements;
	for (k=1;k<=theElements.length;k++)
	var theKey;
	var qstr;
	qstr='';
	for (k=1;k<=theElements.length;k++) {//Chỗ nya2 chưa có dữ liệu
		theKey=theElements[k-1].name;
		if (k<theElements.length)
			qstr=qstr+ theKey + '=' + ReplaceAll(theform[theKey].value, '&', '*|*') + '&';
		else
			qstr=qstr+ theKey + '=' + ReplaceAll(theform[theKey].value, '&', '*|*');
	}
	qstr = ReplaceAll(qstr,'+','*^*');
	qstr = ReplaceAll(qstr,'%','*!*');
	qstr = ReplaceAll(qstr, "&&", "&*|*")
	qstr = ReplaceAll(qstr,' ','%20');
	return qstr;
}

function NhanPhanHoiSearch() 
{
	if (XmlHttpObjSearch.readyState == 4) // only if req shows "complete"
	{
		if (XmlHttpObjSearch.status == 200) // only if "OK"
		{
			XuLyQuerySearch(XmlHttpObjSearch);
		} 
		else 
		{
			alert("There was a problem retrieving the XML data:\n" + XmlHttpObjSearch.statusText);
		}
	}
}