﻿



var checkCount = 0; //当前选中的CHECK
//var clickCount = 0;
var checkBoxChilds = new Array();
var currentCheckedBox = null;

/* *
 * 加载checkbox对象到checkbox组中
 */
function add(checkEl)
{
   checkBoxChilds[checkBoxChilds.length] = document.getElementById(checkEl);

}



//function:insert(插入)  
//params:index(插入位置)
//params:checkEls(元素ID列表)
//return:无
function insert(checkEls,index)
{
   if(checkEls.length && checkEls.length > 0)
   {
      var sourceLength = checkBoxChilds.length;
      
      for(i=1;i<=sourceLength-index;i++)
      { 
         checkBoxChilds[sourceLength+checkEls.length-i] = checkBoxChilds[sourceLength-i];
      } 
      
      for(i=0;i<checkEls.length;i++)
      {
         checkBoxChilds[index+i]=document.getElementById(checkEls[i]); 
      } 
     
      
   }
}

function getOfferIdCount()
{

   value = document.getElementById("OfferID").value;

   if(value == null || value == 'null')
   {
      return 0;
   }

   var count = 1;
   while(value.indexOf(",") != - 1)
   {
      var position = value.indexOf(",");
      var len = value.length;
      value =  value.substring(position + 1, len);
      count ++ ;
   }
   return count;
}


//  ---------------------------------------

function clickcompareBox(id, box,checkBoxValue)
{
   if(box.checked)
   {
      if(getOfferIdCount() > 20)
      {        
         alert('您已经选择了20条信息，不能超过20个');
         box.checked = false;
      }
      else
      {
         addOfferId(id,checkBoxValue);
         currentCheckedBox = box;
      }
   }
   else
   {
      delOfferId(id);
   }
}

//  ---------------------------------------

function getOfferIdCount()
{

   value = document.getElementById("OfferID").value;

   if(value == null || value == 'null')
   {
      return 0;
   }

   var count = 1;
   while(value.indexOf(",") != - 1)
   {
      var position = value.indexOf(",");
      var len = value.length;
      value =  value.substring(position + 1, len);
      count ++ ;
   }
   return count;
}

//  ---------------------------------------

/*
 * 选中CheckBox
 */
function addOfferId(arg,checkBoxValue)
{
   arg  = arg ;

   if(hasOfferId(arg))
   {
      return;
   }
   if(arg == "")
   {
      return;
   }
   
   
   //记录选中的checkbox的name值
   ckName = document.getElementById("OfferID").value
   if(ckName == null || ckName == "null" )
   {
      ckName = arg ;
   }
   else
   {
      ckName = ckName + "," + arg;
   }
   document.getElementById("OfferID").value = ckName;
   //记录选中的checkbox的value值 
   var ckValues=document.getElementById("OfferParentValue").value;
   if (ckValues==null || ckValues=="null")
   {
        ckValues=checkBoxValue
   }
   else
   {
        ckValues=ckValues+","+checkBoxValue;
   }
   document.getElementById("OfferParentValue").value=ckValues;
}

//  ---------------------------------------

/*
 * 删除CheckBox
 */
function delOfferId(arg)
{
   arg = arg;

   if( ! hasOfferId(arg))
   {
      //alert("操作错误");
      return;
   }

   ckName = document.getElementById("OfferID").value;
   var offerIds = stringToArray(ckName);
   var ckValues="";
   var offerValues=stringToArray(document.getElementById("OfferParentValue").value);
   var len = offerIds.length;
   var result = "";
   var counter = 0;
   for(var i = 0; i < len; i ++ )
   {
      if(offerIds[i] != arg)
      {
         if(counter == 0)
         {
            result = offerIds[i];
            ckValues=offerValues[i];
         }
         else
         {
            result = result + "," + offerIds[i];
            ckValues=ckValues+","+offerValues[i];
         }
         counter ++ ;
      }
   }
   document.getElementById("OfferID").value = result;
   document.getElementById("OfferParentValue").value=ckValues;
}

//  ---------------------------------------

/* *
 * 获取离当前checkbox最近的选中的checkbox的序列
 */
function getNearCheckedNum(checkEl)
{
   var checkElNum = getNum(checkEl);
   var upCheckedNum = getUpCheckedNum(checkElNum);
   var downCheckedNum = getDownCheckedNum(checkElNum);
   if(upCheckedNum != null && downCheckedNum != null)
   {
      if((checkElNum - upCheckedNum) <= (downCheckedNum - checkElNum))
      {
         return upCheckedNum;
      }
      else
      {
         return downCheckedNum;
      }
   }
   else if(upCheckedNum == null && downCheckedNum != null)
   {
      return downCheckedNum;
   }
   else if(upCheckedNum != null && downCheckedNum == null)
   {
      return upCheckedNum;
   }
   else
   {
      return checkElNum;
   }
}

//  ---------------------------------------

/* *
 * 获取当前check在checkBox中的序列
 */
function getNum(checkEl)
{
   for(var i = 0; i < checkBoxChilds.length; i ++ )
   {
      if(checkBoxChilds[i].id == checkEl.id)
      {
         return i;
      }
   }
}

//  ---------------------------------------

/* *
 * 向下获取最近的选中的checkbox的序列
 */
function getDownCheckedNum(num)
{
   for(var i = num + 1; i < checkBoxChilds.length;
   i ++ )
   {
      if(checkBoxChilds[i].checked)
      {
         return i;
      }
   }
   return null;
}

