var LANG;
var ROOT_URL;
function empty( mixed_var ) {
	var key;
    if (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false || mixed_var === undefined) {
        return true;
    }
    if (typeof mixed_var == 'object') {
        for (key in mixed_var) {
            return false;
        }
        return true;
    }
    return false;
}
function selectAll(element,targetName) {
	var checks = document.getElementsByName(targetName);
	for (var i = 0; i < checks.length; i++) {
		checks[i].checked = element.checked;
	}
}
function getAbsoluteTop(element) {
	var top = 0;
	while (element != null) {
		top += element.offsetTop;
		element = element.offsetParent;
	}
	return top;
}

function getAbsoluteLeft(element) {
	var left = 0;
	while (element != null) {
		left += element.offsetLeft;
		element = element.offsetParent;
	}
	return left;
}

function toggle(elementId) {
	$(document).ready(function(){
		$("#"+elementId).slideToggle(700);
	});
}

function urlConfirm(msg,url) {
	var c = confirm(msg);
	if (c) {
		window.location = url;
	}
}

function redir(url) {
	window.location = url;
}

function redir_select(select) {
	var url = select.options[select.selectedIndex].value;
	if (!empty(url)) {
		window.location = url;
	}
}

function adm_cities_countryFilter(baseURL) {
$(document).ready(function(){
	$("#country_filter").change( function (i) {
		var sel = '';
		$("#country_filter option:selected").each(function () {
            sel = $(this).val();
		});
		window.location = baseURL.replace('.html','/country/'+sel+'.html'); 
	});
});
}

function adm_selectFilter(select,baseUrl,paramName) {
	var value = select.options[select.selectedIndex].value;
	window.location = baseUrl.replace('.html','/'+paramName+'/'+value+'.html');
}

function adm_inputFilter(input_id,baseUrl,paramName) {
	var value = document.getElementById(input_id).value;
	window.location = baseUrl.replace('.html','/'+paramName+'/'+value+'.html');
}

function adm_updateCityList(select, container, lang) {
	var selectedCountry = select.options[select.selectedIndex].value;
	$("#city_id_container").html('<img src="/media/images/admin/ajax-loader.gif" alt="" />');
	$("#city_id_container").load("/"+lang+"/ajax/cities/name/city_id/country/"+selectedCountry);
}

function adm_updateOfferCityList(select, container, lang) {
	var multiple = select.getAttribute('multiple');
	if (empty(multiple)) {
		var selectedCountry = select.options[select.selectedIndex].value;
		$("#city_id_container").html('<img src="/media/images/admin/ajax-loader.gif" alt="" />');
		$("#city_id_container").load("/"+lang+"/ajax/cities/name/city_id/country/"+selectedCountry);
	}
}

function adm_enableSwitchPosition(object_id) {
	var stat = document.getElementById('pos_static_'+object_id);
	var dyn = document.getElementById('pos_dynamic_'+object_id);
	var input = document.getElementById('pos_input_'+object_id);
	stat.style.display = 'none';
	dyn.style.display = 'block';
	input.select();
}

function adm_hotels_move(object_id,baseUrl) {
	var new_fr_position = Number(document.getElementById('pos_input_'+object_id).value);
	var old_fr_position = Number(document.getElementById('current_pos_'+object_id).value);
	var direction = '';
	if (new_fr_position == old_fr_position) {
		alert('Не сте променяли позицията.');
		return;
	}
	if (new_fr_position > old_fr_position) {
		direction = 'down';
	} else {
		direction = 'up';
	}
	window.location = baseUrl.replace('.html','/direction/'+direction+'/friendly_pos/'+new_fr_position+'.html');
}

function adm_filterOfferHotels(lang, always_update_cities) {
	var country_select = document.getElementById('filter_country');
	var country_id = country_select.options[country_select.selectedIndex].value;
	if (empty(country_id)) {
		return;
	}
	var country_select = document.getElementById('filter_city');
	var city_id = country_select.options[country_select.selectedIndex].value;
	var update_cities = false;
	if (empty(city_id) || !empty(always_update_cities)) {
		update_cities = true;
	}
	if (update_cities) {
		document.getElementById('cities_container').innerHTML = '<img alt="" src="/media/images/admin/ajax-loader.gif" />';
	}
	document.getElementById('hotels_add_list').innerHTML = '<img alt="" src="/media/images/admin/ajax-loader.gif" />';
	if (update_cities) {
		$("#cities_container").load("/"+lang+"/ajax/admoffercities/country/"+country_id);
		$("#hotels_add_list").load("/"+lang+"/ajax/admofferspopulatehotels/country/"+country_id);
	} else {
		$("#hotels_add_list").load("/"+lang+"/ajax/admofferspopulatehotels/country/"+country_id+"/city/"+city_id);
	}
}

