var checkObjects		= new Array(); 	// Array containing the objects to validate.
var errors				= ""; 			// Variable holding the error message.
var returnVal			= false; 		// General return value. The validated form will only be submitted if true.

// -----------------------------------------------------------------------------
// define - Call this function in the beginning of the page. I.e. onLoad.
//
// -----------------------------------------------------------------------------
function define(field_name, regexp_pattern, error_message, d){
	var p;
	var i;
	var x;
	if(!d) d=document;
	if((p=field_name.indexOf("?"))>0&&parent.frames.length){
    	d=parent.frames[field_name.substring(p+1)].document;
    	field_name=field_name.substring(0,p);
    }
	if(!(x=d[field_name])&&d.all) x=d.all[field_name];
	
  	for (i=0;!x&&i<d.forms.length;i++){
  		x=d.forms[i][field_name];
  	}
	for(i=0;!x&&d.layers&&i<d.layers.length;i++){
		x=define(field_name,regexp_pattern,error_message,d.layers[i].document);
		return x;		
	}
	
	// Create Object. The name will be "V_something" where something is the "field_name" parameter above.
	eval("V_"+field_name+" = new formResult(x,field_name,regexp_pattern,error_message);");
	checkObjects[eval(checkObjects.length)] = eval("V_"+field_name);

}



// -----------------------------------------------------------------------------
// formResult - Used internally to create the objects
// -----------------------------------------------------------------------------
function formResult(form,field_name,regexp_pattern,error_message){

	this.form           =   form;
	this.field_name     =   field_name;
	this.regexp_pattern =   regexp_pattern;
	this.error_message  =   error_message;
}


// -----------------------------------------------------------------------------
// regexp_validate - Main validation routine, called from the form
// -----------------------------------------------------------------------------
function regexp_validate(){

    if (checkObjects.length<=0) {
        validation_defs();
    }

	if(checkObjects.length>0){
		errorObject = "";
	
		for(i=0;i<checkObjects.length;i++){
			validateObject 			        =   new Object();
			validateObject.form 	        =   checkObjects[i].form;
			validateObject.field_name       =   checkObjects[i].field_name;
			validateObject.val 		        =   checkObjects[i].form.value;
			validateObject.len 		        =   checkObjects[i].form.value.length;
			validateObject.regexp_pattern 	=   checkObjects[i].regexp_pattern;
			validateObject.error_message 	=   checkObjects[i].error_message;

			
			//Debug alert line
            /*
			alert("validateObject.field_name: "+validateObject.field_name+"\n"+
                  "validateObject.val: "+validateObject.val+"\n"+
                  "validateObject.len: "+validateObject.len+"\n"+
                  "validateObject.regexp_pattern: "+validateObject.regexp_pattern+"\n"+
                  "validateObject.error_message: "+validateObject.error_message+"\n");
            */
			
            //Main validation bit, basically do a regexp match
            if (validateObject.val.match(validateObject.regexp_pattern)) {
                //string is ok
                
            }
            else {
                // It's not a match.
                errors+="\t"+validateObject.error_message+"\n";
            }

		}

	}
	// Used to set the state of the returnVal. If errors -> show error messages in chosen language
	if(errors){
		alert("Form Validation Errors:\n"+errors);
		errors = "";
		returnVal = false;
	} else {
		returnVal = true;
	}
}
