// **************************************************************
// Konstruktor upPortlet Objekt
// **************************************************************

var colArray = new Array(); //array holding selectboxes
var colbox=null;
var oColPool=null;

nn4 = (document.layers)? true:false
ie4 = (document.all)? true:false

function init()
{
   k=0;
   isFirst=true;
   for(j=0; j<document.forms[0].elements.length; j++)
   {
      elem=document.forms[0].elements[j];
      if(elem.type.toLowerCase().indexOf("select")!=-1 && elem.name!="colPool")
      {
           colArray[k]=elem;
           if (isFirst)
           {
             colbox=elem;
             isFirst=false;
           }
           k=k+1;
      }
   }

   if (nn4)
   {
      oColPool= document.layers["UpCoverDiv"].document.forms[0].colPool;
   }
   else
   {
      oColPool=document.forms[0].colPool;
   }
}

/*
function editEntry()
{
   index=colbox.selectedIndex;
   if (index==-1) return;

   portId=colbox.options[index].value;
   url2open="_UpPortletProperties.asp?id="+portId
   winConfig = window.open(url2open,"winConfig","width=600,height=400,left=0,top=0, location=no, menubar=no, toolbar=no, resizable=yes");
}
*/

function configPortlet(portId)
{
   if (portId=="")
      return;
   url2open="_UpPortletProperties.asp?id="+portId
   winConfig = window.open(url2open,"winConfig","width=600,height=400,left=0,top=0, location=no, menubar=no, toolbar=no, resizable=yes");
}

function configPortletCRM(portId)
{
   if (portId=="")
      return;
   url2open="../../../_UpInclude/script/UpCrm/Portal/_UpPortletProperties.asp?id="+portId
   winConfig = window.open(url2open,"winConfig","width=600,height=430,left=0,top=0, location=no, menubar=no, toolbar=no, resizable=yes");
}


//**********************************************************
// remove selection from options when switching to other selectbox
//**********************************************************
function setCol(oColBox)
{
   if (colbox!=oColBox)
   {
      if(colbox!=null && colbox.selectedIndex != -1)
      {
         index = colbox.selectedIndex;
         colbox.selectedIndex = -1;
      }
      colbox= oColBox;
   }
}

//**********************************************************
// preparing to submit changes back to server
//**********************************************************
function prepareSubmit()
{
   dataField= document.forms[0].portsInCols;
   dataFieldPool = document.forms[0].portsInPool;
 
   strData="";
   for(i=0; i<colArray.length; i++)
   {
      if(i!=0) strData=strData+"|"; //separator Columns
      for(j=0; j< colArray[i].options.length; j++)
      {
         if(j!=0) strData=strData+" "; //separator
         strData= strData + colArray[i].options[j].value; //option-value (= Portlet-ID)
      }
   }
   dataField.value = strData;

   strData="";
   for(j=0; j< oColPool.options.length; j++)
   {
      if(j!=0) strData=strData+" "; //separator
      strData= strData + oColPool.options[j].value; //option-value (= Portlet-ID)
   }
   dataFieldPool.value = strData;
   document.forms[0].col1.selectedIndex = -1;
   document.forms[0].col2.selectedIndex = -1;
   document.forms[0].col3.selectedIndex = -1;
}

//**********************************************************
// insert and remove portlets in columns
//**********************************************************
function insert(strTxt, strValue, intPos)
{
   pos=colbox.selectedIndex;
   len=colbox.length;
   posNew=len;

   if (strTxt==null) return false;
   if (strValue==null) return false;

   if (pos==-1)
   {
      if(intPos==null || intPos>=len || intPos<0)
      {
        posNew=len;
        pos=len;
      }
      else
      {
        pos=intPos-1;
        posNew=pos+1;
      }
   }
   else posNew= pos+1;

   newEntry = new Option();
   colbox.options[len]= newEntry;

   for(i=len; i>pos; i--)
   {
       if (i==0) break;
       colbox.options[i].text=colbox.options[i-1].text;
       colbox.options[i].value=colbox.options[i-1].value;
   }

   colbox.options[posNew].text=strTxt;
   colbox.options[posNew].value= strValue;
   colbox.options[posNew].selected=true;
}

