
var taUpdated = false;
var interval = window.setInterval("countdown()",500);
var time = 1

var changedBoxes = [];

function countdown()
{ 
try
   { 
      if (time >= 199) {
  	    time = 1;
      }	
      
	  if(document.getElementById("statusbar") != null)
	      document.getElementById("statusbar").width = time;
      
      if (time <= 0)
      {
        window.clearInterval(interval);
      }
      time = time + 5;
      }
  catch(err)
  {
  }
}
                   

function showpreview()
{
    layout = document.all.page_descr.options[document.all.page_descr.selectedIndex].value ;
    window.open("preview.asp?id=" + layout,"preview","width=800,height=600,scrollbars=yes,resizable=no,toolbar=no,statusbar=no,location=no");
}

function confirmDelete(submiturl)
{
	a = confirm("Weet u zeker dat u dit item wilt verwijderen?");
	if (a) document.location=submiturl;
}

function confirmReturn(submiturl)
{
	a = confirm("Weet u zeker dat u dit item wilt terugzetten?");
	if (a) document.location=submiturl;
}

function fillText(text, elementName)
{
	//alert("filltext elementname=" + elementName);
	document.getElementById(elementName).innerHTML = text;
	submittedEditors++;
	window.status = "Received contents from editor " + elementName + "(" + submittedEditors + "/" + totalEditors + ")";
}

function submit(formName)
{
	document.getElementById(formName).submit();
}

function waitForUpdate(fieldName,formName)
{
	window.setInterval("waitForUpdateInterval('"+formName+"')",1000);
	document.getElementById("cedit_" + fieldName).contentWindow.document.getElementById('Form1').submit();
}

function waitForUpdateInterval(formName)
{
	if (submittedEditors >= totalEditors)
	{
		submit(formName); 
		window.removeInterval("waitForUpdateInterval('"+formName+"')");
	}
}

function openHelpWindow(uri)
{
	helpWindow = window.open(uri,"","width=800,height=600,scrollbars=auto,resizable=yes,toolbar=no,statusbar=yes,location=no");
}

function ShowChilds(parentDivName)
{
	//alert('showing ' + parentDivName);
	parentElement = document.getElementById(parentDivName + "_select");

    for (var i = 0; i < parentElement.length; i++) 
    {
		childDiv = document.getElementById(parentElement.options[i].value+"_div");
		if (childDiv)
		{
			if (parentElement.options[i].selected)
			{
				childDiv.style.display = "block";
			}
			else
			{
				childDiv.style.display = "none";
				HideAllChilds(parentElement.options[i].value);
			}
        }
    }

}

function HideAllChilds(divName)
{
	var parentElement2 = document.getElementById(divName + "_select");
	if (parentElement2)
	{
		for (var i = 0; i < parentElement2.length; i++) 
		{
			childDiv = document.getElementById(parentElement2.options[i].value + "_div");
				
			if (childDiv)
			{
				childDiv.style.display = "none";
				HideAllChilds(parentElement2.options[i].value);
			}
		}
	}
}

function shiftUp(selectboxName)
{
	
	var mySelectBox = document.getElementById(selectboxName);

	

	if (!mySelectBox) return;
	
	if (mySelectBox.options[0].selected) return; // cant go any further up
	
	for (var i = 0; i < mySelectBox.length; i++)
	{
		if (mySelectBox.options[i].selected)
		{
			// swap with the element above ourselves
			tempInnerText						= mySelectBox.options[i-1].innerText;
			tempValue							= mySelectBox.options[i-1].value;
			tempBgColor							= mySelectBox.options[i-1].style.backgroundColor;
			tempColor							= mySelectBox.options[i-1].style.color;
			
			mySelectBox.options[i-1].innerText	= mySelectBox.options[i].innerText;
			mySelectBox.options[i-1].value		= mySelectBox.options[i].value;
			mySelectBox.options[i-1].style.backgroundColor	= mySelectBox.options[i].style.backgroundColor;
			mySelectBox.options[i-1].style.color			= mySelectBox.options[i].style.color;
			
			
			mySelectBox.options[i].innerText	= tempInnerText;
			mySelectBox.options[i].value		= tempValue;
			mySelectBox.options[i].style.backgroundColor = tempBgColor;
			mySelectBox.options[i].style.color			 = tempColor;
			
			// update the selected item
			mySelectBox.options[i].selected		= false;
			mySelectBox.options[i-1].selected	= true;
		}
	}
	
	for(var i=0; i<changedBoxes.length; i++)
	    if(changedBoxes[i] == mySelectBox)
	        return;
	
	changedBoxes[changedBoxes.length] = mySelectBox;
}

