$(document).ready(function() {

	/* Personal profile details form */
	//site_url = 'http://localhost/agrizlive/';
	site_url = 'http://www.agrizlive.com/';
	$("#profilform").submit(function() {
			$(this).ajaxSubmit({
						beforeSubmit: function(before) {
						$('#loadingprofil').css('display','block');
						$("#resultprofil").html('');
						$("#resultprofil").css('display','none');
						},
						success: function(data) {
							$('#loadingprofil').css('display','none');
							
							if(data.iserror){
								$('#resultprofil').html("<p class='bad'>"+data.message+"</p>").fadeIn(500);
							}
							else{
								$('#resultprofil').html("<p class='good'>"+data.message+"</p>").fadeIn(500);
							}
							setTimeout("$('#resultprofil').fadeOut();",5000);
						},
						dataType: 'json'
				}); 
				return false;
	});
	
	/* Profle Information Form */
	
	$('#infoform').submit(function() {
		$('#loadinginfo').show();
		$.ajax({
  			type: 'POST',
			url: site_url+"ajax/account/profile_modification.php",
			data: $("#infoform").serialize(),
			success: function(data){
				$('#loadinginfo').hide();
				if(data.iserror){
					$('#resultinfo').html("<p class='bad'>"+data.message+"</p>").fadeIn(500);
				}
				else{
					$('#resultinfo').html("<p class='good'>"+data.message+"</p>").fadeIn(500);
				}
				setTimeout("$('#resultinfo').fadeOut();",6000);
			},
			dataType: 'json'
		});									  
		return false;
	});
	
	/* Signup form */
	
	$('#inscriptionform').submit(function() {
		$('#loadinginscription').show();
		$.ajax({
  			type: 'POST',
			url: site_url+"ajax/account/signup.php",
			data: $("#inscriptionform").serialize(),
			success: function(data){
				$('#loadinginscription').hide();
				if(data.iserror){
					$('#resultinscription').html("<p class='bad'>"+data.message+"</p>").fadeIn(500);
				}
				else{
					$('#resultinscription').html("<p class='good'>"+data.message+"</p>").fadeIn(500);
				}
				setTimeout("$('#resultinscription').fadeOut();",6000);
			},
			dataType: 'json'
		});									  
		return false;
	});
	
	
	
	
	$("#articleform").submit(function() {
			$(this).ajaxSubmit({
						beforeSubmit: function(before) {
						$('#loadingsubmit').css('display','block');
						$("#resultsubmit").html('');
						$("#resultsubmit").css('display','none');
						},
						success: function(data) {
							$('#loadingsubmit').css('display','none');
							if(data.iserror){
								$('#resultsubmit').html("<p class='bad'>"+data.message+"</p>").fadeIn(500);
							}
							else{
								$('#resultsubmit').html("<p class='good'>"+data.message+"</p>").fadeIn(500);
							}
						},
						dataType: 'json'
				}); 
				return false;
	});
	
	
	/* Comment Form */
	
	$("#commentForm").submit(function() {
			$(this).ajaxSubmit({
						beforeSubmit: function(before) {
						$('#loadingcomment').css('display','block');
						$("#resultcomment").html('');
						$("#resultcomment").css('display','none');
						},
						success: function(data) {
							$('#loadingcomment').css('display','none');
							if(data.iserror){
								$('#resultcomment').html("<p class='bad'>"+data.message+"</p>").fadeIn(500);
							}
							else{
								$('#resultcomment').html("<p class='good'>"+data.message+"</p>").fadeIn(500);
								var count = $('div.user_comments').length;
								comment = data.comment;
								comment = comment.replace(/comment_no/g, count+1);
								$('.cmt').append(comment);
								if($('#si_image_com').height() > 0)
								{
									$('#si_image_com').src = site_url+'captcha/securimage_show_medium.php?si_form_id=com&sid=' + Math.random();
								}
							}
							//setTimeout("$('#resultsubmit').fadeOut();",5000);
						},
						dataType: 'json'
				}); 
				return false;
	});
	
	/* article submit */
	
	$("#passwordform").submit(function() {
			$(this).ajaxSubmit({
						beforeSubmit: function(before) {
						$('#loadingpassword').css('display','block');
						$("#resultpassword").html('');
						$("#resultpassword").css('display','none');
						},
						success: function(data) {
							$('#loadingpassword').css('display','none');
							if(data.iserror){
								$('#resultpassword').html("<p class='bad'>"+data.message+"</p>").fadeIn(500);
							}
							else{
								$('#resultpassword').html("<p class='good'>"+data.message+"</p>").fadeIn(500);
							}
							setTimeout("$('#resultpassword').fadeOut();",5000);
						},
						dataType: 'json'
				}); 
				return false;
	});
	
	
	$("#subscribe").submit(function() {
			$(this).ajaxSubmit({
						beforeSubmit: function(before) {
						$('#loadingsubscribe').css('display','block');
						$("#resultsubscribe").html('');
						$("#resultsubscribe").css('display','none');
						//$('.form .ok input').set('disabled',true);
						},
						success: function(data) {
							$('#loadingsubscribe').css('display','none');
							
							if(data.iserror){
								$('#resultsubscribe').html("<p class='bad'>"+data.message+"</p>").fadeIn(500);
							}
							else{
								$('#resultsubscribe').html("<p class='good'>"+data.message+"</p>").fadeIn(500);
								$('#foll').fadeOut(0);
							}
							//setTimeout("$('#resultsubscribe').fadeOut();",5000);
						},
						dataType: 'json'
				}); 
				return false;
	});
	
	$('#seoform').submit(function() {
		$('#loadinginscription').show();
		$.ajax({
  			type: 'POST',
			url: site_url+"ajax/serp/position.php",
			data: $("#seoform").serialize(),
			success: function(data){
				$('#loadinginscription').hide();
				if(data.iserror){
					$('#resultinscription').html("<p class='bad'>"+data.message+"</p>").fadeIn(500);
				}
				else{
					$('#resultinscription').html("<p class='good' style='text-align: left'>"+data.message+"</p>").fadeIn(500);
				}
			},
			dataType: 'json'
		});									  
		return false;
	});
	
	$('#pagerankform').submit(function() {
		$('#loadinginscription').show();
		$.ajax({
  			type: 'POST',
			url: site_url+"ajax/serp/pagerank.php",
			data: $("#pagerankform").serialize(),
			success: function(data){
				$('#loadinginscription').hide();
				if(data.iserror){
					$('#resultinscription').html("<p class='bad'>"+data.message+"</p>").fadeIn(500);
				}
				else{
					$('#resultinscription').html("<p class='good' style='text-align: left'>"+data.message+"</p>").fadeIn(500);
				}
			},
			dataType: 'json'
		});									  
		return false;
	});
	
	$("#directory").submit(function() {
			$(this).ajaxSubmit({
						beforeSubmit: function(before) {
						$('#loadingdirectory').css('display','block');
						$("#resultdirectory").html('');
						$("#resultdirectory").css('display','none');
						//$('.form .ok input').set('disabled',true);
						},
						success: function(data) {
							$('#loadingdirectory').css('display','none');	
							if(data.iserror){
								if(data.message == 'Link already exists.')
								{
									$('#resultdirectory').html("<p class='bad'>"+data.message+"<br />Vote for this link <a href='directory/"+data.url+".html'>here</a></p>").fadeIn(500);
								}
								else
								{
									$('#resultdirectory').html("<p class='bad'>"+data.message+"</p>").fadeIn(500);
								}	
							}
							else{
								$('#resultdirectory').html("<p class='good'>"+data.message+"<br />View your link <a href='directory/"+data.url+".html'>here</a></p>").fadeIn(500);
								if($('#si_image_com').height() > 0)
								{
									$('#si_image_com').src = site_url+'captcha/securimage_show_medium.php?si_form_id=com&sid=' + Math.random();
								}
							}
							//setTimeout("$('#resultsubmit').fadeOut();",5000);
						},
						dataType: 'json'
				}); 
				return false;
	});
	
	
	
	$("#contactForm").submit(function() {
			$(this).ajaxSubmit({
						beforeSubmit: function(before) {
						$('#loadingcontact').css('display','block');
						$("#resultcontact").html('');
						$("#resultcontact").css('display','none');
						//$('.form .ok input').set('disabled',true);
						},
						success: function(data) {
							$('#loadingcontact').css('display','none');
							
							if(data.iserror){
								$('#resultcontact').html("<p class='bad'>"+data.message+"</p>").fadeIn(500);
							}
							else{
								$('#resultcontact').html("<p class='good'>"+data.message+"</p>").fadeIn(500);
							}
							setTimeout("$('#resultcontact').fadeOut();",5000);
						},
						dataType: 'json'
				}); 
				return false;
	});
});

