functions_table = Array(

	//new Array("ow","roundtrip_img('AVIO','EN')")
/*
	Array("miasto_z","copyToMany('miasto_z',Array('m_z','m_z1','m_zm'))"),

	Array("miasto_d","copyToMany('miasto_d',Array('m_d','m_d1','m_dm'))"),

	Array("miasto_z2","copyToMany('miasto_z2',Array('m_z2'))"),

	Array("miasto_d2","copyToMany('miasto_d2',Array('m_d2'))"),

	Array("miasto_z3","copyToMany('miasto_z3',Array('m_z3'))"),

	Array("miasto_d3","copyToMany('miasto_d3',Array('m_d3'))"),

	Array("miasto_z4","copyToMany('miasto_z4',Array('m_z4'))"),

	Array("miasto_d4","copyToMany('miasto_d4',Array('m_d4'))"),

	*/

//	Array("ile_dor","copyToMany('ile_dor',Array('ile_dor_select'))"),

//	Array("ile_mlo","copyToMany('ile_mlo',Array('ile_mlo_select'))"),

//	Array("ile_nie","copyToMany('ile_nie',Array('ile_nie_select'))"),

//	Array("ile_dzi","copyToMany('ile_dzi',Array('ile_dzi_select'))"),

//	Array("ile_sen","copyToMany('ile_sen',Array('ile_sen_select'))"),

	Array("klasa","copyToMany('klasa',Array('klasa_select'))"),

	

	Array("godz_w","copyToMany('godz_w',Array('godz_w_select','godz_w_select_w_jedna','godz_w_select_1')),specialAction('godz_w')"),

	Array("godz_p","copyToMany('godz_p',Array('godz_p_select')),specialAction('godz_p')"),

	Array("godz_w2","copyToMany('godz_w2',Array('godz_w_select_2'))"),

	Array("godz_w3","copyToMany('godz_w3',Array('godz_w_select_3'))"),

	Array("godz_w4","copyToMany('godz_w4',Array('godz_w_select_4'))"),

		

	Array("win_time_d","copyToMany('dokl_lub_plus_minus_z',Array('dokl_lub_plus_minus_z'));specialAction('win_time_d')"),

	Array("win_time_a","copyToMany('dokl_lub_plus_minus_d',Array('dokl_lub_plus_minus_d'));specialAction('win_time_a')")

		

		);



function write_form(array_id)
{

alert('test');

	var tabDate = Array();

	var type = null;

	//buff = "";

	for (var i=0; i<array_id.length; i++)

		{

		if (array_id[i].indexOf("?")==-1)

			var ile = 1;

		else

			var ile = 9;

			

		for (var j=1; j<=ile; j++)

			{

			

			var search = array_id[i].replace("?",j);	

			//if (document.getElementById(search))
			//	alert(search+' = '+document.getElementById(search).value);

			try {

				type = document.getElementById(search).type;

			}

			catch (e) {

				type = null;

			}

			//buff += search + " " +type+" =>";

			if ((type != null))

			{

				//alert(search + " " + document.getElementById(search).value);

				if(search == 'w_d' 

				|| search == 'w_m'

				|| search == 'w_r'

				|| search == 'p_d'

				|| search == 'p_m'

				|| search == 'p_r'

				|| search == 'w_d2'

				|| search == 'w_m2'

				|| search == 'w_r2'

				|| search == 'w_d3'

				|| search == 'w_m3'

				|| search == 'w_r3'

				|| search == 'w_d4'

				|| search == 'w_m4'

				|| search == 'w_r4'

				|| search == 'date_from'

				|| search == 'date_to'

				|| search == 'date_from_one_way'

				|| search == 'date_from_1'

				|| search == 'date_from_2'

				|| search == 'date_from_3'

				|| search == 'date_from_4'

				)

					{

					tabDate[search] = document.getElementById(search).value;	

					}

				else

					{

					switch (type) 

						{

						case "hidden" :

						case "text" : 		//minisercz ma pola w_kod_d i w_kod_z w 2och miejscach/formularzach!!!!!
											var value;
											if (search=='w_kod_d' || search=='w_kod_z')
											{
											  	if (document.forms['panel'])
											  		var form = 'panel';
										  		else if (document.forms['miniform'])
										  			var form = 'miniform';
									    		
								    			value = document.forms[form][search].value;
									  		}
									  		else
									  			value = document.getElementById(search).value;
											
											
												Cookies.create("_"+search,escape(value),999);

											break;

						case "checkbox" : 	var checked = document.getElementById(search).checked;

											if (checked)

												Cookies.create("_"+search,'true',999);

											else

												Cookies.create("_"+search,'false',999);

											break;

						case "select-one" : Cookies.create("_"+search,document.getElementById(search).selectedIndex,999);

											//buff += document.getElementById(search).selectedIndex;

											break;

						}

					}

			}

			}

		}

			setDateCookies(tabDate);		

}



