﻿
var elementId = 0;
var countPerson = 1;
var personData = Array();

// dummy elements
var formElement = new Element('div');
var formSection = new Element('div');
var personTable = new Element('div');
var inputFields = new Element('div');
var textArea = new Element('div');

	/*	Funkcje obsługi zdarzeń
	-----------------------------------------------------------------------------
	*/	
	
function personInputs(){

	// form elements
	
	var title = new Element('div', {
		'text': 'Podaj członków grupy:',
		'calss': 'grid_8',
		'styles': {'color': '#444', 'text-decoration': 'underline', 'font-size': '12pt', 'padding-left': '7pt', 'padding-bottom': '10pt', 'padding-top': '10pt'}
	}); 
	
	var imieLabel = new Element('label', {
		'text': 'Imię',
		'for': 'Imie',
		'calss': 'grid_4',
		'styles': {'color': '#444', 'font-weight': 'bolder', 'padding-left': '7pt'}
	});
	
	var imie = new Element('input', {
		'name': 'Imie',
		'type': 'text',
		'maxlength': '20',
		'value': '',
		'calss': 'required grid_3',
		'styles': {'color': '#444', 'width': '100pt', 'margin-left': '17pt', 'margin-right': '15pt'}
	});
	
	//----------------------------------------------
	
	var nazwiskoLabel = new Element('label', {
		'text': 'Nazwisko',
		'for': 'Nazwisko',
		'calss': 'required grid_3',
		'styles': {'color': '#444', 'font-weight': 'bolder', 'padding-left': '7pt'}
	});
	
	var nazwisko = new Element('input', {
		'name': 'Nazwisko',
		'type': 'text',
		'maxlength': '30',
		'value': '',
		'calss': 'required grid_3',
		'styles': {'color': '#444', 'width': '100pt', 'margin-left': '27pt', 'margin-right': '15pt'}
	});

	//----------------------------------------------
	
	var miastoLabel = new Element('label', {
		'text': 'Miasto',
		'for': 'Miasto',
		'calss': 'required grid_3',
		'styles': {'color': '#444', 'font-weight': 'bolder', 'padding-left': '7pt'}
	});
	
	var miasto = new Element('input', {
		'name': 'Miasto',
		'type': 'text',
		'maxlength': '30',
		'value': '',
		'calss': 'required grid_3',
		'styles': {'color': '#444', 'width': '100pt', 'margin-left': '5pt', 'margin-right': '15pt'}
	});
	
	//----------------------------------------------
	
	var kodLabel = new Element('label', {
		'text': 'Kod pocztowy',
		'for': 'Kod',
		'calss': 'required grid_3',
		'styles': {'color': '#444', 'font-weight': 'bolder', 'padding-left': '7pt'}
	});
	
	var kod = new Element('input', {
		'name': 'Kod',
		'type': 'text',
		'maxlength': '6',
		'value': '',
		'calss': 'required grid_3',
		'styles': {'color': '#444', 'width': '100pt', 'margin-left': '3pt', 'margin-right': '15pt'}
	});
	
	//----------------------------------------------
	
	var ulicaLabel = new Element('label', {
		'text': 'Ulica',
		'for': 'Ulica',
		'calss': 'required grid_3',
		'styles': {'color': '#444', 'font-weight': 'bolder', 'padding-left': '7pt'}
	});
	
	var ulica = new Element('input', {
		'name': 'Ulica',
		'type': 'text',
		'maxlength': '30',
		'value': '',
		'calss': 'required grid_3',
		'styles': {'color': '#444', 'width': '100pt', 'margin-left': '14pt', 'margin-right': '15pt'}
	});
	
	//----------------------------------------------
	
	var numerLabel = new Element('label', {
		'text': 'Numer',
		'for': 'Numer',
		'calss': 'required grid_3',
		'styles': {'color': '#444', 'font-weight': 'bolder', 'padding-left': '7pt'}
	});
	
	var numer = new Element('input', {
		'name': 'Numer',
		'type': 'text',
		'maxlength': '7',
		'value': '',
		'calss': 'required grid_3',
		'styles': {'color': '#444', 'width': '100pt', 'margin-left': '44pt', 'margin-right': '15pt'}
	});
	
	//----------------------------------------------
	
	var emailLabel = new Element('label', {
		'text': 'e-mail',
		'for': 'e-mail',
		'calss': 'required',
		'styles': {'color': '#444', 'font-weight': 'bolder', 'padding-left': '7pt'}
	});
	
	var email = new Element('input', {
		'name': 'e-mail',
		'type': 'text',
		'maxlength': '50',
		'value': 'adres@email.pl',
		'calss': 'required',
		'styles': {'color': '#444', 'width': '100pt', 'margin-left': '5pt', 'margin-right': '15pt'}
	});

	//----------------------------------------------
	
	var uwagiLabel = new Element('label', {
		'text': 'Uwagi',
		'for': 'Uwagi',
		'styles': {'color': '#444', 'font-weight': 'bolder', 'padding-left': '7pt'}
	});
	
	var uwagi = new Element('select', {
		'name': 'Uwagi',
		'calss': 'required',
		'styles': {'color': '#444', 'width': '100pt', 'margin-left': '48pt', 'margin-right': '15pt'}
	});
	
	var umundurowany = new Element('option', {
		'text': 'Umundurowany',
		'value': 'Umundurowany',
		'selected': 'selected',
		'calss': 'required'
	});
	
	var cywil = new Element('option', {
		'text': 'Cywil',
		'value': 'Cywil',
		'calss': 'required'
	});
	
	// Dodaj bt.
	var personAdd = new Element('input', {
		'name': 'personAdd',
		'type': 'button',
		'value': 'Dodaj osobę',
		'events': {
			'click': function(e){
				e.stop();
				var imie = formSection.getElement('input[name=Imie]').getProperty('value').trim();
				var nazwisko = formSection.getElement('input[name=Nazwisko]').getProperty('value').trim();
				var miasto = formSection.getElement('input[name=Miasto]').getProperty('value').trim();
				var kod = formSection.getElement('input[name=Kod]').getProperty('value').trim();
				var ulica = formSection.getElement('input[name=Ulica]').getProperty('value').trim();
				var numer = formSection.getElement('input[name=Numer]').getProperty('value').trim();
				var email = formSection.getElement('input[name=e-mail]').getProperty('value').trim();
				var uwagiTmp = formSection.getElement('select[name=Uwagi]').getSelected();
				var uwagi = uwagiTmp[0].getProperty('value').trim();
				
				var isRequiredVal = isRequired(imie,nazwisko,miasto,kod,ulica,numer);
				if(isRequiredVal.length == 0){ // Błedu nie było
					var maxLengthVal = maxLength(imie,nazwisko,miasto,kod,ulica,numer,email,uwagi)
					if(maxLengthVal.length == 0){ // Błedu nie było
						var isEmailVal = isEmail(email);
						if(isEmailVal.length == 0){ // Błedu nie było
							personListPut(imie,nazwisko,miasto,kod,ulica,numer,email,uwagi);
							clearPersonInput();
						}else{
							alert(isEmailVal);
						}
					}else{
						alert(maxLengthVal);
					}
				}else{
					alert(isRequiredVal);
				}
			}
		},
	'styles': {'float': 'right', 'margin-right': '120pt'}
	});
	
	// container
	var personSection = new Element('div',{'class': 'grid_8 inputField', 'events': {
		'mouseover': function(e) {
			//this.addClass('inputActive');
		},
		'mouseout': function(e) {
			//this.removeClass('inputActive');
		}
	}});
	
	title.inject(personSection);
	
	imieLabel.inject(personSection);
	imie.inject(personSection);
	personSection.inject(formSection);

	nazwiskoLabel.inject(personSection);
	nazwisko.inject(personSection);
	personSection.inject(formSection);
	
	var personSection = new Element('div',{'class': 'grid_8 inputField', 'events': {
		'mouseover': function(e) {
			//this.addClass('inputActive');
		},
		'mouseout': function(e) {
			//this.removeClass('inputActive');
		}
	}});
	miastoLabel.inject(personSection);
	miasto.inject(personSection);
	personSection.inject(formSection);
	
	kodLabel.inject(personSection);
	kod.inject(personSection);
	personSection.inject(formSection);
	
	var personSection = new Element('div',{'class': 'grid_8 inputField', 'events': {
		'mouseover': function(e) {
			//this.addClass('inputActive');
		},
		'mouseout': function(e) {
			//this.removeClass('inputActive');
		}
	}});
	ulicaLabel.inject(personSection);
	ulica.inject(personSection);
	personSection.inject(formSection);

	numerLabel.inject(personSection);
	numer.inject(personSection);
	personSection.inject(formSection);
	
	var personSection = new Element('div',{'class': 'grid_8 inputField', 'events': {
		'mouseover': function(e) {
			//this.addClass('inputActive');
		},
		'mouseout': function(e) {
			//this.removeClass('inputActive');
		}
	}});
	emailLabel.inject(personSection);
	email.inject(personSection);
	personSection.inject(formSection);
	
	umundurowany.inject(uwagi);
	cywil.inject(uwagi);
	
	uwagiLabel.inject(personSection);
	uwagi.inject(personSection);
	personSection.inject(formSection);
	
	var personSection = new Element('div',{'class': 'grid_8 inputField'});
	personAdd.inject(personSection);
	personSection.inject(formSection);
}

