// JavaScript Document



function guiGrid_Draw(GridName)
{
	var Cols=window["gridCols_"+GridName];
	var Data=window["gridData_"+GridName];
	
	totalWidth=document.getElementById('Grid_' + GridName).offsetWidth;
	totalHeight=document.getElementById('Grid_' + GridName).offsetHeight;
	
	var gui="<DIV ID='GridLabel_"+GridName+"' STYLE='height:40;width:100%;position:absolute;overflow:hidden;'>";

	xPos=0;
	for (t=1;t<Cols.length;t++)
	{
	   gui+="<DIV CLASS=edLabel STYLE='width:"+(Cols[t][4]-1)+";top:0;left:"+(xPos+1)+";height:100%;position:absolute;'>";
	   gui+=Cols[t][0];
		gui+="</DIV>";
		
		xPos+=Cols[t][4];
	}
	if (xPos<totalWidth)
	{
	   xRest=totalWidth-xPos-1;
	   gui+="<DIV CLASS=edLabel STYLE='width:"+xRest+";top:0;left:"+(xPos+1)+";height:40;position:absolute;'></DIV>";
	}
	gui+="</DIV>";

	ListHeight=totalHeight-60;

	gui+="<DIV ID='GridData_"+GridName+"' STYLE='height:"+ListHeight+";width:100%;top:40;position:absolute;overflow:auto;' onScroll=guiGrid_Scroll('"+GridName+"');>";
	selCount=0;
	for (t=0;t<Data.length;t++)
	{
	   yPos=t*20;
	   xPos=0;
	   for (c=1;c<Cols.length;c++)
	   {
	      gui+="<DIV ID=GridData_"+t+"_"+c+" CLASS=edCONT1";
			gui+="     STYLE='width:"+(Cols[c][4])+";top:"+yPos+";left:"+xPos+";height:20;position:absolute;word-wrap:normal;overflow:hidden;border:1 solid #FFFFFF;'";
			gui+="     onMouseOver=guiGrid_HoverLine('"+GridName+"',"+t+");>";

	   	gui+=Data[t][c];
			gui+="</DIV>";
			
			xPos+=Cols[c][4];
			
	   }
	   selCount++;
	}

	
	gui+="</DIV>";
	
	StatusYpos=ListHeight+40;
	var MSG="Selected Records: "+selCount+" / "+Data.length;
	
	gui+="<IFRAME SRC='Status.php?Action=Info&MSG="+MSG+"' ID='GridStatus_"+GridName+"' STYLE='height:20;width:100%;top:"+StatusYpos+";left:0;position:absolute;overflow:hidden;' SCROLLING=no></IFRAME>";

	totalWidth=document.getElementById('Grid_' + GridName).innerHTML=gui;
}

function guiGrid_Scroll(GridName)
{
	//NamesDiv  =document.getElementById("GridNames");
	labelDiv =document.getElementById("GridLabels");
	dataDiv=document.getElementById("GridData");
  scrTop=dataDiv.scrollTop;
  scrLeft=dataDiv.scrollLeft;

	//NamesDiv.scrollTop=scrTop;
	labelDiv.scrollLeft=scrLeft;
  createCookie('GridScrollTop_'+GridName,scrTop,1);
  createCookie('GridScrollLeft_'+GridName,scrLeft,1);	
}
function guiGridRestoreScroll(GridName)
{

	//NamesDiv  =document.getElementById("GridNames");
	labelDiv =document.getElementById("GridLabels");
	dataDiv=document.getElementById("GridData");
  scrTop=readCookie('GridScrollTop_'+GridName);
  scrLeft=readCookie('GridScrollLeft_'+GridName);

	//NamesDiv.scrollTop=scrTop;
	labelDiv.scrollLeft=scrLeft;
	dataDiv.scrollLeft=scrLeft;
	dataDiv.scrollTop=scrTop;
}


function guiGridHover(Line)
{
	   document.getElementById('GridData_'+Line).className="guiGridHover";
	   document.getElementById('GridName_'+Line).className="guiGridHover";

}
function guiGridUnHover(Line)
{
	   document.getElementById('GridData_'+Line).className="guiGridRow";
	   document.getElementById('GridName_'+Line).className="guiGridRowNames";
}

