/*********************************
 * Themes, rules, and i18n support
 * Locale: Chinese; 
 *********************************/
(function ($) {
	
	var validatorRoot;
	$('script[src*=validator]').each(function(){
		var s=this.src;
		if(s.match(/jquery.validator[^\/]*\.js/i)){validatorRoot=s.replace(/[\?#].*$/, '').replace(/(^|[\/\\])[^\/]*$/, '$1');return false;}
	});
	var validatorcss=validatorRoot+"jquery.validator.css"
	$('head').append('<link rel="stylesheet" type="text/css" href="'+validatorcss+'" />');
    /* Global configuration
     */
    $.validator.config({
        //stopOnError: true,//ִֹͣж
        //theme: 'yellow_right_effect',
        defaultMsg: "{0}ʽȷ",
        loadingMsg: "֤...",
        
        // Custom rules
        rules: {
            digits: [/^\d+$/, ""]
			,dot: [/^\d{1,4}(?:\.\d{0,2})?$/, ""]//С֧
            ,letters: [/^[a-z]+$/i, "{0}ֻĸ"]
            ,tel: [/^(?:(?:0\d{2,3}[- ]?[1-9]\d{6,7})|(?:[48]00[- ]?[1-9]\d{6}))$/, "绰ʽȷ"]
            ,mobile: [/^1[3-9]\d{9}$/, "ֻŸʽȷ"]
            ,email: [/^[\w\+-]+(\.[\w\+-]+)*@[a-z\d-]+(\.[a-z\d-]+)*\.([a-z]{2,4})$/, "ʽȷ"]
            ,qq: [/^[1-9]\d{4,}$/, "QQŸʽȷ"]
            ,date: [/^\d{4}-\d{1,2}-\d{1,2}$/, "ȷ,:yyyy-mm-dd"]
            ,time: [/^([01]\d|2[0-3])(:[0-5]\d){1,2}$/, "ȷʱ,:14:3014:30:00"]
            ,ID_card: [/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$/, "ȷ֤"]
            ,url: [/^(https?|ftp):\/\/[^\s]+$/i, "ַʽȷ"]
            ,postcode: [/^[1-9]\d{5}$/, "ʽȷ"]
            ,chinese: [/^[\u0391-\uFFE5]+$/, ""]
            ,username: [/^\w{5,20}$/, "5-20λ֡ĸ»"]
            ,password: [/^[0-9a-zA-Z.]{6,16}$/, "6-16λ֡ĸ"]
			,fieldname: [/^\w{2,20}$/, "2-20λ֡ĸ»"]
			,nickname: [/^[0-9a-zA-Z\u0391-\uFFE5]{2,10}$/, "ǳ2-10ַ,"]
            ,accept: function (element, params){
                if (!params) return true;
                var ext = params[0];
                return (ext === '*') ||
                       (new RegExp(".(?:" + (ext || "png|jpg|jpeg|gif") + ")$", "i")).test(element.value) ||
                       this.renderMsg("ֻ{1}׺", ext.replace('|', ','));
            }
            
        }
    });

    /* Default error messages
     */
    $.validator.config({
        messages: {
            required: "{0}Ϊ",
            remote: "{0}ѱʹ",
            integer: {
                '*': "",
                '+': "",
                '+0': "0",
                '-': "븺",
                '-0': "븺0"
            },
            match: {
                eq: "{0}{1}һ",
                neq: "{0}{1}ͬ",
                lt: "{0}С{1}",
                gt: "{0}{1}",
                lte: "{0}Сڻ{1}",
                gte: "{0}ڻ{1}"
            },
            range: {
                rg: "{1}{2}",
                gt: "ڻ{1}",
                lt: "Сڻ{1}"
            },
            checked: {
                eq: "ѡ{1}",
                rg: "ѡ{1}{2}",
                gt: "ѡ{1}",
                lt: "ѡ{1}"
            },
            length: {
                eq: "{1}ַ",
                rg: "{1}{2}ַ",
                gt: "{1}ַ",
                lt: "{1}ַ",
                eq_2: "",
                rg_2: "",
                gt_2: "",
                lt_2: ""
            }
        }
    });

    /* Themes
     */
    var TPL_ARROW = '<span class="n-arrow"><b></b><i></i></span>';
    $.validator.setTheme({
        'simple_right': {
            formClass: 'n-simple',
            msgClass: 'n-right'
        },
        'simple_bottom': {
            formClass: 'n-simple',
            msgClass: 'n-bottom'
        },
        'yellow_top': {
            formClass: 'n-yellow',
            msgClass: 'n-top',
            msgArrow: TPL_ARROW
        },
		'yellow_bottom': {
            formClass: 'n-yellow',
            msgClass: 'n-bottom',
            msgArrow: TPL_ARROW
        },
        'yellow_right': {
            formClass: 'n-yellow',
            msgClass: 'n-right',
            msgArrow: TPL_ARROW
        },
        'yellow_right_effect': {
            formClass: 'n-yellow',
            msgClass: 'n-right',
			//showOk: '',
            msgArrow: TPL_ARROW,
            msgShow: function($msgbox, type){
                var $el = $msgbox.children();
                if ($el.is(':animated')) return;
                if (type === 'error') {
                    $el.css({
                        left: '20px',
                        opacity: 0
                    }).delay(100).show().stop().animate({
                        left: '-4px',
                        opacity: 1
                    }, 150).animate({
                        left: '3px'
                    }, 80).animate({
                        left: 0
                    }, 80);
                } else {
                    $el.css({
                        left: 0,
                        opacity: 1
                    }).fadeIn(200);
                }
            },
            msgHide: function($msgbox, type){
                var $el = $msgbox.children();
                $el.stop().delay(100).show().animate({
                    left: '20px',
                    opacity: 0
                }, 300, function(){
                   $msgbox.hide();
                });
            }
        }
    });
})(jQuery);