// JavaScript Document



function desplegar()
{
	if(document.getElementById("subcat").style.display=="none")
	{
		document.getElementById("subcat").style.display="block";
	}
	else
	{
		document.getElementById("subcat").style.display="none";
	}
}



function pie_stylo(nombre,stylo,tamano,grosor)

{

	var a=document.getElementById(nombre);

	a.style.textDecoration=stylo;

	a.style.fontSize=tamano;

	a.style.fontWeight=grosor;

}

function stylo_link(nombre,stylo,color)

{

	var a=document.getElementById(nombre);

	a.style.textDecoration=stylo;

	a.style.cursor="pointer";

	

}

function stylo_link2(nombre,stylo)

{

	var a=document.getElementById(nombre);

	a.style.textDecoration=stylo;

	a.style.cursor="pointer";

}

function stylo_link_resaltado(nombre,iframe,param)

{

	 for(i=1;i<=param;i++)

	{

		var a=document.getElementById("p"+i);

		a.style.fontWeight="normal";

		a.style.color="#03579F";

		a.style.fontSize="12px";

		a.style.fontWeight="bold";

	}

		var b=document.getElementById(nombre);

		b.style.fontWeight="bold";

		b.style.color="#9A020E";

		b.style.fontSize="12px";

		

		window.frames["content"].location.href=iframe;



}

function stylo_link_resaltado2(nombre,param)

{

	 for(i=1;i<=param;i++)

	{

		var a=document.getElementById("ps"+i);

		a.style.fontWeight="normal";

		a.style.color="#60584B";

		a.style.fontSize="11.5px";

	}

		var b=document.getElementById(nombre);

		b.style.fontWeight="bold";

		b.style.color="#01589B";

		b.style.fontSize="14px";

}

function ir_principal()

{

	if(!document.getElementById("flash1"))

	{

		window.location.href="http://www.directoparati.com/";

	}

	

}

function if_content()

{

	/*document.getElementById("content").height =900;

	document.getElementById("content").height = window.frames.content.document.body.scrollHeight;*/

}

function ir_pagina(pagina)

{

	document.getElementById("content").src=pagina;

	

}

function enlazar_portada(pagina)

{

	document.getElementById("content").src=pagina;

}

function maximizar_imagen(e,visible,ruta)

{

	//document.images["img_grande"].src=ruta;

	x = e.clientX; 

	y = e.clientY;

	var a=document.getElementById("Layer2");

	a.style.visibility=visible;

	a.style.left=260;

	a.style.top=y-90; 

}

function devolver(pagina,nombre)

{

	window.location.href=pagina+"?nombre="+nombre;

}

function validar_contactenos()

{

	var cajas=new Array("nombres","apellidos","pais","ciudad","codigo_ciudad","telefono","celular","email","consulta");

	var alertas=new Array("Nombres","Apellidos","País","Ciudad","Código Postal","Teléfono","Celular","Email","Consulta");

	var param=new Array(2,2,2,2,2,2,2,5,2);

	

	for(i=0;i<=7;i++)

	{

		if(document.getElementById(cajas[i]).value.length<param[i])

		{

			alert("Ingrese correctamente : "+alertas[i]);

			document.getElementById(cajas[i]).focus();

			break;

		}

		if(i==7)

		{

			document.form_contactenos.submit();

		}

	}

}

function limpiar()

{

	var cajas=new Array("nombres","apellidos","dni","telefono","celular","email","consulta");

	

	for(i=0;i<=6;i++)

	{

		document.getElementById(cajas[i]).value="";

	}

	document.getElementById(cajas[0]).focus();

}



function cargar_imagen1(nombre,imagen)

{

	document.images[nombre].src=imagen;

	

	

}



function nuevoAjax(){

var xmlhttp=false;

 try {

  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

 } catch (e) {

  try {

   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

  } catch (E) {

   xmlhttp = false;

  }

 }



if (!xmlhttp && typeof XMLHttpRequest!='undefined') {

  xmlhttp = new XMLHttpRequest();

}

return xmlhttp;

}



function logear(){

	var usuario,clave,contenedor;

	contenedor = document.getElementById('inicio_sesion');

	usuario = document.getElementById('usuario').value;

	clave = document.getElementById('clave').value;

	ajax=nuevoAjax();

	ajax.open("POST", "logear.php",true);

	ajax.onreadystatechange=function() {

		if (ajax.readyState==4)

		{	

			window.location.reload();

			contenedor.innerHTML = ajax.responseText;

			

	 	}

	}

	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	ajax.send("usuario="+usuario+"&clave="+clave);

}



function validar_logear()