function guiToggleCheckbox(Name)
{

	
}
//------------------------------------------------------------------------------
// HTML Editor
//------------------------------------------------------------------------------
function ctrlHTMLEDIT_init(Obj)
{
	//var div=document.getElementById('div_'+id);
	var txt=Obj.innerHTML;
	var id=Obj.id;
	
	var ed="";
	//ed+="<INPUT ID="+id+" TYPE=HIDDEN VALUE=''>";
	
	ed+="<DIV CLASS=guiLabel>";

	ed+="<A HREF=javascript:htmlFormat('"+id+"','bold');><IMG SRC=img/icons/text_bold.png></A>";
	ed+="<A HREF=javascript:htmlFormat('"+id+"','italic');><IMG SRC=img/icons/text_italic.png></A>";
	ed+="<A HREF=javascript:htmlFormat('"+id+"','underline');><IMG SRC=img/icons/text_underline.png></A>";
	ed+="<IMG SRC=img/sys/seperate.gif>";

	ed+="<A HREF=javascript:htmlBlockFormat('"+id+"','<H1&gt;');><IMG SRC=img/icons/text_heading_1.png></A>";
  ed+="<A HREF=javascript:htmlBlockFormat('"+id+"','<H2&gt;');><IMG SRC=img/icons/text_heading_2.png></A>";
  ed+="<A HREF=javascript:htmlBlockFormat('"+id+"','<P&gt;');><IMG SRC=img/icons/heading_erase.png></A>";
  ed+="<IMG SRC=img/sys/seperate.gif>";

	ed+="<A HREF=javascript:htmlFormat('"+id+"','justifyleft');><IMG SRC=img/icons/text_align_left.png></A>";
	ed+="<A HREF=javascript:htmlFormat('"+id+"','justifycenter');><IMG SRC=img/icons/text_align_center.png></A>";
  ed+="<A HREF=javascript:htmlFormat('"+id+"','justifyright');><IMG SRC=img/icons/text_align_right.png></A>";
	ed+="<IMG SRC=img/sys/seperate.gif>";       

	ed+="<A HREF=javascript:htmlFormat('"+id+"','indent');><IMG SRC=img/icons/text_indent.png></A>";
	ed+="<A HREF=javascript:htmlFormat('"+id+"','outdent');><IMG SRC=img/icons/text_indent_remove.png></A>";
  ed+="<IMG SRC=img/sys/seperate.gif>";

	ed+="<A HREF=javascript:htmlFormat('"+id+"','copy');><IMG SRC=img/icons/page_copy.png></A>";
	ed+="<A HREF=javascript:htmlFormat('"+id+"','cut');><IMG SRC=img/icons/cut.png></A>";
  ed+="<A HREF=javascript:htmlFormat('"+id+"','paste');><IMG SRC=img/icons/page_paste.png></A>";
  ed+="<IMG SRC=img/sys/seperate.gif>";

	ed+="<A HREF=javascript:htmlFormat('"+id+"','undo');><IMG SRC=img/icons/arrow_undo.png></A>";
	ed+="<A HREF=javascript:htmlFormat('"+id+"','redo');><IMG SRC=img/icons/arrow_redo.png></A>";
	ed+="<A HREF=javascript:htmlFormat('"+id+"','RemoveFormat');><IMG SRC=img/icons/edit-clear.png></A>";
  ed+="<IMG SRC=img/sys/seperate.gif>";   

	ed+="<A HREF=javascript:htmlFormat('"+id+"','insertunorderedlist');><IMG SRC=img/icons/text_list_bullets.png></A>";
	ed+="<A HREF=javascript:htmlFormat('"+id+"','insertorderedlist');><IMG SRC=img/icons/text_list_numbers.png></A>";
  ed+="<IMG SRC=img/sys/seperate.gif>";

  ed+="<A HREF=javascript:htmlInsertImg('"+id+"','');><IMG SRC=img/icons/image_add.png></A>";
  ed+="<A HREF=javascript:htmlInsertObj('"+id+"','');><IMG SRC=img/icons/youtube.gif></A>";	
  ed+="<A HREF=javascript:htmlInsertSnd('"+id+"','');><IMG SRC=img/icons/sound.png></A>";
  ed+="<A HREF=javascript:htmlCleanUp('"+id+"')><IMG SRC=img/icons/cut.png></A>";


	ed+="</DIV>";
	
	
	height=Obj.offsetHeight;
	FrameHeight=height-20;
	ed+="<IFRAME frameborder=0 ID=html_"+id+" STYLE='border:1px solid #000000;width:100%;height:"+FrameHeight+";'></IFRAME>";
 
	//Editor in DIV-element plaatsen.
	Obj.innerHTML=ed;

	//Inhoud in de editor plaatsen
	var editor=document.getElementById('html_' + id).contentWindow.document;

	editor.open();
	editor.write("<HTML><HEAD><link rel=stylesheet href=HTMLEditor.css type=text/css></HEAD><BODY>");
	editor.write(txt);
	editor.write("</BODY></HTML>");
	editor.close();

	//Editor activeren
  editor.body.contentEditable = true;
  editor.designMode = 'on';
  
  Obj.getValue=ctrlHTMLEDIT_Get;
}

