	// JavaScript Document
var id_intervalo = 0;
var avanzando = true;
var id_carrusel = "";
var temp_borrar = "";
var anadido = false;
var direccion = "right";
var velocidad = 3;

var temp_val = 0;
var img1, img2, img3, img4, img5, img6, img7, img8;



function empezar(desplazar_inicio){
	var galeria = document.getElementById("carrusel");
	var imagenes = galeria.getElementsByTagName("img");
	var margen = 7;
	var ancho_ = 0;
	var ancho_temp = 0;
	var img;
	var i = 0;
	var cantidad = imagenes.length / 2;
	
	
	 img1 = parseInt(document.getElementById("imagen1").width); 	  
	 img2 = parseInt(document.getElementById("imagen2").width); 
	 img3 = parseInt(document.getElementById("imagen3").width); 
	 img4 = parseInt(document.getElementById("imagen4").width); 
	 img5 = parseInt(document.getElementById("imagen5").width); 
	 img6 = parseInt(document.getElementById("imagen6").width); 
	 img7 = parseInt(document.getElementById("imagen7").width);
	 img8 = parseInt(document.getElementById("imagen8").width); 
	
	temp_val += img1;
	temp_val += img2;
	temp_val += img3;
	temp_val += img4;
	temp_val += img5;
	temp_val += img6;
	temp_val += img7;
	temp_val += img8;	
	temp_val += (cantidad * margen); 							
	
	var left  = temp_val - 470;	
	
	if(desplazar_inicio){
		galeria.style.width = temp_val+"px";
		galeria.style.left = "-"+left+"px";			
	}
			
	return temp_val;
}


function alternar(oImagen, inicial,volver){
	if(oImagen.src == inicial){
		oImagen.src = volver;	
	}else{
		oImagen.src = inicial;	
	}
	
}

function continuar(){
	var ancho = empezar(false);
	if(direccion=="right"){
		direccion = "left";	
	}else{
		direccion = "right";
	}
	move_start(ancho,id_carrusel);
	
}


function move_start(ancho_total,id_carr){	
	avanzando = true;
	id_intervalo = setInterval("mover('"+ancho_total+"',true,'"+id_carr+"')",100);	
}


function move_stop(ancho_total){
	avanzando = false;
	if(direccion=="right"){
		direccion = "left";	
	}else{
		direccion = "right";
	}
	setTimeout("clearInterval("+id_intervalo+")",1);		
}

function enlaces(){
	var enlaces = document.getElementsByTagName("a");
	for(var i=0; i < enlaces.length; i++){
		enlaces[i].onmouseover = function(enlace){
		this.style.cursor = "";
	
		}
		enlaces[i].onmouseout = function(enlace){
		this.style.cursor = "pointer";		
		
		}
		
		
	}
	
}

function insertar_script(script){
	 var oScript = document.createElement('script');
	 oScript.src = script;
	 document.body.appendChild(oScript);	
}

function cargar_poblaciones(oSelect,poblacion){
if(oSelect){
	var sel = oSelect;	
	insertar_script("inc/poblaciones_js.php?id_provincia="+sel.value+"&id_poblacion="+poblacion);	
}
}


function deco_char(html){
	html = replace(html,'&gt;','>');
		html = replace(html,'&lt;','<');
		html = replace(html,'&quot;','"');
		html = replace(html,'&prime;',"'");
		html = replace(html,'&atilde;',"á");
		html = replace(html,'&Atilde;',"Á");
		html = replace(html,'&etilde;',"é");
		html = replace(html,'&Etilde;',"É");
		html = replace(html,'&icute;',"í");
		html = replace(html,'&Itilde;',"Í");
		html = replace(html,'&otilde;',"ó");
		html = replace(html,'&Otilde;',"Ó");
		html = replace(html,'&utilde;',"ú");
		html = replace(html,'&Utilde;',"Ú");
		
		return html;
}

function establecer_poblaciones(valores,id_select){	
	var oSelect = document.getElementById(id_select);	
	if(oSelect){ oSelect.innerHTML = deco_char(valores); 
				oSelect.onchange();
				}
}


function validar(){
	var ids = document.getElementById("tallas_validar");
	var val = true;
	var elem = "";
	
	
	
	if(ids){
		var aIds = ids.value.split("|");

		for(var i = 0; i < aIds.length; i++){
		
			elem = document.getElementById("talla_nombre_"+aIds[i]);			
			if(elem){
				if(elem.value == ""){
					alert("Debes especificar la talla");
					elem.style.background="#ff6699";
					elem.focus();
					val = false;
					break;
				}else{
					elem.style.background="#ffffff";
				}
				
			}
		}
	}
	
	
	
	return val;
}


function otra_poblacion(oSel){
	
	var capa = document.getElementById("o_loc");
	
	if(oSel && capa){
		if(oSel.value == 0){ capa.style.display = "block";}
		else				{capa.style.display = "none"; }
	}
}

function mover(ancho_total, mover,id_carrusel){
	
	var galeria = document.getElementById("carrusel"+id_carrusel);
	var distancia = 0;
	
	
		if(galeria){
			if(galeria.style.left){
				distancia = parseInt(galeria.style.left);
			}else{
				distancia = 0;	
				
			}
			
			if(mover){	
				
				if(direccion == "right"){
					galeria.style.left = distancia + velocidad + "px";
				}else{
					galeria.style.left = distancia - velocidad + "px";
				}
				
				if(distancia >= - 3){
					if(direccion == "right"){
						move_stop(ancho_total);	
						direccion = "left";
						move_start(ancho_total,id_carrusel);
					}
				
				}
				
				
				
				
				if(distancia == (ancho_total - 500)*(-1)){
					if(direccion == "left"){
						move_stop(ancho_total);	
						direccion = "right";
						move_start(ancho_total,id_carrusel);
					}
				}
				
				
				
				if(distancia >= 500){ empezar(true); }
				
				
				if(distancia >= ancho_total - 500){
				}
			}else{
				galeria.style.left = distancia + "px"; 	
			}
	}
}



