function umple_select_tari(data_start_presel, durata_presel) {
	var tip_oferta = $("input[name='tip-oferta']:checked").val();
	if(!tip_oferta) tip_oferta = 'standard';
	var tara = $("#tara");
	var oval = tara.val();
	tara.empty();
	for(i in tari)
		if(tari[i][tip_oferta])
			tara.append('<option value="' + i + '">' + tari[i].nume + '</option>');
	if(oval && tari[oval][tip_oferta]) tara.val(oval);
	umple_select_loc(tara.val(), data_start_presel, durata_presel);
}

function umple_select_loc(tara, data_start_presel, durata_presel) {
	var sel = $("#loc");
	var oval = sel.val();
	sel.empty();
	if(!tari[tara]) return;
	var loc = tari[tara].loc;
	var tip_oferta = $("input[name='tip-oferta']:checked").val();
	if(tara == 'tenerife' || tip_oferta == 'last-minute' || tip_oferta == 'oferte-speciale') sel.append('<option value="" selected="selected">Toate statiunile</option>');
	for(var i in loc) if(!tip_oferta || loc[i][tip_oferta])
		sel.append('<option value="'+i+'">'+loc[i].nume_localitate+'</option>');
	if(tara != 'tenerife' && tip_oferta != 'last-minute' && !tip_oferta == 'oferte-speciale') sel.append('<option value="">Toate statiunile</option>');
	if(oval && loc[oval] && loc[oval][tip_oferta] && tip_oferta != 'last-minute') sel.val(oval);
	umple_select_date(tara, sel.val(), data_start_presel, durata_presel);
}

function in_interval(d) {
	var ts = sqldate(d);
	return ts >= interval[0] && ts < interval[1];
}

function umple_select_date_cu_date(date, data_start, durata) {
	var sel = $("#data_start").empty();
	var loc = $('#loc').val()
	var tip_oferta = $("input[name='tip-oferta']:checked").val();
	var html = new Array();
	if((tip_oferta != 'standard' && tip_oferta != 'early-booking') || (loc && window.location.href.indexOf('sezon=Revelion') == -1)) { html.push('<option value="">Toate plecarile</option>'); }
	for(var i in date) {
		if(i != '_toate_datele' && (typeof(interval)!=='undefined'?in_interval(i):true))
			html.push('<option value="' + i + '"' + (data_start && data_start == i ? ' selected="selected"' : '') + '>' + (dataRomana?dataRomana(i):i) + '</option>');
	}
	sel.html(html.join(''));
	umple_select_durate($('#tara').val(), $('#loc').val(), sel.val(), durata);
}

function umple_select_date(tara, loc, data_start_presel, durata_presel) {
	var sel = $("#data_start").empty();
	var tip_oferta = $("input[name='tip-oferta']:checked").val();
	if(tip_oferta in {'early-booking':'', 'last-minute':'', 'oferte-speciale':''}) url_oferta = tip_oferta + '/'; else url_oferta = '';
	if(!loc) loc = '_toate_loc';
	if(!tip_oferta) tip_oferta = 'standard';
	if(!tari[tara].loc[loc]) tari[tara].loc[loc] = {};
	if(tari[tara].loc[loc].date && tari[tara].loc[loc].date[tip_oferta])
		umple_select_date_cu_date(tari[tara].loc[loc].date[tip_oferta], data_start_presel, durata_presel);
	else {
		sel.append('<option value="">Loading...</option>');
		$("#durata").html('<option value="">Loading...</option>');
		$.getJSON(baseurl + '/' + url_oferta + tara + (loc == '_toate_loc'?'':'/' + loc) + '/pachete' + (data_start_presel?'?data_start='+data_start_presel+(durata_presel?'&durata='+durata_presel:''):''), function(data) {
			if(!data.url_localitate) data.url_localitate = '_toate_loc';
			if(!data.tip_oferta) data.url_localitate = '_toate';
			if(!tari[data.url_tara].loc[data.url_localitate].date)
				tari[data.url_tara].loc[data.url_localitate].date = {};
			tari[data.url_tara].loc[data.url_localitate].date[data.tip_oferta] = data.date;
			var tara = $('#tara').val();
			var loc = $('#loc').val();
			if(!loc) loc = '_toate_loc';
			var tip_oferta = $("input[name='tip-oferta']:checked").val();
			if(!tip_oferta) tip_oferta = 'standard';
			if(tari[tara] && tari[tara].loc && tari[tara].loc[loc] &&
			   tari[tara].loc[loc].date && tari[tara].loc[loc].date[tip_oferta])
				umple_select_date_cu_date(tari[tara].loc[loc].date[tip_oferta], data.data_start, data.durata);
		});
	}
}

function umple_select_durate(tara, loc, data, durata) {
	var sel = $("#durata").empty();
	var tip_oferta = $("input[name='tip-oferta']:checked").val();
	if(!tip_oferta) tip_oferta = 'standard';
	if(!loc) loc = '_toate_loc';
	var html = new Array();
	if(!data || data == '_toate_datele') { html.push('<option value="">Toate duratele</option>'); data = '_toate_datele'; }
	if(tari[tara] && tari[tara].loc && tari[tara].loc[loc] && tari[tara].loc[loc].date &&
	   tari[tara].loc[loc].date[tip_oferta] && tari[tara].loc[loc].date[tip_oferta][data]) {
		var durate = tari[tara].loc[loc].date[tip_oferta][data];
		for(i in durate)
			if(i != '_toate_duratele')
				html.push('<option value="'+i+'"'+(durata&&durata==i?' selected="selected"':'')+'>'+i+' nopti</option>');
		sel.html(html.join(''));
		umple_select_adulti(tara, loc, data, parseInt($(sel).val()));
	}
}

