﻿var validerAlert = new Class({
	initialize: function(id, formId, submitButton, alertXML, alertStyle, onSubmitForm){
		this.id = id;
		this.form = $(formId);
		this.submitButton = $(submitButton);
		this.alertXML = alertXML;
		this.alertStyle = alertStyle;
		this.onSubmitForm = onSubmitForm;
		this.waitInterval = null;
		if (this.form && this.submitButton) {
			this.build();
			this.initForm();
		}
	},

	build: function() {
		var thisObject = this;
		var bgDiv = new Element("div").setProperties({id: this.id}).setStyles({
			font: "10px/10px Arial",
			color: thisObject.alertStyle && thisObject.alertStyle.txtColor ? thisObject.alertStyle.txtColor : "#000",
			background: (thisObject.alertStyle && thisObject.alertStyle.bgColor ? thisObject.alertStyle.bgColor : "#FFFFFF")+" "+"url("+(thisObject.alertStyle && thisObject.alertStyle.bgImage ? thisObject.alertStyle.bgImage : "images/_alert_bar.gif")+") left top no-repeat",
			border: thisObject.alertStyle && thisObject.alertStyle.border ? thisObject.alertStyle.border : "1px solid #000000",
			width: thisObject.alertStyle && thisObject.alertStyle.width ? thisObject.alertStyle.width : "120px",
			position: "absolute",
			top: "0px",
			left: "0px",
			padding: "3px",
			visibility: "hidden",
			zIndex: "101",
			height: "auto"
		}).injectInside($$("body")[0]);
		var closeImg = new Element("img").setProperties({
			id: thisObject.id+"Close", 
			src: thisObject.alertStyle && thisObject.alertStyle.closeImage ? thisObject.alertStyle.closeImage : "images/_alert_close.gif"
		}).setStyles({
			styleFloat: "right",
			cssFloat: "right",
			cursor: "pointer"
		}).injectInside(bgDiv);
		closeImg.addEvent("click", function(){
			thisObject.hide();
		});
		var contDiv = new Element("p").setProperties({
			id: thisObject.id+"Cont"
		}).setStyles({
			padding: "5px 3px 3px 3px", 
			margin: "0",
			clear: "both"
		}).injectInside(bgDiv);
		if (window.ie) {
			var iframeDiv = new Element("iframe").setProperties({
				id: thisObject.id+"Iframe"
				}).setStyles({
				width: thisObject.alertStyle && thisObject.alertStyle.width ? thisObject.alertStyle.width : "120px",
				filter: "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)",
				position: "absolute",
				top: "0px",
				left: "0px",
				visibility: "hidden",
				zIndex: "100"
			}).injectInside($$("body")[0]);
		}
		this.submitButton.addEvent("click", function(event) {
			new Event(event).stop();
			if (thisObject.valid()) {
				if (thisObject.onSubmitForm) {
					thisObject.onSubmitForm();
				} else {
					thisObject.form.submit();
				}
			}
		});
	},
	
	initForm: function() {
		for (var i = 0; i < this.alertXML.length; i++) {
			var inputCtrl = $(this.alertXML[i].name[0].data);
			if (inputCtrl) {
				if (typeof(this.alertXML[i].restrict) != "undefined") {
					inputCtrl.restrict = this.alertXML[i].restrict[0].data.replace("{number}","0-9").replace("number","[0-9]");
					inputCtrl.addEvent("keypress", function(event){
						var e = new Event(event);
						var code = e.code;
						var co = ( code == null || code == 0 || code == 8 || code == 9 || code == 13 || code == 27 || code == 35|| code == 36 || code == 37 || code == 39 || code == 46);
						var key = e.key;
						var re = new RegExp(this.restrict);
						return key.test(re) || co;
					});
				}
				if (inputCtrl.value.trim() != "") {
					inputCtrl.init = inputCtrl.value.trim();
					inputCtrl.addEvent("focus", function(event){
						this.value = "";
					});
					inputCtrl.addEvent("blur", function(event){
						if (this.value.trim() == ""){
							this.value = this.init;
						}
					});
				}
			}
		}
	},
	
	show: function(elementId, warning) {
		var thisObject = this;
		$(this.id).setStyles({
			top: $(elementId).getPosition().y+$(elementId).getCoordinates().height+"px",
			left: $(elementId).getPosition().x+"px"
		});
		$(this.id+"Cont").setHTML(warning);
		if (window.ie) {
			$(this.id+"Iframe").setStyles({
				visibility: "visible",
				top: $(elementId).getPosition().y+$(elementId).getCoordinates().height+"px",
				left: $(elementId).getPosition().x+"px",
				width: $(this.id).getCoordinates().width,
				height: $(this.id).getCoordinates().height
			})
		}
		//
		new Fx.Style($(this.id), 'opacity', {
			duration: 500,
			onComplete: function(){
				clearInterval(thisObject.waitInterval);
				thisObject.waitInterval = setInterval(function() {
					clearInterval(thisObject.waitInterval);
					thisObject.hide();
				}, 5000);
			}
		}).start(0, 1);
		//
		$(elementId).focus();
		$(elementId).addEvents({
			"keyup": function(){
				clearInterval(thisObject.waitInterval);
				thisObject.hide();
			},
			"click": function(){
				clearInterval(thisObject.waitInterval);
				thisObject.hide();
			}
		});
	},
	
	hide: function() {
		clearInterval(this.waitInterval);
		$(this.id).setStyles({visibility: "hidden"});
		if (window.ie) {
			$(this.id+"Iframe").setStyles({visibility: "hidden"});
		}
	},
	
	valid: function() {
		var validArray = new Array();
		validArray.splice(0, validArray.length);
		for (var i = 0; i < this.alertXML.length; i++) {
			var inputCtrl = $(this.alertXML[i].name[0].data);
			if (inputCtrl) {
				if (inputCtrl.name.indexOf("[]") != -1) {
					for (var j = 0; j < this.form.length; j++) {
						if (this.form[j].name == inputCtrl.name) {
							validArray.push({
								control: this.form[j], 
								type: this.alertXML[i].type[0].data, 
								alert: this.alertXML[i].alert[0].data
							});
						}
					}
				} else {
					validArray.push({
						control: inputCtrl, 
						type: this.alertXML[i].type[0].data, 
						alert: this.alertXML[i].alert[0].data
					});
				}
			}
		}
		//
		for (i = 0; i < validArray.length; i++) {
			if (!this.check(validArray[i].control, validArray[i].type)) {
				this.show(validArray[i].control, validArray[i].alert);
				return false;
			}
		}
		return true;
		//
	},
	
	check: function(elementId, condition) {
		var formElement = $(elementId);
		if (condition.indexOf("EVAL") !== -1) {
			var evalCon = getQuote(condition, "[", "]");
			return eval(evalCon);
		}
		if (typeof(formElement.init) != "undefined") {
			return (formElement.value != formElement.init);
		}
		if (condition.indexOf("required") !== -1) {
			var minLength = Math.max(Number(getQuote(condition, "(", ")")), 1);
			return (formElement.value.trim().length >= minLength);
		}
		if (condition.indexOf("min") !== -1) {
			var minNumber = Number(getQuote(condition, "(", ")"));
			return (formElement.value >= minNumber);
		}
		if (condition.indexOf(">") !== -1) {
			if (condition.indexOf(">=") !== -1) {
				var otherCtrl = $(condition.replace(">=",""));
				return (formElement.value >= otherCtrl.value);
			} else {
				var otherCtrl = $(condition.replace(">",""));
				return (formElement.value > otherCtrl.value);
			}
		}
		if (condition.indexOf("<") !== -1) {
			if (condition.indexOf("<=") !== -1) {
				var otherCtrl = $(condition.replace("<=",""));
				return (formElement.value <= otherCtrl.value);
			} else {
				var otherCtrl = $(condition.replace(">",""));
				return (formElement.value < otherCtrl.value);
			}
		}
		if (condition.indexOf("=") !== -1 && condition.indexOf(">") == -1 && condition.indexOf("<") == -1) {
			var otherCtrl = $(condition.replace("=",""));
			return (formElement.value == otherCtrl.value);
		}
		if (condition.indexOf("max") !== -1) {
			var minNumber = Number(getQuote(condition, "(", ")"));
			return (formElement.value <= minNumber);
		}
		if (condition === "select") {
			return (formElement.selectedIndex !== 0);
		}
		if (condition === "email") {
			return isEmail(formElement.value);
		}
		if (condition === "phone") {
			return isPhone(formElement.value);
		}
		if (condition === "date") {
			return isDate(formElement.value);
		}
		//
		//
		//
		//
		function getQuote(str, start, end) {
			return str.substring(str.indexOf(start)+1, str.indexOf(end));
		}
		//
		function isEmail(s){
			var re = new RegExp("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]{2,4}$");
			return (s.search(re) != -1);
		}
		//
		function isPhone(s) {
			var re = new RegExp("^[ .0-9]{3,}$");
			return (s.search(re) != -1);
		}
		//
		function isDate(s) {
			var re = new RegExp("^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$");
			return (s.search(re) != -1);
		}
		//
		function isBlank(s) {
			return (s.trim() == "");
		}
		//
	}
});
//