/*
| =======          I                   ==          I    =
|    I             I                    I          I
|    I   ===   === I ===  I ===   ===   I  I    I ====  I   ===  I ===
|    I  /   \ I    I/   I I/   I I   I  I  I    I  I    I  I   I I/   I
|    I  ===== I    I    I I    I I   I  I  I    I  I    I  I   I I    I
|    I  \     I    I    I I    I I   I  I  I   /I  \    I  I   I I    I
|    I   ===   === I    I I    I  ===  ===  === I   ==  I   ===  I    I
|                 +---------------------------------------------------+
+----+            |  +++++++++++++++++++++++++++++++++++++++++++++++++|
     |            |             ++++++++++++++++++++++++++++++++++++++|
     +------------+                          +++++++++++++++++++++++++|
                                                        ++++++++++++++|
             A U T O M A T I O N     T E C H N O L O G Y         +++++|

 (C) COPYRIGHT 2009 TECHNOLUTION BV, GOUDA NL

 $Id$
*/

/* 
 * Contains logic to check the contents of web forms and give user feedback
 *
 */
 
var FIELDTYPE_TEXT = 0;
var FIELDTYPE_EMAIL = 1;
var FIELDTYPE_NUMBER = 4;
var FIELDTYPE_HIDDEN = 5;
var FIELDTYPE_SELECT_OPTION = 6;
var FIELDTYPE_FILE = 7;

var NBSP = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var EMPTY_STRING = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread
var RE_VALID_EMAIL_ADDRESS = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
var RE_SUSPICIOUS_EMAIL_ADDRESS = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;


/**
 * Trims trailing whitespaces from a string using a regular expression
 */
function trim(str) {
  return str.replace(/^\s+|\s+$/g, '');
}

/**
 * Displays the message in the specified field and set the CSS class to adjust 
 * appearance of message. If the message is empty a &nbsp; is inserted
 */
function displayMessage(fieldID, msgtype, message) {
	var dispmessage;
  	if (EMPTY_STRING.test(message)) { 
    	dispmessage = String.fromCharCode(NBSP);
	}    
  	else {
    	dispmessage = message;
    }

  	var elem = document.getElementById(fieldID);
	elem.innerHTML = dispmessage;  
	elem.className = msgtype;   // 
}

/**
 * Check if a required text field is present  
 */
function checkTextField (fieldToCheck, feedbackSectionId, isRequired) {
 
  	if (EMPTY_STRING.test(fieldToCheck.value)) {
		if (isRequired) {
      		displayMessage(feedbackSectionId, "error", "Dit veld is verplicht.");  
      		return false;
    	}
    	else {
      		displayMessage(feedbackSectionId, "", ""); 
      		return true;  
    	}
  	}
	displayMessage(feedbackSectionId, "", "");   
	return true;  	
}

/**
 * Check if a required e-mail field is present and if a valid e-mail 
 * address is entered.  
 */
function checkEmailField (fieldToCheck, feedbackSectionId, isRequired) {
	
	// An e-mail field is also a text field so check this first
	if (!checkTextField(fieldToCheck, feedbackSectionId, isRequired)) {
		return false;	
	}

  	var fieldValue = trim(fieldToCheck.value);  

  	if (!RE_VALID_EMAIL_ADDRESS.test(fieldValue)) {
    	displayMessage(feedbackSectionId, "error", "Dit is geen geldig e-mail adres.");
    	return false;
  	}

	if (!RE_SUSPICIOUS_EMAIL_ADDRESS.test(fieldValue)) { 
    	displayMessage (feedbackSectionId, "warn", "Is dit een geldig e-mail adres?");
    }
  	else {
    	displayMessage(feedbackSectionId, "", "");
    }
  	return true;
}

/**
 * Generic function to check the field validity
 */
function validatefield( fieldToCheck, feedbackSectionId, fieldType, isRequired) {
	
	switch (fieldType) {
		case FIELDTYPE_TEXT: 
			return checkTextField(fieldToCheck, feedbackSectionId, isRequired);
		break;	
		case FIELDTYPE_FILE: 
			return checkTextField(fieldToCheck, feedbackSectionId, isRequired);
		break;	
		case FIELDTYPE_EMAIL: 
			return checkEmailField(fieldToCheck, feedbackSectionId, isRequired);
		break;	
	}
	return true;
}

function validateFormOnSubmit(formToCheck) {
	var errorCount = 0;
	for (i in formValidationRules) {
 		
 		
 		var field = document.getElementById(formValidationRules[i].fieldID);
 		if (field != null) {
 			if (!validatefield(field, formValidationRules[i].feedbackID, formValidationRules[i].fieldType, formValidationRules[i].isRequired)) {
 				errorCount++;
 			}
 		}
	} 
	return (errorCount == 0);
}
 
 
 
 
 