//  ---------------------------------------

/* *
 * 向上获取最近的选中的checkbox的序列
 */
function getUpCheckedNum(num)
{
   for(var i = num - 1; i >= 0; i -- )
   {
      if(checkBoxChilds[i].checked)
      {
         return i;
      }
   }
   return null;
}

//  ---------------------------------------

function stringToArray(str)
{
   var temp = str;
   var strArray = new Array();
   var count = 0;
   while(temp.indexOf(",") != - 1)
   {

      var position = temp.indexOf(",");

      strArray[count] = temp.substring(0, position);
      // alert( strArray[count] );
      count ++ ;
      var len = temp.length;
      temp =  temp.substring(position + 1, len);

   }
   strArray[count] = temp;
   return strArray;
}

//  ---------------------------------------

function hasOfferId(arg)
{
   var value = document.getElementById("OfferID").value;
   // alert(value + "," + arg + "," + value.indexOf(arg));
   if(value == "" || value.indexOf(arg) == - 1 )
   {
      return false;
   }
   else
   {
      return true;
   }
}

//  ---------------------------------------

function changecheckStat(checkEl)
{

//   if(clickCount == 0)
//   {
//      init(checkEl);
//   }
//   clickCount ++ ;
  if(checkEl)
  {
   if(checkEl.checked)
   {
      checkCount ++ ;
      if(checkCount >= 2)
      {
         showFloatDiv(checkEl);
      }
      else
      {
         hiddenFlaotDiv();
      }
   }
   else
   {
      checkCount -- ;
      if(checkCount >= 2)
      {
         showFloatDiv(getCheck(getNearCheckedNum(checkEl)));
         
       
      }
      else
      {
         hiddenFlaotDiv();
      }
   }
   }
}

//  ---------------------------------------

/* *
 * 根据在checkBox中的序列返回相应的check对象
 */
function getCheck(num)
{
   return checkBoxChilds[num];
}

//  ---------------------------------------

/* *
 * 获取对象el的X, Y坐标
 * @param {Object} el
 */
function getXY(el)
{
   var pos;
   if(getExplorerType() == 1)
   {
      var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
      var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
      pos = [el.getBoundingClientRect().left + scrollLeft, el.getBoundingClientRect().top + scrollTop];
   }
   else
   {
      pos = [el.offsetLeft, el.offsetTop];
      var parentNode = el.offsetParent;
      if (parentNode != el)
      {
         while (parentNode)
         {
            pos[0] += parentNode.offsetLeft;
            pos[1] += parentNode.offsetTop;
            parentNode = parentNode.offsetParent;
         }
      }
      if (el.parentNode)
      {
         parentNode = el.parentNode;

      }
      else
      {
         parentNode = null;

      }
      while (parentNode && parentNode.tagName.toUpperCase() != 'BODY' && parentNode.tagName.toUpperCase() != 'HTML')
      {
         if (parentNode.style.display != 'inline')
         {
            pos[0] -= parentNode.scrollLeft;
            pos[1] -= parentNode.scrollTop;
         }
         if (parentNode.parentNode)
         {
            parentNode = parentNode.parentNode;
         }
         else
         {
            parentNode = null;

         }
      }
   }
   return pos;
}

//  ---------------------------------------

function getExplorerType()
{
   var ua = navigator.userAgent.toLowerCase();
   if(window.ActiveXObject)
   {
      return 1;
   }
   else if((ua.indexOf('firefox') > - 1))
   {
      return 2;
   }
   else if((ua.indexOf('opera') > - 1))
   {
      return 3;
   }
}

//  ---------------------------------------

function init(checkEl)
{
   for(var i = 0; i < this.checkBoxChilds.length;
   i ++ )
   {
      if(this.checkBoxChilds[i].checked == true)
      {
         this.checkCount ++ ;
      }
   }
   if(checkEl.checked == true)
   {
      this.checkCount = this.checkCount - 1;
   }
   if(checkEl.checked == false)
   {
      this.checkCount = this.checkCount + 1;
   }
}

//  ---------------------------------------

/* *
 * 隐藏提示浮动层
 */

function hiddenFlaotDiv()
{
   document.getElementById("tishiDiv").style.display = "none";
}

//  ---------------------------------------

/* *
 * 显示并定位提示浮动层
 */
function showFloatDiv(checkEl)
{
   document.getElementById("tishiDiv").style.display = "block";
   document.getElementById("tishiDiv").style.position = "absolute";
   document.getElementById("tishiDiv").style.top = getXY(checkEl)[1] - 70 + "px";
   document.getElementById("tishiDiv").style.left = getXY(checkEl)[0] - 10 + "px";
}

/**
 * 全选
 */
function selAll(ckAll)
{
    if (ckAll.checked)//全选
    {
        for(var i = 0; i < checkBoxChilds.length; i ++)
       {
          checkBoxChilds[i].checked=true;
          clickcompareBox(checkBoxChilds[i].name,checkBoxChilds[i],checkBoxChilds[i].value);
          changecheckStat(checkBoxChilds[i]);
       }
   }
   else//全消
   {
        for(var i = 0; i < checkBoxChilds.length; i ++)
       {
          checkBoxChilds[i].checked=false;
          document.getElementById("OfferID").value="";
          document.getElementById("OfferParentValue").value="";
          hiddenFlaotDiv();//隐藏提示层
       }
   }
}

