//*****
//***** Dit bestand bevat een aantal handige javascriptfuncties die gebruikt kunnen
//***** worden door de hele appliactie heen.
//*****
//***** Bram Crins
//*****

function checkNumber( e ) 
{
	
		if (isNaN(parseInt(e)) == true)
		{
			return false;
		}
		else
		{
			return true;
		}
	
}

function checkDate( e ) 
{
	// splitsen van de datum
		 
	var arrX = e.split("-");		 		 
		 
	if (arrX.length != 3) return false;
		 
	var day = arrX[0];
	var month = arrX[1];
	var year = arrX[2];
	//controleren of het een juiste dag is
	if (isNaN(day) == true)
	{
		return false;		 
	}
	else
	{
		if (day.length == 1)
		{
			day = '0' + day
		}
	}	 
	// controleren of het en juiste datum is
	if (isNaN(month) == true)
	{
		return false;		 
	}
	else
	{
		if (month.length == 1)
		{
			month = '0' + month
		}
	}
	//eventuele tijd eraf halen
	if (year.length > 4)
	{
		year = year.substr(0,4);
		//alert(year);			
	}
	//controleren of jaar numeriek en 4 cijfers lang is
	if ((isNaN(year) == true) || (year.length != 4))
	{
		return false;
	}
		 
	//combinaties combineren
	var DayArray  = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); 
	var MonthArray  = new Array( "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" ); 
	var inpDate  = day + month + year; 
	var filter  = /^[0-9]{2}[0-9]{2}[0-9]{4}$/; 

	//Check ddmmyyyy date supplied
	if( !filter.test( inpDate ) ) return false;

	/* Check Valid Month */
	filter = /01|02|03|04|05|06|07|08|09|10|11|12/;
	if( !filter.test( month ) ) return false;

	/* Check For Leap Year */
	var N = Number( year );
	if( ( N%4 == 0 && N%100 != 0 ) || ( N%400 == 0 ) ) DayArray[1] = 29;

	/* Check for valid days for month */
	for( var ctr = 0; ctr <= 20; ctr++ )
	{
		if( MonthArray[ctr] == month )
		{
			if( day <= DayArray[ctr] && day > 0 )
			{
				return true
			}
			else
			{
				return false;
			}
		}
	}
}

function funCheckComboDate(day, month, year)
{
	var strTemp = day + '-' + month + '-' + year
	return checkDate(strTemp)

}

function isPostcode(strPC)
{
	var itfError = false
	var strTemp
	var strPCtemp
		
				
	if ((strPC.length != 7) && (itfError == false))
	{
		//als lengte 6 is, dan spatie tussen 4e en 5e teken.
		if ((strPC.length == 6) && (itfError == false))
		{
		strPC = strPC.substring(0,4) + ' ' + strPC.substring(4,6);
		}
		else
		{
		itfError = true
		}
	}		
	if ((isNaN(strPC.substr(0,4)) == false) && (itfError == false))
	{
		//het is een nummer controleren of het wel tussen de jusite waarden ligt			
		if ((strPC.substr(0,4) < 1000)&& (strPC.substr(0,4) >9999)&&(intError == false))	
		{
			itfError = true
		}
	}
	else
	{				
		itfError = true
	}
	if (itfError == false)
	{
		if ((strPC.substr(5,1).toLowerCase() >= 'a') && (strPC.substr(5,1).toLowerCase() <= 'z'))
		{				
			itfError = false
		}
		else
		{
			itfError = true
		}
	}	
		
	if (itfError == false)
	{
		if ((strPC.substr(6,1).toLowerCase() >= 'a') && (strPC.substr(6,1).toLowerCase() <= 'z'))
		{				
			itfError = false
		}
		else
		{				
			itfError = true
		}
	}		
		
	if (itfError == false)
	{
		itfError = true
	}
	else
	{
		itfError = false
	}
	//alert(itfError);
	return itfError	
		
}


function Left(str, n)
{
       if (n <= 0)
           return "";
       else if (n > String(str).length)
           return str;
       else
           return String(str).substring(0,n);
}

function Right(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}


function CheckKey(obj, AllowedValues)
{
	//Deze functie controleert of de inegeven keyvalue voorkomt in
	//het lijstje met toegetane karakters.
	var pressedkey = String.fromCharCode(event.keyCode);
	//alert(pressedkey);
	if (AllowedValues.indexOf(pressedkey) != -1)
	{
		event.returnValue = true;					
	}
	else
	{
		event.returnValue = false;					
		alert('Opgegeven karakter is hier niet toegestaan.');
	}
}	


