/***************************************************
<威博网络后台管理通用JS类>
作者：TerryLv
时间：2008.4.29
***************************************************/
//兼容FF
function getobj(id){
	return document.getElementById(id);	
}
//兼容FF下的childNodes
function childs(nodes)
{
    if(!nodes.length) 
        return [];
    var ret=[];
    for(var i=0;i<nodes.length;i++)
    {
        if(nodes[i].nodeType!=1) 
            continue;
        ret.push(nodes[i]);
    }
    return ret;
}
//多行文本框 MaxLenth设置无效处理
function CheckMaxLenth(obj)
{
    var maxlength = obj.maxsize;
    if(obj.value.length > maxlength)
    {
        alert("输入的内空过长！");
        obj.select();
        return false;
    }
    return true;
}
//选中全部CHECKBOX
function CheckAll(){
    var InputList = document.getElementsByTagName("input");
    var InputAll = document.getElementById("CheckAllBox");
    for(var j = 0 ; j < InputList.length ; j++){
        if(InputList[j].type == "checkbox"){
            InputList[j].checked = InputAll.checked == true ? true : false;
            if(InputList[j].checked == true){
                InputList[j].parentNode.parentNode.style.backgroundColor='#F7F7F7';
                InputList[j].parentNode.parentNode.IsSelected = "1";
            }else{
                InputList[j].parentNode.parentNode.style.backgroundColor='#ffffff';
                InputList[j].parentNode.parentNode.IsSelected = "0";
            }
        }
    }
}

//编辑事件
var count = 0;
function CheckEdit(){
    count = 0;
    var ids = GetCheckedBox();
    if(ids == "0"){
        alert("请选择一条你要编辑的记录");
        return false;
    }else if(count > 1){
        alert("您只能选择一条记录编辑");
        return false;
    }else{
        document.getElementById("CheckedIDS").value = ids.replace('0,','');
        return true;
    }
}


//删除事件
function CheckDel(){
    var ids = GetCheckedBox();
    if(ids == "0"){
        alert("请您至少选择一条数据");
        return false;
    }else{
        if(confirm("您确定要删除这些类别吗？请确定在类别下面没有商品！")){
            document.getElementById("CheckedIDS").value = ids.replace('0,','');
            ids = document.getElementById("CheckedIDS").value;
            var idss = ids.split(',');
            ids="";
            for(var j = 0 ; j < idss.length ; j++){
                var id = "check"+idss[j];
                var obj = document.getElementById(id);
                var node = childs(obj.parentNode.parentNode.childNodes);
                var count = node[3].getElementsByTagName("span")[0].innerHTML;
                if(count!="0"){
                      alert("你选择的了类别下面还有商品，请先删除商品在进行操作！");
                      return false;
                }
                ids +=checkids(idss[j]);
            }
            document.getElementById("CheckedIDS").value = ids;
            return true;
        }else{
            return false;
        }
    }
}

function checkids(id){
    var ids = ","+id;
    var idcheck = "check"+id;
    var obj = document.getElementById(idcheck);
    var node = childs(obj.parentNode.parentNode.parentNode.childNodes);
    
    for(var i=1;i<node.length;i++){
        var chikd = childs(node[i].childNodes);
        if(chikd.length<2){
            continue;
        }
        var childobj = chikd[1].getElementsByTagName("input")[1];
        var familyobj = childs(node[i].childNodes)[1].getElementsByTagName("input")[3];
        var familyid = childs(node[i].childNodes)[1].getElementsByTagName("input")[2].value;
        var img = childs(node[i].childNodes)[1].getElementsByTagName("img")[0];
          var str =    familyobj.value +",";
        if(str.indexOf("," + id+"," ) != -1){
             ids += ","+familyid;
        }
    }
    return ids;
}

//获取全部选中CHECKBOX的值集合
function GetCheckedBox(){
    var ids = "0";
    var InputList = document.getElementsByTagName("input");
    var InputAll = document.getElementById("CheckAllBox");
    for(var j = 0 ; j < InputList.length ; j++){
        if(InputList[j].type == "checkbox"){
            if(InputList[j].checked == true){
                if(InputList[j].value=="on"){
                    continue;
                }
                ids += "," + InputList[j].value;
                count++;
            }
        }
    }
    document.getElementById("CheckedIDS").value=ids;
    return ids;                                               
}

//将选中的类别放入父窗体的控件中
function InitAdsCategory(){
    if(CheckEdit()){
        var id = document.getElementById("CheckedIDS").value;
                                          
        if (navigator.appVersion.indexOf("MSIE") == -1){
            window.opener.returnAction(id);
            window.close();
        }else{
//        window.opener.returnAction(id);
            window.returnValue = id;
            window.close();
        }
        return true;
    }else{
        return false;
    }
}
//----------------------------------
//将选中的值放入父窗体的控件中
//mode=0 只能返回单一类别
//mode!=0 返回多个类别
//-----------------------------------
function InitSelectValue(mode){
    if(mode=="0"){
        if(!CheckEdit())
            return false;
    }
    
    if(mode=="-1"){
        if (navigator.appVersion.indexOf("MSIE") == -1){
            window.opener.returnAction("0|一级分类");
            window.close();
        }else{
            window.returnValue = "0|一级分类";
            window.close();
        }
        return true;
    }
    var id = document.getElementById("CheckedIDS").value;
    
    if (navigator.appVersion.indexOf("MSIE") == -1){
        window.opener.returnAction(id);
        window.close();
    }else{
            window.returnValue = id;
            window.close();
    }
    return true;
}
//打开模式对话框（商品类别选择）
function OpenDialogArticleCategory(ObjID){
    tipsWindown("选择类别","iframe:commodity_Manage_Dialog.aspx?d="+ Date()+"&objid="+ObjID,"350","460","true","","true","text");
}                                                                                                                                

//展开或合并子节点
function ShowChilds(obj,obj2){
    var id = childs(obj.parentNode.childNodes)[2].value;
    obj.src = obj.src.indexOf("images/plus.gif") != -1 ? "images/minus.gif" : "images/plus.gif";
    selectrow1(obj2);
    var node = childs(obj.parentNode.parentNode.parentNode.childNodes);
    var checkobj = childs(obj.parentNode.parentNode.childNodes)[2].getElementsByTagName("input")[0];
//    checkobj.checked = checkobj.checked == false ? true : false;
    
    for(var i=2;i<node.length;i++){
        var chikd = childs(node[i].childNodes);
        if(chikd.length<2){
            continue;
        }
        var childobj = chikd[1].getElementsByTagName("input")[1];
        var familyobj = childs(node[i].childNodes)[1].getElementsByTagName("input")[3];
        var img = childs(node[i].childNodes)[1].getElementsByTagName("img")[0];
        if(familyobj.value.indexOf("," + id + ",") != -1 && node[i].style.display == ""){
        //if(familyobj.value.indexOf("," + id + ",") != -1 && obj.src.indexOf("images/plus.gif") != -1){
            node[i].style.display = "none";
            img.src = "images/minus.gif";
        }else if(familyobj.value.indexOf("," + id + ",") != -1 && node[i].style.display == "none" && img.src.indexOf("images/plus.gif") != -1){
            //node[i].style.display = node[i].style.display == "none" ? "" : "none";
        }else if(childobj.value == id){
            node[i].style.display = node[i].style.display == "none" ? "" : "none";
        }
    }
}


function selectrow1(obj){
    var name = "check"+obj;
    document.getElementById(name).name="1"
}