function shiftDown(selectboxName)
{
	var mySelectBox = document.getElementById(selectboxName);
	if (!mySelectBox) return;
	
	if (mySelectBox.options[mySelectBox.length-1].selected) return; // cant go any further down
	
	for (var i = (mySelectBox.length-1); i >= 0; i--)
	{
		if (mySelectBox.options[i].selected)
		{
			// swap with the element above ourselves
			tempInnerText						= mySelectBox.options[i+1].innerText;
			tempValue							= mySelectBox.options[i+1].value;
			tempBgColor							= mySelectBox.options[i+1].style.backgroundColor;
			tempColor							= mySelectBox.options[i+1].style.color;
			
			mySelectBox.options[i+1].innerText	= mySelectBox.options[i].innerText;
			mySelectBox.options[i+1].value		= mySelectBox.options[i].value;
			mySelectBox.options[i+1].style.backgroundColor	= mySelectBox.options[i].style.backgroundColor;
			mySelectBox.options[i+1].style.color			= mySelectBox.options[i].style.color;
			
			
			mySelectBox.options[i].innerText	= tempInnerText;
			mySelectBox.options[i].value		= tempValue;
			mySelectBox.options[i].style.backgroundColor = tempBgColor;
			mySelectBox.options[i].style.color			 = tempColor;
			
			// update the selected item
			mySelectBox.options[i].selected		= false;
			mySelectBox.options[i+1].selected	= true;
		}
	}
	
	for(var i=0; i<changedBoxes.length; i++)
	    if(changedBoxes[i] == mySelectBox)
	        return;

	changedBoxes[changedBoxes.length] = mySelectBox;
}

function submitOrderChanges()
{
    // Check where we've made changes
    for(var i=0; i<changedBoxes.length; i++)
    {
        var length = changedBoxes[i].options.length;
        for(var j=0; j<length; j++)
            changedBoxes[i].options[j].selected = true;
    }
    
    document.getElementById("swapform").submit();
}

function selectAll()
{
	selectAllPagesOrder("");
	document.getElementById("swapform").submit();
}

function selectAllPagesOrder(element)
{
	if (element == "") element = "root";
	var mySelectbox = document.getElementById(element + "_select");
	
	if (!mySelectbox) return;
	
	for (var i = 0; i < mySelectbox.length; i++)
	{
		mySelectbox.options[i].selected = true;
		selectAllPagesOrder(mySelectbox.options[i].value);
	}
}


