﻿window.onload=function(){
    //Resolve null Field	
    for(var i=1;;i++)
    {
	    var td=document.getElementById("td"+i);
	    if(td!=null)
	    {
		    if(td.innerText==null||td.innerText=="")
		    {
			    td.parentNode.style.display = 'none';
			    td.parentNode.nextSibling.style.display = 'none';
		    }			
	    }
	    else
		    break;
    }

    for(var j=1;;j++)
    {
	    var field=document.getElementById("field"+j);
	    if(field!=null)
	    {
		    if(field.innerText==null||field.innerText=="")
		    {
			    field.style.display = 'none';
		    }			
	    }
	    else
		    break;
    }

    for(var k=1;;k++)
    {
	    var tf=document.getElementById("tf"+k);
	    if(tf!=null)
	    {
		    if(tf.innerText==null||tf.innerText=="")
		    {
			    tf.parentNode.style.display = 'none';
		    }			
	    }
	    else
		    break;
    }
    
    //Resovle From Search Page
    if(location.href.indexOf("SearchWord")>0)
        scroller('list',800);      
}

//Resvolve Full Text Link
function CheckAll(){
  var lnkCheck=document.getElementById("lnkCheckAll");
  var elements =  document.body.getElementsByTagName("input"); 
  if(lnkCheck.innerText=="选择全部")
  {
      lnkCheck.innerText="取消选择"
      for(var i=0,l=elements.length;i<l;i++)
      {
          if(elements[i].type=="checkbox")
          {
             elements[i].checked=true;
          }
      }
  }
  else
  {
      lnkCheck.innerText="选择全部"
      for(var i=0,l=elements.length;i<l;i++)
      {
          if(elements[i].type=="checkbox")
          {
             elements[i].checked=false;
          }
      }
  }
} 

//添加多篇 
function AddToCart(rsid)
{
    var userLoggedIn = Sys.Services.AuthenticationService.get_isLoggedIn(); 

    if (!userLoggedIn)
	  {
		  openInfoDialog("","");
		  return;
	  }

    var elements =  document.body.getElementsByTagName("input");  
    var seldoc="";
    for(var i=0,l=elements.length;i<l;i++)
    {
        if(elements[i].type=="checkbox"&&elements[i].checked)
        {
            
            seldoc = seldoc + elements[i].value + "|";  
        }
    }
    if(seldoc=="")
    {
      alert("您还没有选择文章！");
      return;
    }
    if(confirm("您打算将这些文章放入原文申请列表吗？"))
    {
		    AddShoppingCart1(rsid,seldoc);
    }
}
//添加单篇
function AddSingleToCart(ResId,Pars)
{
    var userLoggedIn = Sys.Services.AuthenticationService.get_isLoggedIn();
    if (!userLoggedIn)
	  {
		  openInfoDialog(ResId,Pars);  
		  return;
	  }
    else
    {
      if(confirm("您打算将这篇文章放入原文申请列表吗？"))
      {
		     AddShoppingCart1(ResId,Pars);
      }
    }   
}

function AddShoppingCart1(a,b)
{	
	$('WebServiceLoadingPanel').style.display = 'block';
  request = uniRMS.WebSite.WebServices.ShoppingService.AddUserShoppingCart(a,b,OnComplete1,onFailed);
}

function OnComplete1(result) 
{
    $('WebServiceLoadingPanel').style.display = 'none';

    if(result)
    {
        if(result=="1")   //无登录或者不是注册用户
        {
          return;  
        }
        else if(result.substring(0,1)=="N")  //一次原文传递文章数超过10篇
        {          
            alert("对不起，一次原文请求的文章数不能超过10篇，您还可以添加"+result.substring(1)+"篇文章。"); 
        }
        else
        {     
            var resultList=result.split('|'); 
            if(resultList[2]!="")
            {
               alert("您添加的文章有重复，‘"+resultList[2]+"‘已经自动过滤。");        
            }
		        else
		        {
			        alert("成功加入"+resultList[1]+"篇文章！");
			        document.getElementById(MainContentId+'CheckedNum').innerText="您已添加 "+resultList[0]+" 篇文章到原文申请列表";
		        }   
		    }  
    }
    else
    {
        alert("加入失败！失败原因：1、可能您已经购买此文档；2、账户无钱；3、请联系管理员！！");
    }
}

