// Fieldvalidator version 1.6 (2008-09-21)
// Noonday

function FieldValidator() {
	var foundError = false;
	var theForm;
	var errorMessage = '';

	this.validateForm = validateForm;
	this.highlightField = highlightField;
	this.validateRadio = validateRadio;
	this.validateEmpty = validateEmpty;
	this.validatePassword = validatePassword;
	this.validateEmail = validateEmail;
	this.validateRegExp = validateRegExp;
	this.validateIsChecked = validateIsChecked;
	this.validateLength = validateLength;
	this.validateValue = validateValue;
	this.getErrorMessage = getErrorMessage;

	function validateForm(e){
		theEvent = getEventFromE(e);
		targetElement = getTargetElementFromE(e);
		theForm = targetElement; // Warning!
		foundError = false;
		errorMessage = 'Formuläret är inte ifyllt korrekt:';
//alert('FormId: ' + theForm.id + 'antal grejor att köra: ' + fields[theForm.id].length);
		for (var i = 0; i < fields[theForm.id].length; i++) {
			if(!theForm[fields[theForm.id][i][1]].disabled){
				if(theForm[fields[theForm.id][i][1]].defaultValue != '' && 
				   theForm[fields[theForm.id][i][1]].defaultValue == theForm[fields[theForm.id][i][1]].value && theForm[fields[theForm.id][i][1]].className=='grayText'){
					theForm[fields[theForm.id][i][1]].value = '';
				}
				highlightField(fields[theForm.id][i][0], theForm[fields[theForm.id][i][1]], fields[theForm.id][i][2], fields[theForm.id][i][3]);
				if(theForm[fields[theForm.id][i][1]].defaultValue != '' && 
				   theForm[fields[theForm.id][i][1]].value == '' && theForm[fields[theForm.id][i][1]].className=='grayText'){
					theForm[fields[theForm.id][i][1]].value = theForm[fields[theForm.id][i][1]].defaultValue;
				}
			}
//alert(theForm[fields[theForm.id][i][1]].id);
		}
		if(foundError){
//alert('hittade fel!!');
			if(e && e.target){
				theEvent.stopPropagation();
				theEvent.preventDefault();
			} 
			else if(window.event && window.event.srcElement){
				theEvent.cancelBubble = true;
				theEvent.returnValue = false;
			}
alert(errorMessage);
			return false;
		}
		else{
			return true;
		}
	}
	function highlightField(fieldErrorMessage,field,validationFunction, property){
		var valid = false;
		switch(validationFunction) {
			case "validateRadio":
				if (validateRadio(field)) {
					valid = true;
				}
				break;
			case "validatePassword":
				if (eval(validationFunction + '(field.value, theForm["' + property + '"].value);')) {
					valid = true;
				}
				break;
			case "validateRegExp":
				if (eval(validationFunction + '(field.value, \'' + property + '\');')) {
					valid = true;
				}
				break;
			case "validateIsChecked":
				if (eval(validationFunction + '(field);')) {
					valid = true;
				}
				break;
			default:
				if (eval(validationFunction + '(field.value, property);')) {
					valid = true;
				}
				break;
		}
		if (valid) {
			//field.className = '';
		}
		else{
			errorMessage += "\n\n- "+fieldErrorMessage;
			foundError = true;
			//field.className = "highlighted";
		}
	}
	function validateRadio(field) {
		var radio_choice = false;
		for (counter = 0; counter < field.length; counter++) {          
			if (field[counter].checked) {
	  			radio_choice = true;
	   		}
		}
		return radio_choice;
	}
	function validateNumeric(sText) {
		var ValidChars = "0123456789";
		var IsNumber=true;
		var Char;

		for (i = 0; i < sText.length && IsNumber == true; i++) { 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) {
				IsNumber = false;
			}
      		}
		return IsNumber;
	}
	function validateEmpty(value) {
		return value && value.length > 0
	}  
	function validateLength(value, len) {
		return eval("value.length" + len);
	}    
	function validatePassword(value, value2) {
		if(value && value.length >= 4 && value == value2) {
			return true;
		}
		else {
			return false;
		}
	}
	function validateEmail(value) {
		if(validateEmpty(value) && value.match(new RegExp('^[^@]+@[^@]+\.[a-zA-Z]{2,6}$'))) {
			return true;
		}
		else {
			return false;
		}
	}
	function validateRegExp(value, regexp) {
		if(value.match(new RegExp(regexp))) {
			return true;
		}
		else {
			return false;
		}
	}
	function validateIsChecked(value) {
		if(value.checked == true) {
			return true;
		}

		else {
			return false;
		}
	}
	function validateValue(value, comparison) {
		return eval("'" + value + "'" + comparison);
	}
	function getErrorMessage() {
		return this.errorMessage;
	}
}

function isEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}
