﻿// http://plugins.jquery.com/project/validity
// http://validity.thatscaptaintoyou.com/Demos/index.htm

$.extend($.validity.patterns, {
			date:/^([012]\d|30|31)[.-\/]([01]\d)[.-\/]\d{1,4}$/,
            phone:/^(([2-9]\d{2}-\d{3}-\d{4})|(([+]358|0)\d{5,10}))$/,
			ssn:/^\d{6}[+-A]\d{3}[a-zA-Z0-9]$/,
			cc:/^\d{7}[-]\d{1}$/,
			pdfdocdocx:/^((.*)\.(pdf|PDF|doc|DOC|docx|DOCX))$/
});

$.extend($.validity.messages, {
            require:"#{field} on pakollinen tieto.",
            // Format validators:
            match:"Tämä tieto on virheellinen.",
			ssn:"Virheellinen henkilötunnus, täytyy olla muodossa PPKKVVZXXXY",
			cc:"Virheellinen y-tunnus, täytyy olla muodossa XXXXXXX-Y",
            integer:"#{field} täytyy olla kokonainen positiivinen luku.",
            date:"#{field} täytyy olla kelvollinen päivämäärä.",
            email:"Virheellinen sähköpostiosoite.",
            usd:"#{field} must be formatted as a US Dollar amount.",
            url:"Virheellinen URL muotoilu.",
            number:"#{field} täytyy olla kelvollinen numero.",
            zip:"Virheellinen postinumero, täytyy olla muodossa #####.",
            phone:"Virheellinen puhelinnumero, täytyy olla muodossa +358401234567.",
            time24:"Virheellinen kellonajan muotoilu, täytyy olla muodossa 23:00.",
			pdfdocdocx:"#{field} kenttä sallii vain tiedostomuodot: PDF, DOC ja DOCX",

            // Value range messages:
            lessThan:"#{field} täytyy olla pienempi kuin #{max}.",
            lessThanOrEqualTo:"#{field} täytyy olla vähemmän tai yhtä paljon kuin #{max}.",
            greaterThan:"#{field} täytyy olla isompi kuin #{min}.",
            greaterThanOrEqualTo:"#{field} täytyy olla isompi tai yhtä paljon kuin #{min}.",
            range:"#{field} täytyy olla väliltö #{min} ja #{max}.",

            // Value length messages:
            tooLong:"#{field} ei voi olla yli #{max} merkkiä.",
            tooShort:"#{field} ei voi olla alle #{min} merkkiä.",

            // Composition validators:
            nonHtml:"#{field} ei saa sisältää HTML merkkejä.",
            alphabet:"#{field} sisältää virheellisiä merkkejä.",

            minCharClass:"#{field} ei voi sisältää enemmän kuin #{min} #{charClass} merkkiä.",
            maxCharClass:"#{field} ei voi sisältää vähemmän kuin #{min} #{charClass} merkkiä.",
            
            // Aggregate validator messages:
            equal:"Arvot eivät täsmää.",
            distinct:"Arvo on jo käytössä.",
            sum:"Arvot eivät täsmää luvulle #{sum}.",
            sumMax:"Yhteenlasketun summan tulee olla alle #{max}.",
            sumMin:"Yhteenlasketun summan tulee olla yli #{min}.",

            // Radio validator messages:
            radioChecked:"Valittu arvo on virheellinen.",
            
            generic:"Virheellinen."
});

$.validity.setup({ 
			defaultFieldName:"Tämä",
			outputMode:"summary"  // "summary", "label", "modal"
});






// Install the summary output
(function($) {
    var 
        // Container contains the summary. This is the element that is shown or hidden.
        container = "#contact-validity-summary-container",
        
        // Erroneous refers to an input with an invalid value,
        // not the error message itself.
        erroneous = "validity-erroneous",
        
        // Selector for erroneous inputs.
        errors = "." + erroneous,
        
        // The wrapper for entries in the summary.
        wrapper = "<li/>",

        // Buffer to contain all the error messages that build up during validation.
        // When validation ends, it'll be flushed into the summary.
        // This way, the summary doesn't flicker empty then fill up.
        buffer = [];

    $.validity.outputs.contactSummary = {
        start:function() {
            $(errors).removeClass(erroneous);
            buffer = [];
        },

        end:function(results) {
            // Hide the container and empty its summary.
            $(container)
                .hide()
                .find("ul")
                    .html('');

            // If there are any errors at all:
            // (Otherwise the container shouldn't be shown):
            if (buffer.length) {
                // Use integer based iteration for solution to Issue 7.
                for (var i = 0; i < buffer.length; ++i) {
                    $(wrapper)
                        .text(buffer[i])
                        .appendTo(container + " ul");
                }

                $(container).show();
                
                // If scrollTo is enabled, scroll the page to the first error.
                if ($.validity.settings.scrollTo) {
                    location.hash = $(errors + ":eq(0)").attr("id");
                }
            }
        },

        raise:function($obj, msg) {
            buffer.push(msg);
            $obj.addClass(erroneous);
        },

        raiseAggregate:function($obj, msg) {
            this.raise($obj, msg);
        },
        
        container:function() {
            document.write(
                "<div class=\"validity-summary-container\">" + 
                    "The form didn't submit for the following reason(s):" +
                    "<ul></ul>" +
                "</div>"
            );
        }
    };
})(jQuery);

