// -------------------------------------------- check if is valide email --------------------------------------------
function isValidEmail (email, strict)
{
 if ( !strict ) email = email.replace(/^\s+|\s+$/g, '');
 return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}

function loading()
{
	$("#loading").show();
	$("#loading").append('<div id="loading_2"><img src="loading.gif"></div>');
}
//Send post data to page comments --------------------------------------------
function loadPage_post(name,email,text,id) {
$.post("comments.php", { name: name, email: email, text:text ,id:id} );

}


// -------------------------------------------- SEND POST AJAX DATA  --------------------------------------------
function send_comment(name,email,text,id) {
     $.ajax({
		type: 'POST',
		data: {name: name, email: email, text:text ,id:id},
		url: 'comments.php',
		success: function(response) {
            // update status element
			$('#dialog_box').show();
			loading();
			$('#dialog_box').text('');
           
			
			$(function(){
				$.jGrowl("<p>&nbsp</p><p align='center'><b><font color='white'>Message was send successful</font></b></p>", {
					life: 1500,
					theme: 'iphone',
					speed: 'slow',
					animateOpen: { 
					height: "show"
					},
					animateClose: { 
						height: "hide",
						width: "show"
					}
					
				});
			});
			setTimeout("clear_form();",3000);
			$("#comments").text('');
			load_comments()
			
         }
     });
	
 }
 
 
// -------------------------------------------- CHECK IF FORM IS VALIDE DATES --------------------------------------------
function add_comment() {
var name;
var email;
var text;
var data;
	if(document.getElementById('name').value == '')
	{
		alert('Empty field [name]');
	} else if (document.getElementById('email').value == '') 
	{
		alert('Empty field [Email]');
	} else if (document.getElementById('text').value == '')
	{
		alert('Empty field [TEXT]');
	}
	
	name = document.getElementById('name').value;
	email = document.getElementById('email').value;
	var validmail=isValidEmail(email);
	if(validmail == false)
	{
		alert('Invalid field [Email]');
	}
	text = document.getElementById('text').value;
	id = document.getElementById('id').value;
	
	if(name !='' && email !='' && text!='' && validmail == true) { send_comment(name,email,text,id); }
}
//-------------------------------------------- CLEAR FORM --------------------------------------------
function clear_form() {
document.getElementById('email').value = '';
document.getElementById('text').value = '';
document.getElementById('name').value = '';
}

// LOAD COMMENTS ON THE PAGE

function load_comments() {
$('#loading').show();
$('#random_photo').hide("slow");
	$.ajax({
         url: 'random.php?random_photo=true',
		 beforeSend: function(){
			 $('#random_photo').text('');
	 	  },

         success: function(response) {
             // update status element
			 $('#random_photo').html(response); 
			 $("#loading").fadeOut();
             
         },
		 complete:  function(response) {
			 $('#random_photo').show("slow");
		 }
     });
$('#random_photo').show();
 }
 
 
 function load_meteo() {
 $("#weather_2").append('<div id="loading" align="center"><img src="loading.gif"></div>');
     $.ajax({
		type: 'POST',
		url: 'curl.php?url=http://news.yam.md/ro/',
		success: function(response) {
            // update status element
			
			
			$("#weather_body").hide();
			$("#weather_body").html(response);
			var weather = document.getElementById("weather").innerHTML;
			$("#weather_body").text('');
			$("#weather_body").append(weather);
			$(".subhead").css("font-size","11px");
			$("#weather_body").show();
			$("#weather_2").remove();
         }
     });
	
 }
