var oldretdate;
var oldonclick;

function epower_switch(elem) {

	d=$('epower_passengers');
	while (d.childNodes.length > 0) d.removeChild(d.childNodes[0]);

	tab=document.createElement('table');
	tab.setAttribute('class', 'flight');
	t=document.createElement('tbody');

	switch (elem.value) {
		case "Standard":
			tr=document.createElement('tr');
			td1=document.createElement('td');
			td2=document.createElement('td');
			td3=document.createElement('td');
			td4=document.createElement('td');
			td5=document.createElement('td');
			td6=document.createElement('td');
			sel1=document.createElement('select');
			sel2=document.createElement('select');
			sel3=document.createElement('select');

			td1.setAttribute('class', 'right');
			td1.appendChild(document.createTextNode('Dorośli:'));

			td3.setAttribute('class', 'right');
			td3.appendChild(document.createTextNode('Dzieci:'));
			td3.appendChild(document.createElement('br'));
			s1=document.createElement('span');
			s1.setAttribute('class', 'small');
			s1.appendChild(document.createTextNode('(2-11 lat)'));
			td3.appendChild(s1);

			td5.setAttribute('class', 'right');
			td5.appendChild(document.createTextNode('Niemowlęta:'));
			td5.appendChild(document.createElement('br'));
			s2=document.createElement('span');
			s2.setAttribute('class', 'small');
			s2.appendChild(document.createTextNode('(do 2 lat)'));
			td5.appendChild(s2);

			sel1.setAttribute('name', 'adt');
			for (i=1; i <= 4; i++) {
				o=document.createElement('option');
				o.setAttribute('value', i);
				o.appendChild(document.createTextNode(i));
				sel1.appendChild(o);
			}
			td2.appendChild(sel1);

			sel2.setAttribute('name', 'chd');
			for (i=0; i <= 4; i++) {
				o=document.createElement('option');
				o.setAttribute('value', i);
				o.appendChild(document.createTextNode(i));
				sel2.appendChild(o);
			}
			td4.appendChild(sel2);

			sel3.setAttribute('name', 'inf');
			for (i=0; i <= 4; i++) {
				o=document.createElement('option');
				o.setAttribute('value', i);
				o.appendChild(document.createTextNode(i));
				sel3.appendChild(o);
			}
			td6.appendChild(sel3);

			tr.appendChild(td1);
			tr.appendChild(td2);
			tr.appendChild(td3);
			tr.appendChild(td4);
			tr.appendChild(td5);
			tr.appendChild(td6);

			t.appendChild(tr);
		break;
		case "Youth":
			tr=document.createElement('tr');
			td1=document.createElement('td');
			td2=document.createElement('td');
			sel=document.createElement('select');

			sel.setAttribute('name', 'yth');
			for (i=1; i <= 4; i++) {
				o=document.createElement('option');
				o.setAttribute('value', i);
				o.appendChild(document.createTextNode(i));
				sel.appendChild(o);
			}

			td1.setAttribute('class', 'right');
			td1.setAttribute('style', 'width: 75px;');
			td1.appendChild(document.createTextNode('Młodzież:'));
			td2.appendChild(sel);
			tr.appendChild(td1);
			tr.appendChild(td2);
			t.appendChild(tr);
		break;
		case "Student":
			tr=document.createElement('tr');
			td1=document.createElement('td');
			td2=document.createElement('td');
			sel=document.createElement('select');

			sel.setAttribute('name', 'stu');
			for (i=1; i <= 4; i++) {
				o=document.createElement('option');
				o.setAttribute('value', i);
				o.appendChild(document.createTextNode(i));
				sel.appendChild(o);
			}

			td1.setAttribute('class', 'right');
			td1.setAttribute('style', 'width: 75px;');
			td1.appendChild(document.createTextNode('Studenci:'));
			td2.appendChild(sel);
			tr.appendChild(td1);
			tr.appendChild(td2);
			t.appendChild(tr);
		break;
		case "Senior":
			tr=document.createElement('tr');
			td1=document.createElement('td');
			td2=document.createElement('td');
			sel=document.createElement('select');

			sel.setAttribute('name', 'src');
			for (i=1; i <= 4; i++) {
				o=document.createElement('option');
				o.setAttribute('value', i);
				o.appendChild(document.createTextNode(i));
				sel.appendChild(o);
			}

			td1.setAttribute('class', 'right');
			td1.setAttribute('style', 'width: 75px;');
			td1.appendChild(document.createTextNode('Seniorzy:'));
			td2.appendChild(sel);
			tr.appendChild(td1);
			tr.appendChild(td2);
			t.appendChild(tr);
		break;
		default:
			tr=document.createElement('tr');
			td=document.createElement('td');
			td.appendChild(document.createTextNode('unsupported'));
			tr.appendChild(td);
			t.appendChild(tr);
		break;
	}

	tab.appendChild(t);
	d.appendChild(tab);
}


