/* VALIDACIJA FORM - CHECK */
/*
* function validate(form)
* {
* var check = new Checker(form); -- nov validator
* check.addField(ime_polja, tip_preverjanja, opis);
* -ime_polja -- ime od polja v formi
* -tip_preverjanje
*  : empty - ce je vpis, preverja se v vsakem primeru
*  : digit - ce so v vpisu samo številke
*  : chars - če so v vpisu samo črke
*  : email - pravilnost email vnosa
*  : <st - preverjanje velikosti vnosa npr <1000 ali >0
*  : st - preverjanje stevila znakov v vnosu npr 8
* -opis -- alert, ki ga dobi uporabnik ob napaki
* check.doubleField(ime_polja1, ime_polja2, opis)
* -ime_polja1 -- ime prvega polja
* -ime_polja2 -- ime drugega polja
* -opis -- alert, ki ga dobi uporabnik če sta polja rezlična
* return check.go(); -- preveri vnešene parametre
* }
* <form ... onsubmit="return validate(this);">
*/
function Checker(form)
{
	// posamezen field check
	this.v_form = form;
	this.v_field = new Array();
	this.v_check = new Array();
	this.v_description = new Array();
	this.count = 0;

	// primerjanje vnosa v dva fielda
	this.d_field = new Array();
	this.d_description = new Array();
	this.d_count = 0;

	this.addField = addField;
	this.doubleField = doubleField;
	this.inValidCharset = inValidCharset;
	this.allDigits = allDigits;
	this.allChars = allChars;
	this.isEmail = isEmail;
	this.checkEq = checkEq;
	this.alertUser = alertUser;
	this.go = go;
}

function addField(field, check, description)
{
	this.v_field[this.count] = field;
	if (check)
	{
		this.v_check[this.count] = check;
		this.v_description[this.count] = description;
	}

	this.count++;
}

function doubleField(field1, field2, description)
{
	if (!field1 || !field2)
		alert("napačna inicialzacija... doubleField");

	this.d_field[this.d_count] = field1;
	this.d_field[this.d_count+1] = field2;
	this.d_description[this.d_count] = description;
	this.d_count += 2;
}

function alertUser(default_alert, i, field)
{
	field.focus();
  if (this.v_description[i])
    alert(this.v_description[i]);
  else
    alert(default_alert);
}

function go()
{
	for (i=0; this.v_field[i]; i++)
	{
    field = document.getElementById(this.v_field[i]);
    if (!field)
    {
      // preverjanje, če je field prazen
      if (!eval('this.v_form.'+this.v_field[i]+'.value'))
  		{
  		 this.alertUser(this.v_field[i] + '?', i, eval('this.v_form.'+this.v_field[i]));
  		 return false;
  		}
      field = eval('this.v_form.'+this.v_field[i]);
      value = eval('this.v_form.'+this.v_field[i]+'.value');
    }
    else
      value = field.value;
    
		check = this.v_check[i];
    
		if (!check || (check == 'empty'))
    {
      // če ni potrebno preverjanje vsebine potem je to to za ta krog :)
      continue;
    }
		else if (check == 'digits')
		{
			if (!this.allDigits(value))
			{
				this.alertUser('samo številke v ' + this.v_field[i] + '!', i, field);
				return false;
			}
		}
		else if (check == 'chars')
		{
			if (!this.allChars(value))
			{
				this.alertUser('samo črke v ' + this.v_field[i] + '!', i, field);
				return false;
			}
		}
		else if (check == 'email')
		{
			if (!this.isEmail(value))
			{
				this.alertUser(this.v_field[i] + ' je napačen email naslov!', i, field);
				return false;
			}
		}
		else if ((check[0] == '<') || (check[0] == '>'))
		{
		  if (!this.checkEq(value, check))
      {
				this.alertUser(this.v_field[i] + ' ni vstrezno!', i, field);
				return false;
      }
    }
    else if (allDigits(check))
    {
    	// preverjanje dolzine vnosa
    	if (value.length > check)
    	{
				this.alertUser(this.v_field[i] + ' ni prave dolžine!', i, field);
				return false;
			}
		}
	}

	// double field check
	for (j=0; this.d_field[j]; j += 2)
	{
		value1 = eval('this.v_form.'+this.d_field[j]+'.value');
    field = eval('this.v_form.'+this.d_field[j]);
    value2 = eval('this.v_form.'+this.d_field[j+1]+'.value');

    if (value1 != value2)
    {
    	field.focus();
    	alert(this.d_description[j]);
    	return false;
		}
	}

	return true;
}

function checkEq(str, check)
{
  eq = check[0];
  digit = '';
  for (var i=1; i<check.length; i++)
    digit = digit + check[i];

  if (!this.allDigits(str) || !this.allDigits(digit))
    return false;

  return eval(str + ' ' + eq + ' ' + digit);
}

function isEmail(email)
{
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

  return re.test(email);
}

function inValidCharset(str, charset)
{
	// Note: doesn't use regular expressions to avoid early Mac browser bugs
	for (var i=0;i<str.length;i++)
	{
		if (charset.indexOf(str.substr(i,1))<0)
			return false;
	}

	return true;
}

function allDigits(str)
{
	return this.inValidCharset(str,"0123456789");
}

function allChars(str)
{
	return this.inValidCharset(str,"qwertzuiopšđžasdfghjklčćyxcvbnmQWERTZUIOPŠĐŽASDFGHJKLČĆYXCVBNM");
}
/**VALIDACIJA FORM - CHECK**/
