/***************************************************
<威博网络后台管理通用JS类>
作者：TerryLv
时间：2008.4.29
***************************************************/
//兼容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;
}
function getobj(id){
	return document.getElementById(id);	
}
function selecttab(obj, act, def, special, areaname){
	var node = childs(obj.parentNode.parentNode.childNodes);
	for(var i=0;i<node.length;i++){
		if(node[i].className != special){
			node[i].className = def;	
			if(getobj(areaname + "_" + i))
				getobj(areaname + "_" + i).style.display = "none";
		}
	}
	obj.parentNode.className = act;
	for(var i=0;i<node.length;i++){
		if(node[i].className != special){
			if(node[i].className == act){
				if(getobj(areaname + "_" + i))
					getobj(areaname + "_" + i).style.display = "";
			}
		}
	}
}
//选中全部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].id != "CheckAllBox" && InputList[j].id.indexOf('disable') == -1){
            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";
            }
        }
    }
}
//选中全部CHECKBOX
function CheckAll2(){
    var InputList = document.getElementsByTagName("input");
    var InputAll = document.getElementById("CheckAllBox2");
    for(var j = 0 ; j < InputList.length ; j++){
        if(InputList[j].type == "checkbox" && InputList[j].id != "CheckAllBox2" && InputList[j].id.indexOf('disable') == -1){
            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";
            }
        }
    }
    
    GetCheckedBox();
}
//----------------------------------------------------
//选中DataGridView中对应列的全部CHECKBOX
//obj   string      DataGridView的id
//col   int         选取列所在位置
//chk   string      CheckAllBox的id
//
//注意，该列中，第一个INPUT项目必须是要操作的Checkbox
//
//----------------------------------------------------
function CheckAllByGridView(obj, col, chk){
    try
    {
        //剥离<TBODY>标签
        var DataGridView = childs(document.getElementById(obj).childNodes)[0];
        var CheckAllBox = document.getElementById(chk);

        //从1开始，直接排除掉表头
        for(var i=1; i < childs(DataGridView.childNodes).length; i++)
        {
            //剥离<TR>标签
            var objTR = childs(DataGridView.childNodes)[i];
            
            //剥离<TD>标签
            var objCol = childs(objTR.childNodes)[col];
            
            //剥离<INPUT>标签
            var checkbox = childs(objCol.childNodes)[0];

            checkbox.checked = CheckAllBox.checked;
            if(checkbox.checked)
            {
                objTR.style.backgroundColor='#F7F7F7';
                objTR.IsSelected = "1";
            }
            else
            {
                objTR.style.backgroundColor='#F7F7F7';
                objTR.IsSelected = "0";
            }
        }
    }
    catch(e)
    {
    }
}
//TR移动变色效果
function ShowSelect(obj, type){
    if(obj.IsSelected == "0"){
        if(type == 0){
            obj.style.backgroundColor = '#F7F7F7';
        }else if(type == 1){
            obj.style.backgroundColor = '#FFFFFF';
        }
    }
}
//根据TR上的时间选择CHECKBOX
function CheckByRowID(RowID){
    var InputList = document.getElementsByTagName("input");
    var InputAll = document.getElementById("CheckAllBox");
    var i = 0;

    for(var j = 0 ; j < InputList.length ; j++){
        if(InputList[j].type == "checkbox" && InputList[j].id != "CheckAllBox" && InputList[j].id.indexOf('disable') == -1){
            if(i == RowID){
                if(InputList[j].mouse != 'over'){
                    InputList[j].checked = InputList[j].checked == true ? false : true;
                    if(InputList[j].checked == true){
                        InputList[j].parentNode.parentNode.style.backgroundColor='#F7F7F7';
                    }else{
                        InputList[j].parentNode.parentNode.style.backgroundColor='#ffffff';
                    }
                }
                
                if(InputList[j].checked == true){
                    InputList[j].parentNode.parentNode.IsSelected = "1";
                }else{
                    InputList[j].parentNode.parentNode.IsSelected = "0";
                }
            }
            i++;
        }
    }
}
//编辑事件
var count = 0;
function CheckEdit(){
    count = 0;
    var ids = GetCheckedBox();
    if(ids == "0"){
        alert(document.getElementById("Alert_Str1").innerHTML);
        return false;
    }else if(count > 1){
        alert(document.getElementById("Alert_Str2").innerHTML);
        return false;
    }else{
        document.getElementById("CheckedIDS").value = ids.replace('0,','');
        return true;
    }
}
//删除事件
function CheckDel(){
    var ids = GetCheckedBox();
    if(ids == "0"){
        alert(document.getElementById("Alert_Str3").innerHTML);
        return false;
    }else{
        if(confirm(document.getElementById("Alert_Str6").innerHTML)){
            document.getElementById("CheckedIDS").value = ids.replace('0,','');
            return true;
        }else{
            return false;
        }
    }
}

