
var validationErrorStrings = {
								'required': 	'Field is required.',
								
								'different': 	'%s is not the same.',
								
								'minlength': 	'Must be atleast %s characters long.',
								'maxlength': 	'May not be more then %s characters long.',
								
								'minoptions': 	'Must select atleast %s options.',
								'maxoptions': 	'May not select more then %s options.',
								
								'notinteger': 	'Not a valid number.',
								'notfloat': 	'Not a valid number.',
								'notemail': 	'Not a valid email address.',
								'noteudate': 	'Not a valid date.',
								'notusdate': 	'Not a valid date.',
								
								'minvalue': 	'Must enter a number equal or bigger then %s.',
								'maxvalue': 	'Must enter a number equal or smaller then %s.',
								
								'decimals': 	'Must have %s decimals.',
								'mindecimals': 	'Must have atleast %s decimals.',
								'maxdecimals': 	'May not have more then %s decimals.'
							};
		
var validationErrors = new Array();
var validationChecks = new Object();

var initVals = new Array();
/**
*
**/
function validateInit( elemId, checks )
{
	var idx = initVals.length;
	initVals[ idx ] = [ elemId, checks ];
	
	setTimeout( "validateInitialization( initVals[" + idx + "][ 0 ], initVals[" + idx + "][ 1 ] )", 10 );
}

/**
*
**/
function validateInitialization( elemId, checks )
{
	var formEl = document.forms[ elemId ];	
	if( formEl == null || typeof formEl == 'undefined' ) {
		alert( 'Form "' + elemId + '" not found' );
		return false;
	}
	
	for( i in checks ) {
		var inpEl = formEl[ i ];
		var chks = checks[ i ];
		if( inpEl == null || typeof inpEl == 'undefined' ) {
			alert( 'Element "' + i + '" not found in form "' + elemId + '".' );
			return false;
		}
		
		var errorSpan = document.getElementById( 'validate_error_' + i );	
		if( errorSpan == null || typeof errorSpan == 'undefined' ) {	
			alert( 'Field "' + i + '" has no error span-tag.' );			
		}
		errorSpan.className = 'validate_error';
			
		var inputType = inpEl.type;
		if( inputType == null || typeof inputType == 'undefined' ) {
			inputType = inpEl.item( 0 ).type;
		}
		if( inputType == null || typeof inputType == 'undefined' ) {
			alert( 'Field "' + i + '" has an unknown input type.' );
			continue;
		}
		
		inputType = inputType.toUpperCase();
		switch( inputType ) 
		{
			case 'TEXT':
			case 'PASSWORD':
			case 'FILE':
			case 'SELECT-ONE':
			case 'SELECT-MULTIPLE':
			case 'TEXTAREA':
			case 'CHECKBOX':
			case 'RADIO':
				break;
			default:
				alert( 'Field type "' + inputType + '" is not supported.' );
		}
		
		if( typeof chks.example != 'undefined' ) {
			if( inpEl.value == '' || inpEl.value == chks.example ) {
				inpEl.value = chks.example;
				inpEl.className = inpEl.className + ' showingExample';				
			}
			else {
				inpEl.className = inpEl.className.replace( 'showingExample', '' );	
			}
			validateAttachEvent( inpEl, 'focus', validateFocusExample, false );
			validateAttachEvent( inpEl, 'keypress', validateRemoveExample, false );
			validateAttachEvent( inpEl, 'change', validateChangeExample, false );
		}
	}
	
	validationChecks[ elemId ] = checks;
	
}

function validateFocusExample( evt )
{
	var targetEl = null;
	if( evt.target != null ) {
		targetEl = evt.target;
	}
	
	else {
		targetEl = evt.srcElement;
	}
	if( targetEl != null && targetEl.className.match( 'showingExample' ) ) {
		targetEl.select();
	}
	return true;
}

function validateChangeExample( evt )
{
	var targetEl = null;
	if( evt.target != null ) {
		targetEl = evt.target;
	}
	
	else {
		targetEl = evt.srcElement;
	}
	if( evt.charCode == 0 ) return true;
	if( targetEl != null && targetEl.className.match( 'showingExample' ) ) {
		targetEl.className = targetEl.className.replace( 'showingExample', '' );
		targetEl.value='';
	}
	return true;
}

