// JavaScript Document
function video(archivo, ancho, alto){
	var newvideo=window.open("","video","");
	if (newvideo) newvideo.close();
	var h=screen.availHeight;
	var w=screen.availWidth;
	var t=(h/2)-(alto/2);
	var l=(w/2)-(ancho/2);
	//if (!newvideo){
		var newvideo=window.open("../videos/"+archivo+".php", "video", "top="+t+", left="+l+", height="+alto+", width="+ancho+"");
		if (newvideo) newvideo.focus();
	//}
}
function carro_anadir(){
	if (!arguments){
		return;
	}
	var id=arguments[0];
	if (!id){
		alert("Debe seleccionar un producto");
		return;
	}
	var total=arguments.length;
	var errores="";
	var valido=true;
	var valor=new Array();
	var adicional=new Array();
	var dato="";
	var contador=0;
	var opcion="";
	for (i=1; i<total; i+=3){
		adicional[contador]=arguments[i];
		valor[contador]=document.getElementById(arguments[i+1]).value;
		if (!valor[contador]){
			errores+=arguments[i+2]+"\n";
			valido=false;
		}
		contador++;
	}
	if (!valido){
		alert(errores);
	} else {
		dato=id;
		for (i=0; i<contador; i++){
			dato+="/"+adicional[i]+":"+valor[i];
		}
		document.temp.dato.value=dato;
		document.temp.action="../includes/carro_anadir.php";
		document.temp.submit();
	}
}
function enviar(cas){
	location.href="mailto:"+cas+"@hemel.cl";
}
function recalcular(formulario){
	var dato=document.getElementById("cantidad"+formulario).value;
	var cantidad=dato.split(".");
	cantidad=cantidad[0]/1;
	//document.getElementById("cantidad"+formulario).value=cantidad;
	var unitario=document.getElementById("unitario"+formulario).value.replace(/[.]/g, "");
	if (cantidad>0 && cantidad<10){
		document.getElementById("total"+formulario).value=formatear_numero((unitario*cantidad)+"");
	} else {
		document.getElementById("cantidad"+formulario).value=1;
		document.getElementById("total"+formulario).value=formatear_numero((unitario*1)+"");
		alert("CANTIDAD: Cantidad incorrecta");
	}
}
function carro_entrega(formulario){
	var dato=formulario.entrega.value.split("/");
	var valor=dato[1]/1;
	if (!valor) valor=0;
	formulario.valor_entrega.value="$ "+valor;
	var subtotal=(formulario.subtotal1.value/1)+valor;
	formulario.total_compra.value="$ "+formatear_numero(subtotal+"");
}
function formatear_numero(numero){
	var regx = /(\d+)(\d{3})/;
	while (regx.test(numero)) {
		numero = numero.replace(regx, '$1' + '.' + '$2');
	}
	return numero;
}
function pregunta(formulario, id){
	if (formulario && id){
		if (confirm('¿Seguro que desea eliminar este producto del carro?')){
			formulario.accion.value="delete";
			formulario.dato.value=id;
			formulario.submit();
		}
	}
}
function cambiar_color(valor, imagen){
	document.images["imagen"].src="../images/productos/"+imagen;
	document.getElementById("color").value=valor;
	location.href="#arriba";
}
function pagar(formulario){
	formulario.action="carro_pagar.php";
	formulario.submit();
}
function seleccionador(objeto, id, precio, nombre, promocion, stock){
	var imagen=document.getElementById("imagen");
	imagen.src="../images/productos/"+id;
	var cadena='<h1>'+nombre.toUpperCase()+'</h1><span class="pesos">$</span><span class="precio">'+precio+'</span>';
	if (promocion) cadena+='<p class="promocion">PROMOCION LIMITADA, 20% DESCUENTO INCLUIDO</p>';
	if (stock) cadena+='<div id="agotado">Este producto se encuentra AGOTADO</div>';
	document.getElementById("precio").innerHTML=cadena;
	location.href="#arriba";
	if (stock) objeto="";
	document.temp.dato.value=objeto;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
