/* AJAX _ SQL _ CHECK */

function writediv(texte, nom){
	document.getElementById(nom).innerHTML = texte;
}

function verif(valeurs){
	nom = valeurs.name;
	valeur = valeurs.value;
	// document.title = nom+valeur;
	if(valeur != ''){
		if(valeur.length<3) writediv('<span style="color:#cc0000"><img src="images/non-valid.png" height="16"> Trop court</span>', nom);
		else if(valeur.length>30) writediv('<span style="color:#cc0000"><img src="images/non-valid.png" height="16"> Trop long</span>', nom);
		else if(texte = file('verif.php?champs='+nom+'&valeur='+escape(valeur))){
			if(nom == "Pseudo"){
				if(texte == 1) writediv('<span style="color:#cc0000"><img src="images/non-valid.png" height="16"> Déjà pris</span>', nom);
				else if(texte == 2) writediv('<img src="images/valid.png" height="16">', nom);
				else writediv(texte, nom);
			}else{
				if(texte == 1) writediv('<img src="images/non-valid.png" height="16">', nom);
				else if(texte == 2) writediv('<img src="images/valid.png" height="16">', nom);
				else writediv(texte, nom);
			}
		}
	}else retour(nom, 0, "Indispensable");
}

function check(what, min, compare, nom){
	if(what.value!=''){
		if(what.value.length>=min){
			if(typeof(compare) != 'undefined' && compare != ''){
				if(document.getElementsByName(compare)[0].value != what.value){
					writediv('<span style="color:#cc0000"><img src="images/non-valid.png" height="16"> Différents</span>', nom);
					writediv('<span style="color:#cc0000"><img src="images/non-valid.png" height="16"> Différents</span>', compare);
				}else{
					writediv('<img src="images/valid.png" height="16">', nom);
					writediv('<img src="images/valid.png" height="16">', compare);
				}
			}else{
				writediv('<img src="images/valid.png" height="16">', nom);
			}
		}else{
			writediv('<span style="color:#cc0000"><img src="images/non-valid.png" height="16"> Trop court</span>', nom);
		}
	}else retour(nom, 0, "Indispensable");
}

function file(fichier)
{
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else return(false);
	xhr_object.open("GET", fichier, false);
	xhr_object.send(null);
	if(xhr_object.readyState == 4) return(xhr_object.responseText);
	else return(false);
}

/* CODE POSTAL _ ONLY NOMBRES */

function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}

function checkNumber(val, limit) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);

  if (cCode < 48 || cCode > 57 || strLength>limit) {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  return false;
}

/* CHANGEMENT DE BGCOLOR _ BGIMAGE */

function backchange(what){
	var image='';
	if(what.value==1){ image = 'url(images/fond-input.jpg)'; color = '#5F97C9'; }
	else{ image = 'url(images/fond-input2.jpg)'; color = '#DB76E0'; }
	for(i=1; i<8; i++){
		if(typeof(document.getElementById('td'+i)) != 'undefined')	document.getElementById('td'+i).style.backgroundColor = color;
	}
	document.getElementsByName('jj')[0].style.backgroundColor = color;
	document.getElementsByName('mm')[0].style.backgroundColor = color;
	document.getElementsByName('aaaa')[0].style.backgroundColor = color;
	document.getElementsByName('Sexe')[0].style.backgroundColor = color;
	document.getElementsByName('Pays')[0].style.backgroundColor = color;
}

function backchanges(what){
	var image='';
	if(what==1){ image = 'url(images/fond-input.jpg)'; color = '#5F97C9'; }
	else{ image = 'url(images/fond-input2.jpg)'; color = '#DB76E0'; }
	for(i=1; i<8; i++){
		if(typeof(document.getElementById('td'+i)) != 'undefined')	document.getElementById('td'+i).style.backgroundImage = image;
	}
	document.getElementsByName('jj')[0].style.backgroundColor = color;
	document.getElementsByName('mm')[0].style.backgroundColor = color;
	document.getElementsByName('aaaa')[0].style.backgroundColor = color;
	document.getElementsByName('Sexe')[0].style.backgroundColor = color;
	document.getElementsByName('Pays')[0].style.backgroundColor = color;
}

