var isNav, isIE;
var strOnLoad = "";
if (parseInt(navigator.appVersion) >= 4) {
	if (navigator.appName == 'Netscape') {
		isNav = true;
		layerRef = 'document.getElementById(';
		styleRef = ').style';
		anchorText = 'text';
	} else {
		if (navigator.appName == 'Nav') {
			isNav = true;
			layerRef = 'document.layers[';
			styleRef = ']';
			anchorText = 'innerText';
		} else {
			isIE = true;
			layerRef = 'document.all[';
			styleRef = '].style';
			anchorText = 'innerText';
		}
	}
}
// IE
//var event=event||window.event;
//if (window.event){
//	event=window.event;
//}
// Firefox
//if (!event){
//	event=window.event;
//}

function cambia(ImageName, ImageFile) {
	ImageName.src = ImageFile;
}

/* funzione che carica un valore da una finestra di pop-up alla principale */
var carica_valore_status = true;
function carica_valore() {
	if (carica_valore_status != true) {
		return false;
	}
	OPEN_FORM_NAME = "document.NAView";// + document.forms[0].name; //nome form
	// principale della pop-up:
	// "document.nome_della_form"
	FORM_NAME = "opener.document.NAView";// + opener.document.forms[0].name;
	// //nome form principale della
	// finestra padre:
	// "opener.document.nome_della_form"
	fk = eval(FORM_NAME + ".CHIAVE_IMPORTA.value");
	fkref = eval(FORM_NAME + ".CHIAVE_IMPORTA_REF.value");
	// controllo se valorizzata
	// alert(eval(OPEN_FORM_NAME + "." + fkref+ ".value"));
	if (eval(OPEN_FORM_NAME + "." + fkref + ".value") == '') {
		alert("Nessun valore selezionato.");
		return false;
	}
	eval(FORM_NAME + "." + fk + ".value=" + OPEN_FORM_NAME + "." + fkref
			+ ".value");
	eval(FORM_NAME + ".FK_" + fk + "_descrizione.value=" + OPEN_FORM_NAME
			+ ".FK_" + fkref + "_descrizione.value");
	// alert("fk=" + fk + " fkrel=" + fkref + " a=" + eval(FORM_NAME + ".FK_" +
	// fk + "_descrizione.value"));
	window.close();
}

function apri_fk(TAB, KEY, KEYREF) {
	document.NAView.CHIAVE_IMPORTA.value = KEY;
	document.NAView.CHIAVE_IMPORTA_REF.value = KEYREF;
	// str = contextPath + "/main/app.NAView?TAB=" + TAB + "&KEY=" + KEY +
	// "&CHILD=YES";
	str = contextPath + "/main/app.NAView?TAB=" + TAB + "&KEY=" + KEYREF
			+ "&CHILD=YES";
	// window.open(str,'apri_fk','scrollbars=yes,status=no,width=700,height=450');
	window.open(str, 'apri_fk',
			'scrollbars=yes,status=no,resizable=yes,width=700,height=450');
}

function menu_gestione(TAB, KEY) {
	if (TAB == 'documenti') {
		document.form_gestione.action = contextPath + "/main/app.DOCGest";
	} else {
		document.form_gestione.action = contextPath + "/main/app.NAView";
		document.form_gestione.TAB.value = TAB;
		document.form_gestione.KEY.value = KEY;
	}
	document.form_gestione.submit();
}

function vedi_doc(doc) {
	var str = contextPath + DOCUMENT_PATH + doc;
	if (doc.charAt(0) == '/') {
		str = doc;
	}
	if (doc.search('http') != -1) {
		str = doc;
	}
	var winDoc = window
			.open(
					str,
					'vediDoc',
					'width='
							+ (screen.width - 100)
							+ ',height='
							+ (screen.height - 100)
							+ ', personalbar=no,l ocationbar=no, toolbar=no, statusbar=no, menubar=no, scrollbars=yes, resizable=yes');
	winDoc.moveTo(50, 50);
}

function visualizza(nome_layer, stato) {
	eval(layerRef + '["' + nome_layer + '"]' + styleRef + '.visibility = "'
			+ stato + '"');
}

