/*  
 * Javascript pour vielocale.fr
 * Ressources :
 * http://www.dynamicdrive.com/
/*--------------------------------------------------------------------------*/

/* Test ma validite du pseudo utilisateur*/
function testPseudoName(pseudo){
	var thename = pseudo;
	if(thename=='') {
		alert('Veuillez saisir un pseudo.');
		return false;
	}
	for (var i = 0; i < thename.length; i++)
	{

		var thechar = thename.substring(i,i+1);
		var hh = thechar.charCodeAt(0);
		if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123)) {
			;  
		} else {
			alert('Caractère non autorisé pour le pseudo. Veuillez le modifier.');
			return false;
		}	
	}	

}

/* Test ma validite du nom du blog saisie dans le formulaire de création*/
function testBlogName(blogName){
	var thename = blogName;
	if(thename=='') {
		alert('veuillez saisir le nom de votre blog');
		return false;
	}
	for (var i = 0; i < thename.length; i++)
	{

		var thechar = thename.substring(i,i+1);
		var hh = thechar.charCodeAt(0);
		if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123)) {
			;  
		} else {
			alert('Caractère non autorisé pour le nom de votre Blog');
			return false;
		}	
	}	

}

function formatNumTel(NumTel){
var Tel;
var i = 0;
while (i < NumTel.length-1)
{
	if ((NumTel.charAt(i) == " ") || (NumTel.charAt(i) == ".")||(NumTel.charAt(i) == ",")||(NumTel.charAt(i) == "/")||(NumTel.charAt(i) == "-"))
	{
		Tel = NumTel.substring(0,i) + NumTel.substring(i+1,NumTel.length);
		NumTel = Tel;
	}
	else{
		i++;
	}
}
return (NumTel);
} 

// Remplace toutes les occurences d'une chaine
function replaceAll(str, search, repl) {
	while (str.indexOf(search) != -1)
		str = str.replace(search, repl);
	return str;
} 

// Remplace les caractÃ¨res accentuÃ©s (+ espace)
// BONNE REFERENCE : http://www.dynamic-tools.net/toolbox/replaceAll/
function replaceAccent(str) {
var origin = new Array("-","À","Á","Â","Ã","Ä","Å","Æ","Ç","È","É","Ê","Ë","Ì","Í","Î","Ï","Ð","Ñ","Ò","Ó","Ô","Õ","Ö","Ø","Ù","Ú","Û","Ü","Ý","Þ","ß","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï","ð","ñ","ò","ó","ô","õ","ö","ø","ù","ú","û","ý","ý","þ","ÿ");
var target = new Array(" ","A","A","A","A","A","A","A","C","E","E","E","E","I","I","I","I","D","N","O","O","O","O","O","O","U","U","U","U","Y","b","s","a","a","a","a","a","a","a","c","e","e","e","e","i","i","i","i","d","n","o","o","o","o","o","o","u","u","u","y","y","b","y");

//var test = "The rain in spain falls mainly on the plain."; 
//var test2 = test.replace(/a/g, "A");

	for (var i = 0; i < target.length; i++)
		str = replaceAll(str, origin[i], target[i]);
return str;
}

/*Recherche d'une entité geo avec le prefix*/
function lookup(inputString,urlrequest) {	
	
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
		
			if(isNaN(inputString)) {
				var nom = replaceAccent(inputString);
							
				$.post(urlrequest + "?action=localisationnom", {critereNomMajuscule: ""+nom.toUpperCase()+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
				}); 			
 			} else { 			
				$.post(urlrequest + "?action=localisationcode", {critereCode: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
				}); 			
			}
				

		}
} // lookup

/*Remplissage du champ WHERE avec valeur sélectionnée */	
function fill(thisValue) {
		$('#whereinput').val(thisValue);
		setTimeout("$('#suggestions').hide();", 200);
}


/*Pour Accueil general*/
function testEntiteGeo() 
{
	if(document.FormZoneGeo.entitegeo.value=='') {
		alert('Veuillez saisir la localisation !\n (commune, département, région, code postal)');
		return false;
	} else
	{
		if(document.FormZoneGeo.entitegeo.value != null) 
		{
			if(isNaN(document.FormZoneGeo.entitegeo.value)) { 
			
			var nomEntiteGeo = document.FormZoneGeo.entitegeo.value;			
			var nom = replaceAccent(nomEntiteGeo);
		
			//var nom = replaceAccent("tÃ©tÃ©");
			//var expr = new RegExp("Ã©", "ig"); 
			//var nom = "pÃ©tÃ©".replace(/[Ã©]/g, "e");
			//nom = nomEntiteGeo.replace(expr, "e");
		
			//document.FormZoneGeo.critereNomMajuscule.value=nomEntiteGeo.toUpperCase();
			document.FormZoneGeo.critereNomMajuscule.value=nom.toUpperCase();	
			document.FormZoneGeo.critereCode.value="";
		
			}else { 
				document.FormZoneGeo.critereCode.value=document.FormZoneGeo.entitegeo.value;
				document.FormZoneGeo.critereNomMajuscule.value="";
			}
		}	
	}
	return true;
}

