// ADMIN
function add_tiny() {
	var box = $$('textarea.tiny_text');
	box.each(function(element) {
		//alert(element.id);
		tinyMCE.execCommand('mceAddControl',true,element.id);
	});
}
function remove_tiny() {
	var box = $$('textarea.tiny_text');
	box.each(function(element) {
		//alert(element.id);
		tinyMCE.execCommand('mceRemoveControl',false,element.id);
	});
}
function ajax_insert(table, extra, from) {
	var from_str = "";
	if (from) from_str = "&from=" + from;
	new Ajax.Updater('function_box', '/' + sf + 'admin/ajax/form_modify.asp?table=' + table + '&action=insert&id=' + extra + from_str, {
		onComplete: function(transport){
			add_tiny()
		}				 
	});
}
function ajax_modify(table, id, from) {
	var from_str = "";
	if (from) from_str = "&from=" + from;
	new Ajax.Updater('function_box', '/' + sf + 'admin/ajax/form_modify.asp?table=' + table + '&id=' + id + '&action=modify' + from_str, {
		onComplete: function(transport){	
			add_tiny();
		}
	});
}
function ajax_upload(table, id) {
	new Ajax.Updater('function_box', '/' + sf + 'admin/ajax/form_upload.asp?table=' + table + '&id=' + id, {});
}
function ajax_province(id, regione, provincia) {
	//alert(id + "|" + regione + "|" + provincia)
	$('regioni_' + id).update('Aggiornamento in corso...');
	new Ajax.Updater('regioni_' + id, '/' + sf + 'admin/ajax/regioni.asp?id=' + id + '&regione=' + regione + '&provincia=' + provincia, {});
}
function ajax_update_details(table, id, action) {
	var str_action = (action) ? "&action=" + action : "" ;
	new Ajax.Updater('details_' + id, '/' + sf + 'admin/ajax/update_details.asp?table=' + table + '&id=' + id + str_action, {});
	new Ajax.Updater('title_' + id, '/' + sf + 'admin/ajax/update_title.asp?table=' + table + '&id=' + id + str_action, {});
}
function ajax_tf(table, id, field, action) {
	new Ajax.Updater('tf_' + field + '_' + id, '/' + sf + 'admin/ajax/tf.asp?table=' + table + '&id=' + id + "&field=" + field + "&action=" + action, {
		onSuccess: function(t) {
			var resp = t.responseText;
			//alert(resp);
			if (resp.indexOf("alert_admin")>-1) {
				alert("Almeno un utente deve avere i privilegi di admin.");	
			}
		}		
	});
}
function ajax_duplicate(table, id) {
	$('update_text_' + id).update('Duplicazione in corso...');
	new Ajax.Updater('update_text_' + id, '/' + sf + 'admin/ajax/duplicate.asp?table=' + table + '&id=' + id, {
		onSuccess: function(t) {
			redir("?page=" + page, 500);
		}		 
	});
}
function ajax_delete_record(table, id, page, extra) {
	if (confirm('Attenzione! La cancellazione non puņ essere annullata, vuoi procedere?')) {
		$('update_text_' + id).update('Cancellazione in corso...');
		new Ajax.Updater('update_text_' + id, '/' + sf + 'admin/ajax/delete.asp?table=' + table + '&id=' + id, {
			onSuccess: function(t) {
				var str_extra = (extra) ? "&id=" + extra : "";
				redir("?page=" + page + str_extra, 500);
			}				 
		});
	}
}
function ajax_delete_record_agenti(table, id, page, extra) {
	//if (confirm('Attenzione! La cancellazione non puņ essere annullata, vuoi procedere?')) {
		$('update_text_' + id).update('Cancellazione in corso...');
		new Ajax.Updater('update_text_' + id, '/' + sf + 'admin/ajax/delete.asp?table=' + table + '&id=' + id, {
			onSuccess: function(t) {
				var str_extra = (extra) ? "&id=" + extra : "";
				redir("?page=" + page + str_extra, 500);
			}				 
		});
	//}
}
// UPLOAD
function start_upload(field) {
	//alert(field);
	$("upload_text_" + field).update("CARICAMENTO IN CORSO...");
}
function post_upload(text, file, file_name, field, table, id) {
	//alert(file);
	//field = field.replace("eventi_", "");
	$("upload_text_" + field).update(text);
	if (file_name != "") {
		var file_ext = file_name;
       	file_ext = file_ext.substring(file_ext.lastIndexOf(".") + 1, file_ext.length);
		var img_exts = "|gif|jpeg|jpg|png|";
		var str_link = file_name;
		if (img_exts.indexOf("|" + file_ext + "|")>-1) str_link = "";
		$("upload_form_" + field).update('<a href="/' + sf + file + '" target="_blank" id="link_'+ field +'">' + str_link + '</a>');
		if (img_exts.indexOf("|" + file_ext + "|")>-1) ajax_image('link_'+ field, file);
		$("upload_button_" + field).update('<a href="elimina_immagine" onclick="ajax_delete_image(\'' + table + '\', \'' + field + '\', \'' + id + '\'); return false;"><img src="/' + sf + 'admin/img/icon_delete.png" alt="Elimina" title="Elimina" /></a>');
	}
}
function ajax_delete_image(table, field, id, field_max_size, field_max_width, field_max_height, field_folder) {
	new Ajax.Updater('upload_text_' + field, '/' + sf + 'admin/ajax/delete_image.asp?table=' + table + '&field=' + field + '&id=' + id, {
	});
	$("upload_form_" + field).update('<form action="/' + sf + 'admin/ajax/upload.asp?field=' + field + '&table=' + table + '&id=' + id + '" method="post" enctype="multipart/form-data" target="upload_target_' + field + '" onsubmit="start_upload(\'' + field + '\');">\
		<input name="' + field + '" type="file" id="' + field + '" style="width: 200px;" />\
		<button type="submit"><img src="/' + sf + 'admin/img/btn_upload.gif" alt="upload" /></button>\
	</form>');
	$("upload_button_" + field).update('');
}
function ajax_image(id, source) {
	new Ajax.Updater(id, '/' + sf + 'admin/ajax/show_image.asp?id=' + source, {
	});
}