function ora() {

	var mesi = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09",
			"10", "11", "12");

	var oggi = new Date();
	Data = ((oggi.getDate() < 10) ? ("0" + oggi.getDate()) : oggi.getDate());
	Mese = mesi[oggi.getMonth()];
	anni = oggi.getFullYear();
	Ora = ((oggi.getHours() < 10) ? ("0" + oggi.getHours()) : oggi.getHours());
	Min = ((oggi.getMinutes() < 10) ? ("0" + oggi.getMinutes()) : oggi
			.getMinutes());

	dataString = Data + "-" + Mese + "-" + anni;

	return dataString;

}

function colora_campi_readOnly() {

	if (document.forms[0]) {
		var form_name = document.forms[0].name;

		for (i = 0; i < eval("document." + form_name + ".elements.length"); i++) {
			// alert(eval("document." + form_name + ".elements[i].name"));
			if (eval("document." + form_name
					+ ".elements[i].type == 'text' && document." + form_name
					+ ".elements[i].readOnly==true")) {
				eval("document." + form_name + ".elements[i].id='read'");
			}
			if (eval("document." + form_name
					+ ".elements[i].type == 'text' && document." + form_name
					+ ".elements[i].readOnly==false")) {
				eval("document." + form_name + ".elements[i].id='work'");
			}

			if (eval("document." + form_name
					+ ".elements[i].type == 'select-one' && document."
					+ form_name + ".elements[i].disabled==true")) {
				eval("document." + form_name + ".elements[i].id='read'");
			}
			if (eval("document." + form_name
					+ ".elements[i].type == 'select-one' && document."
					+ form_name + ".elements[i].disabled==false")) {
				eval("document." + form_name + ".elements[i].id='work'");
			}

			if (eval("document." + form_name
					+ ".elements[i].type == 'textarea' && document."
					+ form_name + ".elements[i].readOnly==true")) {
				eval("document." + form_name + ".elements[i].id='read'");
			}
			if (eval("document." + form_name
					+ ".elements[i].type == 'textarea' && document."
					+ form_name + ".elements[i].readOnly==false")) {
				eval("document." + form_name + ".elements[i].id='work'");
			}
		}
	}
}

function ajaxRequest() {
	// Usage:
	// new ajaxRequest()
	var activexmodes = [ "Msxml2.XMLHTTP", "Microsoft.XMLHTTP" ] // activeX
	// versions
	// to check
	// for in IE
	if (window.ActiveXObject) { // Test for support for ActiveXObject in IE
		// first (as XMLHttpRequest in IE7 is broken)
		for ( var i = 0; i < activexmodes.length; i++) {
			try {
				return new ActiveXObject(activexmodes[i])
			} catch (e) {
				// suppress error
			}
		}
	} else if (window.XMLHttpRequest) // if Mozilla, Safari etc
		return new XMLHttpRequest()
	else
		return false
}

