
function jscss(a, o, c1, c2) {
	switch (a) {
	  case "swap":
		o.className = !jscss("check", o, c1) ? o.className.replace(c2, c1) : o.className.replace(c1, c2);
		break;
	  case "add":
		if (!jscss("check", o, c1)) {
			o.className += o.className ? " " + c1 : c1;
		}
		break;
	  case "remove":
		var rep = o.className.match(" " + c1) ? " " + c1 : c1;
		o.className = o.className.replace(rep, "");
		break;
	  case "check":
		return new RegExp("\\b" + c1 + "\\b").test(o.className);
		break;
	}
}
function trim(str) {
	return str.replace(/^\s+|\s+$/g, "");
}
function ClientValidator(isPopup) {
	var instance = this;
	//validation summary
	this.vs = new Object();
	this.vs.popup = isPopup;
	this.vs.summary = "";
	this.highlighted = new Array();
	this.onValidationStart;
	this.onValidationEnd;
	this.onValidationSuccess;
	this.onValidationError;
	this.highlightClass;
	this.highlightGlobally;
	this.validationOk = true;
	//this.errorClass;
	//
	this.addHightlight = function (id) {
		instance.highlighted[instance.highlighted.length + 1] = id;
	};
	this.clearHighlights = function () {
		if (instance.highlightClass) {
			for (var i = 0; i < instance.highlighted.length; i++) {
				obj = document.getElementById(instance.highlighted[i]);
				if (obj) {
					jscss("remove", obj, instance.highlightClass);
				}
			}
			instance.highlighted = new Array();
		}
	};
	this.showHighlights = function () {
		if (instance.highlightClass) {
			for (var i = 0; i < instance.highlighted.length; i++) {
				obj = document.getElementById(instance.highlighted[i]);
				if (obj) {
					jscss("add", obj, instance.highlightClass);
				}
			}
		}
	};
	this.showValidationDiv = function (obj, display) {
		obj = document.getElementById(obj);
		if (display == "static") {
			obj.style.visibility = "visible";
		} else {
			if (display == "dynamic") {
				obj.style.display = "block";
			} else {
				if (display == "none") {
					obj.style.display = "none";
				}
			}
		}
		//console.log("showValidationDiv ok");
	};
	this.hideValidationDiv = function (obj, display) {
		if (display == "static") {
			document.getElementById(obj).style.visibility = "hidden";
		} else {
			if (display == "dynamic") {
				document.getElementById(obj).style.display = "none";
			} else {
				if (display == "none") {
					document.getElementById(obj).style.display = "none";
				}
			}
		}
	};
	this.highlightField = function (id, highlight) {
		if (instance.highlightGlobally || highlight) {
			instance.addHightlight(id);
		}
	};
	this.showValidationError = function (field, error, highlight, display) {
		if (!document.getElementById(field).disabled) {
			//console.log("error for:",field,error);
			instance.validationOk = false;
			instance.showValidationDiv(error, display);
			instance.addErrorToSummaryIfNecessary(error);
			instance.highlightField(field, highlight);
			//console.log("donewith",field);
		}
	};
	this.hideValidationError = function (field, error, highlight, display) {
		instance.hideValidationDiv(error, display);
	};
	this.showCompareValidationError = function (field1, field2, error, highlight, display) {
		instance.validationOk = false;
		instance.showValidationDiv(error, display);
		instance.addErrorToSummaryIfNecessary(error);
		instance.highlightField(field1, highlight);
		instance.highlightField(field2, highlight);
	};
	this.hideCompareValidationError = function (field1, field2, error, highlight, display) {
		instance.hideValidationDiv(error, display);
	};
	this.validateRequiredField = function (field, error, highlight, display, emptyText) {
		var obj = document.getElementById(field);
		//console.log("validateRequiredField begin ",field, obj);
		if (!obj) {
			return;
		}
		//console.log('validateRequiredField 1');
		if (obj.type == "text") {
			var value = trim(obj.value);
			obj.value = value;
		} else {
			var value = obj.value;
		}
		//console.log('validateRequiredField 2');
		var isEmpty = false;
		if (obj.options) {
			//console.log("obj.options: " + obj.options + ", obj.selectedIndex: " + obj.selectedIndex + ", obj.options[obj.selectedIndex]: " + obj.options[obj.selectedIndex]);
			isEmpty = (obj.selectedIndex == 0);
			/*
			if (obj.selectedIndex > -1) {
				value = obj.options[obj.selectedIndex].text;
				//console.log("value: " + value + ", empty: " + (trim(value) == trim(emptyText)));
				if (value == "null") {
					value = "";
				}
			}*/
		}
		isEmpty = isEmpty || (emptyText ? trim(value) == trim(emptyText) : false);
		//console.log("value: " + value + "; isEmpty: " + isEmpty);
		if (value == null || value == "" || isEmpty) {
			//console.log(field, error, highlight, display);
			instance.showValidationError(field, error, highlight, display);
			return false;
		} else {
			instance.hideValidationError(field, error, highlight, display);
			return true;
		}
	};
	this.validateRangeField_ = function (field, error, minValue, maxValue, highlight, display) {
		var obj = document.getElementById(field);
		if (!obj) {
			return;
		}
		if (obj.type == "text") {
			var value = trim(obj.value);
			obj.value = value;
		} else {
			var value = obj.value;
		}
		if (valueText == null || valueText == "") {
			instance.showValidationError(field, error, highlight, display);
			return false;
		}
		var value = parseInt(valueText);
		if (minValue != null && maxValue != null) {
			if (value > maxValue || value < minValue) {
				instance.showValidationError(field, error, highlight, display);
				return false;
			} else {
				instance.hideValidationError(field, error, highlight, display);
				return true;
			}
		} else {
			if (minValue == null && maxValue != null) {
				if (value > maxValue) {
					instance.showValidationError(field, error, highlight, display);
					return false;
				} else {
					instance.hideValidationError(field, error, highlight, display);
					return true;
				}
			} else {
				if (minValue != null && maxValue == null) {
					if (value < minValue) {
						instance.showValidationError(field, error, highlight, display);
						return false;
					} else {
						instance.hideValidationError(field, error, highlight, display);
						return true;
					}
				}
			}
		}
	};
	this.validateCompareFields = function (field1, field2, error, operator, highlight, display) {
		if (operator == "eq") {
			return instance.validateEquality(field1, field2, error, highlight, display);
		} else {
			if (operator == "not") {
				return instance.validateNotEquals(field1, field2, error, highlight, display);
			}
		}
	};
	this.validateEquality = function (field1, field2, error, highlight, display) {
		var obj1 = document.getElementById(field1);
		var obj2 = document.getElementById(field2);
		if (!obj1 || !obj2) {
			return;
		}
		//console.log('validateEquality 1');
		if (obj1.type == "text") {
			var value1 = trim(obj1.value);
			obj1.value = value1;
			var value2 = trim(obj2.value);
			obj2.value = value2;
		} else {
			var value1 = obj1.value;
			var value2 = obj2.value;
		}
		//console.log('validateEquality 1');
		if (value1 == value2) {
			instance.hideCompareValidationError(field1, field2, error, highlight, display);
			return true;
		} else {
			instance.showCompareValidationError(field1, field2, error, highlight, display);
			return false;
		}
	};
	this.validateNotEquals = function (field1, field2, error, highlight, display) {
		var obj1 = document.getElementById(field1);
		var obj2 = document.getElementById(field2);
		if (!obj1 || !obj2) {
			return;
		}
		//console.log('validateNotEquals 1');
		if (obj1.type == "text") {
			var value1 = trim(obj1.value);
			obj1.value = value1;
			var value2 = trim(obj2.value);
			obj2.value = value2;
		} else {
			var value1 = obj1.value;
			var value2 = obj2.value;
		}
		//console.log('validateNotEquals 2');
		if (value1 != value2) {
			instance.hideCompareValidationError(field1, field2, error, highlight, display);
			return true;
		} else {
			instance.showCompareValidationError(field1, field2, error, highlight, display);
			return false;
		}
	};
	this.validateRegExp = function (field, error, pattern, highlight, display) {
		var obj = document.getElementById(field);
		if (!obj) {
			return;
		}
		//console.log('text 1');
		if (obj.type == "text") {
			var value = trim(obj.value);
			obj.value = value;
		} else {
			var value = obj.value;
		}
		//console.log('text 2');
		if (value == null || value == "") {
			instance.hideValidationError(field, error, highlight, display);
			return true;
		}
		if (value.match(pattern) == null) {
			instance.showValidationError(field, error, highlight, display);
			return false;
		} else {
			instance.hideValidationError(field, error, highlight, display);
			return true;
		}
	};
	this.validateLengthExactly = function (field, error, exactValue, highlight, display) {
		var obj = document.getElementById(field);
		if (!obj) {
			return;
		}
		if (obj.type == "text") {
			var value = trim(obj.value);
			obj.value = value;
		} else {
			var value = obj.value;
		}
		if (value.length != exactValue) {
			instance.showValidationError(field, error, highlight, display);
			return false;
		} else {
			instance.hideValidationError(field, error, highlight, display);
			return true;
		}
	};
	this.validateWithCustomFunction = function (field, error, custFunction, highlight, display) {
		if (!custFunction(field)) {
			instance.showValidationError(field, error, highlight, display);
			return false;
		} else {
			instance.hideValidationError(field, error, highlight, display);
			return true;
		}
	};
	this.validateLengthInterval = function (field, error, min, max, highlight, display) {
		var obj = document.getElementById(field);
		if (obj.type == "text") {
			var value = trim(obj.value);
			obj.value = value;
		} else {
			var value = obj.value;
		}
		var len = value.length;
		if (min != null && max != null) {
			if (len > max || len < min) {
				instance.showValidationError(field, error, highlight, display);
				return false;
			} else {
				instance.hideValidationError(field, error, highlight, display);
				return true;
			}
		} else {
			if (min == null && max != null) {
				if (len > max) {
					instance.showValidationError(field, error, highlight, display);
					return false;
				} else {
					instance.hideValidationError(field, error, highlight, display);
					return true;
				}
			} else {
				if (min != null && max == null) {
					if (len < min) {
						instance.showValidationError(field, error, highlight, display);
						return false;
					} else {
						instance.hideValidationError(error, display);
						return true;
					}
				}
			}
		}
	};
	this.keyPressNumber = function (decimal) {
		var kc = window.event.keyCode;
		var t = window.event.srcElement.value;
		if (decimal) {
			if (t.indexOf(",") != -1) {
				if ((kc >= 48 && kc <= 57) == false) {
					window.event.keyCode = 0;
				}
			} else {
				if ((kc >= 48 && kc <= 57 || kc == 44) == false) {
					window.event.keyCode = 0;
				}
			}
		} else {
			if ((kc >= 48 && kc <= 57) == false) {
				window.event.keyCode = 0;
			}
		}
	};
	this.addErrorToSummaryIfNecessary = function (error, isValid) {
		if (isValid == true) {
			return;
		}
		var errorMsg = document.getElementById(error).innerHTML;
		if (instance.vs.popup == false) {
			if (document.getElementById("clientValidationSummary") != null) {
				document.getElementById("clientValidationSummary").innerHTML += errorMsg + "<br>";
			}
		} else {
			instance.vs.summary += "* " + errorMsg + "\n";
		}
	};
	this.clearValidationSummary = function () {
		if (instance.vs.popup == false) {
			if (document.getElementById("clientValidationSummary") != null) {
				document.getElementById("clientValidationSummary").innerHTML = "";
			}
		} else {
			instance.vs.summary = "";
		}
	};
	this.showPopupIfNecessary = function () {
		if (instance.vs.popup == true && instance.vs.summary != "") {
			window.alert(instance.vs.summary);
		}
	};
	this.beforeValidationEvents = function () {
		if (instance.onValidationStart) {
			res = eval("__aa = function() {" + instance.onValidationStart + " }; __aa();");
			if (res == false) {
				return;
			}
		}
	};
	this.beforeValidation = function () {
		instance.clearValidationSummary();
		instance.clearHighlights();
		instance.validationOk = true;
	};
	this.afterValidationEvents = function () {
		if (instance.onValidationEnd) {
			res = eval("__aa = function() {" + instance.onValidationEnd + " }; __aa();");
			if (res == false) {
				return;
			}
		}
		if (instance.validationOk) {
			if (instance.onValidationSuccess) {
				eval("__aa = function() {" + instance.onValidationSuccess + " }; __aa();");
			}
		} else {
			if (instance.onValidationError) {
				eval("__aa = function() {" + instance.onValidationError + " }; __aa();");
			}
		}
	};
	this.afterValidation = function () {
		instance.showHighlights();
		instance.showPopupIfNecessary();
	};
}

//clientValidator = new ClientValidator();