function onFailed(result) 
{
    alert(result.get_exceptionType()+","+result.get_message()+","+result.get_stackTrace());
    alert("添加失败!");   
    $('WebServiceLoadingPanel').style.display = 'none';
}
//未登陆或未注册用户点击原文请求的提示
function openInfoDialog(ResId,Pars) 
{ 
  Dialog.confirm($('login').innerHTML, 
                 {className:"alphacube", width:400,
                  okLabel: "登录", 
                  cancelLabel: "取消", 
                  onOk:function(win) 
                    { 
                      if($('UserName').value.blank())
                      {
                        showLoginError('请输入用户名！');                       
                        //new Effect.Shake(Windows.focusedWindow.getId()); 
                        return false; 
                      }
                      if($('Password').value.blank()) 
                      {
                        showLoginError('请输入密码！');
                        //new Effect.Shake(Windows.focusedWindow.getId()); 
                        return false;
                      }                   
                                            
                       var url = "/ValidateLogin.aspx";
       　　            var pars = "username="+encodeURIComponent($('UserName').value)+"&password="+encodeURIComponent($('Password').value);
      　               var myajax= new Ajax.Request(url,{method: 'get', parameters: pars, onComplete: updateResponse});
                       return false;                       
                    }
                 }); 
  function updateResponse(originalRequest)
  {     
    var strTemp =originalRequest.responseText;  
    if(strTemp.substr(0,1)=="N")  //验证通过
    {  
      //window.location.reload();
      Sys.Services._AuthenticationService.DefaultWebServicePath = '../../../../../../../../../../Authentication_JSON_AppService.axd';
      Sys.Services.AuthenticationService._setAuthenticated(true);      
      Dialog.closeInfo();
      var strInfo=strTemp.split('|'); //第一个为N，第二个为用户ID，第三个为此用户的购物车的文章数
      $(MainContentId+'HaveSelected').style.display="block";
      $(MainContentId+'CheckedNum').href=$(MainContentId+'transferToPage').value; 
      $(MainContentId+'CheckedNum').innerText="您已添加 "+strInfo[2]+" 篇文章到原文申请列表";
      $('dnn_dnnUSESR_cmdRegister').title="点击修改您的帐号信息";
	    $('dnn_dnnUSESR_cmdRegister').innerText=strInfo[3];
	    $('dnn_dnnLOGIN_cmdLogin').down().src="/Portals/_default/Skins/istic/images/b_button_logout.gif";
      if(ResId!="")
      {
        AddShoppingCart1(ResId,Pars);//继续添加已选单篇文章
      }
      else
      {
        AddToCart(ResId);  //继续添加已选多篇文章
      }
    }
    else if(strTemp=="0")
    {  
      showLoginError('用户名或者密码有误!<br />请重新输入！');      
    }  
    else
    {
      showLoginError('可能由于网络问题，请稍候再试！');    
    }
  }               
                 
}

function showLoginError(errorDisplay)
{
   $('login_error_msg').innerHTML=errorDisplay;
   $('login_error_msg').show(); 
   Windows.focusedWindow.updateHeight();
}

//添加到代查代借申请表
function AddSingleToDcdj(title)
{
    var userLoggedIn = Sys.Services.AuthenticationService.get_isLoggedIn(); 
    if (!userLoggedIn)
    {
      openInfoDialog();
    }
    else
    {
      if(confirm("您打算代查代借这篇文章吗？"))
      {
         var url="http://"+location.host+"/tabid/693/Default.aspx?title="+encodeURIComponent(title); 
		     window.open(url,"dcdj");
      }
    }   
}

