function showList(alink, key, statusOn, statusOff) {

 	var list = document.getElementById(key);

	if (statusOn == alink.innerHTML) {
		list.style.visibility = 'hidden';
		list.style.display = 'none';
		alink.innerHTML = statusOff;
	}
	else {
		list.style.visibility = 'visible';
		list.style.display = 'block';
		alink.innerHTML = statusOn;
	}
	return false;
}

function setArrow(sez, thisBut) {

	var bg = document.getElementById('dynacontent');
	var bord = document.getElementById('ajaxcontent');
	bg.className = ' left colorlab' + thisBut + ' ';
	bord.className = ' right colorbord' + thisBut + ' ';
}

function getContent(filename, thisBut, tID, sez, lang) {

	ajaxRun(filename + '.php?tID=' + tID + '&q=' + sez + '&l=' + lang, 'ajaxcontent', sez, thisBut);
}

function ajaxRun(request, div) {

            var oXmlHttp = zXmlHttp.createRequest();
            oXmlHttp.open("get", request + "&div=" + div, true);
			//init('ajaxloading');
            oXmlHttp.onreadystatechange = function () {
                if (oXmlHttp.readyState == 4) {
					//init('ajaxloading');
                    if (oXmlHttp.status == 200) {
						var data = oXmlHttp.responseText;
						if (data.indexOf('js|') != -1) {
							data = data.split('js|');
							eval(data[1]);
						}
						else showInfo(data, div);
                    } else {
                        showInfo("Impossibile recuperare i dati: " + oXmlHttp.statusText, div); //statusText is not always accurate
                    }
                }
				else {
					//setvisible('ajax', 0, 'loading');
					//text('...', 'ajax', 'loading');
				}
            };
            oXmlHttp.send(null);
}

function init(div)
{
	var dom = document.getElementById(div);
	var domL = document.getElementById(div + 'L');
	if (dom.style.display == 'block') {
		dom.style.display = 'none';
		dom.innerHTML = domL.innerHTML;
	}
	else dom.style.display = 'block';
}

function replaceAll (text, strTarget, strSubString) {

	var intIndexOfMatch = text.indexOf(strTarget);
	while (intIndexOfMatch != -1){
		text = text.replace(strTarget, strSubString);
		intIndexOfMatch = text.indexOf(strTarget);
	}
	return(text);
}

function showInfo(sText, div) {

	var htmlent = new Array("&agrave;", "&egrave;", "&eacute;", "&igrave;", "&ograve;", "&oacute;", "&ugrave", "&euro;", "", "&#39;");
	var utf8 = new Array("à", "è", "é", "ì", "ò", "ó", "ù", "€", "\\", "'");
	var place = document.getElementById(div);
	for (i=0; i < htmlent.length; i++) sText = replaceAll(sText, utf8[i], htmlent[i]);
	place.innerHTML = sText;

}

function initHidden(div, liv) {

	for (i=0; i < liv; i++) {
		var dom = document.getElementById(div + i).style;
		dom.display = 'none';
	}
}

function text (char, div, actual) {
	var dom = document.getElementById(div + actual);
	dom.innerHTML += ' ' + char + ' ';
}

function setvisible(div, numdiv, actual) {

	for (i=0; i < numdiv; i++) document.getElementById(div + i).style.display = 'none';
	var dom = document.getElementById(div + actual).style;
	dom.display = 'block';
}

function toggleTransl(div) {

	var langs = new Array ("it","en","es","de","fr");
	if (document.getElementById(div + langs[1]).style.display == 'none') {
		if (window.navigator.userAgent.indexOf('MSIE') != -1) status = 'inline';
		else status = 'table-row';
	}
	else status = 'none';
	for (i=1; i < langs.length; i++) document.getElementById(div + langs[i]).style.display = status;
}

