var dati_crea_campo= new Array('nome','formato','tipo','numero_risposte','risposte','risp_predefinita','tipi_sezioni');
var div_crea_campo= new Array('ctrl_nome','ctrl_formato','ctrl_tipo','ctrl_risposte','ctrl_risposte','ctrl_risposte','ctrl_tipi_sezioni');

function ctrl_tot_campi(ACTION,url) {
	var array_dati= eval('dati_' + ACTION);
	var array_div= eval('div_' + ACTION);
	for(var i=0;i< array_dati.length;i++) {
		makeRequest(ACTION,array_dati[i],array_div[i],url);
	}
}

function updatediv(div, result) {
	if(document.getElementById(div)) {
		document.getElementById(div).innerHTML=result;
	}
}

function makeRequest(ACTION,campo,div,url,conferma,fields) {
	var i=parseInt(Math.random() * 100);
	var valore= new Array();
	valore[i]=false;

	// Xhr per Mozilla/Safari/Ie7
	if (window.XMLHttpRequest) {
		valore[i] = new XMLHttpRequest();
	}
	// per tutte le altre versioni di IE
	else if (window.ActiveXObject) {
		valore[i] = new ActiveXObject("Microsoft.XMLHTTP");
	}
	valore[i].open('POST', url, true);
	valore[i].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	valore[i].onreadystatechange= function() {
		if(valore[i].readyState == 4) {
			updatediv(div,valore[i].responseText);
		}
	}
	request=getQueryString(ACTION,campo,fields);

	if(conferma) {
		if(confirm(conferma))
			valore[i].send(request);
	}
	else
		valore[i].send(request);
}

function getQueryString(ACTION,campo,fields) {
	if(ACTION) {
		var qstr='&ACTION=' + ACTION;

		if(document.getElementById('COD_CAMPO'))
			qstr += '&COD_CAMPO=' + document.getElementById('COD_CAMPO').value;
		if(typeof(window['dati_' + ACTION]) != "undefined") {
			var array_dati= eval('dati_' + ACTION);
			var array_div= eval('div_' + ACTION);
			for(i=0;i< array_dati.length;i++) {
				var elemento= array_dati[i];
				if(document.getElementsByName(elemento).length==1 && document.getElementById(elemento)) {
					qstr += '&' + elemento + '=' + document.getElementById(elemento).value;
				}
				else if(document.getElementsByName(elemento).length==0) {//è un array
					if(elemento=='tipi_sezioni') {//sono checkbox
						var arr=get_array_for_checkboxes_with_name(elemento + '[]');
						for(var j=0;j<arr.length;j++) {
							if(document.getElementById(elemento + '_' + j)) {
								qstr += '&' + elemento + '[]=' + arr[j];
							}
						}
					}
					else {
						var arr=document.getElementsByName(elemento + '[]');
						for(var j=0;j<arr.length;j++) {
							if(document.getElementById(elemento + '_' + j)) {
								qstr += '&' + elemento + '[]=' + document.getElementById(elemento + '_' + j).value;
							}
						}
					}
				}
				else if(document.getElementsByName(elemento).length > 1) {//radio button
					var radio=get_radio_value_by_name(elemento);
					if(document.getElementById(elemento + '_' + radio))
						qstr += '&' + elemento + '=' + document.getElementById(elemento + '_' + radio).value;
				}
			}
		}
		if(campo)
			qstr += '&ctrl_' + campo + '=1';
		if(fields)
			qstr += fields;
	}
	//alert(qstr);
	return qstr;
}

function makeReqSezioni(campo,sposta) {
	if(document.getElementById('selezione_ordinamento')) {
		var tmp = document.getElementById('selezione_ordinamento').value;
		if(campo) {
			if(sposta) {
				tmp=tmp.replace(campo,sposta);
				tmp=tmp.replace(sposta + '_' + sposta,sposta + '_' + campo);
			}
			else {
				if(tmp.indexOf(campo) == -1) {
					tmp += '_' + campo;
				}
				else {
					tmp=tmp.replace('_' + campo,'');
				}
			}
			document.getElementById('selezione_ordinamento').value= tmp;
		}
	}
	valore=false;

	// Xhr per Mozilla/Safari/Ie7
	if (window.XMLHttpRequest) {
		valore = new XMLHttpRequest();
	}
	// per tutte le altre versioni di IE
	else if (window.ActiveXObject) {
		valore = new ActiveXObject("Microsoft.XMLHTTP");
	}
	valore.open('POST', 'ajax/seleziona_campi.php', true);
	valore.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	valore.onreadystatechange= function() {
		if(valore.readyState == 4) {
			updatediv('campi_selezionati',valore.responseText);
		}
	}
	valore.send('selezione_ordinamento=' + tmp);
}

function get_radio_value_by_name(nome) {
	var res;
	var nodes = document.getElementsByName(nome);
	for (var i = 0, n; n = nodes[i]; i++) {
		if (n.checked == true) {
			res= n.value;
		}
	}
	return res;
}

function get_array_for_checkboxes_with_name(name) {
	var new_arr = [];
	var nodes = document.getElementsByName(name);
	for (var i = 0, n; n = nodes[i]; i++) {
		if (n.checked == true) {
			new_arr.push(n.value);
		}
	}
	return new_arr;
}

function strstr (haystack, needle, bool) {
    var pos = 0;

    haystack += '';
    pos = haystack.indexOf(needle);
	if (pos == -1) {
        	return false;
    	}
	else {
		if (bool) {
			return haystack.substr( 0, pos );
		}
		else {
			return haystack.slice(pos);
		}
    	}
}

function evidenzia_riga(id) {
	var riga=document.getElementById(id);
	riga.style.backgroundColor='#FFFFAA';
	riga.style.color='#000000';
}

function disevidenzia_riga(id) {
	var riga=document.getElementById(id);
	riga.style.backgroundColor='#FFFFFF';
	riga.style.color='#666666';
}
//disevidenzia tutte le righe di una tabella <tr> che hanno gli id con l'inizio uguale
function disevidenzia_tutte_le_righe(inizio_id) {
	var els= document.getElementsByTagName("tr");
	var inizio_id_len=inizio_id.length;

	for(var i=0;i<els.length;i++) {
		if(els[i].id.substring(0,inizio_id_len)==inizio_id) {
			disevidenzia_riga(els[i].id);
		}
	}
}
