function showList(alink, key, statusOn, statusOff) {

 	var list = document.getElementById('livello' + 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, tID, sez) {

	ajaxRun(filename + '.php?id=' + tID + '&task=' + sez, 'ajaxdata', sez);
}

function searchTrip(value) {
	ajaxEval('ajaxViaggio.php?task=viaggio&search='+value);
}

function ajaxRun(request, div, sez) {

            var oXmlHttp = zXmlHttp.createRequest();
            oXmlHttp.open("get", request + "&div=" + div, true);
            oXmlHttp.onreadystatechange = function () {
                if (oXmlHttp.readyState == 4) {
                    if (oXmlHttp.status == 200) {
						if (sez == 'mappa') {
							data = oXmlHttp.responseText.split("||");
							showInfo(data[0], div);
							if (data[2] > 0 && data[3] > 0) {
								setvisible('', 0, 'map');
								GoogleLoad(data[1],data[2],data[3]);
							}
						}
						else {
							var data = oXmlHttp.responseText;
							showInfo(data, div);
						}		
                    } else {
                        showInfo("Impossibile recuperare i dati: " + oXmlHttp.statusText, div); //statusText is not always accurate
                    }
                }
            };
            oXmlHttp.send(null);
}

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 ajaxSend(request, params) {

	var oXmlHttp = zXmlHttp.createRequest();
    oXmlHttp.open("post", request, true);
	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	var data = "";
	if (typeof(params) == "string") {
		data = params;
	} 
	else {
		for (var key in params) {
			if (data != "")
				data += "&";
			data += key + "=" + escape(params[key]).replace(/\+/, "%2B");
		}
	}
	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(data);
}

function quickInsert(data, class) {

	var url = "admin_process.php?type=" + class;
	var data = data.split(",");
	var params = 'ajax=true&status=diretto';
	for (i=0; i<data.length; i=i+2)
		params += '&' + data[i] + '=' + data[i+1];
	ajaxSend(url, params);
}

function addOption(id, label, value) {

	var sel = eval('document.forms[0].'+ id);
	var n = sel.options.length;
	sel.options[n] = new Option (label,value);
	sel.selectedIndex = n;
	sel.focus();
}

function showOrder() {

	var obj = document.forms[0].required.value;
	obj = obj.replace(/_req/, '');
	ajaxEval('ajaxLoad.php?task=ordine&id=' + obj);
}

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.style.display = 'block';
	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 visibile (id, stato) {

	if (document.getElementById && document.getElementById(id)) {
		var dom = document.getElementById(id);
		if (stato == 'si') dom.style.display = 'block';
		else if (stato == 'no') dom.style.display = 'none';
	}
	return;
}

function toggleTransl(div) {
	
	var langs = new Array ("it","en");
	if (document.getElementById(langs[1] + div).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(langs[i] + div).style.display = status;
}

function activeChoice(div, numq) {

	var langs = new Array ("it","en");

	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 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 mapCoordinate(oForm) {
	
	var value = oForm.maplink.value;
	var chop = value.split('&ll=');
	var grades = chop[1].split('&');
	var coord = grades[0].split(',');
	oForm.mapLAT.value = coord[0];
	oForm.mapLONG.value = coord[1];
	oForm.maplink.value = '';
}

function getCookie(c_name)
{
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
  		if (c_start!=-1) {
    		c_start=c_start + c_name.length+1;
    		c_end=document.cookie.indexOf(";",c_start);
    		if (c_end==-1) c_end=document.cookie.length;
    		return unescape(document.cookie.substring(c_start,c_end));
    	}
  	}
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function ultimaCat(oTag) {

	oTag.value = getCookie('ultimaCat');
}