function IsEmail(strEmail)
{	
	if ((strEmail.indexOf('@') < 0) || ((strEmail.charAt(strEmail.length-4) != '.') && (strEmail.charAt(strEmail.length-3) != '.'))) 
	{
		return false;
	}
	else
	{
		return true;
	} 

}

function IsNumber( e ) 
{
	
		if (isNaN(parseInt(e)) == true)
		{
			return false;
		}
		else
		{
			return true;
		}
	
}

function IsDate( e ) 
{
	// splitsen van de datum
		 
	var arrX = e.split("-");		 		 
		 
	if (arrX.length != 3) return false;
		 
	var day = arrX[0];
	var month = arrX[1];
	var year = arrX[2];
	//controleren of het een juiste dag is
	if (isNaN(day) == true)
	{
		return false;		 
	}
	else
	{
		if (day.length == 1)
		{
			day = '0' + day
		}
	}	 
	// controleren of het en juiste datum is
	if (isNaN(month) == true)
	{
		return false;		 
	}
	else
	{
		if (month.length == 1)
		{
			month = '0' + month
		}
	}
	//eventuele tijd eraf halen
	if (year.length > 4)
	{
		year = year.substr(0,4);
		//alert(year);			
	}
	//controleren of jaar numeriek en 4 cijfers lang is
	if ((isNaN(year) == true) || (year.length != 4))
	{
		return false;
	}
		 
	//combinaties combineren
	var DayArray  = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); 
	var MonthArray  = new Array( "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" ); 
	var inpDate  = day + month + year; 
	var filter  = /^[0-9]{2}[0-9]{2}[0-9]{4}$/; 

	//Check ddmmyyyy date supplied
	if( !filter.test( inpDate ) ) return false;

	/* Check Valid Month */
	filter = /01|02|03|04|05|06|07|08|09|10|11|12/;
	if( !filter.test( month ) ) return false;

	/* Check For Leap Year */
	var N = Number( year );
	if( ( N%4 == 0 && N%100 != 0 ) || ( N%400 == 0 ) ) DayArray[1] = 29;

	/* Check for valid days for month */
	for( var ctr = 0; ctr <= 20; ctr++ )
	{
		if( MonthArray[ctr] == month )
		{
			if( day <= DayArray[ctr] && day > 0 )
			{
				return true
			}
			else
			{
				return false;
			}
		}
	}
}

// Left-trim 'strToTrim'
function LTrim (strToTrim) {
  var charIndex;
  var strResult;

  strResult = ''+strToTrim;

  if (strResult.length == 0) {
	return strResult;
  }

  for (charIndex = 0; charIndex < strResult.length; charIndex++) {

	if (strResult.charAt(charIndex) != ' ') {
	  // Break if no whitespace found
	  break;
	}
  }

  if (charIndex < strResult.length) {
	var indexToStart;
	indexToStart = charIndex;
	strResult = strResult.substring(indexToStart,strResult.length);
  } else if ((charIndex == strResult.length) && (strResult.charAt(charIndex) == ' ')) {
	return "";
  }

  return strResult;
}

// Right-trim 'strToTrim'
function RTrim (strToTrim) {
  var charIndex;
  var strResult;

  strResult = ''+strToTrim;

  if (strResult.length == 0) {
	return strResult;
  }

  for (charIndex = strResult.length - 1; charIndex >= 0; charIndex--) {

	if (strResult.charAt(charIndex) != ' ') {
	  // Break if no whitespace found
	  break;
	}
  }

  if (charIndex >= 0) {
	var indexToEnd;
	indexToEnd = charIndex + 1;
	strResult = strResult.substring(0, indexToEnd);
  } else if ((charIndex < 0) || ((charIndex == 0) && (strResult.charAt(charIndex) == ' '))) {
	return "";
  }

  return strResult;
}

// Trim 'strToTrim'
function Trim (strToTrim) {
  return LTrim (RTrim (strToTrim));
}

function Mid(str, start, len)
/***
		IN: str - the string we are LEFTing
			start - our string's starting position (0 based!!)
			len - how many characters from start we want to get

		RETVAL: The substring from start to start+len
***/
{
		// Make sure start and len are within proper bounds
		if (start < 0 || len < 0) return "";

		var iEnd, iLen = String(str).length;
		if (start + len > iLen)
				iEnd = iLen;
		else
				iEnd = start + len;

		return String(str).substring(start,iEnd);
}

function InStr(strSearch, charSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
Requires use of:
	Mid function
	Len function
*/
{
	for (i=0; i < Len(strSearch); i++)
	{
	    if (charSearchFor == Mid(strSearch, i, 1))
	    {
			return i;
	    }
	}
	return -1;
}

function Len(str)
/***
		IN: str - the string whose length we are interested in

		RETVAL: The number of characters in the string
***/
{  return String(str).length;  }





