var validation = new Array(0,0,0,0,0,0);

function runBox(box){
  RedBox.showInline(box);
  return false;
}

function closeBox(){
  RedBox.close(); 
  return false;
}


function envoyer(value) {
  var approve = document.getElementById("approve");
  var dd = approve.parentNode;
  var check_value = 0;
  if(approve.checked == 1) {
    for (var i=0; i<validation.length; i++){
         if(validation[i] == 0)
	     check_value = 1;
    }
  if(check_value == 0) {
       value.form.submit();
       value.disabled = true;
    }else
        alert('Tous les champs ne sont pas correctement complétés !');
  } else {
    dd.className = "danone";
    approve.parentNode.getElementsByTagName("span")[0].style.display = "inline";
  }
}

function onChecked(value){
  var dd = value.parentNode;
  if(value.checked == 1) {
     dd.className = "welldone";
     value.parentNode.getElementsByTagName("span")[0].style.display = "none";
  } else {
     dd.className = ""; 
  }
}

/*Fonctions pour le login commun*/
function loader_loading() {
  var main_div = document.getElementById("loginpanel");
  main_div.innerHTML = '<div style="width:100%;text-align:center;margin-top:10px;"><img src="/media/bigloader.gif" height="31" border="0" alt="loader" /></div>';
}


/* on reprend */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function checkEmail(whatYouTyped) {
	var dd = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
		return true;
	} else {
		dd.className = "danone";
		whatYouTyped.parentNode.getElementsByTagName("span")[0].innerHTML = "Votre adresse e-mail n'est pas valide !<span class=\"hint-pointer\">&nbsp;</span>";
		whatYouTyped.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		return false;
	}
}


function checkEmailAJAX(typed) {
	var dd = typed.parentNode;
	var txt = typed.value;
	var url = '/login/checkEmail?email=' + txt;
	new Ajax.Request(url, {
		method: 'get',
  		onSuccess: function(transport) {
   		 	if (transport.responseText == '1') {
				dd.className = "welldone";
	                	typed.parentNode.getElementsByTagName("span")[0].style.display = "none";
				validation[2] = 1;
    			} else {
				dd.className = "danone";
	                	typed.parentNode.getElementsByTagName("span")[0].innerHTML = "Cette adresse e-mail n'est pas autorisée ou est déjà utilisée par un membre !<span class=\"hint-pointer\">&nbsp;</span>";
        	        	typed.parentNode.getElementsByTagName("span")[0].style.display = "inline";
				validation[2] = 0;
  			}
		}
	});
}

function checkAllEmail(typed) {
	if(checkEmail(typed)) {
		checkEmailAJAX(typed);
	}
}


function checkCode(whatYouTyped) {
        var dd = whatYouTyped.parentNode;
        var txt = whatYouTyped.value;
        if(/^[A-Za-z]{6}$/.test(txt)){
                dd.className = "welldone";
                whatYouTyped.parentNode.getElementsByTagName("span")[0].style.display = "none";
                validation[5] = 1;
        } else {
                dd.className = "danone";
                whatYouTyped.parentNode.getElementsByTagName("span")[0].style.display = "inline";
                validation[5] = 0;
        }
}




function checkName(whatYouTyped) {
	var dd = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if ((/^[A-Za-z_]+([ -]{1}?[A-Za-z_]+)*$/.test(txt)) && (txt.length > 1)){
		dd.className = "welldone";
		whatYouTyped.parentNode.getElementsByTagName("span")[0].style.display = "none";
		validation[1] = 1;
	} else {
		dd.className = "danone";
		whatYouTyped.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		validation[1] = 0;
	}
}



function checkPrenom(whatYouTyped) {
        var dd = whatYouTyped.parentNode;
        var txt = whatYouTyped.value;
        if ((/^[A-Za-z_]+([ -]{1}?[A-Za-z_]+)*$/.test(txt)) && (txt.length > 1)) {
                dd.className = "welldone";
                whatYouTyped.parentNode.getElementsByTagName("span")[0].style.display = "none";
                validation[0] = 1;
        } else {
                dd.className = "danone";
                whatYouTyped.parentNode.getElementsByTagName("span")[0].style.display = "inline";
                validation[0] = 0;
        }
}


function checkPassword(typed) {
	var dd = typed.parentNode;
	var txt = typed.value;
	if(txt.length >= 4) {
      		dd.className = "welldone";
                typed.parentNode.getElementsByTagName("span")[0].style.display = "none";     
		validation[3] = 1;
	} else {
                dd.className = "danone";
                typed.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		validation[3] = 0;
	}
}


function checkPasswordBis(typed) {
        var dd = typed.parentNode;
        var txt = typed.value;
	var password = document.getElementById("password_inscription").value;
        if(txt == password) {
                dd.className = "welldone";
                typed.parentNode.getElementsByTagName("span")[0].style.display = "none";
		validation[4] = 1;
        } else {
                dd.className = "danone";
                typed.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		validation[4] = 0;
        }
}



function prepareInputsForHints() {
        var inputs = document.getElementsByTagName("input");
        for (var i=0; i<inputs.length; i++){
                // test to see if the hint span exists first
                if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
                        // when the cursor moves away from the field, hide the hint
                        inputs[i].onblur = function () {
				if(this.value.length > 0) {
					switch(this.name) {
						case 'email' : checkAllEmail(this);break;
						case 'password_inscription' : checkPassword(this);break;
						case 'password_bis' : checkPasswordBis(this);break;
						case 'typed_code' : checkCode(this);break;
						case 'approve' : onChecked(this);break;
						case 'optin' : onChecked(this);break;
						case 'nom' : checkName(this);break;
						default: checkPrenom(this);
					}
				} else {
				     this.parentNode.className = "";
 				     this.parentNode.getElementsByTagName("span")[0].style.display = "none";
				     var indice = 0;
				     switch(this.name) {
                                                case 'email' : indice = 2;break;
                                                case 'password_inscription' : indice = 3;break;
                                                case 'password_bis' : indice = 4;break;
                                                case 'typed_code' : indice = 5;break;
                                                case 'nom' : indice = 1;break;
                                                default: indice = 0;
                                        }
				     validation[indice] = 0;
	
				}
                        }
                }
        }
        // repeat the same tests as above for selects
}
addLoadEvent(prepareInputsForHints);