function retour(what, type, message){
	if(message != ''){
		if(type==0) document.getElementById(what).innerHTML = '<span style="color:#cc0000"><img src="images/non-valid.png" height="16"> '+message+'</span>';
	}else{
		if(type==0) document.getElementById(what).innerHTML = '<img src="images/non-valid.png" height="16">';
	}
		if(type==1) document.getElementById(what).innerHTML = '<img src="images/valid.png" height="16">';
}

/* VALIDATION EMAIL */

function verifs(what){
	// alert ('lol') ;
	nom = "Email";
	email = what.value;
	if(email != ''){
	var arobase = email.indexOf("@")
	var point = email.lastIndexOf(".")
	// alert(email+arobase+point);
	if((arobase < 3) || (point + 2 > email.length) || (point < arobase+3)) document.getElementById(nom).innerHTML = '<span style="color:#cc0000"><img src="images/non-valid.png" height="16"> Invalide</span>';
	else if(texte = file('verif.php?champs='+nom+'&valeur='+escape(email))){
		if(texte == 1) document.getElementById(nom).innerHTML = '<span style="color:#cc0000"><img src="images/non-valid.png" height="16"> Déjà pris</span>';
		else if(texte == 2) document.getElementById(nom).innerHTML = '<img src="images/valid.png" height="16">';
		else writediv(texte, nom);
	}
	}else retour(nom, 0, "Indispensable");
}

/* DISPLAY INLINE/NONE */

function expand(id, i){
	// alert(id, i);
	var item  = document.getElementById(id);
	if(i=='1'){
		if(item.style.display == 'none'){
			item.style.display = 'inline';
			item.style.position   = '';
		}
	}else{
		if(item.style.display == 'inline'){
			item.style.display = 'none';
			item.style.position   = 'absolute';
		}
	}
	return false;
}

function expanded(id){
	var item  = document.getElementById(id);
	if(item.style.display == 'none'){
		item.style.display = 'inline';
		item.style.position   = '';
	}else{
		item.style.display = 'none';
		item.style.position   = 'absolute';
	}
	return false;
}

/* DISABLED CHECKBOX */

function interdire(what, j){
	if(what.value==0){
		if(document.getElementsByName(what.name)[1].disabled==false) disableit = true;
		else disableit = false;
	}
	if(j<1) j=99;
	for(i=1; i<=j; i++){
		if(typeof(document.getElementsByName(what.name)[i]) != 'undefined')	document.getElementsByName(what.name)[i].disabled = disableit;
	}
}

function interdir(what, j){
	if(j<1) j=99;
	for(i=1; i<=j; i++){
		if(typeof(document.getElementsByName(what)[i]) != 'undefined')	document.getElementsByName(what)[i].disabled = true;
	}
	document.getElementsByName(what)[0].checked = true;
}

function rouges(what){
	document.getElementsByName(what)[0].style.border="1px solid #FF0000";
}

function updb(id1, id2, table, couleur){
	if(table=="flashs") img = "coeur"; else if(table=="favoris") img = "etoile";
	if(id1 != '' && id2 != '' && table != ''){
		if(texte = file('updb.php?id1='+id1+'&id2='+id2+'&table='+table)){
			if(texte == 1){
				if(table=="flashs"){
					// writediv('<a href="javascript:" onclick="updb('+id1+', '+id2+', \''+table+'\', \''+couleur+'\');"><img src="images/'+img+'-'+couleur+'.png" width="20" height="19" border="0"></a>', table+id2);
					writediv('<u>[Coup de coeur envoyé]</u>', couleur+id2);
				}else if(table=="favoris"){
					// writediv('<img src="images/'+img+'-'+couleur+'.png" width="20" height="19">', table+id2);
					writediv('<u>[Ajouté à vos favoris]</u>', couleur+id2);
				}
			}else if(texte == 3){
				if(table=="favoris"){
					// writediv('<img src="images/'+img+'-'+couleur+'.png" width="20" height="19">', table+id2);
					writediv('<u>[Déjà ajouté à vos favoris]</u>', couleur+id2);
				}else if(table=="flashs"){
					// writediv('<img src="images/'+img+'-'+couleur+'.png" width="20" height="19">', table+id2);
					writediv('<u>[Coup de coeur déjà envoyé aujourd\'hui]</u>', couleur+id2);
				}
			}else if(texte == 99){
				writediv('<u>[Réservé aux Abonnés]</u>', couleur+id2);
			}
		}
	}
}

/* SWAP */

function swap(value, id){
	document.getElementById(id).src=value;
}