function ajax_admin_logout() {
	new Ajax.Updater('update_logout', '/' + sf + 'admin/ajax/login.asp?action=logout', {
		onSuccess: function(t) {
			redir("home.asp", 500);
		} 
	});
}

// SITE
function ajax_province(regione) {
	var provincia = $("provincia");
	//alert(provincia.value);
	num_option = provincia.options.length;
	for (a=num_option;a>=0;a--) {
		provincia.options[a]=null;
	}
	//alert(regione);
	new Ajax.Request("/" + sf + "ajax/select_province.asp?regione=" + regione, 
	{
		method:'get',
		onLoading: function() {
			provincia.options[0] = new Option("Loading...", "", true, false);
		},
		onSuccess: function(transport){
		var response = transport.responseText || "no response text";
		var provStr = response;
		//alert(provStr);
		var provArray = provStr.split("|");
		for (a=0;a<provArray.length;a++) {
			var thisProv = provArray[a];
			var thisProvArray = thisProv.split(",");
			var provValue = thisProvArray[0];
			var provName = thisProvArray[1];
			provincia.options[a]=new Option(provName, provValue, false, false);
		}
		 
		},
		onFailure: function(){
			provincia.options[0] = new Option("Errore!", "", true, false);
		}
	});
}

function ajax_form(form_id, action, update_id, redirect) {
	remove_tiny();
	//alert(form_id + "|" + action + "|" + redirect)
	if (update_id == "ajax_login_text") {
		//alert("asd");
		$(update_id).update('<div class="login_mex">Aggiornamento in corso...</div>');
	} else {
		$(update_id).update("Aggiornamento in corso...");
	}
		
	$(form_id).action = action;
	
	$(form_id).getMyBody=function(){
		var objIn=this.serialize(true);
		var valOut="";
		for (var s in objIn){
			var field_text = objIn[s];
			if (typeof(field_text)=='string') field_text = field_text.replace(/\+/g, "&#43;");
			valOut+=(valOut==""?"":"&")+s+"="+escape(field_text);
		}
		return(valOut);
	};

	$(form_id).request({
		 //onFailure: function() { .... },
		encoding:'iso-8859-1',
		postBody:$(form_id).getMyBody(),
		onSuccess: function(t) {
			var resp = t.responseText;
			//alert(resp);
            	$(update_id).update(resp);
			var js1 = resp.indexOf("js[");
			//alert(js1);
			if (js1 > -1 ) {
				var redirect_str = "";
				if (js1 > -1) {
					var js2 = resp.indexOf("]js");
					var js_str = resp.substr(js1 + 3, js2 - js1 - 3);
				}
				eval(js_str);
			}
			var num1 = resp.indexOf("goto[");
			if (redirect || num1 > -1 ) {
				//alert(redirect + "|" + t.responseText.indexOf("goto["))
				var redirect_str = "";
				if (redirect) redirect_str = redirect;
				if (num1 > -1) {
					var num2 = resp.indexOf("]goto");
					var redirect_str = resp.substr(num1 + 5, num2 - num1 - 5);
				}
				redir(redirect_str, 500);
			}

			//alert(form_id + action);

			if (form_id == "form_modify" && action.indexOf("from=agenti") > -1) {
				window.opener.location.href = window.opener.location.href;
				window.close();
			}
			
        }
		/*,
		onFailure: function() {
			alert("failure");
		},
		onException: function() {
			alert("exception");
		}*/

    });
	//return false;
}