function remove()
{
   pos=colbox.selectedIndex;
   len=colbox.length;
   if (len==0) return;
   if (pos==-1) return;
   posNew=pos;

   if (pos==len-1) posNew=pos-1;
   else posNew=pos;
   for(i=pos; i<len-1; i++)
   {
       colbox.options[i].text=colbox.options[i+1].text;
       colbox.options[i].value=colbox.options[i+1].value;
   }

   colbox.options[len-1]=null;
   if (colbox.length>0) colbox.options[posNew].selected=true;
   return true;
}

//**********************************************************
// move portlets between columns left and right
// move portlets from unselected column to column one
//**********************************************************
function addToPortal(intAddToCol)
{
   if(intAddToCol==null || intAddToCol=="") intAddToCol=0;
   colbox=oColPool;
   pos=colbox.selectedIndex;
   len=colbox.length;
   if (pos==-1) return;

   strTxt=colbox.options[pos].text;
   strValue=colbox.options[pos].value;
   remove();
   colbox.selectedIndex=-1;
   colbox=colArray[intAddToCol];
   insert(strTxt, strValue, -1);
}

function removeFromPortal()
{
   pos=colbox.selectedIndex;
   len=colbox.length;
   if (pos==-1) return;

   strTxt=colbox.options[pos].text;
   strValue=colbox.options[pos].value;
   remove();
   colbox.selectedIndex=-1;
   colbox= oColPool;
   insert(strTxt, strValue, -1);

}

//**********************************************************
// move portlets between columns left and right
//**********************************************************
function moveLeft()
{
   pos=colbox.selectedIndex;
   len=colbox.length;
   colIndex=0;
   colIndexNew=0;
   if (pos==-1) return;
   if (colbox==oColPool) return;

   // find column in array
   for(i=0; i<colArray.length;i++)
   {
      if (colArray[i]==colbox)
      {
         colIndex=i;
         break;
      }
   }
// if source is 0 then
   if (colIndex==0) colIndexNew= colArray.length-1;
   else colIndexNew= colIndex-1;

   strTxt=colbox.options[pos].text;
   strValue=colbox.options[pos].value;
   remove();
   setCol(colArray[colIndexNew])
   insert(strTxt, strValue, pos);
}

function moveRight()
{
   pos=colbox.selectedIndex;
   len=colbox.length;
   colIndex=0;
   colIndexNew=0;
   if (pos==-1) return;
   if (colbox==oColPool) return;

// find column in array
   for(i=0; i<colArray.length;i++)
   {
      if (colArray[i]==colbox)
      {
         colIndex=i;
         break;
      }
   }
// if source is 0 then
   if (colIndex==colArray.length-1) colIndexNew= 0;
   else colIndexNew= colIndex+1;

   strTxt=colbox.options[pos].text;
   strValue=colbox.options[pos].value;
   remove();
   setCol(colArray[colIndexNew])
   insert(strTxt, strValue, pos);

}

//**********************************************************
// order portlets in columns by moving them up and down
//**********************************************************
function moveUp()
{
   pos=colbox.selectedIndex;
   len=colbox.length;
   posNew=0;
   if (pos==-1) return;
   if (colbox==oColPool) return;

   textTmp=colbox.options[pos].text;
   valueTmp=colbox.options[pos].value;

   if (pos==0)
   {
     posNew=len-1;

     for(k=1; k<colbox.length; k++)
     {
       colbox.options[k-1].text=colbox.options[k].text;
       colbox.options[k-1].value=colbox.options[k].value;
     }

   }
   else
   {
     posNew= pos-1;

     colbox.options[pos].text=colbox.options[posNew].text;
     colbox.options[pos].value=colbox.options[posNew].value;
   }

   colbox.options[posNew].text=textTmp;
   colbox.options[posNew].value=valueTmp;

   colbox.options[posNew].selected=true;
}

function moveDown()
{
   pos=colbox.selectedIndex;
   len=colbox.length;
   posNew=0;
   if (pos==-1) return;
   if (colbox==oColPool) return;

   textTmp=colbox.options[pos].text;
   valueTmp=colbox.options[pos].value;

   if (pos==len-1)
   {
     posNew=0;

     for(k=colbox.length-2; k>=0; k--)
     {
       colbox.options[k+1].text=colbox.options[k].text;
       colbox.options[k+1].value=colbox.options[k].value;
     }

   }
   else
   {
     posNew= pos+1;

     colbox.options[pos].text=colbox.options[posNew].text;
     colbox.options[pos].value=colbox.options[posNew].value;
   }


   colbox.options[posNew].text=textTmp;
   colbox.options[posNew].value=valueTmp;

   colbox.options[posNew].selected=true;
}
