









Iluria = function() {}

Iluria.cart = function() {}
Iluria.clientAccount = function() {}

Iluria.cart.width = 750;
Iluria.cart.height = 480;

Iluria.clientAccount.width = 750;
Iluria.clientAccount.height = 450;

Iluria.readClientProductComment = function(form, callback) {
	$.ajax({
		url: "/clientServices.do?command=readClientProductComment",
		data: $(form).serialize(),
		type: "POST",
		async: true,
		cache: false,
		contentType : "application/x-www-form-urlencoded; charset=utf-8",
		success: function(responseData, status, xhr) {
			callback.call();
		},
		error: function(xhr, status, error) {
			callback.call("error");
		}
	});
}

Iluria.loginUser = function() {
	
	var redirectUrl = location;
	
	var view = $("#iluria-view");

	if(view.size() == 0)
		$("body").append("<div id='iluria-view' style='display: none'/>");

	$("#iluria-view").html("<div />");
	
	$("#iluria-view").prettyPhoto({
		theme: 'facebook', 
		hideflash: 'true',
		allowresize: false,
		showTitle: false,
		iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no" scrolling="no"></iframe>'
	});
	$.prettyPhoto.open(
		'http://shopconnect.iluria.com/shopconnect/shopconnect.do'
			+ '?command=showLoginForm'
			+ '&locale=pt-br'
			+ '&r=' + Math.random() 
			+ '&redirectUrl=' + encodeURIComponent(redirectUrl) 
			+ '&iframe=true&width=700&height=355',
				'',
				
					"<span style='padding-left: 15px; font-family: \"lucida grande\",tahoma,verdana,arial,sans-serif; font-size: 11px; font-weight: bold; color: #000000'></span>"
				
	);	
}

Iluria.logoutUser = function() {
	$.ajax({
		url: "/clientServices.do?command=logoutClient",
		type: "GET",
		async: true,
		cache: false,
		success: function(responseData, status, xhr) {
			document.location = "/";
		},
		error: function(xhr, status, error) {
		}
	});
}

Iluria.showProductPictureZoom = function(productId, pictureId) {
	Iluria.initView(850, 450, function() {
		Iluria.loadView("/webCommons/includes/product/zoom.jsp?rand="+Math.random(), 
			"pictureId=" + pictureId + "&productId=" + productId, 
			function(response) {
				Iluria.showView(response);			
			});
	});
}


Iluria.addSoldoutNotify = function(productId) {
		
	
	Iluria.initView(400, 165, function() {
		Iluria.loadView("/soldOutNotification.do?command=showAddSoldoutProductNotifyForm&productId=" + productId, 
			function(response, status) {
				if(status == 10005) {
					alert("Este produto está fora de estoque.");
					$.prettyPhoto.close();
					return;
				} else if (status == 10014) {
					alert("Não foi possível calcular o frete pois alguns produtos estão\nsem informação de peso e medidas. Para finalizar o pedido,\npor gentileza entre em contato com nosso atendimento para\nque possamos informar o valor do frete.");
					$.prettyPhoto.close();
					return;
				} else {
					Iluria.showView(response);
				}
			});
	});
}

Iluria.readAddSoldoutNotifyForm = function() {
	var form = $("#notifyForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/soldOutNotification.do", data, 
		function(response, status) {
			if(status == 10001)
				alert("E-mail j\u00E1 cadastrado para este produto.");
			else
				alert("E-mail cadastrado com sucesso!");
			$.prettyPhoto.close();
	}, "POST");
}

Iluria.addToCart = function(productId) {
	

	
	
	Iluria.initView(Iluria.cart.width, Iluria.cart.height, function() {
		var productVariationId = $("#iluria-product-variation").val();
		Iluria.loadView("/cart.do?command=addToCart&productId=" + productId + "&productVariationId=" + productVariationId, 
			function(response, status) {
				if(status == 10005) {
					alert("Este produto está fora de estoque.");
					$.prettyPhoto.close();
					return;
				} else if (status == 10014) {
					alert("Não foi possível calcular o frete pois alguns produtos estão\nsem informação de peso e medidas. Para finalizar o pedido,\npor gentileza entre em contato com nosso atendimento para\nque possamos informar o valor do frete.");
					$.prettyPhoto.close();
					return;
				} else {
					Iluria.showView(response);
				}
			});
	});
}

