$(document).ready(function() {
	/* Search */
	$('.button-search').bind('click', function() {
		url = 'index.php?route=product/search';
		 
		var filter_name = $('input[name=\'filter_name\']').attr('value')
		
		if (filter_name) {
			url += '&filter_name=' + encodeURIComponent(filter_name);
		}
		
		location = url;
	});
	
	$('#header input[name=\'filter_name\']').keydown(function(e) {
		if (e.keyCode == 13) {
			url = 'index.php?route=product/search';
			 
			var filter_name = $('input[name=\'filter_name\']').attr('value')
			
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			
			location = url;
		}
	});
	
	/* Ajax Cart */
	$('#cart > .heading a').bind('click', function() {
		$('#cart').addClass('active');
		
		$.ajax({
			url: 'index.php?route=checkout/cart/update',
			dataType: 'json',
			success: function(json) {
				if (json['output']) {
					$('#cart .content').html(json['output']);
				}
			}
		});			
		
		$('#cart').bind('mouseleave', function() {
			$(this).removeClass('active');
		});
	});
	
	/* Mega Menu */
	$('#menu ul > li > a + div').each(function(index, element) {
		// IE6 & IE7 Fixes
		if ($.browser.msie && ($.browser.version == 7 || $.browser.version == 6)) {
			var category = $(element).find('a');
			var columns = $(element).find('ul').length;
			
			$(element).css('width', (columns * 143) + 'px');
			$(element).find('ul').css('float', 'left');
		}		
		
		var menu = $('#menu').offset();
		var dropdown = $(this).parent().offset();
		
		i = (dropdown.left + $(this).outerWidth()) - (menu.left + $('#menu').outerWidth());
		
		if (i > 0) {
			$(this).css('margin-left', '-' + (i + 5) + 'px');
		}
	});

	// IE6 & IE7 Fixes
	if ($.browser.msie) {
		if ($.browser.version <= 6) {
			$('#column-left + #column-right + #content, #column-left + #content').css('margin-left', '195px');
			
			$('#column-right + #content').css('margin-right', '195px');
		
			$('.box-category ul li a.active + ul').css('display', 'block');	
		}
		
		if ($.browser.version <= 7) {
			$('#menu > ul > li').bind('mouseover', function() {
				$(this).addClass('active');
			});
				
			$('#menu > ul > li').bind('mouseout', function() {
				$(this).removeClass('active');
			});	
		}
	}
});

$('.success img, .warning img, .attention img, .information img').live('click', function() {
	$(this).parent().fadeOut('slow', function() {
		$(this).remove();
	});
});

function addToCart(product_id) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information, .error').remove();
			
			if (json['redirect']) {
				location = json['redirect'];
			}
			
			if (json['error']) {
				if (json['error']['warning']) {
					$('#notification').html('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				}
			}	 
						
			if (json['success']) {
				$('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').fadeIn('slow');
				
				$('#cart_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

function removeCart(key) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'remove=' + key,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				$('#cart_total').html(json['total']);
				
				$('#cart .content').html(json['output']);
			}			
		}
	});
}

function removeVoucher(key) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'voucher=' + key,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				$('#cart_total').html(json['total']);
				
				$('#cart .content').html(json['output']);
			}			
		}
	});
}

function addToWishList(product_id) {
	$.ajax({
		url: 'index.php?route=account/wishlist/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').fadeIn('slow');
				
				$('#wishlist_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 				
			}	
		}
	});
}

