//prechargement des images
i1 = new Image;
i1.src = "images/warning.png";
i2 = new Image;
i2.src = "images/valid.png";

var tracenumber;
function SelectDep(valeur)
{
	if(valeur != '')
	{
	AjaxLoader('id='+valeur,'lib/getville.php', 'POST','ville');
	}
	
}

function SelectCat(valeur)
{
	//on efface les detail plus (km, cm3 etc...) si l'user avait selectionne une sosucat avant
	GetFormSousCat(valeur, '');
}




function DisplayRCS(valeur)
{
	if(valeur =='yes')
	{
		indiv = "<label><strong>Indiquez le nom de l'entreprise : </strong></label>";
		indiv += "<input type='text' size='30' id='rcspro' name='rcspro' />";
		indiv += "<input type='hidden' id='validrcs' size='30' value='1' name='validrcs' />";
	}else{
		indiv ="<input type='hidden' id='validrcs' size='30' value='' name='validrcs' />";
	}
	
	document.getElementById('rcs').innerHTML = indiv;	

}

function str_replace(a, b, str) {
    return str_replace2(str, a, b);
}
function str_replace2(SRs, SRt, SRu) {
  /*
  **  Replace a token in a string
  **    s  string to be processed
  **    t  token to be found and removed
  **    u  token to be inserted
  **  returns new String
  */
  SRRi = SRs.indexOf(SRt);
  SRRr = '';
  if (SRRi == -1) return SRs;
  SRRr += SRs.substring(0,SRRi) + SRu;
  if ( SRRi + SRt.length < SRs.length)
    SRRr += str_replace2(SRs.substring(SRRi + SRt.length, SRs.length), SRt, SRu);
  return SRRr;
}

function CheckPrice(prix)
{
	
	if(prix.value != "")
	{
		prix.value = str_replace("€","", prix.value);
		prix.value = str_replace(",",".", prix.value);
		prix.value = str_replace(" ","", prix.value);

		if(!isNaN(prix.value))
		{
			return prix.value;
		}else{
			return "pb";
		}
	}else{
		return "pb";
	}
}



function ControleNum(champ, retour){
	var valeur = champ.value;
	tracenumber	=	'ControleNum1';
	if(!isNaN(valeur))
	{
		tracenumber	=	'ControleNum2a';
		message ='';
		tracenumber	=	'ControleNum2b';
		champ.className = "";
		tracenumber	=	'ControleNum2c';
	}else{
		tracenumber	=	'ControleNum3';
		message = '- '+retour+' est incorrect <br />';
		champ.className = "redalert";
	}
	tracenumber	=	'ControleNum4';
	return message;
}




function ControleCase(champ, retour)
{
			var message ;
			if(champ.checked==false)
			{
				message = "- Veuillez  "+retour+" <br />";
				champ.style.border = "2px solid red";
			}else{
				message ='';
				champ.style.border = "";
			}
			
		return message;
}

function DisplayError(msg)
{
		indiv  = "<p>";
		indiv  += "<div class='warningimg'>";
		indiv  += "<img src='images/warning.png' alt='Attention des erreurs se sont produites' />";
		indiv  += "</div>";
		indiv  += "<div style='width:380px;float:left;'><strong>Attention les erreurs suivantes se sont produites : </strong>";
		indiv  += "<div class='warningform'>";
		indiv  += msg;
		indiv  += "</div>";
		indiv  += "<div class='backads'><span class='nolink' onclick=\"ClosePopup('popUpDiv');\">Cliquez ici pour revenir à votre annonce</span></div>";
		//indiv  += "<span class='nolink' onclick=\"popup('popUpDiv');\">Cliquez ici pour revenir au formulaire</span>";
		indiv  += "</div></p><div class='clearer'></div>";
		
		return indiv;
}


function AddPic()
{
	var photoForm = document.getElementById("photoForm");
	var picform = document.getElementById("picform");
	var contenu = photoForm.innerHTML ;
	/*
	var prev =document.createElement('div');
	prev.setAttribute("id","loadingthumb");
	prev.setAttribute("class","thumbs");
	photoForm.appendChild(prev);
	*/
	photoForm.innerHTML = contenu+"<div id='loadingthumb' class='thumbs'><img src='images/add/loading.gif' /><br />envoi en cours ...</div>";
	document.getElementById("msgupload").style.display="block";
	picform.style.display = "none";
	picform.submit();
	}