Iluria.removeFromCart = function(productId, variationId) {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=removeFromCart&productId=" + productId + "&variationId=" + variationId , 
		function(response) {
			Iluria.showView(response);			
	});
}

Iluria.cart.selectShippingType = function (shippingType) {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=updateShippingType&shippingType=" + shippingType,
		function(response, status) {
			if(status == 10004) {
				Iluria.loadView("/cart.do?command=showCart",
					function(response) {
						Iluria.showView(response);			
						alert("Não foi possível calcular o frete automaticamente porque o peso ou volume dos produtos excede o máximo permitido pelos Correios. Para finalizar o pedido, por gentileza entre em contato com nosso atendimento para que possamos informar o valor do frete.");
					});
			}
			if(status == 10003) {
				Iluria.loadView("/cart.do?command=showCart",
					function(response) {
						Iluria.showView(response);			
						alert("Não foi possível calcular o frete para o CEP informado.\nO CEP pode estar errado ou o serviço de cálculo de frete\ndos Correios pode estar fora do ar. Para finalizar o pedido,\npor gentileza entre em contato com nosso atendimento\npara que possamos informar o valor do frete.");
					});
			}
			if(status == 10014) {
				Iluria.loadView("/cart.do?command=showCart",
					function(response) {
						Iluria.showView(response);			
						alert("Não foi possível calcular o frete pois alguns produtos estão\nsem informação de peso e medidas. Para finalizar o pedido,\npor gentileza entre em contato com nosso atendimento para\nque possamos informar o valor do frete.");
					});
			}
			if(status = 200) {
				Iluria.showView(response);
				$("#zip").focus();			
			}
		});
}

Iluria.cart.reloadCart = function() {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=showCart", 
		function(response) {
			Iluria.showView(response);			
		});
}

// Iluria.cart.calculateShipping = function (zip) {
// 	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
// 	Iluria.loadView("/cart.do?command=calculateShipping&zip=" + zip, 
// 		 function(response) {
// 			Iluria.showView(response);			
// 	});
// }

Iluria.cart.calculateShipping = function (zip) {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=calculateShipping&zip=" + zip, 
		function(response, status) {
			if(status == 10004) {
				Iluria.loadView("/cart.do?command=showCart",
					function(response) {
						Iluria.showView(response);			
						alert("Não foi possível calcular o frete automaticamente porque o peso ou volume dos produtos excede o máximo permitido pelos Correios. Para finalizar o pedido, por gentileza entre em contato com nosso atendimento para que possamos informar o valor do frete.");
					});
			}
			if(status == 10003) {
				Iluria.loadView("/cart.do?command=showCart",
					function(response) {
						Iluria.showView(response);			
						alert("Não foi possível calcular o frete para o CEP informado.\nO CEP pode estar errado ou o serviço de cálculo de frete\ndos Correios pode estar fora do ar. Para finalizar o pedido,\npor gentileza entre em contato com nosso atendimento\npara que possamos informar o valor do frete.");
					});
			}
			if(status == 10014) {
				Iluria.loadView("/cart.do?command=showCart",
					function(response) {
						Iluria.showView(response);			
						alert("Não foi possível calcular o frete pois alguns produtos estão\nsem informação de peso e medidas. Para finalizar o pedido,\npor gentileza entre em contato com nosso atendimento para\nque possamos informar o valor do frete.");
					});
			}
			if(status = 200) {
				Iluria.showView(response);			
			}
		});
}
			
Iluria.cart.showClientInfoForm = function () {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=showClientInfoForm", 
		function(response) {
			Iluria.showView(response);			
	});
}

Iluria.saveCartComments = function(commentsFormId) {
	var form = $("#" + commentsFormId);
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=updateComments", data, 
		function(response) {
			Iluria.showView(response);			
	}, "POST");
}

Iluria.saveCartCoupon= function(commentsFormId) {
	var form = $("#" + commentsFormId);
	var data = form.serialize();
	//Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=updateCoupon", data, 
		function(response, status) {
			if(status == 10006) {
				Iluria.loadView("/cart.do?command=showCart",
					function(response) {
						Iluria.showView(response);			
						alert("Cupom inválido.");
					});
			}
			Iluria.showView(response);			
	}, "POST");
}

