﻿function $(obj)
{
    if (typeof(obj) == "string")
        return document.getElementById(obj);
    else
        return obj;
} 

function MultiSubmit(formID, cbAllID, actionUrl)
{
    var form = document.getElementById(formID);
    if (!HasCheckedRecord(form, cbAllID))
    {
        alert('请至少选择一条纪录。');
        return;
    }
    if(confirm('确实要操作选定记录?'))
    {
        form.action = actionUrl; 
        form.submit();
    }
}

// 是否有选中记录
function HasCheckedRecord(form, cbAllID)
{
    var cbAll = document.getElementById(cbAllID);
    var coll = form.getElementsByTagName("input");
    for (i=0; i<coll.length; ++i)
    {
        if (coll[i] == cbAll || coll[i].type != "checkbox") continue;
        if (coll[i].checked == true) return true;
    }
    
    return false;
}

// 选中或取消选中所有记录
function CheckAll(containerID, cbAll)
{
    var container = document.getElementById(containerID);
    var coll = container.getElementsByTagName("input");
    for (i=0; i<coll.length; ++i)
    {
        if (coll[i] == cbAll || coll[i].type != "checkbox" || coll[i].disabled == true) continue;

        coll[i].checked = cbAll.checked;
        SetRowState(coll[i]);
    }
}

// 更新全选CheckBox的状态
function SetCheckState(containerID, cbAllID)
{
    var flag = true;
    var cbAll = document.getElementById(cbAllID);
    var container = document.getElementById(containerID);
    var coll = container.getElementsByTagName("input");
    for (i=0; i<coll.length; ++i)
    {
        if (coll[i] == cbAll || coll[i].type != "checkbox") continue;        
        if (coll[i].checked == false) flag = false;
        SetRowState(coll[i]);
    }
    
    cbAll.checked = flag;
}

// 设置行的选中状态
function SetRowState(cb)
{
    if (cb.checked) cb.parentNode.parentNode.className = 'selected';
    else cb.parentNode.parentNode.className = '';
}

function SubmitForm(formID)
{
    var form = document.getElementById(formID);
    form.submit();
}

function ValidateSubmit(formID)
{
    var form = document.getElementById(formID);
    if (SubmitWithCheck(form, ""))
    {
        return true;
    }
    else
    {
        return false; 
    }     
}

function ValidateAndSubmit(formID)
{
    var form = document.getElementById(formID);
    if (SubmitWithCheck(form, ""))
    {
        form.submit();
    }
    else
    {
        return false; 
    }     
}


function NewXmlDocument()
{
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	return xmlDoc;
}

function TryToParseFloat(floatStr)
{
    if (isNaN(parseFloat(floatStr)))
    {
        return 0; 
    }
    else
    {
        return parseFloat(floatStr); 
    }      
}

function IsInt(v)
{
    var vArr = v.match(/^[0-9]+$/);
    if (vArr == null)
    {
        return false;
    }
    else
    {
        return true;
    }
}

function Round(number, pre) 
{ 
    number = Math.round (number*Math.pow(10, pre))/Math.pow(10, pre); 
    return number; 
}

function ShowCategoryTab(type)
{
    switch(type)
    {
        case "0" :
            $("tab0").className = "selected";
            $("tab1").className = "";  
            $("tbc").style.display = "";
            $("tbe").style.display = "none";  
        break;
        case "1" :
            $("tab0").className = "";
            $("tab1").className = "selected";   
            $("tbc").style.display = "none";
            $("tbe").style.display = "";   
        break;    
    }  
}

function ModifyOrderNo(categoryCode)
{
    $("span"+categoryCode).style.display = "none";
    $("mlink"+categoryCode).style.display = "none";
    $("txt"+categoryCode).style.display = ""; 
    $("slink"+categoryCode).style.display = ""; 
    $("clink"+categoryCode).style.display = ""; 
} 

function SaveOrderNo(categoryCode)
{
    $("ordercategorycode").value = categoryCode;
    var orderNo = $("txt"+categoryCode).value;
    if (IsInt(orderNo))
    {  
        $("orderno").value = $("txt"+categoryCode).value;
        $("formOrderNo").submit(); 
    }
    else
    {
        window.alert("排序号应为整数！"); 
    }      
}

