﻿// ===================
// GLOBALES
// ===================
//var CallBackBaseURL = "http://www.fusina.net/clients/bamboo/web2/admin/callback/";


// ===============================
// S?lection globale des images (bouton)
// ===============================
selectAll = function() {
	var elements = findElements("album","div");
	for (i=0;i<elements.length;i++) {
		e = elements[i];
		id = e.id.substring(6);
		if($("photo"+id).className!="disabled")
		{
			$("photoChecker"+id).checked="on";
			$("photo"+id).className="photoChecked";
		}
		
	}
}

unselectAll = function() {
	var elements = findElements("album","div");
	for (i=0;i<elements.length;i++) {
		e = elements[i];
		id = e.id.substring(6);
		if($("photo"+id).className!="disabled")
		{
			$("photoChecker"+id).checked = "";
			$("photo"+id).className = "photo";
		}
	}
}


// ===============================
// Quelle image est s?lectionn?e ?
// ===============================
whoSelected = function() {
	var elements = findElements("album","div");
	var str = new Array();
	for (i=0;i<elements.length;i++) {
		e = elements[i];
		id = e.id.substring(6);

		if (($("photoChecker"+id).checked)&&($("photo"+id).className!="disabled")) {
			t=0;
			zoneselect = ""; // init
			while (t<10)
			{

				t++;
				if($("checkbox"+id+"-"+t)==undefined){break;}
				
				if($("checkbox"+id+"-"+t).checked)
				{	
					valeur = $("checkbox"+id+"-"+t).value;
					
					if(zoneselect!="")
					{zoneselect = zoneselect+"*"+valeur+"*";}
					else
					{zoneselect = "*"+valeur+"*";}
				}
			}
			//zoneselect = $("selector"+id).value;
			//alert(zoneselect);
			str.push($("photoChecker"+id).value+"/"+i+"/"+zoneselect); // on ajoute le nom de fichier et l'ID => "filename / id"
		}
	}
	return str;
}

importPhotos = function(to) {
	selected = whoSelected();
	
	if (selected.length != 0) {
		//$("loadingInfos").innerHTML = selected;
		$("loadingInfos").innerHTML = "0 / " + selected.length + "<p></p>";
		$("loadingArea").style.display = "";
		//Effect.Fade($("loadingArea"),{duration:0.5, from:0.0, to:1.0});
		//Effect.BlindDown($("loadingInfos"),{duration:0.3});
		//Effect.BlindDown($("loadingArea"),{duration:0.3});
		importPhoto(to,selected);
	}
	else
	{
		alert("Veuillez selectionner au moins un fichier !");
		Effect.Fade($("loadingArea"),{duration:1.0, from:1.0, to:0.0});
		//$("loadingArea").style.display = "none";
	}
}

importPhoto = function(to,selected) {
		// Loading in progress
		for(id=0;id<selected.length;id++)
		{
			// on decode
			datas = selected[id].split("/") ;
			percent = Math.round((id) / selected.length * 100);
			//comment = "En cours de traitement...";
			$("loadingInfos").innerHTML = (id+1) + "/" + selected.length + "<p>" + datas[0] + " - "+percent+"%</p>";
			//alert(datas[0]+"-"+datas[2]);
			//$("loadingBar").style.width = percent + "%";
			// ==================================
			// appel de ma fonction callback
			// ==================================
	
			requestURL = CallBackBaseURL+"import.files.php?action=importMedia&data="+datas[0]+"&param="+datas[2];
			
			response = requestDirect(requestURL);
			
			if(response!=true)
			{
				if(((response)=="null")||(((response.substr(0,6)))=="error:"))
				{
					// ====== error !
					alert("Erreur ou probleme d'import")
					
				}
				
			}
			else
			{
				//alert("ok");
				// ===== ok
				// on efface visuellement l'element dans la liste
				$("global"+datas[1]).style.display = "none";
				// on renomme pour ?viter un rappel
				$("photo"+datas[1]).className = "disabled";
				$("numberoffiles").innerHTML = $("numberoffiles").innerHTML-1 ;
				
			}
		}
		// ========================
		// ===== COMPLETE !!!!!
		// ========================
		unselectAll();
		//refreshNumberOfFiles();
		//$("loadingArea").style.display = "none";
		$("loadingInfos").innerHTML = (id+1) + "/" + selected.length + "<p>" + datas[0] + " - "+"100%</p>";
		Effect.Fade($("loadingArea"),{duration:1.0, from:1.0, to:0.0});
		
}


refreshNumberOfFiles = function()
{
	$("numberoffiles").innerHTML = whoSelected.length;
}



// ===============================
// VERIFICATION DE FORMULAIRE
// ===============================