function read_form(array_id)
{
	var flaga = false;
//alert(document.cookie.length);


	var type = null;

	//buff = "";

	//alert(array_id);

	for (var i=0; i<array_id.length; i++)
	{
		if (array_id[i].indexOf("?")==-1)
			var ile = 1;
		else
			var ile = 9;


		for (var j=1; j<=ile; j++)
		{
			var search = array_id[i].replace("?",j);	
			//var cookie = readCookie("_"+search);
			//cookie = unescape(cookie);
			//buff += "_"+search+" => "+cookie+"\n";
			//alert(search+" "+document.getElementById(search).onchange);
			try 
			{
				type = document.getElementById(search).type;
			}
			catch (e) 
			{
				type = null;
			}

			//alert(search+' = '+Cookies['_'+search]);

			if ((Cookies['_'+search] != null) && (type != null))
			{
				//buff += search + " " +type+" => "+cookie+"\n";
				switch (type) 
				{
					case "hidden" :
					case "text"   :	if(Cookies['_'+search] == 'undefined')  
										Cookies['_'+search] = '';

									if (search=='miasto_d' || search=='miasto_z' || search=='miasto_d2' || search=='miasto_z2' || search=='miasto_d3' || search=='miasto_z3' || search=='miasto_d4' || search=='miasto_z4' ||
										search=='w_kod_d' || search=='w_kod_z' || search=='w_kod_d2' || search=='w_kod_z2' || search=='w_kod_d3' || search=='w_kod_z3' || search=='w_kod_d4' || search=='w_kod_z4'
										|| search=='w_d' || search=='w_m' || search=='w_r' || search=='p_d' || search=='p_m' || search=='p_r' || search=='date_from' || search=='date_to' || search=='date_from_one_way'
										|| search=='w_d2' || search=='w_d3' || search=='w_d4' || search=='date_from_1' || search=='date_from_2' || search=='date_from_3' || search=='date_from_4')
										continue;						
									if ((search=='date_from' || search=='date_to') && Cookies['_ow']!='on')
									{//spr. czy daty w cookie nie sa stare, jesli tak to beda ustawione na +3/+10dni wzgledem dzisiaj
									 //dotyczy to tylko sytuacji gdy jest lot w 2 strony
										var today = new Date();
										today = today.getTime();
										
										var date = Cookies['_'+search];
										

										
										var temp = date.split('/');
										date = new Date(temp[2],temp[1]-1,temp[0]);
										date = date.getTime();		
										
										if ((date < today || Cookies['_date_from']=='' || Cookies['_date_to']=='') && !flaga)
										{
										  	wstaw_date2('date_from');
										  	wstaw_date2('date_to');		
								  			flaga = true;							  	
										}																		
	
	
										//var last_date_from = Cookies['_date_from'];
										//var last_date_to = Cookies['_date_to'];
											
	/*									var temp = last_date_from.split('/');
										last_date_from = new Date(temp[2],temp[1]-1,temp[0]);
										last_date_from = last_date_from.getTime();

										var temp = last_date_to.split('/');
										last_date_to = new Date(temp[2],temp[1]-1,temp[0]);
										last_date_to = last_date_to.getTime();	*/
											
										//alert('last_date_to: '+last_date_to+' vs '+today);
										/*	
										if (last_date_to < today || last_date_from < today || Cookies['_date_from']=='' || Cookies['_date_to']=='')
										{
										  	wstaw_date('date_from');
										  	wstaw_date('date_to');									  	
										}*/
										//flaga = true;
									}
									else if (Cookies['_ow']=='on' && search=='date_from_one_way')
									{
										var today = new Date();
										today = today.getTime();									  
									  
										var last_date_from = Cookies['_date_from_one_way'];
					
										
										var temp = last_date_from.split('/');
										last_date_from = new Date(temp[2],temp[1]-1,temp[0]);
										last_date_from = last_date_from.getTime();

										if (last_date_from < today || Cookies['_date_from_one_way']=='')
										{//data jest nieaktualna lub cookie jest pusty po wpisaniu z palca pustej daty do inputa
										//alert('.');
											wstaw_date2('date_from_one_way');
										}  
							  		}
										/*
									if ((search=='miasto_z' || search=='miasto_d') && Cookies['_'+search]=="")
									{
							    		if (search=='miasto_z')
							    		{
							     			Cookies['_miasto_z'] = document.getElementById('miasto_z').value = 'Warszawa';	
							     			Cookies['_w_kod_z']  = document.getElementById('w_kod_z').value = 'WAW';	
							  			}
							  			else
							  			{
							     			Cookies['_miasto_d'] = document.getElementById('miasto_d').value = 'Londyn';	
							     			Cookies['_w_kod_d'] = document.getElementById('w_kod_d').value = 'LON';	
										}
							  		}
									else
									{*/	
										document.getElementById(search).value = Cookies['_'+search];
									//}
										
										
								//	if (search=='date_from_one_way')
								//		alert(search+ ' = '+Cookies['_'+search]+ ' ow='+Cookies['_ow']);	

									break;

					case "checkbox" : if (Cookies['_'+search] == 'true')
   										   document.getElementById(search).checked = true;
        								else
										document.getElementById(search).checked = false;

									break;
					/*case "select-one" : 
										try 
										{
											document.getElementById(search).selectedIndex = Cookies['_'+search];
										}
										catch (e){}
										break;*/
					}



					//specialAction(search)

				/*	

				if (document.getElementById(search).onchange)

					{

					var fireOnThis = document.getElementById(search);

					if (fireOnThis.onchange)

						if( document.createEvent ) 

							{

							var evObj = document.createEvent('HTMLEvents');

							evObj.initEvent( 'change', true, false );

							fireOnThis.dispatchEvent(evObj);

							}

						else if( document.createEventObject ) 

							{

							fireOnThis.fireEvent('onchange');

							}	

					}

				*/

				functions = get_function(search) 

				if (functions != null)

					eval(functions);

			}
			else if (search=='date_from' || search=='date_to')//Cookies['_'+search] == NULL
			{
				wstaw_date2(search);			
				document.getElementById(search).value = Cookies['_'+search];
			}
			else if (search=='date_from_one_way')//Cookies['_'+search] == NULL
			{//alert('?');
				wstaw_date2(search);			
				document.getElementById(search).value = Cookies['_'+search];
			}
			
		}
	}
}