function CancelOrderNo(categoryCode)
{
    $("span"+categoryCode).style.display = "";
    $("mlink"+categoryCode).style.display = "";
    $("txt"+categoryCode).style.display = "none"; 
    $("slink"+categoryCode).style.display = "none"; 
    $("clink"+categoryCode).style.display = "none"; 
}

var tgs = new Array( 'div','td','tr');
var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
var startSz = 2;
function ts( trgt,inc ) 
{
    if (!document.getElementById) return
    var d = document,cEl = null,sz = startSz,i,j,cTags;
    sz += inc;
    if ( sz < 0 ) sz = 0;
    if ( sz > 6 ) sz = 6;
    startSz = sz;
    if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];
    cEl.style.fontSize = szs[ sz ];
    for ( i = 0 ; i < tgs.length ; i++ ) 
    {
        cTags = cEl.getElementsByTagName( tgs[ i ] );
        for ( j = 0 ; j < cTags.length ; j++ )
       {
          cTags[ j ].style.fontSize = szs[ sz ];
       }  
    }
}

function tl( trgt,inc ) 
{
    if (!document.getElementById) return
    var d = document,cEl = null,sz = startSz,i,j,cTags;
    sz += inc;
    if ( sz < 0 ) sz = 0;
    if ( sz > 6 ) sz = 6;
    startSz = sz;
    if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];
    cEl.style.fontSize = szs[ sz ];
    for ( i = 0 ; i < tgs.length ; i++ ) 
    {
        cTags = cEl.getElementsByTagName( tgs[ i ] );
        for ( j = 0 ; j < cTags.length ; j++ )
       {
          if (cTags[ j ].style.lineHeight == "")
          { 
             cTags[ j ].style.lineHeight = "22px";
          }
          cTags[ j ].style.lineHeight = eval(parseInt(cTags[ j ].style.lineHeight)+6*inc)+"px"  
        }  
    }
}

function GoClick()
{
    var pi = $("txtpi").value;
    if (IsInt(pi) && pi > 0)
    {
        var pageCount = $("txtPageCount").value;
        if (pi > pageCount)
        {
            window.alert("页码超出索引范围！");  
        }
        else
        {
            var beforeIndex = $("txtBeforeIndex").value;
            var afterIndex = $("txtAfterIndex").value; 
            var url = beforeIndex+pi+afterIndex;
            window.location.href = url;  
        }       
    }
    else
    {
        window.alert("页码应该为正整数！"); 
    }      
}

function EGoClick()
{
    var pi = $("txtpi").value;
    if (IsInt(pi) && pi > 0)
    {
        var pageCount = $("txtPageCount").value;
        if (pi > pageCount)
        {
            window.alert("Please input correct page number.");  
        }
        else
        {
            var beforeIndex = $("txtBeforeIndex").value;
            var afterIndex = $("txtAfterIndex").value; 
            var url = beforeIndex+pi+afterIndex;
            window.location.href = url;  
        }       
    }
    else
    {
        window.alert("Please input correct page number."); 
    }      
}

function Search()
{
    if (document.getElementById("keyword").value == "")
    {
        window.alert("请输入要查询的关键词！"); 
        return false; 
    } 
    return true; 
}

function DownloadSearch()
{
    if (document.getElementById("dkeyword").value == "")
    {
        window.alert("请输入要查询的关键词！"); 
        return false; 
    } 
    return true;
}

function ESearch()
{
    if (document.getElementById("keyword").value == "")
    {
        window.alert("Please input key words."); 
        return false; 
    } 
    return true; 
}

function AddToDocument()
{
    var form = $("formDocument");
    if (HasCheckedRecord(form, "selectAll"))
    { 
        form.submit();    
    }
    else
    {
        window.alert("请至少选择一条记录！"); 
    }   
}

function DeleteFromDocument()
{
    var form = $("formDocument");
    if (HasCheckedRecord(form, "selectAll"))
    { 
        form.action = "/deletefromdocument_c.aspx" 
        form.submit();    
    }
    else
    {
        window.alert("请至少选择一条记录！"); 
    }   
}

function AddToArticle(formID)
{
    var form = document.getElementById(formID);
    if (SubmitWithCheck(form, ""))
    {
        form.action = "downloadtoarticle_c.aspx"; 
        form.submit();
    }
    else
    {
        return false; 
    }     
}