$(document).ready(function() {
	$("#titlu").charCounter(150, {container: "#counter-titlu", format: "(%1)"});
	$("#catre").charCounter(150, {container: "#counter-catre", format: "(%1)"});
	$("#email").charCounter(150, {container: "#counter-email", format: "(%1)"});
	$("#website").charCounter(150, {container: "#counter-website", format: "(%1)"});
	$("#parola").charCounter(20, {container: "#counter-parola", format: "(%1)"});
	$('.tip').cluetip({
		splitTitle: '|',
		arrows: true,
		dropShadow: false,
		cluetipClass: 'jtip'
	});
	$('#adauga_petitie').ajaxForm({
		dataType: 'json',
		beforeSubmit: function(){
			$('#buttons-adauga').prepend('<img src="/style/images/ajax-loader.gif" alt="" />');
		},
		success:   function(data){
			$('#buttons-adauga img').remove();
			$(".error_long").html("");
			if (data.error) {
				var error_scroll = null
				for (error in data.error){
					$("#error_"+error).html(data.error[error]);
					if (data.error[error])
						error_scroll = error_scroll == null ? error : error_scroll;
				}
				if (data.error['general']) {
					$("#error_general").html(data.error['general']);
				}
				if (data.error['captcha']) {
				    refreshCaptcha();
				}
				$.scrollTo("#"+error_scroll, 500);
			}
			else {
				if (data.msg == 'petitie'){
                    window.location ="/mesaje/ok/petitie_adaugata_cu_succes";
				}
			    else if (data.msg == 'campanie'){
                    window.location ="/mesaje/ok/campanie_adaugata_cu_succes";
				}
				else if (data.msg == 'sondaj'){
                    window.location ="/mesaje/ok/sondaj_adaugat_cu_succes";
				}
                else if (data.msg == 'edit_petitie'){
			        window.location ="/mesaje/ok/petitie_editata_cu_succes";
				}
				else if (data.msg == 'edit_petitie_mail'){
					window.location ="/mesaje/ok/petitie_editata_cu_succes_mail";
				}
                else if (data.msg == 'edit_campanie'){
			        window.location ="/mesaje/ok/campanie_editata_cu_succes";
				}
				else if (data.msg == 'edit_campanie_mail'){
			        window.location ="/mesaje/ok/campanie_editata_cu_succes_mail";
				}
			}
		}
	});
});

function preview(source, destination)
{
    var temp = encodeURI(source.value.replace(/(<([^>]+)>)/ig,""));
    destination.innerHTML = decodeURI(temp.replace(/%20%20/g,'&nbsp;&nbsp;').replace(/%0A/g,'<br />'));
}

function refreshCaptcha()
{
    $.ajax({
	type: 'GET',
	url: 'ajax/refreshCaptcha',
	dataType: 'text',
	success: function(image){
	    $('#captcha_image').attr('src' ,image);
	}
    });
}