function sleep(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}
function windowOpenDocumenti(url) {
	var width = 1000;
	var height = 550;
	var left = (screen.width / 2) - (width / 2);
	var top = (screen.height / 2) - (height / 2);
	window.open(url, '',
			'locationbar=no, titlebar=no, scrollbars=yes, status=no, resizable=yes, width='
					+ width + ',height=' + height + ',left=' + left + ',top='
					+ top);
}
function windowOpenDocumentiMedium(url) {
	var width = 700;
	var height = 350;
	var left = (screen.width / 2) - (width / 2);
	var top = (screen.height / 2) - (height / 2);
	window.open(url, '',
			'locationbar=no, titlebar=no, scrollbars=yes, status=no, resizable=yes, width='
					+ width + ',height=' + height + ',left=' + left + ',top='
					+ top);
}
function windowOpenTemplateEdit(url) {
	var width = 900;
	var height = 550;
	var left = (screen.width / 2) - (width / 2);
	var top = (screen.height / 2) - (height / 2);
	window.open(url, 'template',
			'locationbar=no, titlebar=yes, scrollbars=yes, status=no, resizable=yes, width='
					+ width + ',height=' + height + ',left=' + left + ',top='
					+ top);
}
function windowOpenCostanti(url) {
	var width = 900;
	var height = 350;
	var left = (screen.width / 2) - (width / 2);
	var top = (screen.height / 2) - (height / 2);
	window.open(url, '',
			'locationbar=no, titlebar=no, scrollbars=yes,status=no,width='
					+ width + ',height=' + height + ',left=' + left + ',top='
					+ top);
}
function windowOpenPrimaPagina(url) {
	var width = 950;
	var height = 550;
	var left = (screen.width / 2) - (width / 2);
	var top = (screen.height / 2) - (height / 2);
	window.open(url, '',
			'locationbar=no, titlebar=no, scrollbars=yes,status=no,width='
					+ width + ',height=' + height + ',left=' + left + ',top='
					+ top);
}
function windowOpenTraduzioneLingua(url) {
	var width = 1000;
	var height = 600;
	var left = (screen.width / 2) - (width / 2);
	var top = (screen.height / 2) - (height / 2);
	window.open(url, '',
			'locationbar=no, titlebar=no, scrollbars=yes,status=no,resizable=yes,width='
					+ width + ',height=' + height + ',left=' + left + ',top='
					+ top);
}
function windowOpenEditor(url) {
	var width = 880;
	var height = 420;
	var left = (screen.width / 2) - (width / 2);
	var top = (screen.height / 2) - (height / 2);
	window.open(url, 'FCKeditor',
			'locationbar=no, titlebar=no, scrollbars=yes,status=no,resizable=yes,width='
					+ width + ',height=' + height + ',left=' + left + ',top='
					+ top);
}
function windowOpenHelp(url) {
	var width = 950;
	var height = 550;
	var left = (screen.width / 2) - (width / 2);
	var top = (screen.height / 2) - (height / 2);
	window
			.open(url, '',
					'locationbar=no, titlebar=no, scrollbars=yes,status=no,width='
					+ width + ',height=' + height + ',left=' + left + ',top='
					+ top);
}

function getFlashMovieObject(movieName) {
	if (window.document[movieName]) {
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet") == -1) {
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName];
	} else {
		// for ie e firefox x11
		return document.getElementById(movieName);
	}
}
function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)){
		return false;
	}
	return true;
}
function setDimFlashObject(movie) {
	alert(movie);
	var movieObj = getFlashMovieObject(movie);
	var width = movieObj.TGetProperty("/", 8);
	var height = movieObj.TGetProperty("/", 9);
	document.getElementById(movie).width = width;
	document.getElementById(movie).height = height;
	// alert("The current dimensions for the Flash Object (" + movie + ") are "
	// + width + " in width and " + height + " in height.");
}

function fOnLoad() {
	eval(strOnLoad);
}
function init() {
	if (document.addEventListener) {
		// document.addEventListener("DOMContentLoaded", fOnLoad(), false);
	} else {
		fOnLoad();
	}
}

window.onload = function() {
	init();
}

function viewDesc(id,str,stato){
	if (stato == 1) {
		document.getElementById(id).innerHTML="<span id=\"" + id + "\" class=\"viewDescExt\"><a onclick=\"javascript:viewDesc('" + id + "','" + str + "', 0);\">[-]</a>" + unescape(str) + "</span>";
	}else{
		document.getElementById(id).innerHTML="<span id=\"" + id +"\" class=\"viewDesc\"><a onclick=\"javascript:viewDesc('" + id + "','" + str + "', 1);\">[+]</a>" + str.substring(0,50) + "...</span>";
	}
}

function getAjaxRequest(obj,url,parameters) {
	document.getElementById("body_id").style.cursor="progress";
	var myrequest = new ajaxRequest();
	var str_ajax_response = "";
	var str_ajax_response_old = "";

	myrequest.onreadystatechange = function() {
		if (myrequest.readyState == 4) { 
			if (myrequest.status == 200) { 
				var contentType = myrequest.getResponseHeader("Content-Type")
				if (contentType == "text/xml") { 
					str_ajax_response = myrequest.responseXML;
					
				} else if (contentType == "text/json") { 
					str_ajax_response = eval(myrequest.responseText);
				} else { 
					str_ajax_response = myrequest.responseText;
				}
				document.getElementById(obj).innerHTML = str_ajax_response;
				document.getElementById("body_id").style.cursor="auto";
			}
		}
	}
	myrequest.open("POST", url, true);
	myrequest.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded");
	myrequest.setRequestHeader("Content-lenght", parameters.lenght);
	myrequest.send(parameters) // send POST request
}


