/*
32 space
35,36 init, end
37,39 izq der
50 arroba


*/

function cutChars(e)
{
	if( e.keyCode == 0 || e.keyCode == 60 || e.keyCode == 200 || e.keyCode == 221)
	{
		e.preventDefault();
        return false;
	}
}


function cutCharsNoNums(e)
{
	if( e.keyCode == 0 || e.keyCode == 48 || e.keyCode == 49 || e.keyCode == 50 || e.keyCode == 51 || e.keyCode == 52 || e.keyCode == 53 || e.keyCode == 54 || e.keyCode == 55 || e.keyCode == 56 || e.keyCode == 57 || e.keyCode == 60 || e.keyCode == 200 || e.keyCode == 221)
	{
		e.preventDefault();
        return false;
	}
}




//Solo permite excribir numeros
function onlyNums(e)
{
    if(e.keyCode != 35 && e.keyCode != 36 && e.keyCode != 37 && e.keyCode != 39 && e.keyCode != 48 && e.keyCode != 49 && e.keyCode != 50 && e.keyCode != 51 && e.keyCode != 52 && e.keyCode != 53 && e.keyCode != 54 && e.keyCode != 55 && e.keyCode != 56 && e.keyCode != 57 && e.keyCode != 96 && e.keyCode != 97 && e.keyCode != 98 && e.keyCode != 99 && e.keyCode != 100 && e.keyCode != 101 && e.keyCode != 102 && e.keyCode != 103 && e.keyCode != 104 && e.keyCode != 105 && e.keyCode != 8 && e.keyCode != 46)
    {
        e.preventDefault();
        return false;
    }
}






/**
 * Forces only navigation keys in an input
 * Tab, backspace, enter, arrows, supr.
 *
 * @param keypress event
 */
function nav_keys_only(event){
	return event.keyCode == 8 || event.keyCode==9 || event.keyCode==13 || (event.keyCode>=37 && event.keyCode<=40) || event.keyCode==46;
}

/**
 * Forces only numbers in an input. Accepts also nav keys
 * @param keypress event
 */
function numeric_only(event){
	return (event.charCode >= 48 && event.charCode <= 57) || nav_keys_only(event);
}

/**
 * Forces numbers and ',' in an input. Accepts also nav keys.
 * @param keypress event
 * @id    string
 */
function decimal_only(event,id){
	if((event.charCode == 44 || event.charCode == 46) && ($('#' + id).val().indexOf(",")!= -1 || $('#' + id).val() == '')) return false ;
	if(event.charCode == 46){
		$('#' + id).val($('#' + id).val() + ',');
		 return false;
	}
	return event.charCode == 44 || numeric_only(event);
}

/**
 * Deletes last , in an input field
 */
function decimal_clean(id){
	if($('#' + id).val().slice(-1) == ','){
		$('#' + id).val($('#' + id).val().slice(0, -1));
	}
}

/**
 * Forces a-zA-Z in an input. Accepts also nav keys.
 * @param keypress event
 */
function alpha_only(event){
	return (event.charCode >= 97 && event.charCode <= 122) || (event.charCode >= 65 && event.charCode <= 90) || nav_keys_only(event);
}

/**
 * Forces a-zA-ZñÑçÇáéíóúÁÉÍÓÚïÏüÜàèìòùÀÈÌÒÙ in an input. Accepts also nav keys.
 * @param keypress event
 */
function alpha_spanish_only(event){
	console.log("mandrulia");
	var c = event.charCode;
	// ñÑ
	var ntilde = (c == 241 || c == 209);
	// çÇ
	var ccedil = (c == 231 || c == 199);
	// àèìòùÀÈÌÒÙ
	var open_acute  = (c == 224 || c == 232 || c == 236 || c == 242 || c == 249) || (c == 192 || c == 200 || c == 204 || c == 210 || c == 217);
	// áéíóúÁÉÍÓÚ
	var close_acute = (c == 225 || c == 233 || c == 237 || c == 243 || c == 250) || (c == 193 || c == 201 || c == 205 || c == 211 || c == 218);
	// ïÏüÜ
	var diaeresis = (c == 239 || c == 207 || c == 252 || c == 220)

	var espacio = (c == 32)

	return  ntilde || ccedil || open_acute || close_acute || diaeresis || espacio || alpha_only(event);
}


