var RAISEERROR_USE_MODAL = false;
var MODAL_TITLE = "Bad or Missing User Input";
var MODAL_LINE_ERROR = "<p><label>[label]</label>[text]</p>";

// FORM PROCESSING Functions
function initializeForm( webFormName ) {
	var webForm = $(webFormName);

	//Initialize Form
	with( webForm ) {
		setProperty('isvalid','false');
		reset( );
		formFields = getChildren('.formField');
		formButtons = getChildren('.button');
	}
	//Setup Form Fields
	for( i=0;i<formFields.length;i++ ) {
		with( formFields[i] ) {
			if( getProperty('checkfail')!=null ) {
				setAttribute('title',getProperty('checkfail'));
				addEvent(getProperty('checkOn'), function(event) { validateFormField(this,getProperty('checkFor')) });
			} else {
				setProperty('isvalid',true);
			}
		}
	}
	//Setup Form Buttons
	for( i=0;i<formButtons.length;i++ ) {
		if( formButtons[i].hasClass('Submit') )
			formButtons[i].addEvent('click', function(event) {
				event.stop( );
				submitForm( webFormName );
			});
		if( formButtons[i].hasClass('Reset') )
			formButtons[i].addEvent('click', function(event) {
				event.stop( );
				resetForm( webFormName );
			});
	}
}



function validateForm( webFormName ) {
	var webForm = $(webFormName);
	var errorMsg = "";

	webForm.setProperty('isvalid',true);
	formFields = webForm.getChildren('.formField');
	for( i=0;i<formFields.length;i++ ) {
//		if( formFields[i].hasClass('formField') ) {
			if( eval(formFields[i].getProperty('isvalid'))==null || eval(formFields[i].getProperty('isvalid'))==false ) {
				webForm.setProperty('isvalid',false);
				if( RAISEERROR_USE_MODAL )
					errorMsg = errorMsg+MODAL_LINE_ERROR.replace("[label]",formFields[i].id).replace("[text]",formFields[i].getProperty('title'));
				else
					errorMsg = errorMsg+formFields[i].id+":\n"+formFields[i].getProperty('title')+"\n\n";
			}
//		}
	}
	if( eval(webForm.getProperty('isvalid')) ) {
		return true;
	} else {
		if( RAISEERROR_USE_MODAL )
			popUp(MODAL_TITLE,"<p><b>Please complete the following information:</b></p>"+errorMsg);
		else
			alert("Please complete the following information:\n\n"+errorMsg);
		return false;
	}
}



function resetForm( webFormName ) {
	var webForm = $(webFormName);
	formFields = webForm.getChildren('.formField');

	$(webForm).reset( );

	webForm.setProperty('isvalid',false);
	for( i=0;i<formFields.length;i++ ) {
		with( formFields[i] ) {
			if( getProperty('checkfail')!=null ) {
				setAttribute('isvalid',false);
				setAttribute('title',getProperty('checkfail'));
				eStatusDisplay = getStatusDisplay( formFields[i] );
				eStatusDisplay.removeClass('valid');
				eStatusDisplay.setAttribute('title',formFields[i].getProperty('checkFail'));
			} else {
				setProperty('isvalid',true);
			}
		}
	}
}



function submitForm( webFormName ) {
	var webForm = $(webFormName);

	if( validateForm( $(webForm) ) )
		$(webForm).submit( );
}





// FIELD VALIDATION Functions
function getStatusDisplay( e ) {
	statusDisplays = e.getParent( ).getChildren('.status');
	for( i=0;i<statusDisplays.length;i++ ) {
		statusFor = statusDisplays[i].getAttribute('for')?statusDisplays[i].getAttribute('for'):statusDisplays[i].getAttribute('htmlFor');
		if( statusFor==e.id ) {
			return statusDisplays[i];
		}
	}
}



function validateFormField( e, validationCode ) {
	e.setProperty('isvalid','true');
	if( validationCode&1 ) {
		//alert('Check if Empty');
		if( !isNotEmpty(e) ) e.setProperty('isvalid','false');
	}
	if( validationCode&2 ) {
		//alert('Check if Numeric');
		if( !isNumeric(e) ) e.setProperty('isvalid','false');
	}
	if( validationCode&4 ) {
		//alert('Check if w/in Value Range');
		if( !numericValueRange(e) ) e.setProperty('isvalid','false');
	}
	if( validationCode&8 ) {
		//alert('Check if Alpha Character');
		if( !isAlphabet(e) ) e.setProperty('isvalid','false');
	}
	if( validationCode&16 ) {
		//alert('Check if Alphanumeric');
		if( !isAlphanumeric(e) ) e.setProperty('isvalid','false');
	}
	if( validationCode&32 ) {
		//alert('Check if Valid e-Mail');
		if( !isValidEMail(e) ) e.setProperty('isvalid','false');
	}
	if( validationCode&64 ) {
		//alert('Check if Selection is Made');
		if( !selectionMade(e) ) e.setProperty('isvalid','false');
	}
	
	eStatusDisplay = getStatusDisplay( e );
	if( eval(e.getProperty('isvalid')) ) {
		e.setAttribute('title',"");
		eStatusDisplay.addClass('valid');
		eStatusDisplay.setAttribute('title',"");
	} else {
		e.setAttribute('title',e.getProperty('checkFail'));
		eStatusDisplay.removeClass('valid');
		eStatusDisplay.setAttribute('title',e.getProperty('checkFail'));
	}
}





// DATA VALIDATION Functions
function isNotEmpty( e ) {
	if( e.value.length!=0 )
		return true;
	return false;
}



function isNumeric( e ) {
	var numericExpression = /^[\s?0-9-+]+$/;
	if( e.value.match(numericExpression) )
		return true;
	return false;
}



function numericValueRange( e,minValue,maxValue ) {
	var uInput = e.value;
	var numericExpression = /^[\s?0-9-+]+$/;
	if( e.value.match(numericExpression) ) {
		if( uInput.length>=minValue && uInput.length<=maxValue )
			return true;
		return false;
	} else {
		return false;
	}
}



function isAlphabet( e ) {
	var alphaExp = /^[a-zA-Z\s.]+$/;
	if( e.value.match(alphaExp) )
		return true;
	return false;
}



function isAlphanumeric( e ) {
	var alphaExp = /^[a-zA-Z0-9.+\s-#,']+$/;
	if( e.value.match(alphaExp) )
		return true;
	return false;
}



function isValidEMail( e ) {
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if( e.value.match(emailExp) )
		return true;
	return false;
}



function selectionMade( e ) {
	if( e.value!="" )
		return true;
	return false;
}
