﻿/*
* jQuery myfun plugin
* Version 1.0 (12/22/2008)
* @requires jQuery v1.2.6 or later
*
* Jquery 自定义函数插件，提供一些公共的操作方法 
*/

/*
* 说明：为jQuery对象添加扩展方法
* 调用方法：$("#aa").method()
*/

/**
* 统计和限制TextArea的字符个数
*/
(function($) {
    jQuery.fn.extend({
        showTextareaWordCount: function(isbind, objDiv) {
            // alert(if(arguments[0]);
            //debugger;
            var div;
            if (arguments.length == 0)
                div = $("<div style='position:absolute;font-size:12px;color:#4586b5;width:50px;text-align:right;display:none'></div>")
					.insertAfter(this);
            else
                div = objDiv;

            // 必须指定一个textarea的max属性，整数
            var _max = $(this).attr('maxlength');
            var _length = $(this).text().length;
            if (_length > _max) {
                $(this).text($(this).text().substring(0, _max));
            }
            _left = $(this).offset().left;
            _top = $(this).offset().top;
            _width = $(this).width();
            _height = $(this).height();

            div.html(_length + '/' + _max);
            div.css({
                'left': _left + _width - 60,
                'top': _top + _height - 8
            });

            if (arguments.length == 0) {
                $(this).bind('propertychange', {
                    obj: $(this),
                    objDiv: div
                }, function(event) {
                    // debugger;
                    // alert(event.data.obj.attr("cols"));
                    event.data.obj.showTextareaWordCount("true", event.data.objDiv);
                    // $(tobjDiv: divhis).
                });

                $(this).bind('focus', {
                    obj: $(this),
                    objDiv: div
                }, function(event) {
                    event.data.obj.showTextareaWordCount("true", event.data.objDiv);
                    event.data.objDiv.fadeIn('slow');
                });

                $(this).bind('blur', {
                    obj: $(this),
                    objDiv: div
                }, function(event) {
                    event.data.obj.showTextareaWordCount("true", event.data.objDiv);
                    event.data.objDiv.fadeOut('slow');
                });
            }

            /*
            * focus(function(){ $(this).showWordCount();
            * $('#div_1').fadeIn('slow'); });
            * 
            * $(this).blur(function(){ $('#div_1').fadeOut('slow'); });
            */

        }
    });
})(jQuery);

/*
* 说明：创建自定义命名空间方法函数 调用方法：$.myfun.methd();
*/