function activeChoice(div, numq) {

	var langs = new Array ("it","en","es","de","fr");

	for (i=0; i < numq; i++) {
		var dom = document.getElementById(div + '_' + i);

		if (dom.disabled == true) {
			if (document.getElementById(div + '_td'))
				document.getElementById(div + '_td').className = 'active';
			dom.disabled = false;
			dom.className = 'active';
			for (x=0; x < langs.length; x++)
				document.getElementById(div + '_' + langs[x] + '_' + i).className = 'active';
		}
		else if (dom.disabled == false) {
			if (document.getElementById(div + '_td'))
				document.getElementById(div + '_td').className = 'deactive';
			dom.disabled = true;
			dom.className = 'deactive';
			for (x=0; x < langs.length; x++)
				document.getElementById(div + '_' + langs[x] + '_' + i).className = 'deactive';
		}
	}
}

function activeRow(div, numq) {

	for (i=0; i < numq; i++) {
		var dom = document.getElementById(div + '_' + i);

		if (dom.className == 'deactive') {
			dom.className = 'active';
		}
		else if (dom.className == 'active') {
			dom.className = 'deactive';
		}
	}
}

function doTesti(id) {
	var cod = document.getElementById(id).value;
	if (cod % 2 == 0 || cod % 2 == 1 && cod > 0)
		document.location.href='admin_form.php?type=testi&action=modifica&field=' + cod;
	else
		document.location.href='admin_list.php?type=testi&action=modifica&field=txtID&q=' + cod;
}

function doOverview(cat,grp) {
	var catID = eval('document.forms[0].'+cat+'.value');
	var faseID = eval('document.forms[0].'+grp+'.value');
	if (catID > 0 && faseID > 0)
		document.location.href='admin_form.php?type=partita&action=overview&cat=' + catID + '&grp=' + faseID;
	else
		alert('Selezionare una categoria e una tipologia di fase');
}

function doPartite(id) {
	var cod = document.getElementById(id).value;
	if ((cod % 2 == 0 || cod % 2 == 1) && cod <= 12 && cod > 0)
		document.location.href='admin_form.php?type=partita&num=' + cod;
	else if (cod > 12)
		alert ('Numero di partite troppo alto da gestire');
	else
		alert('Inserire numero partite da aggiungere');
}

function CopyMatch(riga) {

	var rigaprec = riga - 1;
	var rigasucc = riga;
	var campi = Array ('id_strutt_', 'id_fase_', 'data_', 'id_campo_');
	numcampi = campi.length;
	for (i=0; i < numcampi; i++) {
		var precTUR = eval('document.partita.'+campi[i]+rigaprec+'.value');
		var succTUR = eval('document.partita.'+campi[i]+rigasucc);
		if (precTUR) succTUR.value = precTUR;
	}
}

function delRow(rowid, tabid) {
	var thistab = document.getElementById(tabid);
	if (thistab.rows[0].id != 'tabdesc') var i = 0;
	else var i = 1;
	while (thistab.rows[i].id != rowid) i = i + 5;
	var index = i;
	for (j=0; j<5; j++) { thistab.deleteRow(index); }
}

function paganti() {
	var tot = document.getElementById('tot');
	var persone = 0;
	var letti = document.forms['pernotto'].letti.value.split(',');
	if (tot.value == "") tot.value = 0;
	for (i=1; i <= 12; i++) {
		arrCamere = document.getElementById('camera'+i);
		num = letti[i-1];
		if (arrCamere.value > 0) persone += parseInt(arrCamere.value) * num;
	}
	tot.value = parseInt(persone);
}

function ajaxEval(request) {

	var oXmlHttp = zXmlHttp.createRequest();
    oXmlHttp.open("get", request, true);
    oXmlHttp.onreadystatechange = function () {
    	if (oXmlHttp.readyState == 4) {
    		if (oXmlHttp.status == 200) {
				eval(oXmlHttp.responseText);
        	} else {
        		alert("Impossibile recuperare i dati: " + oXmlHttp.statusText); //statusText is not always accurate
        	}
     	}
    };
    oXmlHttp.send(null);
}

function newTeam(obj, iddest) {
	ajaxEval('ajaxLoad.php?class=squadre&search=reset&dest='+iddest);
	obj.value='';
}

function searchTeam(value, iddest) {

	if (value.length > 2) {
		var time = new Date();
		ajaxEval('ajaxLoad.php?class=squadre&time='+Math.floor(time.getTime())+'&search='+value+'&dest='+iddest);
	}
}