function close_function_box() {
	setTimeout("$('function_box').update('');", 500);
}

function redir(address, time) {
	setTimeout("window.location = '" + address + "';", time);
}

function ajax_logout() {
	new Ajax.Updater('update_login', 'ajax/login.asp?action=logout', {
	});
	redir("home.html", 500);
}

// SITE SPECIFIC
function ajax_dettagli_richiesta(id, id_invio, archivio) {
	var str_archivio = (archivio) ? "&action2=archivio" : "";
	new Ajax.Updater('function_box', '/' + sf + 'admin/ajax/richieste.asp?action=dettagli&id=' + id + '&id_invio=' + id_invio + str_archivio, {});
}
function ajax_evasione(id) {
	$('update_text_' + id).update('Evasione in corso...');
	new Ajax.Updater('update_text_' + id, '/' + sf + 'admin/ajax/richieste.asp?action=evasione&id=' + id, {
		onSuccess: function(t) {
			var text = t.responseText;
			if (text.indexOf("intendo_riv") > -1) {
				ajax_dettagli_richiesta(id, 0);
			} else {
				redir("?", 500);
			}
			//$('update_in_evasione_' + id).update('In evasione');
		}			
	});
	//
}
function ajax_non_intendo_evadere(id) {
	$('update_text_' + id).update('Aggiornamento in corso...');
	new Ajax.Updater('update_text_' + id, '/' + sf + 'admin/ajax/richieste.asp?action=non_intendo_evadere&id=' + id, {});
	redir("?", 500);
}
function ajax_in_evasione(id) {
	$('update_text_' + id).update('Aggiornamento in corso...');
	new Ajax.Updater('update_text_' + id, '/' + sf + 'admin/ajax/richieste.asp?action=in_evasione&id=' + id, {
		onSuccess: function(t) {
			$('update_in_evasione_' + id).update('In evasione');
		}				 
	});
}
function ajax_richiesta(id) {
	//new Ajax.Updater('function_box', '?action=richiesta&id=' + id, {});
	new Ajax.Updater('function_box', '?action=richiesta&id=' + id, {});
}
function ajax_regioni(id, regione, provincia, id_richiesta) {
	$('regioni_' + id).update("Aggiornamento in corso...");
	new Ajax.Updater('regioni_' + id, '/' + sf + 'admin/ajax/regioni.asp?id=' + id + '&regione=' + regione + '&provincia=' + provincia + '&id_richiesta=' + id_richiesta, {});
}

