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

	function mantener(idItem, cantidad){
		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 ;
		}		
		paginaReq.open('GET', '?add='+idItem+'&cant='+cantidad, true) ;
		paginaReq.send(null) ;
	}

	function subtotal(precio, cantidad, subTotal, idItem, simbolo){
		var inputSub = document.getElementById(subTotal) ;
		var suma = cantidad * precio ;
		inputSub.value = simbolo + ' ' + suma.toFixed(2) ;
		mantener(idItem, cantidad) ;
	}
	
	
	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(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 ' + existencias + ' existencias.</span><br /><br />' ;
			contenido += '<span class="arialRed">¿Qu&eacute; desea hacer?</span><br />' ;
			
			contenido += '<ul style="padding-left: 20px">' ;
			contenido += '<li><a class="arialContent" style="color: #2951ad; text-decoration: none; cursor: pointer">Ordenar ' + existencias + ' ' + producto + '.</a></li><br/>' ;
			contenido += '<li><a class="arialContent" href="?spr='+ idProducto +'" style="color: #2951ad; text-decoration: none">Ordenar ' + existencias + ' ' + producto + ' e ir a la lista de espera para solicitar las dem&aacute;s existencias.</a></li><br/>' ;
			contenido += '<li><a class="arialContent" href="?cart=1&prd='+ idProducto +'&action=del" style="color: #2951ad; text-decoration: none">Quitar ' + producto + ' del carrito de compras.</a></li>' ;
			contenido += '</ul>' ;
			
		
			var div = document.createElement('div') ;
			div.id = 'insuficiente' ;
		//	div.style.height = '48px' ;
			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.style.color = '#ff0000' ;
			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')) ;		
		}
	}
	
	function sumarSubtotales(simbolo){
		
		var baseSubstr = simbolo.length ;
		inputs = document.getElementsByTagName("input") ;
		var total=0 ;
		for(var i = 0 ; i < inputs.length ; i++){
			if(inputs[i].id.substr(0,8) == 'subtotal'){	
			
			//	alert(inputs[i].value) ;
			
				var valueInput = inputs[i].value.substr(baseSubstr, (inputs[i].value.length - 1)) ;
				total += parseFloat(valueInput) ;
			}
		}
		var itemsAmount = document.getElementById('itemsAmount') ;
		itemsAmount.value = simbolo + ' ' + 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) ;	
		}		
	}
	
	function inputSelect(){	
		var countries	= document.getElementById('countries')	;
		var state	= document.getElementById('state')	;
		
		if(countries.value == 'United States'){
			state.name			= 'unknow'	;
			state.style.display		= 'none'		;
		}else{
			state.name			= 'state'		;
			state.style.display		= 'block'		;
		}
	}
