// crea objeto xmlhttprequest
function nuevoAjax(){
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

//logout
function del_item(id,contenedor, cont_total, cont_total_2, session ){
	if(confirm('¿Seguro de borrar este producto?')){
		ajax=nuevoAjax();
		ajax.open("GET", 'del_item.php?id_item='+id,true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
			contenedor.style.display = 'none';
				get_total(session,cont_total, cont_total_2);
			}
		}
		ajax.send(null);
	}
}
function del_marca(id,contenedor){
	if(confirm('¿Seguro de borrar este proveedor?')){
		
		ajax=nuevoAjax();
		ajax.open("GET", 'del_marca.php?id_proveedor='+id,true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
			contenedor.style.display = 'none';
			
			}
		}
		ajax.send(null);
	}
}
function del_tipo(id,contenedor){
	if(confirm('¿Seguro de borrar este tipo de producto?')){
		
		ajax=nuevoAjax();
		ajax.open("GET", 'del_tipo.php?id_tipo='+id,true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
			contenedor.style.display = 'none';
			
			}
		}
		ajax.send(null);
	}
}

function del_sub_tipo(id,contenedor){
	if(confirm('¿Seguro de borrar este sub tipo de producto?')){
		
		ajax=nuevoAjax();
		ajax.open("GET", 'del_sub_tipo.php?id_sub_tipo='+id,true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
			contenedor.style.display = 'none';
			
			}
		}
		ajax.send(null);
	}
}

function borrar(id,contenedor){
	if(confirm('¿Seguro de borrar este producto?')){
		
		ajax=nuevoAjax();
		ajax.open("GET", 'borrar_p.php?id_producto='+id,true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
			contenedor.style.display = 'none';
			
			}
		}
		ajax.send(null);
	}
}
function agrega_carro(id_producto, cantidad){
	ajax=nuevoAjax();
	ajax.open("POST", 'act_total.php',true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			if(ajax.responseText ==''){
				parent.GB_showCenter('Hacer pedido', '../do_pedido.php', 400,470);	
			}
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id_producto="+id_producto+"&cantidad="+cantidad);
	
}

function get_total(session,contenedor, contenedor2){
		ajax=nuevoAjax();
		ajax.open("GET", 'get_total.php?session='+session,true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
					salida = ajax.responseText;
					precio = salida.split('-',2);
					contenedor.innerHTML = precio[0];
					document.getElementById('cantidad_p').innerHTML = precio[1];
					var iva;
					iva = 0;
					iva =parseInt(precio[0])*1.19;
					iva =  Math.round(iva);
					contenedor2.innerHTML = iva;
					
			}
		}
		ajax.send(null);
}

	function valida(){
		if(document.getElementById('str').value.length > 1){
			if(document.getElementById('nombre').checked || document.getElementById('descripcion').checked || document.getElementById('cod_g').checked || document.getElementById('cod_b').checked){
				document.form1.submit();
			}else{
				alert('Debe seleccionar al menos un criterio de búsqueda');
			}
		}else{
			alert('Debe ingresar al menos 2 letras para buscar');
		}
	}
function sub_tipo(id_tipo,contenedor){
	if(id_tipo > 0){
		contenedor.innerHTML = "Buscando...";
		ajax=nuevoAjax();
		ajax.open("GET", 'do_sub_tipo.php?id_tipo='+id_tipo,true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==3) {
				contenedor.innerHTML = "Actualizando...";
			}
			if (ajax.readyState==4) {
				contenedor.innerHTML = ajax.responseText;
			}
		}
		ajax.send(null);
	}
}
function borrar_banner(id,contenedor){
	if(confirm('¿Seguro de borrar este banner?')){
		ajax=nuevoAjax();
		ajax.open("GET", 'del_banner.php?id_banner='+id,true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
			contenedor.style.display = 'none';
			
			}
		}
		ajax.send(null);
	}
}

function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' debería ser una dirección e-mail válida.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' es obligatorio.\n'; }
    } if (errors) alert('Se han encontrado los siguientes errores:\n'+errors);
    document.MM_returnValue = (errors == '');
} }