function ajax_prodotti_speciali(id_produttore, id_prodotti) {
	$('prodotti').update("Aggiornamento in corso...");
	new Ajax.Updater('prodotti', '/' + sf + 'admin/ajax/prodotti_speciali.asp?id=' + id_produttore + '&action=' + id_prodotti, {});
}
function ajax_reinvia(table, id, id_invio, id_prs, id_pvs, id_grs, id_age) {
	$('update_reinvio_' + id_invio).update('Reinvio in corso...');
	//alert(table + ", " + id + ", " + id_invio +", " + id_prs + ", " + id_pvs + ", " + id_grs);
	new Ajax.Updater('update_reinvio_' + id_invio, '/' + sf + 'admin/ajax/invii.asp?action=reinvia&table='+ table +'&id=' + id + '&id_produttori=' + id_prs + '&id_rivenditori=' + id_pvs + '&id_gruppi=' + id_grs + '&id_agenti=' + id_age, {
		onSuccess: function(t) {
			//$('update_in_evasione_' + id).update('In evasione');
		}				 
	});
}

function ajax_approvazione(id, status) {
	$('update_approvazione_' + id).update('Aggiornamento in corso...');
	new Ajax.Updater('update_approvazione_' + id, '/' + sf + 'admin/ajax/approvazione.asp?action=modify&id=' + id + '&codice=' + status , {});
}

function ajax_delete_approvazione(id) {
	$('update_approvazione_' + id).update('Aggiornamento in corso...');
	new Ajax.Updater('update_approvazione_' + id, '/' + sf + 'admin/ajax/approvazione.asp?action=delete&id=' + id, {
		onSuccess: function(t) {
			$("link_elimina_" + id).hide();
			$("status_" + id).hide();
		}	
	});
}

function ajax_delete_approvazione_ar(id_approvazione, id) {
	$('update_approvazione_' + id).update('Aggiornamento in corso...');
	new Ajax.Updater('update_approvazione_' + id, '/' + sf + 'admin/ajax/approvazione.asp?action=delete&id=' + id_approvazione, {
		onSuccess: function(t) {
			$("link_elimina_" + id).hide();
			$("richiedi_" + id).show();
			$("status_" + id).update("");
		}	
	});
}

function ajax_richiedi_approvazione(id) {
	$('update_approvazione_' + id).update('Aggiornamento in corso...');
	new Ajax.Updater('update_approvazione_' + id, '/' + sf + 'admin/ajax/approvazione.asp?action=richiedi&id=' + id , {
		onSuccess: function(t) {
			$("richiedi_" + id).hide();
			$("link_elimina_" + id).show();
			$("status_" + id).update("In attesa");
			//$('update_in_evasione_' + id).update('In evasione');
		}			 
	});
}

function ajax_set_approvazione(id) {
	$('update_approvazione_' + id).update('Aggiornamento in corso...');
	if ($("id_produttore_" + id).checked) {
		new Ajax.Updater('update_approvazione_' + id, '/' + sf + 'admin/ajax/approvazione.asp?action=insert_ar&id=' + id + '&codice=In attesa' , {});
	} else {
		new Ajax.Updater('update_approvazione_' + id, '/' + sf + 'admin/ajax/approvazione.asp?action=delete_ar&id=' + id , {});
	}
}

