function validateKeyPress(e,parm)
{
	var key;
	var keychar;
	
	if(window.event || !e.which) // IE
	{
	key = e.keyCode; // para IE
	}
	else if(e) // netscape
	{
	key = e.which;
	}
	else
	{
	return true;
	}
	if (key==13) //Enter
	{
		document.frmtienda.elements["inside"].value='1'; 
		changeAmount(parm);
	}
}

function validateKeyPressCat(e,parm)
{
	var key;
	var keychar;
	
	if(window.event || !e.which) // IE
	{
	key = e.keyCode; // para IE
	}
	else if(e) // netscape
	{
	key = e.which;
	}
	else
	{
	return true;
	}
	if (key==13) //Enter
	{
		changeAmountCat(parm);
	}
}
/* metodo que modifica el seleccionable de provincias si se cambia el metodo de envio
y actualiza los campos ocultos para la forma de envio y el pais */
function SelectProv(id_ship,id_select_prov,id_country,prov){
	if (prov=='undefined')
		prov=-1;
	
	if (id_ship != '') {
		    $.ajax({
		        url: "getProvincesByShip.php",
		        type: "POST",
		        dataType: "xml",		        
		        data: {ship: id_ship,
		        		prov: prov
		        },
		        error: function(req, err, obj) {
		            alert('Error: ' + req.status + err);
		        },
		        success: function(xml) {
		        	
		        	
		        	
		            var res = $("res",xml).text();
		            var text = $("text",xml).text();		            
		            if (res == "OK") {
			        	
		            	$("#"+id_select_prov).text('');
		            	$("#"+id_select_prov).append('<option"></option>');
			        	$("prov", xml).each(function() {

							var id = $("id",this).text();
							var title = $("title",this).text(); 
							
							
							$("#"+id_select_prov).append('<option value="'+id+'">'+title+'</option>');
							
							
			        	})
			        	
			        	$('#'+id_select_prov).removeAttr('disabled');
			        	
			        	$("#pais_1").text ($("country",xml).text());
			        	
			        	document.frmtienda.elements["ship_2"].value = id_ship;
			        	document.frmtienda.elements["pais_2"].value = $("country",xml).text();
			        	
			        	
			        	
			        	document.getElementById(id_select_prov).selectedIndex = $("selected",xml).text();
			        		
		            } else 
		            	alert ("ERROR" + text);
  
		            
		        }
		    });
	}
	
}
/* metodo que comprueba que se se ha checkeado la casilla de las Condiciones legales */
function CheckLegal () {
 		if (!(document.frmtienda.condiciones.checked)){
 			alert("Debe aceptar las Condiciones Legales");
 			return false;
 		}else
 			return true;
 	}
 	
/* Metodo que recarga la pagina pasando los parametros ya seleccionados al modificar las cantidas de los
productos */
 function changeAmount (parm) {
  
	var params = parm.split('_');
		
	product_amount=params[0];
	product_insert = params[1];
	
		
     if ((product_amount!=undefined)&&(product_amount!='')){
		    if (!((product_amount>=0)&&(product_amount<10000))){
		    	alert ("Debes introducir una cantidad válida");
		    	document.location.reload();
		    	
		    }else{
		    	
		    	
		    	var href = "tienda_form.php?amount="+product_amount+"&product_insert="+product_insert+"&ship="+  $("input[@name='shiptype']:checked").val()+'&prov='+$("#provincia_1").val();
		    	//alert(href)
		    	document.location.href= href;
		    	
		        
		    }
     }
    
}

/* Metodo que recarga la pagina pasando los parametros ya seleccionados al modificar las cantidas de los
productos */
 function changeAmount (parm) {
  
	var params = parm.split('_');
		
	product_amount=params[0];
	product_insert = params[1];
	
		
     if ((product_amount!=undefined)&&(product_amount!='')){
		    if (!((product_amount>=0)&&(product_amount<10000))){
		    	alert ("Debes introducir una cantidad válida");
		    	document.location.reload();
		    	
		    }else{
		    	
		    	
		    	var href = "tienda_form.php?amount="+product_amount+"&product_insert="+product_insert+"&ship="+  $("input[@name='shiptype']:checked").val()+'&prov='+$("#provincia_1").val();
		    	//alert(href)
		    	document.location.href= href;
		    	
		        
		    }
     }
    
}

function deshabilitar () {
  document.frmtienda.elements["inside"].value=1; 
}
function habilitar () {
  document.frmtienda.elements["inside"].value=0; 
}


function changeAmountCat (parm) {
  
	var params = parm.split('_');
		
	product_amount=params[0];
	product_insert = params[1];
	cat_id = params[2];
	cat_name = params[3];
	pag = params[4];
     if ((product_amount!=undefined)&&(product_amount!='')){
		    if (!((product_amount>=0)&&(product_amount<10000))){
		    	alert ("Debes introducir una cantidad válida");
		    	document.location.reload();
		    	
		    }else{
		    	
		    	
		    	var href = "tienda_categoria.php?amount="+product_amount+"&product_insert="+product_insert+"&ship="+"&cat_id="+cat_id+"&cat_name="+cat_name+"&pag="+pag;
		    	
		    	document.location.href= href;
		    	
		        
		    }
     }
    
}

/* Metodo que al cambiar la forma de envio calcula el coste total de la compra */
function changeShipment () {		 
	
	
	
	/* recuperamos los valores de los campos*/
	var shipvalue =   $("input[@name='shiptype']:checked").val();	
	var price_tot =  $("input[@name='subtotal']").val();	
	var sin_gastos =  $("input[@name='sin_gastos']").val();	
	var weight =  $("input[@name='weight']").val();	
	var euro =  $("input[@name='euro']").val();	
	
	var id1;
	var title1;
	var price1;
		
	
	if (shipvalue!=null && sin_gastos==false){
	$.ajax({
       url: "GetShipmentExpenses.php",
       type: "POST",
       dataType: "xml",
       data: {
           ship1: shipvalue,
           price_tot: price_tot,
           weight: weight
           
       },
        error: function(req, err, obj) {
        	
            alert ("Error " + err +" - "+ req.status);
        },
        success: function (xml) {
			        	
			
			var total_withshipment_formatted = $(xml).find("total_withshipment_formatted").text();
			
			//Sumamos al total los gatos de envío
		    $("#total").text (total_withshipment_formatted);
		    
		    document.frmtienda.elements["total2"].value = total_withshipment_formatted;
		    
			/* se ocultan los precios de todos los gastos de envio menos el seleccionado*/ 
			for (i=0;i<document.frmtienda.shiptype.length;i++){ 
		        if (document.frmtienda.shiptype[i].checked) {
		 	        $("#shiptitle_"+i).css({visibility:"visible"});	
		 
		        }else{
		        	$("#shiptitle_"+i).css({visibility:"hidden"});	
		        }
		        
		     }
		     
			
			


		    
        }
        
        
        
    });
	}
	
	 	
}