/**
 * Forces a-zA-ZñÑçÇáéíóúÁÉÍÓÚïÏüÜàèìòùÀÈÌÒÙ0-9 in an input. Accepts also nav keys.
 * @param keypress event
 */
function alpha_spanish_numeric_only(event){
	return alpha_spanish_only(event) || numeric_only(event);
}

/**
 * Forces a-zA-Z0-9 in an input. Accepts also nav keys.
 * @param keypress event
 */
function alpha_numeric_only(event){
	return alpha_only(event) || numeric_only(event);
}

/**
 * Check if values are equal
 * @param text_1
 * @param text_2
 * @returns {Boolean}
 */
function validate_matches(text_1, text_2){
	if(text_1 != text_2){
		return false;
	} else {
		return true;
	}
}

/**
 * Basic function to check if it is empty, undefined or not
 * @param text
 * @returns {Boolean}
 */
function validate_required(text){
	if (typeof text == 'string' || text instanceof String) {
		//text = text.trim();
        text = text.replace(/^\s+|\s+$/gm,'');
	}
	if(typeof text === 'undefined' || text == 'undefined' || text == "" || text == ":" || text == "0/0/0" || text == null) {
		return false;
	} else {
		return true;
	}
}

/**
 * Validates max length, it cannot be higher than the given length
 * @param text
 * @param max
 * @returns {Boolean}
 */
