
//替换制定索引的字符
function replaceindex(val, indexVal, replaceVal) {
    val = val.substr(0, indexVal) + replaceVal + val.substr(indexVal + 1, val.length - indexVal);
    return val;
}
//根据值选中下拉框
function selSelect(name, value) {
    $(document).ready(function() {
        $('select[name="' + name + '"]').find('option').each(function(i) {
            if ($(this).val() == value) {
                $(this).attr('selected', 'selected');
                return false;
            }
        });
    });
}
//根据值选中单选按钮
function selRadio(name, value) {
    $(document).ready(function() {
        $('input[name="' + name + '"]').each(function(i) {
            if ($(this).val() == value) {
                $(this).attr('checked', 'checked');
                return false;
            } 
        });
    });
}
function getSelectText(controlId) {
    var options = $("#" + controlId + " option");
    for (var i = 0; i < options.length; i++) {
        if (options[i].selected == true) {
            return options[i].text;
        }
    }
    return "";
}

//全选
function checkAll(name,obj) {
    $("input[name='" + name + "']").attr("checked", $(obj).attr("checked"));
}
//控制同级下的显示
function showsubmenu(sendor) {
    var clickSendor = $(sendor).parent().next();
    if (clickSendor.css("display") == "none") {
        clickSendor.show();
    } else {
        clickSendor.hide();
    }
}

/**********Json操作相关*************/

JSON.Add = function(list, item) {
    if (JSON.Contain(list, item.ID)) {
        //更新操作
        for (var i = 0; i < list.length; i++) {
            if (list[i].ID == item.ID) {
                list[i] = item;
                break;
            }
        }
    }
    else {
        list[list.length] = item;
    }
}
JSON.Get = function(list, idValue) {
    var result = null;
    for (var i = 0; i < list.length; i++) {
        if (list[i].ID == idValue) {
            result = list[i];
            break;
        }
    }
    return result;
}
JSON.Contain = function(list, idValue) {
    var result = false;
    for (var i = 0; i < list.length; i++) {
        if (list[i].ID == idValue) {
            result = true;
            break;
        }
    }
    return result;
}
JSON.Delete = function(list, idValue) {
    var result = [];
    for (var i = 0; i < list.length; i++) {
        if (list[i].ID != idValue) {
            JSON.Add(result, list[i]);
        }
    }
    return result;
}
function JsonAjaxRequest(url, data, callback) {
    $.getJSON(url, { Data: JSON.stringify(data) }, callback);
}
function JsonAjaxRequestObject(url, data, callback) {
    $.getJSON(url, { Data: JSON.stringify({ Value: data }) }, callback);
}
function JsonPost(url, data, callback) { 
    $.ajax({
     url: url,
     data: 'Data=' + escape(JSON.stringify({ Value: data })),
     contenttype: "application/json; charset=utf-8",
     datatype: "jsonp",
     type: "post",
     success: function(response) {
          callback(response);
     },
     error: function(a, b, c) {
          
     }
});
}
/**********Json操作相关End*************/
/**********选择站点*************/
//页面的回调函数
var callback = null;
//提交站点的选择
function sub() {
    if (callback == null) {
        alert("提交失败，必须设置回调函数");
    }
    else {
        callback(JSON.stringify(siteValue));
    }
}
//取消选择
function cancle() {
    this.parent.closeDialog();
}
//删除选择的站点
function deleteSelectedItem(selectObj, siteId) {
    $(selectObj).remove();
    siteValue = JSON.Delete(siteValue, siteId);
}

function jsonToUrlString(jsonObj) {
    var result = "";
    for (var item in jsonObj) {
        if (item) {
            result += "&" + item + "=" + escape(jsonObj[item]);
        }
    }
    if (result) {
        result = result.substring(1);
    }
    return result;
}

Date.prototype.Format = function(fmt) { //author: meizz   
    var o = {
        "M+": this.getMonth() + 1,                 //月份   
        "d+": this.getDate(),                    //日   
        "h+": this.getHours(),                   //小时   
        "m+": this.getMinutes(),                 //分   
        "s+": this.getSeconds(),                 //秒   
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
        "S": this.getMilliseconds()             //毫秒   
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}