{

	if(document.getElementById('usuario').value=="")

	{

		document.getElementById('contenedor').innerHTML="<br>Ingrese un usuario<br><br>";

	}

	if(document.getElementById('clave').value=="")

	{

		document.getElementById('contenedor').innerHTML="<br>Ingrese una clave<br><br>";

	}

	if(document.getElementById('usuario').value=="" && document.getElementById('clave').value=="")

	{

		document.getElementById('contenedor').innerHTML="<br>Ingrese un usuario y clave<br><br>";

	}

	if(document.getElementById('usuario').value!="" && document.getElementById('clave').value!="")

	{

		logear();

	}

	

}



function anadir_carrito()

{

	var caja1,caja2;

	caja1 = document.getElementById('usuario');

	caja2 = document.getElementById('clave');

	

	//alert("Usted debe haber iniciado sesión para poder utilizar el carrito de compras!");

	caja1.style.backgroundColor = "#B3E3F7";

	caja2.style.backgroundColor = "#B3E3F7";

	

	window.location.href="index.php#ancla_sesion";

	caja1.focus();

}



function cerrar_sesion(id_user_web)

{

	if(confirm("Desea cerrar sesión? Si cierra sesión se eliminarán todos los productos añadidos a su carrito de compras !"))

	{

	var inicio;

	inicio = document.getElementById('inicio_sesion');

	ajax=nuevoAjax();

	ajax.open("POST", "cerrar_sesion_usuarioweb.php",true);

	ajax.onreadystatechange=function()

	{

		if (ajax.readyState==4)

		{

			

				inicio.innerHTML=ajax.responseText;

				window.frames['content'].location.href='home.php';

		}

	}



		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		ajax.send("estado=cerrar&id_user_web="+id_user_web);

	}

}



function ir_web(web)

{

	window.location.href=web;

}



function cambiar_imagen(ruta)

{

	document.images["imagen10"].src=ruta;

}







function eliminar_producto_carrito(id_detalle_producto,max1,inicio,fin)

{

	if(confirm("Desea eliminar este producto añadido en su carrito?"))

	{

	var itc;

	itc = document.getElementById('item_productos_carrito');

	ajax=nuevoAjax();

	ajax.open("POST", "eliminar_producto_carrito.php",true);

	ajax.onreadystatechange=function()

	{

		if (ajax.readyState==4)

		{

			

				itc.innerHTML=ajax.responseText;

				calcular_importe(inicio,fin-1,'','','');

				 if(fin==3)

				{

					document.getElementById('comprar').disabled=true;

				}

				else

				{

					document.getElementById('comprar').disabled=false;

				}

		}

	}



		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		ajax.send("id_detalle_compra="+id_detalle_producto+"&max1="+max1);

	}

}



function cambiar_moneda(id_agregar_moneda){



	ajax=nuevoAjax();

	ajax.open("POST", "cambiar_moneda.php",true);

	ajax.onreadystatechange=function() {

		if (ajax.readyState==4)

		{

			window.frames["content"].location.reload();

	 	}

	}

	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	ajax.send("id_moneda_agregar="+id_agregar_moneda);

}

/////////////////////////////// Decimales



function redondea(sVal, nDec){ 

    var n = parseFloat(sVal); 

    var s = "0.00"; 

    if (!isNaN(n)){ 

     n = Math.round(n * Math.pow(10, nDec)) / Math.pow(10, nDec); 

     s = String(n); 

     s += (s.indexOf(".") == -1? ".": "") + String(Math.pow(10, nDec)).substr(1); 

     s = s.substr(0, s.indexOf(".") + nDec + 1); 

    } 

    return s; 

   } 

///////////////////////////////

/*

function calcular_importe(inicio,fin,nombre_form,cantidad,precio)

{

	if(cantidad!="")

	{	

		document.getElementById(nombre_form).value=parseFloat(cantidad*precio);

		document.getElementById(nombre_form).value=redondea(document.getElementById(nombre_form).value,2);

	}

	var a=0

	

	while(inicio<=fin)

	{

		a=a+parseFloat(document.getElementById("import_"+inicio).value);

		inicio++;

	}

	

	document.getElementById("total_producto").value=a;

	

	document.getElementById("total_producto").value=redondea(document.getElementById("total_producto").value,2);

	

	document.getElementById("total").value=parseFloat(document.getElementById("total_producto").value)+parseFloat(document.getElementById("total_pa").value)+parseFloat(document.getElementById("total_ge").value);

	

	document.getElementById("total").value=redondea(document.getElementById("total").value, 2);

	

}

*/



function calcular_importe(inicio,fin,nombre_form,cantidad,precio)

