////////////////////////////////////////////////////////////////////////////////
// $Id: validateForm.js,v 1.5 2006/07/19 13:02:23 spliffy Exp $
//
// Known problems:
//

////////////////////////////////////////////////////////////////////////////////
// FORM VALIDATION FOR NN 4+ and IE 4+
//
//
// extend the formfileds with the following parameters and it will 
// be validated:
//  o.dataType = string | float[ing[point]] | int[eger]
//  o.required = yes/1 | no/0/undefined 
//  (not yet implemented) o.errMsg   = (string) "Error message". 

////////////////////////////////////////////////////////////////////////////////
// CONFIGURATION
//
var stdErr = "Invalid Form data.\nPlease correct the following field : \n\n";
var reqErr = 'The following fields cannot be blank:\n'

//
////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
// validate Form
//
function Validator() {

	// delete darts before images (if any):
	clearImages();

	var required = new Array(1);
	var incompat = new Array(1);
	imgs = document.images;
	
	// loop over all forms
	var idxForm = 0; f = '';
	with ( document ) {
		while ( forms[idxForm] ) {
			f = forms[idxForm]
			
			// loop over all elements in this form
			var idxElement = 0; var e = '';
			while ( f.elements[idxElement] ) {
				e = f.elements[idxElement]
				myName = e.name;
				
				// here you can extend all the form objects with your own methods
				// and properties :)
				
				////////////////////////////////////////////////////////////////////////
				// check if required
				//
				if ( e.required ) {
					val = 1;
					if ( e.type == 'select-one' || e.type == 'select-multiple' ) {
						val = e.options[e.selectedIndex].value;
					} else {
						val = e.value;
					}

					if ( val == '' && ( e.required == 'yes' || e.required == 1 ) ) {
						required[required.length] = myName.toUpperCase();
						highlightImage( myName );
					}
				}
				
				////////////////////////////////////////////////////////////////////////
				// Do a type validation if needed
				//
				if ( e.dataType ) {

					switch ( e.dataType ) {

						case "string": {
							// check for string
							// alert(isNaN( e.value ) + ' ' + idxForm + ' ' + idxElement);
							if ( !isNaN( e.value ) ) {
								incompat[incompat.length] = myName.toUpperCase() + ', String expected';
								highlightImage( myName );
							}
							break;
						} // end string 


						case "int": { 
							// check for integer
						}


						case "integer": { // check for integer
							ix=0; myValue  = e.value.toString();
							
							// Char Codes
							// . = 46
							// 0 = 48
							// 9 = 57 
							
							while ( myValue.length > ix) {
								if ( myValue.charCodeAt( ix ) < 48 ||
								     myValue.charCodeAt( ix ) > 57 ) {
									incompat[incompat.length] = myName.toUpperCase() + ', Integer expected';
									highlightImage( myName );
									break; // done, exit loop
								}
								ix++;
							}
							break;
						} // end integer 

						
						case "mobile": { 
							ix=0; myValue  = e.value.toString();
							
							while ( myValue.length > ix) {
								if ( myValue.charCodeAt( ix ) < 48 ||
								     myValue.charCodeAt( ix ) > 57 ) {
									incompat[incompat.length] = myName.toUpperCase() + ', Integer expected';
									highlightImage( myName );
									break; // done, exit loop
								}
								ix++;
							}

							if (myValue.length > 2 && myValue.substr(0,2) != "07")
								incompat[incompat.length] = myName.toUpperCase() + ', Ungültige Natel Nummer';

							break;
						} // end integer 

						
						case "float": { 
							// check for float
						}

						case "floating": { 
							// check for float
						}

						case "floatingpoint": { // check for float
							ix=0; myValue  = e.value.toString();
							
							while ( myValue.length > ix) {

								if ( myValue.charCodeAt( ix ) < 48 ||
								     myValue.charCodeAt( ix ) > 57 ) {
									
									if ( myValue.charCodeAt( ix ) != 46 ) {
										incompat[incompat.length] = myName.toUpperCase() + ', Float expected';
										highlightImage( myName );
										break; // done, exit loop
									}
								}
								ix++;
							}
							break;
						} // end float 

					}
				}
				
				idxElement++;
			} // end element loop
			
			idxForm++;
		} // end form loop
		
		// check wether we have email or fax
		with ( document.adressForm ) {
/*			if ( fax.value == '' && email.value == '') {
				required[required.length] = 'EMail oder Fax muss angegeben werden!';
			} */

			if ( !__alphanum( login.value ) ) {
				required[required.length] = 'Ihr Login darf nur Zahlen und Buchstaben enthalten!';
			}
		}
	}
	
	
	
	// get strings from arrays
	resultIncomp = __notifie( incompat );
	resultRequir = __notifie( required );
	
	result = '';
	// construct the error message
	if ( resultIncomp != '' || resultRequir != '' ) {

		if ( resultIncomp != '' )
			result = stdErr + resultIncomp;
		
		if ( resultRequir != '' ) {
			if (result != '' ) {
				result = result + '\n\n';
			}
			result = result + reqErr + resultRequir;
		}
		
		// display error messgae
		alert( result );
	}
	
	if ( result == '' )
		return true;
	else
		return false;
}


////////////////////////////////////////////////////////////////////////////////
// loop over array and concatinate all contents into one string with the 
// following format:
//
// ' - element1\n
//   - element2\n
//   - elementN\n'
//
// Expect : (Array)
// Returns: (string)
//
function __notifie( arr ) {

	str = ''; i=0;
	while ( i < arr.length ) {
		if ( arr[i] ) {
			if ( arr[i] != '' ) {
				str = str + ' - ' + arr[i] + '\n';
			}
		}
		i++
	}
	return str;
}


////////////////////////////////////////////////////////////////////////////////
// loop over a character array and check if the character is a letter, '_' 
// or a number
//
//
// check charcode 
// a-z : 97-122
// A-Z : 65-90
// _   : 95
// 0-9 : 48-59
//
// Expect : string
// Returns: (bool) true if alphanum else false
//
function __alphanum( str ) {
	var ret = true; mylen = str.length;
	for( i=0; i < mylen; i++ ) {
		chr = str.charAt(i);
		if ( chr < 97 && chr > 122 && chr < 65 && chr > 90 && chr != 95 && chr < 48 && chr > 59 ) {
			ret = false;
		}
	}
	return ret;
}

////////////////////////////////////////////////////////////////////////////////
// Clear all reminder image 
// following format:
//
function clearImages() {
	for ( var i=0; document.images[i]; i++) {
		if ( document.images[i].required ) {
			document.images[i].src = downimage;
			document.images[i].required = 0;
		}
	}
}


function highlightImage( nm ) {
	if ( imgs[ nm ] ) {
		imgs[ nm ].src = upimage;
		imgs[ nm ].required = 1;
	}
}