function epower_atlas(rf) {
	window.open('ext/epower/atlas.html?rf='+rf, 'epower_atlas', 'directories=no,location=no,menubar=no,scrollbars=yes,resizable=no,status=no,toolbar=no,width=700,height=650,top=5,left=5', true);
}

function epower_disable_ret() {
	oldretdate=$('epower_rd').value;
	$('epower_rd').value="-- -- ----";
	$('epower_rd').style.disabled=true;
	oldonclick=$('epower_b_rd').onclick;
	$('epower_b_rd').onclick="";
}

function epower_enable_ret() {
	$('epower_rd').disabled=false;
	$('epower_rd').value=(oldretdate?oldretdate:'Wybierz datę przelotu');
	if (oldonclick) $('epower_b_rd').onclick=oldonclick;
}

function loc_focus(e) {
	if (e.value == "Podaj miasto lub kod lotniska") e.value="";
}

function loc_blur(e) {
	if (e.value.length == 0) e.value="Podaj miasto lub kod lotniska";
}

function epower_validate() {
	f=document.forms['epower'];
	err='';

	rv=false;
	for (i=0; i < f.elements['flightType'].length; i++) {
		if (f.elements['flightType'][i].checked) {
			rv=true;
			break;
		}
	}
	if (rv == false) err+='- Proszę wybrać rodzaj przelotu\n';

	if (f.origin.value.length < 3 || f.origin.value == "Podaj miasto lub kod lotniska") err+='- Proszę wybrać miejsce wylotu\n';
	if (f.destination.value.length < 3 || f.destination.value == "Podaj miasto lub kod lotniska") err+='- Proszę wybrać miejsce przylotu\n';

	if (!f.depDate.value.match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)) err+='- Proszę podać datę wylotu\n';
	if (f.elements['flightType'][0].checked && !f.retDate.value.match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)) err+='- Proszę podać datę powrotu\n';

	if (f.inf.value > f.adt.value) err+='- Liczba niemowląt nie może być większa niż liczba osób dorosłych\n';

	if (f.fb_email.checked) {
		if (f.email.value.length == 0 || f.email.value == "Podaj adres e-mail") err+='- Proszę podać adres e-mail lub odznaczyć pole wyboru\n';
		else if (!f.email.value.match(/^[a-zA-Z0-9]+([\-._]?[a-zA-Z0-9]*)*@[a-zA-Z0-9\-.]+[a-zA-Z]{2,4}$/)) err+='- Podany e-mail jest niepoprawny\n';
	}
	if (f.fb_tel.checked && (f.tel.value.length == 0 || f.tel.value == "Podaj numer telefonu")) err+='- Proszę podać numer telefonu lub odznaczyć pole wyboru\n';
	if (f.fb_sms.checked && (f.sms.value.length == 0 || f.sms.value == "Podaj numer telefonu")) err+='- Proszę podać numer telefonu lub odznaczyć pole wyboru\n';

	if (err.length > 0) {
		alert(err);
		return false;
	}
	else return true;
}

function fb_toggle(elem, field) {
	state=elem.checked;
	elem.form.fb_email.checked=false;
	elem.form.fb_tel.checked=false;
	elem.form.fb_sms.checked=false;
	$('flight_fb_email').style.visibility="hidden";
	$('flight_fb_tel').style.visibility="hidden";
	$('flight_fb_sms').style.visibility="hidden";
	elem.checked=state;

	switch (field) {
		case 'email':
			$('flight_fb_email').style.visibility=(state?"visible":"hidden");
		break;
		case 'tel':
			$('flight_fb_tel').style.visibility=(state?"visible":"hidden");
		break;
		case 'sms':
			$('flight_fb_sms').style.visibility=(state?"visible":"hidden");
		break;
	}
}

function fb_ev(elem, type, event) {
	switch (type) {
		case 'email':
			if (event.type == "focus") {
				if (elem.value == "Podaj adres e-mail") elem.value="";
			}
			else if (event.type =="blur") {
				if (elem.value.length == 0) elem.value="Podaj adres e-mail";
			}
		break;
		case 'tel':
		case 'sms':
			if (elem.value == "Podaj numer telefonu") elem.value="";
		break;
	}
}