//批量操作事件
function CheckSelect(){
    var ids = GetCheckedBox();
    if(ids == "0"){
        alert(document.getElementById("Alert_Str9").innerHTML);
        return false;
    }else{
        document.getElementById("CheckedIDS").value = ids.replace('0,','');
        return true;
    }
}
//单条操作事件
function CheckSelectOne(){
    count = 0;
    var ids = GetCheckedBox();
    if(ids == "0"){
        alert(document.getElementById("Alert_Str9").innerHTML);
        return false;
    }else if(count > 1){
        alert(document.getElementById("Alert_Str2").innerHTML);
        return false;
    }else{
        document.getElementById("CheckedIDS").value = ids.replace('0,','');
        return true;
    }
}
//向上事件
var index = 0;
var indexcount = 0
function CheckUp(){
    var pageindex = document.getElementById("PageIndex").value;
    var maxpageindex = document.getElementById("MaxPageIndex").value;
    indexcount = 0;
    count = 0;
    var ids = GetCheckedBox();
    if(ids == "0"){
        alert(document.getElementById("Alert_Str7").innerHTML);
        return false;
    }else if(count > 1){
        alert(document.getElementById("Alert_Str8").innerHTML);
        return false;
    }else{
        if(index == 0){
            if(pageindex == 0){
                alert(document.getElementById("Alert_Str4").innerHTML);
                return false;
            }else{
                document.getElementById("PageIndex").value = parseInt(pageindex)-1;
                document.getElementById("CheckedIDS").value = ids.replace('0,','');
                return true;
            }
        }else{
            document.getElementById("CheckedIDS").value = ids.replace('0,','');
            return true;
        }
    }
}
//向下事件
function CheckDown(){
    var pageindex = document.getElementById("PageIndex").value;
    var maxpageindex = document.getElementById("MaxPageIndex").value;
    indexcount = 0;
    count = 0;
    var ids = GetCheckedBox();
    if(ids == "0"){
        alert(document.getElementById("Alert_Str7").innerHTML);
        return false;
    }else if(count > 1){
        alert(document.getElementById("Alert_Str8").innerHTML);
        return false;
    }else{
        if(index == indexcount-1){
            if(pageindex == maxpageindex){
                alert(document.getElementById("Alert_Str5").innerHTML);
                return false;
            }else{
                document.getElementById("PageIndex").value = parseInt(pageindex)+1;
                document.getElementById("CheckedIDS").value = ids.replace('0,','');
                return true;
            }
        }else{
            document.getElementById("CheckedIDS").value = ids.replace('0,','');
            return true;
        }
    }
}
//获取全部选中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" && InputList[j].id != "CheckAllBox" && InputList[j].id.indexOf('disable') == -1){
            if(InputList[j].checked == true){
                ids += "," + InputList[j].value;
                count++;
                index = indexcount;
            }
            indexcount++;
        }
    }
    return ids;
}
//将选中的图片放入父窗体的控件中
function InitPicture(obj){
    if (navigator.appVersion.indexOf("MSIE") == -1){
        window.opener.returnAction(obj.title.replace("双击选择该图片，图片地址是",""));
        window.close();
    }else{
        window.returnValue = obj.title.replace("双击选择该图片，图片地址是","");
        window.close();
    }
}
//将选中的类别放入父窗体的控件中
function InitAdsCategory(){
    if(CheckEdit()){
        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;
    }else{
        return false;
    }
}
//打开模式对话框（图片选择）
function OpenDialog(ObjID){
    if (navigator.appVersion.indexOf("MSIE") == -1){
        this.returnAction=function(strResult){
            if(strResult!=null && document.getElementById(ObjID)){
                document.getElementById(ObjID).value = strResult;
                if(document.getElementById(ObjID + "View")){
                    document.getElementById(ObjID + "View").src = "../" + strResult;
                    document.getElementById(ObjID + "View").style.display = "block";
                }
            }
        }
        window.open('Picture_Picture_Manage_Dialog.aspx?d=' + Date(),'newWin','modal=yes,width=550,height=550,top=200,left=300,resizable=no,scrollbars=no');
        return;
    }else{
        var GetValue = showModalDialog('Picture_Picture_Manage_Dialog.aspx?d=' + Date(),'','dialogWidth:550px;dialogHeight:550px;')
        if (GetValue != null && document.getElementById(ObjID)) {
            document.getElementById(ObjID).value = GetValue;
            if(document.getElementById(ObjID + "View")){
                document.getElementById(ObjID + "View").src = "../" + GetValue;
                document.getElementById(ObjID + "View").style.display = "block";
            }
        } 
    }
}
//打开模式对话框（广告类别选择）
function OpenDialogAdsCategory(ObjID){
    if (navigator.appVersion.indexOf("MSIE") == -1){
        this.returnAction=function(strResult){
            if(strResult!=null && document.getElementById(ObjID)){
                document.getElementById(ObjID).value = GetValue;
            }
        }
        window.open('Advertisement_AdsCategory_Manage_Dialog.aspx?d=' + Date(),'newWin','modal=yes,width=550,height=550,top=200,left=300,resizable=no,scrollbars=no');
        return;
    }else{
        var GetValue = showModalDialog('Advertisement_AdsCategory_Manage_Dialog.aspx?d=' + Date(),'','dialogWidth:550px;dialogHeight:550px;')
        if (GetValue != null && document.getElementById(ObjID)) {
            if(document.getElementById(ObjID)){
                document.getElementById(ObjID).value = GetValue;
            }
        } 
    }
}
//打开模式对话框（文章类别选择）
function OpenDialogArticleCategory(ObjID){
    if (navigator.appVersion.indexOf("MSIE") == -1){
        this.returnAction=function(strResult){
            if(strResult!=null && document.getElementById(ObjID)){
                document.getElementById(ObjID).value = GetValue;
            }
        }
        window.open('Article_ArticleCategory_Manage_Dialog.aspx?d=' + Date(),'newWin','modal=yes,width=550,height=550,top=200,left=300,resizable=no,scrollbars=no');
        return;
    }else{
        var GetValue = showModalDialog('Article_ArticleCategory_Manage_Dialog.aspx?d=' + Date(),'','dialogWidth:550px;dialogHeight:550px;')
        if (GetValue != null && document.getElementById(ObjID)) {
            if(document.getElementById(ObjID)){
                document.getElementById(ObjID).value = GetValue;
            }
        } 
    }
}
//打开模式对话框（查看原图）
function ShowOldImg(imgUrl){
    var img = document.getElementById("review");
    img.src = imgUrl;
    img.style.display = "block";
    document.getElementById("AlphaField").style.display = "";
}
//隐藏图片
function HiddenImg(obj){
    obj.style.display = "none";
    document.getElementById("AlphaField").style.display = "none";
}
//初始化广告分类状态
function FormatStatus(id){
    try
    {
        if(!document.getElementById(id)) return;
        var node = childs(childs(document.getElementById(id).childNodes)[0].childNodes);
        var level = 0;
        for(var i=1;i<node.length;i++){
            level = childs(node[i].childNodes)[1].getElementsByTagName("input")[0].value
            if(level != "0"){
                node[i].style.display = "none";
                childs(node[i].childNodes)[1].style.padding = "0 0 0 "+ level*20 +"px";
            }
        }
    }
    catch(e)
    {
    }
}
//展开或合并子节点
function ShowChilds(obj){
    var id = childs(obj.parentNode.childNodes)[2].value;
    obj.src = obj.src.indexOf("images/plus.gif") != -1 ? "images/minus.gif" : "images/plus.gif";
    
    var node = childs(obj.parentNode.parentNode.parentNode.childNodes);
    var checkobj = childs(obj.parentNode.parentNode.childNodes)[0].getElementsByTagName("input")[0];
    //alert(checkobj.id);
    checkobj.checked = checkobj.checked == false ? true : false;
    
    for(var i=1;i<node.length;i++){
        var childobj = childs(node[i].childNodes)[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/plus.gif";
        }else if(familyobj.value.indexOf("," + id + ",") != -1 && node[i].style.display == "none" && img.src.indexOf("images/minus.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 InitTrClass(){
    //var node = document.getElementById("OperateTable").getElementsByTagName("tr");
    //var node = document.getElementById("OperateTable").childNodes;
    var flag = 0;
    var node = childs(document.getElementById("OperateTable").childNodes)[0].childNodes;
    for(var i=0;i<node.length;i++){
        if(node[i].tagName == "TR" && node[i].style.display != "none"){
            if(flag%2 == 0)
                node[i].className = "F7tr";
            else
                node[i].className = "FFtr";
            
            flag++;
        }
    }
}

//编辑页面加载后自动给每行加不同样式
function InitTrClassById(id){
    //var node = document.getElementById("OperateTable").getElementsByTagName("tr");
    //var node = document.getElementById("OperateTable").childNodes;
    var flag = 0;
    var node = childs(document.getElementById(id).childNodes)[0].childNodes;
    for(var i=0;i<node.length;i++){
        if(node[i].tagName == "TR" && node[i].style.display != "none"){
            if(flag%2 == 0)
                node[i].className = "F7tr";
            else
                node[i].className = "FFtr";
            
            flag++;
        }
    }
}

//根据字符串动态加载相应TR
function InitAdsQuery(obj){
    var str = obj.options[obj.selectedIndex].value;
    if(str != ""){
        var arrayMain = str.split('|');
        var ImgCount = arrayMain[0];
        var QueryArray = arrayMain[1].split(',');
        
        alert(ImgCount);
        for(var i=0;i<QueryArray.length;i++){
            alert(QueryArray[i]);
        }
    }
}

//根据SELECT的值给别的INPUT加值
function InitWidth(obj, w, h){
    var str = obj.options[obj.selectedIndex].value;
    if(str != ""){
        var arrayMain = str.split(',');
        document.getElementById(w).value = arrayMain[0];
        document.getElementById(h).value = arrayMain[1];
    }
}

//选中全部
function SelectAllOption(id){
    var obj = document.getElementById(id);
    for(var i=0;i<obj.options.length;i++){
        //obj.options[i].selected = obj.options[i].selected == true ? false : true;
        obj.options[i].selected = true;
    }
}

//查看广告调用代码
function GetAdsCode(){
    if(CheckEdit()){
        var txt = "<script language=\"javascript\" src=\"Show/"+document.getElementById("CheckedIDS").value+".js\"></script>";
        var url = "Advertisement_Ads_Operate_Dialog.aspx?txt=" + escape(txt);
        
        if (navigator.appVersion.indexOf("MSIE") == -1){
            window.open(url,'newWin','modal=yes,width=550,height=180,top=200,left=300,resizable=no,scrollbars=no');
            return;
        }else{
            showModalDialog(url,'','dialogWidth:550px;dialogHeight:180px;')
        }
    }
    return false;
}

//取得XmlHttp对象
function CreateXMLHttpRequest()//这里是构造XMLHttpRequest对象的方法
{
    var xmlHttpRequest = null;//这里是大家都常用的IE，firefox中取得XMLHttpRequest对象的方法
    try 
    {
        xmlHttpRequest = new XMLHttpRequest(); 
    }
    catch(e)
    {
        try
        {
            xmlHttpRequest=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    return xmlHttpRequest;
}
//----------------------------------------
// 用Ajax提交数据
//
// Url指定跳转页,
// data是要post的数据。
// EventHander 函数指针
//----------------------------------------
function AjaxSubmit(url, data, EventHander)
{
    var xmlHttpResquest = CreateXMLHttpRequest();

    xmlHttpResquest.open("POST", url, true);
    xmlHttpResquest.setRequestHeader("Content-Length", data.length);
    xmlHttpResquest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttpResquest.send(data);
    xmlHttpResquest.onreadystatechange = function ()
    {
        if (xmlHttpResquest.readyState == 4)
        {
            try
            {
                if(xmlHttpResquest.status == 200)
                {
                    //alert(xmlHttpResquest.responseText);      //DEBUG用
                    EventHander(xmlHttpResquest.responseText);  //这里可以调用想要的函数
                }
            }
            catch(e)
            {
                var errMsg = "";
                if(e!=null)
                {
                    errMsg = "Number:" + e.number + "\n" +
                             "Message:" + e.message + "\n";
                }
                alert(errMsg);
            }
        }
    }

}

function RowCanSelected(obj){
    obj.style.cursor = 'pointer';
    
    //找到本行TR标签的对象
    var trobj = obj.parentNode.parentNode.childNodes;
    var tdobj = childs(trobj)[0];
    var chkobj = tdobj.getElementsByTagName("input")[0];
    if(chkobj != null){
        chkobj.mouse='out';
    }
}

function RowCanNotSelected(obj){
    obj.style.cursor = 'auto';
    //找到本行TR标签的对象
    var trobj = obj.parentNode.parentNode.childNodes;
    var tdobj = childs(trobj)[0];
    var chkobj = tdobj.getElementsByTagName("input")[0];
    if(chkobj != null){
        chkobj.mouse='over';
    }
}

//------------------------------------------------------------------
// 检查日期型
// obj          要检查的控件
// Isfoucs      验证失败时是否控制焦点
// IsMustInput  是否必须输入
//------------------------------------------------------------------
function CheckDate(obj, Isfoucs, IsMustInput)
{
    if(obj.value == "")
    {
        if(IsMustInput)
        {
            alert("必须输入!");
            if(Isfoucs) obj.select();
            return false;
        }
    }
    else
    {
        var re = /^^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/;
        if(!re.test(obj.value))
        {
            alert("请输入日期类型!");
            if(Isfoucs)
            {
                obj.select();
            }
            else
            {
                obj.value = "";
            }
            return false;
        }
    }
    return true;
}

function ChangePriceCardView(temp)
{
    var tabdiv = "";
    var tabview = "";
    if(temp == 1)
    {
        tabdiv = "pricecardtab0";
        tabview = "pricecardview0";
    }
    else
    {
        tabdiv = "pricecardtab1";
        tabview = "pricecardview1";
    }
    //导航tab样式
    $(tabdiv).setProperty("height","26px");
    $("pricecardtab"+temp).setProperty("height","27px");
    $(tabview).setProperty("display","none");
    $("pricecardview"+temp).setProperty("display","block");
}

/*
载入商品的详细类别列表
*/
function loadTypes(productID,pageName)
{
    var hasHTML = "";
    if(!$(pageName+"showProductType_"+productID))
    {
        var el = new Element("div",{
            "id": pageName+"showProductType_"+productID,
            "styles": {
                "position": "relative",
                "margin" : "-25px"
            }
        }).inject($(pageName+"putinCart_"+productID),"after");
    }
    hasHTML = $(pageName+"showProductType_"+productID).get("html");
    $(pageName+"showProductType_"+productID).setStyle("display","block");
    if(hasHTML == "")
    {        
	    var newurl = "/member/AjaxRequest.aspx?Command=ProductType&PID="+productID+"&PNAME="+pageName;
	    var myRequest = new Request({
	        url: newurl, 
	        method: "get",
            onSuccess : function(responseText){
                if(responseText != "")
                {
                    $(pageName+"putinCart_"+productID).setStyle("target","_self");
                    $(pageName+"putinCart_"+productID).setProperty("href","javascript:void(0)");
			        $(pageName+"showProductType_"+productID).innerHTML = responseText;
			    }
			    else
			    {
			        $(pageName+"showProductType_"+productID).innerHTML = "    ";
			    }
		    }
	    }).send();
    }
}

/*
隐藏商品详细类别列表
*/
function hideTypes(productID,pageName)
{
    if($(pageName+"showProductType_"+productID))
    {
        $(pageName+"showProductType_"+productID).setStyle("display","none");
    }
}

function PutInMiniCart(productID)
{
	var newurl = "../cart.aspx?act=fastbuy&id="+productID;
	var myRequest = new Request({
	    url: newurl, 
	    method: "get",
        onSuccess : function(responseText){
			alert(responseText);
		}
	}).send();
    //document.load("cart.aspx?act=fastbuy&id="+productID)
}