// functie voor het tegelijk selecteren rechten bij rollen toevoegen / wijzigen
function selectroldeel(checkbox) 
{
  i=0; 
  while (i < document.form_cms.length)
	{
	  document.form_cms.elements[i].checked = checkbox.checked; 
	  i++;
	}
}

function showlayer(name){
  if (document.getElementById) {
    // this is the way the new msie works
    document.getElementById(name).style.visibility = "visible";
  }
  else if (document.all) {
    // this is the way the old msie versions work
    document.all[name].style.visibility = "visible";
  }
  else if (document.layers) {
    // this is the way nn4 works
    document.layers[name].visibility = "visible";
  }
}

function hidelayer(name){
  if (document.getElementById) {
    // this is the way the new msie works
    document.getElementById(name).style.visibility = "hidden";
  }
  else if (document.all) {
    // this is the way the old msie versions work
    document.all[name].style.visibility = "hidden";
  }
  else if (document.layers) {
    // this is the way nn4 works
    document.layers[name].visibility = "hidden";
  }
}

function layer(item, name){
	if (document.form_cms.elements[item].checked){
		showlayer(name);
	}
	else {
		hidelayer(name);
	}
}

function toggleDisplay(name, disp) {
  if (disp) {
    if (document.getElementById) {
      document.getElementById(name).style.display = '';
    }
    else if (document.all){
      document.all[name].style.display = '';
    }
  }
  else {
    if (document.getElementById) {
      document.getElementById(name).style.display = 'none';
    }
    else if (document.all) {
      document.all[name].style.display = 'none';
    }
  }
}

// Checks in a value (needle) is in an array (haystack)
function in_array(needle, haystack) {
  var found = false;
  for (var i = 0; i < haystack.length && !found; i++)
    found = (needle == haystack[i]);
  return found;
}

function statusField(fieldName, statusCheckbox){
  if(statusCheckbox.checked == true){
    styleName='enabledField'; 
  }
  else{
    styleName='disabledField';
  }

  document.form_cms.elements[fieldName].disabled=!(statusCheckbox.checked);
  document.form_cms.elements[fieldName].className=styleName;
}

function adminLogin() {
  parent.boven.location.href="boven.php";
  parent.menu.location.href="menu.php";
}

function login() {
  top.boven.location.href="boven.php";
}

//function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear)
{
  var DaysInMonth = 31;
  if (WhichMonth == 4 || WhichMonth == 6 || WhichMonth == 9 || WhichMonth == 11) DaysInMonth = 30;
  if (WhichMonth == 2 && (WhichYear/4) != Math.floor(WhichYear/4))	DaysInMonth = 28;
  if (WhichMonth == 2 && (WhichYear/4) == Math.floor(WhichYear/4))	DaysInMonth = 29;
  return DaysInMonth;
}

//function to change the available days in a months
function ChangeOptionDays(Which) {
  DaysObject = eval("document.form_cms." + Which + "_day");
  MonthObject = eval("document.form_cms." + Which + "_month");
  YearObject = eval("document.form_cms." + Which + "_year");

  Month = MonthObject[MonthObject.selectedIndex].text;
  Year = YearObject[YearObject.selectedIndex].text;

  DaysForThisSelection = DaysInMonth(Month, Year);

  CurrentDaysInSelection = DaysObject.length;
  if (CurrentDaysInSelection > DaysForThisSelection)
  {
    for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
    {
      DaysObject.options[DaysObject.options.length - 1] = null
    }
  }
  if (DaysForThisSelection > CurrentDaysInSelection)
  {
    for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++)
    {
      NewOption = new Option(DaysObject.options.length + 1);
      DaysObject.add(NewOption);
    }
  }
    if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}

function previewPage(text){
  Hoogte=500;
  Breedte=500;
  LeftPosition = (screen.width) ? (screen.width-Breedte)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-Hoogte)/2 : 0;
  settings ='height='+Hoogte+',width='+Breedte+',top='+TopPosition+',left='+LeftPosition+',toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes'
  preview=window.open('',"details", settings);
  preview.document.writeln('<html>');
  preview.document.writeln('<head>');
  preview.document.writeln('  <title>I-Processor</title>');
  preview.document.writeln('  <link rel="stylesheet" type="text/css" href="../../css/frontend_main.css">');
  preview.document.writeln('</head>');
  preview.document.writeln('<body>');
  preview.document.writeln(text);
  preview.document.writeln('</body>');
  preview.document.writeln('</html>');
  preview.document.close();
  preview.focus();
}

