/******************************************************************************
 * File     : js/zzfile.js
 * Purpose  : Javascript functions for handling file upload function
 *			: in possible conjunction with url specification
 *			: (zzFile and zzFileUrl widgets)
 * Project  : ZZ Back-Office
 * Date     : Janvier 2007
 * Author   : Eric Miquelard, www.em-xnet.com, emiquelard@gmail.com
 *****************************************************************************/

function uploadRemove(idx, scriptPath, tmpFolder) {

	// use http request to delete the file stored in the tmp folder
	var url = scriptPath + 'deleteTmpFile.php?path=' + tmpFolder + document.getElementById('id_upload_fn_' + idx).value;
	oHttpReq.open('GET', encodeURI(url), true);
	oHttpReq.onreadystatechange = deleteTmpFile;
	oHttpReq.send(null);
	
	// update page display
	if (document.getElementById('upload_fn_' + idx).getElementsByTagName("IMG").length > 0) {
		var oImg = document.getElementById('upload_fn_' + idx).getElementsByTagName("IMG")[0];
		oImg.style.visibility = "hidden";
		oImg.src = oImg.src.substring(0, oImg.src.lastIndexOf("/") + 1) + "undefined";
	} else {
		document.getElementById('upload_fn_' + idx).innerHTML = '';
	}
	document.getElementById('id_upload_fn_' + idx).value = '';
	document.getElementById('upload_no_' + idx).style.display = 'inline';
	document.getElementById('upload_ok_' + idx).style.display = 'none';
	
	return true;
}

function zzfileurlRemove(idx, scriptPath, tmpFolder) {

	// use http request to delete the file stored in the tmp folder
	var url = scriptPath + 'deleteTmpFile.php?path=' + tmpFolder + document.forms['detailForm'].elements[idx].value;
	oHttpReq.open('GET', encodeURI(url), true);
	oHttpReq.onreadystatechange = deleteTmpFile;
	oHttpReq.send(null);
	
	// update page display
	document.getElementById('upload_fn_' + idx).innerHTML = '';
	document.forms['detailForm'].elements[idx].value = '';
	document.getElementById('url_' + idx).style.display = 'inline';
	document.getElementById('upload_no_' + idx).style.display = 'inline';
	document.getElementById('upload_ok_' + idx).style.display = 'none';
	
	return true;
}

function deleteTmpFile() {
	if (oHttpReq.readyState == 4) {
		if (oHttpReq.status == 200) {
			// Nothing to do
			// alert(oHttpReq.responseText);
		} else {
			// alert("XMLHttpRequest a retourné une erreur ! (status = " + oHttpReq.status + ")");
		}
	}
	return true;
}

function uploadSetValue(idx)
{
	var inputForPost = document.getElementById('id_upload_fn_' + idx);
	inputForPost.value = displayFilename.innerHTML;
}

function initHttpReq () {
	try {
		oHttpReq = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			oHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				oHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				oHttpReq = false
			}
		}
	}
	if (!oHttpReq) {
		alert("Erreur lors de la déclaration de l'objet XMLHttpRequest");
	}
	return oHttpReq;
}

// Initialisation pour les requête http
var oHttpReq = initHttpReq();