﻿// JScript 文件

function showReferencePop(popid , blockid,eventObj,bk)
{

   if(bk == null)
   {
        var block = document.getElementById(blockid) ;
        if(block!=null)
          block.setAttribute('popid',popid ) ;
        
        setOkAction(popid, function(){addReferneceWord('referenceWord','rtype','referenceBloks',null);hideCurrentPopup2();}) ;
        showPopup (popid , eventObj) ; 
    }
    else 
    {
       setOkAction(popid, function(){updateReferneceWord('referenceWord','rtype','referenceBloks',bk);hideCurrentPopup2();}) ;
       showPopup (popid , eventObj) ;
    }
}
function setOkAction(popid, action)
{
    var pop = document.getElementById(popid) ;
    if(pop == null)
       return ;
    
  
    var btns =pop.getElementsByTagName('input') ;  
    if(btns.lenght <= 0 )
      btns =pop.getElementsByTagName('INPUT') ;  
    if(btns.lenght <= 0 )
      btns =pop.getElementsByTagName('Input') ;  
      
     for( i = 0 ; i <btns.length ; i ++ )
     {
        if(btns[i].getAttribute('type') == null)
           continue ;
        if(btns[i].getAttribute('type')=='button' ||btns[i].getAttribute('type')=='BUTTON')
          {
             if(btns[i].getAttribute('btype') == null)
                continue ;
             if(btns[i].getAttribute('btype') == 'OK'|| btns[i].getAttribute('btype') == 'ok')
                {
                  
                  // btns[i].onclick=function(action){ }  ;
                  btns[i].onclick=action;
                   break; 
                }
          }
   
     }
}
function updateReferneceWord(referenceWordid, rtypeid , blockid, bk)
{
    var referenceword = document.getElementById(referenceWordid) ;
    var rtype = document.getElementById(rtypeid) ;
    var block = document.getElementById(blockid) ;
    
    if(referenceword == null)
      return ;
    if(rtype == null)
      return ;
    if(block == null)
      return ;
    if(bk == null)
        return ;
    if(bk.parentNode == null)
      return ;
    bk.parentNode.setAttribute('refword' , referenceword.value) ;
    bk.parentNode.setAttribute('refType' , rtype.value) ;
    bk.innerHTML =  referenceword.value + "::"+ getTypeName(rtype.value) ;
  
      
}
function addReferneceWord(referenceWordid, rtypeid , blockid, bk)
{
    var referenceword = document.getElementById(referenceWordid) ;
    var rtype = document.getElementById(rtypeid) ;
    var block = document.getElementById(blockid) ;
    
    if(referenceword == null)
      return ;
    if(rtype == null)
      return ;
    if(block == null)
      return ;
      
    block.setAttribute('referenceWordid',referenceWordid ) ;
    block.setAttribute('rtypeid',rtypeid ) ;
    if(referenceword.value == '')
       {
          alert("请您填写词目") ;
          return ;
       }
    if(rtype.value == '')
       {
          alert("请您选择词目的类型") ;
          return ;
       }
       
      block.innerHTML += createblock(referenceword.value,rtype.value) ; 
      
}
function selectBlock(bk,evt)
{

 if(bk == null)
    return ;
  if(bk.parentNode == null)
      return ;
  if(bk.parentNode.parentNode == null)
     return ;
   
  var rootblock = bk.parentNode.parentNode ;
  var popid = rootblock.getAttribute('popid') ;
  var referenceWordid = rootblock.getAttribute('referenceWordid') ;
  var rtypeid = rootblock.getAttribute('rtypeid') ;
  
  var rword =  bk.parentNode.getAttribute('refword') ;
  var rtype =  bk.parentNode.getAttribute('refType') ;
  
 
  
  document.getElementById(referenceWordid).value = rword ;
  document.getElementById(rtypeid).value = rtype ;
  
  setTypeByValue(popid , rtype) ;
 
    
  showReferencePop (popid ,rootblock.getAttribute('id'), evt, bk) ; 
}
function setTypeByValue(tableid, value)
{
   var table = document.getElementById(tableid) ;
   if(table == null)
      return ;
    var radios =table.getElementsByTagName('input') ;  
    if(radios.lenght <= 0 )
      radios =table.getElementsByTagName('INPUT') ;  
    if(radios.lenght <= 0 )
      radios =table.getElementsByTagName('Input') ;  
      
     for( i = 0 ; i <radios.length ; i ++ )
     {
         if(radios[i].getAttribute("type")=='radio' || radios[i].getAttribute("type") == 'RADIO')
         { 
             if(radios[i].value==value)
             {
                radios[i].checked = true ;
                break; 
        
              }
          }
   
     }
    
}

function deleteBolck(bk)
{
  if(bk == null)
    return ;
  if(bk.parentNode == null)
      return ;
  if(bk.parentNode.parentNode == null)
     return ;
   bk.parentNode.parentNode.removeChild(bk.parentNode) ;  
}
function  collectRefWords(refWordInputsid , referenceBloksid) 
{
    var refWordInputs = document.getElementById(refWordInputsid) ;
    var referenceBloks =  document.getElementById(referenceBloksid) ;
   
    if(refWordInputs == null ||referenceBloks == null )
        return ;
        
    var divs =referenceBloks.getElementsByTagName('div') ;  
    if(divs.lenght <= 0 )
      divs =referenceBloks.getElementsByTagName('DIV') ;  
    if(divs.lenght <= 0 )
      divs =referenceBloks.getElementsByTagName('Div') ;  
    var xml = "[refWords]" ;
    for(i = 0 ; i < divs.length ; i ++)
    {
         if(divs[i].getAttribute('refword') == null ||divs[i].getAttribute('refType') == null)
            continue ;
         xml += "[word value=\""+divs[i].getAttribute('refword')+"\" type=\""+divs[i].getAttribute('refType') +"\"     /]" ;
    }   
    xml += "[/refWords]"   ;
    refWordInputs.value = xml;
}
function createblock(refWord , refType)
{
    var html = "<div  refword='"+refWord+"' refType='"+refType+"' style='height:20px;border:1px;border-color:#6b90da;border-style:solid;cursor:pointer;background:#ebeff9;width:auto'>" ;
    html +="词目:<label class='main' onclick=\"selectBlock(this,event)\">"+ refWord + "::"+ getTypeName(refType)+ "</label>&nbsp;&nbsp;&nbsp;<label style='float:right' onclick='deleteBolck(this)' class='main'>×</label>" ;
    html += "</div>" ;
    return html ;
}
function getTypeName(rtype)
{
  if(rtype=='project')
     return '项目' ;
     if(rtype=='research')
     return '科研' ;
     if(rtype=='document')
     return '出版' ;
     if(rtype=='news')
     return '新闻' ;
     if(rtype=='staff')
     return '人名' ;
     if(rtype=='area')
     return '地域' ;
     if(rtype=='unit')
     return '所名' ;
   
     return rtype ;
  
}

