$(document).ready( function() {

});

function sendmessage( form)
{
    if ( !checkForm(form ) )  {
		return false;
	}
	
	 $('#sendButton').css('display','none');
	ajax('callback.php',  {
			action: "sendmessage",
			name: $("#name").val(),
			firstname: $("#firstname").val(),
			email: $("#email").val(),
			message: $("#message").val()
		}, 

		function (response) {
			if (response != "ok")   
				message("error","Une erreur est survenue durant l'envoi du message." + response);
			else {
				message("success","Votre message a été envoyé. Merci"); 
				$("#name").val(""); $("#firstname").val(""); $("#email").val("");
				$("#message").val(""); 						   
			}  
		}
	); 
	 $('#sendButton').css('display','block');
	
}


function checkForm( form ) {

	var validForm = true;
	var msg = "Le formulaire n'est pas correctement compl&eacute;té ! ";
	
	if ( form.email.value == "" ) 	{
		form.email.style.borderColor = "red";
		validForm = false;
	}
	else {
		form.email.style.borderColor = "#cccccc";
	}
	
	if ( form.name.value == "" ) 	{
		form.name.style.borderColor = "red";
		validForm = false;
	}
	else {
		form.name.style.borderColor = "#cccccc";
	}
	
	if ( form.message.value == "" ) 	{
		form.message.style.borderColor = "red";
		validForm = false;
	}
	else {
		form.message.style.borderColor = "#cccccc";
	}
	
	if ( validForm == false )  
		message("error",msg); 
	
	return validForm;
}



function ajaxp(url, parameter, onSuccess)
{
    $.ajax({ url: url,
	     type: 'GET',
	     dataType: 'jsonp',
	     data: parameter ,
	     success: onSuccess,
	     error: function() {
		    $('#msg_newson').html("<div class=\"notification  error  \">Erreur survenue durant l'inscription</div>");
		},
	     async: true
    });
}   

function msg_subscribe(type, message) {  
	 $('#msg_newson').html("<div class=\"notification  "+type+"  \">"+message+"</div>"); 
	
}

function subscribe() 
{  
    if ( $('#newson_email').val() == "" ) { 
		msg_subscribe("error", "Veuillez entrer un email valide !"); 
		return;
    }
    
    ajaxp(
	 /* url */
	 "http://public.news-on.be/optin.php",   
	 /* parameter */
	 {
	     account: 'newson_colona',
	     l: $('#newson_listid').val(),  
	     m: $('#newson_email').val()
	 }, 

	 /* onSuccess */
	 function (response) {    

	     if (response == 'ok')  { 
		 	msg_subscribe("success", "Votre inscription a été prise en compte");
		 $('#newson_email').val(''); 
	     }
	     else 
			msg_subscribe("error", "Erreur lors de l'inscription");   
	 }
    );
}

function browse() {      
	browseprocat(false) 
}    

function browseprocat(full) {      
	 ajax('callback.php',  
		{ action: "colona", subaction: "browseprocat", full: full ? 1 : 0  }, 
		function (response) { 
			$("#catalog_procat_container").html(response); }  
	);
}

function showcat( id,pro) {      
	 ajax('callback.php',  
		{ 
			action: "colona", 
			subaction: "showcat",
			id: id,
			pro: pro,
			lang: lang   
	    }, 
		function (response) { 
			$("#catalog_container").html(response);  
		}  
	);
}


function view( id, id_cat, pro) {     
	  
	 ajax('callback.php',  
		{ 
			action: "colona", 
			subaction: "view",
			id: id,
			id_cat: id_cat,
			pro: pro,
			lang: lang   
	    }, 
		function (response) { 
			$("#show_link").fancybox({
					'scrolling' : 'no',
					'titleShow'	: false,
					'content': response
				});
			$("#show_link").trigger('click');  
		}  
	);
} 


function changeCatImg(source, id_cat, hover) {
	if ( id_cat > 0)
		$(source).attr('src',baseurl+"upload/colona/category/"+id_cat+(hover ? "b" : "")+".png")
}