//w przypadku gdy data_from i data_to nie bedzie jeszce zapisana w cookie, lub bedzie nieaktualna, wowczas do inputow zostanie wpisana
//data odpowiednio +3 i +10 dni wzgledem dzisiejszej daty
function wstaw_date2(typ)
{
//alert('___'+typ);
//alert('--'+typ);
	var date = new Date();
	var temp = date.getDate();

	if (typ=='date_from' || typ=='date_from_one_way')
		date.setDate(temp+3);
	else if (typ=='date_to')
		date.setDate(temp+10);
		
		
	var day = date.getDate();
	var month = date.getMonth()+1;
	var year = date.getFullYear();
	
	if (typ=='date_from')// || typ=='date_from_one_way')	
	{
		document.getElementById('w_d').value = day;
		document.getElementById('w_m').value = month;	
		document.getElementById('w_r').value = year;			
	}
	else if (typ=='date_to')
	{
		document.getElementById('p_d').value = day;
		document.getElementById('p_m').value = month;	
		document.getElementById('p_r').value = year;	   
 	}

	Cookies['_'+typ] = day+'/'+month+'/'+year;     
}



function get_function(name)

{

	var func = null;

	for (var k=0; k<functions_table.length; k++)

		if (functions_table[k][0]==name)

			{

				func = functions_table[k][1];

				break;

			}

			

	return func;

	

}