function personListPut(imie,nazwisko,miasto,kod,ulica,numer,email,uwagi){
	if(countPerson == 36){ //36 - 1 = 35 osób
		alert('Przekroczono zakres!\nMaksymalnie można wpisać 100 osób.');
		return 0;
	}
	personData['delbt'+elementId] = countPerson+'. Imię: '+imie.trim();
	personData['delbt'+elementId] += ' , Nazwisko: '+nazwisko.trim();
	personData['delbt'+elementId] += ' , Miasto: '+miasto.trim();
	personData['delbt'+elementId] += ' , Kod: '+kod.trim();
	personData['delbt'+elementId] += ' , Ulica: '+ulica.trim();
	personData['delbt'+elementId] += ' , Numer: '+numer.trim();
	personData['delbt'+elementId] += ' , e-mail: '+email.trim();
	personData['delbt'+elementId] += ' , Uwagi: '+uwagi;
	var personDataTableTmp = '<b>'+countPerson+'. '+imie.trim()+' '+nazwisko.trim()+'</b><br>'+miasto.trim()+' '+kod.trim()+' ul. '+ulica.trim()+' '+numer.trim()+' '+email.trim()+'<br>'+uwagi;
	var rowContent = new Element('div',{'class': 'personRow'});
	if(uwagi == 'Cywil'){
		rowContent.addClass('cywil');
	}
	rowContent.set({html: personDataTableTmp});
	rowContent.inject(personTable);
	var rowDelete = new Element('input',{
		'type': 'button',
		'value': 'Usuń',
		'class': 'delbt'+elementId,
		'events': {
			'click': function(e){
				e.stop();
				var rowid = this.getProperty('class');
				personData[rowid] = 'deleted';
				var previous = this.getPrevious('div');
				previous.destroy();
				this.destroy();
				countPerson--;
			}
		},
		'styles': {'float': 'right', 'margin-right': '110pt', 'margin-top': '-22pt'}
	});
	rowDelete.inject(personTable);
	countPerson++; // liczony od 1
	elementId++ // liczony od 0
}

