﻿//CheckBox 全选
function selectAllCheckBox() {
    var b = false;
    if (document.getElementById("selall").checked) {
        b = true;
    }
    var objs = document.getElementsByTagName("input");
    for (var i = 0; i < objs.length; i++) {
        if (objs[i].type.toLowerCase() == "checkbox")
            objs[i].checked = b;
    }
}

function getLeftChars(varField, num) {
    var i = 0;
    var counter = 0;
    var cap = num;

    var leftchars = cap - varField.value.length;

    return (leftchars);
}

function onCharsChange(varField, num) {
    var leftChars = getLeftChars(varField, num);
    if (leftChars >= 0) {
        charsmonitor.innerText = leftChars;
        return true;
    }
    else {
        charsmonitor.innerText = "0";
        window.alert("超了！！！！太多了！！！！你只能输入" + num + "个字符！！");

        var len = varField.value.length + leftChars;
        varField.value = varField.value.substring(0, len);
        leftChars = getLeftChars(varField, num);
        if (leftChars >= 0) {
            charsmonitor.innerText = leftChars;
        }
        return false;
    }
}


function goCMSAdvSearch() {
    var url = "CMS_List.aspx";
    var keyword = document.getElementById("Keyword").value;
    var field = document.getElementById("FieldList").value;
    var category = document.getElementById("CMS_AdvSearch_CategoryList").value;

    if ($.trim(keyword) == "") {
        alert("请输入要搜索的关键字！");
    }
    else if ($.trim(field) == "") {
        alert("请选择要搜索的内容！");
    }
    else if ($.trim(category) == "") {
        alert("请选择要搜索的分类！");
    }
    else {
        url += "?t=as&c=" + category + "&f=" + field + "&k=" + keyword;
        window.open(url);
    }

}
function goCMSSearch() {
    var url = "CMS_List.aspx";
    var keyword = document.getElementById("Keyword").value;
    var field = document.getElementById("FieldList").value;

    if ($.trim(keyword) == "") {
        alert("请输入要搜索的关键字！");
    }
    else if ($.trim(field) == "") {
        alert("请选择要搜索的内容！");
    }
    else {
        url += "?t=s&f=" + field + "&k=" + keyword;
        window.open(url);
    }

}

function showTip(obj) {
    if (document.getElementById(obj.id + "Tip").style.display == "none") {
        document.getElementById(obj.id + "Tip").style.display = "block";
    } else {
        document.getElementById(obj.id + "Tip").style.display = "none";
    }

}

/*图片预加载插件
///参数设置：
scaling     是否等比例自动缩放
width       图片最大高
height      图片最大宽
loadpic     加载中的图片路径
*/
jQuery.fn.LoadImage = function(scaling, width, height, loadpic) {
    if ((loadpic == null) || (loadpic == ''))
    { loadpic = "/skins/default/images/icon_loading.gif"; }
    return this.each(function() {
        var t = $(this);
        var src = $(this).attr("src")
        var img = new Image();
        //alert("Loading...")
        img.src = src;
        //自动缩放图片
        var autoScaling = function() {
            if (scaling) {

                if (img.width > 0 && img.height > 0) {
                    if (img.width / img.height >= width / height) {
                        if (img.width > width) {
                            t.width(width);
                            t.height((img.height * width) / img.width);
                        } else {
                            t.width(img.width);
                            t.height(img.height);
                        }
                    }
                    else {
                        if (img.height > height) {
                            t.height(height);
                            t.width((img.width * height) / img.height);
                        } else {
                            t.width(img.width);
                            t.height(img.height);
                        }
                    }
                }
            }
        }
        //处理ff下会自动读取缓存图片
        if (img.complete) {
            //alert("getToCache!");
            autoScaling();
            return;
        }
        $(this).attr("src", "");
        var loading = $("<img alt=\"加载中...\" title=\"图片加载中...\" src=\"" + loadpic + "\" />");

        t.hide();
        t.after(loading);
        $(img).load(function() {
            autoScaling();
            loading.remove();
            t.attr("src", this.src);
            t.show();
            //alert("finally!")
        });

    });
}

function checkLayout() {
    var w = 500;
    $(".description table").each(function() {
        var t = $(this);
        if (t.width() > w) {
            t.width(w);
        }
    });

    $(".description img").each(function() {
        var img = $(this);
        var s_width = img.width();
        var s_height = img.height();
        if (img.width() > w) {
            img.width(w);
            img.height(w / s_width * s_height);
        }
        if (img.height() > h) {
            img.height(h);
            img.width(h / s_height * s_width);
        }
    });
};

function checkboothImg(w, h) {
    var img = $(".booth img");
    var s_width = img.width();
    var s_height = img.height();
    if (img.width() > w) {
        img.width(w);
        img.height(w / s_width * s_height);
    }
    if (img.height() > h) {
        img.height(h);
        img.width(h / s_height * s_width);
    }

};

function selectGoodsPic(id, v) {
    var txt = '<a href="/uppic/product/' + v + '" target="_blank"><img src="/uppic/product/' + v + '"/></a>';
    $(".booth").html(txt);
    $(".thumb li").removeClass("selected");
    $("#thumb" + id).addClass("selected");
    checkboothImg(300, 300);
}

function goSearch(e) {
    var keycode = window.event ? e.keyCode : e.which;
    if (keycode == 13) {
        if (checkSearch()) {
            $('#q').focus();
        }

    }
}

function checkSearch() {
    var v = $('#q').val();
    if (v == '请输入要搜索的关键字' || v == "") {
        alert('请输入要搜索的关键字!');
        $('#q').focus();
        return false;
    }
    $('#q').val($.trim(v));
    return true;
}

function getNewsList(cid, num, box) {
    $(box).html(" 请稍候，正在加载内容...");
    //var url = "/Tools/XHttp.aspx?t=cms&f=getlist&cid="+cid+"&where=Commend=True&fields=AddDate&fetch="+num;    
    var url = "/Tools/XHttp.ashx?t=cms&f=getlist&cid=" + cid + "&where=Commend=True&fields=&fetch=" + num;
    $.get(url, function(data) {
        $(box).html(data);
    });
}

function getPageAds(pageNo) {
    $("div[id^=Ad_No_]").html(" 请稍候，正在加载内容...");
    var url = "/XHttpService/XHttp.ashx?t=ad&pageno=" + pageNo + "&dt=" + new Date();
    $.getJSON(url, function(data){
    	if(data != ""){
            $.each(data, function(i, o) {
                $("#Ad_No_" + o.id).html(o.content);
            });
        }
    });
}