function ajax_set_approvazione_produttori(id) {
	if ($F("approvazione_" + id) == "Seleziona") {
		$('update_approvazione_' + id).update('Selezionare il tipo di approvazione!');
	} else {
		$('update_approvazione_' + id).update('Aggiornamento in corso...');
		new Ajax.Updater('update_approvazione_' + id, '/' + sf + 'admin/ajax/approvazione.asp?action=insert_ar_produttori&id=' + id + '&codice=' + $F("approvazione_" + id), {
			onSuccess: function(t) {
				//alert("tr_" + id);
				setTimeout("$('tr_" + id + "').remove();", 500);
			}			
		});
	}
}

function ajax_set_approvazione_gruppo(id, id_gruppo) {
	$('update_approvazione_gruppi').update('Aggiornamento in corso...');
	new Ajax.Updater('update_approvazione_gruppi', '/' + sf + 'admin/ajax/approvazione.asp?action=set_gruppo&id=' + id + '&id_gruppi=' + id_gruppo, {});
}

function ajax_rimuovi_approvazione_gruppo(id, id_gruppo) {
	$('update_approvazione_gruppi').update('Aggiornamento in corso...');
	new Ajax.Updater('update_approvazione_gruppi', '/' + sf + 'admin/ajax/approvazione.asp?action=rimuovi_gruppo&id=' + id + '&id_gruppi=' + id_gruppo, {});
}


function ajax_copia_azienda(id) {
	$('update_text_' + id).update('Duplicazione in corso...');
	new Ajax.Updater('update_text_' + id, '/' + sf + 'admin/ajax/copia_azienda.asp?id=' + id, {
		onSuccess: function(t) {
			redir("?page=" + page, 500);
		}		 
	});
}

function ajax_select_azienda(tipologia) {
	$('select_id').update('Caricamento in corso...');
	new Ajax.Updater('select_id', '/' + sf + 'admin/ajax/select_tipologia.asp?action=' + tipologia, {
		onSuccess: function(t) {
			//redir("?page=" + page, 500);
		}		 
	});
}

function notifiche(what, topic_id) {
	$('update_notifiche').update('Aggiornamento in corso...');
	new Ajax.Updater('update_notifiche', '/' + sf + 'admin/ajax/notifiche.asp?action=' + what + '&id=' + topic_id, {
		onSuccess: function(t) {
			//redir("?page=" + page, 500);
			new Ajax.Updater('link_notifiche', '/' + sf + 'admin/ajax/notifiche.asp?id=' + topic_id);
		},
		onComplete: function(t) {
			setTimeout("$('update_notifiche').update('');", 1000)
		}
	});
}

function ajax_inserisci_azienda(regione, provincia) {
	$('inserisci_box').update('Caricamento in corso...');
	new Ajax.Updater('inserisci_box', '/' + sf + 'admin/ajax/approvazione_inserisci_azienda.asp?regione=' + regione + '&provincia=' + provincia, {
		onSuccess: function(t) {
			//redir("?page=" + page, 500);
		}		 
	});
}

function form_inoltra(id) {
	$('inoltra').update('Aggiornamento in corso...');
	new Ajax.Updater('inoltra', '/' + sf + 'admin/ajax/form_inoltra.asp?id=' + id, {
		onSuccess: function(t) {
			//setTimeout("$('inoltra').update('');", 1000)
		}		 
	});
}

function ajax_lista_prodotti(id_produttore, id_prodotti) {
	$('lista_prodotti_box').update('Aggiornamento in corso...');
	new Ajax.Updater('lista_prodotti_box', '/' + sf + 'admin/ajax/prodotti_utenti_richieste.asp?id=' + id_produttore + '&action=' + id_prodotti, {});
}

/*function ajax_insert_approvazione(produttore, rivenditore, status) {
	$('update_approvazione_' + id).update('Aggiornamento in corso...');
	new Ajax.Updater('update_approvazione_' + id, '/' + sf + 'admin/ajax/approvazione.asp?action=insert&id=' + id + '&codice=' + status , {});
}*/