function htmlCleanUp(editor_id)
{
  document.getElementById('html_' + editor_id).contentWindow.document.body.innerHTML=htmlStripWordShit(document.getElementById('html_' + editor_id).contentWindow.document.body.innerHTML);
}

function htmlFormat(editor_id,cmd)
{
	// Simpele format commando's
   document.getElementById('html_' + editor_id).contentWindow.document.execCommand(cmd, false, null);
}
function htmlInsertImg(editor_id,cmd)
{
	
	var img=BaseUrl+PopImgDialog();
   document.getElementById('html_' + editor_id).contentWindow.document.execCommand('insertimage', false, img);
}

function htmlInsertObj(editor_id,cmd)
{
	// Simpele format commando's
	var objhtml=prompt("Voer htmlcode in voor youtube- ('insluiten') of ander mediaobject.");
if (document.all) {
var oRng = document.getElementById('html_' +editor_id).contentWindow.document.selection.createRange( );
oRng.pasteHTML(objhtml);
oRng.collapse(false);
oRng.select();
} else {
document.getElementById('html_' + editor_id).contentWindow.document.execCommand('inserthtml',false, objhtml);
}

   
}

function htmlInsertSnd(editor_id,cmd)
{
   
 	// Simpele format commando's
 	var soundfilename=BaseUrl+PopSndDialog();
 	
 	var objhtml='<P><embed src='+soundfilename+' AUTOSTART=false HIddEN=false LOOP=false></P>---';
 	
   if (document.all) {
     var oRng = document.getElementById('html_' +editor_id).contentWindow.document.selection.createRange( );
     oRng.pasteHTML(objhtml);
     oRng.collapse(false);
     oRng.select();
   } else {
     document.getElementById('html_' + editor_id).contentWindow.document.execCommand('inserthtml',false, objhtml);
   }
 
//    
}


function htmlBlockFormat(editor_id,tag)
{
	//formateer een blok door er de aangegeven tag omheen te zetten.
	document.getElementById('html_' + editor_id).contentWindow.document.execCommand('formatblock', false, tag);
	
}

function ctrlHTMLEDIT_Get()
{
	var text=document.getElementById('html_' + this.id).contentWindow.document.body.innerHTML;
  //text=htmlStripWordShit(text);
	//text=text.replace(/http:/g,'[URL:]');
	
	return text;
}

function htmlStripWordShit(html)
{
//alert ('VOOR:'+html);

  var ftags= '|link|meta|/xml';
  //    ftags+='|/h1|/h2|/h3|/span|/style|/b|/br|/i|/u|/ul|/ol|/li|/a|/img|/font|/p|/em|/strong|/object|/param|/embed';


htmlA=html.split('<');
html='';
xml=false;

for (i in htmlA)
{

  var pos=htmlA[i].search(/ |>/);
  var Tag=htmlA[i].substr(0,pos);
  Tag=Tag.toLowerCase();
  

  if (Tag=='xml') xml=true;
  if (Tag=='/xml') xml=false;
  if (xml)
  {
    //XML - voorlopig helemaal niets mee doen.
  }
  else
  {
    // HTML - verboden links
    if (ftags.indexOf('|'+Tag)==-1)
    {
      // dit mag dus wel
      if (i>0) html+='<';
      html+=htmlA[i];
    
    }  
    else
    {
      //dit mag dus niet
      htmlB=htmlA[i].split('>');
      if (htmlB[1]>'') html+=htmlB[1];
    }

  }
}

//alert ('NA:'+html) ;   
      return html;

}

