/// <reference path="JQuery.Intellisense.js" /> 
// JavaScript Document
//------------------------------修改语言------------------------------
function editLanguage() {
    document.getElementById('siteLanguageList').className = "";
}
function cancelEditLanguage() {
    document.getElementById('siteLanguageList').className = "hide";
}
//------------------------------其他函数------------------------------
//添加栏目 选择栏目属性----栏目添加页
function radioAttributer(obj) {
    if (obj.checked && obj.value == "901") {
        document.getElementById('tr1').className = "show";
        document.getElementById('tr2').className = "hide";
        document.getElementById('tr3').className = "hide";
    }
    else {
        document.getElementById('tr1').className = "hide";
        document.getElementById('tr2').className = "show";
        document.getElementById('tr3').className = "show";
    }
}

//添加友情链接 选择类型----友情链接添加页
function radioFriendLink(obj) {
    if (obj.checked && obj.value == "true") {
        document.getElementById('tr1').className = "hide";
        document.getElementById('tr2').className = "show";
        //同时设置logo隐藏域需要验证
        var myPicBig = document.getElementById("PicLogoTd").getElementsByTagName("input")[0];
        myPicBig.value = "无";
    }
    else {
        document.getElementById('tr1').className = "show";
        document.getElementById('tr2').className = "hide";
        //同时设置logo隐藏域通过验证
        var myPicBig = document.getElementById("PicLogoTd").getElementsByTagName("input")[0];
        myPicBig.value = "";
    }
}
//添加友情链接 选择类型 初始化
function radioFriendLinkLoad() {
    obj = document.getElementById("typeTr").getElementsByTagName("input")[0];
    if (obj.checked && obj.value == "false") {
        document.getElementById('tr1').className = "show";
        document.getElementById('tr2').className = "hide";
        //同时设置logo隐藏域通过验证
        var myPicBig = document.getElementById("PicLogoTd").getElementsByTagName("input")[0];
        myPicBig.value = "";

    }
    else {
        document.getElementById('tr1').className = "hide";
        document.getElementById('tr2').className = "show";
        //同时设置logo隐藏域需要验证
        var myPicBig = document.getElementById("PicLogoTd").getElementsByTagName("input")[0];
        myPicBig.value = "无";
    }
}

//更改自定义属性数目----在线购物栏目配置页
function attributeTrShow(obj) {
    var num = obj.options[obj.selectedIndex].value;
    for (i = 0; i < 15; i++) {
        if (i < num) {
            document.getElementById('attributeTr' + String(i)).className = "";
        }
        else {
            document.getElementById('attributeTr' + String(i)).className = "hide";
        }
    }
}
//初始化自定义属性数目----在线购物栏目配置页
function attributeTrLoad() {
    var obj = document.getElementById("trNum").getElementsByTagName("select")[0];
    var num = obj.options[obj.selectedIndex].value;
    for (i = 0; i < 15; i++) {
        if (i < num) {
            document.getElementById('attributeTr' + String(i)).className = "";
        }
        else {
            document.getElementById('attributeTr' + String(i)).className = "hide";
        }
    }
}
//初始化自定义属性表单----在线购物栏目产品添加页
function attributeTrLabelShow() {
    var obj = document.getElementById("attributeTr0").getElementsByTagName("input")[1];
    var num = obj.value;
    for (i = 0; i < 15; i++) {
        if (i < num) {
            document.getElementById('attributeTr' + String(i)).className = "";
        }
        else {
            document.getElementById('attributeTr' + String(i)).className = "hide";
        }
    }
}

//初始化自定义属性表单----反馈回复页
function fieldTrLabelShow() {
    var obj = document.getElementById("attributeTr0").getElementsByTagName("input")[0];
    var num = obj.value;
    for (i = 0; i < 15; i++) {
        if (i < num) {
            document.getElementById('attributeTr' + String(i)).className = "";
        }
        else {
            document.getElementById('attributeTr' + String(i)).className = "hide";
        }
    }
}

