﻿$(document).ready(function() {

    var strRegEx = "<(.| )+?>";
    var regEx = new RegExp(strRegEx);
    //轉換語系前的檢查 
    $('#ctl00_UcMasterTopCtrl1_languageMenun1').click(function() {
        //$("input").not(":password").each(function() {
        $("input").each(function() {
            if (regEx.test($(this).val()))
                $(this).val("");
        });
        $("textarea").each(function() {
            if (regEx.test($(this).val()))
                $(this).val("");
        });
    });
    $('#ctl00_UcMasterTopCtrl1_languageMenun2').click(function() {
        //$("input").not(":password").each(function() {
        $("input").each(function() {
            if (regEx.test($(this).val()))
                $(this).val("");
        });
        $("textarea").each(function() {
            if (regEx.test($(this).val()))
                $(this).val("");
        });
    });
    $('#ctl00_UcMasterTopCtrl1_languageMenun3').click(function() {
        //$("input").not(":password").each(function() {
        $("input").each(function() {
            if (regEx.test($(this).val()))
                $(this).val("");
        });
        $("textarea").each(function() {
            if (regEx.test($(this).val()))
                $(this).val("");
        });
    });


    var msg;
    try
    {
        switch ($.cookie("asukabookLanguage")) {
        case "zh-CHT":
            msg = "<div class='error' >請勿輸入HTML標籤.</div>"
            break;
        case "zh-CHS":
            msg = "<div class='error' >Please don't enter html tag.</div>"
            break;
        default:
            msg = "<div class='error' >Please don't enter html tag.</div>"
        }
    }
    catch(err)
    {
    msg = "<div class='error' >Please don't enter html tag.</div>"
    }
   



    //$("input").not(":password").bind("keyup", function() {
    $("input").bind("keyup", function() {
        var me = $(this);
        if (regEx.test(me.val())) {
            if (!me.hasClass("warningTag")) {
                me.addClass("warningTag");
                $(msg).insertAfter(me);
            }
        }
        else {
            $(this).removeClass("warningTag");
            $(this).next("div.error").remove();
        }
    });
    $("textarea").bind("keyup", function() {
        var me = $(this);
        if (regEx.test(me.val())) {
            if (!me.hasClass("warningTag")) {
                me.addClass("warningTag");
                $(msg).insertAfter(me);
            }
        }
        else {
            $(this).removeClass("warningTag");
            $(this).next("div.error").remove();
        }
    });
    $("form").submit(function() {
        var result = true;
        //password html encode
        // $(":password").encHTML();
        //other html checjing
        //$("input").not(":password").each(function() {
        $("input").each(function() {
            var me = $(this);
            if (regEx.test(me.val())) {
                result = false;
                if (!me.hasClass("warningTag")) {
                    me.addClass("warningTag");
                    $(msg).insertAfter(me);
                }
            }
            else {
                $(this).removeClass("warningTag");
                $(this).next("div.error").remove();
            }
        });
        $("textarea").each(function() {
            var me = $(this);
            if (regEx.test(me.val())) {
                result = false;
                if (!me.hasClass("warningTag")) {
                    me.addClass("warningTag");
                    $(msg).insertAfter(me);
                }
            }
            else {
                $(this).removeClass("warningTag");
                $(this).next("div.error").remove();
            }
        });
        return result;
    });
});
