var livesearch = new Object();

livesearch.timeout = false;
livesearch.lastValue = '';
livesearch.cookieName = 'liveSearchParams';
livesearch.inputField = false;
livesearch.inputParam = false;
livesearch.showResult = false;
livesearch.progress   = false;
livesearch.autoactivate = false;

livesearch.filter = function(force) {
	if (livesearch.timeout) clearTimeout( livesearch.timeout );
	if (force) {
		livesearch.execute();
	} else if (livesearch.autoactivate) {
		livesearch.timeout = setTimeout('livesearch.execute()',500);
	}
}

livesearch.execute = function() {
	livesearch.timeout = false;
	var text = livesearch.inputField.value;
	if ( text == '') {
		livesearch.showResult.innerHTML = '';
		livesearch.lastValue = '';
		livesearch.progress.style.visibility = 'hidden';
	} else if ( text != livesearch.lastValue) {
		var params = livesearch.inputParam.value;
		livesearch.lastValue = text;
		livesearch.progress.style.visibility = 'visible';
		XML.load('/livesearch.cgi?params='+params+'&search='+escape(text),livesearch.delivery,false);
	}
}

livesearch.delivery = function(doc) {
	if ( livesearch.inputField.value != livesearch.lastValue) return;
	var root = doc.documentElement;
	livesearch.showResult.innerHTML = root.getAttribute('result');
	livesearch.progress.style.visibility = 'hidden';
}

livesearch.setoption = function(f) {
	var ctrl = f.nodeName.toLowerCase();
	var ov = livesearch.inputParam.value.toString();
	var thisv = f.value;
	if ( ctrl == 'input' ) {
		ov = ov.split(thisv).join('');
		if ( f.getAttribute('type').toLowerCase() == 'checkbox' ) {
			if ( f.checked ) ov += thisv;
		}
	} else if ( ctrl == 'select' ) {
		if ( f.getAttribute('id') == 'typeofsearchQT') {
			ov = ov.replace(/[0-9]+/g,'');
		} else {
			ov = ov.split(thisv).join('');
		}
		ov += thisv;
	}
	livesearch.inputParam.value= ov;
	livesearch.lastValue = '';
	// uncomment the following line if you want live search to be triggered by option changes:
	// livesearch.filter(true);
	if (window.setPermanentCookie) setPermanentCookie( livesearch.cookieName, ov );
}

livesearch.options = function(v) {
	var ov = livesearch.inputParam.value.toString();
	v = v.toString();
	if ( v.match(/^[0-9]+$/)) {
		ov = ov.replace(/[0-9]/g,'');
		ov += v;
	} else if ( v.match(/^[a-z]+$/)) {
		ov = ov.replace(/[a-z]/g,'');
		ov += v;
	} else if ( v.match(/^[A-Z]+$/)) {
		ov = ov.replace(/[A-Z]/g,'');
		ov += v;
	} else {
		ov = v;
	}
	livesearch.inputParam.value= ov;
	livesearch.lastValue = '';
	livesearch.filter(true);
	if (window.setPermanentCookie) setPermanentCookie( livesearch.cookieName, ov );
}

livesearch.init = function() {
	livesearch.inputField = document.getElementById('livesearchField');
	livesearch.inputParam = document.getElementById('livesearchParams');
	livesearch.showResult = document.getElementById('searchByTextLiveResult');
	livesearch.progress   = document.getElementById('livesearchProgress');
	livesearch.timeout = false;
	livesearch.lastValue = '';
	livesearch.inputField.value = '';
	livesearch.autoactivate = ! document.getElementById('livesearchActivation');
	var baseOpts = (window.getCookie) ? getCookie( livesearch.cookieName ) : '';
	baseOpts = baseOpts ? baseOpts : '';
	var defOpts = livesearch.inputParam.value.toString();
	if (! defOpts.match(/[A-Z]/)) defOpts += baseOpts.match(/[A-Z]/) ? baseOpts.replace(/[^A-Z]/g,'') : 'FR';
	if (! defOpts.match(/[a-z]/)) defOpts += baseOpts.match(/[a-z]/) ? baseOpts.replace(/[^a-z]/g,'') : 'it';
	if (! defOpts.match(/[0-9]/)) defOpts += baseOpts.match(/[0-9]/) ? baseOpts.replace(/[^0-9]/g,'') : '20';
	// defOpts = defOpts.replace(/X/,'');
	// if (window.location.href.toString().match(/\.xml$/i) ) defOpts = defOpts.replace(/([A-Z]+)/,"X$1");
	if (window.setPermanentCookie) setPermanentCookie( livesearch.cookieName, defOpts );
	livesearch.inputParam.value = defOpts;
	var si = new Array();
	for ( var i = 1; document.getElementById('typeofsearch'+i); i++ ) si[si.length] = document.getElementById('typeofsearch'+i);
	if (si.length > 0 ) {
		var setFieldActive = function( f ) {
				var t = f.nodeName.toLowerCase();
				if (t == 'option') {
					f.parentNode.value = f.value;
				} else if (t == 'input') {
					t = f.type.toLowerCase();
					if (t == 'radio' ) {
						f.checked = true;
					}
					if (t == 'checkbox' ) {
						f.checked = true;
					}
				}
			};
		for ( var i = 0; i < si.length; i++ ) {
			var v = si[i].getAttribute('value');
			if ( v.match(/^[0-9]+$/)) {
				if ( defOpts.replace(/[^0-9]/g,'') == v ) setFieldActive( si[i] );
			} else if ( v.match(/^[a-z]+$/)) {
				if ( defOpts.replace(/[^a-z]/g,'') == v ) setFieldActive( si[i] );
			} else if ( v.match(/^[A-Z]+$/)) {
				if ( defOpts.replace(/[^A-Z]/g,'') == v ) {
					setFieldActive( si[i] );
				} else if ( defOpts.indexOf(v) >= 0 ) {
					setFieldActive( si[i] );
				}
			}
		}
	}
}