{

	if(cantidad!="")

	{	

		document.form1[nombre_form].value=parseFloat(cantidad*precio);

		document.form1[nombre_form].value=redondea(document.form1[nombre_form].value,2);

	}

	var a=0

	

	while(inicio<=fin)

	{

		a=a+parseFloat(document.form1["import_"+inicio].value);

		inicio++;

	}

	

	document.form1["total_producto"].value=a;

	

	document.form1["total_producto"].value=redondea(document.form1["total_producto"].value,2);

	

	document.form1["total"].value=parseFloat(document.form1["total_producto"].value)+parseFloat(document.form1["total_pa"].value)+parseFloat(document.form1["total_ge"].value);

	

	document.form1["total"].value=redondea(document.form1["total"].value, 2);

	

}



function calcular_detalle(nombre1,nombre2,precio)

{

	

	if(document.form1[nombre1].checked==true)

	{

	 document.form1[nombre2].value=parseFloat(document.form1[nombre2].value)+(precio);

	 document.form1[nombre2].value=redondea(document.form1[nombre2].value,2);

	}

	else

	{

	 document.form1[nombre2].value=parseFloat(document.form1[nombre2].value)-(precio);

	 document.form1[nombre2].value=redondea(document.form1[nombre2].value,2);

	}

	

	document.form1["total"].value=parseFloat(document.form1["total_producto"].value)+parseFloat(document.form1["total_pa"].value)+parseFloat(document.form1["total_ge"].value);

	

	document.form1["total"].value=redondea(document.form1["total"].value, 2);

	

}



function calcular_gasto_envio(nombre,cantidad,cambio,distrito)

{

	document.form1[nombre].value=parseFloat(cantidad*cambio);

	

	document.form1[nombre].value=redondea(document.form1[nombre].value,2);

	

	document.form1["total_ge"].value=document.form1[nombre].value;

	

	document.form1["total_ge"].value=redondea(document.form1[nombre].value,2);

	

	document.form1["total"].value=parseFloat(document.form1["total_producto"].value)+parseFloat(document.form1["total_pa"].value)+parseFloat(document.form1["total_ge"].value);

	

	document.form1["total"].value=redondea(document.form1["total"].value, 2);

	

	//document.getElementById("distrito_des").value=document.getElementById(nombre).value;

	

	document.form1["distrito_des"].value=document.form1["distrito1"].options[document.form1["distrito1"].selectedIndex].text;

	

}



function validar_registrese()

{

	var cajas=new Array("nombres","apellidos","celular","ciudad","codigo_postal","direccion","usuario","clave");

	var alertas=new Array("Nombres","Apellidos","Celular","Ciudad","Código Postal","Dirección","Usuario","Contraseña");

	var param=new Array(2,2,4,2,2,2,6,6);

	

	for(i=0;i<=7;i++)

	{

		if(document.getElementById(cajas[i]).value.length<param[i])

		{

			alert("Ingrese correctamente : "+alertas[i]);

			document.getElementById(cajas[i]).focus();

			break;

		}

		if(i==7)

		{

			document.form_registrese.submit();

		}

	}

}





function validar_destinatario()

{

	

	var cajas=new Array("nombres_uw","apellidos_uw","telefono_uw","celular_uw","ciudad_uw","codigopostal_uw","direccion_uw","email_uw","nombre_des","apellido_des","telefono_des","direccion_des","fecha_entrega_Des");

	var alertas=new Array("Nombres","Apellidos","Teléfono","Celular","Ciudad","Código Postal","Dirección","E-mail","Nombres","Apellidos","Telefono","Dirección","Fecha de entrega");

	var param=new Array(2,2,4,2,2,2,2,7,2,2,7,7,1);

	

	for(i=0;i<=12;i++)

	{

		if(document.getElementById(cajas[i]).value.length<param[i])

		{

			alert("Ingrese correctamente : "+alertas[i]);

			document.getElementById(cajas[i]).focus();

			return false;

		}

	}

	document.getElementById("form1").submit();

}



function ver_email()

{

	if(document.getElementById("capa_olv_con").style.visibility=="hidden")

	{

		document.getElementById("capa_olv_con").style.visibility="visible";

		document.getElementById("caja_email").focus();

	}

	else

	{

		document.getElementById("capa_olv_con").style.visibility="hidden";

	}

}



function envio_clave(email){



	

		ajax=nuevoAjax();

		ajax.open("POST", "enviar_clave.php",true);

		ajax.onreadystatechange=function() {

			if (ajax.readyState==4)

			{

				alert("Su clave ha sido enviada a su Email");

				document.getElementById("capa_olv_con").style.visibility="hidden";

			}

		}

		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		ajax.send("email="+email);

	/*}

	else

	{

		alert("Ingrese su Email");

		document.getElementById("caja_email").focus();

	}*/

}


