// if IE is used, create a ActiveXObject to create new XMLHttpRequest object
if( typeof XMLHttpRequest == "undefined")
	XMLHttpRequest = function(){
		return new ActiveXObject(
			// Because ie5 is defferent from ie6
			navigator.userAgent.indexOf("MSIE 5")>=0?"Microsoft.XMLHTTP":"Msxml2.XMLHTTP"
		);
	};



// a genaric function for performing Ajax request.
// INPUT: options that contains a set of options
// OUTPUT: NULL
function ajax(options){
	var options = {
		// Type of HTTP Request
		requestType: options.requestType || "POST",
		// Type of Data returned from server
		dataType: options.dataType || "html",
		// URL of server that Ajax Request is connected to.
		url: options.url || "",
		// Timeout value that how long Request wait for response
		timeout: options.timeout || "10000",
		
		// Functions that is called 
		// when Request Fails, Complete( fail or succeed) and Succeed.
		onError: options.onError || function(){},
		onComplete: options.onComplete || function(){},
		onSuccess: options.onSuccess || function(){},
		
		// The data which will be returned by server
		data: options.data || ""
		
	};
	// Create new Request object
	var xml = new XMLHttpRequest();
	// Timeout value
	var timeout = 5000;
	// Keep track of when request is completed;
	var requestDone = false;
	// Open asynchronous request to server
	xml.open(options.requestType,options.url,true);
	
	// initalize a callback when request fired 5 seconds from now
	// cancelling the request
	setTimeout(
		function(){ requestDone=true; },
		timeout
	);
	
	xml.onreadystatechange = function(){
		// wait until data is Fully loaded
		// and sure that request is not timeout yet.
		if(xml.readyState==4 || !requestDone || xml.readyState=="complete"){
			// Check that request is Success
			// if Success, Execute 'Success' callback with
			// Data returned from server
			if(isRequestSuccess(xml)){
				options.onSuccess(getResponseData(xml, options.dataType));
			}else{
				options.onError();
			}
			// Call completion callback
			options.onComplete();
			
			// set XML = null
			//xml = null;
		}
	};
	
	// Establish the connection to server
	xml.send(null);
	
	// Function that check Request is Success or not.
	function isRequestSuccess(r){
		try{
			return 	!r.status && location.protocol=="file:" ||
					(r.status>=200 && r.status<300) ||
					r.status == 304 ||
					navigator.userAgent.indexOf("safari")>=0 && typeof r.status=="undefined";
		}catch(e){}
		return false;
	};
	
	// Function get Data returned from Server
	// INPUT: Request Object, type of request.
	// OUTPUT: Data.
	function getResponseData(r,type){
		// get Content-Type header
		var ct = r.getResponseHeader("content-type");
		// check that Default type was not provided and index of 
		// 'xml' string is greater than 0
		var data = !type && ct && ct.indexOf("xml")>=0;
		// get XML Document if condition above is satisfied.
		// otherwise Text Document instead of.
		data = ((type=="xml") || data) ? r.responseXML:r.responseText;
		// if the specified type is 'script', execute the responsed text
		// as if it is jscript
		if(type=="script"){
			eval.call(window,data);
		}
		
		// Return response data ( either a XML Document or a text string)
		return data;
	};
	
};

function getPopupMenuContent(s,t){
	var obj = document.getElementById("popup_menu");
	var parent = obj.parentNode;
	var cloneObj = obj.cloneNode(true);
	parent.insertBefore(cloneObj, obj);
	var child = DOMUtil.getDescendantsByClassName(cloneObj,"div","boundary")[0];
	var title = DOMUtil.getDescendantsByClassName(cloneObj,"div","PopupTitle")[0];
	var more = DOMUtil.getDescendantsByClassName(cloneObj,"a","PopupMore")[0];
	cloneObj.id = "popup_menu_" + s;
	child.id = "PopupContent_" + s;	
	title.id="PopupTitle_" + s;
	more.id="PopupMore_"+s;
	ajax({
		requestType: "GET",
		dataType: "html",
		url: "filter.php?name=" + s,
		timeout: 5000,
		onSuccess:function(html){
			child.innerHTML = html;
			title.innerHTML = t;
		}
	});
}

var filters = new Array("nation", "saler", "manufacturer"); 
var naming = new Array("Xu&#7845;t x&#7913;","Ng&#432;&#7901;i b&#225;n","Th&#432;&#417;ng hi&#7879;u");
window.onload = function(e){
	for( var i=0;i< filters.length;i++)
		getPopupMenuContent(filters[i],naming[i]);
}