function adm_addOfferAssoc(hotel_id) {
	var hidden_container = document.getElementById('hotels');
	if (hidden_container.value.indexOf('h'+hotel_id+';') == -1) {
		hidden_container.value += 'h'+hotel_id+';';
	}
	var hotelTitle = document.getElementById('hotel_'+hotel_id).innerHTML;
	$("#hotel_container_"+hotel_id).fadeOut(500);
	if (empty(document.getElementById('hotel_delete_container_'+hotel_id))) {
		$("#hotels_added_list").append('<div style="display: none;" id="hotel_delete_container_'+hotel_id+'"><img alt="" onclick="admOfferRemove('+hotel_id+')" src="/media/images/admin/delete.gif"/> '+hotelTitle+'</div>');
	}
	$("#hotel_delete_container_"+hotel_id).fadeIn(500);
}

function admOfferRemove(hotel_id) {
	if (!empty(document.getElementById('hotel_container_'+hotel_id))) {
		$('#hotel_container_'+hotel_id).fadeIn(500);
	}
	var hidden_container = document.getElementById('hotels');
	hidden_container.value = hidden_container.value.replace('h'+hotel_id+';', '');
	$("#hotel_delete_container_"+hotel_id).fadeOut(500);
}

function adm_offerSelectType(select) {
	var value = select.options[select.selectedIndex].value;
	switch (value) {
		case 'vacation':
			document.getElementById('country_tr').style.display = '';
			document.getElementById('city_tr').style.display = '';
			document.getElementById('hotels_tr').style.display = '';
			document.getElementById('countries_tr').style.display = 'none';
			document.getElementById('cities_tr').style.display = 'none';
		break;
		case 'excursion':
			document.getElementById('country_tr').style.display = 'none';
			document.getElementById('city_tr').style.display = 'none';
			document.getElementById('hotels_tr').style.display = 'none';
			document.getElementById('countries_tr').style.display = '';
			document.getElementById('cities_tr').style.display = '';
		break;
	}
}

function adm_offereditLoadCities(select,lang) {
	$("#cities_add_list").html('<img src="/media/images/admin/ajax-loader.gif" alt="" />');
	document.getElementById('cities_added_list').innerHTML = '';
	document.getElementById('cities').value = '';
	var n = select.options.length;
	var selected_countries = new Array();
	for (i = 0; i < n; i++) {
		if (select.options[i].selected) {
			selected_countries.push(select.options[i].value);
		}
	}
	var country_list = escape(selected_countries.toString());
	$("div#cities_add_list").load('/'+lang+'/ajax/admofferspopulatecities/countries/'+country_list);
}

function adm_addOfferCityAssoc(city_id) {
	var hidden_container = document.getElementById('cities');
	if (hidden_container.value.indexOf('c'+city_id+';') == -1) {
		hidden_container.value += 'c'+city_id+';';
	}
	var cityTitle = document.getElementById('city_'+city_id).innerHTML;
	$("#city_container_"+city_id).fadeOut(500);
	if (empty(document.getElementById('city_delete_container_'+city_id))) {
		$("#cities_added_list").append('<div style="display: none;" id="city_delete_container_'+city_id+'"><img alt="" onclick="admOfferCityRemove('+city_id+')" src="/media/images/admin/delete.gif"/> <span id="city_remove_'+city_id+'">'+cityTitle+'</span></div>');
	}
	$("#city_delete_container_"+city_id).fadeIn(500);
}

function admOfferCityRemove(city_id) {
	if (empty(document.getElementById('city_container_'+city_id))) {
		var value = document.getElementById('city_remove_'+city_id).innerHTML;
		$('#cities_add_list').append('<div style="display: none;" id="city_container_'+city_id+'"><img onclick="adm_addOfferCityAssoc('+city_id+')" alt="" src="/media/images/admin/add.gif"/><span id="city_'+city_id+'">'+value+'</span></div>');
	}
	$('#city_container_'+city_id).fadeIn(500);
	var hidden_container = document.getElementById('cities');
	hidden_container.value = hidden_container.value.replace('c'+city_id+';', '');
	$("#city_delete_container_"+city_id).fadeOut(500);
}

