function table_ext_edit(record_id, table_id){
	$$('.static_'+table_id+'_'+record_id).each(function (el) {
		return $(el).hide();
	});
	
	$$('.edit_'+table_id+'_'+record_id).each(function (el) {
		return $(el).show();
	});
	
	$$('.cal_trigger_'+table_id+'_'+record_id).each(function (el) {
		return $(el).show();
	});
	
	
	$('edit_img_'+table_id+'_'+record_id).setAttribute('src','i/ico/accept.png');
	$('edit_img_'+table_id+'_'+record_id).onclick = function() { table_save_resp(record_id,table_id); }
	
	$('del_img_'+table_id+'_'+record_id).setAttribute('src','i/ico/cancel.png');
	//$('del_img_'+record_id).observe('click', table_cancel_resp);
}

function table_ext_cancel(record_id, table_id){
	$$('.static_'+table_id+'_'+record_id).each(function (el) {
		return $(el).show();
	});
	
	$$('.edit_'+table_id+'_'+record_id).each(function (el) {
		return $(el).hide();
	});
	
	$$('.cal_trigger_'+table_id+'_'+record_id).each(function (el) {
		return $(el).hide();
	});
	
	
	$('edit_img_'+table_id+'_'+record_id).setAttribute('src','i/ico/page_edit.png');
	$('edit_img_'+table_id+'_'+record_id).onclick = function() { table_ext_edit(record_id,table_id); }
	
	$('del_img_'+table_id+'_'+record_id).setAttribute('src','i/ico/cross.png');
	
}

function table_ext_save(table_id){
	var _table_row = new Array();
	
	$$('.edit_'+table_id+'_add').each(function (el) {
		var table_elem = Array(2);
		table_elem[1] = $(el).getAttribute('name');
		table_elem[2] = $(el).value;
		
		_table_row.push(table_elem);
	});
	
	var result = _table_row.toJSON();
	
	new Ajax.Request('/post.php?module=services&action=common_table_update&table='+table_id+'&dataset='+result,
	{
			onSuccess: function(transport){
				window.location.reload();
			}
	});
}

function table_save_resp(record_id,table_id) {
	
	var _table_row = new Array();
	$$('.edit_'+table_id+'_'+record_id).each(function (el) {
		var table_elem = Array(2);
		table_elem[1] = $(el).getAttribute('name');
		table_elem[2] = $(el).value;
		
		_table_row.push(table_elem);
	});
	
	var result = _table_row.toJSON();
	
	new Ajax.Request('/post.php?module=services&action=common_table_update&table='+table_id+'&dataset='+result,
	{
			onSuccess: function(transport){
				window.location.reload();
			}
	});
	
}
function table_cancel_resp(record_id,table_id) {
	

}

function select_car(name, description, price, filename) {
	$('car_price').value = price;
	
	$('car_name').update(name);
	
	$('car_description').update(description);
	$('car_thmb_preview').setStyle({
		width:'96px',
		height:'96px',
		backgroundImage: 'url('+filename+')'
		});
	
	calc();
}


	var from = 0;
	var to = 0;
	var days = 0;
	
	function get_price(days_count){
		var i = 0;
		var days = 0;
		for (i=0;i<=5;i++)
		{
			var unlimited = $('unlimited').checked;
			//alert(unlimited);
			if (unlimited) {
				days = parseInt($('car_price_ulim_'+i).getAttribute('days'));
				if ( days >= days_count) {
					var _price = parseInt($('car_price_ulim_'+i).value);
					return _price;
				}
			}else{
					days = parseInt($('car_price_'+i).getAttribute('days'));
				if ( days >= days_count) {
					var _price = parseInt($('car_price_'+i).value);
					return _price;
				}
			}
		}
		

		return 0;
	}
	
	function calc(price,days){
		if(price != 0 ) {
			var price_total = days * price;
			
			$('price_day').update(price);
			$('__price_day').value = price;
			$('price_total').update(price_total);
			$('__price_total').value = price_total;
		}
	}
	
	function check_date(from){
		var timestamp = new Date();
		
		var diff = from-(timestamp/1000);
		if (diff < -86400){
			return false;
		}
		return true;
		
		//var order_timestamp = Date.UTC(timestamp.getFullYear(),mon-1,day);	
	}
	
	function changed_from(_from, sender) {
		
			from = _from;
			if (!check_date(from)){ return; }
			
			$$('.date_from_selector').each(function (el) {
				el.setStyle({ backgroundImage:'url(i/booking/cars/calendar_inactive.png)' });
				
				if (el.getAttribute("content") == 1)
					el.update('<strong>'+el.getAttribute("content")+'</strong>');
				else
					el.update(el.getAttribute("content"));
			});
			
			$(sender).setStyle({ backgroundImage:'url(i/booking/cars/calendar_active.png)' });
			$(sender).update('');
			
			if ((to != 0) && (to > from)) {
				days = Math.round((to-from)/(24*3600));	//days
				$('days').value = days;
				$('days_count').update(days);
				calc(get_price(days),days);
			}
			else {
				$('days').value = 0;
				$('days_count').update('0');
			}
	};
	function changed_to(_to, sender) {
			to = _to;
			
			if (to < from) {return;}
			
			$$('.date_to_selector').each(function (el) {
				el.setStyle({ backgroundImage:'url(i/booking/cars/calendar_inactive.png)' });
				
				if (el.getAttribute("content") == 1)
					el.update('<strong>'+el.getAttribute("content")+'</strong>');
				else
					el.update(el.getAttribute("content"));
			});
			
			$(sender).setStyle({ backgroundImage:'url(i/booking/cars/calendar_active.png)' });
			$(sender).update('');
			
			if ((from != 0) && (to > from)) {
				days = Math.round((to-from)/(24*3600));
				$('days').value = days;
				$('days_count').update(days);
				calc(get_price(days),days);
			}
			else {
				$('days').value = 0;
				$('days_count').update('0');
			}
	  };
	  
	function update_calc(){
		if ((from != 0) && (to > from)) {
				days = Math.round((to-from)/(24*3600));
				$('days').value = days;
				$('days_count').update(days);
				calc(get_price(days),days);
			}
		else {
				$('days').value = 0;
				$('days_count').update('0');
		}
	}