// 转换为数字 
function intval(v) {
    v = parseInt(v);
    return isNaN(v) ? 0 : v; 
}  
// 获取元素信息 
function getPos(e) {
    var l = 0;     
    var t   = 0;     
    var w = intval(e.style.width);     
    var h = intval(e.style.height);     
    var wb = e.offsetWidth;     
    var hb = e.offsetHeight;     
    while (e.offsetParent){
        l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0);
        t += e.offsetTop + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0);
        e = e.offsetParent;
    }
    l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0);
    t += e.offsetTop + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0);
    return {x:l, y:t, w:w, h:h, wb:wb, hb:hb}; 
}  
// 获取滚动条信息 
function getScroll()  {     
    var t, l, w, h;          
    if (document.documentElement && document.documentElement.scrollTop) {          
        t = document.documentElement.scrollTop;          
        l = document.documentElement.scrollLeft;          
        w = document.documentElement.scrollWidth;          
        h = document.documentElement.scrollHeight;     
    } 
    else if (document.body) {          
        t = document.body.scrollTop;          
        l = document.body.scrollLeft;          
        w = document.body.scrollWidth;          
        h = document.body.scrollHeight;     
    }     
    return { t: t, l: l, w: w, h: h }; 
}  
// 锚点(Anchor)间平滑跳转 
function scroller(el, duration) {   
    if(typeof el != 'object') { 
        el = document.getElementById(el); }      
    if(!el) return;      
    var z = this;      
    z.el = el;      
    z.p = getPos(el);      
    z.s = getScroll();      
    z.clear = function(){
        window.clearInterval(z.timer);
        z.timer=null};      
        z.t=(new Date).getTime();      
        z.step = function(){         
            var t = (new Date).getTime();         
            var p = (t - z.t) / duration;         
            if (t >= duration + z.t) {
                z.clear();              
                window.setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);         
            } 
            else {
                st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;              
                sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;              
                z.scroll(st, sl);         
            }     
        };      
        z.scroll = function (t, l){window.scrollTo(l, t)
    };      
    z.timer = window.setInterval(function(){z.step();},13); 
} 

function ResovleNull()
{
    for(var i=1;;i++)
    {
	    var td=document.getElementById("td"+i);
	    if(td!=null)
	    {
		    if(td.innerText==null||td.innerText=="")
		    {
			    td.parentNode.style.display = 'none';
			    td.parentNode.nextSibling.style.display = 'none';
		    }			
	    }
	    else
		    break;
    }
	
    for(var j=1;;j++)
    {
	    var field=document.getElementById("field"+j);
	    if(field!=null)
	    {
		    if(field.innerText==null||field.innerText=="")
		    {
			    field.style.display = 'none';
		    }			
	    }
	    else
		    break;
    }
}

function ClientJourTabSelectedHandler(sender, eventArgs)
{
    var cate = $get('dnn_ctr2118_MainView_Journal_lstExpandedCatalogs');
    var pre = $get('dnn_ctr2118_MainView_Journal_pageNav');
    if (sender.SelectedIndex != 0)
    {
        if (cate!=null)
            cate.style.display='none';
        if (pre!=null)
            pre.style.display='none';
    }
    else
    {
        if (cate!=null)	  
            cate.style.display='';
        if (pre!=null)
            pre.style.display='';	  
    }
}

function ClientMeetTabSelectedHandler(sender, eventArgs)
{
    var cate = $get('dnn_ctr2118_MainView_Meeting_lstExpandedCatalogs');
    var pre = $get('dnn_ctr2118_MainView_Meeting_pageNav');
    if (sender.SelectedIndex != 0)
    {
        if (cate!=null)
            cate.style.display='none';
        if (pre!=null)
            pre.style.display='none';
    }
    else
    {
        if (cate!=null)
            cate.style.display='';
        if (pre!=null)
            pre.style.display='';
    }
}
