// Change the next variable to fit your needings
var loading_text = 'Cargando... por favor espera';

// Call this function when you need to start callbacks
// url		= Path to script to run
// target	= HTML container to update
// ffields	= null for GET method or "form_field1+form_field2+...+form_fieldN" for POST method
//		  All fields will be grabed using document.GetElementById()
function load_content(url, target, ffields) {
	// Create the xml object for every request
	var o_xml = false;
	var method = false;
	var post_fields = false;
	if (window.XMLHttpRequest) { // It works for mozilla, safari, etc.
		o_xml = new XMLHttpRequest();
	} else if (window.ActiveXObject){ // Let's figure it out on IExplorer
		try {
			o_xml = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {  // Older versions
				o_xml = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){ alert(e); }
		}
	}
	if(o_xml === false) {
		return false;	// Browser doesn't support ajax or we didn't implement the right method
	}
	if(!ffields) {
		method = 'GET';
		post_fields = null;
	} else {
		method = 'POST';
		// Grab all necessary fields
		if(ffields.indexOf('+')) {
			fields = ffields.split('+');
		} else {
			fields = new Array(ffields);
		}
		post_fields = '';
		sep = '';
		for(i = 0; i < fields.length; i++) {
			curfield = document.getElementById(fields[i]);
			if(curfield == null) {
				continue;
			}
			if(curfield.type == 'select') {
				selIndex = curfield.selectedIndex;
				curvalue = curfield.options[selIndex].value;
			} else if(curfield.type == 'checkbox') {
				curvalue = (curfield.checked == true) ? curfield.value : '';
			} else {
				curvalue = curfield.value;
			}
			// curvalue = (curfield.type == 'select') ?  : curfield.value;
			post_fields += sep + fields[i] + "=" + encodeURIComponent(curvalue);
			sep = '&';
		}
		// Send fields by post
	}
	if(loading_text != '') {
		document.getElementById(target).innerHTML = loading_text;
	}
	o_xml.onreadystatechange=function(){
		load_response(o_xml, target);
	}
	o_xml.open(method, url, true) // asignamos los métodos open y send
	o_xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	o_xml.send(post_fields);
}

function load_response(o_xml, target) {
	if (o_xml.readyState == 4) {	// Process completed
		if(o_xml.status >= 200 ||  o_xml.status <= 300) {	// 200 to 300 are right values
			// document.getElementById(target).innerHTML = o_xml.responseText;
			// The above lines works on real browsers but may fail on IE, so let's look a workaround
			var mainTarget = document.getElementById(target);
			var newContent = o_xml.responseText;
			try {
				// Real browsers will work fine with this single line
				mainTarget.innerHTML = newContent;
			}
			catch (e) {
				// Lets add another element to fix problems on IE
				var wrapDiv = document.createElement('div');
				mainTarget.innerHTML = '';
				wrapDiv.innerHTML = newContent;
				mainTarget.appendChild(wrapDiv);
				
			}
		} else {
			document.getElementById(target).innerHTML = 'Error #' + o_xml.status + '; por favor intenta nuevamente';
		}
	}
}