Iluria.cart.showClientAddressForm = function () {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=showClientAddressForm", 
		function(response) {
			Iluria.showView(response);			
	});
}

Iluria.cart.backFromClientAddress = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=saveClientAddress", data, 
		function(response) {
			Iluria.cart.reloadCart();
	}, "POST");
}

Iluria.cart.nextFromClientAddress = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=saveClientAddress", data, 
		function(response) {
			Iluria.loadView("/cart.do?command=showClientInfoForm", 
				function(response) {
					Iluria.showView(response);			
			});
	}, "POST");
}

Iluria.cart.backFromClientInfoToClientAddress = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=saveClientInfo", data, 
		function(response) {
			Iluria.loadView("/cart.do?command=showClientAddressForm", 
				function(response) {
					Iluria.showView(response);			
			});
	}, "POST");
}

Iluria.cart.backFromClientInfoToCart = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=saveClientInfo", data, 
		function(response) {
			Iluria.cart.reloadCart();
	}, "POST");
}

Iluria.cart.nextFromClientInfoToPayment = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=saveClientInfo", data, 
		function(response) {
			Iluria.cart.showPaymentForm();
	}, "POST");
}

Iluria.cart.submitOrderManual = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=saveClientInfo", data, 
		function(response) {
			Iluria.cart.submitOrder();
	}, "POST");
}

Iluria.cart.showPaymentForm = function () {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=showPaymentForm", 
		function(response) {
			Iluria.showView(response);			
	});
}

Iluria.cart.backFromPaymentInfo = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=savePaymentInfo", data, 
		function(response) {
			Iluria.cart.showClientInfoForm();
	}, "POST");
}

Iluria.cart.submitOrder = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=savePaymentInfo", data, 
		function(response) {
			Iluria.loadView("/cart.do?command=submitOrder", 
				function(response) {
					if(response == null || response == "") {
						alert("Houve um erro inesperado ao enviar seu pedido. \n\nPor favor entre em contato com nosso atendimento para mais informações.");
						$.prettyPhoto.close();
						return;
					}
					else { 
						 alert("Para acompanhar o andamento do pedido, clique na opção 'Minha conta' na loja.\n\nSeu pedido foi enviado com sucesso."); 
						
						
						
						
						
						$("body").append("<div id='iluria-cart-payment-form-container' style='display: none'/>");
						$("#iluria-cart-payment-form-container").html(response);
					}						
			});
	}, "POST");
}

Iluria.updateQuantity = function(productId, quantity, variationId) {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/cart.do?command=updateQuantity&productId=" + productId + "&quantity=" + quantity+ "&variationId=" + variationId, 
		function(response, status) {
			if(status == 10001) {
				Iluria.loadView("/cart.do?command=showCart", 
					function(response) {
						Iluria.showView(response);			
						 alert("Quantidade menor que o mínimo permitido."); 
						
						
						
						
						
					});
			}
			if(status == 10002) {
				Iluria.loadView("/cart.do?command=showCart", 
					function(response) {
						Iluria.showView(response);			
						 alert("Quantidade maior que o máximo permitido."); 
						
						
						
						
						
					});
			}
			if(status == 10004) {
				Iluria.loadView("/cart.do?command=showCart",
					function(response) {
						Iluria.showView(response);			
						alert("Não foi possível calcular o frete automaticamente porque o peso ou volume dos produtos excede o máximo permitido pelos Correios. Para finalizar o pedido, por gentileza entre em contato com nosso atendimento para que possamos informar o valor do frete.");
					});
			}
			if(status == 10003) {
				Iluria.loadView("/cart.do?command=showCart",
					function(response) {
						Iluria.showView(response);			
						alert("Não foi possível calcular o frete automaticamente porque o serviço de cálculo de frete dos Correios está fora do ar. Para finalizar o pedido, por gentileza entre em contato com nosso atendimento para que possamos informar o valor do frete.");
					});
			}
			if(status == 10014) {
				Iluria.loadView("/cart.do?command=showCart",
					function(response) {
						Iluria.showView(response);			
						alert("Não foi possível calcular o frete pois alguns produtos estão\nsem informação de peso e medidas. Para finalizar o pedido,\npor gentileza entre em contato com nosso atendimento para\nque possamos informar o valor do frete.");
					});
			}
			if(status = 200) {
				Iluria.showView(response);			
			}
	});
}

