function creaAjax(){//CREA EL OBJETO AJAX
  var objetoAjax=false;
  try {
   /*Para navegadores distintos a internet explorer*/
   objetoAjax = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
     /*Para explorer*/
     objetoAjax = new ActiveXObject("Microsoft.XMLHTTP");
     } 
     catch (E) {
     objetoAjax = false;
   }
  }

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

function redondear(cantidad, decimales) {
	var cantidad = parseFloat(cantidad);
	var decimales = parseFloat(decimales);
	decimales = (!decimales ? 2 : decimales);
	return Math.round(cantidad * Math.pow(10, decimales)) / Math.pow(10, decimales);
} 



function sum_incremento(incrementos){
	
	var total=0;
	var i;
	var campo;
	var nom_activos="";
	
	//alert ("dentro del sumador de incrementos que son: " + incrementos);
	
	for(i=1;i<=incrementos;i++){
		campo='sumatorio'+i;
		
		if(document.getElementById(campo).checked==true){
			
			var valor=document.getElementById(campo).value;
			var partes=valor.split("-");//Divido valor y nombre de incremento
			
			total=parseFloat(total) + parseFloat(partes[1]);//Sumo las cantidades
			nom_activos+=partes[0]+ ';';
		}
	}
	
	//alert (total);

	document.getElementById('extraspro').value=nom_activos;//Alceno los incrementos marcados en extraspro
	//alert (document.getElementById('extraspro').value);			
	return total;		

}


function envia_valores(id,url,vinc)
{
	
	var ajax=creaAjax();
	//var preciound = document.getElementById('precio_pro_total');  //precio unidad 	
	var precioto = document.getElementById('precio_pro_total');		//precio total
	var preciouni = document.getElementById('precio_uni');		//precio unitario	
   	var cantidad = document.getElementById('cantidad').value;
	var datos;//datos que se devuelven
	var resultado; //resultado que se obtiene (array)
	var valores="";
	//alert (cantidad);
	
	// Coloco el mensaje "Cargando..." en la capa
	precioto.innerHTML="Calculando precio, espere...<img src='../imagenes/loading.gif' alt='cargando' />";
	preciouni.innerHTML="";
	// Abro la conexin, envo cabeceras correspondientes al uso de POST y envo los datos con el mtodo send del objeto AJAX
	ajax.open("POST",url, true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	///VALRES A ENVIAR
	valores = 'id='+ encodeURI(id) +'&cantidad='+encodeURI(cantidad)+'&incremento='+encodeURI(vinc);
	
	
	
	ajax.send(valores);

	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			// Respuesta recibida. Coloco el texto plano en la capa correspondiente
			var total=ajax.responseText;
			total=redondear(total, '2');
			precioto.innerHTML='<span class="precio_grande">Total: <span class="color">' + total + ' euros</span></span>';

			preciouni.innerHTML=redondear(parseFloat(total)/cantidad, '2') + ' &euro;';
			
			//COLOCO LOS VALORES EN SUS RESPECTIVOS CAMBIOS
			document.getElementById('precio').value=total;
			document.getElementById('preciouni').value=redondear(parseFloat(total)/cantidad, '2');
			//resultado=datos.split("-");
			//precioto.innerHTML=resultado[0];
		}
	}
}




function calculo(id,url,minimo,t_incrementos){
	
	var cantidad = document.getElementById('cantidad').value;
	var vinc=0;//cantidad del incremento
	
	//alert ("cantidad: " + cantidad);
	
	if(cantidad>minimo-1){
		
		if(t_incrementos!=0)//si existen incrementos
			vinc=sum_incremento(t_incrementos);	
		
		envia_valores(id,url,vinc);
	
	}else{
		alert ("La cantidad min. del producto es: " + minimo);
	}
}

function impresa()//genera los datos para enviarlos a la version impresa
{

	var url='vimpresa.php?';
	url+='idpro=' + document.getElementById('idpro').value;
	url+='&cantidad=' + document.getElementById('cantidad').value;
	url+='&precio=' + document.getElementById('precio').value;
	url+='&preciouni=' + document.getElementById('preciouni').value;
	url+='&nombre=' + document.getElementById('nombre').value;
	url+='&email=' + document.getElementById('email').value;
	url+='&telefono=' + document.getElementById('telefono').value;		
	url+='&extraspro=' + document.getElementById('extraspro').value;						
	
	//alert(url);
	document.getElementById('enlacev').href=url;
}

function activar(estado)//activa los botones
{
	//--> 1 activa
	//--> 0 desactiva
	if(estado==1){	
		if(document.getElementById('cantidad').value!=""){
			document.getElementById('act_bot').innerHTML= '<input name="enviar" type="image" id="enviar" src="imagenes/botenviar.gif" />'; 
			document.getElementById('print').innerHTML= '<a href="#" id="enlacev" onclick="impresa();" target="_blank"><img src="imagenes/ico-print.gif" width="19" height="23" border="0" alt="zoom imagen"  /></a>'; 
		}
	}else{
		document.getElementById('act_bot').innerHTML= '<img src="imagenes/botenviar2.gif" name="bot_envio" width="47" height="20" id="bot_envio" alt="enviar formulario" />'; 
		document.getElementById('print').innerHTML= '<img src="imagenes/ico-print2.gif" width="19" height="23" alt="imprimir presupuestoerminos" />'; 
	
	}
}


function validar(){

	var nombre = document.getElementById('nombre');
	var email = document.getElementById('email');	
	var telefono = document.getElementById('telefono');	
	var cantidad = document.getElementById('cantidad');		
	var terminos = document.getElementById('terminos_ch');		

	if(nombre.value=="" || nombre.value == undefined || email.value=="" || email.value == undefined || telefono.value=="" || telefono.value == undefined || cantidad.value=="" || cantidad.value == undefined || terminos.checked==false){
		//nombre.onfocus;		
		activar('0');
	}else{
		
		activar('1');
	
	}




}

function EvaluateText(cadena, obj, e){//evalua la entrada de numeros
	opc = false;
	tecla = (document.all) ? e.keyCode : e.which;
	if (cadena == "%d")
		if (tecla > 47 && tecla < 58)
			opc = true;
	if (cadena == "%f"){
		if (tecla > 47 && tecla < 58)
			opc = true;
		if (obj.value.search("[.*]") == -1 && obj.value.length != 0)
			if (tecla == 46)
				opc = true;
		}
	return opc;
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}