// JavaScript Document
if (typeof console != 'object') {
      console = {log:function() {}}
}

function log(m,o){
	if(o=="clear"){ console.clear();}
	console.info("williams@exeperu.com:~$ "+m+".");	
}

var Imagenes = new Array();

function preloading(){
	for (x=0; x<preloading.arguments.length; x++){
		Imagenes[x] = new Image();
		Imagenes[x].src = preloading.arguments[x];
	}
}

$(document).ready(function(){
						   
$("#cargador_invisible").css({"display":"none"})
	$("#nro_productos").load("mete_producto.php?nro=true");

	});

function cargar(capa,url){ 
 $("#mensaje_ajax").css({"display":""})
 $("#"+capa).load(url,{evalScripts:true},function (){ $("#mensaje_ajax").css({"display":"none"})}  );
}

/*Ajax.Responders.register({
  onCreate: function(){
	  $("mensaje_ajax").style.display = "block";
	  
  },
  onComplete: function(){
			if (Ajax.activeRequestCount == 0)
			{
			$("mensaje_ajax").style.display = "none";
			}
			
			}
}); */
function cargar_producto(pagina,pagina2,pagina3){

   	document.getElementById('textotitulo').style.visibility='hidden';
 	document.getElementById('textotitulo').style.display ='none';

	//new Ajax.Updater("tit_tabla_producto",pagina2,{method:'get'});
	//document.getElementById('tit_tabla_producto').innerHTML = pagina3+' / '+pagina2;
	if(pagina3)	$('#tit_tabla_producto').text(pagina3+' / '+pagina2);
	else $('#tit_tabla_producto').text(pagina2);
	cargar("tablaproductos",pagina);
}

function add_productos(){
	 $("#cargador_invisible").load(url,{evalScripts:true});
}

function cargar_carrito(id_producto){
	//document.getElementById('tit_tabla_producto').innerHTML = 'Productos en el carrito de compras';
	var frm='document.form_'+ id_producto + '.cantidad.value' ;
      $("#cargador_invisible").load('mete_producto.php?cantidad=' + eval(frm) +'&id=' +id_producto,{evalScripts:true}, function (){$("#nro_productos").load("mete_producto.php?nro=true");});
}


function cargar_pregunta(capa,pagina,mensaje){
var answer = confirm (mensaje);
	if (answer){
		cargar(capa,pagina);}
	else{
	}
}
function carga_ver_carrito(capa,carrito){
	
	document.getElementById('textotitulo').style.visibility='hidden';
 	document.getElementById('textotitulo').style.display ='none';
	document.getElementById('tit_tabla_producto').innerHTML = 'Productos en el carrito de compras';
    //new Ajax.Updater('tit_tabla_producto','limipio_titulo.php',{method:'get'});				
	cargar(capa,carrito);
}

function carga_ver_carrito_index(capa,carrito){

	
	cargar(capa,carrito);
}

function actualiza_cantidad(capa,carrito){
	cargar(capa,carrito);
}

/*function load_page(capa,url){
	cargar(capa,url);
}
*/
function cargar_destino2(capa,valor,peso){
	cargar_ajax(capa,'destino2.php?des1='+ valor +'&peso='+ peso);
}

function entregar(destino1,distrito,peso){
 if (distrito=='') {
	 alert('Debe seleccionar un lugar de entrega valido');
 } else {
 var Aleatorio=parseInt(Math.random()*99999999);
 //var s2=document.frm_carrito.destino2.value;
	
 cargar_ajax("tablaproductos","mete_producto.php?des1="+ destino1 + "&dist="+ distrito + "&peso=" + peso + "&rand=" + Aleatorio);

		
 }
}

function cargar_(capa,total_pagar,costo_delivery,sin_delivery,sub_total,des1,dist,pess){
	var s1=des1;
	var s2=dist;	
	var peso=pess;	
 cargar_ajax(capa,'mete_producto.php?comprar=true&total_pagar='+total_pagar+'&costo_delivery='+costo_delivery+'&sin_delivery='+sin_delivery +'&sub_total='+sub_total+"&s1="+s1+"&s2="+s2+'&peso_neto='+peso);
}






function cargar_pais_provincia(pais){
/*			new Ajax.Updater("pais_provincia_div","provincia.php?pais=" + pais,{method:'get'});
*/	}
	
