//--- Sygnatura pliku ---------------------------------------------------------------
// $Id: form_utils.js,v 1.4 2006-12-11 14:54:14 pch Exp $
// Copyright  : Internet Designers S.A.
// Projekt    : Content Managment - webUnit
// Program    : form_utils
// Autor      : pzu, 
// Utworzenie : 2003.12.02
// Modyfikacje: 2004.03.26; pch, pablos
// Opis       : funkcje pomocnicze podczas wypełniania formularzy
//--- Sygnatura pliku ---------------------------------------------------------------



	//Dobry, choc moze nie wpuscic dobrego adresu 
	// - RFC2822 dopuszcza w adresach jeszcze znaki !#$%&'*+-/=?^_`~{}| 
	// i kilka nietypowych kombinacji
var	validMail=/^[\w-]+(\.[\w-]+)*\@([\w-]+\.)+[\w]{2,6}$/;
	//Liczba calkowita, pierwsza cyfra != 0
var validInt=/^[1-9]+[\d]*$/;
	//Kod pocztowy w formacie dd-ddd
var validPostCode=/^\d{2}\-\d{3}$/;
	//Kod pocztowy - czesc pierwsza


//function writeIntegerOnly(id) - funkcja uzywana jako zdarzenie pozwalająca wprowadzac tylko znaki numeryczne
function writeIntegerOnly(id, leng)
{
	if ( ((event.keyCode < 47) || ((event.keyCode > 58) && (event.keyCode < 96)) || (event.keyCode > 106)) || (id.value.length > leng))
	{
		var tmp = "";
		for(i = 0;i < id.value.length-1; i++)
			if((id.value.charCodeAt(i) > 47) && (id.value.charCodeAt(i) < 58))
				tmp += id.value.substr(i,1);
		id.value = tmp;
	}
}

function writePostCodeOnly(id)
{
	if( ((event.keyCode < 47) || ((event.keyCode > 58) && (event.keyCode < 96)) || (event.keyCode > 106)) || (id.value.length > 6))
	{
		var tmp = "";
		for(i = 0;i < id.value.length-1; i++)
		if(((id.value.charCodeAt(i) > 47) && (id.value.charCodeAt(i) < 58)) )
		{
			tmp += id.value.substr(i,1);
			if((i==1) && (tmp.charCodeAt(2) != 189)) tmp +="-";
		}

		 id.value = tmp;
	}
	if (id.value.length ==2) id.value += "-";
}
	
	//Lepsza wersja trima, nie biega literka po literce. Dodatkowo wycina wszelkie biale znaki, a nie tylko spacje.
	function LTrim(value) {
		var re = /\s*((\S+\s*)*)/;
		return value.replace(re, "$1");
	}
	
	function RTrim(value) {
		var re = /((\s*\S+)*)\s*/;
		return value.replace(re, "$1");
	}
	
	function Trim(value) {
		return LTrim(RTrim(value));
	}
	
	//sprawdza, czy element jest wypelniony
	function CheckField(element,msg)
	{
		var str = element.value;
		if (str  == '')
		{
      alert(msg);
      element.focus();
      return false;
    }
    else
		return true;
		
	}
	function CheckEMail(element,msg)
	{
		var email = element.value;
		if ((Trim(email) == '')
			|| (email.indexOf('@') > email.lastIndexOf('.'))
			|| (email.indexOf('@') == -1)
			|| (email.length < 4)
			|| (email.indexOf('@') != email.lastIndexOf('@'))
			|| (email.indexOf(' ') != -1)
			|| (email.lastIndexOf('.') == (email.length -1))
			)
			{
		      	 alert(msg);
		      	 element.focus();
		      	 return false;
	    		}
	    else
		return true;
	}
	function Check(element,msg)
	{
		var email = element.value;
		if (!element.checked)
		{
      alert(msg);
//      element.focus();
      return false;
    }
    else
	return true;
}
	function CheckInput(element,msg)
	{
		if ((element.value == ''))
		{
      alert(msg);
	if (element.type != "hidden")  element.focus();
      return false;
    }
    else
	return true;
}
	function CheckCode(element,msg)
	{
		var tmp = element.value;
		if ((element.value == '') || (tmp.charCodeAt(2) != 45) || (tmp.length != 6)) 
		{
  		    alert(msg);
		if (element.type != "hidden")  element.focus();
	      return false;
	    }
	    else
	return true;
	}

