/*文本框灰色提示*/
$(function () {
    $(".grayTip").each(function () {
        var $input = $(this);
        $input.css("color", "#DDDDDD");
        var oldText = $.trim($input.val());
        $input.focus(function () {
            var newText = $.trim($input.val());
            if (newText == oldText) {
                $input.val("");
            }
            $input.css("color", "#000");
        });
        $input.blur(function () {
            var newText = $.trim($input.val());
            if (newText == "") {
                $input.val(oldText);
                $input.css("color", "#DDDDDD");
            }
        });
    });
});
//全选
function SelectAll(chkbox) {
    var box = chkbox;
    oState = box.checked;
    elem = box.form.elements;
    for (i = 0; i < elem.length; i++) {
        if (elem[i].type != "checkbox") continue;
        if (elem[i].name != box.name) {
            if (elem[i].checked != oState) {
                elem[i].click();
            }
        }
    }
}

//获取列表复选框已选中值列表
function getCheckedValueList() {
    var ValueList = "";
    var elem = document.getElementsByTagName("input");
    for (i = 0; i < elem.length; i++) {
        if ((elem[i].name == "chkItem" || elem[i].name.indexOf("$chkItem") > 0) && elem[i].checked == true) {
            var sText = elem[i].value;
            if (!sText) {
                sText = elem[i].getAttribute("value");
            }
            if (ValueList == "")
                ValueList = sText;
            else
                ValueList = ValueList + "," + sText;
        }
    }
    return ValueList;
}

function ChangeTr(TrId) {
    var tr = document.getElementById(TrId);
    if (tr.style.display == "none")
        tr.style.display = "";
    else
        tr.style.display = "none";
}

function OpenModalReturnWindow(pageURL, innerWidth, innerHeight) {
    var ScreenWidth = screen.availWidth;
    var ScreenHeight = screen.availHeight;
    var StartX = (ScreenWidth - innerWidth) / 2;
    var StartY = (ScreenHeight - innerHeight) / 2;
    var ReturnValue = window.showModalDialog(pageURL, 'newWindows', 'dialogWidth:' + innerWidth + 'px;dialogHeight:' + innerHeight + 'px;dialogTop:' + StartY + ';help:no;unadorned:no;resizable:no;status:no;center:yes');
    return ReturnValue;
}

function OpenWindow(pageURL, innerWidth, innerHeight) {
    if (innerHeight > ScreenHeight) {
        innerHeight = ScreenHeight;
    }
    var ScreenWidth = screen.availWidth;
    var ScreenHeight = screen.availHeight;
    var StartX = (ScreenWidth - innerWidth) / 2;
    var StartY = (ScreenHeight - innerHeight - 25) / 2;
    if (StartY < 0) {
        StartY = 0;
    }
    window.open(pageURL, '_blank', 'height=' + innerHeight + ", width=" + innerWidth + ',top=' + StartY + ',left=' + StartX + ', center=Yes, toolbar=no, menubar=no, scrollbars=Yes, resizable=Yes,location=no, status=Yes');

}

function Open_Model_Window(pageURL, innerWidth, innerHeight) {
    if (innerHeight > ScreenHeight) {
        innerHeight = ScreenHeight;
    }
    var ScreenWidth = screen.availWidth;
    var ScreenHeight = screen.availHeight;
    var StartX = (ScreenWidth - innerWidth) / 2;
    var StartY = (ScreenHeight - innerHeight - 25) / 2;
    if (StartY < 0) {
        StartY = 0;
    }
    window.open(pageURL, '_blank', 'height=' + innerHeight + ", width=" + innerWidth + ',top=' + StartY + ',left=' + StartX + ', center=Yes, toolbar=no, menubar=no, scrollbars=Yes, resizable=no,location=no, status=Yes');

}
function GetQuery(arg) {
    var query = window.location.search.substring(1) || "";
    if (query == "") {
        return "";
    }
    var pairs = query.split("&");
    for (var i = 0; i < pairs.length; i++) {
        var key = pairs[i].split("=")[0];
        var val = pairs[i].split("=")[1];
        if (key == arg) {
            return val;
        }
    }
    return "";
}

//省市联动
function ProvinceCtiyCounty(id, op, bindControlId) {
    AjaxProvinceCity('/Ajax.aspx?op=' + op + '&id=' + id +"&r=" + Math.random(), bindControlId);
}
function AjaxProvinceCity(url, ElementId) {
    $.get(url, function (result) {
        if (result == null || result == "") {
            alert("数据加载失败");
        }
        var item = document.getElementById(ElementId);
        item.options.length = 1;
        var dataArray = eval(result);
        for (var i = 0; i < dataArray.length; i++) {
            if (dataArray[i] == "undefined" || dataArray[i] == null) continue;
            item.options.add(new Option(dataArray[i].Text, dataArray[i].Value));
        }
    })
}

//权限提示遮罩层，暂时适用在订单、商品
function NotPerTip(msg) {
    if ($(".main_center").length > 0 && $(".page_main").length > 0) {
        $(".main_center").append("&nbsp;" + msg);
        $(".page_main").hide();
    }
}

//文本框指定的光标位置插入数据,obj文本框对象
function insertAtCursor(obj, value) {
    if (obj.selectionStart || obj.selectionStart == '0') // firefox
    {
        var pretxt = obj.value.substring(0, obj.selectionStart);
        var therest = obj.value.substr(obj.selectionEnd);
        var sel = obj.value.substring(obj.selectionStart, obj.selectionEnd);
        obj.value = pretxt + value + therest;
        obj.focus();
    }
    else if (document.selection && document.selection.createRange) // IE
    {
        obj.focus();
        var sel = document.selection.createRange();
        sel.text = value;
        obj.focus();
    }
}

//指定区域生成遮罩层
function CreateZhezao(element,zhezaoID) {
    var width = $(element).width();
    var height = $(element).height();
    $(element).attr("style", "position: relative");
    $(element).append("<div id=\"" + zhezaoID + "\" style=\"background-color: #ccc;filter:alpha(opacity=50);opacity:0.5;z-index:1;position:absolute; width:" + width + "px; height:" + height + "px;left:0;top:0;  text-align:center; \"><img src=\"/images/loading.gif\" style=\"margin:" + (height / 2) + "px;\"></div>");
}