function imgMOver(obj) {	
	obj.style.cursor='pointer';
	obj.src = obj.src.replace('_i.gif', '_a.gif');
}

function imgMOut(obj) {
	obj.src = obj.src.replace('_a.gif', '_i.gif');
}

function openWindow(url, name, width, height)
{
    var str = "height=" + height + ",innerHeight=" + height;
    str += ",width=" + width + ",innerWidth=" + width;
    if (window.screen)
    {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;

        var xc = (aw - width) / 2;
        var yc = (ah - height) / 2;

        str = str + ",left=" + xc + ",screenX=" + xc;
        str = str + ",top=" + yc + ",screenY=" + yc;
    }
    str = str + ",scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no, resizable=no";
    var thewindow = window.open(url, name, str);
    thewindow.focus();
    return thewindow;
}

function selectFile(id) {
	loading('fileHandler','show_description.php?id='+id);
}

function getFile(id) {
	openWindow('getfile.php?id='+id, 'dldfile', 400, 400);
}

function dblGetFile(id) {
	if (!confirm('By double-clicking on a file, you can automatically download it.\nOpen download page for this file?')) {
		return "";
	}
	getFile(id);
}

function goWebsite(id) {
	
	if (id == '') {
		return '';
	}
	openWindow(id, 'website', 800,600);
}

function dbg_clear() {
	var theTable = document.getElementById('FILES_TABLE');
	if (!theTable) {
		alert('could not get ref. to FILES_TABLE.');
		return 0;
	}
	
	while (theTable.rows.length >= 2) {
		theTable.deleteRow(1);
	}
}

var cur_cat_id   = "-1";

var dfl_deep_cat = "1";			//weather to display files from child categories, or only from current subcategoy
var dfl_keywords = '';			//last keywords that were searched
var dfl_orderby  = 'name ASC';	//default order by ...
var dfl_category = '';			//default category
var dfl_sz_start = '';			//default size start
var dfl_sz_stop  = '';			//default size stop
var dfl_dt_start = '';			//default date start
var dfl_dt_stop  = '';			//default date stop
var cur_page     = 1;              //current page that shows on the screen


function onRecvNewPath(msg) {
	document.getElementById('HTML_PATH').innerHTML = unescape(msg);
}

function onRecvNewCategory(msg) {
	document.getElementById('CATEGORY_DIV').innerHTML = unescape(msg);
}

function selectCategory(catId) {
	ajax_mem_load('get_category_html_path.php?catid='+catId,'onRecvNewPath');
	ajax_mem_load('dump_categories.php?catid='+catId,'onRecvNewCategory');
	dfl_deep_cat = 'none';
	dfl_orderby = 'name ASC';
	dfl_category = catId;
	cur_cat_id = catId;
	goPage(1);
}

function reloadCategory() {
	selectCategory(cur_cat_id);
}

function onLoadPage(msg) {
	document.getElementById('FILES_TABLE_DIV').innerHTML = unescape(msg);
}

function loadPage(page, keywords, orderby, category, sz_start, sz_stop, dt_start, dt_stop) {
	var code="";
	code='show_files.php?page='+page+'&keywords='+escape(keywords)+
		'&orderby='+orderby+'&category='+category+
		'&sz_start='+sz_start+'&sz_stop='+sz_stop+'&dt_start='+escape(dt_start)+
		'&dt_stop='+dt_stop+'&deep='+dfl_deep_cat;
	ajax_mem_load(code, 'onLoadPage');
}

function goPage(page) {
	cur_page = page;
	loadPage(page, dfl_keywords, dfl_orderby, dfl_category, 
			dfl_sz_start, dfl_sz_stop, dfl_dt_start, dfl_dt_stop);
}

function doSearch() {

	var orderby = 'id';
	
	var keywords = document.getElementById('q').value;
	
	if (document.getElementById('by_category').checked) {
		var category = document.getElementById('categor_search').value;
	} else {
		var category = '';
	}
	
	if (document.getElementById('by_size').checked) {
		var sz_start = document.getElementById('size_start').value;
		if (sz_start == '') {
			alert('Please specify the minimum size of the file you are searching');
			document.getElementById('size_start').focus();
			return false;
		}
		
		sz_start = parseFloat(sz_start) * document.getElementById('size_start_unit').value;
		
		var sz_stop = document.getElementById('size_stop').value;
		if (sz_stop == '') {
			alert('Please specify the maximum size of the file you are searching');
			document.getElementById('size_stop').focus();
			return false;
		}
		
		sz_stop = parseFloat(sz_stop) * document.getElementById('size_stop_unit').value;
		
		if (sz_start > sz_stop) {
			alert('Input error: The minimum size is greater then the maximum size.');
			return false;
		}
		
	} else {
		sz_start = '';
		sz_stop  = '';
	}
	
	
	function str2date(s) {
		var dmy = s.split('-');
		var year = parseInt(dmy[0]);
		var month = parseInt(dmy[1]);
		var day = parseInt(dmy[2]);
		return Date.UTC(year,month,day,0,0,0,0);
	}
	
	if (document.getElementById('by_date').checked) {
		var dt_start = document.getElementById('date_start').value;
		if (dt_start == '') {
			alert('Please select the Date Starting for this search.');
			choose_datestart();
			return false;
		}
		var dt_stop = document.getElementById('date_stop').value;
		if (dt_stop == '') {
			alert('Please click on the Date Ending for this search.');
			choose_datestop();
			return false;
		}
		
		if (str2date(dt_stop)<str2date(dt_start)) {
			alert('Date start cannot be greater than date stop.');
			choose_datestop();
			return false;
		}
	} else {
		dt_start = '';
		dt_stop  = '';
	}
	
	/* alert('KEYWORDS: "'+keywords+'"\n'+
			'CATEGORY: "'+category+'"\n'+
			'SIZE START: "'+sz_start+'"\n'+
			'SIZE STOP: "'+sz_stop+'"\n'+
			'DATE START: "'+dt_start+'"\n'+
			'DATE STOP: "'+dt_stop+'"\n');
	*/
	dfl_keywords = keywords;
	dfl_orderby  = orderby;
	dfl_category = category;
	dfl_sz_start = sz_start;
	dfl_sz_stop  = sz_stop;
	dfl_dt_start = dt_start;
	dfl_dt_stop  = dt_stop;
	
	loadPage(1, keywords, orderby, category, sz_start, sz_stop, dt_start, dt_stop);
	
	return false;
}
	