function CheckGroup(form,prefix,msg)
 { 
	var elements = form.elements;
      var i,status=false;
      for (i=0; i<elements.length; i++)
      {
		if ((elements[i].name.indexOf(prefix) > -1) && (elements[i].checked))
        {
		status = true;
          break;
        }
      }
      if (!status)
      {
	alert(msg);
	return false;
      }
 }

/*
	Ujednolicenie odwolan do obiektow (cross-browser).
	Uzycie: var zmienna = new getObj('identyfikator');
*/

function getObj(name)
	{
	  if (document.getElementById)
	  {
	  	this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	  }
	  else if (document.all)
	  {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	  }
	  else if (document.layers)
	  {
	   	this.obj = document.layers[name];
	   	this.style = document.layers[name];
	  }
	}

function displayAlert(oElem, errAlert)
{
	alert(errAlert);
	if (window.focus) oElem.focus();
	//alert(oElem.name);
	return false;
}

function onRegistrationSubmit()
{
	oForm=new getObj('formularz');
	return checkRequredFields(oForm);
}	

// troche zamieszania z tymi funkcjami zeby zachowac "zgodnosc wstecz" ze wczesniejszymi wdrozeniami UNITY
function checkRequredFields(oForm) {
	return checkForm(oForm.obj);
}
	
function checkForm(form)
	{
		//Pobranie obiektu formularza
		// var oForm = new getObj('formularz');
		//Liczymy pola formularza
		var elementsCount =  form.elements.length;
		//Domyslny komunikat bledu
		var errAlert='Prosz\u0119 wype\u0142ni\u0107 wszystkie wymagane pola. Pola wymagane s\u0105 oznaczone gwiazdk\u0105.';
		//Sprawdzamy wszystkie elementy formularza
		for (i=0;i<elementsCount;i++)
		{
			oElem=form.elements[i];
			//Jesli nazwa pola zaczyna sie od 'w_'
			if (oElem.name.indexOf('_w_')==0)
			{
				//jesli pole ma w nazwie email to sprawdz czy jest to poprawny email - by pablos
				//if ((oElem.name.indexOf('email')>0) && CheckEMail(oElem,'Prosz\u0119  wprowadzi\u0107 poprawny adres e-mail'))
				//	return false;
				
				if (oElem.name.indexOf('_email')>0 && oElem.value && oElem.value.search(validMail) == -1)
					return displayAlert(oElem, 'Prosz\u0119  wprowadzi\u0107 poprawny adres e-mail.');

				if (oElem.name.indexOf('_kod_pocztowy')>0 && oElem.value && oElem.value.search(validPostCode) == -1)
					return displayAlert(oElem, 'Prosz\u0119 wprowadzi\u0107 poprawny numer.');

				if (oElem.name.indexOf('_nr_')>0 && oElem.value && oElem.value.search(validInt) == -1)
					return displayAlert(oElem, 'Prosz\u0119 wprowadzi\u0107 poprawny numer.');

				//Na razie dziala na pola tekstowe, inne typy pol juz wkrotce				
				if ((oElem.type=='text' || oElem.type=='textarea') && !oElem.value)
					return displayAlert(oElem, errAlert);

				if (oElem.type=='select-one' && !oElem.options[oElem.selectedIndex].value)
					return displayAlert(oElem, errAlert);
				
				if ((oElem.type=='checkbox' || oElem.type=='radio') && oElem.name.indexOf('_s_')== -1)
				{
					radioGroup = form[oElem.name];
					if (radioGroup.length){
						j = radioGroup.length - 1;
						do
							if (radioGroup[j].checked) 
							break;
						while (j--);
						if (j < 0)
							return displayAlert(oElem, errAlert);
					}
					else
					{
						if (!radioGroup.checked)
							return displayAlert(oElem, errAlert);
					}
				}
				else 
				if (oElem.name.indexOf('_s_')>0 && CheckGroup(form,'_w_s_','Prosz\u0119 wybra\u0107 przynajmniej jedn\u0105 specjalno\u015B\u0107.') == false)
					return false;

			}
		}
		return true;
    }