function AddFormPic()
{
	var picform = document.getElementById("picform");
	picform.innerHTML = '<input type="hidden" name="MAX_FILE_SIZE" value="4000000 type="hidden" ><label><strong>Choisissez une photo a envoyer :</strong></label><input type="file" name="fichier" onchange="AddPic();return false;" style="display:inline"/>';
}

function AddFormVideo()
{
	document.getElementById("naxostream_upload").style.display = "block";
	document.getElementById("videolink").style.display = "none";
}

function FormVideo()
{
	document.getElementById("naxostream_video_id").value ='';
	var formvideo = "<div style='width:68px;height:150px;float:left;'><img src='images/add/video.gif' alt='envoyer votre vidéo' /></div><strong>Exclusif !</strong> TuVends.com vous propose d'insérer gratuitement une vidéo de votre bien!<br /><br />";
	 		formvideo +="<span id='videolink' onclick='AddFormVideo();' class='nolink' style='color:#3165b4;text-decoration:underline;font-weight:bold;'>Cliquez ici pour ajouter une Vidéo</a></span>";											
			formvideo +="<div class='naxostream_upload' id='naxostream_upload'style='display:none;float:left;'></div><br /><br /><br /><br />";

	document.getElementById("videocontent").innerHTML = formvideo ;
	naxostream_replace_div();
}
function naxostream_upload_done_callback(id)
{
	if(id != "")
	{
		document.getElementById("videocontent").innerHTML = "<img src='images/add/videowait.gif' alt='vidéo envoyée !' /><br /><br />";
		document.getElementById("videocontent").style.textAlign = "center";
	}
}

