//ajax

function newsletter() {
	if($('#email').val()!=""){
		email=$('#email').val();
		$('#search-box2').html("<div style='height:20px;margin-right:70px;padding-top:5px' align='center'><img src='"+url_base+"css/images/loader.gif' border='0'></div>");
		$.ajax({
			url: url_base+'ajax/newsletter.php',
			type: 'GET',
			cache: false,
			dataType: 'html',
			data: '&email='+email,
			success: function(responseText){
				$('#search-box2').html(responseText);
			}
		});
	}
}

function sondaj(id, nr, j) {
	id_ales=ales=0;
	for(i=1;i<=nr;i++) {
		if($('#sondaj'+j+" .raspuns"+i).is(':checked')==true) {
			ales=1;
			id_ales=$('#sondaj'+j+" .raspuns"+i).val();
			break;
		}
	}
	if(ales==0) {
		alert('Alege o optiune!');
	}else{
		$('#sondaj'+j).html("<div style='height:170px;margin:0 auto;' align='center'><br /><br /><br /><br /><br /><img src='"+url_base+"css/images/loader2.gif' border='0'></div>");
		$.ajax({
			url: url_base+'ajax/sondaj.php',
			type: 'GET',
			cache: false,
			dataType: 'html',
			data: '&id_sondaj='+id+'&j='+j+'&rasp='+id_ales,
			success: function(responseText){
				//alert(responseText);
				arr=responseText.split("___");
				$('#sondaj'+j).html(arr[1]);
				vals=arr[0].split("_");
				for(i=1;i<=vals.length;i++) {
					$('#sondaj'+j+" div#vote"+i).animate({ width: Math.round(vals[i-1]*1.45)+"px"}, 1000 );
				}
			}
		});
		
	}
}
function vf_field(id,val) 
{
    if(id=="repassword") {
		val+="&val2="+$('#password').val();  
    }
	$.ajax({
		url: url_base+'ajax/vf_field.php',
		type: 'GET',
		cache: false,
		dataType: 'html',
		data: 'tip='+id+'&val='+encodeURI(val),
		success: function(responseText){
			$('#span-'+id).html(responseText);
		}
	});
}

function show_help(id) 
{
	$.ajax({
		url: url_base+'ajax/show_help.php',
		type: 'GET',
		cache: false,
		dataType: 'html',
		data: 'tip='+id,
		success: function(responseText){
			$('#span-'+id).html(responseText);
		}
	});
}
function update_mail_field(table , id) 
{
	$.ajax({
		url: url_base+'ajax/update_mail_filed.php',
		type: 'GET',
		cache: false,
		dataType: 'html',
		data: 'table=' + table +'&id='+id,
		success: function(responseText){
			$('#titlu'+id).css({
				"font-weight" : "normal"
			});
			$('#tr'+id+' td a, #tr'+id+' td span').css({
				"font-weight": "normal",
				"color": "#000"
			});
		}
	});
}
function raspunde_mesaj(title, descr, from, to) 
{
	var nosubmit = 0;
	if(title == ""){
		$('#span_rasp_titlu').html('Completati titlu');
		nosubmit = 1;
	}
	if(title.length>30){
		$('#span_rasp_titlu').html('< 30 caractere');
		nosubmit = 1;
	}	
	if(descr == ""){
		$('#span_rasp_mesaj').html('Completati mesaj');
		nosubmit = 1;
	}	
	if(descr.length>1000){
		$('#span_rasp_mesaj').html('< 1000 caractere');
		nosubmit = 1;
	}
	if(nosubmit == 1){
	return false;
	}
	
	$.ajax({
		url: url_base+'ajax/raspunde_mesaj.php',
		type: 'GET',
		cache: false,
		dataType: 'html',
		data: 'title='+title+'&descr='+descr +'&from='+from +'&to='+to,
		success: function(responseText){
			location.reload(); 
		}
	});
}
function delete_mail(table,id) 
{
	$.ajax({
		url: url_base+'ajax/delete_mail.php',
		type: 'GET',
		cache: false,
		dataType: 'html',
		data: 'table=' + table +'&id='+id,
		success: function(responseText){
			location.reload();
		}
	});
}
function update_mail_as(table,id,as) 
{
	$.ajax({
		url: url_base+'ajax/update_mail_as.php',
		type: 'GET',
		cache: false,
		dataType: 'html',
		data: 'table=' + table +'&id='+id +'&as='+as,
		success: function(responseText){
			location.reload();
		}
	});
}