function validateRemoveExample( evt )
{
	var targetEl = null;
	if( evt.target != null ) {
		targetEl = evt.target;
	}
	
	else {
		targetEl = evt.srcElement;
	}
	if( typeof evt.charCode != 'undefined' && evt.charCode == 0 ) return true;
	if( typeof evt.charCode == 'undefined' && evt.keyCode < 32 ) return true;
	if( targetEl != null && targetEl.className.match( 'showingExample' ) ) {
		targetEl.className = targetEl.className.replace( 'showingExample', '' );
		targetEl.value='';
	}
	return true;
}

/**
*
**/
function validateForm( elemId )
{
	var checks = validationChecks[ elemId ];
	if( checks == null || typeof checks == 'undefined' ) {
		alert( 'Form "' + elemId + '" validation not initialized' );
		return false;
	}
	
	var formEl = document.forms[ elemId ];	
	if( formEl == null || typeof formEl == 'undefined' ) {
		alert( 'Form "' + elemId + '" not found' );
		return false;
	}
	
	var errHead = document.getElementById( "error_heading_" + elemId );
	if( errHead != null && typeof errHead != 'undefined' ) {
		errHead.className = "validate_error_heading";
	}
	
	validationErrors = new Array();
	
	for( i in checks ) {
		var inpEl = formEl[ i ];
		var chks = checks[ i ];
		if( inpEl == null || typeof inpEl == 'undefined' ) {
			alert( 'Element "' + i + '" not found in form "' + elemId + '".' );
			return false;
		}
		
		var errorSpan = document.getElementById( 'validate_error_' + i );		
		errorSpan.className = 'validate_error';
			
		var inputType = inpEl.type;
		if( inputType == null || typeof inputType == 'undefined' ) {
			inputType = inpEl.item( 0 ).type;
		}
		if( inputType == null || typeof inputType == 'undefined' ) {
			alert( 'Field "' + i + '" has an unknown input type.' );
			continue;
		}
		
		inputType = inputType.toUpperCase();
		switch( inputType ) 
		{
			case 'TEXT':
				validateInputText( formEl, inpEl, chks );
				break;
			case 'PASSWORD':
				validateInputText( formEl, inpEl, chks );
				break;
			case 'FILE':
				validateInputText( formEl, inpEl, chks );
				break;
			case 'SELECT-ONE':
				validateInputSelect( formEl, inpEl, chks );
				break;
			case 'SELECT-MULTIPLE':
				validateInputMultiSelect( formEl, inpEl, chks );
				break;
			case 'TEXTAREA':
				validateInputText( formEl, inpEl, chks );
				break;
			case 'CHECKBOX':
				validateInputCheckBox( formEl, inpEl, chks );
				break;
			case 'RADIO':
				validateInputRadio( formEl, inpEl, chks );
				break;
			default:
				alert( 'Unknown input type "' + inputType + '"' );
		}
	}
	
	
	// error occures?
	if( validationErrors.length != 0 ) {
		if( errHead != null && typeof errHead != 'undefined' ) {
			errHead.className = "show_validate_error_heading";
		}
		
		var l = validationErrors.length;
		for( i=0; i<l; i++ ) {
			var errField = validationErrors[ i ][ 0 ];
			var errStr = validationErrors[ i ][ 1 ];
			var errorSpan = document.getElementById( 'validate_error_' + errField );		
			errorSpan.className = 'show_validate_error';
			errorSpan.innerHTML = errStr;
			if( i == 0 ) {
				var inp = formEl[ errField ];
				var inputType = inp.type;
				if( inputType == null || typeof inputType == 'undefined' ) {
					inputType = inp.item( 0 ).type;
				}
				inputType = inputType.toUpperCase();
				
				switch( inputType ) {
					case 'TEXT':
					case 'PASSWORD':
					case 'FILE':
					case 'SELECT-ONE':
					case 'SELECT-MULTIPLE':
					case 'TEXTAREA':
						inp.focus();
						//validateInputText( formEl, inpEl, chks );
						break;
					case 'CHECKBOX':
					case 'RADIO':
						if( typeof inp.length == 'undefined' ) 
							inp.focus();
						else 
							inp.item( 0 ).focus();
						//validateInputRadio( formEl, inpEl, chks );
						break;
					default:
						//alert( 'Unknown input type "' + inputType + '"' );
				}
				//inp.focus();					
			}
		}
		return false;
	}
	
	//remove any examples before posting
	var l = formEl.elements.length;
	for( var i=0; i<l; i++ ) {
		var el = formEl.elements[ i ];
		if( el.className.match( 'showingExample' ) ) {
			el.value = '';
		}
	}
	return true;
}

/**
*
**/
function validateErrorString( idx, val1 )
{
	var str = validationErrorStrings[ idx ];
	if( val1 != null && typeof val1 != 'undefined' ) {
		str = str.replace( '%s', val1 );	
	}
	return str;
}