function DeletePic(photoPath, idphoto, idform)
{
	
	var photoForm = document.getElementById("photoForm");
	photoForm.removeChild(document.getElementById(idphoto));
	
	nbpic = document.getElementById("nbpic").value;
	numpic = parseInt(nbpic);
	numpic = numpic - 1;
	linepic = document.getElementById("linepic").value;
	tb = linepic.split(",");
	longueur = tb.length - 1;
	var newtb ="";
	for($i=0;$i<longueur;$i++)
	{
		if(tb[$i]!= idform)
		{
			newtb += tb[$i]+",";
		}
	}
	document.getElementById("linepic").value = newtb;
	
	document.getElementById("nbpic").value = numpic;
	if(numpic == 5)
	{
		AddFormPic();
	}
	var xhr=getXhr();
	xhr.onreadystatechange=function(idform)
	{
		if(xhr.readyState==4&&xhr.status==200)
		{
			//var retour=xhr.responseText;
			//alert(retour);
		}
	}
		xhr.open("POST","lib/deletepic.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send("pic="+idform);
	
	
}

function IEcompattest(){
	return(document.compatMode&&document.compatMode!="BackCompat")?document.documentElement:document.body
}


function whereami()
{
	return tracenumber;
}

function ValidForm()
{
	var msg ; 
	msg = "";
	

	Dep = document.getElementById('ledep');
	Ville = document.getElementById('city');
	//Cat = document.getElementById('cat');
	SCat = document.getElementById('cat');
	Nom = document.getElementById('nom');
	Prenom = document.getElementById('prenom');
	Email = document.getElementById('email');
	Telephone = document.getElementById('telephone');
	Detail = document.getElementById('detailannonce');
	Titre = document.getElementById('titleads');
	


	
	
	msg += ControleChamp(Dep, "sélectionner un département"); 
	msg += ControleChamp(Ville, "sélectionner une ville");
	//msg += ControleChamp(Cat, "sélectionner une catégorie");
	msg += ControleChamp(SCat, "sélectionner une catégorie");
	msg += ControleChamp(Nom, "indiquer un nom");
	msg += ControleChamp(Prenom, "indiquer un prénom");
	msg += ControleChamp(Detail, "décrire votre annonce"); 
	msg += ControleChamp(Titre, "indiquer un titre pour votre annonce"); 
	msg += ControleChamp(Email, "indiquer un email");
	//msg += ControleChamp(Email, "un email");
	
	
	if(Email.value != "")
	{
		msg += verifierMail(Email);
	}
	
	
	
	//la description doit au moins faire 50 caractere minimum
	if(Detail.value.length < 50 && Detail.value != ""){
		msg += "- La Description de votre annonce est trop courte (50 caractères minimum).<br />";
		Detail.style.border = "2px solid red";
	}
	
	if(msg != "")
	{
		indiv = DisplayError(msg);
		document.getElementById('inpopup').innerHTML = indiv;
		CenterPopup('popUpDiv');
		
	}else{
		document.getElementById('sendoffer').disabled = true;
		document.getElementById('sendoffer').value = "Validation en cours ...";
		document.addannonce.submit();
		
	}
}






function DisplayUpdateOK()
{
		indiv  = "<p>";
		indiv  += "<div class='warningimg'>";
		indiv  += "<img src='images/valid.png' alt='Profil Modifié' />";
		indiv  += "</div>";
		indiv  += "<div style='width:380px;float:left;'><strong>Votre profil a bien été modifié ! </strong>";
		indiv  += "<div class='warningform'>";
		indiv  += "<strong>Les modifications que vous avez apporté à votre profil ont bien été prises en compte.</strong><br />";
		indiv  += "Votre profil va être examiné par nos modérateurs avant sa publication.<br />";
		indiv  += "Si celui ci est accepté, il sera en ligne d'ici 24 heures.<br />";
		indiv  += "<br />Annuaire-AutoEntrepreneurs.com vous remercie de votre confiance.<br />";
		indiv  += "</div><br />";
		indiv  += "</div></p><div class='clearer'></clearer>";
		
		indiv  += "<div class='backads'><span class='nolink' onclick=\"ClosePopup('popUpDiv');\">Fermer la fenêtre</span></div>";
		//indiv  += "<span class='nolink' onclick=\"popup('popUpDiv');\">Cliquez ici pour revenir au formulaire</span>";
		indiv  += "</div></p><div class='clearer'></clearer>";
		
		document.getElementById('inpopup').innerHTML = indiv;
		CenterPopup('popUpDiv');
		

}
function DisplayAddOK()
{
		indiv  = "<p>";
		indiv  += "<div class='warningimg'>";
		indiv  += "<img src='images/valid.png' alt='Profil inséré' />";
		indiv  += "</div>";
		indiv  += "<div style='width:380px;float:left;'><strong>Votre profil a bien été publié ! </strong>";
		indiv  += "<div class='warningform'>";
		indiv  += "<strong>Un Email vous à été envoyé</strong>, il vous permet de supprimer votre profil plus tard si vous le souhaitez.<br />";
		indiv  += "<br /><u>N'oubliez pas de regarder dans votre boite a SPAM si vous ne trouvez pas cet email.</u><br /><br />";
		indiv  += "<strong>Votre profil va être examinée par nos modérateurs avant sa publication.</strong><br />";
		indiv  += "Si celui ci est accepté, il sera en ligne d'ici 24 heures.<br />";
		indiv  += "<br />Annuaire-AutoEntrepreneurs.com vous remercie de votre confiance.<br />";
		indiv  += "</div><br />";
		indiv  += "</div></p><div class='clearer'></clearer>";
		
		indiv  += "<div class='backads'><span class='nolink' onclick=\"ClosePopup('popUpDiv');\">J'ai terminé, revenir au site</span></div>";
		//indiv  += "<span class='nolink' onclick=\"popup('popUpDiv');\">Cliquez ici pour revenir au formulaire</span>";
		indiv  += "</div></p><div class='clearer'></clearer>";
		
		document.getElementById('inpopup').innerHTML = indiv;
		CenterPopup('popUpDiv');
		

}

function DisplayOkDelete()
{
		indiv  = "<p>";
		indiv  += "<div class='warningimg'>";
		indiv  += "<img src='images/valid.png' alt='Profil supprimé' />";
		indiv  += "</div>";
		indiv  += "<div style='width:380px;float:left;'><strong>Votre profil a bien été supprimé ! </strong>";
		indiv  += "<div class='warningform'>";
		indiv  += "<strong>Votre profil à été supprimé</strong>, celui-ci n'apparaît plus sur notre site.<br />";
		indiv  += "<br />Annuaire-AutoEntrepreneurs.com vous remercie de votre confiance.<br />";
		indiv  += "</div><br />";
		indiv  += "</div></p><div class='clearer'></clearer>";
		
		indiv  += "<div class='backads'><span class='nolink' onclick=\"ClosePopup('popUpDiv');\">J'ai terminé, revenir au site</span></div>";
		//indiv  += "<span class='nolink' onclick=\"popup('popUpDiv');\">Cliquez ici pour revenir au formulaire</span>";
		indiv  += "</div></p><div class='clearer'></clearer>";
		
		document.getElementById('inpopup').innerHTML = indiv;
		CenterPopup('popUpDiv');
		

}

function EffaceForm()
{
	//Reset le fomrulaire
		document.forms["addannonce"].reset();
		document.getElementById('detailannonce').value = "";
		
}

function ValidDetail(valeur)
{
	var msg;
	

}

function GetFormSousCat(valeur, idcar)
{
			        
		var i;
		var tab = document.getElementsByName('transaction');
		for (i=0;i<tab.length;i++)
		{
		    if(tab[i].checked)
		    {
		        var typetransaction = tab[i].value;
		        break;
		    }
		}
		
	if(typetransaction == "1")
	{
		indiv =  "<label><strong>Prix (EUR) : </strong></label>\n";
		if(document.getElementById('prix') != null)
		{
			prixinside = document.getElementById('prix').value ;
		}else{
			prixinside = "";
		}
		
		indiv += "<input type='text' name='prix' id='prix' size='20' value='"+prixinside+"'/> €<br /><br />";
		document.getElementById('price').innerHTML = indiv;	
	}
	
	

	
	
	
}




function GetFormDefault()
{
			document.getElementById('detailplus').innerHTML = "";
			if(document.getElementById('prix') != null)
			{
				if(document.getElementById('prix').value != "")
				{
					setprix = document.getElementById('prix').value;
				}else{
					setprix ="";
				}
				document.getElementById('price').innerHTML = "<label><strong>Prix (EUR) : </strong></label><input type='text' name='prix' id='prix' size='20' value='"+setprix+"' /> €<br /><br />";
			}
		
	
}




function GetFormNoPrice()
{
		var tab = document.getElementsByName('transaction');
		
		tab[1].checked = true;
		document.getElementById('price').innerHTML = "";
		document.getElementById('detailplus').innerHTML = "";
}

function AddBasePic(valeur)
{
	nbpic = document.getElementById("nbpic").value;
	numpic = parseInt(nbpic);
	numpic = numpic + 1;
		
	var photoForm = document.getElementById("photoForm");
	var idform = valeur;
	var idphoto = valeur;
	var photoId = valeur;
	
	document.getElementById("nbpic").value = numpic;
	
	var linepic = document.getElementById("linepic");
	linepic.value += idform+",";
		
	
	var contenu = photoForm.innerHTML ;
	photoForm.innerHTML = contenu+"<div class='thumbs' id='"+idphoto+"'><img src='images/storage/thumbs/"+photoId+"'><br /><span onclick='DeletePicBase(\""+photoId+"\", \""+idphoto+"\", \""+idform+"\");' class='nolink' style='color:#3165b4;text-decoration:underline'>Supprimer</span></div>";
	
}


function DeletePicBase(photoPath, idphoto, idform)
{
	
	var photoForm = document.getElementById("photoForm");
	photoForm.removeChild(document.getElementById(idphoto));
	
	nbpic = document.getElementById("nbpic").value;
	numpic = parseInt(nbpic);
	numpic = numpic - 1;
	
	linepic = document.getElementById("linepic").value;
	tb = linepic.split(",");
	longueur = tb.length - 1;
	var newtb ="";
	for($i=0;$i<longueur;$i++)
	{
		if(tb[$i]!= idform)
		{
			newtb += tb[$i]+",";
		}
	}
	document.getElementById("linepic").value = newtb;
	
	document.getElementById("nbpic").value = numpic;
	if(numpic == 5)
	{
		AddFormPic();
	}
		
	
}


