try{if(!window.console||!console.firebug){var a=["log","debug","info","warn","error","assert","dir","dirxml","group","groupEnd","time","timeEnd","count","trace","profile","profileEnd"];window.console={};for(var i=0;i<a.length;++i){window.console[a[i]]=function(){}}}}catch(e){};

		var TweenWait = 300,
		popinFadeOutMS = 5000;
		
		function doAjax(UrlResource, Data) {
		new Request({
			"url": UrlResource,
			"data": Data,
			"headers": {'X-Request': 'JSON'},
			"onSuccess": function(req) {
				var r = JSON.decode(req);
				if(r.status > 0) {
					return r;
				} else {
					switch(r.status) {
						case -2:
							alert("Internal error");						
						break;                
					}                                                                                                                                                                                                    
				}
				return null;
			}
		}).send();
		}
		
		function doAjaxCallback(UrlResource, Data, Callback, CallbackArgs) {
		new Request.JSON({
			"url": UrlResource,
			"data": Data,
			/* "headers": {'X-Request': 'JSON'}, */
			"onSuccess": function(req) {
				var r = req;
				if(r.status > 0) {
					if (Callback)
						Callback(r, CallbackArgs);
				} else {
					switch(r.status) {
						case -2:
							alert("Internal error");
						break;                
					}                                                                                                                                                                                                                                   
				}
			}
		}).send();
		}
		
		function doPopinRound(headline, textmass, buttons, overlay) {
		var popin = $("popin_round");
		
		if (popin != null) {
			return;
		}

        var style = (overlay) ? '' : "background:none;";
			
		$(document.body).adopt(
				popin = new Element("div", {"id": "popin_over", "class": "popin_over_bg", "opacity": 1, "style": style}).adopt(
					 new Element("div", {"class": "popin_round", "id": "popin_round", "opacity": 1, "style": "left:29%;top:20%"}).adopt($$(
						new Element("div", {"class": "content_box"}).adopt($$(
							new Element("div", {"class": "content_round"}).adopt($$(
							new Element("h3", {"html": headline, "class": "popin_headline"}),
							new Element("p", {"html": textmass, "class": "popin_content"}),
							new Element("div",{"html": buttons, "class": "popin_buttons"})
						))
					))
				))
			)
		);
		
		popin.setStyle("left", window.getSize().x/2 - popin.getStyle("width").toInt()/2);
		new Fx.Tween(popin, {duration: TweenWait, onComplete: function() {
			(function() {
				new Fx.Tween(popin, {duration: TweenWait, onComplete: function() {
					popin.dispose();
				}}).start("opacity", 0);
			});
		}}).start();
		}
		
		function doPopinRoundSmall(headline, textmass, buttons, overlay) {
		var popin = $("popin_round");
		
		if (popin != null) {
			return;
		}
		if (overlay) {
			var style = "";
		} else {
			var style = "background:none;";
		}
		
		$(document.body).adopt(
				popin = new Element("div", {"id": "popin_over_small", "class": "popin_over_bg", "opacity": 1, "style": style}).adopt(
					 new Element("div", {"class": "popin_round_small", "id": "popin_round_small", "opacity": 1, "style": "left:33%;top:10%"}).adopt($$(
						new Element("div", {"class": "content_box_small"}).adopt($$(
							new Element("div", {"class": "content_round_small"}).adopt($$(
							new Element("h3", {"html": headline, "class": "popin_headline_small"}),
							new Element("p", {"html": textmass, "class": "popin_content_small"}),
							new Element("div",{"html": buttons, "class": "popin_buttons_small"})
						))
					))
				))
			)
		);
		
		popin.setStyle("left", window.getSize().x/2 - popin.getStyle("width").toInt()/2);
		new Fx.Tween(popin, {duration: TweenWait, onComplete: function() {
			(function() {
				new Fx.Tween(popin, {duration: TweenWait, onComplete: function() {
					popin.dispose();
				}}).start("opacity", 0);
			});
		}}).start();
		}
		
		function doPopin(headline, textmass, buttons) {
		var popin = $("popin");
		
		if (popin != null)
			return;
		
		$(document.body).adopt(
			popin = new Element("div", {"id": "popin", "opacity": 0}).adopt(
				new Element("div", {"class": "content"}).adopt($$(
					new Element("h3", {"html": headline}),
					new Element("p", {"html": textmass}),
					new Element("div",{"html": buttons})
				))
			)
		);
		popin.setStyle("top", window.getSize().y/2 + window.getScroll().y - popin.getSize().y/2);
		popin.setStyle("left", window.getSize().x/2 - popin.getStyle("width").toInt()/2);
		new Fx.Tween(popin, {duration: TweenWait, onComplete: function() {
			(function() {
				new Fx.Tween(popin, {duration: TweenWait, onComplete: function() {
					popin.dispose();
				}}).start("opacity", 0);
			});//.delay(popinFadeOutMS);
		}}).start("opacity", 1);
		}
		
		window.addEvent("domready", function(){
		$$('a.submitform').addEvent('click', function(e){
			e.preventDefault();
			$(e.target).getParent('form').submit();	
		});
		});
		
		function validateEmail( email ) {
		apos = email.value.indexOf("@");
		dotpos = email.value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			return false;
		}
		return true;
		}
		
		function adtimestate(state) {
		if (state) {
			a = $('timebox');
			a.setStyle("display", "block");
		}
		else {
			a = $('timebox');
			a.setStyle("display", "none");
		}
		}
		
		// todo move to differnt js-files
		
		function adtype(field1,field2,id) {			
			a = $('link_1');
			b = $('link_2');
			c = $('link_3');
			d = $('link_4');
			
			var urlbox = $$(".urlbox");
			if(urlbox != null) {
				urlbox.addClass("hide");
			}
			
			// show video box
			if( field1 == 'box1' || field1 == 'box3' ) {		
				$$(".videobox").removeClass('hide');		
			}
			
			// show banner box
			if( field2 == 'box2' || field2 == 'box6' || field1 == 'box5' ){		
				$$(".bannerbox").removeClass('hide');
			}
			
			// show image box
			if( field1 == 'box4' ) {
				$$(".imagebox").removeClass('hide');
			}
			
			var field1_info = $('info_' + field1);
			var field2_info = $('info_' + field2);
			
			$$('.information_campaign_box_upload').addClass('hide');
			
			if (field1_info!=null) {
				field1_info.removeClass('hide');		
			}
			
			if (field2_info!=null) {
				field2_info.removeClass('hide');
			}
			
			if (id) {
				if (id=="link_1") {
					a.setStyle("display", "block");
					b.setStyle("display", "none");
					c.setStyle("display", "none");
					d.setStyle("display", "none");
					$('upload_label_1').set('style', '');
					$('upload_label_2').set('style', 'color: #c0c0c0');
					$('upload_label_3').set('style', 'color: #c0c0c0');
					$('upload_label_4').set('style', 'color: #c0c0c0');
				}
				
				if (id=="link_2") {
					a.setStyle("display", "none");
					b.setStyle("display", "block");
					c.setStyle("display", "none");
					d.setStyle("display", "none");
					$('upload_label_2').set('style', '');
					$('upload_label_1').set('style', 'color: #c0c0c0');
					$('upload_label_3').set('style', 'color: #c0c0c0');
					$('upload_label_4').set('style', 'color: #c0c0c0');
				}
				
				if (id=="link_3") {
					a.setStyle("display", "none");
					b.setStyle("display", "none");
					c.setStyle("display", "block");
					d.setStyle("display", "none");
					$('upload_label_3').set('style', '');
					$('upload_label_1').set('style', 'color: #c0c0c0');
					$('upload_label_2').set('style', 'color: #c0c0c0');
					$('upload_label_4').set('style', 'color: #c0c0c0');
				}
				
				if (id=="link_4") {
					a.setStyle("display", "none");
					b.setStyle("display", "none");
					c.setStyle("display", "none");
					d.setStyle("display", "block");
					$('upload_label_4').set('style', '');
					$('upload_label_1').set('style', 'color: #c0c0c0');
					$('upload_label_2').set('style', 'color: #c0c0c0');
					$('upload_label_3').set('style', 'color: #c0c0c0');
				}
				
				if (id=="link_5") {
					a.setStyle("display", "none");
					b.setStyle("display", "none");
					c.setStyle("display", "none");
					d.setStyle("display", "none");
					$('upload_label_1').set('style', 'color: #c0c0c0');
					$('upload_label_2').set('style', 'color: #c0c0c0');
					$('upload_label_3').set('style', 'color: #c0c0c0');
					$('upload_label_4').set('style', 'color: #c0c0c0');
				}
			}
			
			var swiffydiv = $$('object[id^=Swiff_]');
			
			if (swiffydiv!=null) {
				var swiffybrowse = swiffydiv.getParent();	
				swiffybrowse.dispose();
			}
				
			var swiffy1 = new FancyUpload2($('status1'), $('list'), $('selecting'), $('uploading1'), $('upload_error'), 1,   1, {
				url: $('form_upload_material').action,
				fieldName: 'uploaded_file',
				path: '/flash/Swiff.Uploader.swf',
				limitSize: 100000 * 1024 * 1024, // x*y*z where x will be the file size in mb
				onLoad: function() {
					var fallback_1 = $('fallback_1');
					var status = $('status1');
					if (status!=null) {
						$('status1').removeClass('hide');								
					}
				},					
				// The changed parts!
				debug: true, // enable logs, uses console.log
				target: 'browse_1', // the element for the overlay (Flash 10 only)
				fileInvalid: function(){
					alert("the file is not valid");
				}
			});
			
			var swiffy2 = new FancyUpload2($('status2'), $('list'), $('selecting'), $('uploading2'), $('upload_error'), 3,   1, {
				url: $('form_upload_material').action,
				fieldName: 'uploaded_file',
				path: '/flash/Swiff.Uploader.swf',
				limitSize: 100000 * 1024 * 1024, // x*y*z where x will be the file size in mb
				onLoad: function() {
					var fallback_1 = $('fallback_1');
					var status = $('status2');
					if (status!=null) {
						$('status2').removeClass('hide');
					}
				},
				// The changed parts!
				debug: true, // enable logs, uses console.log
				target: 'browse_2', // the element for the overlay (Flash 10 only)
				fileInvalid: function(){
					alert("the file is not valid");
				}								
			});
			
			var swiffy3 = new FancyUpload2($('status3'), $('list'), $('selecting'), $('uploading3'), $('upload_error'), 1, 2, {
				url: $('form_upload_material').action,
				fieldName: 'uploaded_file',
				path: '/flash/Swiff.Uploader.swf',
				limitSize: 100000 * 1024 * 1024, // x*y*z where x will be the file size in mb
				onLoad: function() {
					var fallback_1 = $('fallback_1');
					var status = $('status3');
					if (status!=null) {
						$('status3').removeClass('hide');
					}
				},
				// The changed parts!
				debug: true, // enable logs, uses console.log
				target: 'browse_3', // the element for the overlay (Flash 10 only)
				fileInvalid: function(){
					alert("the file is not valid");
				}
			});
			
			var swiffy4 = new FancyUpload2($('status4'), $('list'), $('selecting'), $('uploading4'), $('upload_error'), 2, 3, {
				url: $('form_upload_material').action,
				fieldName: 'uploaded_file',
				path: '/flash/Swiff.Uploader.swf',
				limitSize: 100000 * 1024 * 1024, // x*y*z where x will be the file size in mb
				onLoad: function() {
					var fallback_1 = $('fallback_1');
					var status = $('status');
					if (status!=null) {
						$('status').removeClass('hide');
					}
				},
				// The changed parts!
				debug: true, // enable logs, uses console.log
				target: 'browse_4', // the element for the overlay (Flash 10 only)
				fileInvalid: function(){
					alert('This file is not valid');
				}					
			});
			
			var swiffy5 = new FancyUpload2($('status5'), $('list'), $('selecting'), $('uploading5'), $('upload_error'), 3, 3, {
				url: $('form_upload_material').action,
				fieldName: 'uploaded_file',
				path: '/flash/Swiff.Uploader.swf',
				limitSize: 100000 * 1024 * 1024, // x*y*z where x will be the file size in mb
				onLoad: function() {
					var fallback_1 = $('fallback_1');
					var status = $('status5');
					if (status!=null) {
						$('status5').removeClass('hide');				
					}
				},
				// The changed parts!
				debug: true, // enable logs, uses console.log
				target: 'browse_5', // the element for the overlay (Flash 10 only)
				fileInvalid: function(){
					alert("the file is not valid");
				}
			});
			
			var swiffy6 = new FancyUpload2($('status6'), $('list'), $('selecting'), $('uploading6'), $('upload_error'), 3, 4, {
				url: $('form_upload_material').action,
				fieldName: 'uploaded_file',
				path: '/flash/Swiff.Uploader.swf',
				limitSize: 100000 * 1024 * 1024, // x*y*z where x will be the file size in mb
				onLoad: function() {
					var fallback_1 = $('fallback_1');
					var status = $('status');
					if (status!=null) {
						$('statu6').removeClass('hide');
					}
				},
				// The changed parts!
				debug: true, // enable logs, uses console.log
				target: 'browse_6', // the element for the overlay (Flash 10 only)
				fileInvalid: function(){
					alert("the file is not valid");
				}
			});
			
			if( $('browse_1') )
			$('browse_1').addEvent('click', function() {
									swiffy1.browse();
									return false;
			});
			if( $('browse_2') )
			$('browse_2').addEvent('click', function() {
									swiffy2.browse();
									return false;
			});
			if( $('browse_3') )
			$('browse_3').addEvent('click', function() {
									swiffy3.browse();
									return false;
			});
			if( $('browse_4') )
			$('browse_4').addEvent('click', function() {
									swiffy4.browse();
									return false;
			});
			
			if( $('browse_5') )
			$('browse_5').addEvent('click', function() {
									swiffy5.browse();
									return false;
			});
			
			if( $('browse_6') )
			$('browse_6').addEvent('click', function() {
									swiffy6.browse();
									return false;
			});
		}
		
		function doBlogSearch(campaign_id, list_start) {
			doAjaxCallback("/ajax", "ajax_task=blog_list&campaign_id="+campaign_id+"&list_start="+list_start, getBlogs);
		}
		
		function doEffectedSearch( list_start ) {
			$('ajax_task').value = 'get_effected_blogs';
			$('list_start').value = list_start;
			$('campaignform').set('send', {
				url: 'campaigns_edit',
				method: 'post',
				"headers": {'X-Request': 'JSON'},
				"onSuccess": function(req) {
				var r = JSON.decode(req);
				if(r.status > 0) {
					getEffectedBlogs(r);
				} else {
					switch(r.status) {
						case -2:
							alert("Internal error");
						break;                
					}                                                                                                                                                                                                                                   
				}
			}
		});
			$('campaignform').send();
		}
		
		// report
		function getBlogs( result ) {
		$("popin_blog_list").empty();
		result.result.each(function(item, index){
			new Element("li", {
				"html": "<div>"+item.name+"</div><a href='"+item.link+"'>"+item.link+"</a>",
				"style": "padding:0px;"
			}).inject($("popin_blog_list"))
		});
		new Element("div", {
			"html": result.pagenav
		}).inject($("popin_blog_list"));
		}
		
		// new campaign
		function getEffectedBlogs( result ) {	
		$("popin_blog_list").empty();
		result.result.each(function(item, index){
			new Element("li", {
				"html": "<div>"+item.name+"</div><a href='"+item.link+"'>"+item.link+"</a>",
				"style": "padding:0px;"
			}).inject($("popin_blog_list"))
		});
		new Element("div", {
			"html": result.pagenav
		}).inject($("popin_blog_list"));
		}
		
		// report
		function campaignBlogList(CampaignId) {
			doBlogSearch(CampaignId, 0);
			var blogs = "<ul id='popin_blog_list'><li>Laddar</li></ul>";
			var buttons = '';	
			var headline = 'Alla bloggar där kampanjen synts <a class="buttonsmallgrey" onclick="$(\'popin_round_small\').dispose();$(\'popin_over_small\').dispose();" href="javascript:;"><span>Stäng</span></a>';
			var textmass = blogs;
			doPopinRoundSmall(headline, textmass, buttons, false);
		}
		
		function campaignEffectedList() {
			doEffectedSearch( 0 );
			var blogs = "<input type='hidden' value='1' class='start' name='start'><ul id='popin_blog_list'><li>Laddar inkluderade bloggar</li></ul>";
			var buttons = '';	 
			var headline = 'Lista alla inkluderade bloggar <a class="buttonsmallgrey" onclick="$(\'popin_round_small\').dispose();$(\'popin_over_small\').dispose();" href="javascript:;"><span>Stäng</span></a>';
			var textmass = blogs;
			doPopinRoundSmall(headline, textmass, buttons, false);
		}
		
		function browseIncludedBlogsPage( list_start ) {
		doEffectedSearch( list_start );
		}
		
		function openupload(id) {
		 a = $('box1');
		 b = $('box2');
		 d = $('box4');
		 e = $('box5');
		 f = $('box6');
		 g = $('box3');
		 h = $('selecting1');
		
		if (id) {
			if (id=="box1") {
				a.setStyle("display", "block");
				h.setStyle("display", "block");
				b.setStyle("display", "none");
				g.setStyle("display", "none");
				d.setStyle("display", "none");
				e.setStyle("display", "none");
				f.setStyle("display", "none");
			}
			
			if (id=="box2") {
				a.setStyle("display", "none");
				b.setStyle("display", "block");
				g.setStyle("display", "none");
				d.setStyle("display", "none");
				e.setStyle("display", "none");
				f.setStyle("display", "none");
				
			}
			
			if (id=="box3") {
				a.setStyle("display", "none");
				b.setStyle("display", "none");
				g.setStyle("display", "block");
				d.setStyle("display", "none");
				e.setStyle("display", "none");
				f.setStyle("display", "none");
			}
			
			if (id=="box4") {
				a.setStyle("display", "none");
				b.setStyle("display", "none");
				g.setStyle("display", "none");
				d.setStyle("display", "block");
				e.setStyle("display", "none");
				f.setStyle("display", "none");
			}
			
			if (id=="box5") {
				a.setStyle("display", "none");
				b.setStyle("display", "none");
				g.setStyle("display", "none");
				d.setStyle("display", "none");
				e.setStyle("display", "block");
				f.setStyle("display", "none");	
			}
			
			if (id=="box6") {
				a.setStyle("display", "none");
				b.setStyle("display", "none");
				g.setStyle("display", "none");
				d.setStyle("display", "none");
				e.setStyle("display", "none");
				f.setStyle("display", "block");
			}
		}
		}
		
		function dosearchforselected() {
			var result = $$('.bloglink');
			var blogs = $$('.superuserlink');
		
			if (blogs!=null) {
					blogs.each(function(item, index){
							var id = $(item).get("value");
							var status = $('blogliststatus:' + id).get("value");
							var namn = $('bloglistnamn:' + id).get("value");
							addBlog(namn, id, status);
					});
			}
			
			if (result!=null) {		
					result.each(function(item, index){
							var id = $(item).get("value");
							var status = $('blogliststatus:' + id).get("value");
							var namn = $('bloglistnamn:' + id).get("value");
							addBlog(namn,id,status);
					});
			}
		}
		
		function SendBackBlog(name, id, status) {
			new Element("li", {
					"html": "<div class='bloglinkdiv' id='bloglinkdiv:"+id+"'><a href='javascript:;' id='bloglink:" + id +"' onclick='addBlog(\""+name+"\", \""+id+"\",\""+"include"+"\");'><img align='absmiddle' src='/img/plus.png' style='margin-right:2px;' /></a><a href='javascript:;' id='bloglink:" + id +"' onclick='addBlog(\""+name+"\", \""+id+"\",\""+"exclude"+"\");'><img align='absmiddle' src='/img/minus.png'></a> "+name+"</div>",
					"id": "bloglista:"+id,
					"class": "odd"
				}).inject($("lista"))			
		}
		
		function RemoveSelectedBlog(name, id, status) {
					var clicked = $('bloglistselected:' + id);
					
					if (clicked!=null) {
							SendBackBlog(name, id, status);
							clicked.dispose();
							$('bloglistinput:' + id).dispose();
							$('bloglistinputname:' + id).dispose();
							$('bloglistinputstate:' + id).dispose();
					}
		}
		
		function getCategorys() {
					doAjaxCallback("/ajax", "ajax_task=popin_categorys", getCategorysList);			
		}
		
		function getCategorysList( result ) {		
			result.result.each(function(item, index){
					new Element("option", {
							"html": item.name,
							"value": item.id
					}).injectInside($('category_input'))
			});	
		}
		
		function IncludeUsers() {
			var headline = '<h1>Välj bloggar</h1>';
			dosearch();
			var textmass = '<div class="search">\n\
                                <div id="lmk_error"></div>\n\
                                <input type="text" id="searchfield" name="field" onkeyup="dosearch();" />\n\
                                <select onchange="dosearch();" id="category_input" class="category_input" name="category">\n\
                                    <option value="all">Välj kategorier</option>\n\
                                </select>\n\
                            </div>\n\
                            <div class="result" id="resultat">\n\
                                <ul id="lista" class="lista">\n\
                                    <li>Laddar...</li>\n\
                                </ul>\n\
                            </div>\n\
                            <div class="result_two" id="resultat_two">\n\
                                <ul id="lista_two" class="lista_two"></ul>\n\
                            </div>\n\
                            <a style="float:right;margin-top:10px" id="campaignsubmit" class="printbutton printbuttonright" style="float: left; margin-left: 0px;margin-top:10px;" href="javascript:;" onclick="closepopin_blogs('+"''"+');"><span>Spara</span></a>';
			var buttons = '';
			getCategorys();
			doPopinRound(headline, textmass, buttons, false);
		}
		
		/* BLOGGAR */
		function addCategory(name, id, status) {
					var toremove = $('categoryline:' + id);
					if (toremove!=null) {
									toremove.dispose();
									
									if (status=="exclude") {
													statusico = '/img/exkludera.png';
									} else {
													statusico = '/img/inkludera.png';
									}
									
									new Element("li", {
													"id": "categorylistselected:"+id,
													"html": "<div class='removeselected'><a href='javascript:;' onclick='RemoveSelectedCategory(\""+name+"\", \""+id+"\",\""+"exclude"+"\");'><img src='/img/remove.png'></a></div><div class='selectedname'>" + name + "</div><div class='selectedicon'><img src='" + statusico + "'></div>",
													"class": "categorylistselected"
									}).inject($('lista_two'))
									
									new Element("input", {
													"id": "categorylistinput:"+id,
													"value": id,
													"name": "categorylistinput",
													"style": "display: none",
													"class": "categorylistinput"
									}).inject($('lista_two'))
									
									new Element("input", {
													"id": "categorylistinputstate:"+id,
													"value": status,
													"style": "display:none",
													"name": "categorylistinputstate",
													"class": "categorylistinputstate"
									}).inject($('lista_two'))
									
									new Element("input", {
													"id": "categorylistinputname:"+id,
													"value": name,
													"style": "display: none",
													"name": "categorylistinputname",
													"class": "categorylistinputname"
									}).inject($('lista_two'))
					}		
					
		}
		
		function closepopin_category() {
			var popin = $("popin_round");
			var result = $$(".categorylistinput");
			var overlay = $("popin_over");
			var categorylink = $$(".categorylink");
			var categorylistan = $$(".categorylistan");
		    var categorylistan_exclude = $$('.categorylistan_exclude');
			var categorylistan_include = $$('.categorylistan_include');
			var all_categorys = $('all_categorys');
			
			if (all_categorys!=null) {
					if (result.length>0) {
									$('all_categorys').set('html', '');			
					} else {
								$('all_categorys').set('html', 'Alla kategorier');		
					}
			}
			
			if (categorylistan_exclude!=null) {
					categorylistan_exclude.dispose();
			}
			
			if (categorylistan_include!=null) {
					categorylistan_include.dispose();
			}
			
			if (categorylink!=null) {
					categorylink.dispose();		
			}
			
			if (categorylistan!=null) {
					categorylistan.dispose();		
			}
			
				var removecategorylistan = $$('.categorylistan_include');
				var removecategorylistanstatus = $$('.categoryliststatus');
				var removecategorylistnamn = $$('.categorylistnamn');
				
				if (removecategorylistan!=null) {
					$$('.categorylistan_include').dispose();
				}
				 
				if (removecategorylistanstatus!=null) {
					$$('.categoryliststatus').dispose()	
				}
				
				if (removecategorylistnamn!=null) {
					$$('.categorylistnamn').dispose()
				}
		
			result.each(function(item, index){
				var id = $(item).get("value");
				var status = $('categorylistinputstate:' + id).get("value");
				var namn = $('categorylistinputname:' + id).get("value");
					
				new Element("a", {
					"html": namn + ", ",
					"class": "categorylistan_" + status,
					"id": "categoryid:"+id,
					"style": "color:#666666"
				}).injectBefore($('all_categorys'))
			
		
					new Element("input", {
						"name": status + "_category[]",
						"value": id,
						"id": "categorylist:"+id,
						"class": "categorylink",
						"style": "display: none"
				}).injectBefore($('excluded_categorys'))
					
							new Element("input", {
						"value": status,
						"id": "categoryliststatus:"+id,
						"class": "categoryliststatus",
						"style": "display: none"
				}).injectBefore($('excluded_categorys'))
							
							
					new Element("input", {
						"value": namn,
						"id": "categorylistnamn:"+id,
						"class": "categorylistnamn",
						"style": "display: none"
				}).injectBefore($('excluded_categorys'))	
										
						
						
						
			});
			
			popin.dispose();
			overlay.dispose();
			
			ajax_target_do_est(this);
					
		}
		
		function SearchForSelectedCategorys() {
            var result = $$('.categorylink');
            if (result!=null) {
                result.each(function(item, index){
                    var id = $(item).get("value");
                    var status = $('categoryliststatus:' + id).get("value");
                    var namn = $('categorylistnamn:' + id).get("value");
                    addCategory(namn,id,status);
                });

            }
		}
		
		function SendBackCateogry(name, id, status) {
					new Element("li", {
					"html": "<div class='categorylinkdiv' id='categorylinkdiv:"+id+"'><a href='javascript:;' id='categorylink:" + id +"' onclick='addCategory(\""+name+"\", \""+id+"\",\""+"include"+"\");'><img align='absmiddle' src='/img/plus.png' style='margin-right:2px;' /></a><a href='javascript:;' id='categorylink:" + id +"' onclick='addCategory(\""+name+"\", \""+id+"\",\""+"exclude"+"\");'><img align='absmiddle' src='/img/minus.png'></a> "+name+"</div>",
					"class": "",
					"id": "categoryline:" + id
				}).inject($("lista"))		
		}
		
		function RemoveSelectedCategory(name, id, status) {
				var clicked = $('categorylistselected:' + id);
					
					if (clicked!=null) {
									SendBackCateogry(name, id, status);
									clicked.dispose();
									$('categorylistinput:' + id).dispose();
									$('categorylistinputname:' + id).dispose();
									$('categorylistinputstate:' + id).dispose();
					}		
		}
		
		function getCategorysPost( result ) {
				
			$('lista').empty();
			result.result.each(function(item, index){
				new Element("li", {
					"html": "<div class='categorylinkdiv' id='categorylinkdiv:"+item.id+"'>\n\
                                <a href='javascript:;' id='categorylink:" + item.id +"' onclick='addCategory(\""+item.name+"\", \""+item.id+"\",\""+"include"+"\");'>\n\
                                    <img align='absmiddle' src='/img/plus.png' style='margin-right:2px;' />\n\
                                </a>\n\
                                <a href='javascript:;' id='categorylink:" + item.id +"' onclick='addCategory(\""+item.name+"\", \""+item.id+"\",\""+"exclude"+"\");'>\n\
                                    <img align='absmiddle' src='/img/minus.png'>\n\
                                </a> "+item.name+"\
                            </div>",
					"class": item.classen,
					"id": "categoryline:" + item.id
				}).inject($("lista"))		
			});
			
			SearchForSelectedCategorys();
		}
		
		function docategorysearch() {
				doAjaxCallback("/ajax", "ajax_task=popin_categorys", getCategorysPost);
		}
		
		function Categorys() {
			var headline = '<h1 style="margin-bottom:0px;padding:0px">Välj kategorier</h1>';
			docategorysearch();
			var textmass = '<div class="search"><div class="result" id="resultat"><ul id="lista" class="lista"><li>Loading...</li></ul></div><div class="result_two" id="resultat_two"><ul id="lista_two" class="lista_two"></ul></div><a style="float:right;margin-top:10px" id="campaignsubmit" class="printbutton printbuttonright" style="float: left; margin-left: 0px;margin-top:10px;" href="javascript:;" onclick="closepopin_category();"><span>Spara</span></a>';
			var buttons = '';
			doPopinRound(headline, textmass, buttons, false);
		}	
		/* KATEGORIER */
		
		/* STÄDER */
		function stader() {
			var headline = '<h1>Välj städer</h1>';
			dostadersearch();
			var textmass = '<div class="search"><div id="lmk_error"></div><input type="text" id="searchfield" name="field" onkeyup="dostadersearch();" /></div><div class="result" id="resultat"><ul id="lista" class="lista"><li>Laddar...</li></ul></div><div class="result_two" id="resultat_two"><ul id="lista_two" class="lista_two"></ul></div><a style="float:right;margin-top:10px" id="campaignsubmit" class="printbutton printbuttonright" style="float: left; margin-left: 0px;margin-top:10px;" href="javascript:;" onclick="closepopin_stader();"><span>Spara</span></a>';
			var buttons = '';
			doPopinRound(headline, textmass, buttons, false);
		}
		
		function SearchForSelectedStader() {
			var result = $$('.staderlink');
			if (result!=null) {
				result.each(function(item, index){
					var id = $(item).get("value");
					var status = $('staderliststatus:' + id).get("value");
					var namn = $('staderlistnamn:' + id).get("value");
					addstad(namn,id,status);
				});
			}			
		}
		
		function SendBackStad(name, id, status) {
					new Element("li", {
					"html": "<div class='staderlinkdiv' id='staderlinkdiv:"+id+"'><a href='javascript:;' id='staderlink:" + id +"' onclick='addstad(\""+name+"\", \""+id+"\",\""+"include"+"\");'><img align='absmiddle' src='/img/plus.png' style='margin-right:2px;' /></a><a href='javascript:;' id='categorylink:" + id +"' onclick='addstad(\""+name+"\", \""+id+"\",\""+"exclude"+"\");'><img align='absmiddle' src='/img/minus.png'></a> "+name+"</div>",
					"class": "",
					"id": "stadline:" + id
				}).inject($("lista"))		
		}
		
		function RemoveSelectedstad(name, id, status) {
				var clicked = $('stadlistselected:' + id);
					
					if (clicked!=null) {
									SendBackStad(name, id, status);
									clicked.dispose();
									$('staderlistinput:' + id).dispose();
									$('staderlistinputname:' + id).dispose();
									$('staderlistinputstate:' + id).dispose();
					}		
		}
		
		function closepopin_stader() {
			var popin = $("popin_round");
			var result = $$(".staderlistinput");
			var overlay = $("popin_over");
			var staderlink = $$(".staderlink");
			var staderlistan = $$(".staderlistan");
			var staderlistan_exclude = $$('.staderlistan_exclude');
			var staderlistan_include = $$('.staderlistan_include');
			var all_stader = $('all_stader');
			
			if (all_stader!=null) {
					if (result.length>0) {
									$('all_stader').set('html', '');			
					} else {
								$('all_stader').set('html', '&nbsp;alla städer');		
					}
			}
			
			if (staderlistan_exclude!=null) {
					staderlistan_exclude.dispose();
			}
			
			if (staderlistan_include!=null) {
					staderlistan_include.dispose();
			}
			
			if (staderlink!=null) {
					staderlink.dispose();		
			}
			
			if (staderlistan!=null) {
					staderlistan.dispose();		
			}
			
				var removestaderlistan = $$('.staderlistan_include');
				var removestaderlistanstatus = $$('.staderliststatus');
				var removestaderlistnamn = $$('.staderlistnamn');
				
				if (removestaderlistan!=null) {
					$$('.staderlistan_include').dispose();
				}
				 
				if (removestaderlistanstatus!=null) {
					$$('.staderliststatus').dispose()	
				}
				
				if (removestaderlistnamn!=null) {
					$$('.staderlistnamn').dispose()
				}
				
			result.each(function(item, index){
				var id = $(item).get("value");
				var status = $('staderlistinputstate:' + id).get("value");
				var namn = $('staderlistinputname:' + id).get("value");
				
				
				new Element("a", {
					"html": namn + ", ",
					"class": "staderlistan_" + status,
					"id": "staderid:"+id,
					"style": "color:#666666"
				}).injectBefore($('all_stader'))
		
				new Element("input", {
						"name": status + "_stader[]",
						"value": id,
						"id": "staderlist:"+id,
						"class": "staderlink",
						"style": "display: none"
				}).injectBefore($('excluded_stader'))
					
							new Element("input", {
						"value": status,
						"id": "staderliststatus:"+id,
						"class": "staderliststatus",
						"style": "display: none"
				}).injectBefore($('excluded_stader'))
										
				new Element("input", {
						"value": namn,
						"id": "staderlistnamn:"+id,
						"class": "staderlistnamn",
						"style": "display: none"
				}).injectBefore($('excluded_stader'))	
									
									
			});
			
			popin.dispose();
			overlay.dispose();
			
		}
		
		function addstad( name, id, status) {
			var toremove = $('stadline:' + id);
					if (toremove!=null) {
									toremove.dispose();
								
									if (status=="exclude") {
													statusico = '/img/exkludera.png';
									} else {
													statusico = '/img/inkludera.png';
									}
									
									new Element("li", {
													"id": "stadlistselected:"+id,
													"html": "<div class='removeselected'><a href='javascript:;' onclick='RemoveSelectedstad(\""+name+"\", \""+id+"\",\""+"exclude"+"\");'><img src='/img/remove.png'></a></div><div class='selectedname'>" + name + "</div><div class='selectedicon'><img src='" + statusico + "'></div>",
													"class": "stadlistselected"
									}).inject($('lista_two'))
									
									new Element("input", {
													"id": "staderlistinput:"+id,
													"value": id,
													"name": "staderlistinput",
													"style": "display: none",
													"class": "staderlistinput"
									}).inject($('lista_two'))
									
									new Element("input", {
													"id": "staderlistinputstate:"+id,
													"value": status,
													"style": "display:none",
													"name": "staderlistinputstate",
													"class": "staderlistinputstate"
									}).inject($('lista_two'))
									
									new Element("input", {
													"id": "staderlistinputname:"+id,
													"value": name,
													"style": "display: none",
													"name": "staderlistinputname",
													"class": "staderlistinputname"
									}).inject($('lista_two'))
					}
		}
		
		function getStaderPost( result ) {
				$('lista').empty();	
				result.result.each(function(item, index){
					new Element("li", {
						"html": "<div class='stadlinkdiv' id='stadlinkdiv:"+item.id+"'><a href='javascript:;' id='stadlink:" + item.id +"' onclick='addstad(\""+item.name+"\", \""+item.id+"\",\""+"include"+"\");'><img align='absmiddle' src='/img/plus.png' style='margin-right:2px;' /></a><a href='javascript:;' id='stadlink:" + item.id +"' onclick='addstad(\""+item.name+"\", \""+item.id+"\",\""+"exclude"+"\");'><img align='absmiddle' src='/img/minus.png'></a> "+item.name+"</div>",
						"id": "stadline:"+item.id,
						"class": item.classen
					}).inject($("lista"))					
			  });
				
				SearchForSelectedStader();
		}
		
		function dostadersearch() {
			var searchkey   = $('searchfield');
            var country     = $('campaign_audience_location').get("value");
			if (searchkey!=null) {
				doAjaxCallback("/ajax", "ajax_task=popin_stader&country="+country+"searchkey=" + searchkey.get('value'), getStaderPost);
			} else {
				doAjaxCallback("/ajax", "ajax_task=popin_stader&country="+country, getStaderPost);
			}
		}
		/* STÄDER */
		
		function limitText(limitField, limitNum) {
						if (limitField.value.length > limitNum) {
							limitField.value = limitField.value.substring(0, limitNum);
						}
		}
		
		function collapse(box) {
			if ($("userlisting_" + box).hasClass('showed')) {
				$("userlisting_" + box).setStyle("display", "none");
				$$(".collapse_button_" + box).set("html","+");
				$$(".collapse_button_" + box).set("style","");
				$("userlisting_" + box).removeClass('showed');
			}
			else {
				
				$("userlisting_" + box).set("style", "display: table; margin-top: 10px;");
				$$(".collapse_button_" + box).set("html","-");
				$$(".collapse_button_" + box).set("style","font-size:35px;margin-top:4px;");
				$("userlisting_" + box).addClass('showed');
				
			}
		}
		
		function video_post_hover(todo, box) {
	
				if (todo=='enter') {
						onclick = $(box).get('onclick');
					
						new Element("div", {
								"class": "video_post_hover",
								"html": "&nbps;",
								"onclick": onclick
								}).injectInside($(box));
					
				} else {
					
						var video_post_hover = $$('.video_post_hover');
						if (video_post_hover!=null) {
								video_post_hover.dispose();
						}
						
				}
		}
		
		function hoverThumbs(){
		
						
    $$('div.video_post').addEvent('mouseenter', function(){        
        video_post_hover('enter', this);
    });
    $$('div.video_post').addEvent('mouseleave', function(){
        video_post_hover('leave', this);
    });
    
						
		}
		
		function dofacebookLogout() {
				window.location.href='/logout';
		}
		
		
		/* redirect after login */
		function facebookLogin() {
				window.location.href = '/?loginstart=1&do=1';
		}
		/* function used by Facebook */