// Validation functions

// Is True check for checkboxes
function IsFieldTrue( field, fieldLabel ) 
{
   // No check if this field is disabled
   if ( field.disabled )
      return false;

	var value = 0;
	var bool = false;

	if ( field.type == "checkbox" )
	   value = field.checked;
	else
	   value= field.value

	// Set value to lowercase
	if ( typeof( value ) == 'string' )
	{
		value = value.toLowerCase();
	}

	// Set value
	bool = ( value == true 
			|| value == '1' 
			|| value == 'true' 
			|| value == 'yes'
			|| value == 'on' );

	if ( !bool )
	{
		alert( fieldLabel + ' moet aangevinkt worden.' );
		if ( field.focus )
		   field.focus();
		return true;
	}
	else
		return false;
}

// Is Empty Check
function IsFieldEmpty( field, fieldLabel ) 
{
   // No check if this field is disabled
   if ( field.disabled )
      return false;
      
	var value = field.value;
	if ( value == null || Trim(value) == '' )
	{
		alert( fieldLabel + ' mag niet leeg zijn.' );
		if ( field.focus )
		   field.focus();
		return true;
	}
	else
		return false;
}

// Empty check for radio buttons
function IsEmptyRadio( radioName, fieldLabel )
{
   var radiobuttons = document.getElementsByName( radioName );
   var length = radiobuttons.length;
   if ( length > 0 )
   {
      for( var i = 0; i < length; i++ )
      {
         // No check if this field is disabled
         if ( radiobuttons[i].disabled )
            return false;

         else if ( radiobuttons[i].checked )
            return false;
      }
      
      radiobuttons[0].focus();
		alert( fieldLabel + ' mag niet leeg zijn.' );
      return true;
   }
   else
      return false;
}

// GreaterThanMaxChars
// -----------------------------------------------------------------------
//    returns true if the maximum number of characters is exceeded

function GreaterThanMaxChars( field, maxLength, fieldLabel ) 
{
   // No check if this field is disabled
   if ( field.disabled )
      return false;

   // check
   if ( field.value.length > maxLength ) 
   {
      alert( 'Veld ' + fieldLabel + ' bevat ' + field.value.length + ' tekens, het maximum is ' + maxLength );
      field.focus();
      return true;
   } 
   else
      return false;
}


// SmallerThanMinChars
// -----------------------------------------------------------------------
//    returns true if the minimum number of characters is not met

function SmallerThanMinChars( field, minLength, fieldLabel ) 
{
   // No check if this field is disabled
   if ( field.disabled )
      return false;

   // check
   if ( field.value.length < minLength ) 
   {
      alert( 'Veld ' + fieldLabel + ' bevat ' + field.value.length + ' tekens, het minimum is ' + minLength );
      field.focus();
      return true;
   } 
   else
      return false;
}


/*** Standard Field validators  ******************************************************************/
// Validators 
// parameters: 
//		field: field element 
//		fieldLabel: field label
// returns true if input is valid
// returns false string if input is invalid and alert
//

// Confirms the other field in its own name: confirm[ name of field to confirm ]
function ConfirmsField( field, fieldLabel )
{
   // No check if this field is disabled
   if ( field.disabled )
      return false;

	// Get field name to confirm from its own name
	var fieldToConfirmName = field.name.substring( 20, field.name.length - 2 );
	var fieldToConfirm = document.Form.elements[ 'PostedField[' + fieldToConfirmName + ']' ];
   if ( field.value == fieldToConfirm.value ) 
      return true;
   else 
   {
      field.focus();
      alert( "'" + fieldLabel.replace( '&nbsp;', ' ' ) + "' is incorrect.");
      return false;
   }
}

// a utility function which checks if a field contains a valid email adress
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
function IsValidEmail( field, fieldLabel )
{    
   // No check if this field is disabled
   if ( field.disabled )
      return false;

	// there must be >= 1 character before @, so we
	// start looking at character position 1 
	// (i.e. second character)
	var value = Trim( field.value );
	
	if ( IsEmail( value ) )
		return true;
	else
	{	
		alert( fieldLabel + ' bevat geen geldig email adres.' );
		field.focus();
		return false;
	}	
}

// a utility function which checks if a field contains a valid email adress
function IsEmail( value )
{
   // Null check
   if ( value == null )
      return false;
      
   var i = 1;
	var validEmail = true;
	var sLength = value.length;
	
	// look for @
	while ( (i < sLength) && (value.charAt(i) != "@") )
		i++;
	
	
	if ( (i >= sLength) || (value.charAt(i) != "@") ) 
		validEmail = false;
	else 
		i += 2;
	
	// look for .
	while ((i < sLength) && (value.charAt(i) != ".") )
		i++;
	
	
	// there must be at least two character after the .
	if ( (i >= sLength - 2) || (value.charAt(i) != ".") ) 
		validEmail = false;
		
	return validEmail;
}

