﻿var WQ = {
    checkInput: function (obj) {
        var element = obj.element;
        var regExp = obj.regExp;
        var msg = obj.msg;
        var hasTip = obj.hasTip;
        var match = obj.match;
        var notMatch = obj.notMatch;
        var min = obj.min;
        var max = obj.max;
        var even;
        if (obj.even == null) {
            even = ["keyup", "blur", "change"];
        }
        else {
            even = obj.even;
        }
        var tipEven;
        var removeTipEven;
        if (hasTip == null) {
            hasTip = false;
            tipEven = function () { };
            removeTipEven = function () { };
        }
        else {
            if (hasTip) {
                $(element).after("<br /><label class='tipMsg' style='color:red'></label>")
                tipEven = function () {
                    $(element).next("br").next("label.tipMsg").html(msg)
                }
                removeTipEven = function () {
                    $(element).next("br").next("label.tipMsg").html("");
                }
            }
            else {
                tipEven = function () { };
                removeTipEven = function () { };
            }
        }

        if ($(element).val() == "") {
            $(element).addClass("notMatchInput");
        }

        function matchEven(value) {
            var isMatched = true;

            if (regExp != null && isMatched) {
                if (regExp.test(value)) {
                    isMatched = true;
                }
                else {
                    isMatched = false;
                }
            }

            if (min == null && isMatched) {
                if (max == null || (max != null && parseFloat(value) <= max)) {
                    isMatched = true;
                }
                else {
                    isMatched = false;
                }
            }
            else if (isMatched) {
                if (min <= parseFloat(value)) {
                    if (max == null || (max != null && parseFloat(value) <= max)) {
                        isMatched = true;
                    }
                    else {
                        isMatched = false;
                    }
                }
                else {
                    isMatched = false;
                }
            }

            if (isMatched) {
                $(element).removeClass("notMatchInput");
                removeTipEven();
                match();
            }
            else {
                $(element).addClass("notMatchInput");
                tipEven();
                notMatch();
            }
            return isMatched;
        }

        for (var i in even) {
            $(element).bind(even[i],
			function () {
			    matchEven($(element).val());
			});
        }
    },

    disableButton: function (button) {
        $(button).addClass("disabledButton");
        $(button).attr("disabled", true);
    },

    enableButton: function (button) {
        $(button).removeClass("disabledButton");
        $(button).attr("disabled", false);
    },

    summaryValueId: 0,

    setInputSummary: function (obj) {
        var txtHtml = "<div class='subTitle'><label>" + obj.title + "</label></div>";

        var label = obj.label;
        var ids = new Array();
        for (var i in label) {
            txtHtml += "<ul class='listItem'>";
            txtHtml += ("<li class='label'><label>" + label[i] + "</label></li>");
            txtHtml += ("<li class='value' id='summaryValue_" + this.summaryValueId + "'>---</li>");
            txtHtml += "</ul>";
            txtHtml += "<hr />";
            ids.push("summaryValue_" + this.summaryValueId);
            this.summaryValueId++;
        }

        var position = obj.position;
        if (position == "after") {
            $(obj.div).prepend(txtHtml);
        }
        else if (position == "before") {
            $(obj.div).prependto(txtHtml);
        }
        else {
            $(obj.div).append(txtHtml);
        }

        var bind = obj.bind;
        var even;
        if (obj.even == null) {
            even = ["keyup", "focus", "blur", "change"];
        }
        else {
            even = obj.even;
        }

        var labelId = new Object();
        for (var i in bind) {
            labelId[$(bind[i]).attr("id")] = ids[i];
        }

        $(bind).each(
        function () {
            var val = $(this).val();
            if (val == "") {
                val = "---";
            }
            $("#" + labelId[$(this).attr("id")]).html(val);
            for (var i in even) {
                $(this).bind(even[i],
                function () {
                    val = $(this).val();
                    if (val == "") {
                        val = "---";
                    }
                    $("#" + labelId[$(this).attr("id")]).html(val);
                })
            }
        });
    },

    omitStr: function (obj, w) {
        var width;
        if (w == null) {
            width = $(obj).width();
        }
        else {
            width = w;
        }
        w -= ($(obj).css("margin-left") + $(obj).css("margin-right") - $(obj).css("padding-left") - $(obj).css("padding-right"));
        var fontSize = $(obj).css("font-size");
        var strHtml = $(obj).html();
        var textSize = this.textSize(fontSize, strHtml);
        if (textSize.width > width) {
            var omitSize = this.textSize(fontSize, "...");
            var spanHtml = "<span style='width:" + (width - omitSize.width) + "px;overflow:hidden;display:inline-block;margin:0px;padding:0px'>" + strHtml + "</span>";
            $(obj).html(spanHtml + "...");
        }
    },

    textSize: function (fontSize, text) {
        $("body").append("<span id='getTextSize'></span>");
        var e = $("#getTextSize");
        $(e).css(
		{
		    "font-size": fontSize
		});

        var result = new Object();
        result.width = $(e).width();
        result.height = $(e).height();

        $(e).html("11" + text);
        result.width = $(e).width() - result.width - 13;
        result.height = $(e).height() - result.height;
        $(e).remove();

        return result;
    },

    cutInput: function (obj) {
        var element = obj.element;
        var maxLen = obj.maxLen;

        var str = $(element).val();
        if (str.length > maxLen - 1) {
            str = str.substring(0, maxLen - 1);
            $(element).val(str);
        }

        $(element).keydown(
        function () {
            str = $(this).val();
            if (str.length > maxLen - 1) {
                str = str.substring(0, maxLen - 1);
                $(element).val(str);
            }
        });
    },

    matchPattern: function ()
    { },

    getNotMatchInputNum: function () {
        var num = 0;
        $(".notMatchInput").each(
        function () {
            num++;
        });
        return num;
    },

    appButtonMouseDownEven: function () {
        $(".appButton").each(
        function () {
            if (!$(this).hasClass("blueButton")) {
                $(this).unbind("mousedown");
                $(this).mousedown(
                function () {
                    $(this).addClass("appButton_mousedown");
                });

                $(this).mouseup(
                function () {
                    $(this).removeClass("appButton_mousedown");
                });

                $(this).hover(
                function () { },
                function () {
                    $(this).removeClass("appButton_mousedown");
                });
            }
        });
    },
    blueButtonMouseDownEven: function () {
        $(".blueButton").each(
        function () {
            $(this).unbind("mousedown");
            $(this).mousedown(
            function () {
                $(this).addClass("blueButton_mousedown");
            });

            $(this).mouseup(
            function () {
                $(this).removeClass("blueButton_mousedown");
            });

            $(this).hover(
            function () { },
            function () {
                $(this).removeClass("blueButton_mousedown");
            });
        });
    },
    helpTip: function (obj) {
        var time;
        var element = obj.element;
        var tipText = obj.html;
        var timeout = obj.timeout != null ? obj.timeout : 1000;
        var offsetX = obj.offsetX != null ? obj.offsetX : 0;
        var offsetY = obj.offsetY != null ? obj.offsetY : 0;

        $(element).css("cursor", "pointer");
        $(element).hover(
		function () {
		    var eICP = WQ.absPosition(document.getElementById(this.id));
		    var x = eICP.left + 20 + offsetX;
		    var y = eICP.top - 20 + offsetY;
		    $(".WQ_HelpTip, .WQ_HelpTip_Shadow").remove();
		    $("body").append("<div class='WQ_HelpTip'>" + tipText + "</div>" + "<div class='WQ_HelpTip_Shadow'></div>");
		    $(".WQ_HelpTip").css(
			{
			    "top": y + "px",
			    "left": x + "px"
			});
		    $(".WQ_HelpTip_Shadow").css(
			{
			    "top": y - 8 + "px",
			    "left": x - 8 + "px",
			    "width": $(".WQ_HelpTip").width() + 20 + "px",
			    "height": $(".WQ_HelpTip").height() + 8 + "px",
			    "filter": "progid:DXImageTransform.Microsoft.Blur(pixelradius=8)"
			});
		    time = setTimeout(
			function () {
			    $(".WQ_HelpTip, .WQ_HelpTip_Shadow").addClass("WQ_DisplayTip").hover(
				function () {
				    $(this).addClass("WQ_HoverTip");
				},
				function () {
				    $(this).removeClass("WQ_HoverTip");
				});
			}, timeout);
		},
		function () {
		    $(".WQ_HelpTip, .WQ_HelpTip_Shadow").removeClass("WQ_DisplayTip");
		    clearTimeout(time);
		});
    },
    absPosition: function (node) {
        var left = 0;
        var top = 0;
        do {
            left += node.offsetLeft;
            top += node.offsetTop;
        } while (node = node.offsetParent);
        return { "left": left, "top": top };
    },

    SetCwinHeight: function () {
        var obj = document.getElementById("mainFrame"); //iframe id
        if (document.getElementById) {
            if (obj && !window.opera) {
                if (obj.contentDocument && obj.contentDocument.body.offsetHeight) {

                    //下面这句是解决变态Chrom的load事件中不能重新计算高度问题
                    obj.height = 10;

                    obj.height = obj.contentDocument.body.offsetHeight;

                    //下面两行解决ie下自适应问题
                    if (obj.contentDocument.body.offsetHeight != obj.contentWindow.document.body.scrollHeight)
                        obj.height = obj.contentWindow.document.body.scrollHeight;


                } else if (obj.Document && obj.Document.body.scrollHeight) {
                    obj.height = obj.Document.body.scrollHeight;
                }
            } else {
                obj.height = 1000;
            }
        }
    },

    LoadJs: function (file) {
        var head = document.getElementsByTagName("head").item(0);
        var scriptTag = document.createElement("script");
        scriptTag.src = file;
        scriptTag.type = "text/javascript";
        head.appendChild(scriptTag);
    },

    LoadCss: function (file) {
        var head = document.getElementsByTagName("head").item(0);
        var cssTag = document.createElement("link");
        cssTag.href = file;
        cssTag.rel = "stylesheet";
        cssTag.type = "text/css";
        head.appendChild(cssTag);
    },

    appButtonHoverEvent: function (file) {
        this.appButtonMouseDownEven();
        $(".appButton").each(
        function () {
            if (!$(this).hasClass("blueButton")) {
                $(this).hover(
                function () {
                    $(this).addClass("appButton_hover");
                },
                function () {
                    $(this).removeClass("appButton_hover")
                });
            }
        });
    },

    selectAllEvent: function (obj) {//all: select all checkbox, jquery object. sub: sub checkbox, jquery object
        var $all = obj.all;
        var $sub = obj.sub;

        $all.change(function () {
            $sub.prop('checked', $(this).prop('checked'));
        });
        $sub.change(function () {
            var selectAll = true;
            $sub.each(function () {
                if ($(this).prop('checked') == false) {
                    selectAll = false;
                }
            });
            $all.prop('checked', selectAll);
        });
    },

    setContentGrid: function(obj)
    {
        var UNIT_WIDTH=57;
        var WIDTH=990;
        

        var $insertPos=$('#'+obj.id+':first-child');
        var grids='<div class="wqcontent_header_grids">';
        
        for (var i=0; i<6; i++)
        {
            var width=WIDTH;
            var j=0;
            while (width>0)
            {
                var color;
                if ((i==0 && j==0) || (i==3 && j==1) || (i==1 && j==2) || (i==2 && j==5) ||
                    (i==0 && j==13) || (i==5 && j==2))
                    color=Math.round(Math.random()*35).toString();
                else
                    color=Math.round(Math.random()*40+50).toString();
                if (color.length<2)
				    color='0'+color;
			    color=color+color+color;
			
			    if (width<UNIT_WIDTH)
			    {
				    grids+='<div style="background:#'+color+';width:'+width+'px"></div>';
				    width=0;
			    }
			    else
			    {
				    grids+='<div style="background:#'+color+'"></div>'
				    width-=UNIT_WIDTH;
			    }
			    j++;
            }
        }
        grids+='</div><div class="wqcontent_header_grids_gradient0"></div><div class="wqcontent_header_grids_gradient1"></div>';
        $insertPos.prepend(grids);
    },

    setIEShadow: function (element, color, pixelradius, offsetTop, offsetLeft) {
        return;
        if (window.ActiveXObject) {
            if (offsetTop == null) {
                offsetTop = 0;
            }
            if (offsetLeft == null) {
                offsetLeft = 0;
            }
            this.LoadCss('./Content/WQMobileStyle.css');
            var $element = $(element);
            $element.addClass('WQ_ieShadow');
            if ($element.css('position') == 'static' && !$element.hasClass('ingorePosition')) {
                $element.css('position', 'relative');
            }

            var strVer = window.navigator.appVersion;
            var $parent = $element.parent();
            if ($parent.css('position') == 'static' && !$element.hasClass('ingorePosition')) {
                $parent.css('position', 'relative');
            }

            $element.each(function () {
                var $this = $(this);
                $this.prev('.WQ_ieShadowDiv').remove();
                $this.before('<div class="WQ_ieShadowDiv"></div>')
                var $shadow = $this.prev('.WQ_ieShadowDiv');

                var padding_left = isNaN(parseInt($this.css('padding-left'))) ? 0 : parseInt($this.css('padding-left'));
                var padding_right = isNaN(parseInt($this.css('padding-right'))) ? 0 : parseInt($this.css('padding-right'));
                var padding_top = isNaN(parseInt($this.css('padding-top'))) ? 0 : parseInt($this.css('padding-top'));
                var padding_bottom = isNaN(parseInt($this.css('padding-bottom'))) ? 0 : parseInt($this.css('padding-bottom'));

                var margin_left = isNaN(parseInt($this.css('margin-left'))) ? 0 : parseInt($this.css('margin-left'));
                var margin_right = isNaN(parseInt($this.css('margin-right'))) ? 0 : parseInt($this.css('margin-right'));
                var margin_top = isNaN(parseInt($this.css('margin-top'))) ? 0 : parseInt($this.css('margin-top'));
                var margin_bottom = isNaN(parseInt($this.css('margin-bottom'))) ? 0 : parseInt($this.css('margin-bottom'));

                $shadow.css(
                {
                    'filter': 'progid:DXImageTransform.Microsoft.Blur(pixelradius=' + pixelradius + ')',
                    '-ms-filter': 'progid:DXImageTransform.Microsoft.Blur(pixelradius=' + pixelradius + ')',
                    'width': $this.width() + padding_left + padding_right,
                    'height': $this.height() + padding_bottom + padding_top,
                    'z-index': $this.css('z-index') - 1,
                    'top': $this.position().top - pixelradius + margin_top + offsetTop,
                    'left': $this.position().left - pixelradius + margin_left + offsetLeft,
                    'position': 'absolute',
                    'background-color': color
                });
            });
        }
    }
}

function OnEnter(field) {
    if (field.value == field.defaultValue) {
        $(field).css("color", "black");
        field.value = "";
    }
}

function OnExit(field) {
    if (field.value == "") {
        $(field).css("color", "000000");
        field.value = field.defaultValue;
    }
}