function setPersonTable(listaOsob){
	var listaOsobTrim = listaOsob.trim();
	var osoby = listaOsobTrim.split('>>>>>');
	osoby.each(function(item,index){
		var osobyPola = item.split(',');
		var pole = Array();
		var i = 0;
		osobyPola.each(function(item,index){
			var osobyPolaVal = item.split(':');
			osobyPolaVal.each(function(item,index){
				if(index == 1){
					pole[i] = item;
					i++;
				}
			});
		});
		personListPut(pole[0],pole[1],pole[2],pole[3],pole[4],pole[5],pole[6],pole[7]);
	});
}

function clearPersonInput(){
	var imie = formSection.getElement('input[name=Imie]').setProperty('value','');
	var nazwisko = formSection.getElement('input[name=Nazwisko]').setProperty('value','');
	var miasto = formSection.getElement('input[name=Miasto]').setProperty('value','');
	var kod = formSection.getElement('input[name=Kod]').setProperty('value','');
	var ulica = formSection.getElement('input[name=Ulica]').setProperty('value','');
	var numer = formSection.getElement('input[name=Numer]').setProperty('value','');
	var email = formSection.getElement('input[name=e-mail]').setProperty('value','adres@email.pl');
	var uwagiTmp = formSection.getElement('select[name=Uwagi]').getFirst();
	var uwagi = uwagiTmp.setProperty('selected', 'selected');
}

	/*	Walidatory
	-----------------------------------------------------------------------------
	*/
	
function isRequired(imie,nazwisko,miasto,kod,ulica,numer){
	var errorMsg = 'Pola: \n\n';
	var err = 0;
	if(imie==''){errorMsg += '"Imię" ';err = 1;}
	if(nazwisko==''){errorMsg += '"Nazwisko" ';err = 1;}
	if(miasto==''){errorMsg += '"Miasto" ';err = 1;}
	if(kod==''){errorMsg += '"Kod" ';err = 1;}
	if(ulica==''){errorMsg += '"Ulica" ';err = 1;}
	if(numer==''){errorMsg += '"Numer" ';err = 1;}
	errorMsg += '\n\n są wymagane!';
	if(err){
		return errorMsg;
	}else{
		errorMsg = '';
		return errorMsg; //0
	}
}
	
