/**
	cValidate 1.1 - klasa Java Script pozwalająca na proste sprawdzanie ("validacje")
	formularzy HTML po stronie użytkownika. 
	
	Autor:	Bartosz "BTM" Szczeciński
	WWW:	http://epicweb.pl
	E-mail:	btm@anfo.pl
	Wersja:	1.1
	Data:	10.07.2009
	
	Klasa pozwala na sprawdzanie czy:
	
	- pole jest wypełnione / zaznaczone (vSet)
	- pole jest nie wypełnione / nie zaznaczone (vNotSet)
	- wartość pola jest liczbą (może zawierać jeden znak . lub ,) (vNumeric)
	- wartość pola jest składniowo poprawnym adresem e-mail (vEmail)
	- wartość pola jest zgodna z podanym wyrażeniem regularnym (vRegex)
	
	By dodać validację do formularza, ustaw odpowiednią klasę (podane wyżej) dla jego elementów.
	Validacja typu vRegex dodatkowo oczekuje podania wyrażenia regularnego w parametrze "alt"
	
	Sprawdzanie formularza odbywa się poprzez wywołanie metody cValidate.validate(obj), jako 
	parametr przekazać należy obiekt formularza do sprawdzenia. W przypadku niepowodzenia metoda
	zwraca wartość false, w innym wypadku true.
	
	Wszystkie nie poprawnie wypełnione elementy w momencie sprawdzenia otrzymują dodatkową klasę
	CSS .vError Jeżeli istnieje element <label> o parametrze "for" zgodnym z "id" sprawdzanego
	elementu również zostanie on oznaczony klasą vError
 
/**
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


function cValidate () {
	
	this.validationType = { set : 1, email : 2, oneof : 3, numeric : 4 , notset : 5, regex : 6};
	
	this.regex = {		
		email 	: /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i, // regex for e-mail address
		numeric : /^(?:\d*[\.|\,]\d{1,9}|\d+|\-\d+|\-\d*[\.|\,]\d{1,9})$/ // numeric regex, only allow digits, minus sign , a dot or a comma (Polish decimal notation), and up to 9 digits it
	};
	
	this.validate = function(obj) {				
		var validationType = 0;
		var validReturn = 1;
		var errorCount = 0;		

		var labels = document.getElementsByTagName('label');
		for(var i = 0; i < labels.length ; i++ ) {
			if(labels[i].className.indexOf('vError') > -1) {
				labels[i].className = labels[i].className.replace(/vError/g, '');
			}
		}

		for(var i = 0 ; i < obj.elements.length ; i++) {
			validationType = this.needsVal(obj.elements[i]);
			if(validationType != false) {
				validReturn = true;
				if(obj.elements[i].className.indexOf('vError') > -1) obj.elements[i].className = obj.elements[i].className.replace(/vError/g, '');

				if(validationType		==	cValidate.validationType.set)		validReturn = this.validateSet(obj.elements[i]);
				else if(validationType	==	cValidate.validationType.notset)	validReturn = !this.validateSet(obj.elements[i]);
				else if(validationType	==	cValidate.validationType.email)		validReturn = this.validateSet(obj.elements[i]) && this.validateRegex(obj.elements[i].value, cValidate.regex.email);
				else if(validationType	==	cValidate.validationType.oneof)		validReturn = this.validateOneOf(obj.elements[i].name);
				else if(validationType	==	cValidate.validationType.numeric)	validReturn = this.validateSet(obj.elements[i]) && this.validateRegex(obj.elements[i].value, cValidate.regex.numeric);
				else if(validationType	==	cValidate.validationType.regex)		validReturn = this.validateSet(obj.elements[i]) && this.validateRegex(obj.elements[i].value, new RegExp(obj.elements[i].alt));
				
				if(validReturn == 0) {
					this.markErrors(obj.elements[i], errorCount);
					errorCount++;
				}
			}
		}
				
		if(errorCount > 0) {
			return false;
		}
		return true;
	};

	this.markErrors = function(obj, errorCount) {
		if(errorCount == 0) {
			obj.focus();			
		}
		obj.className += ' vError';
		var labels = document.getElementsByTagName('label');

		for(var i = 0; i < labels.length ; i++ ) {
			if(labels[i].htmlFor == obj.id) {
				labels[i].className += ' vError';
			}
		}
	};

	this.validateOneOf = function(objname) {
		var collection = document.getElementsByName(objname);
		for(var i = 0 ; i < collection.length ; i++) {
			if(collection[i].checked) return true;
		}
		return false;
	};

	this.validateSet = function(obj) {
		if(obj.type == 'checkbox' || obj.type == 'radio') {
			if(obj.checked) return true;
			return false;
		}
		else if(!obj.value.replace(/ /g, '')) {
			return false;
		}

		return true;
	};


	this.validateRegex = function(value, filter){			
		if (filter.test(value)) return 1;
		return 0;
	};

	this.needsVal = function(obj) {		
		if(obj.className.indexOf('vSet') > -1) 		return cValidate.validationType.set;
		if(obj.className.indexOf('vEmail') > -1) 	return cValidate.validationType.email;
		if(obj.className.indexOf('vOneOf') > -1) 	return cValidate.validationType.oneof;
		if(obj.className.indexOf('vNumeric') > -1)	return cValidate.validationType.numeric;
		if(obj.className.indexOf('vNotSet')	> -1)	return cValidate.validationType.notset;
		if(obj.className.indexOf('vRegex')	> -1)	return cValidate.validationType.regex;
		return false;
	};
}
var cValidate = new cValidate();