
function fillCategories(host, obj, section, form, first) {
	
	this.obj = obj;
	this.form = form;

	
	
	/****************************************
	* Setting the form action
	*****************************************/
	var array_resultPages = new Array();
	array_resultPages["listing"] 	= "results.php";
	array_resultPages["product"] 	= "products_results.php";
	array_resultPages["event"] 		= "events_results.php";
	array_resultPages["promotion"] 	= "promotion_results.php";
	array_resultPages["article"] 	= "Online-Resources/results.php";
	//alert(host+" | "+host.charAt(host.length-1));
	form.action = host+"/"+array_resultPages[section];
	/****************************************/
	
	if (!first) {
		resetSelect(obj);
		
		// check if object exisits.
		if(typeof obj != 'object') return false;

		obj.disabled=true;
 		
 		url = host+"/fillcategory.php?section="+section;
 		
 		loadCategoriesResult(url, '');
 	}
	
}

function str_replace(search, replace, subject) {
    
    var f = search, r = replace, s = subject;
    var ra = is_array(r), sa = is_array(s), f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
    };
     
    return sa ? s : s[0];
}

function is_array( mixed_var ) {
    return ( mixed_var instanceof Array );
}

function loadCategoriesResult(url, result) {
	if (result != ''){
		// Response mode
		for(i=0; i < result.length; i++){
			this.obj.options[this.obj.options.length]  = new Option(str_replace("+", " ", result[i].name), result[i].id);
		}
	} else if(url != '') {
		// Input mode
		return (loadXMLCategories(url));
	}
	
}

function loadXMLCategories(url) {

	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processXMLCategories;
		req.open("GET", url, true);
		req.send(null);
		// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
	
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processXMLCategories;
			req.open("GET", url, true);
			req.send();
		}
		
	}
	
}

function processXMLCategories() {

	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here
			response  = req.responseXML.documentElement;
			if(response) {
				var result = new Array();
				for(i=0; i < response.getElementsByTagName('id').length; i++) {
					result[i] = {'id': response.getElementsByTagName('id')[i].firstChild.data, 'name': response.getElementsByTagName('name')[i].firstChild.data};
				}
				loadCategoriesResult('', result);
				unlockCategories();
			}
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
			unlockCategories();
		}
		unlockCategories();
	}
	
}

function unlockCategories() {
	this.form.category_id.disabled = "";
	
}