Iluria.showCart = function() {
	

	Iluria.initView(Iluria.cart.width, Iluria.cart.height, function() {
		Iluria.loadView("/cart.do?command=showCart", 
			function(response) {
				Iluria.showView(response);			
			});
	});
}

Iluria.clientAccount.showClientAccountForm = function () {
	Iluria.initView(Iluria.clientAccount.width, Iluria.clientAccount.height, function() {
		Iluria.loadView("/clientServices.do?command=showClientOrderHistoryForm", 
			function(response) {
				Iluria.showView(response);			
		});
	});
}

Iluria.clientAccount.showClientInfoForm = function () {
	Iluria.showViewProgress(Iluria.clientAccount.width, Iluria.clientAccount.height);
	Iluria.loadView("/clientServices.do?command=showClientInfoForm", 
		function(response) {
			Iluria.showView(response);			
	});
}

Iluria.clientAccount.subscribeNewsletter = function (email) {
	

	$.ajax({
		url: "/clientServices.do?command=subscribeNewsletter&email=" + email,
		type: "POST",
		async: true,
		cache: false,
		error: function(xhr, status, error) {
			if(xhr.status == 10000) {
				 alert("Email inválido"); 
				
				
				
				
				
			}
			if(xhr.status == 10001) {
				 alert("Seu email foi cadastrado com sucesso!"); 
				
				
				
				
				
			}
		}
	});
}

Iluria.clientAccount.saveClientInfoForm = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.clientAccount.width, Iluria.clientAccount.height);
	Iluria.loadView("/clientServices.do?command=saveClientInfoForm", data, 
		function(response, responseStatus) {
			if(responseStatus == 31001)
				 alert("O email informado já está cadastrado."); 
				
				
				
				
				
			else
				 alert("Dados atualizados com sucesso."); 
				
				
				
				
				

			Iluria.clientAccount.showClientInfoForm();
	}, "POST");
}

Iluria.clientAccount.saveClientAddressForm = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.clientAccount.width, Iluria.clientAccount.height);
	Iluria.loadView("/clientServices.do?command=saveClientAddressForm", data, 
		function(response) {
			 alert("Dados atualizados com sucesso."); 
			
			
			
			
			
			Iluria.clientAccount.showClientAddressForm();
	}, "POST");
}

Iluria.clientAccount.showClientAddressForm = function () {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/clientServices.do?command=showClientAddressForm", 
		function(response) {
			Iluria.showView(response);			
	});
}

Iluria.clientAccount.saveClientPasswordForm = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.clientAccount.width, Iluria.clientAccount.height);
	Iluria.loadView("/clientServices.do?command=saveClientPasswordForm", data, 
		function(response, responseStatus) {
			if(responseStatus == 32001) {
				 alert("A senha atual está incorreta."); 
				
				
				
				
				
			}
			else 
				 alert("Senha atualizada com sucesso."); 
				
				
				
				
				
			Iluria.clientAccount.showClientPasswordForm();
	}, "POST");
}
 
Iluria.clientAccount.saveClientTestimonialForm = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.clientAccount.width, Iluria.clientAccount.height);
	Iluria.loadView("/clientServices.do?command=saveClientTestimonialForm", data, 
		function(response, responseStatus) {
			if(responseStatus == 32003) {
				alert("O depoimento não pode estar em branco.");
			}
			else if(responseStatus == 32004) {
				alert("O apelido não pode estar em branco.");
			}
			else 
				 alert("Recebemos seu depoimento com sucesso e após a aprovação ele será publicado em nossa loja.\n\nObrigado!"); 
				
				
				
				
				
			
			Iluria.clientAccount.showClientTestimonialForm();
	}, "POST");
}

Iluria.clientAccount.showClientPasswordForm = function () {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/clientServices.do?command=showClientPasswordForm", 
		function(response) {
			Iluria.showView(response);			
	});
}

Iluria.clientAccount.showClientTestimonialForm = function () {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/clientServices.do?command=showClientTestimonialForm", 
		function(response) {
			Iluria.showView(response);			
	});
}