function swapDiv(name)
{
	var el = document.getElementById(name);
	if (el.style.display == "block")
	{
		el.style.display = "none";
	}
	else
	{
		el.style.display = "block";
	}
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function loadSidebarState()
{
	document.getElementById("sidebar_menu_hide_or_show").style.width = 170;
   	
	document.getElementById("sidebar_menu_hide_or_show").style.height = window.document.body.clientHeight - 70;
    var p = readCookie("scrollbar_position");
	if (p > 0)
		document.getElementById("sidebar_menu_hide_or_show").scrollTop = p;
    window.setInterval("storeScrollbarPos()",500);
}

function storeScrollbarPos()
{
    createCookie("scrollbar_position",document.getElementById("sidebar_menu_hide_or_show").scrollTop,1);
}

function scroll()
{
  alert("I'm scrolling!");
}

function startFullScreenEdit(originalTextArea)
{
	taWindow = window.open("../javascript/TextAreaPopup.htm","","statusbar=no,toolbar=no,locationbar=no,resizable=yes,fullscreen=yes,scrollbars=no");
	taWindow.onLoad = fillFullScreenEdit(originalTextArea, taWindow);
}

function fillFullScreenEdit(originalTextArea, windowHandle)
{
	var orgTextElement = document.getElementById(originalTextArea);
	windowHandle.document.getElementById("ta").innerText = orgTextElement.innerText;
	windowHandle.document.getElementById("elementNameOnSave").value = originalTextArea;
}

var waitingForMenudiv;
var openDiv;
var countdown2;

function openPopupDiv(name,x,y)
{
	if (openDiv)
	{
	  var oDiv = document.getElementById(openDiv);
	  oDiv.style.display = "none";
	  clearInterval(waitingForMenudiv);
	  openDiv = null;
	}
	var theDiv = document.getElementById(name);
	if (theDiv.style.display == "none")
	{
	theDiv.style.left = x;
	theDiv.style.top = y;
	theDiv.style.display = "block";
	}
	else theDiv.style.display = "none";
	
	waitingForMenudiv = window.setInterval("waitForHiding('" + name + "')",5);
	openDiv = name;
	countdown2 = 10;
}

function updateMousePos(e)
{
	mouseX = (window.event) ? window.event.x : e.pageX;
    mouseY = (window.event) ? window.event.y + document.body.scrollTop : e.pageY;
}

if (document.layers)
    document.captureEvents(Event.MOUSEMOVE);
    
document.onmousemove = updateMousePos; 
// hier boven
function waitForHiding(divname)
{
	// check if mouse is in div
	var theDiv = document.getElementById(divname);
	if (mouseX < replace("x","",replace("p","",theDiv.style.left)) || 
	     mouseY < (replace("x","",replace("p","",theDiv.style.top)) - 50) ||
	     mouseX > (replace("x","",replace("p","",theDiv.style.left)) + 150) || 
	     mouseY > (replace("x","",replace("p","",theDiv.style.top)) + 200) )
		countdown2--;
	else
		countdown2 = 100;
		
	if (countdown2 == 0)
	{
		theDiv.style.display = "none";
		//open = null;
		clearInterval(waitingForMenudiv);
	}
}

function replace (inchar, outchar, instr) {
  var outstr = "";
  for (var i = 0, j = 0; i < instr.length; i++)
    if (instr.charAt(i) == inchar) {
      outstr += instr.substring(j,i) + outchar;
      j = i + 1;
    }
  return outstr + instr.substring(j,i);

}

function updateRightsManagement(selectBoxElement)
{
	document.location = "Default.aspx?page=admin/userrights&action=rightsoverview&selected_user=" + selectBoxElement.value;
}

function selectAllPages()
{
	var elements = document.getElementsByTagName("input");
	for (var i = 0; i < elements.length; i++)
	{
		elements[i].checked = true;
	}
}

function unselectAllPages()
{
	var elements = document.getElementsByTagName("input");
	for (var i = 0; i < elements.length; i++)
	{
		elements[i].checked = false;
	}
}

var totalEditors = 0;
var submittedEditors = 0;

function XHConn()
{
    var xmlhttp, bComplete = false;
    try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
    catch (e) { try { xmlhttp = new XMLHttpRequest(); }
    catch (e) { xmlhttp = false; }}}
    if (!xmlhttp) return null;
    this.connect = function(sURL, sMethod, sVars, fnDone)
    {
        if (!xmlhttp) return false;
        bComplete = false;
        sMethod = sMethod.toUpperCase();

        try
        {
            if (sMethod == "GET")
            {
                xmlhttp.open(sMethod, sURL+"?"+sVars, true);
                sVars = "";
            }
            else
            {
                xmlhttp.open(sMethod, sURL, true);
                xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
                xmlhttp.setRequestHeader("Content-Type",
                    "application/x-www-form-urlencoded");
            }
            xmlhttp.onreadystatechange = function()
                {
                if (xmlhttp.readyState == 4 && !bComplete)
                {
                    bComplete = true;
                    fnDone(xmlhttp);
                }
                };
            xmlhttp.send(sVars);
        }
        catch(z) { return false; }
        return true;
    };
    return this;
}+

function getContent()
{
	var content = tinyMCE.activeEditor.getContent({format : 'text'});
	return content;
}

/**
* 
*/
function codePressFixIFrames()
{
	var iframes = document.getElementsByTagName("iframe");
	for(i = 0; i < iframes.length; i++)
	{
		iframes[i].style.width = "722px";
		iframes[i].style.height = "240px";
	}
}

var totalEditors = 0;
var submittedEditors = 0;


function DeleteItem(table,field,key,id) 
{
    var conn = XHConn();
    conn.connect("/admin/Default.aspx", "GET", "page=admin/deleteItem&table="+table+"&field="+field+"&key="+key+"&id=" + id, function(a){});

    if ( confirm("Het item is verwijderd en het scherm moet worden vernieuwd. Dit houdt in dat uw al ingevulde gegevens verloren gaan. Wilt u het scherm verversen?"))
    {
       window.location.reload( false );

    }
} 

