letsgo = '';
submitting = '';

function checkEnter(e){ //e is event object passed from function invocation
var characterCode; //literal character code will be stored in this variable

if(e && e.which){ //if which property of event object is supported (NN4)
e = e;
characterCode = e.which; //character code is contained in NN4's which property
}
else{
e = event;
characterCode = e.keyCode; //character code is contained in IE's keyCode property
}

if(characterCode == 13){ //if character code is equal to ascii 13 (if enter key)
return false //return false to the event handler
}
else{
return true //return true to the event handler
}

}

var lwr = 'abcdefghijklmnopqrstuvwxyz0123456789';
function isValid(parm,val) {
  if (parm == "") return false;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}
function isLower(parm) {return isValid(parm,lwr);}

function MM_validateForm() { //v5.0
ok="<img src='images/yes.png'>";
nok="<img src='images/no.png'>";
nokemail="<img src='images/no.png'><span class=\"email_error\"> Email déja existant</span>";
errorcolor='#FF9797';
formname=MM_validateForm.arguments[0];
clearTimeout(submitting);
errors='';

if (formname){
    var i,p,q,nm,test,num,min,max,args=MM_validateForm.arguments;
	for (i=1; i<(args.length-2); i+=3) { test=args[i+2]; val=formname[args[i]]; bg=formname[args[i]].style; bg.backgroundColor = 'white';
      if (val) { nm=val.name; var divname = nm + '_error'; if ((val=val.value)!="") {
		  document.getElementById(divname).innerHTML=ok;
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@'); q=val.indexOf('.');
          if ((p<1 || p==(val.length-1)) || (q<1 || q==(val.length-1))) { errors+='- '+nm+' must contain an e-mail address.\n'; bg.backgroundColor = errorcolor; document.getElementById(divname).innerHTML=nok; }
		} else if (test=='RisLet') {
			if (!isNaN(val)) {
			errors+='- '+nm+' must contain a letter.n'; bg.backgroundColor = errorcolor; document.getElementById(divname).innerHTML=nok;		}
		} else if (test!='R') { 
		num = parseFloat(val);
			  if (isNaN(val)) { 
			  errors+='- '+nm+' must contain a number.n'; bg.backgroundColor = errorcolor; document.getElementById(divname).innerHTML=nok;		  }          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) { errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n'; bg.backgroundColor = errorcolor; document.getElementById(divname).innerHTML=nok; } } } } else if (test.charAt(0) == 'R') {
			errors += '- '+nm+' is required.\n';
			bg.backgroundColor = errorcolor; 
			document.getElementById(divname).innerHTML=nok;
	} }
	
					if (val == 'Veuillez choisir') {
						errors += '- '+nm+' est requis.\n';
						bg.backgroundColor = errorcolor; 
						document.getElementById(divname).innerHTML=nok;
					}
					
					if (nm == 'password') {
							if(formname.password.value.length > 12) {
								errors+='- le mot de passe ne doit pas dépasser 12 caractères.\n';
								formname.password.style.backgroundColor = errorcolor;
								document.getElementById('password_error').innerHTML=nok;
							}
					}
					
					if (nm == 'password2') {
							if(formname.password.value != formname.password2.value) {
								errors+='- les mots de passe ne correspondent pas.\n'; 
								formname.password2.style.backgroundColor = errorcolor;
								document.getElementById('password2_error').innerHTML=nok;
							}
					}
					
					if (nm == 'email2') {
							if(formname.email.value != formname.email2.value) {
								errors+='- les emails ne correspondent pas.\n';
								formname.email2.style.backgroundColor = errorcolor;
								document.getElementById('email2_error').innerHTML=nok;
							}
					}
					
					if (nm == 'security') {
							SHA1encrypt_send();
					}

}
	
	if (letsgo==1) {
		submitting = setTimeout("Submission()",200);
	}
	
	} }
	
function Submission() {
	if (errors) {
		letsgo = '';
		alert('Erreur : certains champs sont manquants ou incorrects, veuillez les corriger afin de continuer');
	} else {
		formname.submit();
	}
}

function unhide(tdname) {
	document.getElementById(tdname).style.display='inline';
}

function hide(tdname) {
	document.getElementById(tdname).style.display='none';
}

function AJAXrequest() {
  var http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('ERROR!\nYou are using an outdated or uncompatible browser!\n\nPlease upgrade to FireFox (http://www.getfirefox.com)');
	 return false;
  }
  return http_request; // return the connection
}

var AJAX = AJAXrequest();
var AJAX2 = AJAXrequest();
//----------------------------------------------------------------------------------------------------//