Iluria.clientAccount.showClientOrderHistoryForm = function () {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/clientServices.do?command=showClientOrderHistoryForm", 
		function(response) {
			Iluria.showView(response);			
	});
}

Iluria.clientAccount.showClientOrderDetailsForm = function (orderId) {
	Iluria.showViewProgress(Iluria.cart.width, Iluria.cart.height);
	Iluria.loadView("/clientServices.do?command=showClientOrderDetailsForm&webCode=" + orderId, 
		function(response) {
			Iluria.showView(response);
	});
}

Iluria.clientAccount.saveClientOrderMessage = function(orderId) {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.clientAccount.width, Iluria.clientAccount.height);
	Iluria.loadView("/clientServices.do?command=saveClientOrderMessage", data, 
		function(response, responseStatus) {
			Iluria.loadView("/clientServices.do?command=showClientOrderDetailsForm&webCode=" 
				+ orderId + "&tabId=2", 
			function(response) {
				Iluria.showView(response);
		});
	}, "POST");
}

Iluria.showView = function(html) {
	$("#iluria-view-container").html(html);
}

Iluria.loadView = function(url, data, callback, method) {
	if(typeof(data) == "function") {
		callback = data;
	}
	if(method == null) method = "GET";
	$.ajax({
		url: url,
		data: data,
		type: method,
		async: true,
		cache: false,
		contentType : "application/x-www-form-urlencoded; charset=utf-8",
		success: function(responseData, status, xhr) {
			callback.call(this, responseData, xhr.status);
		},
		error: function(xhr, status, error) {
			callback.call(this, null, xhr.status);
		}
	});
}

Iluria.showViewProgress = function(width, height) {
	$("#iluria-view-container").html("<div style='width: " 
		+ width + "px; height: " + height 
		+ "px; overflow: auto'><table cellpadding=0 cellspacing=0 width='" 
		+ width + "' height='" 
		+ height + "'><tr><td valign='center' align='center'><img src=\
		'/webCommons/img/spinner2.gif'></td></tr></table></div>");
}

Iluria.initView = function(width, height, callback) {
	var view = $("#iluria-view");

	if(view.size() == 0)
		$("body").append("<div id='iluria-view' style='display: none'/>");

	$("#iluria-view").html("<div style='overflow: auto; width:" + (width+20) + "px; height: " + height + "px;'><div id='iluria-view-container' style='width: " 
							+ width + "px; height4: " + height 
							+ "px; overflow: auto'><table cellpadding=0 cellspacing=0 width='" 
							+ width + "' height='" 
							+ height + "'><tr><td valign='center' align='center'><img src=\
							'/webCommons/img/spinner2.gif'></td></tr></table></div></div>");
							
	$("#iluria-view").prettyPhoto({
		theme: 'facebook', 
		hideflash: 'true',
		allowresize: false,
		showTitle: false,
		default_width: width+20,
		default_height: height,
		changepicturecallback: function() {
			$("#iluria-view").html("");
			callback.call(this);
		}
	});
	$.prettyPhoto.open('#iluria-view');
}

Iluria.trim = function(str) {
	if(typeof(str) == "undefined") str = "";
	return str.replace(/^\s+|\s+$/g,"");
}

Iluria.showContactForm = function () {
	

	Iluria.initView(Iluria.clientAccount.width, Iluria.clientAccount.height, function() {
		Iluria.loadView("/clientServices.do?command=showContactForm", 
			function(response) {
				Iluria.showView(response);			
		});
	});
}

Iluria.saveContactForm = function() {
	var form = $("#clientDataForm");
	var data = form.serialize();
	Iluria.showViewProgress(Iluria.clientAccount.width, Iluria.clientAccount.height);
	Iluria.loadView("/clientServices.do?command=saveContactForm", data, 
		function(response, responseStatus) {
			$.prettyPhoto.close();
			if(responseStatus == 32005) {
				 alert("A mensagem não pode estar em branco."); 
				
				
				
				
				
			}
			else if(responseStatus == 32006) {
				 alert("O email não pode estar em branco."); 
				
				
				
				
				
			}
			else {
				 alert("Recebemos sua mensagem com sucesso e em breve ela será respondida."); 
				
				
				
				
				
			}
	}, "POST");
}

