// ----------------------------------------------------------------------
// Javascript form validation routines.
//
// Simple routines to quickly pick up obvious typos.
// All validation routines return true if executed by an older browser:
// in this case validation must be left to the server.
//
// ----------------------------------------------------------------------

FormValidator = {
	
	nbsp : 160,			// non-breaking space char
	node_text : 3,		// DOM text node-type
	emptyString : /^\s*$/,
	global_valfield : 0,	// retain valfield for timer thread
	proceed : 2,

    init : function ( ) {
	
	},
	
	// --------------------------------------------
	//            validatePresent
	// Validate if something has been entered
	// Returns true if so 
	// --------------------------------------------

	validatePresent : function ( valfield, infofield )
	{
	  	var stat = FormValidator.commonCheck ( valfield, infofield, true );
	  	if ( stat != FormValidator.proceed ) return stat;
		
   		FormValidator.setClass ( infofield, "corrected" );
		return true;
	},

	// --------------------------------------------
	//            validateTelnr
	// Validate telephone number
	// Returns true if so (and also if could not be executed because of old browser)
	// Permits spaces, hyphens, brackets and leading +
	// --------------------------------------------

	validateTelnr : function ( valfield, infofield, required ) 
	{
		var stat = FormValidator.commonCheck ( valfield, infofield, required );
	  	if ( stat != FormValidator.proceed ) return stat;

	  	var tfld = FormValidator.trim( valfield.value );  // value of field with whitespace trimmed off
	  	var telnr = /^\+?[0-9 ()-]+[0-9]$/;
	  	if ( !telnr.test( tfld )) {
	    	FormValidator.setfocus( valfield );
      		FormValidator.setClass ( infofield, "error" );
	    	return false;
	  	}

	  	var numdigits = 0;
	  	for ( var j = 0; j < tfld.length; j++ )
	    	if ( tfld.charAt( j ) >= '0' && tfld.charAt( j ) <= '9' ) numdigits++;

	  	if ( numdigits < 6 ) {
      		FormValidator.setClass ( infofield, "error" );
	    	FormValidator.setfocus( valfield );
	    	return false;
	  	}	
		
		FormValidator.setClass ( infofield, "corrected" );
	  	return true;
	},

	// --------------------------------------------
	//             validateAge
	// Validate person's age
	// Returns true if OK 
	// --------------------------------------------

	validateAge : function ( valfield, infofield, required ) 
	{
	  	var stat = FormValidator.commonCheck ( valfield, infofield, required );
	  	if ( stat != FormValidator.proceed ) return stat;

	  	var tfld = FormValidator.trim( valfield.value );
	  	var ageRE = /^[0-9]{1,3}$/
	  	if ( !ageRE.test( tfld )) {
			FormValidator.setClass ( infofield, "error" );
	    	FormValidator.setfocus( valfield );
	    	return false;
	  	}

	  	if ( tfld >= 200 ) {
			FormValidator.setClass ( infofield, "error" );
	    	FormValidator.setfocus( valfield );
	    	return false;
	  	}
		
		FormValidator.setClass ( infofield, "corrected" );
	  	return true;
	},

	// --------------------------------------------
	//               validateEmail
	// Validate if e-mail address
	// Returns true if so (and also if could not be executed because of old browser)
	// --------------------------------------------

	validateEmail : function ( valfield, infofield, required )
	{
	 	var stat = FormValidator.commonCheck ( valfield, infofield, required );
	  	if ( stat != FormValidator.proceed ) return stat;

	  	var tfld = FormValidator.trim( valfield.value );  // value of field with whitespace trimmed off
	  	var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
	  	if ( !email.test( tfld )) {
			FormValidator.setClass ( infofield, "error" );
	    	FormValidator.setfocus( valfield );
	    	return false;
	  	}
	  	
		var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
		
		FormValidator.setClass ( infofield, "corrected" );
		return true;
	},

	// --------------------------------------------
	//            commonCheck
	// Common code for all validation routines to:
	// (a) check for older / less-equipped browsers
	// (b) check if empty fields are required
	// Returns true (validation passed), 
	//         false (validation failed) or 
	//         proceed (don't know yet)
	// --------------------------------------------


	commonCheck : function ( valfield, infofield, required )
	{
	  	if ( !document.getElementById ) 
			return true;  // not available on this browser - leave validation to the server
	  	
		
			var elem = document.getElementById( infofield );
	  //	if ( !elem.firstChild ) return true;  // not available on this browser 
	  	//if ( elem.firstChild.nodeType != node_text ) return true;  // infofield is wrong type of node  
		if ( FormValidator.emptyString.test( valfield.value )) {
			if ( required ) {
	      		FormValidator.setClass ( infofield, "error" );  
		 		FormValidator.setfocus( valfield );
	      		return false;
	    	} else {
	    		FormValidator.setClass ( infofield, "corrected" );   // OK
	     		return true;  
	    	}
	  	}
	  	return FormValidator.proceed;
	},
	
	// --------------------------------------------
	//                  msg
	// Display warn/error message in HTML element.
	// commonCheck routine must have previously been called
	// --------------------------------------------

	msg : function ( fld, msgtype, message ) 
	{
	  	// setting an empty string can give problems if later set to a 
	  	// non-empty string, so ensure a space present. (For Mozilla and Opera one could 
	  	// simply use a space, but IE demands something more, like a non-breaking space.)
		var dispmessage;
		if ( FormValidator.emptyString.test( message )) 
			dispmessage = String.fromCharCode( nbsp );    
		else  
			dispmessage = message;

		var elem = document.getElementById( fld );
		elem.firstChild.nodeValue = dispmessage;  

		elem.className = msgtype;   // set the CSS class to adjust appearance of message
	},

	setClass : function ( fld, newClass ) 
	{
		var elem = document.getElementById( fld );
		elem.className = newClass;   // set the CSS class to adjust appearance of message
	},
	
	// --------------------------------------------
	//                  setfocus
	// Delayed focus setting to get around IE bug
	// --------------------------------------------

	setFocusDelayed : function ()
	{
		global_valfield.focus();
	},

	setfocus : function ( valfield )
	{
	  // save valfield in global variable so value retained when routine exits
	  global_valfield = valfield;
	  setTimeout( 'setFocusDelayed()', 100 );
	},

	// --------------------------------------------
	//                  trim
	// Trim leading/trailing whitespace off string
	// --------------------------------------------

	trim : function ( str )
	{
		return str.replace( /^\s+|\s+$/g, '' );
	}

};
