// JavaScript Document

window.addEvent('domready', function()
{
	var Tips1 = new Tips($$('.tooltip'), 
	{
			className: '<?php echo $classPrefixToUse ?>Tip'
	});
        
	if($('email'))
        {
            $('email').addEvent('keyup', function(e)
            {
                    var jSonRequest2 = new Request.JSON
                    ({url: "validateEmail.php?username="+$('email').value+"",
                    onComplete : function(stat) {
                            checkstatus(stat);
                    }
                    }).send();
            });
            $('email').addEvent('blur', function(e)
            {
                    var jSonRequest2 = new Request.JSON
                    ({url: "validateEmail.php?username="+$('email').value+"",
                    onComplete : function(stat) {
                            checkstatus(stat);
                    }
                    }).send();
            });
        }
	/*var jSonRequest = new Request.JSON
		({url: "ajaxForm.php",
		onComplete : function(res) {
			checkPassword(res);
		}
		});*/
         var JsonReq= new Request.JSON({
                url: 'ajaxForm.php', method: 'get',
                onComplete: function(res) {
                    /*if(res[0]=='Email incorrect' || res[0]=='Mot de passe incorrect')
                    {}else{myQueue.cancel(JsonReq);}*/
                    checkPassword(res);
                    $("pwd_loader").style.display="none";
                },
                onRequest:function(){
                    $("pwd_loader").style.display="block";
                }

            });
        
          /*var myQueue = new Request.Queue({
                requests: jSonRequest});*/
        var myQueue = new Request.Queue();
        if($('password1'))
        {
            $('password1').addEvent('keyup', function(e2)
            {
                  if($('ajaxFunvalue').value == "need")
                  {
                    myQueue.addRequest("JsonReq", JsonReq);
                    JsonReq.send({data: { username: $("email").value,password: $("password1").value}});
                  }
            });
	
            $('password1').addEvent('blur', function(e2)
            {
                  if($('ajaxFunvalue').value == "need")
                  {
                      myQueue.addRequest("JsonReq", JsonReq);
                      JsonReq.send({data: { username: $("email").value,password: $("password1").value}});
                  }

            });
        }
	
	$('NatureProject').addEvent('change', function(e3) 
	{
		var jSonRequest2 = new Request.JSON
		({url: "json-subcat-load.php?ch="+$('NatureProject').value+"",
		onComplete : function(result_subcat) {
			loadSubcats(result_subcat);
		}
		}).send();
	});
	
});

