// INITIALIZE FORM VALIDATION CODE
function setupFormValidation(frm, defaultValidation) {

	frm.validationObject = new formObject(frm.name);
	frm.errorDisplayMode = "popup";  //Default to popup.
	
	if(typeof(defaultValidation) != "undefined") 
		frm.validationObject.addVal("default", defaultValidation);

	frm.saveData = dxValidation_form_saveData;
	frm.validate = dxValidation_form_validate;
	frm.displayErrors = dxValidation_form_displayErrors;
}

//METHOD
function dxValidation_form_saveData() {
	// This function is a placeholder, and will be replaced at run-time by framework-generated code.
}

//METHOD
function dxValidation_form_validate(mode) {
	var isValid = true;
	this.saveData();
	if(typeof(this.validationObject) != "undefined") {
		if(typeof(this.validationObject.validate) == "function") {
			isValid = this.validationObject.validate(mode);
		}
	}
	this.isValid = isValid;
	return(isValid);
}

//METHOD
function dxValidation_form_displayErrors() {
	if(this.errorDisplayMode == "popup") {
		hideBoxes(true);
		with(this.validationObject) {
			if(errorMessage) alert(errorMessage);
			for(var fieldName in fields.all) {
				with (fields.all[fieldName]) {
					if(!isValid) 
						showErrorBox(fieldName, errorMessage);
				}
			}
		}
	}
	if(this.errorDisplayMode == "div") {
		for(var fieldName in this.validationObject.fields.all) {
			var field = this.validationObject.fields.all[fieldName];
			var ErrorDiv = document.getElementById("err" + fieldName);
			
			if(field.isValid) {
				if(typeof(ErrorDiv) != 'undefined') {
					if(ErrorDiv.style.display != 'none')
						ErrorDiv.style.display = 'none';
				}
			}
			else {
				if(typeof(ErrorDiv) == 'undefined') {
					alert(field.errorMessage);
				}
				else {
					ErrorDiv.innerHTML = '<table width="100%" cellpadding="0" cellspacing="0">'
									     + 	'<tr class="form-field-error">'
									     + 		'<td width="1"><img src="' + root + '/images/x.gif" align="absmiddle"/></td>'
									     + 		'<td align="left">' + field.errorMessage + '</td>'
									     + 	'</tr>'
									     +'</table>';
	
					ErrorDiv.style.display = 'inline';
				}
			}
		}
	}
}




//CLASS: formObject
function formObject(name) {
	this.name = name;	
	this.fields = new fieldsCollection(this);
	this.errorMessage = "";
	this.modes = new Object();
	
	this.addVal = function(mode, validation) {
		this.modes[mode] = new Object();
		this.modes[mode].validation = validation;
	}
	
	this.validate = function (mode) {
		if(typeof(mode) == "undefined") mode = "default";
		var isValid = this.fields.validateAll(mode);
		if(isValid) {
			if(typeof(this.modes[mode]) != "undefined") {
				if(typeof(this.modes[mode].validation) == "function") {
					isValid = this.modes[mode].validation(this);
				}
			}
		}
		return(isValid);
	}
}

//CLASS: fieldObject
function fieldObject(name) {
	this.name = name;
	this.errorMessage = "";
	this.isValid = true;
	this.modes = new Object();
	
	//METHOD
	this.addVal = function(mode, validation, required) {
		this.modes[mode] = new Object();
		this.modes[mode].validation = validation;
		this.modes[mode].required = required;
	}

	//METHOD
	this.validate = function (mode) {
		if(typeof(mode) == "undefined") mode = "default";
		if(typeof(this.modes[mode]) != "undefined") {
			if(typeof(this.modes[mode].validation) == "function") {
				this.isValid = this.modes[mode].validation(this, this.collection);
			}
			else {
				this.isValid = true;
			}
			if(this.isValid) {
				if((this.value + '').replace(/\s/g, '').length == 0) {
					if(this.modes[mode].required) {
						this.errorMessage = "This is a required field."
		 				this.isValid = false;
					}
				}
			}
			return(this.isValid);
		}
		else return(true);
	};

}

//CLASS: fieldsCollection
function fieldsCollection(form) {
	this.form = form;
	this.all = new Object();

	
	//METHOD
	this.add = function (name) {
		var field = new fieldObject(name);
	
		field.collection = this;
		field.form = this.form;
		this.all[name] = field;
		this[name] = field;

		return(field);
	}
	
	
	//METHOD
	this.validateAll = function (mode) {
		var name;
		var field;
		var retval = true;
		
		for(name in this.all) {
			field = this.all[name];
			if(typeof(field)=="object"){
				field.isValid = true;
				if(typeof(field.validate) == "function") {
				    	if(!field.validate(mode)) {
						retval = false;
					}
				}
			}
		}
		return(retval);
	}
}



//-------------------------------------------------------------------------------------------------------------
// These are shared validation functions that can be reused.



function global_validateNumeric(field) {
	var stringVal = field.value.toString();
	if(stringVal.toUpperCase() != "NAV" && stringVal.toUpperCase() != "NAP") {
		if(isNaN(stringVal.replace(/\$|\,|%/g,''))) {
			field.errorMessage = "Value must be numeric.";
			return(false);
		}
		return(true);
	}
	else return(true);
}


function global_validateDate(field) {
	if(field.value != "" && field.value.toUpperCase() != "NAV" && field.value.toUpperCase() != "NAP") {
		if (isNaN(Date.parse(field.value))) {
			field.errorMessage = "Not a valid date. Must be in the format <b>yyyy-mm-dd</b>.";
			return(false);
		}
		else return(true);
	} else return(true);
}

function global_validateTime(field) {
	if(field.value != "" && field.value.toUpperCase() != "NAV" && field.value.toUpperCase() != "NAP") {
		var currentDate = new Date();
		var sDate = currentDate.getYear() + "/" + currentDate.getMonth() + "/" + currentDate.getDay() + " " + field.value;

		if (isNaN(Date.parse(sDate))) {
			field.errorMessage = "Not a valid time.  Must be in the format HH:MM";
			return(false);
		}
		else return(true);
	} else return(true);
}