function SHA1encrypt_send() {

	// get the password to encrypt from the field
	var pswd = document.getElementById('security').value;

	// check if its empty, if not we proceed
	if(pswd!=""){

		// build the post string
		var poststr = 'security=' + pswd + '&action=checkSHA1';

		// specify our AJAX file and also its params
		var url = 'include/AJAX.php';

		// open a new connection to our AJAX file and set as POST
		AJAX.open('POST', url, true);

		//************************************************************************************//
		// [-- DEBUG SECTION --]															*//
		//**********************************************************************************//
		//alert(poststr);
		//*********************************************************************************//

		// set our state change function
		AJAX.onreadystatechange = SHA1encrypt_recieve;

		// set headers and send away!
		AJAX.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		AJAX.setRequestHeader('Content-length', poststr.length);
		AJAX.setRequestHeader('Connection', 'close');
		AJAX.send(poststr);

	}
}

function SHA1encrypt_recieve() {

	// is the response ready?
	if (AJAX.readyState == 4) {
		if (AJAX.status == 200) {

			// ok, our response is ready, lets store it in a var
			SHApswd = '';
			SHApswd = AJAX.responseText;

			security2_send();
			//************************************************************************************//
			// [-- DEBUG SECTION --]															*//
			//**********************************************************************************//
			
			//*********************************************************************************//
		}
	}
}

function security2_send() {

	// get the password to encrypt from the field
	var pswd = document.getElementById('security').value;

	// check if its empty, if not we proceed
	if(pswd!=""){

		// build the post string
		var poststr = 'security=' + pswd + '&action=security2';

		// specify our AJAX file and also its params
		var url = 'include/AJAX.php';

		// open a new connection to our AJAX file and set as POST
		AJAX.open('POST', url, true);

		//************************************************************************************//
		// [-- DEBUG SECTION --]															*//
		//**********************************************************************************//
		//alert(poststr);
		//*********************************************************************************//

		// set our state change function
		AJAX.onreadystatechange = security2_receive;
		

		// set headers and send away!
		AJAX.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		AJAX.setRequestHeader('Content-length', poststr.length);
		AJAX.setRequestHeader('Connection', 'close');
		AJAX.send(poststr);

	}
}

function security2_receive() {
	// is the response ready?
	if (AJAX.readyState == 4) {
		if (AJAX.status == 200) {

			// ok, our response is ready, lets store it in a var
			SHApswd2 = AJAX.responseText;
			//alert(SHApswd);
			//alert(SHApswd2);
			//alert(formname.security2.value);
							if(SHApswd != SHApswd2) {
								errors+='- Security code does not match.\n';
								formname.security.style.backgroundColor = errorcolor;
								document.getElementById('security_error').innerHTML=nok;
								letsgo = '';
							} else {
								formname.security.style.backgroundColor = '#FFFFFF';
								document.getElementById('security_error').innerHTML=ok;
							}
			
			//************************************************************************************//
			// [-- DEBUG SECTION --]															*//
			//**********************************************************************************//
			
			//*********************************************************************************//
		}
	}
}

function removeElement(i,tablename){
    document.getElementById(tablename).deleteRow(i)
}

function addEvent(obj, evType, fn){ 
if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
} else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
} else { 
   return false; 
} 
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function checklogout(url) {
if(window.confirm('Do you want to log out ?')){
window.location = url;
}}

function show_additemdiv (option){
document.getElementById('addnew_' + option).style.display='inline';
document.getElementById('button_' + option).style.display='none';
}

function cancel_additem (option){
document.getElementById('button_' + option).style.display='inline';
document.getElementById('addnew_' + option).style.display='none';
}

function do_additem (option){
theItem = document.getElementById('textfield_' + option).value;
	if(theItem){
		theNewOption = new Option(theItem,theItem);
		document.forms['form1'].elements[option].options[document.forms['form1'].elements[option].options.length] = theNewOption;
		document.getElementById('textfield_' + option).value='';
		document.getElementById('addnew_' + option).style.display='none';
		document.getElementById('button_' + option).style.display='inline';
		
		for(i=0;i<document.getElementById(option).length;i++){
			if(document.getElementById(option).options[i].value==theItem){
				document.getElementById(option).selectedIndex=i
			}
		}	
			
	}else{
		alert('You did not enter anything to add!');	
	}
}

function ChangeStatut(paiement) {
	if(paiement.cgv.checked == true){
		paiement.button.disabled = false;
	} else {
		paiement.button.disabled = true;
	}
}

function popupcentree(page,largeur,hauteur,options) {
var top=(screen.height-hauteur)/2;
var left=(screen.width-largeur)/2;
window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}

var time=60  //Changer ici le temps en seconde
var time2=1000
function CountDown() {
if(time>0) {
if(time>1) {
document.s.Time.value="Il vous reste "+time+" secondes pour voter"
}
else {
document.s.Time.value="Il vous reste "+time+" secondes pour voter"
}
time=time-1
setTimeout("CountDown()", time2)
}
else {
url="/menu/vote_valid"  // Changer ici l'url
Go(url)
}
}
function Go(url) {
window.status="Go !"
document.s.Time.value="Go !"
setTimeout("window.location=url", 500)
}