/*Pour Masquer/Demasquer un zone*/
function ShowHide (block_id)
{
  block = document.getElementById(block_id);
  block.style.display = (block.style.display == 'block') ? 'none' : 'block';
}

/*Demasquer un zone*/
function Show (block_id)
{
  block = document.getElementById(block_id);
  block.style.display = 'block';
}

/*Masquer un zone*/
function Hide (block_id)
{
  block = document.getElementById(block_id);
  block.style.display = 'none';
}


/*Test Formulaire upload image*/
if(!String.prototype.endsWith)
{
String.prototype.endsWith=function endsWith(su)
{
var s=this.length-su.length;if(s<0){return false;}
return(this.lastIndexOf(su,s)==s);};
}

function uploadSubmit()
{ 
document.uploadForm.uploadButton.disabled=true; 
setTimeout('document.images["imgAttente"].src="../img/vl_picto_attente.gif"',200);
document.getElementById('attente').style.display='block';
}
function uploadChange()
{ 
v = document.uploadForm.fichier.value.toLowerCase();
if (v.lastIndexOf('.')==-1)return;
if (!v.endsWith(".bmp")&&!v.endsWith(".jpg")&&!v.endsWith(".jpeg")&&!v.endsWith(".gif")&&!v.endsWith(".png") &&!v.endsWith(".bmp")) 
{ alert("La photo doit Ãªtre au format bmp, gif, jpg, jpeg, jpe ou png."); document.photo.reset(); 
} else { 
uploadSubmit();document.uploadForm.submit(); 
}}

function motcleSubmit()
{ 
v = document.motcleForm.motCle.value.toLowerCase();
if (v.length == 0)
{ alert("Vous devez saisir un mot !"); } 
else { document.motcleForm.submit(); }
}

/*Autres*/
function popup_map(url)
{
eval("w=window.open(url,'w','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=640,height=560');");
}


function media(i)
{
document['imgmedia'].src=i;return;
}

/*Pour afficher l'URL du BLOG lors de la saisie de son nom*/
function displayBlogUrl(id,urlBlog)
  {
  if (document.getElementById)
    {
    document.getElementById(id).innerHTML = urlBlog;
    }
  else if (document.all)
    {
    document.all[id].innerHTML = urlBlog;
    }
  }

/*Modifier un ID*/
function modifyInnerHTML(id,content)
  {
  if (document.getElementById)
    {
    document.getElementById(id).innerHTML = content;
    }
  else if (document.all)
    {
    document.all[id].innerHTML = content;
    }
  }
  
/*Test si au moins un bouton Radio coché dans le formulaire */  
function testRadioFormulaire(formName)
{

var message_alert = "";
var cptRadio = 0;
var cptRadioFalse = 0;
//message_alert +="\n" + document.getElementById(formName).name;
for(var i = 0; i < document.getElementById(formName).length ; i++) {
//message_alert +="\n" + document.getElementById(formName).elements[i].type;
if(document.getElementById(formName).elements[i].type == 'radio')
	{
		cptRadio++;
		if(document.getElementById(formName).elements[i].checked == false) cptRadioFalse++;
	}
}

if(cptRadioFalse == cptRadio)
{
	for(var i = 0; i < document.getElementById(formName).length ; i++) {
	if(document.getElementById(formName).elements[i].type == 'radio')
		document.getElementById(formName).elements[i].style.backgroundColor = "#CCCCCC";
	}
	message_alert +=" Veuillez cocher une case\n";
}


if (cptRadioFalse != cptRadio) {
	return true;
} else {
	alert(message_alert);
	return false;
}
    
}

//Requete HTTP avec javascript
function makeRequest(url) {

        var httpRequest = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // Voir la note ci-dessous à propos de cette ligne
            }
        }
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {}
            }
        }

        if (!httpRequest) {
            alert('Abandon :( Impossible de créer une instance XMLHTTP');
            return false;
        }
        httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
        httpRequest.open('GET', url, true);
        httpRequest.send(null);

    }

    function alertContents(httpRequest) {

        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
                alert(httpRequest.responseText);
            } else {
                alert('Un problème est survenu avec la requête.');
            }
        }

    }
 
 