function validate_max_length(text, max){
	if(text){
		if(text.length <= max){
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

/**
 * Validates if the youtube url is real
 * @param url
 * @returns {Boolean}
 */
function validate_youtube_url(url){
	if(url.match(/http:\/\/(?:www\.)?youtube.*watch\?v=([a-zA-Z0-9\-_]+)/)) {
		return true;
	} else {
		return false;
	}
}

/**
 * Validates min length, it cannot be smaller than the given length
 * @param text
 * @param min
 * @returns {Boolean}
 */
function validate_min_length(text, min){
	if(text){
		if(text.length >= min){
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

/**
 * This function validate if it is a real email, well not really only if it is written properly
 * @param email
 * @returns {Boolean}
 */
function validate_valid_email(email){
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	return (email.match(re) && email != "");
}

/**
 * Validates spanish postalcodes
 * @param cp
 * @returns {Boolean}
 */
function validate_spanish_cp(cp){
	return (cp.length == 5 && !isNaN(cp));
}

/**
 * Validates portuguese postalcodes
 * @param cp
 * @returns {Boolean}
 */
function validate_portuguese_cp(cp){
	return (cp.length==8 && !isNaN(cp.replace("-","")) && cp.replace("-","").length == 7 );
}

/**
 * Validates spanish phone numbers
 * @param telf
 * @returns {Boolean}
 */
function validate_spanish_phone(telf){
	var start = telf.slice(0, 1);
	return (telf.length == 9 && !isNaN(telf) && (start >= "6" && start <= "9"));
}

/**
 * Validates portuguese phone numbers
 * @param telf
 * @returns {Boolean}
 */
function validate_portuguese_phone(telf){
	var start = telf.slice(0, 1);
	return (telf.length == 9 && !isNaN(telf) && (start == "2" || start== "9"));
}

/**
 * Check the time is valid
 * @param time
 * @returns {Boolean}
 */
function validate_time(time){
	var time_array = time.split(":");
	return(time != ":" && time_array[1] < 60 && time_array[0] < 24);
}

/**
 * Check the date is not the default or empty
 * @param date
 * @returns {Boolean}
 */
function validate_date(date){
	var date_array = date.split("/");
	return (date != "0/0/0" && date_array[0] != "0" && date_array[1] != "0" && date_array[2] != "0");
}

/**
 * Check if the IBAN number is real
 * @param iban
 * @returns
 */
function validate_IBAN(iban) {
	return IBAN.isValid(iban);
}

/**
 * Returns age for a given birthdate
 * @param birth_day
 * @param birth_month
 * @param birth_year
 * @returns {Number}
 */
function calculates_age(birth_day, birth_month, birth_year){
    date = new Date();
    actual_year = date.getYear();
    actual_month = date.getMonth();
    actual_day = date.getDate();
    age = (actual_year + 1900) - birth_year;
    if (actual_month < (birth_month - 1)){
    	age--;
    }
    if (((birth_month - 1) == actual_month) && (actual_day < birth_day)){
    	age--;
    }
    if (age > 1900){
    	age -= 1900;
    }
    return age;
}


/**
 * Check if the user is on legal age
 * @param date (in Spanish Format) (Legal age in Spain)
 * @returns {Boolean}
 */
function validate_legal_age(date){
	var params = date.split('/');
	var age = calculates_age(params[0], params[1], params[2]);
	console.log("AGE "+ age);
	if(age > 17){
		return true;
	} else {
		return false;
	}
}

/**
 * Return time differences between two given dates
 * @param date_one
 * @param date_two
 * @param type
 * @returns {Number}
 */
function time_diff_between_dates (date_one, date_two, type) {
	if(type == 'days'){
		return Math.floor((date_one.getTime() - date_two.getTime()) / (1000 * 60 * 60 * 24 ));
	} else if(type == 'seconds'){
		return Math.floor((date_one.getTime() - date_two.getTime()) / 1000) ;
	} else if(type == 'minutes'){
		return Math.floor((date_one.getTime() - date_two.getTime()) / (1000 * 60)) ;
	} else if(type == 'hours') {
		return Math.floor((date_one.getTime() - date_two.getTime()) / (1000 * 60 * 60)) ;
	}
}

/**
 * Validates a spanish id card and NIEs
 * @param id_card
 * @returns {Boolean}
 */
function validate_idcard(id_card) {
	var num, let, character;
	var id_card_expression = /^[XYZ]?\d{5,8}[A-Z]$/;
	//We change everything to uppercas
	id_card = id_card.toUpperCase();

	if(id_card_expression.test(id_card) === true){
		num = id_card.substr(0, id_card.length - 1);
		num = num.replace('X', 0);
		num = num.replace('Y', 1);
		num = num.replace('Z', 2);
		id_card.substr(id_card.length - 1, 1);
		num = num % 23;
		character = 'TRWAGMYFPDXBNJZSQVHLCKET';
		character = character.substring(num, num+1);
		if (character != let) {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}

/**
 * Corrects the value in the time inputs, so we help the users to put right the time in those inputs and we avoid errors before validation
 * @param form_id
 * @param input
 */
function input_time_check(form_id, input){
	if($(form_id + " input[name=" + input + "_hours]").length){
		$(form_id + " input[name=" + input + "_hours]").blur(function(){
			var hours = $(this).val();
			if(hours > 23){
				$(this).val(23);
			} else if(hours == ""){
				$(this).val("00");
			} else if(hours < 10 && hours.length != 2){
				$(this).val("0" + hours);
			}
		}).keydown(
			function(event){
				if(event.shiftKey){
					event.preventDefault();
				}
				if (event.keyCode != 46 && event.keyCode != 8 && event.keyCode != 9 && event.keyCode != 13 && !(event.keyCode >= 96 && event.keyCode <= 105) && !(event.keyCode >= 48 && event.keyCode <= 57)){
					event.preventDefault();
				} else if($(this).val() == "00" || $(this).val() == "000") {
					$(this).val("");
				}
			}
		);
	}
	if($(form_id + " input[name=" + input + "_minutes]").length){
		$(form_id + " input[name=" + input + "_minutes]").blur(function(){
			var min = $(this).val();
			if(min > 59){
				$(this).val(59);
			} else if(min == ""){
				$(this).val("00");
			} else if(min < 10 && min.length != 2){
				$(this).val("0" + min);
			}
		}).keydown(
			function(event){
				if(event.shiftKey){
					event.preventDefault();
				}
				if (event.keyCode != 46 && event.keyCode != 8 && event.keyCode != 9 && event.keyCode != 13 && !(event.keyCode >= 96 && event.keyCode <= 105) && !(event.keyCode >= 48 && event.keyCode <= 57)){
					event.preventDefault();
				} else if($(this).val() == "00" || $(this).val() == "000") {
					$(this).val("");
				}
			}
		);
	}
	if($(form_id + " input[name=" + input + "_seconds]").length){
		$(form_id + " input[name=" + input + "_seconds]").blur(function(){
			var sec = $(this).val();
			if(sec > 59){
				$(this).val(59);
			} else if(sec == ""){
				$(this).val("00");
			} else if(sec < 10 && sec.length != 2){
				$(this).val("0" + sec);
			}
		}).keydown(
			function(event){
				if(event.shiftKey){
					event.preventDefault();
				}
				if (event.keyCode != 46 && event.keyCode != 8 && event.keyCode != 9 && event.keyCode != 13 && !(event.keyCode >= 96 && event.keyCode <= 105) && !(event.keyCode >= 48 && event.keyCode <= 57)){
					event.preventDefault();
				} else if($(this).val() == "00" || $(this).val() == "000") {
					$(this).val("");
				}
			}
		);
	}
}

/**
 * A jQuery Plugin to make masks on form fields and HTML elements.
 * https://github.com/igorescobar/jQuery-Mask-Plugin
 *
 * Created by Igor Escobar on 2012-03-10. Please report any bug at http://blog.igorescobar.com
 *
 * Copyright (c) 2012 Igor Escobar http://blog.igorescobar.com
 *
 * The MIT License (http://www.opensource.org/licenses/mit-license.php)
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */
(function (factory) {
    if (typeof define === "function" && define.amd) {
        // AMD. Register as an anonymous module.
        define(["jquery"], factory);
    } else if (typeof exports === 'object') {
        // Node/CommonJS
        factory(require('jquery'));
    } else {
        // Browser globals
        factory(window.jQuery || window.Zepto);
    }
}(function ($) {
    "use strict";
    var Mask = function (el, mask, options) {
        el = $(el);
        var jMask = this, old_value = el.val(), regexMask;
        mask = typeof mask === "function" ? mask(el.val(), undefined, el,  options) : mask;

        var p = {
            invalid: [],
            getCaret: function () {
                try {
                    var sel,
                        pos = 0,
                        ctrl = el.get(0),
                        dSel = document.selection,
                        cSelStart = ctrl.selectionStart;

                    // IE Support
                    if (dSel && navigator.appVersion.indexOf("MSIE 10") === -1) {
                        sel = dSel.createRange();
                        sel.moveStart('character', el.is("input") ? -el.val().length : -el.text().length);
                        pos = sel.text.length;
                    }
                    // Firefox support
                    else if (cSelStart || cSelStart === '0') {
                        pos = cSelStart;
                    }

                    return pos;
                } catch (e) {}
            },
            setCaret: function(pos) {
                try {
                    if (el.is(":focus")) {
                        var range, ctrl = el.get(0);
                        if (ctrl.setSelectionRange) {
                            ctrl.setSelectionRange(pos,pos);
                        } else if (ctrl.createTextRange) {
                            range = ctrl.createTextRange();
                            range.collapse(true);
                            range.moveEnd('character', pos);
                            range.moveStart('character', pos);
                            range.select();
                        }
                    }
                } catch (e) {}
            },
            events: function() {
                el
                .on('keyup.mask', p.behaviour)
                .on("paste.mask drop.mask", function() {
                    setTimeout(function() {
                        el.keydown().keyup();
                    }, 100);
                })
                .on('change.mask', function(){
                    el.data('changed', true);
                })
                .on("blur.mask", function(){
                    if (old_value !== el.val() && !el.data('changed')) {
                        el.trigger("change");
                    }
                    el.data('changed', false);
                })
                // it's very important that this callback remains in this position
                // otherwhise old_value it's going to work buggy
                .on('keydown.mask, blur.mask', function() {
                    old_value = el.val();
                })
                // select all text on focus
                .on('focus.mask', function (e) {
                    if (options.selectOnFocus === true) {
                        $(e.target).select();
                    }
                })
                // clear the value if it not complete the mask
                .on("focusout.mask", function() {
                    if (options.clearIfNotMatch && !regexMask.test(p.val())) { p.val(''); }
                });
            },
            getRegexMask: function() {
                var maskChunks = [], translation, pattern, optional, recursive, oRecursive, r;
                for (var i = 0; i < mask.length; i++) {
                    translation = jMask.translation[mask.charAt(i)];
                    if (translation) {
                        pattern = translation.pattern.toString().replace(/.{1}$|^.{1}/g, "");
                        optional = translation.optional;
                        recursive = translation.recursive;

                        if (recursive) {
                            maskChunks.push(mask.charAt(i));
                            oRecursive = {digit: mask.charAt(i), pattern: pattern};
                        } else {
                            maskChunks.push(!optional && !recursive ? pattern : (pattern + "?"));
                        }

                    } else {
                        maskChunks.push(mask.charAt(i).replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
                    }
                }
                r = maskChunks.join("");

                if (oRecursive) {
                    r = r.replace(new RegExp("(" + oRecursive.digit + "(.*" + oRecursive.digit + ")?)"), "($1)?")
                         .replace(new RegExp(oRecursive.digit, "g"), oRecursive.pattern);
                }
                return new RegExp(r);
            },
            destroyEvents: function() {
                el.off(['keydown', 'keyup', 'paste', 'drop', 'blur', 'focusout', ''].join('.mask '));
            },
            val: function(v) {
                var isInput = el.is('input'),
                    method = isInput ? 'val' : 'text',
                    r;

                if (arguments.length > 0) {
                    if (el[method]() !== v) {
                        el[method](v);
                    }
                    r = el;
                } else {
                    r = el[method]();
                }

                return r;
            },
            getMCharsBeforeCount: function(index, onCleanVal) {
                for (var count = 0, i = 0, maskL = mask.length; i < maskL && i < index; i++) {
                    if (!jMask.translation[mask.charAt(i)]) {
                        index = onCleanVal ? index + 1 : index;
                        count++;
                    }
                }
                return count;
            },
            caretPos: function (originalCaretPos, oldLength, newLength, maskDif) {
                var translation = jMask.translation[mask.charAt(Math.min(originalCaretPos - 1, mask.length - 1))];

                return !translation ? p.caretPos(originalCaretPos + 1, oldLength, newLength, maskDif)
                                    : Math.min(originalCaretPos + newLength - oldLength - maskDif, newLength);
            },
            behaviour: function(e) {
                e = e || window.event;
                p.invalid = [];
                var keyCode = e.keyCode || e.which;
                if ($.inArray(keyCode, jMask.byPassKeys) === -1) {
                    var caretPos = p.getCaret(),
                        currVal = p.val(),
                        currValL = currVal.length,
                        changeCaret = caretPos < currValL,
                        newVal = p.getMasked(),
                        newValL = newVal.length,
                        maskDif = p.getMCharsBeforeCount(newValL - 1) - p.getMCharsBeforeCount(currValL - 1);

                    p.val(newVal);

                    // change caret but avoid CTRL+A
                    if (changeCaret && !(keyCode === 65 && e.ctrlKey)) {
                        // Avoid adjusting caret on backspace or delete
                        if (!(keyCode === 8 || keyCode === 46)) {
                            caretPos = p.caretPos(caretPos, currValL, newValL, maskDif);
                        }
                        p.setCaret(caretPos);
                    }
                    return p.callbacks(e);
                }
            },
            getMasked: function (skipMaskChars) {
                var buf = [],
                    value = p.val(),
                    m = 0, maskLen = mask.length,
                    v = 0, valLen = value.length,
                    offset = 1, addMethod = "push",
                    resetPos = -1,
                    lastMaskChar,
                    check;

                if (options.reverse) {
                    addMethod = "unshift";
                    offset = -1;
                    lastMaskChar = 0;
                    m = maskLen - 1;
                    v = valLen - 1;
                    check = function () {
                        return m > -1 && v > -1;
                    };
                } else {
                    lastMaskChar = maskLen - 1;
                    check = function () {
                        return m < maskLen && v < valLen;
                    };
                }

                while (check()) {
                    var maskDigit = mask.charAt(m),
                        valDigit = value.charAt(v),
                        translation = jMask.translation[maskDigit];
                    if (translation) {
                        if (valDigit.match(translation.pattern)) {
                            buf[addMethod](valDigit);
                             if (translation.recursive) {
                                if (resetPos === -1) {
                                    resetPos = m;
                                } else if (m === lastMaskChar) {
                                	m = resetPos - offset;
                                }
                                if (lastMaskChar === resetPos) { m -= offset; }
                            }
                            m += offset;
                        } else if (translation.optional) {
                            m += offset;
                            v -= offset;
                        } else if (translation.fallback) {
                            buf[addMethod](translation.fallback);
                            m += offset;
                            v -= offset;
                        } else {
                          p.invalid.push({p: v, v: valDigit, e: translation.pattern});
                        }
                        v += offset;
                    } else {
                        if (!skipMaskChars) { buf[addMethod](maskDigit); }
                        if (valDigit === maskDigit) { v += offset; }
                        m += offset;
                    }
                }

                var lastMaskCharDigit = mask.charAt(lastMaskChar);
                if (maskLen === valLen + 1 && !jMask.translation[lastMaskCharDigit]) {
                    buf.push(lastMaskCharDigit);
                }

                return buf.join("");
            },
            callbacks: function (e) {
                var val = p.val(),
                    changed = val !== old_value,
                    defaultArgs = [val, e, el, options],
                    callback = function(name, criteria, args) {
                        if (typeof options[name] === "function" && criteria) {
                            options[name].apply(this, args);
                        }
                    };

                callback('onChange', changed === true, defaultArgs);
                callback('onKeyPress', changed === true, defaultArgs);
                callback('onComplete', val.length === mask.length, defaultArgs);
                callback('onInvalid', p.invalid.length > 0, [val, e, el, p.invalid, options]);
            }
        };


        // public methods
        jMask.mask = mask;
        jMask.options = options;
        jMask.remove = function() {
            var caret = p.getCaret();
            p.destroyEvents();
            p.val(jMask.getCleanVal());
            p.setCaret(caret - p.getMCharsBeforeCount(caret));
            return el;
        };

        // get value without mask
        jMask.getCleanVal = function() {
           return p.getMasked(true);
        };

       jMask.init = function(only_mask) {
            only_mask = only_mask || false;
            options = options || {};
            jMask.byPassKeys = $.jMaskGlobals.byPassKeys;
            jMask.translation = $.jMaskGlobals.translation;
            jMask.translation = $.extend({}, jMask.translation, options.translation);
            jMask = $.extend(true, {}, jMask, options);
            regexMask = p.getRegexMask();

            if (only_mask === false) {
                if (options.placeholder) { el.attr('placeholder' , options.placeholder); }
                // autocomplete needs to be off. we can't intercept events
                // the browser doesn't  fire any kind of event when something is
                // selected in a autocomplete list so we can't sanitize it.
                el.attr('autocomplete', 'off');
                p.destroyEvents();
                p.events();

                var caret = p.getCaret();
                p.val(p.getMasked());
                p.setCaret(caret + p.getMCharsBeforeCount(caret, true));
            } else {
                p.events();
                p.val(p.getMasked());
            }
        };

        jMask.init(!el.is("input"));
    };

    $.maskWatchers = {};
    var HTMLAttributes = function () {
        var input = $(this),
            options = {},
            prefix = "data-mask-",
            mask = input.attr('data-mask');
        if (input.attr(prefix + 'reverse')) { options.reverse = true; }
        if (input.attr(prefix + 'clearifnotmatch')) { options.clearIfNotMatch = true; }
        if (input.attr(prefix + 'selectonfocus') === 'true') { options.selectOnFocus = true; }
        if (notSameMaskObject(input, mask, options)) { return input.data('mask', new Mask(this, mask, options)); }
    },
    notSameMaskObject = function(field, mask, options) {
        options = options || {};
        var maskObject = $(field).data('mask'),
            stringify = JSON.stringify,
            value = $(field).val() || $(field).text();
        try {
            if (typeof mask === "function") {
                mask = mask(value);
            }
            return typeof maskObject !== "object" || stringify(maskObject.options) !== stringify(options) || maskObject.mask !== mask;
        } catch (e) {}
    };

    $.fn.mask = function(mask, options) {
        options = options || {};
        var selector = this.selector,
            globals = $.jMaskGlobals,
            interval = $.jMaskGlobals.watchInterval,
            maskFunction = function() {
                if (notSameMaskObject(this, mask, options)) {
                    return $(this).data('mask', new Mask(this, mask, options));
                }
            };

        $(this).each(maskFunction);

        if (selector && selector !== "" && globals.watchInputs) {
            clearInterval($.maskWatchers[selector]);
            $.maskWatchers[selector] = setInterval(function(){
                $(document).find(selector).each(maskFunction);
            }, interval);
        }
        return this;
    };

    $.fn.unmask = function() {
        clearInterval($.maskWatchers[this.selector]);
        delete $.maskWatchers[this.selector];
        return this.each(function() {
            var dataMask = $(this).data('mask');
            if (dataMask) {
                dataMask.remove().removeData('mask');
            }
        });
    };

    $.fn.cleanVal = function() {
        return this.data('mask').getCleanVal();
    };

    $.applyDataMask = function() {
      $(document).find($.jMaskGlobals.maskElements).filter(globals.dataMaskAttr).each(HTMLAttributes);
    }

    var globals = {
        maskElements: 'input,td,span,div',
        dataMaskAttr: '*[data-mask]',
        dataMask: true,
        watchInterval: 300,
        watchInputs: true,
        watchDataMask: false,
        byPassKeys: [9, 16, 17, 18, 36, 37, 38, 39, 40, 91],
        translation: {
            '0': {pattern: /\d/},
            '9': {pattern: /\d/, optional: true},
            '#': {pattern: /\d/, recursive: true},
            'A': {pattern: /[a-zA-Z0-9]/},
            'S': {pattern: /[a-zA-Z]/}
        }
    };

    $.jMaskGlobals = $.jMaskGlobals || {};
    globals = $.jMaskGlobals = $.extend(true, {}, globals, $.jMaskGlobals);

    // looking for inputs with data-mask attribute
    if (globals.dataMask) { $.applyDataMask(); }

    setInterval(function(){
        if ($.jMaskGlobals.watchDataMask) { $.applyDataMask(); }
    }, globals.watchInterval);
}));
