// JavaScript para agregar una enlace nuevo
function makePOSTRequest_ajax(url, parameters) {

	__$.ajax({
		type:"post",
		url:url,
		data:parameters,
		beforeSend:function(){
			//__$('#ubr_alert').html("");
			__$('body').append("<div id='ajax_loader_wait'></div>");
		},
		success:function(msg){
			
			if (msg.substr(0,7)=="success"){
				__$("#image_attached_"+msg.substr(7)).remove();
			}else if (msg.substr(0,9)=="filesonly"){
				__$('#attached_files_area').html(msg.substr(9));
			}else if (msg.substr(0,4)=="fail"){
				//nothing
			}else{
				alertContents_ajax(msg);	
			}
		},
		complete:function(){
			__$('#ajax_loader_wait').remove();
		}
	});
}
function alertContents_ajax(msg) {
		__$('#ubr_alert').html(msg);
}

function make_main_image(addvar) {
	var poststr = encodeURI("main=" + addvar) ;
	makePOSTRequest_ajax('upload_inc/delimg.php', poststr);
}

function delete_file(addvar,sn,sc) {
	var poststr = encodeURI("del=" + addvar) ;
	if (sn)poststr += "&sn=" + encodeURI(sn) + "&sc=" + encodeURI(sc);
	makePOSTRequest_ajax('upload_inc/delimg.php', poststr);

}

function delete_attached_file(addvar,sn, sc){

	var poststr = encodeURI("fdel=" + addvar) ;
	if (sn)poststr += "&sn=" + encodeURI(sn) + "&sc=" + encodeURI(sc);
	makePOSTRequest_ajax('upload_inc/delimg.php', poststr);
	
}var ajx_pub_var="";
var ajx_pub_var2="";
var ajx_pub_var3="";

function anonymous_subscribe(addvar, warn, type,inf) {

 	__$.fn.createDialog({
    	html: inf,
		okLabel: "Suscribirme",
		cancelLabel: "Cerrar",
		okClass:"btn",
		cancelClass:"btn",
		auto:"yes",
		cancelClass:"btn",
		doonok:function (){
			perform_fav_complex(addvar, warn, type,__$('#anonymous_email_info').attr("value"));		
		},
	    opacity: 0.9
    }); 

}

function perform_fav(addvar, warn, type){
	return perform_fav_complex(addvar, warn, type,"")
}

function perform_fav_complex(addvar, warn, type,eml){
	
	ajx_pub_var=type;
	func=function(inf){
		if (inf.indexOf ('id="anonymous_email_div"')>0){
				anonymous_subscribe(addvar, warn, type,inf);
		}else if (inf.indexOf ("success")>0){	
			var re=new RegExp(/\[\[success_type=(.*)\]\]/ig);
			var suc_type=re.exec(inf);
			if (suc_type!="anonymous"){
				if (suc_type){
					__$(".li_fav_button_"+ajx_pub_var+" a").hide(); //input
					__$(".li_fav_button_"+ajx_pub_var+" ."+suc_type[1]).show(); 	
				}else{
					__$(".li_fav_button_"+ajx_pub_var+" a").toggle(); //input
				}
			}
		}else if (inf.indexOf ("notify_anyway")>0){
			__$.fn.createDialog({
				html:inf,
				method:"get",
				okLabel: "Cerrar",
				okClass:"btn",
				auto:"yes",
				progress:"yes",
				cancelClass:"btn",
				opacity: 0.9	
			});	
		}
	}
	
	if (warn=="no"){
		msgType="no_confirm";
		warn="";
		func=function(inf){
			if (inf.indexOf ('id="anonymous_email_div"')>0){
				anonymous_subscribe(addvar, warn, type,inf);
			}else if (inf.indexOf ("success")>0){
				var re=new RegExp(/\[\[success_type=(.*)\]\]/ig);
				var suc_type=re.exec(inf);
				if (suc_type!="anonymous"){
					if (suc_type){
						__$(".li_fav_button_"+ajx_pub_var+" a").hide(); //input
						__$(".li_fav_button_"+ajx_pub_var+" ."+suc_type[1]).show(); 	
					}else{
						__$(".li_fav_button_"+ajx_pub_var+" a").toggle(); //input
					}
				}
			}else{
				__$.fn.createDialog({
					html:inf,
					method:"get",
					okLabel: "Cerrar",
					okClass:"btn",
					auto:"yes",
					progress:"yes",
					cancelClass:"btn",
					/*success:func,*/
					opacity: 0.9	
				});				
			}
		};
	}else{
		msgType="";
	}
	
	if (warn){
		direct_subscribe_warn_complex(addvar,warn,func,eml);
	}else{
		direct_ajax_complex(addvar,func,msgType,eml);
	}
	return false;
}

function direct_ajax(addvar,func,msgType){
	direct_ajax_complex(addvar,func,msgType,"");
}

function direct_ajax_complex(addvar,func,msgType,eml){
	
	if (!func)func=function(inf){ return ""; };
    if (msgType=="no_confirm"){
		
		__$.ajax({
			url:"tus-favoritos.php",
			type:"get",
			data:"ajax=1&"+addvar+"&eml="+encodeURI(eml),
			success:func,
			beforeSend:function(){
				__$('#ubr_alert').html("");
				__$('body').append("<div id='ajax_loader_wait'></div>");
			},
			complete:function(){
				__$('#ajax_loader_wait').remove();
			}			
			
		});
		
	}else{
		__$.fn.createDialog({
			addr: "tus-favoritos.php",
			method:"get",
			data:"ajax=1&"+addvar+"&eml="+encodeURI(eml),
			okLabel: "Cerrar",
			okClass:"btn",
			auto:"yes",
			progress:"yes",
			cancelClass:"btn",
			success:func,
			opacity: 0.9
			
		});
	}
}

