/* LOAD ONLINE USERS LIST */
function loadsearch(q) {
	if ((q.length < 0)) {
		alert('Invalid request!');
		return false;
	}else if (q.length > 1){
	   document.getElementById('livesearch').style.width='448px';
       document.getElementById('livesearch').style.border='1px solid #A5ACB2';
       document.getElementById('livesearch').style.backgroundColor ="#f3f3f3"; 
       var eurl = document.getElementById('lsarea').innerHTML+'mod_eshop_search/lsearch.php';
	   var etype= 'POST';
	   var eloadelement = 'livesearch';
	   if (typeof jQuery != 'undefined') {
	       	var eloadtext = null;
		      $('#'+eloadelement).slideUp();
	   } else {
	       	var eloadtext = 'Φόρτωση δεδομένων ...';
    	}

	   var successfunc = function(data) {
	       	if (typeof jQuery != 'undefined') {
		      	$('#'+eloadelement).html(data);
			     $('#'+eloadelement).slideDown();
		} else {
			document.getElementById(eloadelement).innerHTML = data;
		}
	};
	var errorfunc;
	var edata = { 'q': q };

	elxAjax(etype, eurl, edata, eloadelement, eloadtext, successfunc, errorfunc);
	}else if(q.length <=1 && q.length >= 0){
	   document.getElementById('livesearch').style.display = 'none';
	}


}

/* CREATE A STANDARD AJAX OBJECT */
function newStdAjax() {
    var ro;
    if (window.XMLHttpRequest) {
        ro = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        ro = new ActiveXObject("Msxml2.XMLHTTP");
        if (!ro) {
            ro = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return ro;
}

/* AJAX WRAPPER - WORKS WITH OR WITHOUT JQUERY */
function elxAjax(etype, eurl, edata, eloadelement, eloadtext, successfunc, errorfunc) {
	if ((etype == null) || (etype == '')) { etype = 'GET'; }
	if (eurl == '') { return false; }


	if (successfunc == null) {
		if ((eloadelement != null) && (eloadelement != '')) {
			successfunc = function (result) {
				document.getElementById(eloadelement).innerHTML = result;
			}
		} else {
			successfunc = function (result) { }
		}
	}

 	if ((eloadtext != null) && (eloadtext != '') && (eloadelement != null) && (eloadelement != '')) {
 		document.getElementById(eloadelement).innerHTML = eloadtext;
 		if (typeof jQuery != 'undefined') {
 			$('#'+eloadelement).fadeIn('slow');
		 } else {
		 	document.getElementById(eloadelement).style.display = '';
	 	}
	}

	if (etype == 'GET') {
		if (typeof jQuery != 'undefined') {
			if (edata && (typeof (edata) === 'object')) { edata = $.param(edata); }
        	$.ajax({
            	type: 'GET',
            	url: eurl,
            	data: edata,
            	success: successfunc,
            	error: errorfunc
        	});
		} else {
			var rhttp = newStdAjax();
			if (edata && (typeof (edata) === 'object')) {
				var sdata = '';
				for (k in edata) { sdata += k+'='+edata[k]+'&'; }
				sdata += 'rnd='+Math.random();
				edata = sdata;
			}

			try {
            	rhttp.open('GET', eurl+'?'+edata, true);
            	rhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            	rhttp.setRequestHeader('charset', 'utf-8');
				rhttp.onreadystatechange = function () {
					if (rhttp.readyState == 4) {
						if (rhttp.status != 200) {
							errorfunc(rhttp.responseText, rhttp.status, rhttp.statusText);
						} else {
							successfunc(rhttp.responseText);
						}
					}
				};
            	rhttp.send(null);
			}
			catch(e){}
			finally{}
		}

        return;
	}

	if (etype == 'JSON') {
		if (edata && (typeof(edata) === 'object')) { edata = JSON.stringify(edata); }
		if (typeof jQuery != 'undefined') {
        	$.ajax({
            	type: 'POST',
            	url: eurl,
            	data: edata,
            	dataType: 'json',
            	contentType: 'application/json; charset=utf-8',
            	success: successfunc,
				error: errorfunc
			});
			return;
		} else {
			var rhttp = newStdAjax();
			try {
            	rhttp.open('POST', eurl, true);
            	rhttp.setRequestHeader('Content-Type', 'application/json');
            	rhttp.setRequestHeader('charset', 'utf-8');
				rhttp.onreadystatechange = function () {
					if (rhttp.readyState == 4) {
						if (rhttp.status != 200) {
							errorfunc(rhttp.responseText, rhttp.status, rhttp.statusText);
						} else {
							successfunc(rhttp.responseText);
						}
					}
				};
            	rhttp.send(edata);
			}
			catch(e){}
			finally{}
		}
	}

	if (etype == 'POST') {
		if (typeof jQuery != 'undefined') {
			if (edata && (typeof (edata) === 'object')) { edata = $.param(edata); }
        	$.ajax({
            	type: 'POST',
            	url: eurl,
            	data: edata,
            	dataType: 'html',
            	contentType: 'application/x-www-form-urlencoded; charset=utf-8',
            	success: successfunc,
				error: errorfunc
			});
			return;
		} else {
			if (edata && (typeof (edata) === 'object')) {
				var sdata = '';
				for (k in edata) { sdata += k+'='+edata[k]+'&'; }
				sdata += 'rnd='+Math.random();
				edata = sdata;
			}
			var rhttp = newStdAjax();
			try {
           		rhttp.open('POST', eurl, true);
           		rhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
           		rhttp.setRequestHeader('charset', 'utf-8');
				rhttp.onreadystatechange = function () {
					if (rhttp.readyState == 4) {
						if (rhttp.status != 200) {
							errorfunc(rhttp.responseText, rhttp.status, rhttp.statusText);
						} else {
							successfunc(rhttp.responseText);
						}
					}
				};
           		rhttp.send(edata);
			}
			catch(e){}
			finally{}
		}
	}
}




