/**
 * Cette classe javascript gère les processus des pages
 * utilisateurs, comme le login, l'inscription, l'édition
 * du profile, etc...
**/

var Process = {
	
    /**
     * ID par défaut
     */
    img_process : 'img_process',        // image loading
    process_message : 'process_message',// affichage message
    process_error : 'process_error',    // affichage erreur
    submit : 'submit',                  // bouton(s) soumission
    
    formSubmit : function(form) {
        
        getById(Process.img_process).style.visibility = 'visible' ;
        getById(Process.process_message).style.visibility = 'hidden' ;
        getById(Process.process_error).style.visibility = 'hidden' ;

        getById(Process.img_process).style.display = 'block' ;
        getById(Process.process_message).style.display = 'none' ;
        getById(Process.process_error).style.display = 'none' ;
                
        getById(Process.submit).style.visibility = 'hidden' ;
        getById(Process.submit).style.display = 'none' ;
        
        vAjax.form_submit(form, Process.refresh) ;
        
        return false ;
    },
    
    /**
     * Soumission formulaire avec check FValidate
     */
	formSubmitValidate : function(form, id_img, id_msg, id_err, id_sub)	{
        
        if (validateForm(form, false, false, false, true, 5) == false) {
            return false ;
        }
        
        // redéfinition des id
        if (id_img) Process.img_process = id_img ;
        if (id_msg) Process.process_message = id_msg ;
        if (id_err) Process.process_error = id_err ;
        if (id_sub) Process.submit = id_sub ;

		getById(Process.img_process).style.visibility = 'visible' ;
		getById(Process.process_message).style.visibility = 'hidden' ;
		getById(Process.process_error).style.visibility = 'hidden' ;

		getById(Process.img_process).style.display = 'block' ;
		getById(Process.process_message).style.display = 'none' ;
		getById(Process.process_error).style.display = 'none' ;
				
		getById(Process.submit).style.visibility = 'hidden' ;
		getById(Process.submit).style.display = 'none' ;
        
		vAjax.form_submit(form, Process.refresh) ;
        
        return false ;
	},
	
	refresh : function()	{
		
        if (vAjax.xhReq.readyState == 4 && vAjax.xhReq.status == 200)	{
		
            // gestion du retour xml différent pour Explorer ou FF
			if (window.ActiveXObject)	{
				//vLogs.addLine(vAjax.xhReq.responseText) ;

				xmldoc = new ActiveXObject("Microsoft.XMLDOM");
				textXML = vAjax.xhReq.responseText;
				xmldoc.async="false";
				if (!xmldoc.loadXML(textXML) )	{
					alert("Une erreur inconnue s'est produite durant le processus.") ;
				}
			} else	{
				//vLogs.addLine(vAjax.xhReq.responseText) ;
				xmldoc = vAjax.xhReq.responseXML ;
			}

			response = xmldoc.getElementsByTagName('response').item(0) ;
			response = response.firstChild.data ;
            
			// on test tout d'abord s'il faut faire une redirection
			redirect = xmldoc.getElementsByTagName('redirect').item(0) ;
			if (redirect && redirect.firstChild && redirect.firstChild.data)	{
                location.href = redirect.firstChild.data ;
                return false ;
			}
			
			/*
			if (response == 'true')	{
			}
			*/

			message = xmldoc.getElementsByTagName('message').item(0) ;
			if (message && message.firstChild)	{

                message = message.firstChild.data ;
				obj = getById(Process.process_message) ;
				obj.innerHTML = message ;

				getById(Process.process_message).style.visibility = 'visible' ;
				getById(Process.process_message).style.display = 'block' ;

			} else	{
				
                error = xmldoc.getElementsByTagName('error').item(0) ;
				if (error && error.firstChild)	{
					
                    error = error.firstChild.data ;

					obj = getById(Process.process_error) ;
					obj.innerHTML = error ;

					getById(Process.process_error).style.visibility = 'visible' ;
					getById(Process.process_error).style.display = 'block' ;
				}
			}
			
			getById(Process.img_process).style.visibility = 'hidden' ;
			getById(Process.img_process).style.display = 'none' ;
			
			getById(Process.submit).style.visibility = 'visible' ;
			getById(Process.submit).style.display = 'block' ;
			
			// on réactive la classe Ajax
			vAjax.busy = false ;
		}
	}	
} ;