function StripUnwantedHtml(html)
{
alert ('VOOR:'+html);

  var ftags= '|!-- |h1 |h2 |h3 | span |style |b |br |i |u |ul |ol |li |a |img |font |p |em |strong |object |param |embed ';
      ftags+='|/h1|/h2|/h3|/span|/style|/b|/br|/i|/u|/ul|/ol|/li|/a|/img|/font|/p|/em|/strong|/object|/param|/embed';


htmlA=html.split('<');
html='';
xml=false;

for (i in htmlA)
{

  var pos=htmlA[i].search(/ |>/);
  var Tag=htmlA[i].substr(0,pos);
  Tag=Tag.toLowerCase();
  
  //fully allowed tags
  if (Tag=='xml') xml=true;
  if (Tag=='/xml') xml=false;
  if (xml)
  {
    //XML - voorlopig helemaal niets mee doen.
  }
  else
  {
    // HTML
    if (ftags.indexOf('|'+Tag)>-1)
    {
      if (i>0) html+='<';
      html+=htmlA[i];
    
    }  
    else
    {
      htmlB=htmlA[i].split('>');
      html+=htmlB[1];
    }
  }
}

alert ('NA:'+html) ;   
      return html;
}

//------------------------------------------------------------------------------
//POPUP Dialogs
//------------------------------------------------------------------------------

function OpenDialog(w,h,title,page,modal)
{
	var DBox=document.getElementById('DialogDiv');
	DBox.style.height=w;
	DBox.style.width=h;
	DBox.style.top=100;
	DBox.style.left=100;
	Content='<IFRAME HEIGHT=100% WIDTH=100% SRC='+page+'></IFRAME>';
	document.getElementById('DialogContent').innerHTML=Content;
	document.getElementById('DialogTitle').innerHTML=title;
	DBox.style.visibility='visible';
}
function CloseDialog()
{
	var DBox=document.getElementById('DialogDiv');
	document.getElementById('DialogContent').innerHTML='';
	DBox.style.visibility='hidden';
}
function PopImgDialog()
{
  page=BaseUrl+"/pupFilePick.php?rootdir=/img/content"; //?BlkID="+BlockFldr;
  RetVar = window.showModalDialog(page, "", "dialogHeight: 600px; dialogWidth: 400px; edge: Sunken; center: Yes; help: No; resizable: No; status: No;scroll: No;");

  return RetVar;
}
function PopSndDialog()
{
  page=BaseUrl+"/pupSoundPick.php?rootdir=/snd/content"; //?BlkID="+BlockFldr;
  RetVar = window.showModalDialog(page, "", "dialogHeight: 600px; dialogWidth: 400px; edge: Sunken; center: Yes; help: No; resizable: No; status: No;scroll: No;");

  return RetVar;
}
//------------------------------------------------------------------------------
// TAB Controls
//
 
function tabSelect(ctrlName,idxTabtoSet)
{

  for(i in window['tabList'+ctrlName])
  {
 
    var TabName=window['tabList'+ctrlName][i][0];
  
    document.getElementById(TabName).style.visibility ='hidden';
    if (i==idxTabtoSet) document.getElementById(TabName).style.visibility ='visible';
  }
} 
//------------------------------------------------------------------------------
// andere rotzooi

    function getElementTop(Elem) {
    
  
        if(document.getElementById) {    
            var elem = document.getElementById(Elem);
        } else if (document.all) {
            var elem = document.all[Elem];
        }

        var yPos = elem.offsetTop;
        //tempEl = elem.offsetParent;
        //while (tempEl != null) {
        //      yPos += tempEl.offsetTop;
        //      tempEl = tempEl.offsetParent;
        //  }
        return yPos ;
    
    }
        function getElementAbsTop(Elem) {
    
  
        if(document.getElementById) {    
            var elem = document.getElementById(Elem);
        } else if (document.all) {
            var elem = document.all[Elem];
        }

        var yPos = elem.offsetTop;
        tempEl = elem.offsetParent;
        while (tempEl != null) {
              yPos += tempEl.offsetTop;
              tempEl = tempEl.offsetParent;
          }
        return yPos ;
    
    }
    