function addToCompare(product_id) { 
	$.ajax({
		url: 'index.php?route=product/compare/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').fadeIn('slow');
				
				$('#compare_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

$(document).ready( function () {
$('#enviar_forma').click(
   function () {
   if($('#nombre_remitente').val() != ''){
   	var nombre_remitente = $('#nombre_remitente').val();
   }else{var nombre_remitente = '';}
   
   if($('#direccion_remitente').val() != ''){
   	var direccion_remitente = $('#direccion_remitente').val();
   }else{var direccion_remitente = '';}
   
   if($('#telefono_fijo').val() != ''){
   	var telefono_fijo = $('#telefono_fijo').val();
   }else{var telefono_fijo = '';}
   
   if($('#celular').val() != ''){
   var celular = $('#celular').val();	
   }else{var celular = '';}
   
   if($('#email').val() != ''){
   var email = $('#email').val();	
   }else{var email = '';}
   
   
   var monto_diezmil = $('#monto_diezmil:checkbox:checked').val();
   var monto_treintamil = $('#monto_treintamil:checkbox:checked').val();
   var monto_sesentamil = $('#monto_sesentamil:checkbox:checked').val();
   var monto_noventamil = $('#monto_noventamil:checkbox:checked').val();
   var otro_monto = $('#otro_monto').val();
   var monto_total;
   if(monto_diezmil != 'undefined'){
   monto_total = monto_diezmil;	
   }else if( monto_treintamil != 'undefined'){
   monto_total = monto_treintamil;}
   else if( monto_sesentamil != 'undefined'){
   monto_total = monto_sesentamil;	
   }
   else if( monto_noventamil != 'undefined'){
   monto_total = monto_noventamil;		
   }
   else if( otro_monto != 'undefined'){
   monto_total = otro_monto;
   }
   
   
   
   var donar_mensual = $('#donar_mensual:checkbox:checked').val();
   var donar_unavez = $('#donar_unavez:checkbox:checked').val();   
   var donar_eleccion = $('#donar_eleccion').val();
   var total_donar;
   
   if(donar_mensual != 'undefined'){
   	total_donar = donar_mensual;
   }else if(donar_unavez != 'undefined'){
   	total_donar = donar_unavez;
   }else if(donar_eleccion != 'undefined'){
   	total_donar = donar_eleccion;
   }
   
   
   var forma_pago_electronica = $('#forma_pago_electronica:checkbox:checked').val();
   var forma_pago_credito = $('#forma_pago_credito:checkbox:checked').val();
   var forma_pago_cheque = $('#forma_pago_cheque:checkbox:checked').val();
   var forma_pago_deposito = $('#forma_pago_deposito:checkbox:checked').val();
   var total_forma_pago;
   
   if(forma_pago_electronica != 'undefined'){
   total_forma_pago = forma_pago_electronica;	
   }else if(forma_pago_credito != 'undefined'){
   	total_forma_pago = forma_pago_credito;
   }else if(forma_pago_cheque != 'undefined'){
   	total_forma_pago = forma_pago_cheque;
   }else if(forma_pago_deposito != 'undefined'){
   	total_forma_pago = forma_pago_deposito;
   }
   
   var motivo_donacion = $('#motivo_donacion').val();
   
   
   
   
			$.ajax({
					      url: "http://fobeju.cl/catalog/view/javascript/mailform.php",
					      global: false,
					      type: "POST",
					      data: ({nombreContacto : nombre_remitente,
		      							direccionRemitente : direccion_remitente,
		      								telefonoFijo : telefono_fijo ,
		      								 contactoCelular : celular,
		      								  contactoEmail : email,
		      								   montoTotal : monto_total,
		      								    totalDonar : total_donar,
		      								     totalFormaPago : total_forma_pago,
		      								      motivoDonacion : motivo_donacion }),
		      				dataType: "html",
		      				async: true,
		      				success: function(msg){
		         					console.log(msg);
		         					$('.contenedor_amigos').empty();
		         					$('.contenedor_amigos').html("<div align='center'><br /><br /><br /><br /><br /><h3>Gracias por tu interes, nos pondremos en contacto a la brevedad.</h3></div>");	         				
		      				},
		      				error: function(msg){
		      					$('.contenedor_amigos').empty();
		         					$('.contenedor_amigos').html("<div align='center'><br /><br /><br /><br /><br /><h3>Un error ha ocurrido, podrias volver a intentarlo en unos momentos, gracias.</h3></div>");
		      				}
		   				}
					).responseText;
		   						
				   
	
	console.log(nombre_remitente);
	console.log(direccion_remitente);
	console.log(telefono_fijo);
	console.log(celular);
	console.log(email);
	console.log(monto_diezmil);
	console.log(monto_treintamil);
	console.log(monto_sesentamil);
	console.log(monto_noventamil);
	console.log(otro_monto);
	console.log(donar_mensual);
	console.log(donar_unavez);
	console.log(donar_eleccion);
	console.log(forma_pago_electronica);
	console.log(forma_pago_credito);
	console.log(forma_pago_cheque);
	console.log(forma_pago_deposito);
	console.log(motivo_donacion);


  });
  
  $('#enviar_forma_certificados').click(
  	
   function () {
   var datosFormaCertificados = 
   	   new Array($('#nombre_remitente').val(),
   	   			 $('#direccion_remitente').val(),
   	   			 $('#telefono_fijo_remitente').val(),
   	   			 $('#celular_remitente').val(),
   	   			 $('#email_remitente').val(),
   	   			 $('#nombre_destinatario').val(),
   	   			 $('#direccion_destinatario').val(),
   	   			 $('#telefono_fijo_destinario').val(),
   	   			 $('#celular_destinatario').val(),
   	   			 $('#email_destinatario').val(),
   	   			 $('#motivo_certificado').val(),
   	   			 $('#comentario_certificado').val(),
   	   			 $('#valor_donacion_certificados').val(),
   	   			 $('#forma_pago_electronica').val(),
   	   			 $('#forma_pago_credito').val(),
   	   			 $('#forma_pago_cheque').val(),
   	   			 $('#forma_pago_deposito').val(),
   	   			 $('#motivo_donacion').val()
   	   			 );
   	   			 
   console.log(datosFormaCertificados);

   
			$.ajax({
					      url: "http://fobeju.cl/catalog/view/javascript/mailformcertificados.php",
					      global: false,
					      type: "POST",
					      data: ({dataVar : datosFormaCertificados}),
		      				dataType: "html",
		      				async: true,
		      				success: function(msg){
		         					/*console.log(msg);*/
		         					$('.contenedor_amigos').empty();
		         					$('.contenedor_amigos').html("<div align='center'><br /><br /><br /><br /><br /><h3>Gracias por tu interes, nos pondremos en contacto a la brevedad.</h3></div>");	         				
		      				},
		      				error: function(msg){
		      					console.log(msg);
		      					$('.contenedor_amigos').empty();
		         				$('.contenedor_amigos').html("<div align='center'><br /><br /><br /><br /><br /><h3>Un error ha ocurrido, podrias volver a intentarlo en unos momentos, gracias.</h3></div>");
		      				}
		   				}
					).responseText;		   						
				   
	


  });
  
});