function add_comment(id, table, id_user){
	if($('#desc').val()!=""){
		val=$('#nr1').html();
		$.ajax({
			url: url_base+'ajax/add_comm.php',
			type: 'GET',
			cache: false,
			dataType: 'html',
			data: 'id_base_user='+id_user+'&id='+id+'&desc='+encodeURI( $("#desc").val() )+'&table='+table,
			success: function(responseText){
				$('#nr1').html(parseInt(val)+1);
				$('#nr2').html(parseInt(val)+1);
				$('#comment').hide();
				$('#comments').prepend(responseText);
				$('#desc').val("");
				$('#noComm').hide();
			}
		});
	}
}

function mycallbackfunc(v,m,f){
	if(v==true){
		save_log();
	}else{
		location.href="http://www.google.com/";
	}
}
function save_log(){
	$.ajax({
		url: url_base+'ajax/log.php',
		type: 'GET',
		cache: false,
		dataType: 'html',
		data: '',
		success: function(responseText){}
	});
}

function mesaj_admin(from) 
{
	title=$("#titlu").val();
	descr=$("#mesaj").val();
	to=$("input[name='catre']:checked").val();
	user=$("#id_user option:selected").val();
	
	var nosubmit = 0;
	if($("#titlu").val() == ""){
		$('#span_rasp_titlu').html('Completati titlu');
		nosubmit = 1;
	}
	if($("#titlu").val().length>30){
		$('#span_rasp_titlu').html('< 30 caractere');
		nosubmit = 1;
	}	
	if($("#mesaj").val() == ""){
		$('#span_rasp_mesaj').html('Completati mesaj');
		nosubmit = 1;
	}	
	if($("#mesaj").val().length>1000){
		$('#span_rasp_mesaj').html('< 1000 caractere');
		nosubmit = 1;
	}
	if($("input[name='catre']:checked").val()=="user" && ($("#id_user option:selected").val()=="" || $("#id_user option:selected").val()=="0")){
		$('#span_rasp_id_user').html('Alege user');
		nosubmit = 1;
	}
	
	if(nosubmit == 1){
		return false;
	}
	
	$.ajax({
		url: url_base+'ajax/mesaj_admin.php',
		type: 'GET',
		cache: false,
		dataType: 'html',
		data: 'title='+title+'&descr='+descr +'&from='+from +'&to='+to+'&user='+user,
		success: function(responseText){
			location.reload(); 
		}
	});
}

function raspunde_mesaj_admin(title, descr, from, to) 
{
	var nosubmit = 0;
	if(title == ""){
		$('#span_rasp_titlu').html('Completati titlu');
		nosubmit = 1;
	}
	if(title.length>30){
		$('#span_rasp_titlu').html('< 30 caractere');
		nosubmit = 1;
	}	
	if(descr == ""){
		$('#span_rasp_mesaj').html('Completati mesaj');
		nosubmit = 1;
	}	
	if(descr.length>1000){
		$('#span_rasp_mesaj').html('< 1000 caractere');
		nosubmit = 1;
	}
	if(nosubmit == 1){
		return false;
	}
	
	$.ajax({
		url: url_base+'ajax/mesaj_admin.php',
		type: 'GET',
		cache: false,
		dataType: 'html',
		data: 'title='+title+'&descr='+descr +'&from='+from +'&to=user&user='+to,
		success: function(responseText){
			location.reload(); 
		}
	});
}
