/*------------------------------------------------------------------------------\
|                 Form Validation Installation and Samples			|
|-------------------------------------------------------------------------------|
|										|
|   ***************************** INSTALLATION ******************************	|
|   1. Include validation.js in your HTML-file:					|
|  	<script src="../se/validation.js" language="JavaScript"></script>		|
| 										|
|   2. Insert the following code in your <HEAD>-section:			|
| 	<script language="JavaScript">						|
| 	var header = '<%=objLabels.strGetLabel(9070)%>'				|
| 										|
| 	function init(){							|
| 		formValidate('name','type','ErrorMessage',min,max);			|
| 	}									|
| 	</script>								|
| 										|
| 	header:		The message in the dialogbox with DTF label		|
| 	name:		The field's name in the form (Required)			|
|   	type:		(string, num, email) (Required)				|
|   	ErrorMessage:	The error message shown in the dialogbox (Required)	|
|   	min:		[minimum num. of characters,null] (Optional)		|
|   	max:		[maximum num. of characters,null] (Optional)		|
|   										|
|   3. Insert an "onLoad" in your <BODY>-tag:					|
|   	<body onLoad="init();">							|
|   										|
|   4. Insert an "onSubmit" inside your <FORM>-tag:				|
|   	<form name="..." action="..." onSubmit="validate();return returnVal;">	|
|   										|
|   *************************************************************************	|
|   										|
|   ******************************* SAMPLES *********************************	|
|   function init(){								|
|   	formValidate('arrival','string','<%=objLabels.strGetLabel(9026)%>');	|
|   	formValidate('children','num','<%=objLabels.strGetLabel(9008)%>');	|
|   	formValidate('phone','string','<%=objLabels.strGetLabel(9014)%>',8,16);	|
|   }										|
|   *************************************************************************	|
|   										|
\------------------------------------------------------------------------------*/

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.
var start			= "  -> ";
var additional		= "";

if (header == null) var header = "Der opstod f\370lgende fejl (The following error(s) occured):";


// -----------------------------------------------------------------------------
// formValidate - Call this function in the beginning of the page. I.e. onLoad.
//
// n = name of the input errormessage (Required)
// type= string, num, email (Required)
// min = the value must have at least [min] characters (Optional)
// max = the value must have maximum [max] characters (Optional)
// d = (Optional)
// -----------------------------------------------------------------------------
function formValidate(n,type,ErrorMsg,min,max,d){
	var p;
	var i;
	var x;
	if(!d) d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length){
    		d=parent.frames[n.substring(p+1)].document;
    		n=n.substring(0,p);
    	}
	if(!(x=d[n])&&d.all) x=d.all[n];
	
  	for (i=0;!x&&i<d.forms.length;i++){
  		x=d.forms[i][n];
  	}
	for(i=0;!x&&d.layers&&i<d.layers.length;i++){
		x=formValidate(n,type,ErrorMsg,min,max,d.layers[i].document);
		return x;		
	}
	
	// Create Object. The name will be "V_something" where something is the "n" parameter above.
	eval("V_"+n+" = new formResult(x,type,ErrorMsg,min,max);");
	checkObjects[eval(checkObjects.length)] = eval("V_"+n);
}

// -----------------------------------------------------------------------------
// formResult - Used internally to create the objects
// -----------------------------------------------------------------------------
function formResult(form,type,ErrorMsg,min,max){
	this.form = form;
	this.type = type;
	this.ErrorMsg = ErrorMsg;
	this.min  = min;
	this.max  = max;
}

// -----------------------------------------------------------------------------
// validate - Call this function onSubmit and return the "returnVal". (onSubmit="validate();return returnVal;")
// -----------------------------------------------------------------------------
function validate(){
	if(checkObjects.length>0){
		errorObject = "";
	
		for(i=0;i<checkObjects.length;i++){
			validateObject 		= new Object();
			validateObject.form 	= checkObjects[i].form;
			validateObject.ErrorMsg = checkObjects[i].ErrorMsg;
			validateObject.val 	= checkObjects[i].form.value;
			validateObject.len 	= checkObjects[i].form.value.length;
			validateObject.min 	= checkObjects[i].min;
			validateObject.max 	= checkObjects[i].max;
			validateObject.type 	= checkObjects[i].type;
			
			//Debug alert line
			//alert("validateObject: "+validateObject+"\nvalidateObject.val: "+validateObject.val+"\nvalidateObject.len: "+validateObject.len+"\nvalidateObject.min,validateObject.max: "+validateObject.min+","+validateObject.max+"\nvalidateObject.type: "+validateObject.type);
			
			// Checking input. If "min" and/or "max" is defined the input has to be within the specific range
			if(validateObject.type == "num" || validateObject.type == "string"){
				if((validateObject.type == "num" && validateObject.len <= 0) || (validateObject.type == "num" && isNaN(validateObject.val))){errors+=start+validateObject.ErrorMsg+"\n";
				} else if (validateObject.min && validateObject.max && (validateObject.len < validateObject.min || validateObject.len > validateObject.max)){errors+=start+validateObject.ErrorMsg+"\n";
				} else if (validateObject.min && !validateObject.max && (validateObject.len < validateObject.min)){errors+=start+validateObject.ErrorMsg+"\n";
				} else if (validateObject.max && !validateObject.min &&(validateObject.len > validateObject.max)){errors+=start+validateObject.ErrorMsg+"\n";
				} else if (!validateObject.min && !validateObject.max && validateObject.len <= 0){errors+=start+validateObject.ErrorMsg+"\n";
				}
			} else if(validateObject.type == "email"){
				// Checking existense of "@" and ".". The length of the input must be at least 5 characters. The "." must neither be preceding the "@" nor follow it.
				//if((validateObject.val.indexOf("@") == -1) || (validateObject.val.charAt(0) == ".") || (validateObject.val.charAt(0) == "@") ||(validateObject.len < 6) || (validateObject.val.indexOf(".") == -1) || (validateObject.val.charAt(validateObject.val.indexOf("@")+1) == ".") || (validateObject.val.charAt(validateObject.val.indexOf("@")-1) == ".")){errors+=start+validateObject.ErrorMsg+"\n";}
				/* AHB 13/10/2003 - Start */
				if(
					(validateObject.val.indexOf("@") == -1) ||
					(validateObject.val.charAt(0) == ".") ||
					(validateObject.val.charAt(0) == "@") ||
					(validateObject.len < 6) ||
					(validateObject.val.indexOf(".") == -1) ||
					(validateObject.val.indexOf(":") != -1) ||
					(validateObject.val.indexOf("?") != -1) ||
					(validateObject.val.indexOf("&") != -1) ||
					(validateObject.val.indexOf(",") != -1) ||
					(validateObject.val.indexOf("*") != -1) ||
					(validateObject.val.indexOf("\"") != -1) ||
					(validateObject.val.indexOf("<") != -1) ||
					(validateObject.val.indexOf(">") != -1) ||
					(validateObject.val.indexOf("%") != -1) ||
					(validateObject.val.indexOf("=") != -1) ||
					(validateObject.val.indexOf("´") != -1) ||
					(validateObject.val.indexOf("`") != -1) ||
					(validateObject.val.indexOf("£") != -1) ||
					(validateObject.val.indexOf("[") != -1) ||
					(validateObject.val.indexOf("]") != -1) ||
					(validateObject.val.indexOf("(") != -1) ||
					(validateObject.val.indexOf(")") != -1) ||
					(validateObject.val.indexOf("~") != -1) ||
					(validateObject.val.indexOf("¨") != -1) ||
					(validateObject.val.indexOf("!") != -1) ||
					(validateObject.val.indexOf("¤") != -1) ||
					(validateObject.val.indexOf("$") != -1) ||
					(validateObject.val.indexOf("§") != -1) ||
					(validateObject.val.indexOf("'") != -1) ||
					(validateObject.val.charAt(validateObject.val.indexOf("@")+1) == ".") ||
					(validateObject.val.charAt(validateObject.val.indexOf("@")-1) == ".")
				){
					errors+=start+validateObject.ErrorMsg+"\n";
				}
				/* AHB 13/10/2003 - Slut */
			}
		else if(validateObject.type == "membersub"){
			if(
					(validateObject.val.indexOf("&") != -1) ||
					(validateObject.val.indexOf("?") != -1) ||
					(validateObject.len <= 0)
				){
					errors+=start+validateObject.ErrorMsg+"\n";
				}
			};
		}
	}
	// Used to set the state of the returnVal. If errors -> show error messages
	if(errors || additional){
		alert(header.concat("\n"+errors+additional));
		errors = "";
		additional = "";
		returnVal = false;
	} else {
		returnVal = true;
	}
}