function changeDimensions(what) {
  var oldwidth = document.forms['form_cms'].oldwidth.value;
  var oldheight = document.forms['form_cms'].elements['oldheight'].value;

  var percent = parseInt(document.forms['form_cms'].elements['ratio'].value);
  var newwidth = parseInt(document.forms['form_cms'].elements['newwidth'].value);
  var newheight = parseInt(document.forms['form_cms'].elements['newheight'].value);

  switch (what) {
    case 'ratio':
      // Calculate new width & height
      if (!isNaN(document.forms['form_cms'].elements['ratio'].value) && percent > 0) {
        var ratio = percent / 100;
		// controle toegevoegd zodat een image niet breeder of hoger kan zijn dan 1500 pixels bij een resize dmv een ratio
        nieuwebreedte = Math.round(oldwidth * ratio);
		nieuwehoogte  = Math.round(oldheight * ratio);
		if ((nieuwebreedte > 1500) || (nieuwehoogte > 1500))
		{
			alert('De breedte of de hoogte mag niet groter dan 1500 pixels zijn.');
			document.forms['form_cms'].elements['ratio'].value  = 100;
			document.forms['form_cms'].elements['newwidth'].value  = oldwidth;
			document.forms['form_cms'].elements['newheight'].value = oldhieght;
			break;
		}
		document.forms['form_cms'].elements['newwidth'].value  = nieuwebreedte;
		document.forms['form_cms'].elements['newheight'].value = nieuwehoogte;
	    
      }
      break;
    case 'width':
      // Calculate new height & ratio
      if (!isNaN(document.forms['form_cms'].elements['newwidth'].value) && newwidth > 0) {
        document.forms['form_cms'].elements['ratio'].value = Math.round((newwidth / oldwidth) * 100);
        document.forms['form_cms'].elements['newheight'].value = Math.round((newwidth / oldwidth) * oldheight);
      }
      break;
    case 'height':
      // Calculate new width & ratio
      if (!isNaN(document.forms['form_cms'].elements['newheight'].value) && newheight > 0) {
        document.forms['form_cms'].elements['ratio'].value = Math.round((newheight / oldheight) * 100);
        document.forms['form_cms'].elements['newwidth'].value = Math.round((newheight / oldheight) * oldwidth);
      }
      break;
  }
  return true;
}

function showImageChooser(form, elem) {
  var width = 500;
  var height = 400;

  var left = (screen.width) ? (screen.width-width)/2 : 0;
  var top = (screen.height) ? (screen.height-height)/2 : 0;

  var uplID = document.forms[form].elements[elem].value;

  window.open('imagechooser.php?form='+form+'&element='+elem+'&uplID='+uplID, 'imgchoose', 'width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top+',scrollbars=no,resizable=no,status=no,toolbar=no,menubar=no');
}

function setChosenImage(form, elem, uplID, uplName) {
  var dom = document.getElementById && !document.all ? 1 : 0;

  document.forms[form].elements[elem].value = uplID;
  
  var ndiv = dom ? document.getElementById(elem + '_namediv') : document.all[elem + '_namediv'];

  ndiv.innerHTML = uplName;
}

// For mouseover effect on tables
// which: row object
// action: 'over'/'out'
// orig: original classname
function changeRow(which, action, orig) {
  switch (action) {
    case 'over':
      which.className = 'RowHighlight';
      break;
    case 'out':
      which.className = orig;
      break;
  }
}

function selectListboxItem ( theListbox, theItem ) {
	theOptions = theListbox.options;
	for(i=0; i<theOptions.length; i++) {
		theOptions[i].selected = theOptions[i].value == theItem;
	}
}

function windowOpen(name,url,Hoogte,Breedte) {
  LeftPosition = (screen.width) ? (screen.width-Breedte)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-Hoogte)/2 : 0;
  settings ='height='+Hoogte+',width='+Breedte+',top='+TopPosition+',left='+LeftPosition+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes'
  window_open = window.open(url,name, settings);
  window_open.focus();
}

function popWindow(img) 
{	
	msgWindow = window.open("", "displayWindow", "menubar=no,scrollbars=no,status=no,width=640,height=446")
	msgWindow.document.write ('<HEAD><\/HEAD>');
	msgWindow.document.write('<body topmargin=0 leftmargin=0 class=NormalBold onblur="window.close()" onclick="window.close()"">');
	msgWindow.document.write('<table width=100% border=0><tr><td><img src='+img+' border=0>');
	msgWindow.document.write('</td></tr></table><\/body><\/html>');
	msgWindow.document.close();
}