function vote(id,type)
{
	$.ajax({
		type: 'POST',
		url: site_url+"ajax/article/vote.php",
		data: "id="+id+"&type="+type,
		success: function(data){
			if(type == 'like')
			{
				$('#vote'+id).html('Like / Agree ('+data.vote+')');
			}
			else
			{
				$('#votebf'+id).html('Unlike / Disagree ('+data.vote+')');
			}
			$('.total_vote_'+id).html(data.total_vote).fadeOut('slow').fadeIn('slow');
		},
		dataType: 'json'
	});	
}


function voteComment(id,type)
{
	$.ajax({
		type: 'POST',
		url: site_url+"ajax/article/comment_vote.php",
		data: "id="+id+"&type="+type,
		success: function(data){
			$('#comment_'+id).html('('+data.vote+')');
		},
		dataType: 'json'
	});	
}

function dispHiddenComment(id)
{
	if($('.texte_p_'+id).css('display') == 'none'){
		$('.texte_p_'+id).css('display','');
	}
	else{
		$('.texte_p_'+id).css('display','none');
	}	
}

function subscribe(u_id,s_id,type)
{
	$.ajax({
		type: 'POST',
		url: site_url+"ajax/account/subscribe.php",
		data: "u_id="+u_id+"&follow_id="+s_id+"&type="+type,
		success: function(data){
			if(data.iserror)
			{
				$('#subscribe'+s_id).html(data.message);
			}
			else
			{
				$('#subscribe'+s_id).html(data.message);
				if(type == 'unsubscribe'){
					$('.followers_post_'+s_id).fadeOut(500);
				}	
			}
		},
		dataType: 'json'
	});	
}

function displayPaypal()
{
	var n = $("input:checked").length;
	if(n == 1){
		$('#paypal_email').slideDown('slow', function() {
			// Animation complete.
		});
	}
	else{
		$('#paypal_email').slideUp('slow', function() {
			// Animation complete.
		});
	}
}

function getMoreReferRecords()
{
	var lim = $('#refer_total').val();
	$.ajax({
		type: 'POST',
		url: site_url+"ajax/account/getRefferals.php",
		data: "limit="+lim,
		success: function(data){
			if(data.iserror == 'true'){
				$('#refer_more').val('No more records');
			}
			else{
				$('.referalls p:last').after(data.result);
				ne = parseInt(lim)+5;
				$('#refer_total').val(ne);
			}	
		},
		dataType: 'json'
	});
}


function checkCount()
{
	var len = $('#description').val().length;
	$('#desc_length').html('Description Length : '+ len);
}
