/* ----------------- */
/* © Hyperborea 2010 */
/* ----------------- */

function checkInteger(element) {
	if (element != null) {
		if (!element.value.match(/^\d*$/)) element.value = "";
	}
}


function isEmpty(v) {
	return (v == null || v == '');
}

function removeTableRow(el) {
	el = $(el);
	while (el.tagName.toLowerCase() != 'tr') el = el.getParent();
	el.dispose();
}

function removefavorite(id, el) {
	if (!confirm('Eliminare la scheda dai preferiti?')) return;
	var theurl = 'favorite.html?id='+id;
	var myAjax = new Request.HTML({
		url: theurl,
		method: 'get',
		onComplete: function() {
			if (this.response.text == 'DELETED') {
					removeTableRow(el);
			}
		}
	}).send();
}

function setfavorite(id, el) {
	var theurl = 'favorite.html?id='+id;
	var myAjax = new Request.HTML({
		url: theurl,
		method: 'get',
		onComplete: function() {
			if (this.response.text == 'ADDED') {
					el.src='resources/love_del.png';
			}
			if (this.response.text == 'DELETED') {
					el.src='resources/love_add.png';
			}
		}
	}).send();
}



function fill(theString, thePart) {
	var start = theString.toLowerCase().indexOf(thePart.toLowerCase());
	if (start == -1) return theString;
	var length = thePart.length;
	var pre = theString.substr(0,start);
	var middle = theString.substr(start,length);
	var post = theString.substr(start+length);
	return pre+'<span class="stringa_cercata">'+middle+'</span>'+post;
}



function removeTendina() {
	var tendina = $('div_filtro_tendina');
	if (tendina != null) tendina.dispose();
}


function isValidKey(ev) {
	var code = ev.code;
	var key = ev.key;
//	console.log(code);
	if (code >= 97 && code <= 122) return true;  //a-z
	if (code >= 65 && code <= 90) return true;   //A-Z
	if (code >= 48 && code <= 57) return true;   //0-9
//	if (['à','è','ì','ò','ù','é'].contains(key)) return true;
	if (key == 'enter') return true;
	if (key == 'backspace') return true;
	if (key == 'space') return true;
	if (key == 'up') return true;
	if (key == 'down') return true;
	if (key == 'left') return true;
	if (key == 'true') return true;
	return false;
}

function ricercaPronostico(ev, element, mode) {
	return ricerca(ev, element, mode, 'classifica.html?');
}

function ricercaPronosticoturno(ev, element, mode, turno) {
	return ricerca(ev, element, mode, 'classificaturno.html?idturno='+turno+"&");
}


function ricerca(ev, element, mode, dest) {
	removeTendina();
	ev = new Event(ev);
	if (!isValidKey(ev)) {
		ev.stop();
		return;
	}
	var val = element.getProperty('value').trim();
	if (val.length<3) return;
	if (ev.key == 'enter') {
		var theurl = "searchPronostico.html?search="+val;
		//var struct = [{scheda:'scheda1',autore:'autore1',idscheda:'2'},{scheda:'scheda2',autore:'autore2',idscheda:'5'}];
		new Request.HTML({
			url: theurl,
			onSuccess: function(a,b,struct){
				showTendina(JSON.decode(struct), val, mode, dest);
			}
		}).send();
	}
		
}

function showTendina(struct, val, mode, dest) {
	var tendina = new Element('div');
	tendina.addClass('filtro_tendina');
	tendina.id = 'div_filtro_tendina';
	tendina.injectAfter($('filtroRicercaPronostico'));
	
	if (mode == null) 
		var url = dest+'search=';
	else
		url = 'partiteiiturno.html?mode='+mode+"&id=";
	if (struct.length==0) {
		var span = new Element('span');
		var ul = new Element('ul');
		tendina.appendChild(ul);
		var li = new Element('li');
		ul.appendChild(li);
		var span2 = new Element('span');
		span2.addClass('nome_pronostico');
		span2.innerHTML = 'nessun pronostico trovato';
		li.appendChild(span2);
	} else {
		var ul = new Element('ul');
		tendina.appendChild(ul);
		struct.each(function(item) {
			var li = new Element('li');
			ul.appendChild(li);
			var a = new Element('a', {'href': url+item.idScheda});
			li.appendChild(a);
			var span1 = new Element('span');
			span1.addClass('nome_pronostico');
			span1.innerHTML = fill(item.scheda, val);
			var span2 = new Element('span');
			span2.addClass('autore_pronostico');
			span2.innerHTML = ' di '+fill(item.cognome + ' '+ item.nome, val);
			a.appendChild(span1);
			a.appendChild(span2);
		});
	}
//	removeTendina.delay(5000);
}










function fillRandom(fireAllEvents) {
	var all = $$('input.golprono');
	var errors = $$('input.erroregol');
	all.extend(errors);
	all.each(function(el) {
			el.removeClass('erroregol');
			el.value = getGolRandom();
		}
	);	
}


function getGolRandom() {
	var a;
	a = Math.random();
	return Math.round(4*a*a*a);
}


