// Copyright 2011 Bitban Technologies, S.L.
// Todos los derechos reservados.
//ECMComercio JS Functions

Namespace.Register("ECMComercio");

ECMComercio.new_captcha = function (jObj) {
	var _image = new Image();
	var imageurl = "/bbtcaptcha/captcha?rand=" + Math.round(Math.random() * 1000);
	_image.src = imageurl;
	jObj.find(".captcha").empty();
	jObj.find(".captcha").append(_image);
};

ECMComercio.register = function (jObj) {

        jObj.ajaxForm({
	    beforeSerialize: function($form, options) {
				var jform = jQuery($form);
				var errors = BBT.validation.validate(jObj);

				var isValid = true;
				jQuery.each(errors, function (field, _errors) {
						if (field == "ecm_registerProfile-password" || field == "ecm_registerProfile-passwordChk"){
							jQuery(".id-" + field).addClass("bb-field_required");
							jQuery.fancybox({
									autoDimensions: true,
										centerOnScroll: true,
										scrolling: 'no',
										titleShow: false,
										transitionIn : 'none',
										transitionOut : 'none',
										content: "<div id='userRegMessages' class='cp-lightboxed-users'><p class='text error'>Su contraseña debe tener al menos 6 caracteres</p></div>"
										});
						}
						jQuery("#" + field).addClass("bb-field_required");
						isValid = false;
					});
				if (isValid == false) {
					jQuery(".message-error").show();
					return false;
				}
			},
	    success: function(_data) {
				var data = BBT.fixJson(_data);
				if (data["error"] == 0) {
					jQuery.fancybox({
							autoDimensions: true,
								centerOnScroll: true,
								scrolling: 'no',
								titleShow: false,
								transitionIn : 'none',
								transitionOut : 'none',
								content: "<div id='userWellcome' class='cp-lightboxed-users'><h2 class='title'>Bienvenido/a " + data['message'] + "</h2><p class='text'>Usted se ha registrado exitosamente en nuestro Sitio Web, a continuación recibirá un correo electrónico con el que debe activar su cuenta y así podrá acceder a nuestros servicios.</p><p class='text'>Gracias por suscribirse,</p><p class='text'><strong>Elcomercio.com</strong><br />el punto de la información.</p></div>",
								onClosed: function() {
								window.location.href="/";
							}
						});
				} else {
					ECMComercio.new_captcha(jObj);
					jQuery.fancybox({
							autoDimensions: true,
								centerOnScroll: true,
								scrolling: 'no',
								titleShow: false,
								transitionIn : 'none',
								transitionOut : 'none',
								content: "<div id='userRegMessages' class='cp-lightboxed-users'><p class='text error'>" + data['message'] + "</p></div>"
					});
					return false;
				}
			}
		});
        jObj.submit();
};

ECMComercio.update = function (jObj) {

        jObj.ajaxForm({
	    beforeSerialize: function($form, options) {
				var jform = jQuery($form);
				var errors = BBT.validation.validate(jObj);

				var isValid = true;
				jQuery.each(errors, function (field, _errors) {
						jQuery("#" + field).addClass("bb-field_required");
						isValid = false;
					});
				if (isValid == false) {
					jQuery(".message-error").show();
					return false;
				}
			},
	    success: function(_data) {
				var data = BBT.fixJson(_data);
				if (data["error"] == 0) {
					jQuery.fancybox({
							autoDimensions: true,
								centerOnScroll: true,
								scrolling: 'no',
								titleShow: false,
								transitionIn : 'none',
								transitionOut : 'none',
								content: "<div id='userRegMessages' class='cp-lightboxed-users'><p class='text'>Las modificaciones a su perfil se han guardado exitosamente.</p></div>",
								onClosed: function() {
								window.location.reload();
							}
						});
				} else if (data["error"] == 3) {
					jQuery.fancybox({
							autoDimensions: true,
								centerOnScroll: true,
								scrolling: 'no',
								titleShow: false,
								transitionIn : 'none',
								transitionOut : 'none',
								content: "<div id='userRegMessages' class='cp-lightboxed-users'><p class='text error'>No tiene permiso para modificar este usuario.</p></div>",
								onClosed: function() {
								window.location.href="/";
							}
					});
				} else {
					jQuery.fancybox({
							autoDimensions: true,
								centerOnScroll: true,
								scrolling: 'no',
								titleShow: false,
								transitionIn : 'none',
								transitionOut : 'none',
								content: "<div id='userRegMessages' class='cp-lightboxed-users'><p class='text error'>Lo sentimos. Las modificaciones a su perfil no fueron realizadas. Por favor, inténtelo más tarde.</p></div>"
					});
					return false;
				}
			}
		});
        jObj.submit();
};

ECMComercio.new_captcha2 = function (jObj) {
	var _image = new Image();
	var imageurl = "/bbtcaptcha/captcha?rand=" + Math.round(Math.random() * 1000);
	_image.src = imageurl;
	jObj.find(".img-captcha").empty();
	jObj.find(".img-captcha").append(_image);
};