//------------------------------------------------------------------------------
// CHECKBOX

function ctrlCHECKBOX_init(Obj)
{
  Obj.Toggle=ctrlCHECKBOX_toggle;
  Obj.setValue=ctrlCHECKBOX_set;
  Obj.getValue=ctrlCHECKBOX_get;
  Obj.onclick=ctrlCHECKBOX_toggle;
    
  Obj.setValue(Obj.getValue());
} 

function ctrlCHECKBOX_toggle()
{ 
	if (this.getValue()==0)
     this.setValue(1);
	else
     this.setValue(0);
}
function ctrlCHECKBOX_set(set_val)   
{  
	if (set_val==1)
	   this.src='img/sys/checked.gif';
	else
	{
	   set_val=0;
	   this.src='img/sys/not_checked.gif';
	}
  this.setAttribute('value',set_val);      
}
function ctrlCHECKBOX_get()   
{
  return this.getAttribute('value');	      
}

//------------------------------------------------------------------------------
//IMG
function ctrlIMG_init(Obj)
{
  Obj.getValue=ctrlIMG_get;
}

function ctrlIMG_get()
{
  return this.src;
}

//------------------------------------------------------------------------------
//TABCTRL


function ctrlTABSelect(ctrlName,idxTabtoSet)
{
  for(i in window['tabList'+ctrlName])
  {
 
    var TabName=window['tabList'+ctrlName][i][0];
  

    if (i==idxTabtoSet) 
    {
      document.getElementById(TabName).style.visibility ='visible';
      document.getElementById(ctrlName+'_'+TabName).className ='ctrlTABS_up';
    }
    else
    {
      document.getElementById(TabName).style.visibility ='hidden';  
      document.getElementById(ctrlName+'_'+TabName).className ='ctrlTABS_down';  
    }
  }
    
}

//------------------------------------------------------------------------------
//BOX

function ctrlBOX_init(Obj)
{
  var divd=Obj.getAttribute('divider');
  
  if (divd>'')
  {
    divd=divd.split(' ');
    DivObj=document.getElementById('divider_'+Obj.id);
    if (DivObj)
      if (divd[0]=='Y')
      { 
        DivObj.Update=_YDivider_Update;
      }
      else
      {
        DivObj.Update=_XDivider_Update;
      }
    if (divd[2]=='DRAG') makeDraggable(DivObj);
  }

}

function _YDivider_Update()
{
  //----------------------------------------------------------------------------
  //Update repaints the root object and al it's children
  
  // We zoeken eerst de updateroot, dat is bijvoorbeeld bij een divider de BOX
  // waar de divider toe behoort
  
  
  
  UpdRoot=document.getElementById(this.getAttribute('updateroot'));

  this.style.left=0;
  this.style.width='100%';
  if (this.offsetTop<0) this.style.top=0;
    else if (this.offsetTop>(UpdRoot.offsetHeight-4)) this.style.top=(UpdRoot.offsetHeight-4);

  UpdRoot.setAttribute('divider','Y '+this.offsetTop+' DRAG');
  DoUpdate(UpdRoot);
}
function _XDivider_Update()
{
  //----------------------------------------------------------------------------
  //Update repaints the root object and al it's children
  
  // We zoeken eerst de updateroot, dat is bijvoorbeeld bij een divider de BOX
  // waar de divider toe behoort

  UpdRoot=document.getElementById(this.getAttribute('updateroot'));
  
  this.style.top=0;
  this.style.height='100%';
  if (this.offsetLeft<0) this.style.left=0;
    else if (this.offsetLeft>(UpdRoot.offsetWidth-20)) this.style.left=(UpdRoot.offsetWidth-20);

  UpdRoot.setAttribute('divider','X '+this.offsetLeft+' DRAG');
  DoUpdate(UpdRoot);
}
