﻿/* $Id: form_utils_utf.js,v 1.3 2005/11/24 18:55:56 pch Exp $ */
//--- Sygnatura pliku ---------------------------------------------------------------
// 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 ---------------------------------------------------------------

//validMail=/^[a-zA-Z0-9._-]+\@([a-zA-Z0-9_-]+\.){1,}[\w]{1,5}$/;
var	validTelFull=/^[0-9\s\-\/\(\)\+]+$/;
var	validNum=/^[\d]+$/;
var	validTxtPl=/^[A-Za-ząęóśłżźćńĄĘÓŚŁŻŹĆŃ]+$/;
var	validTxtSpPl=/^[\-\sA-Za-ząęóśłżźćńĄĘÓŚŁŻŹĆŃ]+$/;
//validPostCode=/^[0-9]{2}-[0-9]{3}$/;
var	validYear=/^[0-9]{4}$/;
var	validDayMonth=/^[0-9]{2}$/;	
var	validNIP=/^[0-9]{3}[ -]?[0-9]{3}[ -]?[0-9]{2}[ -]?[0-9]{2}$/;	

	//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

//Obiekt sprawdzanego formularza
var oForm;


function compactNonNum(textVal) {

var inpstr= textVal
var inpstl= textVal.length;  			// document.forms[0].dane.length;
//	alert('compactNonAlfa = '+inpstr);
    for(i=0;i<inpstr.length;i++) {
		znak = inpstr.charAt(i);
		if (znak<'0' || znak>'9') {
		  inpstr = inpstr.substring(0,i) + inpstr.substring(i+1,inpstr.length);
		  i = i-1; 
		}
    }
	return inpstr;
}
	
function checkNIP(nip){
	var weights = new Array(6, 5, 7, 2, 3, 4, 5, 6, 7);
	var nipA = new Array();
	var nipS = new String(compactNonNum(nip));
	nipA = nipS.split("");

	var sum=0;
	for (i=0; i<weights.length; i++){
		sum+=weights[i]*parseInt(nipA[i]);
	}
	sum=sum % 11;
	if (sum==10) sum=0;

	if (sum==nipA[9])
		return true;
	else
		return false;
}

//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 += "-";
}
	//obcina ewentualne spacje z pocz±tku i końca zadanego łańcucha
	function Trim(lancuch)
	{
		if (lancuch=='')
			return lancuch;
		while (lancuch.charAt(0) == " ") 
			lancuch = lancuch.substring(1,lancuch.length);
		while (lancuch.charAt(lancuch.length-1) == " ") 
			lancuch = lancuch.substring(0,lancuch.length-2);
		return lancuch;	
	}
	//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(oForm,prefix,msg)
 { 
	var elements = oForm.obj.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;
      }
 }

function displayAlert(oElem, errAlert){
	alert(errAlert);
	if (window.focus) oElem.focus();
	//alert(oElem.name);
	return false;
}

function getFormObject(name){
if (DHTML){
  if (document.getElementById && document.getElementById(name)){
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all && document.all[name]){
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers && document.layers[name]){
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
 }
}

function sprawdzFormularz(formName){
	oForm=new getFormObject(formName);
	if (oForm.obj) 
		return checkRequiredFields(oForm);
}

function onRegistrationSubmit(){
	oForm=new getObj('formularz');
	return checkRequiredFields(oForm);
}	
	
function checkRequiredFields(oForm){

		//Pobranie obiektu formularza
		// var oForm = new getObj('formularz');
		//Liczymy pola formularza
		var elementsCount =  oForm.obj.elements.length;
		//Domyslny komunikat bledu
		var errAlert = WYSIWYG_INVTXT;
		//Sprawdzamy wszystkie elementy formularza
		for (k=0;k<elementsCount;k++){
			oElem=oForm.obj.elements[k];

			//Jesli nazwa pola zaczyna sie od '_n_' - niewymagane, sprawdzamy tylko format
			//Jesli nazwa pola zaczyna sie od '_w_' - wymagane
			//Wyjatek: plik (nazwa od 'file_w_')
			
			//debug:
			//alert(i+'/'+elementsCount+': '+oElem.name+' = '+oElem.value);
			//
			if (oElem.name.indexOf('_n_')==0 || 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, WYSIWYG_INVEMAILFORMAT);

				if (oElem.name.indexOf('_nip')>0 && oElem.value && (oElem.value.search(validNIP) == -1 || !checkNIP(oElem.value)))
					return displayAlert(oElem, WYSIWYG_INVNIPFORMAT);

				if (oElem.name.indexOf('_kod_pocztowy')>0 && oElem.value && oElem.value.search(validPostCode) == -1)
					return displayAlert(oElem, WYSIWYG_INVZIPFORMAT);

				if (oElem.name.indexOf('_nr')>0 && oElem.value && oElem.value.search(validNum) == -1)
					return displayAlert(oElem, WYSIWYG_INVNUMFORMAT);

				if (oElem.name.indexOf('_tekstSp')>0 && oElem.value && oElem.value.search(validTxtSpPl) == -1){
					return displayAlert(oElem, WYSIWYG_INVTEXTSPFORMAT);
				} else {
					if (oElem.name.indexOf('_tekst')>0 && oElem.value && oElem.value.search(validTxtPl) == -1)
						return displayAlert(oElem, WYSIWYG_INVTEXTFORMAT);
				}
			}

			if (oElem.name.indexOf('_w_')==0 || oElem.name.indexOf('file_w_')==0){
				//Pole tekstowe
				if ((oElem.type=='text' || oElem.type=='textarea') && !oElem.value)
					return displayAlert(oElem, WYSIWYG_INVTXT);
				//Plik
				if (oElem.type=='file' && !oElem.value)
					return displayAlert(oElem, WYSIWYG_INVFILE);
				//select
				if (oElem.type=='select-one' && !oElem.options[oElem.selectedIndex].value)
					return displayAlert(oElem, WYSIWYG_INVSELECT);
				//grupa radio lub checkboxy
				if (oElem.type=='checkbox' || oElem.type=='radio'){
					radioGroup = oForm.obj[oElem.name];
					if (radioGroup.length){
						j = radioGroup.length - 1;
						do
							if (radioGroup[j].checked) 
							break;
						while (j--);
						if (j < 0)
							return displayAlert(oElem, WYSIWYG_INVCHECK	);
					}
					else {
						if (!radioGroup.checked)
							return displayAlert(oElem, WYSIWYG_INVCHECK	);
					}
				}
			}
		}
		return true;
    }