function formValidator(){
	// set up array to hold error messages
	this.errorList = new Array;
	this.errorArray = new Array;
	//obs³uga b³êdów

	this.raiseError = raiseError;
	this.addErrorMsg = addErrorMsg;
	this.delErrorMsg = delErrorMsg;
	this.addErrorFields = addErrorFields;
	this.markField = markField;
	this.unmarkField = unmarkField;
	this.numErrors = numErrors;
	this.displayErrors = displayErrors;
	//this.displayFields = displayFields;
	this.checkField = checkField;
	this.checkForm = checkForm;
	this.isCorrect = isCorrect;
	
	// set up object methods
	this.isPesel = isPesel;
	this.isNip = isNip;
	this.isRegon = isRegon;
	this.isRegon14 = isRegon14;
	this.isEmpty = isEmpty;
	this.isNull = isNull;
	this.isNumber = isNumber;
	this.isAlphabetic = isAlphabetic;
	this.isAlphaNumeric = isAlphaNumeric;
	this.isWithinRange = isWithinRange;
	this.isChecked = isChecked;
	this.isEmailAddr = isEmailAddr;
	this.isCompAddrNr = isCompAddrNr;

}

function displayErrors(){
	errors = '';
	for (i=0; i<this.errorArray.length; i++){
	  	if (this.errorArray[i][1] != null){
		    errors += this.errorArray[i][1]+'\n';
		}
	}
	if (errors != ''){
	  	alert(errors);
	} else {
	  	alert('jest null');
	}
}

function displayFields(){
  	fields = null;
  	for (i=0; i<this.errorArray.length;i++){
	    fields += this.errorArray[i][0]+'\n';
	}
	alert(fields);
}
// add an error to error list
function raiseError(msg){
	this.errorList[this.errorList.length] = msg;
}

function addErrorFields(field){
  	idx = this.errorArray.length;
  	this.errorArray[idx] = new Array(2);
  	this.errorArray[idx][0] = field;
  	this.errorArray[idx][1] = null;  
}

function addErrorMsg(field, msg){
	for (i=0; i < this.errorArray.length; i++){
	    if (this.errorArray[i][0] == field){
		  	idx = i;
		  	break;
		}
	}
	this.errorArray[idx][1] = msg;
}

function isCorrect(field){
  	for (i=0; i < this.errorArray.length; i++){
	    if (this.errorArray[i][0] == field){
		  	ind = i;
		  	break;
		}
	}
	if (this.errorArray[i][1] != null){
	  	return true;
	} else {
	  	return false;
	}
}

function delErrorMsg(field, msg){
  	for (i=0; i < this.errorArray.length; i++){
	    if (this.errorArray[i][0] == field){
		  	ind = i;
		  	break;
		}
	}
	if (this.errorArray[i][1] == msg) {
	  	this.errorArray[i][1] = null;
	}
}

function markField(field){
  	var e=document.getElementsByName(field);
  	for (i=0;i<e.length;i++){
  		e[i].style.backgroundColor = '#ffc0c0';
	};  		
}

function unmarkField(field){
  	var e=document.getElementsByName(field);
  	for (i=0;i<e.length;i++){
  		e[i].style.backgroundColor = 'white';
	};  		
}
// return number of errors in error array
function numErrors(){
	suma = 0;
	for (i=0; i< this.errorArray.length; i++){
	  	if (this.errorArray[i][1] != null){
		    suma++;
		}
	}
	return suma;
}

function checkField(fldName, checkFunction, errMsg){
	if (!eval(checkFunction)){
		this.addErrorMsg(fldName, errMsg);
	    this.markField(fldName);
	  	//this.displayErrors();	    
	} else {
	  	this.delErrorMsg(fldName, errMsg);
	  	this.unmarkField(fldName);
	  	//this.displayErrors();
	}
}

function checkForm(){
	if (this.numErrors() > 0){
		this.displayErrors();
		return false;
	} else {
	  	return true;
	}
}