function wstaw_cookie(get_city)

{
	var tab = new Array(
	
		'w_kod_z2',

		'w_kod_z3',

		'w_kod_z4',

		'w_kod_d',

		'w_kod_d2',

		'w_kod_d3',

		'w_kod_d4',

		'miasto_z2',

		'miasto_z3',

		'miasto_z4',

		'miasto_d',

		'miasto_d2',

		'miasto_d3',

		'miasto_d4',

		'w_d',

		'w_m',

		'w_r',

		'p_d',

		'p_m',

		'p_r',

		'w_d2',

		'w_m2',

		'w_r2',

		'w_d3',

		'w_m3',

		'w_r3',

		'w_d4',

		'w_m4',

		'w_r4',

		'date_from',

		'date_to',

		'date_from_one_way',

		'date_from_1',

		'date_from_2',

		'date_from_3',

		'date_from_4',

		'godz_w',

		'godz_p',

		/*'godz_w2',

		'godz_w3',

		'godz_w4',

		'klasa',*/

		'ile_dor',

		//'ile_mlo',

		'ile_dzi',

		'ile_nie',

		/*'ile_sen',

		'dir',

		'flight_type',

		'checkbox3',

		'direct_flight_all'*/

		'win_time_a',

		'win_time_d',

		'dokl_lub_plus_minus_z',

		'dokl_lub_plus_minus_d',
		
		'ow');	
	
	if (get_city)
	{
		tab.push('w_kod_z');
		tab.push('miasto_z');
	}
	

	read_form(tab);

}



function zapisz_cookie()

{	

	write_form(new Array(

		'w_kod_z',

		'w_kod_z2',

		'w_kod_z3',

		'w_kod_z4',

		'w_kod_d',

		'miasto_z',

		'miasto_z2',

		'miasto_z3',

		'miasto_z4',

		'miasto_d',

		'miasto_d2',

		'miasto_d3',

		'miasto_d4',

		'w_d',

		'w_m',

		'w_r',

		'p_d',

		'p_m',

		'p_r',

		'w_d2',

		'w_m2',

		'w_r2',

		'w_d3',

		'w_m3',

		'w_r3',

		'w_d4',

		'w_m4',

		'w_r4',

		'date_from',

		'date_to',

		'date_from_one_way',

		'date_from_1',

		'date_from_2',

		'date_from_3',

		'date_from_4',

		'godz_w',

		'godz_p',

		/*'godz_w2',

		'godz_w3',

		'godz_w4',

		'klasa',*/

		'ile_dor',

		//'ile_mlo',

		'ile_dzi',

		'ile_nie',

		/*'ile_sen',

		'dir',

		'flight_type',

		'checkbox3',

		'direct_flight_all'*/

		'win_time_a',

		'win_time_d',

		'dokl_lub_plus_minus_z',

		'dokl_lub_plus_minus_d',

		'ow'

		));

}

function copyToMany(origin,destination)

{

	var origin_tmp = document.getElementById(origin).value;

	for(var i=0; i<destination.length; i++)

		document.getElementById(destination[i]).value = origin_tmp;

}



function setDateCookies(tab)

