$(document).ready( function () {
	/***********************************/
	/* Save Alerte                     */
	/***********************************/
	$("#searchForm").submit( function() {
		if(document.getElementById('email')){
			if ($("#email").val()==""){
				alert("Please enter an Email address");
				return(false);
			}
			else{
				if(checkEmail($("#email").val())==false){
					alert("Email address is not valid!");
					return(false);
				}
			}
		}
		if ($('#q').val()=="" && $("#listtofollow").val()==""){
			alert("Please fill a search!");
			return(false);
		}
		/**********************************/
		$("#submit_alert").attr("disabled","disabled");
		$('#info').html("&nbsp;");
		$('#tweetretriever_demo').fadeOut('slow');
		$('#loading').fadeIn('slow');
		$.ajax({
			type: "POST",
			url: "snt_tools.php",
			data: "q="+encodeURIComponent($("#q").val())+"&email="+encodeURIComponent($("#email").val())+"&listtofollow="+encodeURIComponent($("#listtofollow").val())+"&select_type="+$('input[name=select_type]:checked').val(),
			success: function(json_string){
				//alert(json_string);
				var response=eval("(" + json_string + ")");
				$('#tweetretriever_demo').html(response.table_content);
				$('#info').html(response.message);
				//Setup Table
				oTable = $('#table_FF').dataTable({
					"bAutoWidth": false,
					"iDisplayLength": 50,
					"oLanguage": {
						"sSearch": "Search all columns:"
					},
					"bSort": false
				});
			},
			complete: function(){
				$('#loading').fadeOut('fast');
				$('#tweetretriever_demo').fadeIn('slow');
				$("#submit_alert").attr("disabled","");
			}
		});
		return(false);
	});
	/***********************************/
	/* CLICK on DEMO                   */
	/***********************************/
	$(".demo").submit( function() {
		/**********************************/
		//alert($(this).serialize());
		//return false;

		$('#info').html("&nbsp;");
		//$('#tweetretriever_demo').animate({ opacity: "hide" }, "slow");//.fadeOut('slow');
		$('#all').animate({ opacity: 0.3 }, "slow");
		$('#loading').fadeIn('slow');
		$.ajax({
			type: "POST",
			url: "demo.php",
			data: $(this).serialize(),
			success: function(json_string){
				//alert(json_string);
				var response=eval("(" + json_string + ")");
				$('#tweetretriever_demo').html(response.table_content);
				$('#info').html(response.message);
			},
			complete: function(){
				$('#loading').fadeOut('fast');
				$('#all').animate({ opacity: 1 }, "slow");

				//$('#tweetretriever_demo').fadeIn('slow');
			}
		});
		return false;
	});
	/***********************************/
	/* Click Save Alert                */		
	/***********************************/
	$("#saveAlert").click( function() {
		if ($("#q").val()=="" && $("#listtofollow").val()==""){
			alert("User/List or Keywords or both must not filled!");
			return(false);
		}
		$('#save_processing').fadeIn('fast');
		//Save Alert
		$.ajax({
			type: "POST",
			url: "snt_tools.php",
			data: "q="+encodeURIComponent($("#q").val())+"&listtofollow="+encodeURIComponent($("#listtofollow").val()),
			success: function(json_string){
				var response=eval("(" + json_string + ")");
				$('#info').html(response.message);
			},
			complete: function(){
				$('#save_processing').fadeOut('fast');
			}
		});
		return(false);
	});
});
