// functions for switching visiblity admin options

var visibilityTime = 500;
var timeoutUnit = 1;
var currentTime = 0;

function toggleAdminOptions(id, way)
{   
   hideAllOptionsBoxesAccept(id);
   var box = document.getElementById(id);
   var item = document.getElementById("link_"+id);
   
   if(way==1)
   {
     box.style.visibility = 'visible';
     item.style.color = '#ee6d43';   
   }
   else
   {
     //box.style.visibility = 'hidden';
   }
}

function hideAllOptionsBoxesAccept(id)
{
  startTimeout();
  hideAllOptionsBoxes();
  document.getElementById(id).style.visibility = 'visible';
  var item = document.getElementById("link_"+id);
  item.style.color = '#ee6d43'; 
}

function hideAllOptionsBoxes()
{
  var boxes = document.getElementsByClassName('adminOptions');
  for(i=0;i<boxes.length;i++)
      boxes[i].style.visibility = 'hidden';
      
  var items = document.getElementsByClassName('sublist_link');
  for(i=0;i<items.length;i++){
      items[i].style.fontWeight = 'normal';
      items[i].style.color = '#1c3b96'; 
  }

}

function freaseTimeout()
{
   timeoutUnit = 0;
}

function startTimeout()
{
   currentTime = 0;
   timeoutUnit = 1;
   loopTimeout();
}

function increaseTimeout()
{
   currentTime += timeoutUnit;
   loopTimeout();
}

function loopTimeout()
{  
   if(currentTime>=visibilityTime)
     hideAllOptionsBoxes();
   else
     setTimeout("increaseTimeout();", timeoutUnit);
    
   //document.getElementById('helper').innerHTML = currentTime;
}

function validateAndSubmit(fields)
{
 
  
  var noErrors = true;
  cleanUpErrorMessages();
  for(i=0;i<fields.length;i++)
  {

    var value = document.getElementById(fields[i]).value;
    if((value=='')||(!value))
    {
       noErrors = false;
       document.getElementById(fields[i]).style.border = "1px solid #ff5d5d";
       document.getElementById(fields[i]).style.backgroundColor = "#ffdbdb";
       document.getElementById(fields[i]+"Error").innerHTML = "<span style='color: red;'>Geen of onjuiste invoer!</span>";
    }
  }
 
  if(document.getElementById('uploadFile'))
  {
     var filename = document.getElementById('uploadFile').value;

     if(filename.length>0)
     {

        if((filename.indexOf('.pdf')==-1)&&(filename.indexOf('.doc')==-1)&&(filename.indexOf('.DOC')==-1)&&(filename.indexOf('.PDF')==-1))
        {
          noErrors = false;
          document.getElementById('filenameError').innerHTML = "<span style='color: red;'>Ongeldige extentie. Alleen .doc en .pdf is toegestaan.</span>";
        }
     }
  }
  
  if(noErrors)
    document.getElementById('checkedForm').submit();
  else
    document.getElementById('submitError').innerHTML = "<span style='color: red;'>Formulier is niet compleet</span>";
  
}

function cleanUpErrorMessages()
{
var boxes = document.getElementsByClassName('error_message');
   
		// loop through all checkboxes
		for (var i=0; i<boxes.length; i++){
		boxes[i].innerHTML = '';
		var name = boxes[i].id;
        var fieldName = name.substr(0,name.indexOf('Error')); 
        
		document.getElementById(fieldName).style.border='1px solid #CCC';
		document.getElementById(fieldName).style.backgroundColor='white';  
		}
}

function cancel()
{
  document.getElementById('cancelForm').submit();
}

document.getElementsByClassName = function(className, parentElement) {
  var children = ($(parentElement) || document.body).getElementsByTagName('*');
  return $A(children).inject([], function(elements, child) {
    if (child.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
      elements.push(child);
    return elements;
  });
}

function validate(theForm)
{	
  theForm = document.getElementById(theForm);
  var error = 0;
  if(theForm.tfLast.value == ""){
	theForm.tfLast.style.border = '2px solid #e88862;';
	theForm.tfLast.focus();
	error = 1;
   }
   
   if(theForm.tfPhone.value == ""){
	theForm.tfPhone.style.border = '2px solid #e88862;';
	if(error==0)
	theForm.tfPhone.focus();
	error = 1;
   }  

  if(theForm.tfContactSender.value == ""){
	theForm.tfContactSender.style.border = '2px solid #e88862;';
	if(error==0)
	theForm.tfContactSender.focus();
	error = 1;
   }
   


  if(error==0)
    theForm.submit();
}