function ver_documento(){
	var tipocomprobante=document.datos_envio.tipocomprobante.value;
	if (tipocomprobante=='Factura'){
		//document.getElementById('capa_factura').style.visibility='visible'
		//document.getElementById('capa_factura').style.display ='';
		$('#capa_factura').show("slow");
		$('#ruc').focus();
		//$('#boton').scrollTo(100,100);
		//$(this).effect('transfer', { to: '#boton' }, 1000);
	}else{
		//document.getElementById('capa_factura').style.visibility='hidden'
		//document.getElementById('capa_factura').style.display ='none';
		$('#capa_factura').hide("slow");
	}
}	
	
function grabar_compra(capa,pagina){
	
	var contacto=document.datos_envio.contacto.value;
	var direccion= document.datos_envio.direccion.value;
	//var referencia = document.datos_envio.referencia.value;
	//var codigo_postal = document.datos_envio.codigo_postal.value;
	var telefono1=document.datos_envio.telefono1.value;
	//var telefono2=document.datos_envio.telefono2.value;
	var tipocomprobante=document.datos_envio.tipocomprobante.value;
	var costodelivery=document.datos_envio.costodelivery.value;	
	var costo_total=document.datos_envio.costo_total.value;		
	var sub_total=document.datos_envio.sub_total.value;		
	var datos_envio=document.datos_envio.datos_envio.value;	
	var peso_neto=document.datos_envio.peso_neto.value;	
	var nota=document.datos_envio.nota.value;	
	
	var ruc=document.datos_envio.ruc.value;	
	var razonsocial=document.datos_envio.razonsocial.value;		
	var direccionfactura=document.datos_envio.direccionfactura.value;
	
	var pais				=	$("#pais").val();
	var departamento		=	$("#txtdepartamento").val();
	var provincia			=	$("#txtprovincia").val();
	var contacto_envio		=	$("#contacto_envio").val();
	var pais_envio			=	$("#pais_envio").val();
	var departamento_envio	=	$("#txtdepartamento_envio").val();
	var provincia_envio		=	$("#txtprovincia_envio").val();
	var direccion_envio		=	$("#direccion_envio").val();
	
	
/*	alert(contacto);
	alert(direccion);
	alert(ciudad);
	alert(codigo_postal);
	alert(pais);
	alert(provincia);
	alert(telefono1);
	alert(telefono2);
	
	alert(tipocomprobante);
*/	
//	mete_producto.php?comprar_realizada=true
//cargar_ajax(capa,'mete_producto.php?comprar_realizada=true&co='+ contacto + '&dir='+ direccion + '&ref='+referencia + '&cod='+ codigo_postal + '&ruc='+ ruc +'&razonsocial='+ razonsocial +  '&te1=' + telefono1+  '&te2=' + telefono2 + '&tco=' + tipocomprobante + '&costodel=' + costodelivery + '&cos_tot=' + costo_total + '&s_to=' +  sub_total + '&direccionfactura=' + direccionfactura + '&datos_envio=' + datos_envio + '&peso_neto='+peso_neto +'&nota='+nota);
	cargar_ajax(capa,'mete_producto.php?comprar_realizada=true&co='+ contacto 
														   +'&dir='+ direccion 
														   +'&ruc='+ ruc 
														   +'&razonsocial='+ razonsocial 
														   +'&te1=' + telefono1
														   +'&tipocomprobante=' + tipocomprobante 
														   +'&costodel=' + costodelivery 
														   +'&cos_tot=' + costo_total 
														   +'&s_to=' +  sub_total 
														   +'&direccionfactura=' + direccionfactura 
														   +'&datos_envio=' + datos_envio 
														   +'&peso_neto='+peso_neto 
														   +'&nota='+nota
														   +'&ruc='+ruc
														   +'&pais='+pais
														   +'&departamento='+departamento
														   +'&provincia='+provincia
														   +'&contacto_envio='+contacto_envio
														   +'&pais_envio='+pais_envio
														   +'&departamento_envio='+departamento_envio
														   +'&provincia_envio='+provincia_envio
														   +'&direccion_envio='+direccion_envio);
}