function getExcelData(submit) {
	var offerId = submit.id.replace('data_loader_','');
	var textarea = document.getElementById('excelData_'+offerId);
	var rawText = textarea.value;
	if (empty(rawText)) {
		return;
	}
	textarea.value = '';
	//clear (if any) old table data 
	$('#priceTable_'+offerId).empty().append('<tbody></tbody>');
	var rows;
	var cols;
	var data = new Array();
	rows = rawText.split(String.fromCharCode(10));
	var maxRows = rows.length;
	if (empty(rows[(rows.length-1)])) {
		maxRows--;
	}
	var maxCols = 0;
	for (i = 0; i < maxRows; i++) {
		cols = rows[i].split(String.fromCharCode(9));
		maxCols = maxCols < cols.length ? cols.length : maxCols;
		data[i] = cols;
	}
	var tr;
	var td;
	var input;
	var value;
	
	var tableContainer = document.getElementById('priceTable_'+offerId);
	var table = tableContainer.childNodes[0]; //TBODY
	
	// function buttons
	var tr = document.createElement('tr');
	tr.setAttribute('id', 'control_row_'+offerId);
	
	// controls container td
	var td = document.createElement('td');
	td.setAttribute('id', 'td_add_buttons_'+offerId);
	td.setAttribute('nowrap', 'nowrap');
	
	//Add row button
	var input = document.createElement('input');
	input.setAttribute('type', 'button');
	input.setAttribute('value', '[+]');
	input.setAttribute('title', 'Добави ред');
	input.setAttribute('id', 'btn_add_row_'+offerId);
	td.appendChild(input);
	
	td.appendChild(document.createTextNode(' '));
	
	//Add column button
	var input = document.createElement('input');
	input.setAttribute('type', 'button');
	input.setAttribute('value', '[+]');
	input.setAttribute('title', 'Добави колона');
	input.setAttribute('id', 'btn_add_col_'+offerId);
	td.appendChild(input);
	
	//append buttons
	tr.appendChild(td);
	
	for (i = 0; i < maxCols; i++) {
		var td = document.createElement('td');
		td.setAttribute('id', 'td_col'+i+'_'+offerId);
		var input = document.createElement('input');
		input.setAttribute('type', 'button');
		input.setAttribute('value', '[-]');
		input.setAttribute('title', 'Изтрий колоната');
		input.setAttribute('id', 'btn_del_col'+i+'_'+offerId);
		td.appendChild(input);
		tr.appendChild(td);
	}
	table.appendChild(tr);
	for (i = 0; i < maxRows; i++) {
		var tr = document.createElement('tr');
		tr.setAttribute('id', 'tr_r'+i+'_'+offerId);
		//controls
		var td = document.createElement('td');
		td.setAttribute('id', 'td_row'+i+'_'+offerId);
		var input = document.createElement('input');
		input.setAttribute('type', 'button');
		input.setAttribute('value', '[-]');
		input.setAttribute('title', 'Изтрий реда');
		input.setAttribute('id', 'btn_del_row'+i+'_'+offerId);
		td.appendChild(input);
		tr.appendChild(td);
		//data
		for (ii = 0; ii < maxCols; ii++) {
			var td = document.createElement('td');
			td.setAttribute('id', 'td_r'+i+'c'+ii+'_'+offerId);
			var input = document.createElement('input');
			input.setAttribute('type', 'text');
			input.setAttribute('name', 'input_r'+i+'c'+ii);
			input.className = 'priceTableField';
			value = empty(data[i][ii]) && data[i][ii] != 0 ? '' : data[i][ii];
			input.setAttribute('value', value);	
			td.appendChild(input);
			tr.appendChild(td);
		}
		table.appendChild(tr);
	}
	//delete columns
	for (i = 0; i < maxCols; i++) {
		$('#btn_del_col'+i+'_'+offerId).click(function() {
			var cid = $(this).attr('id').replace('btn_del_col', '');
			$('td[id$=c'+cid+']').each(function() {
				$(this).remove();
			});
			$("td#td_col"+cid).remove();
		});
	}
	//delete rows 
	for (i = 0; i < maxRows; i++) {
		$('#btn_del_row'+i+'_'+offerId).click(function() {
			var rid = $(this).attr('id').replace('btn_del_row', '');
			$('#tr_r'+rid).remove();
		});
	}
	//add columns 
	$('#btn_add_col_'+offerId).click(function() {
		var colId = Number($('#cols_'+offerId).val());
		//add cells
		$("tr[id^='tr_r'][id$='_"+offerId+"']").each( function() {
			var row_id = $(this).attr('id').replace('tr_r','').replace('_'+offerId, '');
			var td = document.createElement('td');
			td.setAttribute('id', 'td_r'+row_id+'c'+colId+'_'+offerId);
			var input = document.createElement('input');
			input.setAttribute('type', 'text');
			input.setAttribute('name', 'input_r'+row_id+'c'+colId);
			input.className = 'priceTableField';
			td.appendChild(input);
			$(this).append(td);
		});
		//add delete column button
		var input = document.createElement('input');
		input.setAttribute('type', 'button');
		input.setAttribute('value', '[-]');
		input.setAttribute('title', 'Изтрий колоната');
		input.setAttribute('id', 'btn_del_col'+colId+'_'+offerId);
		var td = document.createElement('td');
		td.setAttribute('id', 'td_col'+colId+'_'+offerId);
		td.appendChild(input);
		$('#control_row_'+offerId).append(td);
		$('#btn_del_col'+colId+'_'+offerId).click(function() {
			var cid = $(this).attr('id').replace('btn_del_col', '');
			$('td[id$=c'+cid+']').each(function() {
				$(this).remove();
			});
			$("td#td_col"+cid).remove();
		});
		colId++;
		$('#cols_'+offerId).val(colId);
	});
	
	//add rows 
	$('#btn_add_row_'+offerId).click(function() {
		var rowId = Number($('#rows_'+offerId).val());
		//create table row
		var tr = document.createElement('tr');
		tr.setAttribute('id', 'tr_r'+rowId+'_'+offerId);
		$('#priceTable_'+offerId).append(tr);
		//delete row button
		var td = document.createElement('td');
		td.setAttribute('id', 'td_row'+rowId+'_'+offerId);
		var input = document.createElement('input');
		input.setAttribute('type', 'button');
		input.setAttribute('value', '[-]');
		input.setAttribute('title', 'Изтрий реда');
		input.setAttribute('id', 'btn_del_row'+rowId+'_'+offerId);
		td.appendChild(input);
		$("#tr_r"+rowId+'_'+offerId).append(td);
		$("#btn_del_row"+rowId+'_'+offerId).click(function() {
			var rid = $(this).attr('id').replace('btn_del_row', '');
			$('#tr_r'+rid).remove();
		});
		// row cells & inputs
		$("input[id^='btn_del_col'][id$='_"+offerId+"']").each(function() {
			var colId = $(this).attr('id').replace('btn_del_col','').replace('_'+offerId,'');
			var input = document.createElement('input');
			input.setAttribute('type', 'text');
			input.setAttribute('name', 'input_r'+rowId+'c'+colId);
			input.className = 'priceTableField';
			var td = document.createElement('td');
			td.setAttribute('id', 'td_r'+rowId+'c'+colId+'_'+offerId);
			td.appendChild(input);
			$("#tr_r"+rowId+'_'+offerId).append(td);
		});
		rowId++;
		$('#rows_'+offerId).val(rowId);
	});
	document.getElementById('rows_'+offerId).value = maxRows;
	document.getElementById('cols_'+offerId).value = maxCols;
}

function res_selectOfferType(select) {
	var value = select.options[select.selectedIndex].value;
	if (empty(value)) {
		return false;
	}
	$('#country').html('<img alt="" src="'+ROOT_URL+'media/images/admin/ajax-loader.gif" />');
	var url = ROOT_URL+LANG+'/ajax/reservationselecttype/type/'+value;
	$('#country').load(url);
}

function res_selectOfferCountry(select) {
	var country_id = select.options[select.selectedIndex].value;
	var offer_type = $("select[name='offer_type']").val();
	if (empty(country_id) || empty(offer_type)) {
		return false;
	}
	$('#city').html('<img alt="" src="'+ROOT_URL+'media/images/admin/ajax-loader.gif" />');
	var url = ROOT_URL+LANG+'/ajax/reservationselectcountry/type/'+offer_type+'/country/'+country_id;
	$('#city').load(url);
}



















