var  allValidations = new Array();

function finalCheck(form, submit)
{
	 if (submit == null)
	 {
	   submit = true;
	 }
	var valid = true;
	
	for(var i=0; i<allValidations.length; i++)
	{
		if( eval(allValidations[i].funcCall) == false )
		{
			valid = makeValidation(allValidations[i]);
		}
	}
	
	if(valid==true && submit==true)
	{
		form.submit();
	}
	
	return valid;
}

function CLabel( id, errStyle )
{
	this.id=id;
	this.errStyle=errStyle;
}

function CControl(elmID)
{
	this.elmID = elmID;
	this.ntfcArea;
	this.label;
	this.validations = new Array();
	this.getErrorState = function(){
							for(var i=0; i<this.validations.length; i++)
							{
								if( eval(this.validations[i].funcCall) == false )
								{
									return false;
								}
							}
							return true;
						}

}
function CValidation(varName, funcCall, control, errText, event, onsubmit)
{
	this.varName = varName
	this.funcCall = funcCall;
	this.control = control;
	this.text = errText;
	this.event = event;
	this.onsubmit = onsubmit;
}

function CNotification(ntfcAreaID, errStyle)
{
	this.ntfcAreaID = ntfcAreaID;
	this.errStyle = errStyle;
	this.status = true;
	// this.getStatus = getNtfcStatus;
	// this.addValidation = addNtfcValidation;
	// this.generateValidation = function(){}
	
}

function addNtfcValidation( validation )
{
	allValidations.push(validation);
	if( validation.onsubmit == false )
	{
		eval(validation.varName).control.validations.push(validation);
		$(document.getElementById(validation.control.elmID)).bind( validation.event, function(e){makeValidation(validation);} );	
		$(document.getElementById(validation.control.elmID)).bind( 'blur', function(e){ makeValidation(validation);} );
	}
	else
	{
		// document.forms[0].observe( 'submit', function(e){makeValidation(validation);} );
	}
}

function makeValidation( validation )
{
	var labelID = validation.control.label.id; 
	var lblErrStyle = validation.control.label.errStyle; 
	var errTxt = validation.text; 
	var ntfcID = validation.control.ntfcArea.ntfcAreaID; 
	var ntfcErrStyle = validation.control.ntfcArea.errStyle;
	
	if( eval(validation.funcCall)  ) 
	{
		if( eval(validation.varName).control.getErrorState() )
		{
			document.getElementById(labelID).className = 'empty';//.setAttribute('class', '');
			if(validation.onsubmit==true)
			{ document.getElementById(ntfcID).innerHTML = ''; }
			return true;
		}
	}
	else
	{
		document.getElementById(labelID).className = lblErrStyle; //.setAttribute('class', lblErrStyle);
		document.getElementById(ntfcID).className = ntfcErrStyle; //.setAttribute('class', ntfcErrStyle);
		document.getElementById(ntfcID).innerHTML = errTxt;
		return false;
	}
	
}

function makeHelpMessage(control, text, style)
{
	ntfcArea  = document.getElementById(control.ntfcArea.ntfcAreaID);
	if( control.getErrorState() == true)
	{
		ntfcArea.innerHTML = text;
		ntfcArea.className = style; //.setAttribute('class', style);
	}
	$(ntfcArea).bind( 'blur', function(e){ if( control.getErrorState() == true) {
											document.getElementById(control.ntfcArea.ntfcAreaID).innerHTML = text;
											document.getElementById(control.ntfcArea.ntfcAreaID).className = 'empty';//.setAttribute('class', '');
											}
											});
}

function regExpValidation( value, regExp)
{
	if( value!=undefined && value.match(regExp) )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function condValidation( value, cond )
{
	if( eval(cond) )
	{
		return true;
	}
	else
	{
		return false;
	}
}
