// JavaScript Document

// --------------------------------------------------------------------------
//
//	FORMS
//
// --------------------------------------------------------------------------


//--------------------------------------------------------------------
//		valid le formulaire 
//--------------------------------------------------------------------
function validData(form){
	
	//alert(form);
	$("#"+form).validate({
		submitHandler: function() { 
			submitData(form,"Sending form...");
			//alert(form);
			return false;
		}
	});
	//
	return false;
}
//--------------------------------------------------------------------
//		envoit le formulaire
//--------------------------------------------------------------------
function submitData(form,dialogText){
	
	//alert(form);
	
	var options = { 
        success:   insertData,  // post-submit callback  
        clearForm: false ,       // clear all form fields after successful submit 
        resetForm: false ,
		dataType:'xml'
    }; 
    // bind form using 'ajaxForm'
    $("#"+form).ajaxSubmit(options);
	$("#"+form).next(".dialog").empty().html(dialogText).slideDown("normal");

	return false;
}
//--------------------------------------------------------------------
//		Callback Formulaire
//--------------------------------------------------------------------
function insertData(backdata)
{
	//alert(backdata);
	
	var root 		= backdata.getElementsByTagName('root');
	var form		= root[0].getAttribute("form");
	var message 	= root[0].getAttribute("message");
	var errors  	= root[0].getAttribute("errors");
	
	//alert(form);
	
	if(errors)
	{
		if(errors == 1)
		{
			//si il y a une seule erreur on affiche le message
			$("#"+form).next(".dialog").empty().html(message).slideDown("normal");
		}
		else
		{
			//si il y a plusieurs erreurs
			var taille = root[0].childNodes.length;	
			
			for (i=0; i<taille; i++)
			{  
				var error = root[0].getElementsByTagName("error")[i].getAttribute("errortype");
				$("#"+form).next(".dialog").empty().prepend(error+'<br />');
			}
		}
	}	
	else
	{	
		$("#"+form).next(".dialog").empty();
		// si pas d'erreurs
		if(form=="login_form")
		{
			$("#"+form).slideUp("slow",function(){$("#"+form).next(".dialog").empty().html(message).slideDown("normal");});
			setTimeout('document.location.reload();',1000);
		}
		else
		{
			$("#"+form).slideUp("slow",function(){$("#"+form).next(".dialog").empty().html(message).slideDown("normal");});
		}
	}
	
}

// -------------------------------------------------------------------------
// -------------------------------------------------------------------------

function producthighlights()
{
	$('#producthighlights .article').mouseover(
											    function()
											    {
													$(this).removeClass("passif");
													$(this).addClass("actif");
												}
											   );

	$('#producthighlights .article').mouseout(
											    function()
											    {
													$(this).removeClass("actif");
													$(this).addClass("passif");
												}
											   );
};

function gotoPage(monLien, fenetre)
{
	if(fenetre == "_self")
	{
		$(location).attr('href',monLien);	
	}
	else if(fenetre == "_blank")
	{
		window.open(monLien);
		return false;
	};
};
