(function(){ /* * "Class" for select and option html generation */ var SelectElement = (function() { function SelectElement(name, id) { this.select = document.createElement("select"); } SelectElement.prototype.setAttr = function(attr, value) { return this.select.setAttribute(attr, value); }; SelectElement.prototype.addOption = function(option) { return this.select.add(option); }; SelectElement.prototype.getSelect = function() { return this.select; }; SelectElement.generateOption = function(value, text) { var option; option = document.createElement("option"); option.setAttribute("value", value); option.text = text; return option; }; return SelectElement; })(); /* * "Class" that switch state field between input and select * If the Country if Brazil, set state to select field * else set it as a input field */ var SelectFieldChoices = (function() { function SelectFieldChoices(state_id, city_id, state_url) { this.state_id = state_id; this.input_html = jQuery(state_id).parent().html(); this.old_value = jQuery(state_id).val(); this.city_parent_div = jQuery(city_id).parent().parent().parent(); this.state_url = state_url; } SelectFieldChoices.prototype.getCurrentStateElement = function() { return jQuery(this.state_id); }; SelectFieldChoices.prototype.replaceWith = function(html) { var parent_div = this.getCurrentStateElement().parent(); parent_div.html(html); }; SelectFieldChoices.prototype.generateSelect = function(state_list) { var select_element, option; select_element = new SelectElement(); select_element.setAttr("name", "profile_data[state]"); select_element.setAttr("id", "state_field"); select_element.setAttr("class", "type-select valid"); state_list.forEach(function(state) { option = SelectElement.generateOption(state, state); select_element.addOption(option); }); return select_element.getSelect(); }; SelectFieldChoices.prototype.replaceStateWithSelectElement = function() { var klass = this; jQuery.get(this.state_url, function(response) { var select_html; if (response.length > 0) { select_html = klass.generateSelect(response); klass.replaceWith(select_html); if (klass.old_value.length !== 0 && response.include(klass.old_value)) { klass.getCurrentStateElement().val(klass.old_value); } } }); }; SelectFieldChoices.prototype.replaceStateWithInputElement = function() { this.replaceWith(this.input_html); }; SelectFieldChoices.prototype.hideCity = function() { this.city_parent_div.addClass("mpog_hidden_field"); }; SelectFieldChoices.prototype.showCity = function() { this.city_parent_div.removeClass("mpog_hidden_field"); }; SelectFieldChoices.prototype.actualFieldIsInput = function() { return this.getCurrentStateElement().attr("type") === "text"; }; return SelectFieldChoices; })(); function set_form_count_custom_data() { var divisor_option = SelectElement.generateOption("-1", "--------------------------------"); var default_option = SelectElement.generateOption("BR", "Brazil"); jQuery('#profile_data_country').find("option[value='']").remove(); jQuery('#profile_data_country').prepend(divisor_option); jQuery('#profile_data_country').prepend(default_option); jQuery('#profile_data_country').val("BR"); } function set_initial_form_custom_data(selectFieldChoices) { set_form_count_custom_data(); jQuery("#password-balloon").html(jQuery("#user_password_menssage").val()); jQuery("#profile_data_email").parent().append(jQuery("#email_public_message").remove()); if( jQuery("#state_field").length !== 0 ) selectFieldChoices.replaceStateWithSelectElement(); } function check_reactivate_account(value, input_object){ jQuery.ajax({ url : "/plugin/mpog_software/check_reactivate_account", type: "GET", data: { "email": value }, success: function(response) { if( jQuery("#forgot_link").length === 0 ) jQuery(input_object).parent().append(response); else jQuery("#forgot_link").html(response); }, error: function(type, err, message) { console.log(type+" -- "+err+" -- "+message); } }); } function put_brazil_based_on_email(){ var suffixes = ['gov.br', 'jus.br', 'leg.br', 'mp.br']; var value = this.value; var input_object = this; var gov_suffix = false; suffixes.each(function(suffix){ var has_suffix = new RegExp("(.*)"+suffix+"$", "i"); if( has_suffix.test(value) ) { gov_suffix = true; jQuery("#profile_data_country").val("BR"); } }); jQuery("#profile_data_country").find(':not(:selected)').css('display', (gov_suffix?'none':'block')); check_reactivate_account(value, input_object); } function validate_email_format(){ var correct_format_regex = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/; if( this.value.length > 0 ) { if(correct_format_regex.test(this.value)) this.className = "validated"; else this.className = "invalid"; } else this.className = ""; } function verify_user_password_size() { if( this.value.length < 6 ) { jQuery(this).switchClass("validated", "invalid"); } else { jQuery(this).switchClass("invalid", "validated"); } } function show_or_hide_phone_mask() { if(jQuery("#profile_data_country").val() == "BR") { if( (typeof jQuery("#profile_data_cell_phone").data("rawMaskFn") === 'undefined') ) { jQuery("#profile_data_cell_phone").mask("(99) 9999?9-9999"); jQuery("#profile_data_comercial_phone").mask("(99) 9999?9-9999"); jQuery("#profile_data_contact_phone").mask("(99) 9999?9-9999"); } } else { jQuery("#profile_data_cell_phone").unmask(); jQuery("#profile_data_comercial_phone").unmask(); jQuery("#profile_data_contact_phone").unmask(); } } function fix_phone_mask_format(id) { jQuery(id).blur(function() { var last = jQuery(this).val().substr( jQuery(this).val().indexOf("-") + 1 ); if( last.length == 3 ) { var move = jQuery(this).val().substr( jQuery(this).val().indexOf("-") - 1, 1 ); var lastfour = move + last; var first = jQuery(this).val().substr( 0, 9 ); jQuery(this).val( first + '-' + lastfour ); } }); } // Sorry, I know its ugly. But I cant get ([^\w\*\s*])|(^|\s)([a-z]|[0-9]) // to ignore Brazilian not so much special chars in names function replace_some_special_chars(text) { return text.replace(/([áàâãéèêíïóôõöú])/g, function(value){ if( ["á","à","â","ã"].indexOf(value) != -1 ) return "a"; else if( ["é","è","ê"].indexOf(value) != -1 ) return "e"; else if( ["í","ï"].indexOf(value) != -1 ) return "i"; else if ( ["ó","ô","õ","ö"].indexOf(value) != -1 ) return "o"; else if( ["ú"].indexOf(value) != -1 ) return "u"; else return value; }); } function invalid_name_validation(text) { if( text.trim().length === 0 ) { return true; } var full_validation = /([^\w\*\s*])|(^|\s)([a-z]|[0-9])/; // no special chars and do not initialize with no capital latter var partial_validation = /[^\w\*\s*]/; // no special chars text = replace_some_special_chars(text); var slices = text.split(" "); var invalid = false; for(var i = 0; i < slices.length; i++) { if( slices[i].length > 3 || text.length <= 3 ) { invalid = full_validation.test(slices[i]); } else { invalid = partial_validation.test(slices[i]); } if(invalid) break; } return invalid; } // Generic function show_plugin_error_message(field_selector, hidden_message_id ) { var field = jQuery(field_selector); field.removeClass("validated").addClass("invalid"); if(!jQuery("." + hidden_message_id)[0]) { var message = jQuery("#" + hidden_message_id).val(); field.parent().append("