function insertar_css(css){
	var oLink = document.createElement('link');
	 oLink.rel = "stylesheet";
	 oLink.type = "text/css";
	 oLink.href = css;
	 document.getElementById("head").appendChild(oLink);
}



function replace(texto,s1,s2){
	return texto.split(s1).join(s2);
}


function limpiar_lazo(flash,coleccion,talla,referencia_a,referencia_b,referencia_n, precio, id_color){
	delCookie("lazo_a");
	delCookie("lazo_b");
	delCookie("lazo_n");
	delCookie("referencia_a");
	delCookie("referencia_b");
	delCookie("referencia_n");
	insertar_script('js/disena/vaciar_lazos.php?id_color='+id_color+'&precio='+precio+'&coleccion='+coleccion+'&talla='+talla+'&referencia_a='+referencia_a+'&referencia_b='+referencia_b+'&referencia_n='+referencia_n);		
	return true;
	//textura('','',flash,'','','','','','');
}


function eliminar_cookies(){
	delCookie("lazo_a");
	delCookie("lazo_b");
	delCookie("lazo_n");
	delCookie("referencia_a");
	delCookie("referencia_b");
	delCookie("referencia_n");
}


function eliminar_cookies(){
	delCookie("lazo_a");
	delCookie("lazo_b");
	delCookie("lazo_n");
	delCookie("referencia_a");
	delCookie("referencia_b");
	delCookie("referencia_n");
}

// JavaScript Document
// CREAR UNA COOKIE CON TIEMPO DE EXPIRACION
function guardar_cookie(name, value, expires, path, domain, secure)
{	
  document.cookie = name+"="+escape(value)+(expires ? "; expires="+expires.toGMTString() : "")+ (path    ? "; path="   +path   : "")+ (domain  ? "; domain=" +domain : "")+    (secure  ? "; secure" : "");
}


// CREAR UNA COOKIE CON LIFETIME
function setCookieLT(name, value, lifetime, path, domain, secure)
{
  if (lifetime)
    lifetime = new Date(Date.parse(new Date())+lifetime*1000);
  setCookie(name, value, lifetime, path, domain, secure);
}

// OBTIENE EL VALOR DE UNA COOKIE
function recuperar_cookie(name)
{
  var cookie = " "+document.cookie;
  var offset = cookie.indexOf(" "+name+"=");
  var end;
  
  if (offset == -1) return undefined;

  offset += name.length+2;
  end     = cookie.indexOf(";", offset)

  if (end == -1) end = cookie.length;

  return unescape(cookie.substring(offset, end));
}

// BORRA UNA COOKIE
function delCookie(name, path, domain)
{
  if (recuperar_cookie(name))
  {
    var date = new Date("January 01, 2000 00:00:01");
    guardar_cookie(name, "", date, path, domain);
  }
}



function enviar_formulario(id){
	if(validar()){
		var form = document.getElementById(id);
		if(form){
			form.submit();	
		}
	}
}


function marcar_sombrerera(id_input, enlace){
	var input = document.getElementById(id_input);
	if(enlace){
		if(input.value == ""){
			enlace.className = "marcar selec";	
			input.value = "si";	
		}else{
			enlace.className = "marcar";
			input.value = "";
		}
	}
}



function marcar_motivo(id_input,valor, enlace, cont){
	var input = document.getElementById(id_input);
	var lista = document.getElementById("motivos_"+cont);
	var desactivar = false;
	
	if(input.value != ""){ desactivar = true; }
	
	if(lista){
		var enlaces = lista.getElementsByTagName("a");
		
		for(var i=0; i < enlaces.length; i++){
			if(enlaces[i].className == "marcar_motivo selec"){
				enlaces[i].className = "marcar_motivo";	
				input.value = "";
			}
					 
		}
		
	}
	
	if(enlace){
		if(input.value == ""){
			if(!desactivar){
				enlace.className = "marcar_motivo selec";	
				input.value = valor;	
			}else{
				enlace.className = "marcar_motivo";	
			}
		}else{
			enlace.className = "marcar_motivo";
			input.value = "";
		}
	}	
	
}




function set_forma_pago(forma, enlace){
	var input = document.getElementById("forma_pago");	
	input.setAttribute("value", forma);
	
	var tabla = document.getElementById("metodo");
	var enlaces = tabla.getElementsByTagName("a");
	for(var i=0; i < enlaces.length; i++){ enlaces[i].className = "marcar" ; }
	
	
	if(enlace) { enlace.className += " selec " ; }
}


function validar_formulario_resumen(id_form){
	var input = document.getElementById("forma_pago");	
	if(input.value!="") {
		if(input.value == "tarjeta"){
			alert('A continuación se cargará la pasarela de pago del banco.\n\nImportante: debes navegar a través de los enlaces que ofrezca la pasarela del banco. No pulses \"Atrás\" en el navegador o la pasarela devolverá el error: \"Número de pedido repetido\".\n\nEn ese caso y siempre que recibas este tipo de error, vuelve a nuestra página mediante los enlaces de la pasarela y vacía la cesta y comienza tu compra de nuevo (esto genera un nuevo número de pedido).');	
		}
		enviar_formulario(id_form);
		return true;	
	}else{
		alert('Debes escoger un método de pago para poder realizar el pedido');
		return false;	
	}
}