(function($) {

    $.myfun = {

        test: function() {
            alert("fsdf");
        },
        htmlEncode: function(text) {
            if (typeof (text) != "string")
                text = text.toString();

            text = text.replace(/&/g, "&amp;");
            text = text.replace(/"/g, "&quot;");
            text = text.replace(/</g, "&lt;");
            text = text.replace(/>/g, "&gt;");
            text = text.replace(/'/g, "&#39;");
            text = text.replace(/ /g, "&nbsp;");
            text = text.replace(/\n\r/g, "<br />");
            text = text.replace(/\r\n/g, "<br />");
            text = text.replace(/\n/g, "<br />");
            text = text.replace(/\r/g, "<br />");
            return text;
        },
        htmlDecode: function(text) {
            if (typeof (text) != "string")
                text = text.toString();
            text = text.replace(/<[Bb][Rr]\s*(\/)?>/g, "\r");
            text = text.replace(/&amp;/g, "&");
            text = text.replace(/&quot;/g, '"');
            text = text.replace(/&lt;/g, "<");
            text = text.replace(/&gt;/g, ">");
            text = text.replace(/&#39;/g, "'");
            text = text.replace(/&nbsp;/g, " ");

            return text;
        },
        /*
        * 对字符串进行Url编码，默认采用utf-8编码 @param: str 要编码的字符串
        */
        urlEncode: function(str) {
            return escape(str);
        },
        /*
        * 对字符串进行Url编码，默认采用utf-8编码 @param: str 要解码的字符串
        */
        urlDecode: function(str) {
            return unescape(str);
        },
        /*
        * 获取url参数 @param: strname:参数名称
        */
        getParaStr: function(strname) {
            var pageUrl = document.location.href;
            pageUrl = pageUrl.substring(pageUrl.indexOf("?") + 1);
            var para = pageUrl.split("&");
            var tempstr = "";
            for (var i = 0; i < para.length; i++) {
                tempstr = para[i].split("=");
                if (strname == tempstr[0]) {
                    return tempstr[1];
                }
            }
            return "";
        },

        /*
        *获取url字符串变量
        *参数名称
        */
        getUrlQueryString: function() {
            var pageUrl = document.location.href;
            if (pageUrl.indexOf("?") == -1) {
                return "";
            } else {
                pageUrl = pageUrl.substring(pageUrl.indexOf("?") + 1);
            }
            return $.trim(pageUrl);
        },

        /*
        * 对一个Url添加参数，格式: a=b&c=d 如果参数名称相同，则替换已存在的参数的值 @param: url @param: pkey
        * @param: pvalue
        */
        addUrlPara: function(url, pkey, pvalue) {
            if (url == null)
                return;

            if (url.length == 0)
                return "";

            if (url.indexOf("?") == -1) {
                url += "?" + pkey + "=" + pvalue;
            } else if (url.indexOf("?") == (url.length - 1)) {
                url += pkey + "=" + pvalue;
            } else {
                var query = url.substring(url.indexOf("?") + 1);
                var para = query.split("&");
                var tempstr;
                for (var i = 0; i < para.length; i++) {
                    tempstr = para[i].split("=");
                    if (tempstr[0] == pkey) {
                        return url.replace(pkey + "=" + tempstr[1], pkey + "="
								+ pvalue);
                    }
                }
                url += "&" + pkey + "=" + pvalue;
            }

            return url;
        },
        /*
        * 生成一个javascript GUID
        */
        createGUID: function() {
            function S4() {
                return (((1 + Math.random()) * 0x10000) | 0).toString(16)
						.substring(1);
            }

            return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-"
					+ S4() + S4() + S4());

        },
        /*
        * 格式化数字为固定长度，不够前面不零 例如: padNum(3,2)格式化后为: 03 @param num : 需要格式化的数字或字符
        * @param n: 长度
        */
        padNum: function(num, n) {
            var len = num.toString().length;
            while (len < n) {
                num = '0' + num;
                len++;
            }
            return num;
        },

        /*
        * 生成一个当前日期的字符串 yyyy-MM-dd格式
        */
        createDateTimeString: function() {
            var a = new Date();
            var y = this.padNum(a.getYear(), 4) + "";
            var m = this.padNum(a.getMonth(), 2) + "";
            var d = this.padNum(a.getDay(), 2) + "";
            var h = this.padNum(a.getHours(), 2) + "";
            var x = this.padNum(a.getMinutes(), 2) + "";
            var s = this.padNum(a.getSeconds(), 2) + "";
            var ms = a.getMilliseconds();
            return y + m + d + h + x + s + ms;
        },

        /*
        * 去除html标签 要控制被删除的标签列表,可以通过向TAGLIST常数中添加/删除标记来实现. 例如,要保留所有的<B>标签,则从TAGLIST中删除B.
        * 当前的列表包含了MSDN中的所有html标签以及 LAYER 标签. 每个标签要用";"括起来. 开始标签和结束标签都会被删除,例如"<A...>"和</A...>
        * 若标签同时在 TAGLIST 和 BLOCKTAGLIST 常数中,则起始标签和结束标签之间的所有内容都会被删除
        * 没有结束标记的标签不被视为html标签,其内容不会被删除 块标签若没有结尾标记,从此标签开始到文本结束的所有内容会被删除 若"<!--"后跟的字符不是空格,注释标签不会被删除
        * @param: strHtml 要去除Html代码的字符串 @return: 返回没有html代码的字符串
        */
        removeHTML: function(strHtml) {
            //            String.prototype._indexOf = String.prototype.indexOf;
            //            String.prototype.indexOf = function() {
            //                if (typeof (arguments[arguments.length - 1]) != 'boolean')
            //                    return this._indexOf.apply(this, arguments);
            //                else {
            //                    var bi = arguments[arguments.length - 1];
            //                    var thisObj = this;
            //                    var idx = 0;
            //                    if (typeof (arguments[arguments.length - 2]) == 'number') {
            //                        idx = arguments[arguments.length - 2];
            //                        thisObj = this.substr(idx);
            //                    }

            //                    var re = new RegExp(arguments[0], bi ? 'i' : '');
            //                    var r = thisObj.match(re);
            //                    return r == null ? -1 : r.index + idx;
            //                }
            //            }

            var TAGLIST = "";
            TAGLIST += ";!--;!DOCTYPE;A;ACRONYM;ADDRESS;APPLET;AREA;B;BASE;BASEFONT;";
            TAGLIST += "BGSOUND;BIG;BLOCKQUOTE;BODY;BR;BUTTON;CAPTION;CENTER;CITE;CODE;";
            TAGLIST += "COL;COLGROUP;COMMENT;DD;DEL;DFN;DIR;DIV;DL;DT;EM;EMBED;FIELDSET;";
            TAGLIST += "FONT;FORM;FRAME;FRAMESET;HEAD;H1;H2;H3;H4;H5;H6;HR;HTML;I;IFRAME;IMG;";
            TAGLIST += "INPUT;INS;ISINDEX;KBD;LABEL;LAYER;LAGEND;LI;LINK;LISTING;MAP;MARQUEE;";
            TAGLIST += "MENU;META;NOBR;NOFRAMES;NOSCRIPT;OBJECT;OL;OPTION;P;PARAM;PLAINTEXT;";
            TAGLIST += "PRE;Q;S;SAMP;SCRIPT;Select;SMALL;SPAN;STRIKE;STRONG;STYLE;SUB;SUP;";
            TAGLIST += "TABLE;TBODY;TD;TEXTAREA;TFOOT;TH;THEAD;TITLE;TR;TT;U;UL;VAR;WBR;XMP;";

            var BLOCKTAGLIST = ";APPLET;EMBED;FRAMESET;HEAD;NOFRAMES;NOSCRIPT;OBJECT;SCRIPT;STYLE;";

            var nPos1 = 0, nPos2 = 0, nPos3 = 0;
            var strResult = "", strTagName = "";
            var bRemove, bSearchForBlock;

            nPos1 = strHtml.indexOf("<");
            while (nPos1 > -1) {
                nPos2 = strHtml.indexOf(">", nPos1 + 1);
                if (nPos2 > -1) {
                    strTagName = strHtml.substr(nPos1 + 1, nPos2 - nPos1 - 1);
                    strTagName = strTagName.replace(/\r/ig, " ").replace(
							/\n/ig, " ");

                    nPos3 = strTagName.indexOf(" ");
                    if (nPos3 > -1) {
                        strTagName = strTagName.substr(0, nPos3);
                    }

                    if (strTagName.substr(0, 1) == "/") {
                        strTagName = strTagName.substr(1);
                        bSearchForBlock = false;
                    } else {
                        bSearchForBlock = true;
                    }

                    if (TAGLIST.indexOf(";" + strTagName + ";", 1, true) > -1) {
                        bRemove = true;
                        if (bSearchForBlock == true) {
                            if (BLOCKTAGLIST.indexOf(";" + strTagName + ";", 1,
									true) > -1) {
                                nPos2 = strHtml.length;
                                nPos3 = strHtml.indexOf("</" + strTagName,
										nPos1 + 1, true);
                                if (nPos3 > 0)
                                    nPos3 = strHtml.indexOf(">", nPos3 + 1);
                                if (nPos3 > 0)
                                    nPos2 = nPos3;
                            }
                        }
                    } else {
                        bRemove = false;
                    }

                    if (bRemove == true) {
                        strResult = strResult + strHtml.substr(0, nPos1);
                        strHtml = strHtml.substr(nPos2 + 1);
                    } else {
                        strResult = strResult + strHtml.substr(0, nPos1 + 1);
                        strHtml = strHtml.substr(nPos1 + 1);
                    }
                } else {
                    strResult = strResult + strHtml;
                    strHtml = "";
                }
                nPos1 = strHtml.indexOf("<");
            }

            strResult = strResult + strHtml;
            return strResult;
        },

        /**
        打开window.ModalDialog对话框
        **/
        openModalDlg: function(sPath, oArgs, iX, iY, left, top) {

            var __ie6 = /msie 6/i.test(window.navigator.userAgent);
            //在实际部署使用时，应打开下面一行代码的注释。
            //
            //因为站点一般不会被用户设置为Local intranet，所以IE6模态窗口中会出现状态栏，
            //并且IE6的模态窗口高度指整个窗口的高度，而不是窗口中可用显示区域的高度。
            //因此状态栏会挡住窗口下部的界面，影响使用。
            //
            if (__ie6) iY += 20;

            //IE7中的dialogWidth和dialogHeight参数的值所指的宽和高，不包括窗口的标题栏和外边框。IE6包括。
            //可惜客户端界面风格无法通过脚本获得。在WinXP和WinClassic风格中，标题栏的高度是不同的。
            if (__ie6) {
                iX += 6;
                iY += 32;   //WinXP界面风格
                //iY += 25; //WinClassic界面风格
            }

            return window.showModalDialog(sPath, oArgs, "dialogWidth:" + iX + "px;dialogHeight:" + iY + "px;dialogLeft:" + left + "px;dialogTop:" + top + "px;help:0;status:0;scroll:0;center:0");
        },

        //以下方法需要Jquery UI 插件的支持
        /**
        显示对话框
        */
        showDialogNoButtons: function(title, text, width, height) {
            width = width || 250;
            height = height || 140;
            var div = $("<div title=\"" + title + "\"><p style='margin:10px; text-align:center;'><span class=\"ui-icon ui-icon-alert\" style=\"float:left; margin:0 7px 20px 0;\"></span></p></div>");
            div.find("p").append(text);
            div.dialog({
                resizable: false,
                width: width,
                height: height,
                modal: true,
                close: function(event, ui) {
                    $(this).dialog();
                }
            });
        },
        showDialogAlert: function(title, text, width, height) {
            width = width || 250;
            height = height || 140;
            var div = $("<div title=\"" + title + "\"><p style='margin:10px; text-align:center;'><span class=\"ui-icon ui-icon-alert\" style=\"float:left; margin:0 7px 20px 0;\"></span></p></div>");
            div.find("p").append(text);
            div.dialog({
                resizable: false,
                width: width,
                height: height,
                modal: true,
                buttons: {
                    '确  定': function() {
                        $(this).dialog('close');
                    }
                },
                close: function(event, ui) {
                    $(this).dialog('destory');
                }
            });
        },
        showDialogMessage: function(title, text, width, height) {
            width = width || 250;
            height = height || 140;
            var div = $("<div title=\"" + title + "\"><p style='margin:10px; text-align:center;'><span class=\"ui-icon ui-icon-circle-check\" style=\"float:left; margin:0 7px 20px 0;\"></span></p></div>");
            div.find("p").append(text);
            div.dialog({
                resizable: false,
                width: width,
                height: height,
                modal: true,
                buttons: {
                    '确  定': function() {
                        $(this).dialog('close');
                    }
                },
                close: function(event, ui) {
                    $(this).dialog('destory');
                }
            });
        },
        showDialogConfirm: function(title, text, width, height, callBak) {
            width = width || 250;
            height = height || 140;
            var div = $("<div title=\"" + title + "\"><p style='margin:10px; text-align:center;'><span class=\"ui-icon ui-icon-notice\" style=\"float:left; margin:0 7px 20px 0;\"></span></p></div>");
            div.find("p").append(text);
            div.dialog({
                resizable: false,
                width: width,
                height: height,
                modal: true,
                buttons: {
                    '取  消': function() {
                        $(this).dialog('close');
                        callBak(false);
                    },
                    '确  定': function() {
                        $(this).dialog('close');
                        callBak(true);
                    }
                },
                close: function(event, ui) {
                    $(this).dialog('destory');
                }
            });
        },
        /**
        显示内嵌Iframe框架的对话框
        */
        showIframeDialog: function(title, src, width, height, para, callBak) {
            width = width || 600;
            height = height || 400;
            para = para || {};
            isModal = para.isModal != undefined ? para.isModal : true;
            isResize = para.isResize != undefined ? para.isResize : true;
            isAllowRefresh = para.isAllowRefresh != undefined ? para.isAllowRefresh : true;
            src = this.addUrlPara(src, "random", Math.random());
            //<iframe src="http://m.weather.com.cn/m/pn6/weather.htm " width="140" height="20" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" allowTransparency="true"></iframe>

            //var iframId = "if" + Math.random();
            var str = "<div title=\"" + title + "\" style=\"text-align:center;\">";
            str += "<div class=\"ui-ajaxloading\" style=\"position:relative; position:absolute; margin:" + ((parseInt(height, 10) / 2) - 33) + "px 0 0 " + ((parseInt(width, 10) / 2) - 33) + "px; width:66px; height:66px;\"></div>";
            str += "<iframe width=\"" + width + "\" height=\"" + height + "\" marginwidth=\"0\" marginheight=\"0\" hspace=\"0\" vspace=\"0\" frameborder=\"0\" scrolling=\"auto\" >";
            str += "</iframe></div>";
            //<p style='margin:10px; text-align:center;'><span class=\"ui-icon ui-icon-notice\" style=\"float:left; margin:0 7px 20px 0;\"></span></p></div>"
            var iframe = $(str);
            iframe.dialog({
                resizable: isResize,
                width: width + 30,
                height: height + 70,
                modal: isModal,
                buttons: {
                    '关闭': function() {
                        $(this).dialog('close');
                    },
                    '刷新': function() {
                        this.re = this.re == undefined ? isAllowRefresh : this.re;
                        if (!this.re)
                            return false;
                        var ifr = $(this).find("iframe")[0];
                        var div = $(this).find("div");
                        div.show();
                        $(ifr).attr("src", src);
                        if (ifr.attachEvent) {
                            ifr.attachEvent("onload", function() {
                                div.hide();
                            });
                        } else {
                            ifr.onload = function() {
                                div.hide();
                            };
                        }
                    }
                },
                resizeStop: function(event, ui) {
                    iframe.find("iframe").css("height", (ui.size.height - 90) + "px");
                },
                open: function(event, ui) {

                    var ifr = $(this).find("iframe")[0];
                    var div = $(this).find("div");
                    div.show();
                    $(ifr).attr("src", src);
                    if (ifr.attachEvent) {
                        ifr.attachEvent("onload", function() {
                            div.hide();
                        });
                    } else {
                        ifr.onload = function() {
                            div.hide();
                        };
                    }
                },
                close: function(event, ui) {
                    var ifr = $(this).find("iframe")[0];
                    $(ifr).attr("src", "");
                    $(this).dialog('destory');
                }
            });
            return iframe;
        }

    };

})(jQuery);


