

function CheckMaxLength (field, maxlength, displayname)
{
	if (field.value.length > maxlength) {
		alert('Het veld ' + displayname + ' heeft een maximale lengte van ' + maxlength + ' karakters');
		
		field.focus();
		return false;
	} else {
		return true;
	}
}


/*
 ####################################################
 autocomplete 
 ####################################################
*/

function matchFieldSelect (field, select, value) 
{
  var property = value ? 'value' : 'text';
  var found = false;
  for (var i = 0; i < select.options.length; i++)
    if ((found = select.options[i][property].indexOf(field.value) == 0))
      break;
  if (found)
    select.selectedIndex = i;
  else
    select.selectedIndex = -1;
  if (field.createTextRange) {
    var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;"
    if (cursorKeys.indexOf(event.keyCode+";") == -1) {
      var r1 = field.createTextRange()
      var oldValue = r1.text;
      var newValue = found ? select.options[i][property] : oldValue;
      if (newValue != field.value) {
        field.value = newValue
        var rNew = field.createTextRange()
        rNew.moveStart('character', oldValue.length) 
        rNew.select()
      }
    }
  }
}

function ShowList(divlist, left, top) 
{
	if (divlist) {
		divlist.style.position = 'absolute';
		divlist.style.visibility = 'visible';
		if (left) divlist.style.left = left;
		if (top) divlist.style.top = top;
	}
}

function HideList(divlist, left, top)
{
	if (divlist) {
		divlist.style.visibility = 'hidden';
		
		if (left) divlist.style.left = left;
		if (top) divlist.style.top = top;
	}
}

function ToggleDiv(clickbutton, elementDIV)
{
	//try to toggle div
	
	bu = document.getElementById('laatsterecordBUTT');
	el = document.getElementById('laatsterecordDIV');
	//alert(bu.basename);
	//alert(el);
	try 
	{
		if (el.style.visibility == 'hidden') {			
			el.style.position   = 'relative';
			timer = setTimeout("el.style.visibility = 'visible'", 1);
			bu.innerText = 'Verberg ' + bu.basename;
		}else {
			el.style.visibility = 'hidden';
			el.style.position   = 'absolute';	
			bu.innerText = 'Toon ' + bu.basename;	
		}
	
	
	} catch(errorObject) {
		// do nothing
		//return true;
		
			
	}

}


function IsEntered(objName) 
{
	var fieldName = objName;
	
	if ((fieldName.value) && (fieldName.value !='') && (fieldName.value.length > 0) ) {
		return true
	} else {
		return false;		
	}
}


function HasSelected(objName) {
	var fieldName = objName;
	if (fieldName.value == '') {
		alert ('Niet alle verplichte velden zijn ingevuld.');
		return false;
	} else {
		return true;
	}
}

function IsEmail (objName) {
	var email = objName.value;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		alert('Het ingevulde e-mailadres is ongeldig');
		return false;
	}
	return true;
}

function VerifyPassword(objPassword1, objPassword2) {
	if (objPassword1.value != objPassword2.value) {
		alert ('De wachtwoorden zijn niet identiek.');
		return false;
	} else {
		return true;
	}
}


/*
 ####################################################
 Time and time interval checks
 ####################################################
*/

function UpdateDuration(theForm, loopID) 
{
	if (!loopID) loopID = '';
	eval('var starttime = theForm.starttime'+loopID+'.value;');
	eval('var stoptime  = theForm.stoptime'+loopID+'.value;');
	
	var startminutes = TimeToMinutes(starttime);	
	if(!startminutes)
	{
		//theForm.starttime.focus()
		return false;
	}
	var stopminutes = TimeToMinutes(stoptime);
	if(!stopminutes)
	{
		//theForm.stoptime.focus()
		return false;
	}	
	eval( 'var durationObj = theForm.duration'+loopID+';');
	durationObj.value = '';
	if ( (stopminutes*1) < (startminutes*1) ) 
			stopminutes += 60*24;
	
	var duration = (stopminutes - startminutes);
	
	durationObj.value = MinutesToTime(duration);
	
	return true;

}
function GetCurrentTime()
{
	now = new Date;
	theHour = now.getHours();
	theMinute = now.getMinutes();
		
	if (theMinute<10) theMinute = '0' + theMinute;
	
	return theHour + ':' + theMinute;
}

function HasTime(theField)
{	
	if ((theField.value=='')||IsTime(theField.value)) {		
		return true;
	}else {
		alert("vul de tijd in als HH:MM. Bijvoorbeeld 14:35 of 9:10");
		theField.focus();
		theField.select();
	}
}

function IsTime(theTime)
{		
	if (!theTime) return false	
	var aTime = theTime.split(':');			
	return !(aTime.length!=2 || isNaN(aTime[0]*1) || isNaN(aTime[1]*1) || aTime[0]>23 || aTime[1]>59 );
	
}
function TimeToMinutes(theTime)
{
	// check input
	if (!IsTime(theTime)) return false;	
	// convert HH:MM to Minutes
	var aTime = theTime.split(':');	
	return (aTime[0] * 60) + (aTime[1] * 1);
	
}

function MinutesToTime(theMinutes)
{
	// check input
	if (isNaN(theMinutes)) return false;
		
	var theHour = Math.floor((theMinutes*1)/60);
	var theMinute = (theMinutes*1)%60;
	
	if (theMinute<10) theMinute = '0' + theMinute;
		
	theTime = theHour + ':' + theMinute;
		
	return theTime;
}