ECMComercio.recommend = function (jObj) {
        jObj.ajaxForm({
	    beforeSerialize: function($form, options) {
				var jform = jQuery($form);
				var errors = BBT.validation.validate(jObj);
				var errorsExist = false;
				jQuery.each(errors, function (field, _errors) {
						jQuery("#" + field).addClass("bb-field_required");
						errorsExist = true;
					});
				if (errorsExist) {
					jQuery(".message-error").show();
					FBSE.releasePassword(jObjForm);
				} else {
					jQuery(".message-error").hide();
				}
			},

	    success: function(_data) {
				var data = BBT.fixJson(_data);
				if (data["error"] == 0) {
					jQuery.fancybox({
							autoDimensions: true,
								centerOnScroll: true,
								scrolling: 'no',
								titleShow: false,
								transitionIn : 'none',
								transitionOut : 'none',
								content: "<div id='blogRecommendMessages' class='cp-lightboxed-users'><p class='text'>Su recomendación ha sido enviada exitosamente.</p></div>",
								onClosed: function() {
								window.location.href="/blogs/";
							}
						});
				} else {
					ECMComercio.new_captcha2(jObj);
					return false;
				}
			}
		});
        jObj.submit();
};

ECMComercio.contact = function (jObj) {
        jObj.ajaxForm({
			beforeSerialize: function($form, options) {
				var jform = jQuery($form);
				var errors = BBT.validation.validate(jObj);
				var isValid = true;

				jQuery.each(errors, function (field, _errors) {
						jQuery("#" + field).addClass("bb-field_required");
						isValid = false;
					});

				if (isValid == false) {
					jQuery(".message-error").show();					
					FBSE.releasePassword(jObjContact);
					return false;
				}
			},
			success: function(_data) {
				var data = BBT.fixJson(_data);
				
				if (data["error"] == 0) {
					jQuery.fancybox({
					     autoDimensions: true,
					     centerOnScroll: true,
					     scrolling: 'no',
					     titleShow: false,
					     transitionIn : 'none',
								transitionOut : 'none',
								content: "<div id='correctNWSMessages' class='cp-lightboxed-users'><p class='text'>El envio se realizó correctamente.</p></div>",
					     onClosed: function() {
								window.location.reload();
					     }
					 });
				} else {
					jQuery.fancybox({
					     autoDimensions: true,
					     centerOnScroll: true,
					     scrolling: 'no',
					     titleShow: false,
					     transitionIn : 'none',
					     transitionOut : 'none',
					     content: "<div id='userRegMessages' class='cp-lightboxed-users'><p class='text error'>" + data['message'] + "</p></div>",
					     onClosed: function() {}
					 });
					jQuery(".input-captcha input").val("");
					FBSE.releasePassword(jObjForm);
					ECMComercio.new_captcha2(jObj);
					return false;
				}
			}
		});
        jObj.submit();
};

ECMComercio.sendFriend = function (jObj) {

        jObj.ajaxForm({
			beforeSerialize: function($form, options) {
				var jform = jQuery($form);
				var errors = BBT.validation.validate(jObj);
				var isValid = true;

				jQuery.each(errors, function (field, _errors) {
						isValid == false;
					});
				if (isValid == false) {
					jQuery.fancybox({
					     autoDimensions: true,
					     centerOnScroll: true,
					     scrolling: 'no',
					     titleShow: false,
					     transitionIn : 'none',
					     transitionOut : 'none',
					     content: "<div id='correctNWSMessages' class='cp-lightboxed-users'><p class='text'>Porfavor rellene todos los campos.</p></div>",
					     onClosed: function() {}
					 });
					return false;
				}
			},
	    success: function(_data) {
				var data = BBT.fixJson(_data);
				if (data["error"] == 0) {
					jQuery.fancybox({
					     autoDimensions: true,
					     centerOnScroll: true,
					     scrolling: 'no',
					     titleShow: false,
					     transitionIn : 'none',
					     transitionOut : 'none',
					     content: "<div id='correctNWSMessages' class='cp-lightboxed-users'><p class='text'>El envio se realizó correctamente.</p></div>",
					     onClosed: function() {}
					 });
				} else {
					jQuery.fancybox({
					     autoDimensions: true,
					     centerOnScroll: true,
					     scrolling: 'no',
					     titleShow: false,
					     transitionIn : 'none',
					     transitionOut : 'none',
					     content: "<div id='correctNWSMessages' class='cp-lightboxed-users'><p class='text'>" + data["message"]  + "</p></div>",
					     onClosed: function() {}
					 });
					jQuery(".input-captcha input").val("");
					ECMComercio.new_captcha2(jObj);
					return false;
				}
			}
		});
        jObj.submit();
};


ECMComercio.correction = function (jObj) {
        jObj.ajaxForm({
	    success: function(_data) {
				var data = BBT.fixJson(_data);

				if (data["error"] == 0) {
					jQuery.fancybox({
					     autoDimensions: true,
					     centerOnScroll: true,
					     scrolling: 'no',
					     titleShow: false,
					     transitionIn : 'none',
					     transitionOut : 'none',
					     content: "<div id='correctNWSMessages' class='cp-lightboxed-users'><p class='text'>El envio se realizó correctamente.</p></div>",
					     onClosed: function() {}
					 });
				} else {
					jQuery.fancybox({
					     autoDimensions: true,
					     centerOnScroll: true,
					     scrolling: 'no',
					     titleShow: false,
					     transitionIn : 'none',
					     transitionOut : 'none',
					     content: "<div id='correctNWSMessages' class='cp-lightboxed-users'><p class='text'>Hubo problemas en el envio. Inténtelo más tarde.</p></div>",
					     onClosed: function() {}
					 });
					jQuery(".input-captcha input").val("");
					ECMComercio.new_captcha2(jObj);
					return false;
				}
			}
		});
        jObj.submit();
};