{

	dni0 = getDaysFromToday(tab['w_r'],tab['w_m'],tab['w_d']);

	dni1 = getDaysFromToday(tab['p_r'],tab['p_m'],tab['p_d']);

	dni2 = getDaysFromToday(tab['w_r2'],tab['w_m2'],tab['w_d2']);

	dni3 = getDaysFromToday(tab['w_r3'],tab['w_m3'],tab['w_d3']);

	dni4 = getDaysFromToday(tab['w_r4'],tab['w_m4'],tab['w_d4']);

	

	

	 
	if (document.getElementById('w_r'))	
		Cookies.create("_w_r",escape(document.getElementById('w_r').value),dni0);

	if (document.getElementById('w_m'))
		Cookies.create("_w_m",escape(document.getElementById('w_m').value),dni0);

	if (document.getElementById('w_d'))
		Cookies.create("_w_d",escape(document.getElementById('w_d').value),dni0);

	
	if (document.getElementById('w_r2'))
		Cookies.create("_w_r2",escape(document.getElementById('w_r2').value),dni2);

	if (document.getElementById('w_m2'))
		Cookies.create("_w_m2",escape(document.getElementById('w_m2').value),dni2);

	if (document.getElementById('w_d2'))
		Cookies.create("_w_d2",escape(document.getElementById('w_d2').value),dni2);

	
	if (document.getElementById('w_r3'))
		Cookies.create("_w_r3",escape(document.getElementById('w_r3').value),dni3);

	if (document.getElementById('w_m3'))
		Cookies.create("_w_m3",escape(document.getElementById('w_m3').value),dni3);

	if (document.getElementById('w_d3'))
		Cookies.create("_w_d3",escape(document.getElementById('w_d3').value),dni3);

	
	if (document.getElementById('w_r4'))
		Cookies.create("_w_r4",escape(document.getElementById('w_r4').value),dni4);

	if (document.getElementById('w_m4'))
		Cookies.create("_w_m4",escape(document.getElementById('w_m4').value),dni4);

	if (document.getElementById('w_d4'))
		Cookies.create("_w_d4",escape(document.getElementById('w_d4').value),dni4);

	
	if (document.getElementById('p_r'))
		Cookies.create("_p_r",escape(document.getElementById('p_r').value),dni1);

	if (document.getElementById('p_m'))
		Cookies.create("_p_m",escape(document.getElementById('p_m').value),dni1);

	if (document.getElementById('p_d'))
		Cookies.create("_p_d",escape(document.getElementById('p_d').value),dni1);

	

	Cookies.create("_date_from",escape(document.getElementById('date_from').value),dni0);

	Cookies.create("_date_to",escape(document.getElementById('date_to').value),dni1);

	
	if (document.getElementById('date_from_one_way'))
		Cookies.create("_date_from_one_way",escape(document.getElementById('date_from_one_way').value),dni0);

	if (document.getElementById('date_from_1'))
		Cookies.create("_date_from_1",escape(document.getElementById('date_from_1').value),dni0);

	if (document.getElementById('date_from_2'))
		Cookies.create("_date_from_2",escape(document.getElementById('date_from_2').value),dni2);

	if (document.getElementById('date_from_3'))
		Cookies.create("_date_from_3",escape(document.getElementById('date_from_3').value),dni3);

	if (document.getElementById('date_from_4'))
		Cookies.create("_date_from_4",escape(document.getElementById('date_from_4').value),dni4);

}



function getDaysFromToday(y,m,d)

{

	y = parseInt(y);

	m = parseInt(m)- 1;

	d = parseInt(d) -1;

	

	toDay = new Date();

	toDay.setHours(0,0,0,0);

	

	date = new Date();

	date.setFullYear(y,m,d);

	date.setHours(0,0,0,0);

	

	return parseInt((date.getTime() - toDay.getTime())/1000/60/60/24);

	

}



function specialAction(name)

{

	switch(name)

	{

		case 'godz_w':

			if(Cookies['_godz_w'] != - 1)

				modify_time(2);

			break;

		case 'godz_p':

			if(Cookies['_godz_w'] != - 1 || Cookies['_godz_p'] != - 1)

				modify_time(1);

			break;

		case 'win_time_d':

		case 'win_time_a':

			if(Cookies['_win_time_a'] == 'on' || Cookies['_win_time_d'] == 'on')

			modify_date(1);

			

	

	}

}

		

		

/* COOKIES */



var Cookies = {

	init: function () {

		var allCookies = document.cookie.split('; ');

		for (var i=0;i<allCookies.length;i++) {

			var cookiePair = allCookies[i].split('=');

			this[cookiePair[0]] = unescape(cookiePair[1]);

		}

	},

	create: function (name,value,days) 
	{
		value = escape(value);
	

		if (days) {

			var date = new Date();

			date.setTime(date.getTime()+(days*24*60*60*1000));

			var expires = "; expires="+date.toGMTString();

		}

		else var expires = "";

		document.cookie = name+"="+value+expires+"; path=/";

		this[name] =value;

	},

	erase: function (name) {

		this.create(name,'',-1);

		this[name] = undefined;

	}

};

Cookies.init();





