function GSGform () {
	this.errorMessage = new Array();
	this.template;
	this.errorClass;
	this.confirmation = true;
	this.checktype;
}

GSGform.prototype.setErrorClass = function (klass){
	this.errorClass = klass;
	this.hideErrors();
}

GSGform.prototype.setTemplate = function (temp){
	this.template = temp;
}

GSGform.prototype.hideErrors = function(){
	var errors = "div."+this.errorClass;
	$$(errors).each(
		function (error) {
			Element.remove(error.previousSibling.previousSibling);
			Element.remove(error.previousSibling);
			Element.remove(error);

		}
	);
};

GSGform.prototype.createError = function (klass,message) {
	var templ = this.template.replace("KLASS",klass).replace("MESSAGE",message);
	return templ;
}

GSGform.prototype.setError = function (type,message){
	this.errorMessage[type] = message;
}

GSGform.prototype.getError  = function (type){
	return this.errorMessage[type];
}

GSGform.prototype.formcheck = function(){

	if (arguments.length == 2) {
		element = arguments[0];
		type = arguments[1];
		insertelement = arguments[0];
	}

	if (arguments.length == 3) {
		element = arguments[0];
		type = arguments[1];
		insertelement = arguments[2];
	}

	var more = null;
	var typess;
	var checkt = Array();
	var nums = Array();
	var typs = Array();
	var num1,num2;

	if (type.indexOf(",") != -1){
		typess = type.split(",");
		var pocettypov = typess.length;
		for (var i = 0; i < pocettypov; i++){
			checkt[i] = typess[i];
		}
		if (pocettypov >= 1){
			for (var i = 0; i < pocettypov; i++){
				if (checkt[i].indexOf("-") != -1){
					typess = checkt[i].split("-");
					typs[i] = typess[0];
					if  (typess[1].indexOf("|") != -1){
					    var numbers = typess[1].split("|");
						nums[i] = typess[1];
					}
					else {
						nums[i] = typess[1];
					}
				}
				else {
					typs[i] = checkt[i];
					nums[i] = null;

				}
			}
		}
	}else {
		typs[0] = type;
		pocettypov = 1;
		if (typs[0].indexOf("-") != -1){
			typess = typs[0].split("-");
			typs[0] = typess[0];
			nums[0] = typess[1];
		}
	}

	for (var i = 0; i < pocettypov; i++){
		if (this.confirmation){
			switch (typs[i]) {
				case "email":
					var value = $(element).value;
					emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/;
					if (!emailRegExp.test(value)){
						this.showError(insertelement,this.createError(this.errorClass,this.getError(typs[i])));
					}
				break;
				case "select":
					var value = $(element).value;
					var val = nums[i];
					if (val == value){
						this.showError(insertelement,this.createError(this.errorClass,this.getError(typs[i])));
					}
				break;
				case "checkbox":
					var value = $(element).checked;
					if (!value){
						this.showError(insertelement,this.createError(this.errorClass,this.getError(typs[i])));
					}
				break;
				case "required":
					var value = $(element).value;
					if (value.empty()){
						this.showError(insertelement,this.createError(this.errorClass,this.getError(typs[i])));
					}
				break;
				case "message":
					var value = $(element).value;
					this.showError(insertelement,this.createError(this.errorClass,this.getError(typs[i])));
				break;
				case "minmax":
					var value = $(element).value;
					var minmax = nums[i].split("|");
					var minn = minmax[0];
					var maxx = minmax[1];
					if (value.length < minn || value.length > maxx){
					    var error = this.getError(typs[i]).replace("XXX",minn + "-"+maxx);
						this.showError(insertelement,this.createError(this.errorClass,error));
					}
				break;
				case "equal":
					var value = $(element).value;
					var equals = nums[i].split("|");
					var elem1 = equals[0];
					var elem2 = equals[1];
					var elem1_value = $(equals[0]).value;
					var elem2_value = $(equals[1]).value;
					if (elem1_value != elem2_value){
						this.showError(insertelement,this.createError(this.errorClass,this.getError(typs[i])));
					}
				break;
				case "max":
					var maxx = nums[i];
					var value = $(element).value;
					if (value.length > maxx){
					    var error = this.getError(typs[i]).replace("XXX",maxx);
						this.showError(insertelement,this.createError(this.errorClass,error));
					}
				break;
				case "min":
					var minn = nums[i];
					var value = $(element).value;
					if (value.length < minn){
					var error = this.getError(typs[i]).replace("XXX",minn);
						this.showError(insertelement,this.createError(this.errorClass,error));
					}
				break;
				case "postalcode":
					var value = $(element).value;
					postalcodeRegExp = /^\d{5}$/;
					if (!postalcodeRegExp.test(value)){
						this.showError(insertelement,this.createError(this.errorClass,this.getError(typs[i])));
					}
				break;
				case "number":
					var value = $(element).value;
					numberRegExp = /^[0-9- ]+$/;
					if (!numberRegExp.test(value)){
						this.showError(insertelement,this.createError(this.errorClass,this.getError(typs[i])));
					}
				break;
				case "unique_user":
					var value = $(element).value;
					var url = '/modules/ajax/check_unique_username.php';
					new Ajax.Request(url,{parameters: {login: value},method:'post',
                    onComplete: function(transport){
	       					var response = transport.responseText;
	       					if (response == 'false'){
	       					   alert("Prihlasovacie meno uz existuje!");
						    }
       					}
       				  }
       				);
				break;
			}
		}
	}
};

GSGform.prototype.showError =  function(insertelement,message){
	//$(element).setStyle("border:red 1px solid;");
	$(insertelement).focus();
	new Insertion.After(insertelement,message);
	this.confirmation = false;
}

GSGform.prototype.ok =  function(){
	return this.confirmation;
}