(function($){
    $.fn.validationEngineLanguage = function(){
    };
    $.validationEngineLanguage = {
        newLang: function(){
            $.validationEngineLanguage.allRules = {
                "required": { // Add your regex rules here, you can take telephone as an example
                    "regex": "none",
                    "alertText": "Acest camp este obligatoriu!",
                    "alertTextCheckboxMultiple": "Selectati o optiune",
                    "alertTextCheckboxe": "Bifarea acestui camp este obligatorie pentru a putea continua"
                },
                "minSize": {
                    "regex": "none",
                    "alertText": "Minimum ",
                    "alertText2": " caractere permise!"
                },
                "maxSize": {
                    "regex": "none",
                    "alertText": "Maximum ",
                    "alertText2": " caractere permise!"
                },
                "groupRequired": {
                    "regex": "none",
                    "alertText": "You must fill one of the following fields"
                },
                "min": {
                    "regex": "none",
                    "alertText": "Valoarea minima este "
                },
                "max": {
                    "regex": "none",
                    "alertText": "Valoarea maxima este "
                },
                "past": {
                    "regex": "none",
                    "alertText": "Data inainte de "
                },
                "future": {
                    "regex": "none",
                    "alertText": "Data dupa "
                },
                "maxCheckbox": {
                    "regex": "none",
                    "alertText": "Limita maxima de optiuni a fost depasita"
                },
                "minCheckbox": {
                    "regex": "none",
                    "alertText": "Selectati cel putin ",
                    "alertText2": " optiuni"
                },
                "equals": {
                    "regex": "none",
                    "alertText": "Campurile nu coincid"
                },
                "phone": {
                    // credit: jquery.h5validate.js / orefalo
                    "regex": /^([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/,
                    "alertText": "Numar de telefon eronat"
                },
                "email": {
                    // Shamelessly lifted from Scott Gonzalez via the Bassistance Validation plugin http://projects.scottsplayground.com/email_address_validation/
                    "regex": /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,
                    "alertText": "Adresa de email este invalida!"
                },
                "vodafone": {
                    "regex": /^07[0-9]{8}$/i,
                    "alertText": "Numarul de telefon mobil trebuie sa inceapa cu \"07\" si sa fie format din 10 cifre!"
                },
                "tel": {
                    "regex": /^[0-9]{10}$/i,
                    "alertText": "Numarul de telefon trebuie sa fie format din 10 cifre!"
                },
                "nume": {
                    "regex": /^[a-z\ \-]{3,50}$/i,
                    "alertText": "Numele trebuie sa contina doar litere si sa aiba intre 3 si 50 de caractere!"
                },
                
                "label_cnp": {
                    "regex": "none",
                    "alertText": "CNP trebuie sa aiba 13 cifre!",
                    "alertText2": "CNP invalid!",
                    "alertText3": "Format invalid!"
                },
                
                "label_cui": {
                    "regex": "none",
                    "alertText": "CUI invalid!"
                },

                // Not working.
                // Use function 'cnp' instead which is at the end of this file.
                /*
                "cnp": {
                    "regex": /^[1-8]\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])(0[1-9]|[1-4]\d|5[0-2]|99)\d{4}$/,
                    "alertText": "Numele trebuie sa contina doar litere si sa aiba intre 3 si 50 de caractere!"
                },
                */
               
                "digits": {
                    "regex": /^[0-9]+$/i,
                    "alertText": "Acest camp trebuie sa contina doar cifre!"
                },
                "integer": {
                    "regex": /^[\-\+]?\d+$/,
                    "alertText": "Numar intreg eronat"
                },
                "number": {
                    // Number, including positive, negative, and floating decimal. credit: orefalo
                    "regex": /^[\-\+]?(([0-9]+)([\.,]([0-9]+))?|([\.,]([0-9]+))?)$/,
                    "alertText": "Numar zecimal eronat"
                },
                "date": {
                    "regex": /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/,
                    "alertText": "Data eronata, formatul de introducere este: YYYY-MM-DD"
                },
                "ipv4": {
                    "regex": /^((([01]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))[.]){3}(([0-1]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))$/,
                    "alertText": "Adresa IP eronata"
                },
                "url": {
                    "regex": /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i,
                    "alertText": "URL eronat"
                },
                "onlyNumberSp": {
                    "regex": /^[0-9\ ]+$/,
                    "alertText": "Doar numere"
                },
                "onlyLetterSp": {
                    "regex": /^[a-zA-Z\ \']+$/,
                    "alertText": "Doar litere"
                },
                "onlyLetterNumber": {
                    "regex": /^[0-9a-zA-Z]+$/,
                    "alertText": "Caracterele speciale (',', '.', '-', etc) nu sunt permise"
                },
                // --- CUSTOM RULES -- Those are specific to the demos, they can be removed or changed to your likings
                "ajaxUserCall": {
                    "url": "ajaxValidateFieldUser",
                    // you may want to pass extra data on the ajax call
                    "extraData": "name=eric",
                    "alertText": "Acest nume de utilizator este deja folosit",
                    "alertTextLoad": "Se valideaza, va rugam asteptati"
                },
                "ajaxNameCall": {
                    // remote json service location
                    "url": "ajaxValidateFieldName",
                    // error
                    "alertText": "Acest nume este deja folosit",
                    // if you provide an "alertTextOk", it will show as a green prompt when the field validates
                    "alertTextOk": "Acest nume este disponibil",
                    // speaks by itself
                    "alertTextLoad": "Se valideaza, va rugam asteptati"
                },
                "validate2fields": {
                    "alertText": "Introduceti: HELLO"
                }
            };

        }
    };
    $.validationEngineLanguage.newLang();
})(jQuery);


function cnp(field, rules, i, options){
    
    //  if (field.val() != "HELLO") {
    //     // this allows the use of i18 for the error msgs
    //     return options.allrules.validate2fields.alertText;
    //  }

    
    // Conversie din jQuery object in DOM object
    var el = field[0];
    
    // credits to Adrian Carstea
    // Partea de validare CNP
    if(!/^\d{13}$/.test(el.value)){
        
        // this allows the use of i18 for the error msgs
        return options.allrules.label_cnp.alertText;
    }
		
    //C este cifra de control (un cod autodetector) aflata in relație cu toate celelate 12 cifre ale CNP-ului. 
    //Cifra de control este calculata dupa cum urmeaza: fiecare cifra din CNP este inmultita cu cifra de pe 
    //aceeasi pozitie din numarul 279146358279; rezultatele sunt insumate, iar rezultatul final este impartit cu rest la 11. 
    //Daca restul este 10, atunci cifra de control este 1, altfel cifra de control este egala cu restul.

    var cifra_control = "279146358279";
    var cifreCNP = el.value.match(/\d(?=\d)/g);
    var cifreControl = cifra_control.match(/\d/g);
    //1780309463048

    if(cifreCNP.length == 12){
        var suma = 0;
        
        for(i=0; i<cifreCNP.length; i++){
            suma += cifreCNP[i] * cifreControl[i];
        }
										
        if(suma>0){
            
            if( (suma%11 == 10 && el.value.substr(12,1)==1) || (suma%11 == el.value.substr(12,1)) ) {
                // CNP valid
                //return options.allrules.label_cnp.alertText2;
            } else {
                return options.allrules.label_cnp.alertText2;
            }

        } else {
            return options.allrules.label_cnp.alertText2;
        }
        
    } else {
        return options.allrules.label_cnp.alertText3;
    }
    
}

//O versiune revizuita a functiei JavaScript a lui Alex.
//Suporta si coduri CIF cu RO in fata
function cui(field, rules, i, options) {

    var el = field[0],
        s = el.value;
    if (parseInt(s)!=s) // CIF is of form ROxxxxxxxxx
    {
        if (s.substr(0,2).toUpperCase()!='RO' || s.length > 12)
            return options.allrules.label_cui.alertText;

        s = s.substr(2, s.length); //Extract only the numeric content
    }
    else // CIF is only numeric
    {
        if (s.length > 10)
            return options.allrules.label_cui.alertText;
    }

    cifraControl = s.charAt(s.length - 1);
    content = s.substr(0, s.length - 1);
    while (content.length < 9)
    {
        content = '0' + content;
    }
    suma = content.charAt(0) * 7 + content.charAt(1) * 5 + content.charAt(2)
            * 3 + content.charAt(3) * 2 + content.charAt(4) * 1
            + content.charAt(5) * 7 + content.charAt(6) * 5 + content.charAt(7)
            * 3 + content.charAt(8) * 2;
    suma = suma * 10;
    rest = suma % 11;
    if (rest == 10)
        rest = 0;

    if (rest == cifraControl) {
    }
    else
        return options.allrules.label_cui.alertText;

}