function loadSubcats(result_subcat)
{
	if( result_subcat.error == false )
	{
		$('Specify').length=0;
		$('Specify').disabled=false
		result_subcat.subcats.each( function(scat){
			 var optionElement = new Element('option', {  
				 'value': scat.subcat_id,
				 'text': scat.subcat_name
			}).inject("Specify");  
		});
	}
	else
	{
		if($('Specify').length>0){$('Specify').length=0;}
		$('Specify').disabled=true
	}
	
}
var firstJson = true;
function checkPassword(res)
{
        //$("pwd_loader").style.display="none";
	if(res[0]=='Email incorrect' || res[0]=='Mot de passe incorrect')
	{
		$('lastName').value = "";
		$('firstName').value = "";
		$('companyName').value = "";
		$('phone').value = "";
		$('fax').value = "";
		$('cellPhone').value = "";
		$('wContacted').selectedIndex = 0;
		$('otherInfo').selectedIndex = 0;
		$('statusCheck').value = "";
		$('existUser').value = "no";
		
		//hide elements
		$('existpass').style.display = '';
		$('password1').className = "validate required";
		$('ajaxFunvalue').value = "need";
		$('existUser').value = "yes";
		$('existrepass').style.display = 'none';
		$('passwordInstruct').style.display = '';
		$('userTitre').style.display='none';
		$('userlname').style.display = 'none';
		$('userfname').style.display = 'none';
		$('usercname').style.display = 'none';
		$('usertelephone').style.display = 'none';
		$('userfax').style.display = 'none';
		$('usercellphone').style.display = 'none';
		$('userotherinfo').style.display = 'none';
		$('usercontacted').style.display = 'none';
		$('usercontacted').style.display = 'none';
		$('usercontacted').style.display = 'none';
		$('usercontacted').style.display = 'none';
		$('everForget').style.display = 'block';
		
		//Password class
		$('password1').className = "validate required passwordreq";
		
		//document.getElementById('userwaiting').style.display='none';
		$('dispError').style.display='block';
		
		if(res[0] == "Email incorrect")
		{	
			$('dispError').innerHTML= "<b>Email invalide</b>";
			
		}	
		if(res[0] == "Mot de passe incorrect")
		{
			document.getElementById('dispError').innerHTML= "Mot de passe invalide";
		}
	}
	else
	{
            firstJson = true;
        //$("pwd_loader").style.display="none";
		$('ajaxFunvalue').value == "";
		//display elements
		$('userTitre').style.display='';
		$('userfname').style.display='';
		$('userlname').style.display='';
		$('userlname').style.clear='left';				
		$('usercname').style.display='';
		$('usertelephone').style.display='';
		$('userfax').style.display='';
		$('userfax').style.clear='left';
		$('usercellphone').style.display='';
		$('usercontacted').style.display='';
		$('userotherinfo').style.display='';
		//document.getElementById('userwaiting').style.display='none';
		$('dispError').style.display='none';
		
		//Display the password forget link always
		$('everForget').style.display="none";
		
		//Password Class
		$('password1').className = "validate required";
		
		//set values
		$('lastName').value = res[1];
		$('firstName').value = res[0];
		$('companyName').value = res[7];
		$('phone').value = res[2];
		$('fax').value = res[4];
		$('cellPhone').value = res[3];
		$('statusCheck').value = "registered";
		$('existUser').value = "yes";
		if(res[5]=="8H-12H")
		{
			$('wContacted').selectedIndex = 1;
		}
		else if(res[5]=="12H-14H")
		{
			$('wContacted').selectedIndex = 2;
		}
		else if(res[5]=="14H-18H")
		{
			$('wContacted').selectedIndex = 3;
		}
		else if(res[5]=="18H-20H")
		{
			$('wContacted').selectedIndex = 4;
		}
		else if(res[5]=="Dès 20H")
		{
			$('wContacted').selectedIndex = 5;
		}
		else if(res[5]=="Peu importe")
		{
			$('wContacted').selectedIndex = 6;
		}
		else
		{
			$('wContacted').selectedIndex = 0;
		}
		if(res[6]=="Dans la presse")
		{
			$('otherInfo').selectedIndex = 1;
		}
		else if(res[6]=="A la radio")
		{
			$('otherInfo').selectedIndex = 2;
		}
		else if(res[6]=="A la TV")
		{
			$('otherInfo').selectedIndex = 3;
		}
		else if(res[6]=="Sur Internet")
		{
			$('otherInfo').selectedIndex = 4;
		}
		else if(res[6]=="Sur un salon")
		{
			$('otherInfo').selectedIndex = 5;
		}
		else if(res[6]=="Bouche à Oreille")
		{
			$('otherInfo').selectedIndex = 6;
		}
		else
		{
			$('otherInfo').selectedIndex = 0;
		}
		if(res[12] == "Mr")
		{
			$('user_titre').selectedIndex = 0;
		}
		else if(res[12] == "Mme")
		{
			$('user_titre').selectedIndex = 1;
		}
		else 
		{
			$('user_titre').selectedIndex = 2;
		}
	}
}
function checkstatus(stat)
{
	if(stat[0]=="validated")
	{
		//Change the style of the email
		$('existEmail').className = " ";
					
		//Display the password field
		$('existpass').style.display = '';
					
		//Hide all other fileds
		$('existrepass').style.display = 'none';
		$('userTitre').style.display = 'none';
		$('userfname').style.display='none';
		$('userlname').style.display='none';
		$('usercname').style.display='none';
		$('usertelephone').style.display='none';
		$('userfax').style.display='none';
		$('usercellphone').style.display='none';
		$('usercontacted').style.display='none';
		$('userotherinfo').style.display='none';
					
		//Reset values to null
		$('password1').value = "";
		$('repassword').value = "";
		$('lastName').value = "";
		$('firstName').value = "";
		$('companyName').value = "";
		$('phone').value = "";
		$('fax').value = "";
		$('cellPhone').value = "";
		$('wContacted').selectedIndex = 0;
		$('otherInfo').selectedIndex = 0;
		$('statusCheck').value = "";
		$('existUser').value = "yes";
		
		//Display the password forget link always
		$('everForget').style.display="block";
		
		//$('userwaiting').style.display='none';
		$('dispError').style.display='none';
		
		//Set a bit for Hidden value
		$('ajaxFunvalue').value = "need";
		
		//Change the class Name of firstPassword Filed
		$('password1').className = "validate required";
		$('repassword').className = "";
		
		//Hide the please wait message
	//	$('userwaiting').className = "largeField";
	//	$('userwaiting').style.display = 'none';
		
		//Display the instruction
		$('passwordInstruct').style.display = '';
		$('passwordInstruct').style.width = '600px';
		$('passwordInstruct').style.marginLeft = '25px';
	}
	
	if (stat[0] == "validatedfailed")
	{
		$('existpass').style.display = '';
		$('passwordInstruct').style.display = 'none';
		$('existrepass').style.display = '';
		$('userTitre').style.display = '';
		$('userlname').style.display = '';
		$('userlname').style.clear = 'left';
		$('userfname').style.display = '';
		$('usercname').style.display = '';
		$('usercname').style.clear = 'left';
		$('usertelephone').style.display = '';
		$('userfax').style.display = '';
		$('userfax').style.clear = 'left';
		$('usercellphone').style.display = '';
		$('userotherinfo').style.display = '';
		$('usercontacted').style.display = '';
		$('usercontacted').style.display = '';
		$('usercontacted').style.display = '';
		$('usercontacted').style.display = '';
			//valeur
		$('ajaxFunvalue').value = "";
		$('existUser').value = "no";
		$('existrepass').value = '';
		$('userlname').value = '';
		$('userfname').value = '';
		$('usercname').value = '';
		$('usertelephone').value = '';
		$('userfax').value = '';
		$('usercellphone').value = '';
		$('userotherinfo').value = '';
		$('usercontacted').value = '';
		$('usercontacted').value = '';
		$('usercontacted').value = '';
		$('usercontacted').value = '';
		$('wContacted').selectedIndex = 0;
		$('otherInfo').selectedIndex = 0;
	}	
	if (stat[0] == "incomplete")
	{
		$('existpass').style.display = 'none';
		$('password1').className = "";
		$('ajaxFunvalue').value = "";
		$('existUser').value = "no";
		$('existrepass').style.display = 'none';
		$('passwordInstruct').style.display = 'none';
		$('userTitre').style.display = 'none';
		$('userlname').style.display = 'none';
		$('userfname').style.display = 'none';
		$('usercname').style.display = 'none';
		$('usertelephone').style.display = 'none';
		$('userfax').style.display = 'none';
		$('usercellphone').style.display = 'none';
		$('userotherinfo').style.display = 'none';
		$('usercontacted').style.display = 'none';
		$('usercontacted').style.display = 'none';
		$('usercontacted').style.display = 'none';
		$('usercontacted').style.display = 'none';
		$('everForget').style.display = 'none';
		$('existUser').value = "no";
		$('existrepass').value = '';
		$('userlname').value = '';
		$('userfname').value = '';
		$('usercname').value = '';
		$('usertelephone').value = '';
		$('userfax').value = '';
		$('usercellphone').value = '';
		$('userotherinfo').value = '';
		$('usercontacted').value = '';
		$('usercontacted').value = '';
		$('usercontacted').value = '';
		$('usercontacted').value = '';
		$('wContacted').selectedIndex = 0;
		$('otherInfo').selectedIndex = 0;
	}
}	