// Check for valid decimal number
function IsValidNumber( field, fieldLabel )
{
   // No check if this field is disabled
   if ( field.disabled )
      return false;

	var value = Trim( field.value );

   var oneDecimal = false;
   var oneChar;
   var validNumber = true;
   var length = value.length;
   for ( var i = 0; i < length; i++ )
   {
      oneChar = value.charAt(i);
      if ( i == 0 && oneChar == "-" )
         continue;
      
      if ( oneChar == "," || oneChar == "." )
      {
			if ( oneDecimal == false )
			{
				oneDecimal = true;
			   continue;
			}
			else
			{
				validNumber = false;
				break;
			}
      }
      
      if ( oneChar < "0" || oneChar > "9" )
      {
         validNumber = false;
         break;
      }
   }
   
   if ( validNumber )
		return true;
	else
	{
		alert( fieldLabel + ' bevat geen geldig getal.' );
		return false;
	}
}	

// Check for valid phone number
function IsValidPhonenumber( field, fieldLabel )
{
   // No check if this field is disabled
   if ( field.disabled )
      return false;

	var value = StripCharsInBag( field.value, " +()-" );
	
	if ( value.length > 14 || value.length < 10 || !IsInteger( value ) ) 
	{
		alert( fieldLabel + ' bevat geen geldig telefoonnummer.' );
		field.focus();
		return false;
	}
	else 
		return true;	
}

// Check valid NL postcode
function IsValidPostalcode( field, fieldLabel )
{
   // No check if this field is disabled
   if ( field.disabled )
      return false;

	var validPostcode = true;
	var value = StripWhitespace( field.value );
	
	if ( value.length != 6 )
		validPostcode = false;
	else if (! IsInteger( value.substr(0,4) ) )
		validPostcode = false;
	else if ( !IsLetter( value.charAt(4) ) ) 
		validPostcode = false;
	else if ( !IsLetter( value.charAt(5) ) ) 
		validPostcode = false;
		
	if ( validPostcode )
		return true;
	else
	{
		alert( fieldLabel + ' bevat geen geldige postcode.' );
		field.focus();
		return false;
	}
}

function IsValidUrl ( field, fieldLabel )
{   
   // No check if this field is disabled
   if ( field.disabled )
      return false;

	var url = Trim( field.value );
	
	var startpos = url.indexOf( '://' );
	
	var slashPos = url.indexOf( '/', startpos + 1 );
	var quesPos = url.indexOf( '?' );
	var hashPos = url.indexOf( '#' );
	var endPos = -1;
	if ( slashPos > -1 )
		endPos = slashPos - 1;
	else if ( quesPos > -1 )
		endPos = quesPos - 1;
	else
		endPos = hashPos - 1;
		
	if ( endPos > 0 )
		url = url.substr( 0, endPos );
		
	var dotPos = url.lastIndexOf( '.' );
	if ( dotPos > -1 && url.length - dotPos < 5 )
		return true;
	else
	{

		alert( fieldLabel + ' bevat geen geldige URL.' );
		field.focus();
		return false;
	}

}

/* String helper function */

// General purpose function to see if a suspected numeric input 
// is a positive or negative integer 
function IsInteger( inputVal )
{ 
   var inputStr = inputVal.toString() 
   var length = inputStr.length;
   var oneChar;
   for ( var i = 0; i < length; i++ ) 
   {
      oneChar = inputStr.charAt(i) 
      if ( i == 0 && oneChar == "-" )
         continue;
      
      if (oneChar < "0" || oneChar > "9")
         return false;      
   }
   return true;
} 

// Returns true if character c is an English letter 
// (A .. Z, a..z).
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.
function IsLetter( c )
{   return ( ( (c >= "a") && (c <= "z") ) || 
				( (c >= "A") && (c <= "Z") ) );
}

// whitespace characters
var Whitespace = " \t\n\r";

// Removes all characters which appear in string bag from string s.
function StripCharsInBag( s, bag )
{
   var returnString = "";

   // Search through string's characters one by one.
   // If character is not in bag, append to returnString.
	var length = s.length;
	var c;
   for( var i = 0; i < length; i++ )
   {   
      // Check that current character isn't whitespace.
      c = s.charAt( i );
      if ( bag.indexOf( c ) == -1 ) returnString += c;
   }

   return returnString;
}



// Removes all characters which do NOT appear in string bag 
// from string s.
function StripCharsNotInBag( s, bag )
{   
	var returnString = "";

   // Search through string's characters one by one.
   // If character is in bag, append to returnString.
	var c;
	var length = s.length;
   for ( var i = 0; i < length; i++ )
   {   
      // Check that current character isn't whitespace.
      c = s.charAt( i );
      if ( bag.indexOf( c ) != -1 ) returnString += c;
   }

   return returnString;
}


// Removes all whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.
function StripWhitespace( s )
{
	return StripCharsInBag ( s, Whitespace )
}