function make_ajax_now(addvar) {
	
	return direct_ajax(addvar,"","");
	
}

function make_vote(addvar,vote,e_id,votnum) {

	return make_vote_gen(addvar,vote,e_id,votnum,"comment");
	
}

function make_text_vote(addvar,vote,e_id,votnum) {

	return make_vote_gen(addvar,vote,e_id,votnum,"text");
	
}


function make_thread_vote(addvar,vote,e_id,votnum) {

	return make_vote_gen(addvar,vote,e_id,votnum,"thread");
	
}

function make_vote_gen(addvar,vote,e_id,votnum,vtype) {
	
	var fil="comment_vote.php";
	if (vtype=="thread"){
	   fil="thread_vote.php";
	}else if (vtype=="text"){
	   fil="includes/text_vote.php";
	}
	
	__$.fn.createDialog({
    	addr: fil,
		method:"get",
		data:"id="+encodeURI (addvar)+"&vk="+encodeURI (vote),
		okLabel:"Cerrar",
		okClass:"btn",
		auto:"yes",
		progress:"yes",
		cancelClass:"btn",
		checkMsg: function(inf){
			if (inf.indexOf ("success")>0){
				return false;
			}else return true;
		},
		success: function(inf) {
																																	
				if (inf.indexOf ("success")>0){
					votnum=parseInt(votnum);
					vote=parseInt(vote);
					votnum +=vote;
					res="";
					if (votnum<0)res="<span class='bad_vote'>"+votnum+"</span>";
					if (votnum>0)res="<span class='good_vote'>+"+votnum+"</span>";
					if (votnum==0)res="<span class='normal_vote'>"+votnum+"</span>";
					__$("#"+e_id).html('<div class="viewcount">'+res+'</div>');
					
					__$("#yesnototal_"+e_id).html(parseInt(__$("#yesnototal_"+e_id).html())+1);
					if (vote>0)__$("#yesnohelpful_"+e_id).html(parseInt(__$("#yesnohelpful_"+e_id).html())+1);
					
					__$("#yesno_links_"+e_id).css({display:"none"});
				}
				return true;  
																																			 		},
	    opacity: 0.9
    });
	
}

function direct_subscribe_warn(addvar,warn,func){
	direct_subscribe_warn_complex(addvar,warn,func,"");
}

function direct_subscribe_warn_complex(addvar,warn,func,eml){
	if (!func)func=function(inf){ return ""; };

	__$.fn.createDialog({
    	html: warn,
		okLabel:  "Suscribirme",
		cancelLabel: "Cancelar",
		okClass:"btn",
		cancelClass:"btn",
		auto:"yes",
		cancelClass:"btn",
		okAjax:
				{
					type:'post', 
					url:'tus-favoritos.php?ajax=1&'+addvar+"&eml="+encodeURI(eml),
					dataFunc:function() {return ''; },
					success: function(msg) {
					
						__$.fn.createDialog({
							html: msg,
							method:"get",
							okLabel:  "Cerrar",
							okClass:"btn",
							auto:"yes",
							success:ajx_pub_var2,
							opacity: 0.9
							
						});						
						
						func(msg);
						return false;
					},
					complete: function(){
						ajx_pub_var3="";
					}
				},
	    opacity: 0.9
    }); 


}

function subscribe_warning(addvar, warn) {

 	return direct_subscribe_warn(addvar,warn,"");
	
}


function report_spam(addvar) {
	return report_spam_func(addvar,"text");
}

function nreport_spam(addvar) {
	return report_spam_func(addvar,"news");
}


function report_spam_func(addvar, typ) {

  	
	if (typ!="news" && typ!="comment"){
		typ="text";
	}



 	__$.fn.createDialog({
    	html: __$('#report_spam').html(),
		okLabel: "Denunciar",
		cancelLabel: "Cerrar",
		okClass:"btn",
		cancelClass:"btn",
		auto:"yes",
		cancelClass:"btn",
		okAjax:
				{
					type:'post', 
					url:'report_spam.php',
					dataFunc:function() {return 'reason='+escape(__$('#reason_for_spam').attr("value")); },
					dataNoExec:'id='+escape(addvar)+"&typ="+escape(typ)+"&ajx=1",
					success: function(msg) {
						 __$('#report_error_msg').html(msg);
						 __$('#report_error_msg').show(); 
						 if (__$('#report_success').html()=="success"){
							 var tmp_html=__$('#report_spam_message_display').html();
						
							__$.fn.createDialog({
								html:tmp_html,
								method:"get",
								okLabel:  "Cerrar",
								okClass:"btn",
								auto:"yes",
								opacity: 0.9
							});						
						}	 
						return false;
					}
				},
	    opacity: 0.9
    }); 
}


function call_in_ajax(file){

	__$.ajax({
		type:"post",
		data:"ajx=1",
		url:file,
		success: function(msg){
			update = msg.split('|');
			__$("#"+update[0]).html(update[1]);	
		}
	
	});
	
}