  
/*
Name    validate.js
====

Description
===========
Suite of functions that perform client-side validation.
    
Author / Date
=============
Gavin McBain (GMAC Consulting Ltd)
20th October 2003

Modifications
=============
None  
*/

// Ensure that the string is valid
function isValidString(objVal,minLength,maxLength)
{
   var strVal = objVal.value;
	
   // Ensure that we have a string
	if ( !strVal.length ) {
	   objVal.focus();
		return false;
   }
	
	// Check the min length of the string 
   if ( minLength != -1 && strVal.length < minLength ) {
	   objVal.focus();
		return false;
   }
	
	// Check the max length of the string
	if ( maxLength != -1 && strVal.length > maxLength ) {
	   objVal.focus();
		return false;
	}
 
   return true;
}

// Ensure that the value is an integer and within range
function isValidInt(objVal,minValue,maxValue)
{
   var intVal = parseInt(objVal.value,10);
	
	if ( isNaN(intVal) )
	{
	   objVal.focus();
		return false;
	}

	if ( minValue != -1 && intVal < minValue )
	{
	   objVal.focus();
		return false;
	}

   if ( maxValue != -1 && intVal > maxValue )
	{
	   objVal.focus();
		return false;
	}

   return true;		
}


// Ensure that the email address is valid
function isValidEmail(objVal)
{
   var emailexp = /^[a-z][a-z_0-9\.]+@[a-z_0-9\.\-\_]+\.[a-z]{3}$/
   var strVal = objVal.value;

   if ( !strVal.length || !emailexp.test(strVal) )	{
	   objVal.focus();
		return false;
	}

   return true;
}

// Ensure that an input date conforms to the dd-mmm-yyyy format
function isValidDate(objVal)
{
	var val,day,month,year;
	
	val = objVal.value.toLowerCase();
	
	// first check the number of chars
	if ( val.length < 10 || val.length > 11 ) {
	   objVal.focus();
   	return false;
	}
	
	// split the date into the dmy parts
	var dmy = val.split('-');
	if ( dmy.length != 3 ) {
      objVal.focus();
	   return false;
	}
	
	// check on days - simple check first then check with months later
	day = parseInt(dmy[0],10);
	if ( isNaN(day) || day < 1 || day > 31 ) {
      objVal.focus();
	   return false;
	}
	
	// check on months
	switch ( dmy[1] )
	{
		// no need to check days for jan,mar,nay,jul,aug,oct,dec - checks are done above

		// months with only 30 days
		case 'apr':
		case 'jun':
		case 'sep':
		case 'nov':
			
			// must be less than 30 days for these months
			if ( day == 31 ) {
            objVal.focus();
			   return false;
			}
			
			break;
			
		// leap year month
		case 'feb':

			// Check for leap year
			var year = parseInt(dmy[2],10);
			
			if ( (!(year%4) && (year%100)) || !(year%400) )
			{
				// leap year
				if ( day > 29 ) {
               objVal.focus();
			      return false;
			   }				
			}
			else
			{
				if ( day > 28 ) {
               objVal.focus();
			      return false;
			   }				
			}
											
			break;

	}
	
	// finally passed 
	return true;

}

