/**
 * The array holding the field names and the info related to them
 * 
 * fieldsToBeValidated[i] = new Array(theFieldName, fieldNameDisplayText, validationType);
 */
var fieldsToBeValidated = new Array();

/**
 * The validation types. We need to keep this in sync with the server side validation types
 */
var validationTypeNonEmpty = 1;
var validationTypeEmail = 2;
var validationTypeStrlen = 4;
var validationTypeNumber = 5;

/**
 * The error/success messages
 */
var displayMessages = new Array();

/**
 * 
 */
var useInnerHTML = false;
var lineTerminationString = '\n';

/**
 * Flag to indicate whether the validation resulted in any errors
 */
var containsErrors = false;
 
/**
 * Check if a given field contains a valid email address
 */
function validateNonEmptyField(theForm, fieldInfo)
{
    var isEmpty  = false;
    var theField = theForm.elements[fieldInfo[0]];
    // Whether the replace function (js1.2) is supported or not
    var isRegExp = (typeof(theField.value.replace) != 'undefined');

    if (!isRegExp) {
        isEmpty      = (theField.value == '') ? true : false;
    } else {
        var space_re = '\\s+';
        isEmpty      = (theField.value.replace(space_re, '') == '') ? true : false;
    }

		if(isEmpty){
			addError(fieldInfo, errorText_elementIsEmpty);	
			containsErrors = true;
		}
		
    return isEmpty;
} 

/**
 * Check if a given field contains a valid email address
 */
//function validateStringLength(theForm, fieldInfo)
//{
//    var isEmpty  = false;
//    var theField = theForm.elements[fieldInfo[0]];
//    // Whether the replace function (js1.2) is supported or not
//    var isRegExp = (typeof(theField.value.replace) != 'undefined');
//
//    if (!isRegExp) {
//        isEmpty      = (theField.value == '') ? true : false;
//    } else {
//        var space_re = '\\s+';
//        isEmpty      = (theField.value.replace(space_re, '') == '') ? true : false;
//    }
//
//		if(isEmpty){
//			addError(fieldInfo, errorText_elementIsEmpty);	
//			containsErrors = true;
//		}
//		
//    return isEmpty;
//} 

/**
 * Check if a given field contains a valid email address
 */
function validateEmailField(theForm, theFieldName){
	var pattern = "/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/";
	
	if(!validateNonEmptyField(theForm, theFieldName)){
									
	}
}

/**
 * Check for valid string lengths
 */
function validateStringLength(theForm, fieldInfo){
    var theField = theForm.elements[fieldInfo[0]];
		var minLength = fieldInfo[3][0];
		var maxLength = fieldInfo[3][1];
		var valueLength = theField.value.length;

		if(minLength && valueLength < minLength){
			addError(fieldInfo, errorText_lessThanMinChars, '%2$d', minLength);	
			containsErrors = true;
		}else if(maxLength && valueLength > maxLength){
			addError(fieldInfo, errorText_exceedsMaxChars, '%2$d', maxLength);	
			containsErrors = true;
		}
}

function validateNumber(theForm, fieldInfo){
    var theField = theForm.elements[fieldInfo[0]];
		var minValue = fieldInfo[3][0];
		var maxValue = fieldInfo[3][1];
		
		var fieldValue = theField.value;

		/*if(fieldValue.isNAN){
			addError(fieldInfo, errorText_notANumber);	
			containsErrors = true;
			return;
		}*/
	
		if(minValue != null && fieldValue < minValue){
			addError(fieldInfo, errorText_numberLessThanMinimum, '%2$d', minValue);	
			containsErrors = true;
			return;
		}

		if(maxValue != null && fieldValue > maxValue){
			addError(fieldInfo, errorText_numberGreaterThanMaximum, '%2$d', maxValue);	
			containsErrors = true;
			return;
		}
}

function getFieldInfoForValidation(theFieldName){
	for(var i = 0; i < fieldsToBeValidated.length; i++){
		if(fieldsToBeValidated[i][0] == theFieldName){
			return fieldsToBeValidated[i];
		}		
	}
	
	return null;
}

function addError(fieldInfo, errorText, param1, param1Value){
	var msg;
	
	if(fieldInfo){
		msg = errorText.replace('%1\$s', fieldInfo[1]);
	} else {
		msg = errorText;
	}
	
	
	if(param1){
		msg = msg.replace(param1, param1Value);
	}
	displayMessages[displayMessages.length] = msg;
}	

function validate(theForm){
	containsErrors = false;
	
	for(var i = 0; i < fieldsToBeValidated.length; i++){
		
		switch(fieldsToBeValidated[i][2]){
			case validationTypeNonEmpty:
				
				validateNonEmptyField(theForm, fieldsToBeValidated[i]);
				break;
			
			case validationTypeEmail:
				validateEmailField(theForm, fieldsToBeValidated[i]);
				break;

			case validationTypeStrlen:
				validateStringLength(theForm, fieldsToBeValidated[i]);
				break;

			case validationTypeNumber:
				validateNumber(theForm, fieldsToBeValidated[i]);
				break;
		}	
	}	
	doDisplayMessages();
	resetErrors();
	return !containsErrors;
}

function resetErrors(){
	displayMessages = new Array();
}	

function doDisplayMessages(){
	if(displayMessages && displayMessages.length > 0){
		var msg = '';
		for(var i = 0; i < displayMessages.length; i++){
				msg += displayMessages[i]+ "\n";
		}
		alert(msg);
	}
}








/**
* Submits the form
*/
function submitForm(theForm){
	theForm.submit();
}

/**
* Confirms an action
*/
function confirmAction(theMessage, theForm, theAction)
{
    if(confirm(theMessage)){
    	theForm.action = theAction;
    	theForm.submit();
    }
}


/**
 * Get element by id
 */
function getById(id) {
   return document.getElementById?document.getElementById(id):(document.all?document.all(id):null);
}

function doNothing(){
	//
}