checkForm = function(myForm,passBool)
{
	error = false;
	
	
	if((myForm.pseudo.value=="")||(myForm.email.value==""))
	{
		alert ("Please, fill out all fields");
		error = true;
	}
	
	if(passBool)
	{
		if(myForm.password.value=="")
		{
			alert ("Please, enter a password");
			myForm.password.focus();
			error = true;
		}
		if((myForm.password.value!=myForm.password_verif.value)&&(!error))
		{
	
			alert ("Second password is wrong. Enter the same strings... Try again");
			myForm.password_verif.focus();
			error = true;
	
		}
	}
	
	
	if((myForm.email.value!="")&&(!error))
	{
	email = myForm.email.value;
	if(email.indexOf("@")==-1)
		{
		alert ("Email not valid. Please, enter an email like 'nom@domaine.com'");
		myForm.email.focus();
		error = true;
		}else{
			if(email.indexOf(".")==-1)
			{
			alert ("Email not valid. Please, enter an email like 'nom@domaine.com'");
			myForm.email.focus();
			error = true;
			}
		}
	
	}
	
	
	if((myForm.country.value=="")&&(!error))
	{
	
		alert ("Select a country...");
		myForm.country.focus();
		error = true;
		
	}
	return !error;
	
}

// =========================
// Rafraichit les vignettes
// =========================
refreshThumb = function(obj,thepath,filename,id)
{
	myImage = new Image();
	if(id<=0){
	myImage.src = "/stuff/"+filename;
	}else{myImage.src = thepath+filename;}
	document.images[obj].src = myImage.src;
	
	// mise à jour du lien
	link = "/stuff/zoom/"+filename;
	document.links[obj+"_link"].href = link;
}

// =========================
// Rafraichit les vignettes
// =========================
refreshThumb2 = function(obj,thepath,thepathzoom,filename,id)
{
	myImage = new Image();
	if(id<=0){
	myImage.src = thepath+filename;
	}else{myImage.src = thepath+filename;}
	document.images[obj].src = myImage.src;
	
	// mise à jour du lien
	link = thepathzoom+filename;
	document.links[obj+"_link"].href = link;
}



// =========================
// Rafraichit les vignettes V3.0
// =========================
refreshThumb3 = function(obj,thepath,thepathzoom,tableauimage,id)
{
	splitChaine = id.split("**"); // séparateur
	filename = tableauimage[splitChaine[0]];
	
	
	myImage = new Image();
	if(id<=0){
	myImage.src = thepath+filename;
	}else{myImage.src = thepath+filename;}
	document.images[obj].src = myImage.src;
	
	// mise à jour du lien
	link = thepathzoom+filename;
	document.links[obj+"_link"].href = link;
}


// =========================
// ZOOM
// =========================
showPict = function(thepath,filename,id,width,height)
{
	mypath = escape(thepath+filename);
	idwindow = open("index_zoom.php?url="+mypath, "Magnify", "directories=no,status=no,location=no,menubar=no,resizable=no,toolbar=no,width="+width+",height="+height+",innerWidth="+width+",innerHeight="+height);

}


// =========================
// VERIF FORM
// =========================
verifForm = function(objForm,inputs)
{
	//alert("coucou");
	//inputs est une liste
	myliste = new Array();
	myliste = inputs.split(",");
	error = false;
	
	//alert(inputs);
	
	
	// ============
	for(n=0;n<myliste.length;n++)
	{
		nameObj = myliste[n];
		//alert(objForm.elements[nameObj].type);
		
		
		// INPUT TEXT = OBJECT
		switch (objForm.elements[nameObj].type)
		{
		case "text":
			if(nameObj=="email")
			{
				emailvalue = objForm.elements[nameObj].value;
				if(emailvalue.indexOf("@")==-1)
					{
					alert ("Email not valid. Please, enter an email like 'nom@domaine.com'");
					objForm.elements[nameObj].focus();
					error = true;
					}else{
						if(emailvalue.indexOf(".")==-1)
						{
						alert ("Email not valid. Please, enter an email like 'nom@domaine.com'");
						objForm.elements[nameObj].focus();
						error = true;
						}
					}
			
			}
		
			else
			{if(objForm.elements[nameObj].value=="")
			{
				alert("Erreur : un des champs est obligatoire");
				objForm.elements[nameObj].focus();
				error = true;
				return !(error);
			}
			}
		break;
		
		case "select-one":
			if(objForm.elements[nameObj].options[objForm.elements[nameObj].options.selectedIndex].value<0)
			{
			alert("Erreur : La selection d'une valeur est obligatoire");
		objForm.elements[nameObj].focus();
			error = true;
			return !(error);
			}
		break;
		}
		
	}
	
	return !(error);
}




