/*****	Funciones utilizadas en el carrito de compras		********/

	function mantener(idItem, cantidad, extra, preferencias){
		var paginaReq = false
		if(window.XMLHttpRequest) {
			paginaReq = new XMLHttpRequest()
		}else if (window.ActiveXObject){ 
			try {
			paginaReq = new ActiveXObject("Msxml2.XMLHTTP")
			} catch (e){
				try{
				paginaReq = new ActiveXObject("Microsoft.XMLHTTP")
				} catch (e){}
			}
		}
		else{
			return false ;
		}
		
		if(extra == 0){
			paginaReq.open('GET', '?add='+idItem+'&cant='+cantidad+'&preferencias='+preferencias, true) ;
		}else{
			paginaReq.open('GET', '?add='+idItem+'&cant='+cantidad+'&preferencias='+preferencias, true) ;
		}
		
		
		paginaReq.send(null) ;
	}

	function subtotal(precio, cantidad, tdSubtotal, idItem, extra){
		
		var inputSub 	= document.getElementById(tdSubtotal) ;
		var suma = (cantidad) * precio;
			
		inputSub.innerHTML = '$' + suma.toFixed(2) ;
				
		mantener(idItem, cantidad, extra, document.getElementById('_preferencias_'+idItem).value) ;
		sumarSubtotales() ;
	}
	
	function subtotalPromo(precio, cantidad, tdSubtotal, precioNormal, limite, mensajePromo){
		var inputSub = document.getElementById(tdSubtotal) ;
		var suma=0;
		if(limite>0){
			if(limite>cantidad){
				suma = (cantidad) * precio;	
				document.getElementById(mensajePromo).innerHTML="";
			}else{
				suma = (limite) * precio+(cantidad-limite)*precioNormal;
				document.getElementById(mensajePromo).innerHTML=cantidad-limite +" serán facturados a sin Promoción*"
			}
				
		}else{
			suma = (cantidad) * precio;
			document.getElementById(mensajePromo).innerHTML="";
		}
		
		inputSub.innerHTML = '$' + suma.toFixed(2) ;
		sumarSubtotales() ;
	}
	
	function sumarSubtotales(){
		
		var tablaCarrito = document.getElementById('tablaCarrito') ;
		var tds = tablaCarrito.getElementsByTagName("td") ;
		var costoEnvio, totalTotal;
		
		var total = 0 ;
		for(var i = 0 ; i < tds.length ; i++){
			if(tds[i].id.substr(0,8) == 'subtotal'){
				var valorEnElTd = tds[i].innerHTML.split('$')[1] ; // para eliminar el simbolo $
				total += parseFloat(valorEnElTd) ;
			}
		}
		
		var totalProductos = document.getElementById('totalProductos') ;
		var inputEnv 	= document.getElementById('totalEnvio') ;
		var inputTot 	= document.getElementById('totalTotal') ;
		
		if(total < 25){
			costoEnvio = 3;
			
		}else{
			costoEnvio = 0;
		}
		
		totalTotal = costoEnvio + total;
		
		inputEnv.innerHTML = '$' + costoEnvio.toFixed(2) ;
		inputTot.innerHTML  = '$' + totalTotal.toFixed(2) ;
		
		
		
		totalProductos.innerHTML = '$' + total.toFixed(2) ;
	}
	
	function numeros(control){
		if (isNaN(parseInt(control.value))){
			control.value = '' ;
		}else if(control.value < 1){
			control.value = 1 ;
		}else{
			control.value = parseInt(control.value) ;	
		}		
	}	
	
	/* copiadas desde tv ofert*/
	
	function getLeft(ele){  
	     if (ele.offsetParent){ return ele.offsetLeft + getLeft(ele.offsetParent) ; }
	     else{ return ele.offsetLeft ; } 
	}  

	function getTop(ele){
		if (ele.offsetParent){ return (ele.offsetTop + getTop(ele.offsetParent)) ;  }
		else{ return (ele.offsetTop) ; }
	}	
	
	function comprobarExistencias(inputCantidad, existencias, idProducto, producto){
	
		var cantidad = inputCantidad.value ;
		
		if(existencias != -1){
			if(cantidad > existencias){
				inputCantidad.value = existencias ;
				//	alert('Lo sentimos, en este momento no contamos con suficientes existencias para su pedido, pero tenemos a su disposición '+ existencias +' ' + producto) ;			
				
				if(!document.getElementById('insuficiente')){
				
					var contenido = '<span class="arialContent" style="font-weight: normal;">Lo sentimos, por el momento no tenemos las existencias que necesita de </span> ' + producto + '; ' ;
					contenido += '<span class="arialContent" style="font-weight: normal">pero tenemos a su disposici&oacute;n <strong>' + existencias + '</strong> existencias.</span><br /><br />' ;
									
				
					var div = document.createElement('div') ;
					div.id = 'insuficiente' ;
					div.style.width = '210px' ;
					div.style.position = 'absolute' ;
					div.style.textAlign = 'left' ;
					//div.style.padding = '16px' ;
					div.style.top = (getTop(inputCantidad) + 16) + 'px' ;
					div.style.left = (getLeft(inputCantidad) + 30) + 'px' ;
					//div.style.border = '1px #ff0000 solid' ;
					//div.style.backgroundColor = '#ffffff' ;
					//div.style.fontWeight = 'bold' ;
					//div.className = 'arialRed' ;
					div.style.opacity = '0.97' ;
					div.innerHTML = contenido ;
					
					var padre = inputCantidad.parentNode ;
					padre.appendChild(div) ;
				}
				
			}else if(document.getElementById('insuficiente') && cantidad <= existencias){
				var padre = document.getElementById('insuficiente').parentNode ;
				padre.removeChild(document.getElementById('insuficiente')) ;		
			}
		}
	}
	
	function quitarDiv(){
		if(document.getElementById('insuficiente')){
		
			var padre = document.getElementById('insuficiente').parentNode ;
			padre.removeChild(document.getElementById('insuficiente')) ;		
		}
	}
	
	
	/*Haciendo un poco de ajax*/
	
	function getXMLHTTPReques(){
		try{
			req = new XMLHttpRequest();
		}catch(err1){
			
			try{
				req = new ActiveXObject("Msxm12.XMLHTTP");
			}catch(err2){
			
				try{
					req = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(err3){
					req = false ;
				}
			}
		}
		return req;
	}
	
	
	
	
	function prodVeriExist(idDiv,frm){
		var req = getXMLHTTPReques() ;
		var comprobarexist;
		
		if(req){
			
			var div = document.getElementById(idDiv) ;
			var myRand = parseInt(Math.random()*9999999999999) ;
			
			
			req.onreadystatechange = function(){
				if(req.readyState == 4){
				
					if(req.responseText.length > 0){
					
						div.innerHTML = req.responseText ;
						
					}else{
						
						frm.submit();
					}
					
				}
				else{
					
					div.innerHTML = '<img src="images/ajax-loader.gif" style="padding-left:10px;padding-right:10px">' ;
					
				}
			}
			req.open("GET", "miAsincrono.php?rand="+myRand, true) ;
			req.send(null) ;
			return false;
			
		}
	
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
