errors = new Object();
warnings = new Object();
errorMessage = 'Formular bitte vollständig ausfüllen';

function validTest() {
	alert("test valid");
}


// Validates given form-field (obj) and displays warnings
//	as innerHTML of DOM-Object with same ID as obID + _warn
function validate_name(obj, objname) {
	obID = obj.name;
	warnID = d(obID + '_warn');
	
	// register an error for this input field if:
	//	1: length is 0 after removing whitespace
	registerError(obID, 1, validate_length_min(obj.value, 1, objname + ' zu kurz'));
	
	// We are passing along an errorIndex / warningIndex so that multiple
	//	warnings / errors per input field do not conflict / overwrite themselves.
		
	// register a warning for this input field if:
	//	1: length < 3 after removing whitespace
	//	2: length > 24 after removing whitespace
	//	3: starts with lowercase
	registerWarning(obID, 1, validate_length_min(obj.value, 3, objname + ' zu kurz'));
	registerWarning(obID, 2, validate_length_max(obj.value, 24, objname + ' zu lang'));
	registerWarning(obID, 3, validate_starts_uppercase(obj.value, objname + ' beginnt nicht mit Großbuchstabe'));
	// display all warnings for obj
	displayWarnings(obID, warnID);
}

// adds or removes an error entry for given combination of object-ID and errorIndex
//	in the errors-object
function registerError(obID, errorIndex, error) {
	if(error.length == 0) {
		// trying to delete an object that does not exist might throw an error?
		if(typeof errors[obID] !='undefined' && typeof errors[obID][errorIndex] !='undefined') {
			delete errors[obID][errorIndex];
		}
	} else {
		// make absolutely positively sure the errors[obID][errorIndex] object exists
		if(typeof errors[obID] == 'undefined') {
			errors[obID] = new Object();
		}
		if(typeof errors[obID][errorIndex] == 'undefined') {
			errors[obID][errorIndex] = new Object();
		}
		errors[obID][errorIndex] = error;
	}
}

// adds or removes a warning for given combination of object-ID and warningIndex
// 	in the warnings-object
function registerWarning(obID, warnIndex, warning) {
	if(warning.length == 0) {
		// trying to delete an object that does not exist might throw an error?
		if(typeof warnings[obID] !='undefined' && typeof warnings[obID][warnIndex] !='undefined') {
			delete warnings[obID][warnIndex];
		}
	} else {
		// make absolutely positively sure the object exists
		if(typeof warnings[obID] == 'undefined') {
			warnings[obID] = new Object();
		}
		if(typeof warnings[obID][warnIndex] == 'undefined') {
			warnings[obID][warnIndex] = new Object();
		}
		warnings[obID][warnIndex] = warning;
	}
}

// display all "warnings" for given obID in innerHTML of field "warnID"
function displayWarnings(obID, warnID) {
	warning = '';
	for(var w in warnings[obID]) {
		warning += warnings[obID][w];
	}
	warnID.innerHTML = warning;
}

// Returns warning IF string TOO SHORT
//	else returns empty string
// 	TODO: trims whitespace
function validate_length_min(str, len, warning) {
	if(str.length < len) {
		return warning;
	} else {
		return '';
	}
}
// Returns warning IF string TOO LONG
//	else returns empty string
// 	TODO: trims whitespace
function validate_length_max(str, len, warning) {
	if(str.length > len) {
		return warning;
	} else {
		return '';
	}
}

// Returns warning IF first char is NOT UPPERCASE
function validate_starts_uppercase(str, warning) {
	var regexp = /[A-Z]/g;
	if(regexp.exec(str.substr(0, 1)) == null) {
		return warning;
	} else {
		return '';
	}
}

// Displays an alert with details and returns false to the form if there are
// 	errors in the errors object
//	TODO: Validate Fields even if they haven´t been changed by the user...
// 	Should be called by the form like this: onsubmit="return checkForErrors()"
//
function checkForErrors() {
	errCount = 0;
	errorDetails = "";
	// outer loop goes through all top-level error object entries
	//	-> those represent input fields with possible errors (too long, no uppercase, etc.)
	for(var fields in errors) {
		// inner loop goes through all error descriptions for this field
		for(var descriptions in errors[fields]) {
			errCount++;
			errorDetails += errors[fields][descriptions] + "\n";
		}
	} 
	if(errCount > 0) {
		alert(errorMessage + ":\n" + errorDetails);
		return false;
	} else {
		return true;
	}
}


// Used for adding function calls to the "window.onload" event-handler
//	"deBagelValidation"-Class uses this to ensure javascript-validation of input fields
//	runs as soon as the page has loaded, so that errors will be
//	reported even if no "onchange", "onkey", "onfocus", etc.
//	handler of the input field is invoked.
//	-> http://simonwillison.net/2004/May/26/addLoadEvent/
//
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


// Shorthand / convenience function for calling "document.getElementByID"
//
function d(o)
{
    return document.getElementById(o);
}




/*
function CountMax(max,tbl) {
	var remaining,field,showremain,val,warn,tmp;
	
	field = d("goethe_p1");
	preview = d("goethe_preview");
	warn = d("goethe_warn");
	tbl = d(tbl);
	showremain = d("showremain");
	val=field.value.length;
	
	tmp=field.value;
	tmp=replace_entity(tmp);
	tmp=unescape(tmp);
	tmp=tmp.toUpperCase();
	
	field.value=tmp;
	
	remaining = max-val;
	if(remaining<0) {
		remaining = 0;
		warn.innerHTML="Anzahl empfohlener Buchstaben &uuml;beschritten.<br>";
		tbl.bgColor="red";	
	}
	else if(remaining>=0) {
		warn.innerHTML="";
		tbl.bgColor="white";
	}
	preview.href="vorschau.php?val="+"49|"+tmp;
	showremain.innerHTML=remaining;
}

function replace_entity(str) {
	
	var len = str.length;

	tmp=escape(str)
    tmp=tmp.replace('%FC', "UE");
 	tmp=tmp.replace('%DC', "UE");
	tmp=tmp.replace('%E4', "AE");
	tmp=tmp.replace('%C4', "AE");
	tmp=tmp.replace('%F6', "OE");
	tmp=tmp.replace('%D6', "OE");
	tmp=tmp.replace('%DF', "SS");
	return tmp;
}*/
