var loading_img_src = '/images/load.gif';

var startdl = 0;
var fid = null;
var ajax = null;
var progresslabel, progressbar,	upload_ifrm, input_file;
var selectfilebox, uploadfilebox;
var imgbox = null;
var name = null;


function HumanSize(b) {
	if(b>1048576) return round(b/1048576)+'Mo';
	else return Math.round(b/1024)+'Ko';
}

function round(v) {
	return Math.round(v*100)/100;
}

function sectostr(t) {
	var min = Math.floor(t/60);
	if(min) return 'Moins de '+(min+1)+' minutes';
	else return (t%60)+'sec';
}

/*
function displayPhoto(_xmlhttp) {
	startdl = 0;
	input_file.value = '';
	selectfilebox.style.display = 'block';
	uploadfilebox.style.display = 'none';
	var xml = _xmlhttp.responseXML.documentElement; 
	
	if(xml.nodeName=='error') {
		imgbox.box.parentNode.removeChild(imgbox.box);
		smailAlert.alert(xml.firstChild.nodeValue, null, "erreur");
		return;
	} 

	var load = new Image();

	load.onload = function() {
		imgbox.img.src = '/photos/thumb/'+xml.firstChild.nodeValue+'.jpg';
		var span = document.createElement('span');
		span.className = 'w8p';
		imgbox.a.appendChild(span);
		SmaiLib.setOpacity(imgbox.img, 0);
		new Fade(imgbox.img, {start: 1, limit: 100});
	};

	load.src = '/photos/thumb/'+xml.firstChild.nodeValue+'.jpg';
	imgbox.box.id = 'photo'+xml.firstChild.nodeValue;
	zoomTo(imgbox.a, '/photos/'+xml.firstChild.nodeValue+'.jpg');
	imgbox.rm.innerHTML = 'Supprimer';
	imgbox.rm.onclick = confirmDelete;
	imgbox.rm.rev = xml.firstChild.nodeValue;
}
*/

function displayUpload(_xmlhttp) {
	//alert(_xmlhttp.responseText);
	var xml = _xmlhttp.responseXML.documentElement; 
	if(xml.nodeName=='error') {
		input_file.value = '';
		uploadfilebox.style.display = 'none';
		selectfilebox.style.display = 'block';
		ajax.stopTimer();
		startdl = 0;
		upload_ifrm.src='';
		//smailAlert.alert(xml.firstChild.nodeValue, null, "erreur");
		return;
	} 
	try { var totalsize = parseInt(xml.getElementsByTagName("totalsize").item(0).firstChild.nodeValue); }
	catch(e) { return; }
	if(!totalsize) return; 
	var uploadsize = parseInt(xml.getElementsByTagName("uploadsize").item(0).firstChild.nodeValue);
	var debit = uploadsize / ((new Date()).getTime() - startdl.getTime());
	var pourcent = (uploadsize/totalsize)*100;
	var tps = Math.round((totalsize - uploadsize)/1024/debit);
	progressbar.style.width = pourcent+"%";
	progresslabel.innerHTML = '<span style="float: right">'+ sectostr(tps)+' restante(s)</span>'+round(pourcent)+'% de '+ HumanSize(totalsize)+' à '+round(debit)+'Ko/sec';
}

function beginUpload(f,sid) {	
	if(startdl) { myAlert("error","Upload déjà en coursé", null); return false; }
	if(!f.mypicture.value) { myAlert("error","Choisir un fichier", "CV manquant"); return false; }
	if(!validFileCV(input_file.value)) { myAlert("error","Seuls les fichiers doc, docx, rtf ou pdf sont autorisés.", "Fichier invalide"); return false; }
	progressbar.style.width = "0%"; progresslabel.innerHTML ='Début du téléchargement en cours...';
	selectfilebox.style.display = 'none';
	uploadfilebox.style.display = 'block';
	fid = sid;
	name = f.mypicture.value;
	startdl = new Date();
	ajax.onsuccess = displayUpload;
	ajax.timerRequest("./cgi-bin/upload.cgi?"+sid, 1000, {nocache: true});
}


function endUpload() {
	if(!startdl) return;
	ajax.stopTimer(); 
	ajax.resetFunction();
	progressbar.style.width = '100%';
	ajax.onsuccess = function(r) { 
		var xml = r.responseXML.documentElement;
		if ((xml) && (xml.nodeName=='error')) myAlert('error','Veuillez saisir correctement le formulaire',xml.firstChild.nodeValue);
		else {
			$('fcv').reset();
			startdl = 0;
			input_file.value = '';
			selectfilebox.style.display = 'block';
			uploadfilebox.style.display = 'none';
			myAlert("alert","CV envoyé avec succès.", "Nous avons bien reçu votre CV. Merci de l'intérêt tout particulier que vous portez à notre Structure. Si vous postulez à une de nos annonces, un Conseiller en Ressources Humaines examinera votre candidature et vous contactera dans les plus brefs délais.");
			//progresslabel.innerHTML = 'CV envoyé';
		}
	};
	ajax.request("/js/ajax/addcv.php?fid="+fid+'&'+getDataFromForm($('fcv')));
	progresslabel.innerHTML = 'Traitement de votre CV en cours...';
	startdl = false;
}


/*
function addPhotoNode() {
	var fiche = document.createElement("div");
	fiche.className = "fiche";

	var ph = document.createElement("div");
	ph.className = 'photo transpa';
	var anchor = document.createElement("a");
	var img = new Image();
	img.src = loading_img_src;
	
	anchor.appendChild(img);
	ph.appendChild(anchor);

	var fi = document.createElement("div");
	fi.className='ficheInfo';
	fi.innerHTML = 'En attente...<br />';

	var rm = document.createElement("a");
	rm.href = '#';
	fi.appendChild(rm);

	fiche.appendChild(ph);
	fiche.appendChild(fi);

	document.getElementById('photolist').appendChild(fiche);
	return {img: img, box: fiche, a: anchor, rm: rm};
}
*/

function validFile(fname) { 
	var ext=fname.substring(fname.length-4,fname.length).toLowerCase();
	if((ext=='.jpg')||(ext=='.gif')||(ext=='.png')) return true;
	ext=fname.substring(fname.length-5,fname.length).toLowerCase();
	if(ext=='.jpeg') return true;
	return false;
}

function validFileCV(fname) { 
	var ext=fname.substring(fname.length-4,fname.length).toLowerCase();
	if((ext=='.doc')||(ext=='.trf')||(ext=='.pdf')) return true;
	ext=fname.substring(fname.length-5,fname.length).toLowerCase();
	if(ext=='.docx') return true;
	return false;
}


function initUpload() {
	progresslabel = document.getElementById('label');
	progressbar = document.getElementById("progressbar");
	upload_ifrm = document.getElementById("iframe_upload");
	selectfilebox = document.getElementById("selectfile_box");
	uploadfilebox = document.getElementById("uploadfile_box");
	input_file = document.getElementById("input_file");
	ajax = new Ajax();
	var module = document.getElementById("module_loading");
	if (module) module.style.display = 'none';
	if (selectfilebox) selectfilebox.style.display = 'block';
}


addLoadEvent(initUpload);