function isEmail(email){
	var errorMsg = '';
	if(email.contains('@')){
		return errorMsg;
	}else{
		errorMsg = 'Podaj poprawny aders e-mail!';
		return errorMsg;
	}
}	

function maxLength(imie,nazwisko,miasto,kod,ulica,numer,email,uwagi){
	var errorMsg = 'Max. ilość znaków dla pola: \n\n';
	var err = 0;
	if(imie.length > 20){errorMsg += '"Imię" to '+20+' znaków\n';err = 1;}
	if(nazwisko.length > 30){errorMsg += '"Nazwisko" to '+30+' znaków\n';err = 1;}
	if(miasto.length > 30){errorMsg += '"Miasto" to '+30+' znaków';err = 1;}
	if(kod.length > 6){errorMsg += '"Kod" to '+6+' znaków\n';err = 1;}
	if(ulica.length > 30){errorMsg += '"Ulica" to '+30+' znaków\n';err = 1;}
	if(numer.length > 7){errorMsg += '"Numer" to '+7+' znaków\n';err = 1;}
	if(email.length > 50){errorMsg += '"E-mail" to '+50+' znaków\n';err = 1;}
	if(uwagi.length > 20){errorMsg += '"Uwagi" to '+20+' znaków';err = 1;}
	if(err){
		return errorMsg;
	}else{
		errorMsg = '';
		return errorMsg; //0
	}
}

function checkTextareaLength(textarea){
	var property = textarea.getProperty('class'); // np. maxlen:101
	var propertyTab = property.split(' ');
	propertyTab.each(function(item){
		if(item.trim().substr(0,6) == 'maxlen'){
			var maxlen = item.trim().split(':');
			var text = textarea.getProperty('value');
			if(text.length >= maxlen[1]){
				var maxText = maxlen[1] - 1;
				alert('Możesz wprowadzić max. '+maxText+' znaków!');
				textarea.setProperty('value', text.substr(0,maxText));
			}
		}
	});	
}

	/*	Pętla zdarzeń
	-----------------------------------------------------------------------------
	*/

	
window.addEvent('domready', function() {

	// Wyłączenie komunikatu o braku obsługi javascriptu
	//$('javascriptok').addClass('javaok');
	
	// Baner reklamowy
	new viewer($$('#reklamaSpons img)')).play(true);
	
	// Sprawdza, czy dany element jest w dostępny w DOM - inaczej skrypt się wywali -> NULL
	
	// Formularz kontaktowy
	if($('f1') != null){
		var sendButton = $('ctrl_5').removeClass('javaok');
		textArea = $('ctrl_3');
	}
	
	// Sprawdzenie dla formularza "Rejestracja grupy"
	if($('f3') != null){
		personTable = $('personTable');
		formElement = $('f3');
		formSection = $('osoby');
		textArea = $('ctrl_15');
		inputFields = formElement.getElements('.inputField');
		personInputs();
		var sendButton = $('ctrl_18').removeClass('javaok');
		
		var listaOsob = formElement.getElement('.listaOsob').getProperty('value');
		if(listaOsob.trim() != ''){
			setPersonTable(listaOsob);			
		}
	}
	
	// Sprawdzenie dla formularza "Rejestracja pojazdu"
	if($('f8') != null){
		personTable = $('personTable');
		formElement = $('f8');
		formSection = $('osoby');
		//textArea = $('ctrl_4');
		//textArea = $('ctrl_9');
		textArea = $('f8').getElements('textarea');
		inputFields = formElement.getElements('.inputField');
		personInputs();
		var sendButton = $('ctrl_114').removeClass('javaok');
		
		var listaOsob = formElement.getElement('.listaOsob').getProperty('value');
		if(listaOsob.trim() != ''){
			setPersonTable(listaOsob);			
		}
	}
	
	/*	Wysyłanie formularza
	-----------------------------------------------------------------------------
	*/
	
	// submit
	formElement.addEvent('submit', function(e) {
		//e.stop();
		var tmpContent = '';
		for(i=0;i < elementId;i++){
			if(personData['delbt'+i] != 'deleted'){
				if(i == 0){
					tmpContent += personData['delbt'+i];
				}else{
					tmpContent += ' >>>>> '+personData['delbt'+i];
				}
			}	
		}
		formElement.getElement('.listaOsob').set('value',tmpContent);
	});
	
	inputFields.addEvent('mouseover', function(e) {
		//this.addClass('inputActive');
	});
	
	inputFields.addEvent('mouseout', function(e) {
		//this.removeClass('inputActive');
	});
	
	textArea.addEvent('keydown', function(e) {
		//checkTextareaLength(textArea);
		checkTextareaLength(this);
	});
	
});