function cerrarsession (){
if(confirm("Realmente desea cerrar la sesión?")){
	window.location.href="logout.php";
}else{}

}

function  carga_deta_s(valor){
//alert(valor);
cargar("s_s_cat_prod","tabla_productos.php?filtro=true&id=" + valor);
}

function  olvido(valor){
//alert(valor);
cargar("mensaje_olvido","olvido_respuesta.php?valor=" + valor);
}

/*function detenerError() 
{ 
return true 
} 
window.onerror=detenerError 
*/
function validar_olvido(frm){
	
if (frm.olvido.value==''){
alert('Debe ingresar su email')
frm.olvido.focus();
return false
}
return true
}


function validar_login(frm){
	if (frm.email.value=='Email')	{
	alert('Debe ingresar su email');
	frm.email.focus();
	return false
	}
	
	if (frm.clave.value=='*******')	{
	alert('Debe ingresar su contraseña');
	frm.clave.focus();
	return false
	}
	
	return true
}

function objetoAjax(){

	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 cargar_ajax(capa,url){
	

	divresultado=document.getElementById(capa);

	var Aleatorio=parseInt(Math.random()*99999999);

	ajax=new objetoAjax();
	
	//aki sacamos la varia que seguira a la url si es un & o ?
	
	if (url.indexOf("?")>0){encadenar="&";}else{encadenar="?";}

	ajax.open("GET",url + encadenar +"rand=" + Aleatorio,true);

	ajax.onreadystatechange=eval(useHttpResponse);
 
	ajax.send(null);
}


function useHttpResponse() {
  if (ajax.readyState == 4)    {
   		 if(ajax.status == 200) 		{
divresultado.innerHTML=ajax.responseText;
		}
 }  else   {
  divresultado.innerHTML = '<center><div align="center"  style="display:none; width:116px;	height:30px;"><table border="0" cellspacing="0" cellpadding="0" ><tr><td> <img src="images/loading.gif" alt="Cargando" /></td>	<td>Cargando   ...</td></tr></table></div></center>';
  }
}

function imprimir(que,css) {
var ventana = window.open("", "", "");
var contenido = "<html><head></script><link href='css/"+css+".css' rel='stylesheet' type='text/css' /><style type='text/css'>body{ font-family:arial;font-size:12px;color:#000000; }#baseimp{margin-right:auto;margin-left: auto;}</style></head><body onload='window.print();window.close()'><div id='baseimp'>" + document.getElementById(que).innerHTML + "</div></body></html>";
ventana.document.open();
ventana.document.write(contenido);
ventana.document.close();
}




function valida_mail(i){
  $.get("busca_mail.php", { mail:i },
  function(data){
	  //$('#m_email').text(data);
	  //$('#error').val('');
	  if(data == "correo disponible"){
		  $('#m_email').text('');
		  $('#error').val(data);
	  }else{
		  $('#m_email').text(data);
		  $('#error').val('');
	  }
  });
}


$(document).ready(function(){
    
    //get the height and width of the page
    var window_width = $(window).width();
    var window_height = $(window).height();
    
    //vertical and horizontal centering of modal window(s)
    /*we will use each function so if we have more then 1 
    modal window we center them all*/
    $('.modal_window').each(function(){
        
        //get the height and width of the modal
        var modal_height = $(this).outerHeight();
        var modal_width = $(this).outerWidth();
        
        //calculate top and left offset needed for centering
        var top = (window_height-modal_height)/2;
        var left = (window_width-modal_width)/2;
        
        //apply new top and left css values 
        $(this).css({'top' : top , 'left' : left});
        
		
		
		
    });
        
        $('.close_modal').click(function(){
            close_modal();
        });
        
    });
    
    //THE FUNCTIONS
    
    function close_modal(){
        
        //hide the mask
        $('#mask').fadeOut(500);
        
        //hide modal window(s)
        $('.modal_window').fadeOut(500);
        
    }
    function show_modal(modal_id){
    
        //set display to block and opacity to 0 so we can use fadeTo
        $('#mask').css({ 'display' : 'block', opacity : 0,'height': $("body").height() });
        //fade in the mask to opacity 0.8 
        $('#mask').fadeTo(100,0.8);
         
         //show the modal window
        $('#'+modal_id).fadeIn("slow");
        
    }