/**
*
**/
function validateGetInputText( elem ) 
{	
	var val = elem.value;
	var inpType = elem.type;
	inpType = inpType.toUpperCase();
	if( elem.className.match( 'showingExample' ) ) {
		return '';
	}
	
	if( inpType != 'PASSWORD' ) {
		val = val.replace(/^\s*/, "").replace(/\s*$/, "");
		elem.value = val;
	}
	return val;
}

/**
*
**/
function validateInputText( formElem, elem, checks ) 
{
	var val = validateGetInputText( elem );
	
	if( typeof checks.required != 'undefined' && checks.required ) {
		if( val.length == 0 || elem.className.match( 'showingExample' ) ) {
			validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'required' ) ]; 
			return;
		}	
	}
	if( typeof checks.minlength != 'undefined' ) {
		if( val.length < checks.minlength ) {
			validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'minlength', checks.minlength ) ];
			return;
		}	
	}
	if( typeof checks.maxlength != 'undefined' ) {
		if( val.length > checks.maxlength ) {
			validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'minlength', checks.maxlength ) ];
			return;
		}	
	}
	
	if( typeof checks.equals != 'undefined' ) {
		var inpName = checks.equals;
		var compareInpEl = formElem[ inpName ];
		if( val != validateGetInputText( compareInpEl ) ) {
			validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'different', checks.equalslabel ) ];
			return;
		}	
	}
	if( typeof checks.syntax != 'undefined' && val.length > 0 ) {
		switch( checks.syntax ) {
			case 'integer':
				if( !validateFormatInteger( val ) ) {
					validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'notinteger' ) ];
					return;
				}
				break;
				
			case 'float':
				if( !validateFormatFloat( val ) ) {
					validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'notfloat' ) ];
					return;
				}
				break;
				
			case 'email':
				if( !validateFormatEmail( val ) ) {
					validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'notemail' ) ];
					return;
				}
				break;
				
			case 'eudate':
				if( !validateFormatEuroDate( val ) ) {
					validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'noteudate' ) ];
					return;
				}
				break;
				
			case 'usdate':
				if( !validateFormatUSDate( val ) ) {
					validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'notusdate' ) ];
					return;
				}
				break;
				
		}
		switch( checks.syntax ) {
			case 'integer':
			case 'float':
				if( typeof checks.minvalue != 'undefined' ) {
					if( val < checks.minvalue ) {
						validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'minvalue', checks.minvalue ) ];
						return;
					}
				}
				if( typeof checks.maxvalue != 'undefined' ) {
					if( val > checks.maxvalue ) {
						validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'maxvalue', checks.maxvalue ) ];
						return;
					}
				}
				break;
		}
		switch( checks.syntax ) {
			case 'float':
				var dec = val.match( '\\.([0-9]+)$' );
				if( dec != null ) {
					dec = dec[ 1 ];
				}
				else {
					dec = '';
				}
				
				if( typeof checks.mindecimals != 'undefined' && typeof checks.maxdecimals != 'undefined'
				 && checks.mindecimals == checks.maxdecimals ) {
						if( dec.length != checks.mindecimals ) {
							validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'decimals', checks.mindecimals ) ];
							return;
						}
				}
				else {
					if( typeof checks.mindecimals != 'undefined' ) {
						if( dec.length < checks.mindecimals ) {
							validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'mindecimals', checks.mindecimals ) ];
							return;
						}
					}
					if( typeof checks.maxdecimals != 'undefined' ) {
						if( dec.length > checks.maxdecimals ) {
							validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'maxdecimals', checks.maxdecimals ) ];
							return;
						}
					}
				}
				break;
		}
	}
}


/**
*
**/
function validateInputSelect( formElem, elem, checks ) 
{
	var val = elem.value;
	
	if( typeof checks.required != 'undefined' && checks.required ) {
		if( val.length == 0 ) {
			validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'required' ) ]; 
			return;
		}	
	}
}