function isPesel(number, sex){
	wagi = '13791379131';
	suma = 0;
	if ((number.length != 11) || (!isNumber(number))){
	  	return false;
	}
	if (!isNull(sex)){
	  	if ((number.charAt(9)%2 && sex=='k') || (!number.charAt(9)%2 && sex=='m')){
			return false;
		}
	} 
	for (i=0;i<wagi.length;i++){
		suma += parseInt(wagi.charAt(i)) * parseInt(number.charAt(i));
	}
	if (suma%10 != 0){
		return false;
	} else {
		return true;
	}
}

function isNip(number){
  	wagi = '657234567';
  	suma = 0;
  	if ((number.length != 10) || (!isNumber(number))){
	    return false;
	} 
	for (i=0;i<wagi.length;i++){
		suma += parseInt(wagi.charAt(i)) * parseInt(number.charAt(i));
	}
	suma = (suma%11).toString();
	if (suma.charAt(suma.length-1)== number.charAt(number.length-1)){
		return true;	
	} else {
		return false;
	}  	
}

function isRegon(number){
  	wagi = '89234567';
  	suma = 0;
  	if ((number.length != 9 && number.length != 14) || (!isNumber(number))){
	    return false;
	} 
	for (i=0;i<wagi.length;i++){
		suma += parseInt(wagi.charAt(i)) * parseInt(number.charAt(i));
	}
	suma = (suma%11).toString();
	if (suma.charAt(suma.length-1)== number.charAt(number.length-1)){
	  	if (number.length == 14){
		    if (!isRegon14(number)){
			  	return false;
			}
		}
		return true;	
	} else {
		return false;
	}  	
}

function isRegon14(number){
  	wagi = '2485097361248';
  	suma = 0;
  	for (i=0;i<wagi.length;i++){
		suma += parseInt(wagi.charAt(i)) * parseInt(number.charAt(i));
	}
	suma = (suma%11).toString();
	if (suma.charAt(suma.length-1)== number.charAt(number.length-1)){
		return true;	
	} else {
		return false;
	}  	
}

function isEmpty(val){
	if (val.match(/^s+$/) || val == ""){
		return true;
	} else {
		return false;
	}
}

function isFilled(val){
	if (val.match(/^s+$/) || val == ""){
		return false;
	} else {
		return true;
	}
}

function isNull(val){
  	return(val==null);
}

function isNumber(val){
 	if (isNaN(val)) {
	   	return false;
	} else {
		return true;
	}
}

function isAlphabetic(val){
	if (val.match(/^[a-z±æê³ñó¶¿¼A-Z¡ÆÊ£ÑÓ¦¯¬\s\.,:;_-]+$/)){
		return true;
	} else {
		return false;
	} 
}

function isCompAddrNr(val){
	if (val.match(/^[a-z±æê³ñó¶¿¼A-Z¡ÆÊ£ÑÓ¦¯¬\0-9s\.\/,:;_-]+$/)){
		return true;
	} else {
		return false;
	} 
}

function isDate(val){
	if (val.match(/^(19[0-9][0-9]|200[0-6])\-(0[0-9]|1[0-2])\-(0[0-9]|[1-2][0-9]|3[0-1])$/)){
		return true;
	} else {
		return false;
	} 
}

function isZip(val){
	if (val.match(/^[0-9][0-9]\-[0-9][0-9][0-9]$/)){
		return true;
	} else {
		return false;
	} 
}

function isPhone(val){
	if (val.match(/^0[0-9]{9}$/)){
		return true;
	} else {
		return false;
	} 
}

function isAlphaNumeric(val){
	if (val.match(/^[a-z±æê³ñó¶¿¼A-Z¡ÆÊ£ÑÓ¦¯¬0-9\s\.,:;_-]+$/)){
		return true;
	} else {
		return false;
	} 
}

function isWithinRange(val, min, max){
	if (val >= min && val <= max){
		return true;
	} else {
		return false;
	} 
}

function isChecked(obj){
	if (obj.checked){
		return true;
	} else {
		return false;
	} 
}

function isEmailAddr(val){
	if (val.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/)){
		return true;
	} else {
		return false;
	} 
}