//清除checkbox 的标题----权限设置页
function clearCheckboxTitle() {
    if (document.getElementById("TreeViewTd") == null) {
        return;
    }
    var obj = document.getElementById("TreeViewTd").getElementsByTagName("input");
    var num = obj.length;
    for (i = 0; i < num; i++) {
        obj[i].title = "";
    }

    if (document.getElementById("TreeViewTd1") == null) {
        return;
    }
    var obj = document.getElementById("TreeViewTd1").getElementsByTagName("input");
    var num = obj.length;
    for (i = 0; i < num; i++) {
        obj[i].title = "";
    }

    var mya = document.getElementById("TreeViewTd").getElementsByTagName("a");
    num = mya.length;
    for (i = 0; i < num; i++) {
        //mya[i].href="?id=测试一下加的字符";
    }

}
// 树 checkbox 选择----权限设置页
function OnTreeNodeChecked() {
    var ele = event.srcElement;
    if (ele.type == 'checkbox' && ele.name.indexOf("checkbox") < 0) {
        var table = GetParentByTagName(ele, 'table');
        var checkBoxs = table.getElementsByTagName('INPUT');
        if (checkBoxs.length > 1) {
            for (var i = 0; i < checkBoxs.length; i++) {
                checkBoxs[i].checked = ele.checked;
            }
        }
    }
    else {
        var table = GetParentByTagName(ele, 'table');
        var checkBoxs = table.getElementsByTagName('INPUT');
        if (checkBoxs.length > 1) {
            var myBool = false;
            for (var i = 1; i < checkBoxs.length; i++) {
                myBool = myBool | checkBoxs[i].checked;
            }
            checkBoxs[0].checked = myBool;
        }
    }
}
// 自定义 checkbox 选择----权限设置页
function spanClick(obj, value) {
}
function GetParentByTagName(element, tagName) {
    var parent = element.parentNode;
    var upperTagName = tagName.toUpperCase();
    while (parent && (parent.tagName.toUpperCase() != upperTagName)) {
        parent = parent.parentNode ? parent.parentNode : parent.parentElement;
    }
    return parent;
}
//客户端验证，大图，小图是否上传
function ValidationPicSmall(source, arguments) {
    var myPicSmall = document.getElementById("PicSmallTd").getElementsByTagName("input")[0];
    var myPicSmallValue = myPicSmall.value;
    if (myPicSmallValue != "无") {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
}
function ValidationPicBig(source, arguments) {
    var myPicBig = document.getElementById("PicBigTd").getElementsByTagName("input")[0];
    var myPicBigValue = myPicBig.value;
    if (myPicBigValue != "无") {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
}
//客户端验证友情链接logo图片是否上传
function ValidationPicLogo(source, arguments) {
    var myPicBig = document.getElementById("PicLogoTd").getElementsByTagName("input")[0];
    var myPicBigValue = myPicBig.value;
    if (myPicBigValue != "无") {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
}
function UrlFileName(url) {
    var aUrl = url;
    aUrl = aUrl.slice(aUrl.lastIndexOf("/") + 1, aUrl.length);
    if (aUrl.indexOf("?") != -1) {
        aUrl = aUrl.slice(0, aUrl.indexOf("?"));
    }
    return aUrl
}

//延时渐隐(等待时间，当前时间，div对象，当前透明度)
function HideDiv(maxTime, thisTime, divId, alphaOpacity) {
    var myStep = 100;
    var tempTime = thisTime + myStep;
    //设置屏幕位置
    var x = 0;
    var y = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    } else {
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    }
    //屏幕大小
    var divObj = document.getElementById(divId);
    //得到屏幕大小函数问题！！！lspcieee
    var clientBounds = $common.getClientBounds();
    var width = clientBounds.width;
    var height = clientBounds.height;
    var element = $get(divId);
    x = Math.max(0, Math.floor(x + width / 2.0 - element.offsetWidth / 2.0));
    //y = Math.max(0, Math.floor(y + height / 2.0 - element.offsetHeight / 2.0));
    y = Math.max(0, Math.floor(y));
    divObj.style.left = x + 'px';
    divObj.style.top = y + 'px';
    if (tempTime > maxTime) {

        var tempAlphaOpacity = alphaOpacity - 20;
        if (tempAlphaOpacity > -1) {

            divObj.style.filter = "Alpha(Opacity=" + (tempAlphaOpacity + " ") + ")";
            divObj.style.MozOpacity = tempAlphaOpacity / 100;

            //alert(divObj.getElementsByTagName("span")[0].style.filter);
            setTimeout("HideDiv(" + maxTime + "," + tempTime + ",'" + divId + "'," + tempAlphaOpacity + ")", myStep);
        }
        else {
            divObj.style.filter = 'Alpha(opacity= ' + 100 + ')';
            divObj.style.MozOpacity = 1;
            divObj.className = "hide";
            var masterMsgBoxDiv = document.getElementById("masterMsgBox"); //内层idv
            try {
                masterMsgBoxDiv.removeChild(masterMsgBoxDiv.lastChild); //移出信息span
            }
            catch (e) {
                //防止页面更换后的问题
            }
            return false;
        }
    }
    else {
        setTimeout("HideDiv(" + maxTime + "," + tempTime + ",'" + divId + "'," + alphaOpacity + ")", myStep);
    }
}
//隐藏删除确认对话框
function hideModalPopupViaClient(listName) {
    for (var i = 2; i < 13; i++) {
        var tempStr = "";
        if (i < 10) {
            tempStr = "ctl00_MidContent_" + listName + "_ctl0" + i + "_ConfirmModalPopupExtender";
        }
        else {
            tempStr = "ctl00_MidContent_" + listName + "_ctl" + i + "_ConfirmModalPopupExtender";
        }
        var modalPopupBehavior = $find(tempStr);
        if (modalPopupBehavior != null) {
            modalPopupBehavior.hide();
        }
    }
}
//客户端msg显示
function ShowMsg() {
    var masterMsgDiv = document.getElementById("masterMsg"); //顶层div
    var masterMsgBoxDiv = document.getElementById("masterMsgBox"); //内层idv
    var msgSpan = document.getElementById("main").getElementsByTagName("span")[0];
    if (msgSpan != null && (msgSpan.className == "msg" || msgSpan.className == "errorMsg")) {
        msgSpan.parentNode.className = "hide";
        masterMsgBoxDiv.appendChild(msgSpan);
        masterMsgDiv.className = "loader_container";
        //延时关闭，并清除masterMsgBoxDiv的子节点
        HideDiv(3000, 0, "masterMsg", 100);
        //防止在"栏目管理"页中，回发更新左侧菜单后，链接ajax获取事件丢失
        myOnload()
    }
}
/*
* 描述：跨浏览器的设置 innerHTML 方法
*       允许插入的 HTML 代码中包含 script 和 style
* 作者：kenxu <ken@ajaxwing.com>
* 日期：2006-03-23
* 参数：
*    el: 合法的 DOM 树中的节点
*    htmlCode: 合法的 HTML 代码
* 经测试的浏览器：ie5+, firefox1.5+, opera8.5+
*/
function setInnerHTML(el, htmlCode) {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('msie') >= 0 && ua.indexOf('opera') < 0) {
        htmlCode = '<div style="display:none">for IE</div>' + htmlCode;
        htmlCode = htmlCode.replace(/<script([^>]*)>/gi,
                                    '<script$1 defer="true">');
        el.innerHTML = htmlCode;
        el.removeChild(el.firstChild);
    }
    else {
        var el_next = el.nextSibling;
        var el_parent = el.parentNode;
        el_parent.removeChild(el);
        el.innerHTML = htmlCode;
        if (el_next) {
            el_parent.insertBefore(el, el_next)
        } else {
            el_parent.appendChild(el);
        }
    }
}

//更换页面加载
function LoadNewPage() {
    //ajax请求页面
    function AjaxRequest() {
        //设置请求url 添加时间字符串参数，防止浏览器缓存
        var requestUrl = this.href;
        //判断服务端是否有约定通知，先提示语句，再加载
        if (requestUrl.indexOf("javascript.alert#") > -1) {
            if (requestUrl.indexOf("javascript.alert#Menu.aspx") > -1) {
                alert("暂时无任何栏目可管理，请先到【栏目管理】添加栏目!");
            }
            requestUrl = requestUrl.replace("javascript.alert#", "");
        }
        //显示loading
        document.getElementById("masterLoading").className = "loader_container";
        //创建Sys.Net.WebRequest
        var myWebRequest = new Sys.Net.WebRequest();
        var today = new Date();
        var sStr = today.toString();
        if (requestUrl.indexOf("?") > 0) {
            requestUrl = requestUrl + "&s=" + sStr;
        }
        else {
            requestUrl = requestUrl + "?s=" + sStr;
        }
        myWebRequest.set_url(requestUrl);
        //设置请求完成后出来函数
        myWebRequest.add_completed(OnWebRequestCompleted)
        //执行请求
        myWebRequest.invoke();
        return false;
    }
    //请求返回时事件
    function OnWebRequestCompleted(executor, eventArgs) {
        if (executor.get_responseAvailable()) {
            var responseHtml = executor.get_responseData();
            responseHtml = responseHtml.replace("bodyBox", "");
            //更改标题
            var newTitle = responseHtml.match(/<title>\s*([^<\/title>]*)\s*<\/title>/)[1];
            //如果连接超时退出
            if (newTitle.indexOf("登陆") > 0) {
                window.location = 'Login.aspx';
                return;
            }
            document.getElementById("bodyBox").innerHTML = "";
            setInnerHTML(document.getElementById("bodyBox"), responseHtml);
            //调试
            //document.getElementById("textarea").value=responseHtml;

            //模拟执行页面window.onload()事件
            myOnload();
            window.document.title = newTitle;
        }
        else {
            alert("网络连接失败!");
        }
    }
    //一级菜单ajax读取
    var topMenuLi = document.getElementById("top").getElementsByTagName("li");
    for (i = 0; i < topMenuLi.length; i++) {
        var aObj = topMenuLi[i].getElementsByTagName("a")[0];
        var aUrl = aObj.href;
        var myIframe = document.getElementById("TempIframe");
        aObj.onclick = AjaxRequest;
    }
    //二级菜单ajax读取
    var leftMenuLi = document.getElementById("left").getElementsByTagName("li");
    for (i = 0; i < leftMenuLi.length; i++) {
        var aObj = leftMenuLi[i].getElementsByTagName("a")[0];
        var aUrl = aObj.href;
        var myIframe = document.getElementById("TempIframe");
        aObj.onclick = AjaxRequest;
    }
    //三级菜单ajax读取
    var mainMenuLi = document.getElementById("mainMenu").getElementsByTagName("li");
    for (i = 0; i < mainMenuLi.length; i++) {
        var aObj = mainMenuLi[i].getElementsByTagName("a")[0];
        var aUrl = aObj.href;
        var myIframe = document.getElementById("TempIframe");
        aObj.onclick = AjaxRequest;
    }
}
//页面加载完成执行
function myOnload() {
    var loadingDiv = document.getElementById("masterLoading");
    loadingDiv.className = "hide";
    LoadNewPage();
}
//页面初始化事件
window.onload = myOnload;

//添加栏目 添加功能项 javascript
function MenuAddHrefSet() {
    var menuHrefTd = $("#menuHrefTd")
    var menuHrefTdA = menuHrefTd.find("a");
    var lang = menuHrefTd.find("span").eq(0).text();
    var menuHrefTdInput = $("#menuHrefTd input");
    menuHrefTdA.eq(0).click(function() {
        menuHrefTdInput.val("javascript:CartLink(0," + lang + ");");
        return false;
    });
    menuHrefTdA.eq(1).click(function() {
        menuHrefTdInput.val("javascript:AddFavorite();");
        return false;
    });
}
//jquery加载
$(document).ready(function() {
    MenuAddHrefSet();
});