﻿function isValidEmailAddress(emailAddress) {
    var filter = /^([a-zA-Z0-9_\-\.\+]*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return filter.test(emailAddress);
};

$(document).ready(function () {
    $('input.ac-off').attr('autocomplete', 'off');
});

$.fn.ValidateEmailRegister = function (emailFieldClass, msgErroClass, frmId, text, onError) {

    $(emailFieldClass).val(text);
    $(this).submit(function () {

        var email = $(emailFieldClass).val();

        if (!isValidEmailAddress(email)) {
            $(msgErroClass).show();
            $(emailFieldClass).addClass(onError);
            return false;
        } else {
            return true;
        }
    });

    $(msgErroClass).hide();

    $(emailFieldClass).blur(function () {
        if (this.value == '') {
            this.value = text;
        }
    });

    $(emailFieldClass).focus(function () {
        if (this.value == text) {
            this.value = '';
        }
    });
}

//gets a localized text string based on the country/locale of the client
//this function is useful if dynamic js needs to show localized text
function getLocalizedString(key) {
    //use a special meta tag set by the server to detect the locale of the client
    var data = $("meta[name='currentCountryId']").attr("content");
    // 1 => Brazil (default), 2 => Argentina, 3 => Mexico
    var currentCountryId = 1;
    if (data && data.length > 0) {
        currentCountryId = parseInt(data);
    }

    var localizedStrings = getLocalizedStringArray(currentCountryId);

    return (localizedStrings[key] && localizedStrings[key].length > 0) ? localizedStrings[key] : null;
}

function getLocalizedStringArray(countryId) {
    if (!countryId) {
        countryId = 1; //default is portuguese
    }
    var locStrings = Array();
    switch (countryId) {
        case 3: //mexico
        case 2: //argentina
            locStrings['fb-connect-pwd-error'] = 'Necesita su contraseña de Pez Urbano para poder conectarse con su cuenta de Facebook.';            
            break;
        case 1: //brazil
        default:
            locStrings['fb-connect-pwd-error'] = 'Digite sua senha do Peixe Urbano para conectar-se à conta do Facebook.';
            break;
    }

    return locStrings;
}
