/**
 * Notify in case of a JavaScript error
 */
window.onerror=function() {
	window.onerror=function(msg, url, linenumber){
		alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
		return true;
	}
}


/**
 * Manages errors in submitted forms.
 */
function ErrorManager(aDocument, $jqc) {
    
	this.document = aDocument
    
	this.$jqc = $jqc;

	this.errors = new Array();

	this.errorViewMode= $("input[name='errorViewMode']",$jqc).attr("value");
    
	this.formMessage = "test";
        
	this.addError = function(anError) {
		this.errors[this.errors.length] = anError;
	}
    
	this.getErrorViewMode = function(){
		return this.errorViewMode;
	}
    
	this.getFormMessage = function() {
		return this.formMessage;
	}
    
	this.setFormMessage = function(message) {
		this.formMessage = message;
	}

	this.processErrors = function() {
		this.removeErrors();
		if (this.errors.length > 0) {
			this.showErrors();
			this.focusOnFirstField();
			this.showAlert();
		}

		if (this.document.onFormError) {
			this.document.onFormError();
		}
		
		if (this.document.gloEntityForm) {
			this.document.gloEntityForm.setBeingSubmitted(false);
		}
	}

	this.removeErrors = function() {
		// Remove all shown errors
		$("dd.error", this.$jqc).remove();
		$("dt.error", this.$jqc).remove();
		$("div.error", this.$jqc).remove();
		$(".error", this.$jqc).removeClass("error").attr("title", "");
		$errorDiv = $("div.errorDiv", this.$jqc).html("<a name='errors'/>");
	}

	this.showErrors = function() {
		// Give a message that states that the form is not correct
		$errorDiv.append("<p>" + this.getFormMessage() + "</p>");

		// Show the errors
		this.applyErrors(this.errors);
	}

	this.applyErrors = function(errors) {
		for(var i=0; i<errors.length; i++) {
			this.applyErrorToFields(errors[i]);
		}
	}

	this.applyErrorToFields = function(error) {
		fields = error.getFields();
		for(var i=0; i<fields.length; i++) {
			this.applyErrorToField(fields[i], error);
		}
	}

	this.applyErrorToField = function(name, error) {
		var expression = '*[name="' + name + '"]';
		var activeInput = $(expression, this.$jqc);

		if (activeInput!=null) {
			this.applyFieldError(activeInput, error);
			return;
		}

		var k= 0;
		var element;
		var hasApplied = false;
		while(element=this.document.getElementById(name+"_"+k++) != null) {
			this.applyFieldError(element, error);
		}
		if (!hasApplied) {
			alert("Unable to apply error for field:" + name);
		}
	}

	this.applyFieldError = function(element, error) {
		element.addClass('error');
		element.attr('title', error.getMessage());
		$(element).after('<div class="error">'+error.getMessage()+'</div>');
	};

	this.focusOnFirstField = function() {
		var fields = this.errors[0].getFields();
		var firstFieldName = fields[0];
		var firstField = $('[name="'+firstFieldName+'"]', this.$jqc)[0];
		
		if (firstField!=null && firstField.focus) {
			firstField.focus();
		}
	}

	this.showAlert = function() {
		alert(this.getFormMessage());
	}
}

