// Provide the XMLHttpRequest class for IE 5.x-6.x:
// Other browsers (including IE 7.x-8.x) ignore this when XMLHttpRequest is predefined
if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
	try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
	alert("Advertencia: Tu explorador no soporta Ajax.");
};

//	url	- The url of the serverside script that is to be called. Append all the arguments to this url - eg. 'get_data.php?id=5&car=benz'
//	callback - Function that must be called once the data is ready.
//	format - The return type for this function. Could be 'xml' or 'text'
function load(mode,format,params,callback,warn){
	mode = mode.toUpperCase();
	format = format.toLowerCase();
	if(xhttp.readyState>0 && xhttp.readyState<4){
		if(warn) setLabel('Por favor espere a que se complete la operación anterior.');
		return;
	}
	if(mode == 'GET'){
		var url = '../ajax.php?'+params;
		var data = null;
	}else{
		var url = '../ajax.php';
		var data = params;
	}
	xhttp.open(mode, url, true);
	if(mode == 'POST'){
		xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhttp.setRequestHeader("Content-length", params.length);
		xhttp.setRequestHeader("Connection", "close");
	}
	xhttp.onreadystatechange = function(){
		if(xhttp.readyState == 4){
			if(xhttp.status == 200){
				var result = '';
				if(xhttp.responseText) result = xhttp.responseText;
				// Give the data to the callback function.
				if(callback) callback(result);
			}else{
				// An error occured
				if(warn) setLabel('Error de conexión. (status='+xhttp.status+')');
			}
		}
	}
	xhttp.send(data);
}

/* Agregar producto al carrito */
function agregarProd(codarti,endetalle){
	// Determinar cantidad
	if(document.getElementById('cant'+codarti)){
		var cant = parseInt(document.getElementById('cant'+codarti).value);
		if(isNaN(cant) || cant==0){
			alert("Ingrese la cantidad que desea agregar a su carrito.");
			return;
		}
	}else{
		var cant = 1;
	}
	// Mostrar label
	if(!endetalle) imgsrc = 'imagenes/loading.gif'; else imgsrc = '../ventas/imagenes/loading.gif';
	if(document.getElementById('status'+codarti)){
	   document.getElementById('status'+codarti).innerHTML = '<img src="'+imgsrc+'" alt="..." width="16" height="16" />';
	}
	// Ejecutar request
	var params = 'action=agregarprod&codarti='+codarti+'&cant='+cant;
	load('GET', 'text', params, resAgregarProd, true);
}
function resAgregarProd(){
	var response = xhttp.responseText;
	if(response.indexOf('|') > 0){
		// Producto agregado OK
		response = response.split('|');
		var codarti = parseInt(response[0]);
		var infocarro = response[1];
		document.getElementById('infocarro').innerHTML = infocarro;
		if(document.getElementById('status'+codarti)){
			document.getElementById('status'+codarti).innerHTML = '<strong>Agregado</strong>';
		}else{
			setLabel("Producto agregado con éxito a su carrito");
		}
	}else{
		// Error agregando producto: Alertar
		setLabel(response);
	}
}
function setLabel(texto,cerrar){
	if(texto.indexOf('Warning:') > 0) texto = 'Error de conexión: Por favor inténtelo nuevamente';
	alert(texto);
}
/* Init HTTP request */
var xhttp = false;
xhttp = new XMLHttpRequest();
