function RunAJAX(div_res, file, vars) {
	$.ajax({
		url: file,
		type: 'POST',
		dataType: 'html',
		data: vars,
		timeout: 5000,
		error: function(){
			RunAJAX(div_res, file, vars);
		},
		success: function(html){
			$(div_res).html(html);
		}
	});

}

function alphenumericPlate(plate){
	
	plate = plate.replace(/[^a-zA-Z0-9]/g, "");
	return plate.toUpperCase();

}	

$(document).ready(function(){
						   
// ----yourInfo----
$('#yourInfo_submit').click(function()
	{
		$('#loading_msg').fadeIn();
		RunAJAX('#loading_msg','modules/actions/profile.php?action=yourInfo','platestate='+$('#platestate').val()+'&firstname='+$('#firstname').val()+'&lastname='+$('#lastname').val()+'&city='+$('#city').val()+'&state='+$('#state').val()+'&sex='+$('#sex').val()+'&email='+$('#email').val()+'&notify_email='+$("input[@name='notify_email']:checked").val()+'&email_type='+$("input[@name='email_type']:checked").val());
	});
// ----yourInfo----

// ----yourPrivacy----
$('#yourPrivacy_submit').click(function()
	{
		if($('#password').val() == $('#confirm_password').val())
		{
			$('#error_password').fadeOut(500);
			$('#loading_msg').fadeIn();
			RunAJAX('#loading_msg','modules/actions/profile.php?action=yourPrivacy','platestate='+$('#platestate').val()+'&password='+$('#password').val());
		}
		else
		{
			$('#error_password').html("Passwords doesn't match");
			$('#error_password').fadeIn(500);
		}
		
	});
// ----yourPrivacy----

// ----yourPlates----
$('#yourPlates_submit').click(function()
	{
		$('#plate2').val(alphenumericPlate($('#plate2').val()));
		$('#plate3').val(alphenumericPlate($('#plate3').val()));
		$('#loading_msg').fadeIn();
		RunAJAX('#loading_msg','modules/actions/profile.php?action=yourPlates','platestate='+$('#platestate').val()+'&nickname='+$('#nickname').val()+'&platestate2='+$('#plate2').val()+'-'+$('#state2').val()+'&platestate3='+$('#plate3').val()+'-'+$('#state3').val());
	});
// ----yourPlates----

// ----yourMessaging----
$('#yourMessaging_submit').click(function()
	{
		if(($("input[@name='notify_mobile']:checked").val() == '1') && ( ($('#mobile1').val() == "") || ($('#mobile2').val() == "") || ($('#mobile3').val() == "") || ($('#provider').val() == "")))
		{
			$('#error_mobile').html("Complete mobile number and Provider are needed");
			$('#error_mobile').fadeIn(500);
		}
		else
		{
			$('#error_mobile').fadeOut(500);
			$('#loading_msg').fadeIn();
			RunAJAX('#loading_msg','modules/actions/profile.php?action=yourMessaging','platestate='+$('#platestate').val()+'&notify_mobile='+$("input[@name='notify_mobile']:checked").val()+'&mobinumber='+$('#mobile1').val()+$('#mobile2').val()+$('#mobile3').val()+'&mobiprovider='+$('#provider').val());
		}
	});
// ----yourMessaging----

// ----Profile Links----
$('#new_link_button').click(function()
	{
		$('#link_new').fadeOut(500,function(){$('#editnewlink').fadeIn(500);});

	});

$('.linkedit').click(function()
	{
		var auxid = $(this).parent().attr('id');
		auxid = auxid.substr(4);
		$('#link'+auxid).fadeOut(500,function(){$('#editlink'+auxid).fadeIn(500);});
	});

$('#linksendingnew').click(function()
	{
		RunAJAX('#links_container',base+'modules/actions/profile.php?action=Links1','platestate='+$('#platestatenew').val()+'&link='+$('#linkinputnew').val());
	});

$('.linksendingedit').click(function()
	{
		var auxid = $(this).parent().attr('id');
		auxid = auxid.substr(8);
		RunAJAX('#link'+auxid,base+'modules/actions/profile.php?action=Links2','platestate='+$('#platestate'+auxid).val()+'&link='+$('#linkinput'+auxid).val()+'&id='+auxid);
		$('#editlink'+auxid).fadeOut(500,function(){$('#link'+auxid).fadeIn(500);});
	});


// ----Profile Links----

// ----Add public message----
$('#commentsubmit').click(function(){
		$('#buttons_row_2').slideUp(250);
		RunAJAX('#comments_list',base+'modules/actions/profile.php?action=sendHonk','from='+$('#from').val()+'&to='+$('#to').val()+'&message='+$('#message').val()+'&type=0&parent=0');
		$('#message').val("");
		$('#from').val("--");

});

// ----Add public message----

// ----Add Follow Plate----
$('#follow_plate').click(function()
	{
		RunAJAX('#follow_plate',base+'modules/actions/profile.php?action=addFollow','platestate='+$('#follow_plate_platestate').val()+'&follow='+$('#follow_plate_follow').val());
	});

// ----Add Follow Plate----

// ----Close Iframe----
$('#close_window').click(function()
	{
		self.parent.tb_remove();
	});

// ----Close Iframe----

// ---- Honks Pagination ----
$('#btNextHonks').click(function(){
		$('#comments_list').html = "";
		RunAJAX('#comments_list',base+'modules/actions/profile.php?action=getHonkPage','p='+(honksActualPage+1)+'&platestate='+$('#to').val());
		honksActualPage++;
		$('#btPrevHonks').show();

});

$('#btPrevHonks').click(function(){
		$('#comments_list').html = "";
		RunAJAX('#comments_list',base+'modules/actions/profile.php?action=getHonkPage','p='+(honksActualPage-1)+'&platestate='+$('#to').val());
		honksActualPage--;
		$('#btNextHonks').show();

});
// ---- Honks Pagination ----


});