/**
*
**/
function validateInputMultiSelect( formElem, elem, checks ) 
{
	var val = elem.value;
	
	
	var selectedCnt = 0;
	var l = elem.options.length;
	for( i=0; i<l; i++ ) {
		if( elem.options[ i ].value.length > 0 && elem.options[ i ].selected ) {
			selectedCnt++;	
		}
	}
	
	if( typeof checks.required != 'undefined' && checks.required ) {
		if( selectedCnt == 0 ) {
			validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'required' ) ]; 
			return;
		}	
	}
	
	if( typeof checks.minoptions != 'undefined' ) {
		if( selectedCnt < checks.minoptions ) {
			validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'minoptions', checks.minoptions ) ]; 
			return;
		}	
	}
	if( typeof checks.maxoptions != 'undefined' ) {
		if( selectedCnt > checks.maxoptions ) {
			validationErrors[ validationErrors.length ] = [ elem.name, validateErrorString( 'maxoptions', checks.maxoptions ) ]; 
			return;
		}	
	}
}

/**
*
**/
function validateInputRadio( formElem, elem, checks ) 
{
	var inpName = elem.item( 0 ).name;
	
	var selectedCnt = 0;
	var l = elem.length;
	for( i=0; i<l; i++ ) {
		if( elem.item( i ).value.length > 0 && elem.item( i ).checked ) {
			selectedCnt++;	
		}
	}
	
	if( typeof checks.required != 'undefined' && checks.required ) {
		if( selectedCnt == 0 ) {
			validationErrors[ validationErrors.length ] = [ inpName, validateErrorString( 'required' ) ]; 
			return;
		}	
	}
}

/**
*
**/
function validateInputCheckBox( formElem, elem, checks ) 
{
	if( typeof elem.length != 'undefined' ) {
		var inpName = elem.item( 0 ).name;
		
		var selectedCnt = 0;
		var l = elem.length;
		for( i=0; i<l; i++ ) {
			if( elem.item( i ).value.length > 0 && elem.item( i ).checked ) {
				selectedCnt++;	
			}
		}
	}
	else {
		var inpName = elem.name;
		
		var selectedCnt = 0;
		var l = elem.length;
		
		if( elem.checked ) selectedCnt++;
	}
	
	if( typeof checks.required != 'undefined' && checks.required ) {
		if( selectedCnt == 0 ) {
			validationErrors[ validationErrors.length ] = [ inpName, validateErrorString( 'required' ) ]; 
			return;
		}	
	}
	
	if( typeof checks.minoptions != 'undefined' ) {
		if( selectedCnt < checks.minoptions ) {
			validationErrors[ validationErrors.length ] = [ inpName, validateErrorString( 'minoptions', checks.minoptions ) ]; 
			return;
		}	
	}
	if( typeof checks.maxoptions != 'undefined' ) {
		if( selectedCnt > checks.maxoptions ) {
			validationErrors[ validationErrors.length ] = [ inpName, validateErrorString( 'maxoptions', checks.maxoptions ) ]; 
			return;
		}	
	}
}

/**
*
**/
function validateFormatInteger( val )
{
	return Math.floor( val ) == val;
}

/**
*
**/
function validateFormatFloat( val )
{
	return ( val * 1 ) == val;
}

/**
*
**/
function validateFormatEmail( val )
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	return filter.test( val );
}

/**
*
**/
function validateFormatUSDate( val )
{
	var res = val.match( /^([0-9]{1,2})[\/-]([0-9]{1,2})[\/-]([0-9]{4})$/ );
	if( res == null ) return false;
	var dt = new Date();
	dt.setDate( res[ 2 ] );
	dt.setMonth( res[ 1 ]-1 );
	dt.setFullYear( res[ 3 ] );
	
	return ( dt.getDate() * 1 == res[ 2 ] * 1
	      && dt.getMonth() * 1 + 1 == res[ 1 ] * 1
	      && dt.getFullYear() * 1 == res[ 3 ] * 1 );
	      
}

/**
*
**/
function validateFormatEuroDate( val )
{
	var res = val.match( /^([0-9]{1,2})[\/-]([0-9]{1,2})[\/-]([0-9]{4})$/ );
	if( res == null ) return false;
	var dt = new Date();
	dt.setDate( res[ 1 ] );
	dt.setMonth( res[ 2 ]-1 );
	dt.setFullYear( res[ 3 ] );
	
	return ( dt.getDate() * 1 == res[ 1 ] * 1
	      && dt.getMonth() * 1 + 1 == res[ 2 ] * 1
	      && dt.getFullYear() * 1 == res[ 3 ] * 1 );
	      
}

/**
*
**/
function validateAttachEvent( obj, evt, fnc, useCapture ){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		validateAttachEventAncient(obj,evt,fnc);
		obj['on'+evt]=function(){ validateFireEvent(obj,evt) };
	}
} 

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function validateAttachEventAncient( obj, evt, fnc )
{
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}

function validateFireEvent( obj, evt )
{
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}