function umple_select_adulti(tara, loc, data, durata) {
	var tip_oferta = $("input[name='tip-oferta']:checked").val();
	if(!tip_oferta) tip_oferta = 'standard';
	if(!loc) loc = '_toate_loc';
	if(!data) data = '_toate_datele';
	if(!durata) durata = '_toate_duratele';
	var html = new Array();
	if(tari[tara] && tari[tara].loc && tari[tara].loc[loc] && tari[tara].loc[loc].date &&
	   tari[tara].loc[loc].date[tip_oferta] && tari[tara].loc[loc].date[tip_oferta][data]
	    && tari[tara].loc[loc].date[tip_oferta][data][durata]) {
		adulti = tari[tara].loc[loc].date[tip_oferta][data][durata];
		for(i in adulti) html.push('<option value="'+i+'">'+i+'</option>');
		adultihtml = html.join('');
		$(".adulti").each(function(i, elm) {
			var oval = $(elm).val();
			$(elm).html(adultihtml);
			if(oval && tari[tara].loc[loc].date[tip_oferta][data][durata][oval]) $(elm).val(oval);
			var pelm = $('#camera'+i);
			umple_select_copii(tara, loc, data, durata, $(elm).val(), pelm);
		});
	}
}

function umple_select_copii(tara, loc, data, durata, adulti, elm) {
	var tip_oferta = $("input[name='tip-oferta']:checked").val();
	if(!tip_oferta) tip_oferta = 'standard';
	if(!loc) loc = '_toate_loc';
	var html = new Array();
	if(tari[tara] && tari[tara].loc && tari[tara].loc[loc] && tari[tara].loc[loc].date &&
	   tari[tara].loc[loc].date[tip_oferta] && tari[tara].loc[loc].date[tip_oferta][data]
	    && tari[tara].loc[loc].date[tip_oferta][data][durata]
		&& tari[tara].loc[loc].date[tip_oferta][data][durata][adulti]) {

		var oval = $(".copii", elm).val();
		var copii = tari[tara].loc[loc].date[tip_oferta][data][durata][adulti];
		for(i in copii) html.push('<option value="'+i+'">'+(i==0?'-':i)+'</option>');
		var celm = $(".copii", elm);
		celm.html(html.join(''));
		if(oval && tari[tara].loc[loc].date[tip_oferta][data][durata][adulti][oval]) {
			celm.val(oval);
		}
		arata_varste($(elm).attr('idx'), parseInt(celm.val()));
	}
}

function arata_varste(cam, ncopii) {
	var tara       = $('#tara').val();
	var loc        = $('#loc').val();
	if(!loc) loc = '_toate_loc';
	var tip_oferta = $("input[name='tip-oferta']:checked").val();
	var data_start = $('#data_start').val();
	var durata     = $('#durata').val();
	var nadulti    = $('#camera'+cam+' .adulti').val();
	for(var i = 0; i < 2; i++) {
		var sel = ".varsta-"+cam+"-"+(i+1);
		if(i <= ncopii - 1) {
			if(tari[tara].loc[loc].date && tari[tara].loc[loc].date[tip_oferta] &&
			   tari[tara].loc[loc].date[tip_oferta][data_start] &&
			   tari[tara].loc[loc].date[tip_oferta][data_start][durata] &&
			   tari[tara].loc[loc].date[tip_oferta][data_start][durata][nadulti] &&
			   tari[tara].loc[loc].date[tip_oferta][data_start][durata][nadulti][ncopii]) {
				var t = tari[tara].loc[loc].date[tip_oferta][data_start][durata][nadulti][ncopii];
				var html = new Array();
				var varsta_min = parseInt(t[i * 2]);
				var varsta_max = parseInt(t[i * 2 + 1]);
				for(var j = varsta_min; j <= varsta_max; j++) {
					html.push('<option value="'+j+'">'+(j<1?'&lt;1':j)+'</option>');
				}
				var elm = $(sel+' select');
				var oval = elm.val();
				elm.html(html.join(''));
				if(oval >= varsta_min && oval <= varsta_max) elm.val(oval);
			}
			$(sel).show();
		} else {
			$(sel).hide();
		}
	}
}

function arata_camere(ncamere) {
	for(var i=0; i<4; i++) {
		var e = $('#camera'+i);
		if(i>=ncamere) e.hide();
		else e.show();
	}
}

function winHeight() {
	if(typeof(window.top.innerHeight) != 'undefined') { return window.top.innerHeight; }
	else if(typeof(window.top.document.documentElement != 'undefined') &&
			typeof(window.top.document.documentElement.clientHeight) != 'undefined' &&
			window.top.document.documentElement.clientHeight != 0) { return window.top.document.documentElement.clientHeight; }
	return window.top.document.getElementsByTagName('body')[0].clientHeight;
}

function loadingCharter() {
	$('div', window.top.document).hide();
	var h = winHeight();
	var pt = 40 * (h - 150) / 100;
	$(window.top.document.body).append('<div style="cursor: wait; text-align: center; font-size: 14px; font-weight: bold; color: #4d492b; padding-top: '+pt+'px; height: '+(winHeight()-pt)+'px; background: #fff url(\'/charter/static/graphics/degradeu-searching.gif\') repeat-x top">Va rugam asteptati, cautam cea mai buna oferta pentru vacanta Dvs...<br><br><div style="background: url(\'/charter/static/graphics/searching.gif\') no-repeat 50% 50%; height: 30px">&nbsp;</div><br><br><br><img alt="CMB" src="/css/img/logo_cmb.png"><br><span style="font-size: 12px">Alegerea celor exigenti</span></div>');
}

$(function() {
	$('#formular-cautare form').submit(loadingCharter);
	$('#charter-sidebar-formular form').submit(loadingCharter);
});