/*
 need special javascript code to reach object handle 
 because of incompatibility between javascript and php
 when we want a multiple selection.
*/
function GetObjectArrayFromName(theForm, theObjectName ) {
	var theObjectArray = Array();
	a = 0;
	// go through each form element looking for the name
	for (var i=0,max=theForm.elements.length;i<max;i++) {
		if ( theForm.elements[i].name == theObjectName ) {
			// found it, add object to array
			theObjectArray[a] = theForm.elements[i];
			a++;
		}
   	}
	if (theObjectArray.length > 0) 
		return theObjectArray
	else
   	   return null;
}

function GetObjectFromName(theForm, theObjectName ) {
	
	// go through each form element looking for the name
	for (var i=0;i<theForm.elements.length;i++) {
		if ( theForm.elements[i].name == theObjectName ) {
			// found it, add object to array
			return theForm.elements[i];			
		}
   	}
	// object not found
   	return null;
}
/*
 ##################################################
 adi form function
 ##################################################
*/
function checkadiform(theForm) {
		
	// check for project
	var projectsid = GetObjectFromName(theForm, 'ulprojects_id');		
	if (projectsid.value=='') {
		alert('Project is niet ingevuld.');
		projectsid.focus();
		return false; 
	}
	
	// check for activity
	var workcat = GetObjectFromName(theForm, 'workcat');
	if (workcat.value=='') {
		alert('Activiteit is niet ingevuld.');
		workcat.focus();
		return false;		
	}
	
	return true;
}

/*
 ##################################################
 multirow form functions
 ##################################################
*/

function checkrows(theForm) {
	// check only validity of rows that have a numbered field starting with
	// duration, for example duration10
	var i = 1;
	eval('var duration = GetObjectFromName(theForm, \'duration' + i + '\');');
	
	while (duration) {
		// projectid and workcat must be filled if duration is entered
		if (duration && duration.value!='') {
			// check for project
			var projectsid = GetObjectFromName(theForm, 'ulprojects_id'+i);		
			if (projectsid.value=='') {
				alert('Project is niet ingevuld.');
				projectsid.focus();
				return false; 
			}
	
			// check for activity
			var workcat = GetObjectFromName(theForm, 'workcat'+i);
			if (workcat.value=='') {
				alert('Activiteit is niet ingevuld.');
				workcat.focus();
				return false;		
			}
			
			/*
			var projectsid = GetObjectFromName(theForm, 'ulprojects_id'+i);
			var workcat = GetObjectFromName(theForm, 'workcat'+i);
			
			if ((projectsid.value=='')||(workcat.value=='')) {
				alert('Een rij (' + i + ') is nog niet helemaal ingevuld.');
				// set focus to this row
				duration.focus();
				return false;
			}
			*/
		} 
		i++;
		eval('var duration = GetObjectFromName(theForm, \'duration' + i + '\');');	
	}	
	return true;	
}


/*
	Propagate time to next row
*/
function PropagateTimeToNext(theForm, i) {

	eval('var stoptimeObj = GetObjectFromName(theForm, \'stoptime' + i + '\');');
	eval('var starttimeObj = GetObjectFromName(theForm, \'starttime' + (i+1) + '\');');
	
	if (starttimeObj && (starttimeObj.value=='') && stoptimeObj &&(stoptimeObj.value!='')) {
		starttimeObj.value = stoptimeObj.value;		
	}
	return true;
}

function GetTimeFromPrev(theForm, i) {

	eval('var stoptimeObj = GetObjectFromName(theForm, \'stoptime' + (i-1) + '\');');
	eval('var starttimeObj = GetObjectFromName(theForm, \'starttime' + i + '\');');
	
	if (stoptimeObj && (stoptimeObj.value!='') && starttimeObj && (starttimeObj.value=='') ) {
		starttimeObj.value = stoptimeObj.value;
	}
	return true;
}

function ToggleForcelink(chkboxislink) {
    
    //var divurl = document.getElementById('divurl');
    //var divforceurl = document.getElementById('divforcelink') ;
    var ff = document.getElementById('frmforcelink');
    var fs = document.getElementById('frmsjabloon')
    
    if (ff && fs) {
        
        if (chkboxislink.checked) {
            ff.disabled = false;
            fs.disabled = true;
            fs.style.border='1px solid #eee';
            //fs.style.backgroundColor = '#eee';

            //window.setTimeout(" document.getElementById('divforcelink').style.position='relative'; ", 200 );
            //-window.setTimeout(" document.getElementById('divforcelink').style.visibility='visible'; ", 1 );

            //window.setTimeout("document.getElementById('divurl').style.position='absolute'; ", 100 );
            //window.setTimeout("document.getElementById('divurl').style.visibility='hidden'; ", 2 );
            
        } else {
            ff.disabled = true;
            //ff.style.backgroundColor = '#eee';
            fs.disabled = false;
            fs.style.border='';
           
            //fs.style.backgroundColor = '';

            //window.setTimeout("document.getElementById('divurl').style.position='relative'; ",  100 );
            //window.setTimeout("document.getElementById('divurl').style.visibility='visible'; ", 101 );
            
            //window.setTimeout(" document.getElementById('divforcelink').style.position='absolute'; ", 1 );
            //-window.setTimeout(" document.getElementById('divforcelink').style.visibility='hidden'; ", 2 );

        }//endif
        
    }//endif
}

