Skip to content

Instantly share code, notes, and snippets.

@fernandofleury
Created February 4, 2014 13:00
Show Gist options
  • Save fernandofleury/8803168 to your computer and use it in GitHub Desktop.
Save fernandofleury/8803168 to your computer and use it in GitHub Desktop.
var app = app || {};
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
}
app.sendRequest = function(action, data, callSuccess, callError) {
jQuery.support.cors = true;
var data = typeof data != 'undefined' ? data : {};
$.ajax({
url: "facade/WebService.ashx?op=" + action,
data: JSON.stringify(data),
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data == null) {
alertBox('error', internalError[getLang()]);
return;
}
if (typeof callSuccess != 'undefined') {
callSuccess(data);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (typeof callError != 'undefined') {
callError(XMLHttpRequest, textStatus, errorThrown);
}
}
});
}
app.alertBox = function(type, message, time) {
if ($('.alert-box').length > 0) {
$('.alert-box').remove();
}
if (!time) {
time = 5000;
}
var wrapper = document.getElementsByTagName('body')[0];
var alertNode = document.createElement('div');
var textNode = document.createTextNode(message);
alertNode.appendChild(textNode);
alertNode.className = 'alert-box' + ' ' + 'alert-' + type;
wrapper.appendChild(alertNode);
$('.alert-box:last').fadeIn();
function alertRemove(element) {
$(element).fadeOut('300', function () {
$(this).remove();
});
};
var timedRemove = setTimeout(function () {
alertRemove('.alert-box:last');
}, time);
$('body').on('click', '.alert-box', function () {
alertRemove(this);
});
}
app.loader = function(param) {
var loader = document.querySelector('.loader'),
text = document.querySelector(".text");
if(param === 'hide') {
loader.removeAttribute('style');
text.style.display = "none";
clearInterval(counter);
} if(param === 'show') {
loader.style.width = '100%';
text.style.display = "block";
counter = window.setInterval( function() {
var dots = document.querySelector(".dots");
if ( dots.innerHTML.length > 2 )
dots.innerHTML = "";
else
dots.innerHTML += ".";
}, 500);
}
}
app.validateForm = function(form) {
var $form = form,
$requiredFields = $form.find('[data-required="true"]'),
mailRegex = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
$requiredFields.each(function () {
var $this = $(this),
$element = $this.is('input') ? $this.parent() : $this;
if (!$this.val()) {
$element.addClass('error-field');
} else {
if ($this.is('[data-regex="email"]')) {
if (mailRegex.test($this.val())) {
$element.removeClass('error-field');
} else {
$element.addClass('error-field');
}
} else if ($this.is('[data-confirm]')) {
var toCheck = $this.data('confirm');
var $input = $form.find('[name="' + toCheck + '"]');
if (!($input.parent().hasClass('error-field')) && $this.val() === $input.val()) {
$element.removeClass('error-field');
} else {
$element.addClass('error-field');
}
} else {
$element.removeClass('error-field');
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment