/*
 *
 * Filename		: CheckForm.inc.js
 * Project		: LCube - JavaScript Librarys
 * Created 		: 11.08.2009
 * Copyright 	: Lars Laehn, Lcube - Webhosting & Webdesign
 * E-Mail		: info@lcube.de
 *
 */

function LC_CheckForm() {
	this.Fields = new Array();
	this.FieldsCount = 0;
	this.ErrorText = "";
	this.Error = false;

	this.AddField = function (fieldname, conditiontype, condition, errortype, ifthen, ifelse, stopform) {
		this.Fields[this.FieldsCount] = new Object;
		this.Fields[this.FieldsCount].Name = fieldname;
		this.Fields[this.FieldsCount].ConditionType = conditiontype;
		this.Fields[this.FieldsCount].Condition = condition;
		this.Fields[this.FieldsCount].ErrorType = errortype;
		this.Fields[this.FieldsCount].IfThen = ifthen;
		this.Fields[this.FieldsCount].IfElse = ifelse;
		this.Fields[this.FieldsCount].StopForm = stopform;
		this.FieldsCount++;
	}

	this.ReplaceVars = function (Text, obj) {
		var tmp="";
		tmp=Text.split("[FIELDNAME]").join(obj.Name);
		return tmp;
	}

	this.CheckError = function (error, obj, FormFieldObj) {
		switch (obj.ErrorType) {
			case "errortext" :
				if (error) {
					this.ErrorText = this.ErrorText + this.ReplaceVars(obj.IfThen, obj);
					if (obj.StopForm) this.Error = true;
				} else {
					this.ErrorText = this.ErrorText + this.ReplaceVars(obj.IfElse, obj);
				}
				break;
			case "alert" :
				if (error) {
					if (obj.IfThen.length>0) alert(this.ReplaceVars(obj.IfThen, obj));
					if (obj.StopForm) this.Error = true;
				} else {
					if (obj.IfElse.length>0) alert(this.ReplaceVars(obj.IfElse, obj));
				}
				break;
			case "class" :
				if (error) {
					FormFieldObj.className=this.ReplaceVars(obj.IfThen, obj);
					if (obj.StopForm) this.Error = true;
				} else {
					FormFieldObj.className=this.ReplaceVars(obj.IfElse, obj);
				}
				break;
			default :
				if (obj.StopForm) this.Error = true;
				break;
		}
	}

	this.CheckCondition = function (FieldValue, obj, FormFieldObj) {
		var tmp = "";
		switch (obj.ConditionType) {
			case "regexp" :
				eval("this.CheckError((FieldValue.match(" + obj.Condition + ") != FieldValue), obj, FormFieldObj);");
				break;
			case "!regexp" :
				eval("this.CheckError((FieldValue.match(" + obj.Condition + ") == FieldValue), obj, FormFieldObj);");
				break;
			case "length_greater" :
				this.CheckError((FieldValue.length > obj.Condition), obj, FormFieldObj);
				break;
			case "length_lower" :
				this.CheckError((FieldValue.length < obj.Condition), obj, FormFieldObj);
				break;
			case "length_equal" :
				this.CheckError((FieldValue.length == obj.Condition), obj, FormFieldObj);
				break;
			case "boolean" :
				this.CheckError((FieldValue==obj.Condition), obj, FormFieldObj);
				break;
			case "function" :
				eval("tmp = "+obj.Condition+";");
				this.CheckError(tmp, obj);
				break;
			case "predefined" :
				switch (obj.Condition) {
					case "email" :
						this.CheckError((FieldValue.match(/[\.a-z0-9_-]+@[a-z0-9-öäüß\.]{2,}\.[a-z]{2,4}$/i) != FieldValue), obj, FormFieldObj);
						break;
					case "alphanumeric" :
						this.CheckError((FieldValue.match(/[a-z0-9]*/i) != FieldValue), obj, FormFieldObj);
						break;
					case "umlautalphanumeric" :
						this.CheckError((FieldValue.match(/[a-zöäüß0-9]*/i) != FieldValue), obj, FormFieldObj);
						break;
					case "name" :
						this.CheckError((FieldValue.match(/.*/) != FieldValue), obj, FormFieldObj);
						break;
					case "date" :
						this.CheckError((FieldValue.match(/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/) != FieldValue), obj, FormFieldObj);
						break;
					case "time" :
						this.CheckError((FieldValue.match(/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/) != FieldValue), obj, FormFieldObj);
						break;
					case "shorttime" :
						this.CheckError((FieldValue.match(/^[0-9]{2}:[0-9]{2}$/) != FieldValue), obj, FormFieldObj);
						break;
				}
				break;
		}
	}

	this.CheckField = function (obj, FormFieldObj) {
		//alert("Type:" + FormFieldObj.type);
		switch (FormFieldObj.type) {
			case "checkbox" :
				this.CheckCondition(FormFieldObj.checked, obj, FormFieldObj);
				break;
			case "checkboxvalue" :
				this.CheckCondition((FormFieldObj.checked ? FormFieldObj.value : ""), obj, FormFieldObj);
				break;
			case "select" :
				this.CheckCondition(FormFieldObj.selectedIndex, obj, FormFieldObj);
				break;
			case "selectvalue" :
				this.CheckCondition(FormFieldObj.options[FormFieldObj.selectedIndex].value, obj, FormFieldObj);
				break;
			case "text" :
			case "textarea" :
				this.CheckCondition(FormFieldObj.value, obj, FormFieldObj);
				break;
		}
	}

	this.CheckForm = function (FormObj) {
		this.ErrorText = "";
		this.Error = false;

		//alert("elements:" + FormObj.elements.length);
		for (i=0; i < this.FieldsCount; i++) {
			this.CheckField(this.Fields[i], FormObj.elements[this.Fields[i].Name]);
		}
		if (this.ErrorText.length > 0) {
			alert(this.ErrorText);
		}
		return !this.Error;
	}
}
