var popupStatus = 0;
var view_conversion_5_days_var = 0;
var view_conversion_var = 0;
var media_logins_var = 0;
var chatIsActive = 0;

//loading popup with jQuery magic!  
function loadPopup(){  
    //loads popup only if it is disabled  
    if(popupStatus==0){ 
        $("#showMailBackground").css("opacity", "0.5");  
        $("#showMailBackground").css("display", "block");
	$("#showMailBox").css("display", "block");  
        $("#showMail").css("display", "block");  
        popupStatus = 1;
	CKEDITOR.replace( 'ckeditor_mail', {
				toolbar : "Basic",
				height	: "200",
				width	: "600"
	});
    }
}

function setPriceExpireStamp(id) {
	var price_expire_stamp_id = "price_expire_stamp_"+id;
	var price_expire_selected_id = "price_expire_selected_"+id;
	var price_expire_selected_val = $("select[name='"+price_expire_selected_id+"']").val();
	var expirestamp = 0;

	$("input[name='"+price_expire_stamp_id+"']").val("");

	$.get('/cgi-bin/ajax_functions.pl', {action: "setPriceExpireStamp", id: id, price_expire_selected_val: price_expire_selected_val}, function(data){
		if(data){
			$("input[name='"+price_expire_stamp_id+"']").val(data);
		}
	});
}

function decRevAction(agent_id, checked) {
	$.get('/cgi-bin/ajax_functions.pl', {action: "decRevAction", agent_id: agent_id, checked: checked}, function(data){
		if (data) {
			alert(data);
		}
	});
}

function create_campaign_get_clients(country_id) {
	$.get('/cgi-bin/ajax_functions.pl', {action: "getClientsFromCountry", country_id: country_id}, function(data){
		if (data) {
			$("#selectclients").html(data);
		}
	});
}

function setPriceExpireSelected(id) {
	var price_expire_selected_id = "price_expire_selected_"+id;
	$("select[name='"+price_expire_selected_id+"']").val("4");
}

function toggle_potential_view(){
    if($("#potential_long").css("display") == "none"){
	$("#potential_short").css("display", "none");
	$("#potential_long").css("display", "block");
    } else {
	$("#potential_long").css("display", "none");
	$("#potential_short").css("display", "block");
    }
}

function send_feedback(){
    var pathname = $(location).attr('href');
    var user = getURLParameter('login');
    var text = $("#feedback_text").val();
    if(text != ""){
	$.get('/cgi-bin/ajax_functions.pl', {action: "send_feedback", pathname : pathname, user:user, text:text});
	$("#submit_text").css("display", "block");
	$(".panel").delay(1500).toggle("fast", function(){
	    $("#feedback_text").val("");
	    $("#submit_text").css("display", "none");
	});
    } else {
	alert("Please add your feedback");
    }
}

function register_partner(login){
    if(login == ""){
	alert("You need to login before you can register accounts.");
    } else {
	var username = $("#partner_username").val();
	var password = $("#partner_password").val();
	if(username != "" && password != ""){
	    $.get('/cgi-bin/ajax_functions.pl', {action: "register_partner", login:login, username:username, password:password}, function(data){
		if(data == "ok"){
		    getRegisteredPartners(login);
		    $("#submit_text1").css("display", "block");
		    $(".panel1").delay(1500).toggle("fast", function(){
			$("#partner_password").val("");
			$("#partner_username").val("");
			$("#submit_text1").css("display", "none");
		    });
		} else {
		    alert(data);
		}
	    });
	} else {
	    alert("Please write username and password on the account you want to register.");
	}
    }
}

function login_registered_agent(id, login, logged){
    $.post('/cgi-bin/ajax_functions.pl', {action: "get_credentials", id:id, login:login, logged:logged}, function(data){
	if(data){
	    var dataArr = data.split("|");
	    var form= document.createElement('form');
	    form.method= 'post';
	    form.action= 'members.pl';
	    var input= document.createElement('input');
	    input.type= 'hidden';
	    input.name= 'login';
	    input.value= dataArr[0];
	    form.appendChild(input);
	    var input1= document.createElement('input');
	    input1.type= 'hidden';
	    input1.name= 'pwd';
	    input1.value= dataArr[1];
	    form.appendChild(input1);
	    var input2= document.createElement('input');
	    input2.type= 'hidden';
	    input2.name= 'no_reg_login';
	    input2.value= 1;
	    form.appendChild(input2);
	    document.body.appendChild(form);
	    form.submit();
	    return false;
	} else {
	    return false;
	}
    });
}

function getRegisteredPartners(login){
    if(login != ""){
	$.get('/cgi-bin/ajax_functions.pl', {action: "get_registered_partners", login:login}, function(data){
	    if(data){
		$(".registered_accounts").html(data);
	    }
	});
    }
}

function remove_feedback(id){
    $("#feedback_row_"+id).fadeOut();
    $.get('/cgi-bin/ajax_functions.pl', {action: "remove_feedback", id : id});
}

function select_all_checkbox(){
    if($("#select_all_checkbox").attr("checked")){
	$("#sortTable_plain input[name='agent_check']").each(function(){
	    $(this).attr('checked', true);
	});
    } else {
	$("#sortTable_plain input[name='agent_check']").each(function(){
	    $(this).attr('checked', false);
	});
    }
}

function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

function adjustpopup(obj){
    var windowWidth = "";
    var windowHeight = "";
    if (window.innerWidth){
     	windowWidth=window.innerWidth;
    }else if (document.documentElement && document.documentElement.clientWidth){
	windowWidth=document.documentElement.clientWidth;
    } else if (document.body){
	windowWidth=document.body.clientWidth;
    }
		
    if (window.innerHeight){
 	windowHeight=window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight){
	windowHeight=document.documentElement.clientHeight;
    } else if (document.body){
	windowHeight=document.body.clientHeight;
    } 
    var popupHeight = 400;  
    var popupWidth = 1200;
    var offset = $(obj).offset();
    //centering
    $("#view_container").css({  
        "position": "absolute",  
        "top": offset.top-200,  
        "left": (windowWidth/2)-(popupWidth/2)
    });  
    //only need force for IE6     
    $("#view_container_back").css({  
        "height": windowHeight,
	"width": windowWidth
    });
}

function view_bad_agents(obj, camp_id, currency){
    $(".view_container_content").html("");
    $(".loading_text").css("display", "block");
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_bad_agents", camp_id : camp_id, currency:currency}, function(data){
			if(data){
			    $(".loading_text").css("display", "none");
			    $(".view_container_content").html(data);}
    });
    adjustpopup(obj);
    
    $("#view_container_back").css("display", "block");
    $("#view_container").css("display", "block");
}

function view_bad_agents_short(obj, camp_id, currency){
    $(".view_container_content").html("");
    $(".loading_text").css("display", "block");
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_bad_agents_short", camp_id : camp_id, currency:currency}, function(data){
			if(data){
			    $(".loading_text").css("display", "none");
			    $(".view_container_content").html(data);}
    });
    adjustpopup(obj);
    
    $("#view_container_back").css("display", "block");
    $("#view_container").css("display", "block");
}

function view_campaign_chart(obj, camp_id){
    $(".view_container_content").html("");
    $(".loading_text").css("display", "block");
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_campaign_chart", camp_id : camp_id}, function(data){
			if(data){
			    $(".loading_text").css("display", "none");
			    $(".view_container_content").html(data);
			}
    });
    adjustpopup(obj);
    
    $("#view_container_back").css("display", "block");
    $("#view_container").css("display", "block");
}

function close_view_container(){
    $("#view_container_back").css("display", "none");
    $("#view_container").css("display", "none");
}

function del_highlighted_campaign(id){
    $("#row_"+id).slideUp("fast");
    $.get('/cgi-bin/ajax_functions.pl', {action: "delete_highlighted_campaign", camp_id : id});
}

function shoh(id){
    if($("#"+id).css("display") == "none"){
	$("#"+id).css("display", "block");
    } else {
	$("#"+id).css("display", "none");
    }
}

function disableEnterKey(e)
{
     var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}

function activateChat(nickname, manager){
    if($("#chat_container").css("display") == "none"){
	if(chatIsActive == 0){
	    parent.chat_iframe.location.href="/chat/set_session.php?nick="+nickname+"&privmsg="+manager;
	}
	chatIsActive = 1;
	$("#chat_container").slideDown();
    } else {
	$("#chat_container").slideUp();
	//parent.chat_iframe.location.href="";
    }
}

function removeChat(){
    $(document).ready(function() { 
    $("#chat_div").css("display", "none");
    });
}

function checkOnlineUser(user){
    $.get('/chat/checkOnlineUser.php', {user: user}, function(data){
	if(data == 1){
	    // Enable chat
	    $("#img_chat").attr("src", "/con_images/chat_online.gif");
	    $(".chat_status").html("online");
	} else {
	    // Disable Chat
	   $("#chat_div").removeAttr("onclick");
	   $("#img_chat").attr("src", "/con_images/chat_offline.gif");
	}
    });
}

function toggle_highlighted_campaign(id){
    if($("input[name='hc_active_"+id+"']").attr('checked')){
	$.get('/cgi-bin/ajax_functions.pl', {action: "activate_highlighted_campaign", camp_id : id});
    } else {
	$.get('/cgi-bin/ajax_functions.pl', {action: "deactivate_highlighted_campaign", camp_id : id});
    }
}

function toggle_partnermessage_campaign(id){
    if($("input[name='hc_active_"+id+"']").attr('checked')){
	$.get('/cgi-bin/ajax_functions.pl', {action: "activate_partnermessage_campaign", id : id});
    } else {
	$.get('/cgi-bin/ajax_functions.pl', {action: "deactivate_partnermessage_campaign", id : id});
    }
}

function removeDeeplink(id){
    $.get('/cgi-bin/ajax_functions.pl', {action: "remove_deeplink", banner_id : id});
    $("."+id).css("display", "none");
}

function show_edit_tags(id, tags){
    var windowWidth = "";
		var windowHeight = "";
		if (window.innerWidth){
		 	windowWidth=window.innerWidth;
		}
		else if (document.documentElement && document.documentElement.clientWidth){
			windowWidth=document.documentElement.clientWidth;
		} 
		else if (document.body){
			windowWidth=document.body.clientWidth;
		}
		
		if (window.innerHeight){
		 	windowHeight=window.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight){
			windowHeight=document.documentElement.clientHeight;
		} 
		else if (document.body){
			windowHeight=document.body.clientHeight;
		} 
    var popupHeight = 400;  
    var popupWidth = 500;  
    //centering
    $("#show_edit_tags").css({   
        "top": 200,//windowHeight/2-popupHeight/2,  
        "left": (windowWidth/2)-(popupWidth/2)
    });
    $("#categori_id").val(id);
    $("#tag_text_to_add").text(tags);
    $("#show_edit_tags_back").fadeIn();
    $("#show_edit_tags").fadeIn();
}

function delete_categori(categori_id){
     $.get('/cgi-bin/ajax_functions.pl', {action: "delete_categori", categori_id : categori_id});
    $("#"+categori_id).css("display", "none");
}

function disable_edit_tags(){
    $("#show_edit_tags").fadeOut();
    $("#show_edit_tags_back").fadeOut();
}

function update_edit_tags() {
    var tags = $("#tag_text_to_add").val();
    var categori_id = $("#categori_id").val();
    $.get('/cgi-bin/ajax_functions.pl', {action: "update_edit_tags", tags : tags, categori_id : categori_id}, function(data){
	window.location.href = "staff_edit_tags.pl?page=options";
    });
}

function remove_staff_rights_tree() {
    $("#staff_rights_tree").fadeOut();
}

function toggleMail(id) {
    var isDisplayed = 0;
    if($("#showmail_"+id).css("display") == "block"){
	isDisplayed = 1;
    }
    $(".mailContainer").each(function(){
	$(this).css("display", "none");
    });
    if(isDisplayed == 0){
	$("#showmail_"+id).css("display", "block");
    }
} 

function showSubmenu() {
    $(".table_left").css("display", "block");
}

//disabling popup with jQuery magic!  
function disablePopup(){  
    //disables popup only if it is enabled  
    if(popupStatus==1){  
        $("#showMailBackground").css("display", "none");
	$("#showMailBox").css("display", "none"); 
        $("#showMail").css("display", "none");  
        popupStatus = 0;  
    }  
}

function centerPopup(){  

    //request data for centering  
    var windowWidth = "";
		var windowHeight = "";
		if (window.innerWidth){
		 	windowWidth=window.innerWidth;
		}
		else if (document.documentElement && document.documentElement.clientWidth){
			windowWidth=document.documentElement.clientWidth;
		} 
		else if (document.body){
			windowWidth=document.body.clientWidth;
		}
		
		if (window.innerHeight){
		 	windowHeight=window.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight){
			windowHeight=document.documentElement.clientHeight;
		} 
		else if (document.body){
			windowHeight=document.body.clientHeight;
		} 
    var popupHeight = 400;  
    var popupWidth = 670;  
    //centering
    $("#showMailBox").css({  
        "position": "absolute",  
        "top": 50,//windowHeight/2-popupHeight/2,  
        "left": (windowWidth/2)-(popupWidth/2)
    });  
    //only need force for IE6     
    $("#showMailBackground").css({  
        "height": windowHeight,
	"width": windowWidth
    });  
}

function changeurl(country_id){
    if (document.editform.country_id.value == '54') document.editform.closed_url.value="closed.adservicemedia.com/da/";
    if (document.editform.country_id.value == '154') document.editform.closed_url.value="closed.adservicemedia.com/no/";
    if (document.editform.country_id.value == '194') document.editform.closed_url.value="closed.adservicemedia.com/se/";
    if (document.editform.country_id.value == '68') document.editform.closed_url.value="closed.adservicemedia.com/fi/";
    if (document.editform.country_id.value == '75') document.editform.closed_url.value="closed.adservicemedia.com/de/";
    if (document.editform.country_id.value == '213') document.editform.closed_url.value="closed.adservicemedia.com/us/";
}

function toggleBanner(camp_id, id, action) {
	if(action == 'stop'){
		$.get('/cgi-bin/ajax_functions.pl', {action: "stop_banner", camp_id : camp_id, id : id}, function(data){
			if(data){alert(data);}
		});			
	}
	if(action == 'reactivate'){
		$.get('/cgi-bin/ajax_functions.pl', {action: "reactivate_banner", camp_id : camp_id, id : id}, function(data){
			if(data){alert(data);}
		});
	}
}

function showAllCampaigns(){
    $(".notSubscribed").each(function(){
	$(this).css("display", "");
    });
}

function showMyCampaigns(){
    $(".notSubscribed").each(function(){
	$(this).css("display", "none");
    });
}

function add_agents_to_camp(args) {
	var count = 0;
	var lastCount = 0;
	$('.selected_agent:checked').each(function(){count += 1});
	$('.selected_agent:checked').each(function(){
		var camp_id = $('input[name="camp_id"]').val();
		$.get('/cgi-bin/ajax_functions.pl', {action: "add_agent_to_camp", camp_id : camp_id, agent_id : $(this).val()});		
		lastCount += 1;
		if(count == lastCount){
			alert(count + " agents has been transferred!");			
		}
	});
}

function run_wordfilter() {
	var answer = confirm("WARNING: This function takes some time and may result in delays for other users!");
	if(answer){
		window.location = "staff_wordfilter.pl";
	} 
}

function view_illigal_domains() {
	if($("#view_illigal_domains_table").css("display") == "block"){
		$("#view_illigal_domains_table").slideUp();
	} else {
		$("#view_illigal_domains_table").slideDown();
	}
}

function media_restrictions_change(){
	if($("#media_restrictions").val() == "Standard"){
		$("#media_restrictions_text").css("background-color", "#F0F0F6");
		$("#media_restrictions_text").attr("disabled", "disabled");
	} else {
		$("#media_restrictions_text").css("background-color", "#FFFFFF");
		$("#media_restrictions_text").removeAttr("disabled");
	}
}

function sem_policy_change() {
	if($("#sem_policy").val() == "Standard"){
		$("#sem_policy_text").css("background-color", "#F0F0F6");
		$("#sem_policy_text").attr("disabled", "disabled");
	} else {
		$("#sem_policy_text").css("background-color", "#FFFFFF");
		$("#sem_policy_text").removeAttr("disabled");
	}
}

function campaign_alert_change() {
	if($("#campaign_alert").is(':checked')){
	    $("#campaign_alert_text").css("background-color", "#FFFFFF");
	    $("#campaign_alert_text").removeAttr("disabled");	
	} else {
	    $("#campaign_alert_text").css("background-color", "#F0F0F6");
	    $("#campaign_alert_text").attr("disabled", "disabled");
	} 
}

function create_campaign_onSubmit() {
    if($("#pay_click_client").val() != "" && $("#pay_click_agent").val() != ""){
	if($("#max_number_clicks").val() == ""){
	    alert("You need to write a value to 'Max Number of clicks'");
	    return false;
	}
    }
}

function view_conversion_5_days() {
    if(view_conversion_5_days_var == 0){
	$("#ajax_loader").css("display", "block");
	if($("#overview_container").css("display") == "block"){
	    $("#overview_container").css("display", "none");
	}
	$.get('/cgi-bin/ajax_functions.pl', {action: "view_conversion_5_days"}, function(data){
		    if(data){
			$("#overview_container").append(data);
			$("#overview_container").slideDown();
			$("#ajax_loader").css("display", "none");
		    }
	});
	view_conversion_5_days_var = 1;
    } 
}

function view_page_log(){
    $("#ajax_loader4").css("display", "block");
    if($("#pagelog_container").css("display") == "block"){
	$("#pagelog_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_page_log"}, function(data){
        if(data){
	    $("#pagelog_container").html(data);
	    $("#pagelog_container").slideDown();
	    $("#ajax_loader4").css("display", "none");
        }
    });
}

function view_staff_page_log(){
    $("#ajax_loader7").css("display", "block");
    if($("#pagelog_container").css("display") == "block"){
	$("#pagelog_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_staff_page_log"}, function(data){
        if(data){
	    $("#pagelog_container").html(data);
	    $("#pagelog_container").slideDown();
	    $("#ajax_loader7").css("display", "none");
        }
    });
}

function view_visits_log(){
    $("#ajax_loader4").css("display", "block");
    if($("#pagelog_container").css("display") == "block"){
	$("#pagelog_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_visits_log"}, function(data){
        if(data){
	    $("#pagelog_container").html(data);
	    $("#pagelog_container").slideDown();
	    $("#ajax_loader4").css("display", "none");
        }
    });
}

function view_pool_usage_log(type){
    $("#ajax_loader10").css("display", "block");
    if($("#pagelog_container").css("display") == "block"){
	$("#pagelog_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_pool_usage_log", type:type}, function(data){
        if(data){
	    $("#pagelog_container").html(data);
	    $("#pagelog_container").slideDown();
	    $("#ajax_loader10").css("display", "none");
        }
    });
}

function view_pool_usage_log_per_country(type, country){
    $("#ajax_loader10").css("display", "block");
    if($("#pagelog_container").css("display") == "block"){
	$("#pagelog_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_pool_usage_log_per_country", type:type, country:country}, function(data){
        if(data){
	    $("#pagelog_container").html(data);
	    $("#pagelog_container").slideDown();
	    $("#ajax_loader10").css("display", "none");
        }
    });
}

function view_staff_visits_log(){
    $("#ajax_loader7").css("display", "block");
    if($("#pagelog_container").css("display") == "block"){
	$("#pagelog_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_staff_visits_log"}, function(data){
        if(data){
	    $("#pagelog_container").html(data);
	    $("#pagelog_container").slideDown();
	    $("#ajax_loader7").css("display", "none");
        }
    });
}

function validate_date(date){
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1;//January is 0!
    var yyyy = today.getFullYear();
    if(dd<10){dd='0'+dd}
    if(mm<10){mm='0'+mm}
    var today = yyyy+'-'+mm+'-'+dd;
    if(date >= today){
	$("input[name='to_date']").val("");
	alert("You can't select the current date or higher! ");
	
    } 
}

function reg_exceldata_download(date, client_id){
    $.get('/cgi-bin/ajax_functions.pl', {action: "reg_exceldata_download", date:date, client_id:client_id});
}

function getIphonelogPerAgent(page){
    $("#ajax_loader6").css("display", "block");
    if($("#iphonelog_container").css("display") == "block"){
	$("#iphonelog_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "getIphonelogPerAgent", page: page}, function(data){
        if(data){
	    $("#ajax_loader6").css("display", "none");
	    $("#iphonelog_container").html(data);
	    $("#iphonelog_container").slideDown();
        }
    });
}

function view_iphone_log(){
    $("#ajax_loader6").css("display", "block");
    if($("#pagelog_container").css("display") == "block"){
	$("#pagelog_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_iphone_log"}, function(data){
        if(data){
	    $("#pagelog_container").append(data);
	    $("#pagelog_container").slideDown();
	    $("#ajax_loader6").css("display", "none");
        }
    });
}

function view_android_log(){
    $("#ajax_loader6").css("display", "block");
    if($("#pagelog_container").css("display") == "block"){
	$("#pagelog_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_android_log"}, function(data){
        if(data){
	    $("#pagelog_container").append(data);
	    $("#pagelog_container").slideDown();
	    $("#ajax_loader6").css("display", "none");
        }
    });
}

function view_iphone_users(){
    $("#ajax_loader9").css("display", "block");
    if($("#iphoneusers_container").css("display") == "block"){
	$("#iphoneusers_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_iphone_users"}, function(data){
        if(data){
	    $("#iphoneusers_container").html(data);
	    $("#iphoneusers_container").slideDown();
	    $("#ajax_loader9").css("display", "none");
        } 
    });
}

function view_android_users(){
    $("#ajax_loader9").css("display", "block");
    if($("#iphoneusers_container").css("display") == "block"){
	$("#iphoneusers_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_android_users"}, function(data){
        if(data){
	    $("#iphoneusers_container").html(data);
	    $("#iphoneusers_container").slideDown();
	    $("#ajax_loader9").css("display", "none");
        } 
    });
}

function view_iphone_users_by_country(country){
    $("#ajax_loader9").css("display", "block");
    if($("#iphoneusers_container").css("display") == "block"){
	$("#iphoneusers_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_iphone_users", country:country}, function(data){
        if(data){
	    $("#iphoneusers_container").html(data);
	    $("#iphoneusers_container").slideDown();
	    $("#ajax_loader9").css("display", "none");
        } 
    });
}

function view_android_users_by_country(country){
    $("#ajax_loader9").css("display", "block");
    if($("#iphoneusers_container").css("display") == "block"){
	$("#iphoneusers_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_android_users", country:country}, function(data){
        if(data){
	    $("#iphoneusers_container").html(data);
	    $("#iphoneusers_container").slideDown();
	    $("#ajax_loader9").css("display", "none");
        } 
    });
}

function view_agent_feedback(){
    $("#ajax_loader5").css("display", "block");
    if($("#feedback_container").css("display") == "block"){
	$("#feedback_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_agent_feedback"}, function(data){
        if(data){
	    $("#feedback_container").html(data);
	    $("#feedback_container").slideDown();
	    $("#ajax_loader5").css("display", "none");
        }
    });
}

function view_revenue_this_month(){
    $("#ajax_loader14").css("display", "block");
    if($("#feedback_container").css("display") == "block"){
	$("#feedback_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_revenue_this_month"}, function(data){
        if(data){
	    $("#feedback_container").html(data);
	    $("#feedback_container").slideDown();
	    $("#ajax_loader14").css("display", "none");
        }
    });
}

function view_revenue_today(){
    $("#ajax_loader15").css("display", "block");
    if($("#feedback_container").css("display") == "block"){
	$("#feedback_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_revenue_today"}, function(data){
        if(data){
	    $("#feedback_container").html(data);
	    $("#feedback_container").slideDown();
	    $("#ajax_loader15").css("display", "none");
        }
    });
}

function view_expiring_campaigns(){
    $("#ajax_loader16").css("display", "block");
    if($("#feedback_container").css("display") == "block"){
	$("#feedback_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_expiring_campaigns"}, function(data){
        if(data){
	    $("#feedback_container").html(data);
	    $("#feedback_container").slideDown();
	    $("#ajax_loader16").css("display", "none");
        }
    });
}

function view_expired_campaigns(staff_id){
    $("#ajax_loader13").css("display", "block");
    if($("#feedback_container").css("display") == "block"){
	$("#feedback_container").css("display", "none");
    }
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_expired_campaigns", staff_id:staff_id}, function(data){
        if(data){
	    $("#feedback_container").html(data);
	    $("#feedback_container").slideDown();
	    $("#ajax_loader13").css("display", "none");
        }
    });
}


function view_conversion() {
    if(view_conversion_var == 0){
	$("#ajax_loader1").css("display", "block");
	if($("#overview_container").css("display") == "block"){
	    $("#overview_container").css("display", "none");
	}
	$.get('/cgi-bin/ajax_functions.pl', {action: "view_conversion"}, function(data){
	    if(data){
		$("#overview_container").append(data);
		$("#overview_container").slideDown();
		$("#ajax_loader1").css("display", "none");
	    }
	});
	view_conversion_var = 1;
	
    }
}

function view_split() {
	$("#ajax_loader6").css("display", "block");
	if($("#overview_container").css("display") == "block"){
		$("#overview_container").css("display", "none");
	}
	$.get('/cgi-bin/ajax_functions.pl', {action: "view_split"}, function(data){
		if(data){
			$("#overview_container").append(data);
			$("#overview_container").slideDown();
			$("#ajax_loader1").css("display", "none");
		}
	});
}

function media_logins() {
    if(media_logins_var == 0){
	$("#ajax_loader2").css("display", "block");
	if($("#overview_container").css("display") == "block"){
	    $("#overview_container").css("display", "none");
	}
	$.get('/cgi-bin/ajax_functions.pl', {action: "media_logins"}, function(data){
	    if(data){
		$("#overview_container").append(data);
		$("#overview_container").slideDown();
		$("#ajax_loader2").css("display", "none");
	    }
	});
	media_logins_var = 1;
    }
}

function showMail(mail_id, func_name) {
	$.get('/cgi-bin/ajax_functions.pl', {action: "showMail", mail_id : mail_id, func_name : func_name}, function(data){
	    if(data){$("#showMailText").html(data)}
	});
	centerPopup();  
	loadPopup(); 
}

function updateTemplateDetails(){
    removeEditor();
    var template_id = $("select[name='mail_template'] option:selected").val();
    $.get('/cgi-bin/ajax_functions.pl', {action: "getTemplateSubject", template_id : template_id}, function(data){
	$("input[name='mail_subject']").val(data);
    });
    $.get('/cgi-bin/ajax_functions.pl', {action: "getTemplateSender", template_id : template_id}, function(data){
	$("input[name='mail_sender']").val(data);    
    });
    $.get('/cgi-bin/ajax_functions.pl', {action: "getTemplateBody", template_id : template_id}, function(data){
	$("#mail_body").html(data);
	loadCKEditor();
    });
}

function deleteTemplate(){
    var answer = confirm("Are you sure you want to delete this template?");
    if(answer){
	return true;
    } else {
	return false;
    }
}

function toggleRecipients(div){
    $("#campaign_specific").css("display", "none");
    $("#country_specific").css("display", "none");
    $("#partner_specific").css("display", "none");
    $("#specific").css("display", "none");
    if(div != "all"){
	$("#"+div).css("display", "block");
    }
}

function validate_template(){
    for(var i in CKEDITOR.instances) {
	    instance = CKEDITOR.instances[i];
	    instance.destroy();
    }
    var body = $("#mail_body").html(); 
    body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="content-type" content="text/html; charset=UTF-8" /><title>Adservice Media</title></head><body style="background-color:#999;">'+body+'</body></html>';
    //alert(body);
    $("input[name='body']").val(body);
    var group = $("input[name='recipients_group']:checked").val();
    if(group == "all"){ 
	var answer = confirm("Are you sure you want to send this mail out to everyone?");
	if(answer){
	    return true
	}
    } else if(group == "country") {
	if($("select[name='country_select'] option:selected").val()){
	    return true;
	} else {
	    alert("Select a country");
	}
    } else if(group == "partner") {
	if($("select[name='partner_select'] option:selected").val()){
	    return true;
	} else {
	    alert("Select partner(s)");
	}
    } else if(group == "campaign") {
	if($("select[name='campaign_select'] option:selected").val()){
	    return true;
	} else {
	    alert("Select a campaign");
	}
    } else if(group == "specific") {
	if($("input[name='specific_recipient']").val()){
	    return true;
	} else {
	   alert("Write an email address"); 
	}
    }
    return false;
}

function saveTemplate(){
    if($("input[name='template_name']").val() == "template name" || $("input[name='template_name']").val() == ""){
	alert("Write a template name!");
	return false;
    } else {
	for(var i in CKEDITOR.instances) {
	    instance = CKEDITOR.instances[i];
	    instance.destroy();
	}
	var body = $("#mail_body").html();
	$("input[name='body']").val(body);
	/*var name = $("input[name='template_name']").val();
	var sender = $("input[name='mail_sender']").val();
	var subject = $("input[name='mail_subject']").val();
	var body = $("#mail_body").html();
	
	
	$.get('/cgi-bin/ajax_functions.pl', {action: "saveNewTemplate", body:body, sender:sender, subject:subject, name:name});*/
	return true;
    }
}

function removeEditor(){
    for(var i in CKEDITOR.instances) {
	instance = CKEDITOR.instances[i];
	instance.destroy();
    }
    return true;
}

function removeAndCopyEditor(){
    for(var i in CKEDITOR.instances) {
	instance = CKEDITOR.instances[i];
	instance.destroy();
    }
    var tmp = $("#mail_body").html();
    $("#tmp_body").val(tmp);
    return 1;
}

function loadCKEditor(){
    for(var i in CKEDITOR.instances) {
	instance = CKEDITOR.instances[i];
	instance.destroy();
    }
    var exist = 0;
    $("div").each(function(){
        if($(this).hasClass("ckeditor")){
	    exist = 1;
        }
    });
    if(exist == 1){
        CKEDITOR.replace( 'ckeditor', {
    			toolbar : "Basic",
    			height	: "auto",
    			width	: "auto"
        });
    } else {
        CKEDITOR.replace( 'mail_body', {
    			toolbar : "Basic",
    			height	: "auto",
    			width	: "auto"
        });
    }
}

function filter2 (phrase, _id){
	var words = phrase.value.toLowerCase().split(" ");
	var table = document.getElementById(_id);
	var ele;
	for (var r = 1; r < table.rows.length; r++){
		ele = table.rows[r].innerHTML.replace(/<[^>]+>/g,"");
		var displayStyle = 'none';
		for (var i = 0; i < words.length; i++) {
		    if (ele.toLowerCase().indexOf(words[i])>=0)
		        displayStyle = '';
		    else {
		        displayStyle = 'none'; break;
		    }
		}
		table.rows[r].style.display = displayStyle;
	}
}

function showGeneralRules() {
	window.open('/general_campaign_rules.html');//, 'Campaign rules', 'width=600,height=400,scrollbars=yes');
	return false;
}

function view_chart(){
    if($("#charts").css("display") == "none"){
	if($("#charts").html() == ""){
	    $("#ajax_loader3").css("display", "block");
	    $.get('/cgi-bin/ajax_functions.pl', {action: "getChart"}, function(data){
	        if(data){
		    $("#charts").html(data);
		    $("#ajax_loader3").css("display", "none");
		    $("#charts").css("display", "block");
		}
	    });
	} else {
	    $("#charts").css("display", "block");
	}
    } else {
	$("#charts").css("display", "none");
    }
}

function view_conversion_chart() {
    var view_day = 0;
    var view_period = 0;
    var from_period = "";
    var to_period = "";
    var compare_date1 = "";
    var compare_date2 = "";
    var country_id = 54;

    if($("input[name='group1']:checked").val() == "view_period"){ view_period = 1; }
    if($("input[name='group1']:checked").val() == "view_day"){ view_day = 1; }
    country_id = $("select[name='country']").val();

    from_period = $("input[name='period_from']").val();
    to_period = $("input[name='period_to']").val();
    compare_date1 = $("input[name='compare_date1']").val();
    compare_date2 = $("input[name='compare_date2']").val();

    $("#charts").css("display", "none");
    $("#ajax_loader3").css("display", "block");
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_conversion_chart", compare_date1: compare_date1, compare_date2: compare_date2, view_day: view_day, view_period: view_period, from_period: from_period, to_period: to_period, country_id: country_id}, function(data){
        if(data){
            $("#charts").html(data);
            $("#ajax_loader3").css("display", "none");
            $("#charts").css("display", "block");
        }
    });
}

function view_monitor_chart(serverId) {
    var view_day = 0;
    var day_select = "";
    var view_period = 0;
    var from_period = "";
    var to_period = "";

    if($("input[name='group1']:checked").val() == "view_period"){ view_period = 1; }
    if($("input[name='group1']:checked").val() == "view_day"){ view_day = 1; }

    day_select = $("select[name='day_select']").val();
    from_period = $("input[name='period_from']").val();
    to_period = $("input[name='period_to']").val();

    $("#charts").css("display", "none");
    $("#ajax_loader3").css("display", "block");
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_monitor_chart", serverId: serverId, view_day: view_day, day_select: day_select, view_period: view_period, from_period: from_period, to_period: to_period}, function(data){
        if(data){
            $("#charts").html(data);
            $("#ajax_loader3").css("display", "none");
            $("#charts").css("display", "block");
        }
    });
}
function view_invalidlinks_page_chart(){
    $("#page_chart").css("display", "none");
    $("#ajax_loader3").css("display", "block");
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_invalidlinks_page_chart"}, function(data){
        if(data){
            $("#page_chart").html(data);
            $("#ajax_loader3").css("display", "none");
            $("#page_chart").css("display", "block");
        }
    });
}

function view_invalidlinks_chart() {
    var view_day = 0;
    var day_select = "";
    var view_period = 0;
    var from_period = "";
    var to_period = "";

    if($("input[name='group1']:checked").val() == "view_period"){ view_period = 1; }
    if($("input[name='group1']:checked").val() == "view_day"){ view_day = 1; }

    day_select = $("select[name='day_select']").val();
    from_period = $("input[name='period_from']").val();
    to_period = $("input[name='period_to']").val();

    $("#charts").css("display", "none");
    $("#ajax_loader3").css("display", "block");
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_invalidlinks_chart", view_day: view_day, day_select: day_select, view_period: view_period, from_period: from_period, to_period: to_period}, function(data){
        if(data){
            $("#charts").html(data);
            $("#ajax_loader3").css("display", "none");
            $("#charts").css("display", "block");
        }
    });
}

function staff_view_camp_chart(camp_id) {
    var agent_id = 0;
    var impr_check = 0;
    var lead_check = 1;
    var click_check = 1;
    var sale_check = 1;
    var total_check = 1;
    var view_month = 0;
    var view_period = 0;
    var from_period = "";
    var to_period = "";
    var show_all = 0;
    var month_select = "";
    var year_select = "";
    var all_agents = "true";
    var small_graph = "1";
    var staff_view = "1";
    var campaign_array = new Array();
 
    if($("input[name='group1']:checked").val() == "view_month"){
        view_month = 1;
    }
    if($("input[name='group1']:checked").val() == "view_period"){
        view_period = 1;
    }
    month_select = $("select[name='month_select']").val();
    year_select = $("select[name='year_select']").val();
    from_period = $("input[name='period_from']").val();
    to_period = $("input[name='period_to']").val();
    
    campaign_array.push(camp_id); 
    $("select[name='campaigns'] :selected").each(function(){
	campaign_array.push($(this).val()); 
    });
    var campaign_ids = campaign_array.join(",");

    $("#charts").css("display", "none");
    $("#ajax_loader3").css("display", "block");
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_agent_graph", small_graph: small_graph, all_agents: all_agents, agent_id: agent_id, impr_check:impr_check, lead_check:lead_check, click_check:click_check, sale_check:sale_check, total_check:total_check, view_month:view_month, show_all:show_all, campaign_ids:campaign_ids, month_select:month_select, year_select:year_select, view_period:view_period, from_period:from_period, to_period:to_period, staff_view:staff_view}, function(data){
        if(data){
            $("#charts").html(data);
            $("#ajax_loader3").css("display", "none");
            $("#charts").css("display", "block");
        }
    });
}

function staff_view_agent_chart(agent_id) {
    var impr_check = 0;
    var lead_check = 0;
    var click_check = 0;
    var sale_check = 0;
    var staff_view = "1";
    var total_check = 1;
    var view_month = 0;
    var view_period = 0;
    var from_period = "";
    var to_period = "";
    var show_all = 1;
    var month_select = "";
    var year_select = "";
    var small_graph = "1";

    if($("input[name='group1']:checked").val() == "view_month"){
        view_month = 1;
    }
    if($("input[name='group1']:checked").val() == "view_period"){
        view_period = 1;
    }
    month_select = $("select[name='month_select']").val();
    year_select = $("select[name='year_select']").val();
    from_period = $("input[name='period_from']").val();
    to_period = $("input[name='period_to']").val();

    var campaign_ids = 'all';
    $("#charts").css("display", "none");
    $("#ajax_loader3").css("display", "block");
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_agent_graph", small_graph: small_graph, agent_id: agent_id, impr_check:impr_check, lead_check:lead_check, click_check:click_check, sale_check:sale_check, total_check:total_check, view_month:view_month, show_all:show_all, campaign_ids:campaign_ids, month_select:month_select, year_select:year_select, view_period:view_period, from_period:from_period, to_period:to_period, staff_view: staff_view}, function(data){
        if(data){
            $("#charts").html(data);
            $("#ajax_loader3").css("display", "none");
            $("#charts").css("display", "block");
        }
    });
}

function view_agent_chart_staff(agent_id, period) {
    var impr_check = 0;
    var lead_check = 0;
    var click_check = 0;
    var sale_check = 0;
    var total_check = 1;
    var view_month = 1;
    var view_period = 0;
    var from_period = "";
    var to_period = "";
    var show_all = 0;
    var month_select = "";
    var year_select = "";
    var campaign_ids = "";
    var small_graph = "1";
    if(period){
	from_period = $("input[name='period_from']").val();
	to_period = $("input[name='period_to']").val();
	view_month = 0;
	view_period = 1;
	
    }

    month_select = $("input[name='month_select']").val();
	campaign_ids = $("input[name='campaign_ids']").val();

    $("#charts").css("display", "none");
    $("#ajax_loader3").css("display", "block");
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_agent_graph", small_graph: small_graph, agent_id: agent_id, impr_check:impr_check, lead_check:lead_check, click_check:click_check, sale_check:sale_check, total_check:total_check, view_month:view_month, show_all:show_all, campaign_ids:campaign_ids, month_select:month_select, year_select:year_select, view_period:view_period, from_period:from_period, to_period:to_period}, function(data){
        if(data){
            $("#charts").html(data);
            $("#ajax_loader3").css("display", "none");
            $("#charts").css("display", "block");
        }
    });
}

function view_agent_chart(agent_id){
    var impr_check = 0;
    var lead_check = 0;
    var click_check = 0;
    var sale_check = 0;
    var total_check = 1;
    var view_month = 0;
    var view_period = 0;
    var from_period = "";
    var to_period = "";
    var show_all = 1;
    var month_select = "";
    var year_select = "";
    var campaign_array = new Array();
    if($("input[name='detailed_graph']").attr("checked")){
	//impr_check = 1;
	lead_check = 1;
	click_check = 1;
	sale_check = 1;
    }
    if($("input[name='group1']:checked").val() == "view_month"){
	view_month = 1;
    }
    if($("input[name='group1']:checked").val() == "view_period"){
        view_period = 1;
    }
    month_select = $("select[name='month_select']").val();
    year_select = $("select[name='year_select']").val();
    from_period = $("input[name='period_from']").val();
    to_period = $("input[name='period_to']").val();

    
    $("select[name='campaigns'] :selected").each(function(){
	campaign_array.push($(this).val()); 
	if($(this).text() != "All"){
	    show_all = 0;
	}
    });
    var campaign_ids = campaign_array.join(",");
    $("#charts").css("display", "none");
    $("#ajax_loader3").css("display", "block");
    $.get('/cgi-bin/ajax_functions.pl', {action: "view_agent_graph", agent_id: agent_id, impr_check:impr_check, lead_check:lead_check, click_check:click_check, sale_check:sale_check, total_check:total_check, view_month:view_month, show_all:show_all, campaign_ids:campaign_ids, month_select:month_select, year_select:year_select, view_period:view_period, from_period:from_period, to_period:to_period}, function(data){
        if(data){
	    $("#charts").html(data);
	    $("#ajax_loader3").css("display", "none");
	    $("#charts").css("display", "block");
        }
    });
}

function togglediv(id){
    if($("#"+id).css("display") == "block"){
	$("#"+id).fadeOut("normal");
    } else {
	$("#"+id).fadeIn("normal");
    }
}

function checkSelectedCampaigns(){
    var showAll = true;
    var counter = 0;
    $("select[name='campaigns'] :selected").each(function(){
	if(!showAll){
	    $("select[name='campaigns'] option:selected").removeAttr("selected");
	}
	if($(this).text() == "All"){
	    showAll = false;
	}
	counter++;
	if(counter > 5){
	    alert("No more then 5 campaigns - cause of data overflow");
	    $("select[name='campaigns'] option:selected").removeAttr("selected");
	}
    });
}

function showThirdPartyPixelRules() {
    window.open("/third_party_pixel_rules.html");//, "Third Party Pixel rules", "width=600,height=400,scrollbars=yes");
    return false;
}

function showDeeplinkHelp() {
	window.open('/deeplink_rules.html');//, 'Deeplink rules', 'width=600,height=400,scrollbars=yes');
	return false;
}

function toggledeeplink() {
    if($("input[name='deeplink_url']").attr("disabled") == true){
	$("input[name='deeplink_url']").removeAttr("disabled");
	$("input[name='deeplink_url']").val("insert [URL] tag where partner url is allowed");
    } else {
	$("input[name='deeplink_url']").attr("disabled", true);
	$("input[name='deeplink_url']").val("");
	
    }
}

function updateTreeRights() {
    $("#staff_rights_tree").find("input").each(function(){
	$(this).removeAttr("checked");
    });
    if($("select[name='level']").val() == 1){ // Sale Manager
	// Agents
	$("input[name='agents']").attr("checked", "checked");
	$("input[name='create_new_agent']").attr("checked", "checked");
	$("input[name='agent_info']").attr("checked", "checked");
	$("input[name='agent_details']").attr("checked", "checked");
	$("input[name='suspend_agent']").attr("checked", "checked");
	$("input[name='modify_agent']").attr("checked", "checked");
	$("input[name='statistics']").attr("checked", "checked");
	$("input[name='payment_notes']").attr("checked", "checked");
	$("input[name='login_log']").attr("checked", "checked");
	$("input[name='disable_login']").attr("checked", "checked");
	$("input[name='enable_login']").attr("checked", "checked");
	$("input[name='unauthorize_logged_ips']").attr("checked", "checked");
	$("input[name='reauthorize_ips']").attr("checked", "checked");
	$("input[name='mail']").attr("checked", "checked");
	$("input[name='partner_message']").attr("checked", "checked");
	$("input[name='unauthorized_ips']").attr("checked", "checked");
	$("input[name='add_ips']").attr("checked", "checked");
	$("input[name='unauthorized_logins']").attr("checked", "checked");
	$("input[name='closed_agents']").attr("checked", "checked");
	$("input[name='distribution_list']").attr("checked", "checked");
	
	// Clients
	$("input[name='clients']").attr("checked", "checked");
	$("input[name='create_new_client']").attr("checked", "checked");
	$("input[name='client_details']").attr("checked", "checked");
	$("input[name='remove_client']").attr("checked", "checked");
	$("input[name='modify_details_client']").attr("checked", "checked");
	$("input[name='add_campaign']").attr("checked", "checked");
	$("input[name='add_contact']").attr("checked", "checked");
	$("input[name='view_invoices']").attr("checked", "checked");
	
	// Campaigns
	$("input[name='campaigns']").attr("checked", "checked");
	$("input[name='dublet_check']").attr("checked", "checked");
	$("input[name='create_new_dubletpool']").attr("checked", "checked");
	$("input[name='delete_dubletpool']").attr("checked", "checked");
	$("input[name='cosign_up']").attr("checked", "checked");
	$("input[name='add_campaign_to_dubletpool']").attr("checked", "checked");
	$("input[name='remove_campaign_from_dubletpool']").attr("checked", "checked");
	$("input[name='statistics_dubletpool']").attr("checked", "checked");
	$("input[name='approve_campaigns']").attr("checked", "checked");
	$("input[name='create_new_campaign']").attr("checked", "checked");
	$("input[name='campaign_details']").attr("checked", "checked");
	$("input[name='select_agents']").attr("checked", "checked");
	$("input[name='close_campaign']").attr("checked", "checked");
	$("input[name='add_banners']").attr("checked", "checked");
	$("input[name='add_xml_banners']").attr("checked", "checked");
	$("input[name='add_text_link']").attr("checked", "checked");
	$("input[name='stopped_banners']").attr("checked", "checked");
	$("input[name='get_tracking_code']").attr("checked", "checked");
	$("input[name='subscribers']").attr("checked", "checked");
	$("input[name='subscribers_click_stat']").attr("checked", "checked");
	$("input[name='subscribers_click_lead_stat']").attr("checked", "checked");
	$("input[name='subscribers_click_sale_stat']").attr("checked", "checked");
	$("input[name='campaign_statistics']").attr("checked", "checked");
	$("input[name='invoice_overview_campaign']").attr("checked", "checked");
	$("input[name='show_xml_orders']").attr("checked", "checked");
	$("input[name='export_xml_orders']").attr("checked", "checked");
	$("input[name='add_lead']").attr("checked", "checked");
	$("input[name='delete_lead']").attr("checked", "checked");
	$("input[name='banner_download_stats']").attr("checked", "checked");
	$("input[name='show_referrers']").attr("checked", "checked");
	$("input[name='campaign_email']").attr("checked", "checked");
	$("input[name='banners']").attr("checked", "checked");
	$("input[name='banner_statistics']").attr("checked", "checked");

    } else if($("select[name='level']").val() == 2){ // Media Manager
	// Agents
	$("input[name='agents']").attr("checked", "checked");
	$("input[name='create_new_agent']").attr("checked", "checked");
	$("input[name='agent_info']").attr("checked", "checked");
	$("input[name='agent_details']").attr("checked", "checked");
	$("input[name='suspend_agent']").attr("checked", "checked");
	$("input[name='modify_agent']").attr("checked", "checked");
	$("input[name='statistics']").attr("checked", "checked");
	$("input[name='payment_notes']").attr("checked", "checked");
	$("input[name='login_log']").attr("checked", "checked");
	$("input[name='disable_login']").attr("checked", "checked");
	$("input[name='enable_login']").attr("checked", "checked");
	$("input[name='unauthorize_logged_ips']").attr("checked", "checked");
	$("input[name='reauthorize_ips']").attr("checked", "checked");
	$("input[name='mail']").attr("checked", "checked");
	$("input[name='partner_message']").attr("checked", "checked");
	$("input[name='unauthorized_ips']").attr("checked", "checked");
	$("input[name='add_ips']").attr("checked", "checked");
	$("input[name='unauthorized_logins']").attr("checked", "checked");
	$("input[name='closed_agents']").attr("checked", "checked");
	$("input[name='distribution_list']").attr("checked", "checked");
	
	// Clients
	$("input[name='clients']").attr("checked", "checked");
	$("input[name='create_new_client']").attr("checked", "checked");
	$("input[name='client_details']").attr("checked", "checked");
	$("input[name='remove_client']").attr("checked", "checked");
	$("input[name='modify_details_client']").attr("checked", "checked");
	$("input[name='add_campaign']").attr("checked", "checked");
	$("input[name='add_contact']").attr("checked", "checked");
	$("input[name='view_invoices']").attr("checked", "checked");
	
	// Campaigns
	$("input[name='campaigns']").attr("checked", "checked");
	$("input[name='dublet_check']").attr("checked", "checked");
	$("input[name='create_new_dubletpool']").attr("checked", "checked");
	$("input[name='delete_dubletpool']").attr("checked", "checked");
	$("input[name='cosign_up']").attr("checked", "checked");
	$("input[name='add_campaign_to_dubletpool']").attr("checked", "checked");
	$("input[name='remove_campaign_from_dubletpool']").attr("checked", "checked");
	$("input[name='statistics_dubletpool']").attr("checked", "checked");
	$("input[name='approve_campaigns']").attr("checked", "checked");
	$("input[name='create_new_campaign']").attr("checked", "checked");
	$("input[name='campaign_details']").attr("checked", "checked");
	$("input[name='select_agents']").attr("checked", "checked");
	$("input[name='close_campaign']").attr("checked", "checked");
	$("input[name='add_banners']").attr("checked", "checked");
	$("input[name='add_xml_banners']").attr("checked", "checked");
	$("input[name='add_text_link']").attr("checked", "checked");
	$("input[name='stopped_banners']").attr("checked", "checked");
	$("input[name='get_tracking_code']").attr("checked", "checked");
	$("input[name='subscribers']").attr("checked", "checked");
	$("input[name='subscribers_click_stat']").attr("checked", "checked");
	$("input[name='subscribers_click_lead_stat']").attr("checked", "checked");
	$("input[name='subscribers_click_sale_stat']").attr("checked", "checked");
	$("input[name='campaign_statistics']").attr("checked", "checked");
	$("input[name='invoice_overview_campaign']").attr("checked", "checked");
	$("input[name='show_xml_orders']").attr("checked", "checked");
	$("input[name='export_xml_orders']").attr("checked", "checked");
	$("input[name='add_lead']").attr("checked", "checked");
	$("input[name='delete_lead']").attr("checked", "checked");
	$("input[name='banner_download_stats']").attr("checked", "checked");
	$("input[name='show_referrers']").attr("checked", "checked");
	$("input[name='campaign_email']").attr("checked", "checked");
	$("input[name='banners']").attr("checked", "checked");
	$("input[name='banner_statistics']").attr("checked", "checked");
    } else if($("select[name='level']").val() == 3){ // Admin
	// Home funtions
	$("input[name='home']").attr("checked", "checked");
	
	// Agents
	$("input[name='agents']").attr("checked", "checked");
	$("input[name='create_new_agent']").attr("checked", "checked");
	$("input[name='agent_info']").attr("checked", "checked");
	$("input[name='agent_details']").attr("checked", "checked");
	$("input[name='suspend_agent']").attr("checked", "checked");
	$("input[name='modify_agent']").attr("checked", "checked");
	$("input[name='statistics']").attr("checked", "checked");
	$("input[name='payment_notes']").attr("checked", "checked");
	$("input[name='login_log']").attr("checked", "checked");
	$("input[name='disable_login']").attr("checked", "checked");
	$("input[name='enable_login']").attr("checked", "checked");
	$("input[name='unauthorize_logged_ips']").attr("checked", "checked");
	$("input[name='reauthorize_ips']").attr("checked", "checked");
	$("input[name='mail']").attr("checked", "checked");
	$("input[name='partner_message']").attr("checked", "checked");
	$("input[name='unauthorized_ips']").attr("checked", "checked");
	$("input[name='add_ips']").attr("checked", "checked");
	$("input[name='unauthorized_logins']").attr("checked", "checked");
	$("input[name='closed_agents']").attr("checked", "checked");
	$("input[name='distribution_list']").attr("checked", "checked");
	
	// Clients
	$("input[name='clients']").attr("checked", "checked");
	$("input[name='create_new_client']").attr("checked", "checked");
	$("input[name='client_details']").attr("checked", "checked");
	$("input[name='remove_client']").attr("checked", "checked");
	$("input[name='modify_details_client']").attr("checked", "checked");
	$("input[name='add_campaign']").attr("checked", "checked");
	$("input[name='add_contact']").attr("checked", "checked");
	$("input[name='view_invoices']").attr("checked", "checked");
	
	// Campaigns
	$("input[name='campaigns']").attr("checked", "checked");
	$("input[name='dublet_check']").attr("checked", "checked");
	$("input[name='create_new_dubletpool']").attr("checked", "checked");
	$("input[name='delete_dubletpool']").attr("checked", "checked");
	$("input[name='cosign_up']").attr("checked", "checked");
	$("input[name='add_campaign_to_dubletpool']").attr("checked", "checked");
	$("input[name='remove_campaign_from_dubletpool']").attr("checked", "checked");
	$("input[name='statistics_dubletpool']").attr("checked", "checked");
	$("input[name='approve_campaigns']").attr("checked", "checked");
	$("input[name='create_new_campaign']").attr("checked", "checked");
	$("input[name='campaign_details']").attr("checked", "checked");
	$("input[name='select_agents']").attr("checked", "checked");
	$("input[name='close_campaign']").attr("checked", "checked");
	$("input[name='add_banners']").attr("checked", "checked");
	$("input[name='add_xml_banners']").attr("checked", "checked");
	$("input[name='add_text_link']").attr("checked", "checked");
	$("input[name='stopped_banners']").attr("checked", "checked");
	$("input[name='get_tracking_code']").attr("checked", "checked");
	$("input[name='subscribers']").attr("checked", "checked");
	$("input[name='subscribers_click_stat']").attr("checked", "checked");
	$("input[name='subscribers_click_lead_stat']").attr("checked", "checked");
	$("input[name='subscribers_click_sale_stat']").attr("checked", "checked");
	$("input[name='campaign_statistics']").attr("checked", "checked");
	$("input[name='invoice_overview_campaign']").attr("checked", "checked");
	$("input[name='show_xml_orders']").attr("checked", "checked");
	$("input[name='export_xml_orders']").attr("checked", "checked");
	$("input[name='add_lead']").attr("checked", "checked");
	$("input[name='delete_lead']").attr("checked", "checked");
	$("input[name='banner_download_stats']").attr("checked", "checked");
	$("input[name='show_referrers']").attr("checked", "checked");
	$("input[name='campaign_email']").attr("checked", "checked");
	$("input[name='banners']").attr("checked", "checked");
	$("input[name='banner_statistics']").attr("checked", "checked");
	
	// Finance
	$("input[name='finance']").attr("checked", "checked");
	$("input[name='revenue_per_campaign']").attr("checked", "checked");
	$("input[name='agent_revenue']").attr("checked", "checked");
	$("input[name='show_all_month']").attr("checked", "checked");
	$("input[name='agent_payments']").attr("checked", "checked");
	$("input[name='rollback']").attr("checked", "checked");
	$("input[name='export_payments']").attr("checked", "checked");
	$("input[name='close_month']").attr("checked", "checked");
	$("input[name='revenue_per_day']").attr("checked", "checked");
	$("input[name='invoice_overview']").attr("checked", "checked");
	$("input[name='create_invoice']").attr("checked", "checked");
	$("input[name='invoice_unpaid']").attr("checked", "checked");
	$("input[name='invoice_due']").attr("checked", "checked");
	$("input[name='invoice_paid']").attr("checked", "checked");
	$("input[name='gross_profit']").attr("checked", "checked");
	$("input[name='export_invoices']").attr("checked", "checked");
	$("input[name='export_payment_notes']").attr("checked", "checked");
	$("input[name='invoice_paid_extra']").attr("checked", "checked");
	
	// Options
	$("input[name='options']").attr("checked", "checked");
	$("input[name='manage_staff']").attr("checked", "checked");
	$("input[name='accounts']").attr("checked", "checked");
	$("input[name='add_new_account']").attr("checked", "checked");
	$("input[name='properties']").attr("checked", "checked");
	$("input[name='edit_categories']").attr("checked", "checked");
	$("input[name='change_log']").attr("checked", "checked");
	$("input[name='edit_countries']").attr("checked", "checked");
	$("input[name='staff_login_log']").attr("checked", "checked");
	$("input[name='group_by_ip']").attr("checked", "checked");
	$("input[name='edit_search_tags']").attr("checked", "checked");
	
    } else if($("select[name='level']").val() == 4){ // Disabled
	// No rights
    } else if($("select[name='level']").val() == 5){ // Country Manager
	// Agents
	$("input[name='agents']").attr("checked", "checked");
	$("input[name='create_new_agent']").attr("checked", "checked");
	$("input[name='agent_info']").attr("checked", "checked");
	$("input[name='agent_details']").attr("checked", "checked");
	$("input[name='suspend_agent']").attr("checked", "checked");
	$("input[name='modify_agent']").attr("checked", "checked");
	$("input[name='statistics']").attr("checked", "checked");
	$("input[name='payment_notes']").attr("checked", "checked");
	$("input[name='login_log']").attr("checked", "checked");
	$("input[name='disable_login']").attr("checked", "checked");
	$("input[name='enable_login']").attr("checked", "checked");
	$("input[name='unauthorize_logged_ips']").attr("checked", "checked");
	$("input[name='reauthorize_ips']").attr("checked", "checked");
	$("input[name='mail']").attr("checked", "checked");
	$("input[name='partner_message']").attr("checked", "checked");
	$("input[name='unauthorized_ips']").attr("checked", "checked");
	$("input[name='add_ips']").attr("checked", "checked");
	$("input[name='unauthorized_logins']").attr("checked", "checked");
	$("input[name='closed_agents']").attr("checked", "checked");
	$("input[name='distribution_list']").attr("checked", "checked");
	
	// Clients
	$("input[name='clients']").attr("checked", "checked");
	$("input[name='create_new_client']").attr("checked", "checked");
	$("input[name='client_details']").attr("checked", "checked");
	$("input[name='remove_client']").attr("checked", "checked");
	$("input[name='modify_details_client']").attr("checked", "checked");
	$("input[name='add_campaign']").attr("checked", "checked");
	$("input[name='add_contact']").attr("checked", "checked");
	$("input[name='view_invoices']").attr("checked", "checked");
	
	// Campaigns
	$("input[name='campaigns']").attr("checked", "checked");
	$("input[name='dublet_check']").attr("checked", "checked");
	$("input[name='create_new_dubletpool']").attr("checked", "checked");
	$("input[name='delete_dubletpool']").attr("checked", "checked");
	$("input[name='cosign_up']").attr("checked", "checked");
	$("input[name='add_campaign_to_dubletpool']").attr("checked", "checked");
	$("input[name='remove_campaign_from_dubletpool']").attr("checked", "checked");
	$("input[name='statistics_dubletpool']").attr("checked", "checked");
	$("input[name='approve_campaigns']").attr("checked", "checked");
	$("input[name='create_new_campaign']").attr("checked", "checked");
	$("input[name='campaign_details']").attr("checked", "checked");
	$("input[name='select_agents']").attr("checked", "checked");
	$("input[name='close_campaign']").attr("checked", "checked");
	$("input[name='add_banners']").attr("checked", "checked");
	$("input[name='add_xml_banners']").attr("checked", "checked");
	$("input[name='add_text_link']").attr("checked", "checked");
	$("input[name='stopped_banners']").attr("checked", "checked");
	$("input[name='get_tracking_code']").attr("checked", "checked");
	$("input[name='subscribers']").attr("checked", "checked");
	$("input[name='subscribers_click_stat']").attr("checked", "checked");
	$("input[name='subscribers_click_lead_stat']").attr("checked", "checked");
	$("input[name='subscribers_click_sale_stat']").attr("checked", "checked");
	$("input[name='campaign_statistics']").attr("checked", "checked");
	$("input[name='invoice_overview_campaign']").attr("checked", "checked");
	$("input[name='show_xml_orders']").attr("checked", "checked");
	$("input[name='export_xml_orders']").attr("checked", "checked");
	$("input[name='add_lead']").attr("checked", "checked");
	$("input[name='delete_lead']").attr("checked", "checked");
	$("input[name='banner_download_stats']").attr("checked", "checked");
	$("input[name='show_referrers']").attr("checked", "checked");
	$("input[name='campaign_email']").attr("checked", "checked");
	$("input[name='banners']").attr("checked", "checked");
	$("input[name='banner_statistics']").attr("checked", "checked");
    } else if($("select[name='level']").val() == 6){ // Revisor
	// Finance
	$("input[name='finance']").attr("checked", "checked");
	$("input[name='revenue_per_campaign']").attr("checked", "checked");
	$("input[name='agent_revenue']").attr("checked", "checked");
	$("input[name='show_all_month']").attr("checked", "checked");
	$("input[name='agent_payments']").attr("checked", "checked");
	$("input[name='rollback']").attr("checked", "checked");
	$("input[name='export_payments']").attr("checked", "checked");
	$("input[name='close_month']").attr("checked", "checked");
	$("input[name='revenue_per_day']").attr("checked", "checked");
	$("input[name='invoice_overview']").attr("checked", "checked");
	$("input[name='create_invoice']").attr("checked", "checked");
	$("input[name='invoice_unpaid']").attr("checked", "checked");
	$("input[name='invoice_due']").attr("checked", "checked");
	$("input[name='invoice_paid']").attr("checked", "checked");
	$("input[name='gross_profit']").attr("checked", "checked");
	$("input[name='export_invoices']").attr("checked", "checked");
	$("input[name='export_payment_notes']").attr("checked", "checked");
	$("input[name='invoice_paid_extra']").attr("checked", "checked");
    }
}

function updateClosedUrl() {
    if($("#campaign_type option:selected").text() == "CLICK"){
	var val = $("input[name='camp_url']").val();
	$("input[name='closed_url']").val(val);
    }
}

function modify_tags(tags) {
    $("#tag_container").html(tags);
}

function updateStatImage(camp_id) {
    var tmp = $("#updateStatSelector").val();
    if(tmp == "sale"){
	window.location.href = "staff_subscribers_sale_stat.pl?page=campaigns&camp_id="+camp_id;
    } else if(tmp == "click_sale"){
	window.location.href = "staff_subscribers_clicksale_stat.pl?page=campaigns&camp_id="+camp_id;
    } else if(tmp == "click_lead") {
	window.location.href = "staff_subscribers_clicklead_stat.pl?page=campaigns&camp_id="+camp_id;
    } else if(tmp == "lead"){
	window.location.href = "staff_subscribers_lead_stat.pl?page=campaigns&camp_id="+camp_id;
    }
    
}

function modify_campaign_table() {
    $(".maxnumberofclicks_row").css("display", "");
    $(".maxnumberofleads_row").css("display", "");
    $(".maxnumberofimpr_row").css("display", "");
    $(".maxsaleamount_row").css("display", "");
    $(".clientpayperlead_row").css("display", "");
    $(".agentreceiveperlead_row").css("display", "");
    $(".clientpayperimpr_row").css("display", "");
    $(".agentreceiveperimpr_row").css("display", "");
    $(".clientpayperclick_row").css("display", "");
    $(".agentreceiveperclick_row").css("display", "");
    $(".cookielength_row").css("display", "");
    $(".percentageclient_row").css("display", "");
    $(".percentageagent_row").css("display", "");
    $(".dubletpool_row").css("display", "");
    $(".dubletcheck_row").css("display", "");
    $(".productfeed_row").css("display", "");
    $(".deeplink_row").css("display", "");
    if($("#campaign_type option:selected").text() == "LEAD"){
	$(".maxnumberofclicks_row").css("display", "none");
	$(".maxsaleamount_row").css("display", "none");
	$(".clientpayperclick_row").css("display", "none");
	$(".agentreceiveperclick_row").css("display", "none");
	$(".percentageclient_row").css("display", "none");
	$(".percentageagent_row").css("display", "none");
	$(".maxnumberofimpr_row").css("display", "none");
        $(".clientpayperimpr_row").css("display", "none");
        $(".agentreceiveperimpr_row").css("display", "none");
    } else if($("#campaign_type option:selected").text() == "CLICK"){
	$(".maxnumberofleads_row").css("display", "none");
	$(".maxsaleamount_row").css("display", "none");
	$(".clientpayperlead_row").css("display", "none");
	$(".agentreceiveperlead_row").css("display", "none");
	$(".percentageclient_row").css("display", "none");
	$(".percentageagent_row").css("display", "none");
	$(".dubletpool_row").css("display", "none");
	$(".dubletcheck_row").css("display", "none");
	$(".productfeed_row").css("display", "none");
	$(".deeplink_row").css("display", "none");
	$(".cookielength_row").css("display", "none");
        $(".maxnumberofimpr_row").css("display", "none");
        $(".clientpayperimpr_row").css("display", "none");
        $(".agentreceiveperimpr_row").css("display", "none");
    } else if($("#campaign_type option:selected").text() == "SALE"){
	$(".maxnumberofclicks_row").css("display", "none");
	$(".maxnumberofleads_row").css("display", "none");
	$(".clientpayperlead_row").css("display", "none");
	$(".agentreceiveperlead_row").css("display", "none");
	$(".clientpayperclick_row").css("display", "none");
	$(".agentreceiveperclick_row").css("display", "none");
        $(".maxnumberofimpr_row").css("display", "none");
        $(".clientpayperimpr_row").css("display", "none");
        $(".agentreceiveperimpr_row").css("display", "none");
    } else if($("#campaign_type option:selected").text() == "IMPR"){
	$(".maxnumberofclicks_row").css("display", "none");
	$(".maxnumberofleads_row").css("display", "none");
	$(".maxsaleamount_row").css("display", "none");
	$(".clientpayperlead_row").css("display", "none");
	$(".agentreceiveperlead_row").css("display", "none");
	$(".clientpayperclick_row").css("display", "none");
	$(".agentreceiveperclick_row").css("display", "none");
	$(".cookielength_row").css("display", "none");
	$(".percentageclient_row").css("display", "none");
	$(".percentageagent_row").css("display", "none");
	$(".dubletpool_row").css("display", "none");
	$(".dubletcheck_row").css("display", "none");
	$(".productfeed_row").css("display", "none");
	$(".deeplink_row").css("display", "none");
    }
}

function adjustBanners() {
    $(".banner_table").find("img").each(function(){
	var width = $(this).css("width");
	width = width.substr(0,3);
	if(width > 700){
	    $(this).css("width", "700px");
	}
    });
}

function removeAgent(agent_id, camp_id){
	$.get('/cgi-bin/ajax_functions.pl', {action: "removeAgent", agent_id : agent_id, camp_id : camp_id});
	$("#"+agent_id).css("display", "none");
}

function agent_emails_btn(){
    if($("#agent_emails").css("display") == "block"){
	$("#agent_emails").css("display", "none");
    } else {
	$("#agent_emails").css("display", "block");
    }
}

function selected_agent_emails_btn() {
    if($("#selected_agent_emails").css("display") == "block"){
	$("#selected_agent_emails").css("display", "none");
    } else {
	var tmp = "";
	$("input[name='agent_selectbox']:checked").each(function(){
		if(tmp != ''){tmp += "; ";}
		tmp += $(this).val();    
	});
	$("#selected_agent_emails").html(tmp);
	$("#selected_agent_emails").css("display", "block");
    }
}

function checkChooseCompany(){
    var tmp;
    $("input[name='group1']:checked").each(function(){
	tmp = $(this).val();
	$("input[name='agent_id']").val(tmp);
    });
    if(tmp){
	return true;
    } else {
	return false;
    }
}

function show_analytics(div) {
    var open = true;
    if($("#"+div).css("display") == "block"){
	open = false;
    }
    $(".analytics_box_content").each(function(){
	//$(this).css("display","none");
	$(this).slideUp();
    });
    if(open){
	$("#"+div).slideDown();
    }
}

function checkPassword(){
    var pass = $("input[name='passw']").val();
    if(pass.length < 6){
	alert("Password is too short. Minimun 6 characters!");
	return false;
    } else {
	return true;
    }
    
}

function checkDisableParameters(){
    if($("input[name='disable_agent_1']").attr('checked') == false &&
       $("input[name='disable_agent_2']").attr('checked') == false &&
       $("input[name='disable_agent_3']").attr('checked') == false &&
       $("input[name='disable_agent_4']").attr('checked') == false &&
       $("textarea[name='disable_agent_5']").val() == "") {
	alert("Choose a reason!");
	return false;
    } else {
	document.forms["disablelogin_form"].submit();
    }
}

function export_subscribers_xml(id, type) {
	if($("input[name='from_date']").val() == ""){
		alert("Angiv en 'From' dato");
	} else if($("input[name='to_date']").val() == ""){
		alert("Angiv en 'To' dato");
	} else {
		if($("input[name='agent_check']:checked").val() == "on"){
			$("#ajax_loader").css("display", "block");
			var agents = "";
			$("input[name='agent_check']:checked").each(function(){
				if(agents != ""){
					agents += "-";
				}
				agents += $(this).attr("id");
			});
			if(type == "stat"){
				$.get('/cgi-bin/ajax_functions.pl', {action: "agentSubscriptionXml", camp_id : id, from_date : $("input[name='from_date']").val(), to_date : $("input[name='to_date']").val(), agents:agents}, function(data) {
					window.open(data, "Download XML");
					$("#ajax_loader").css("display", "none");
				});
			} else if(type == "lead_stat"){
				$.get('/cgi-bin/ajax_functions.pl', {action: "agentSubscriptionLeadXml", camp_id : id, from_date : $("input[name='from_date']").val(), to_date : $("input[name='to_date']").val(), agents:agents}, function(data) {
					window.open(data, "Download XML");
					$("#ajax_loader").css("display", "none");
				});
			} else if(type == "sale_stat"){
				$.get('/cgi-bin/ajax_functions.pl', {action: "agentSubscriptionSaleXml", camp_id : id, from_date : $("input[name='from_date']").val(), to_date : $("input[name='to_date']").val(), agents:agents}, function(data) {
					window.open(data, "Download XML");
					$("#ajax_loader").css("display", "none");
				});
			} else if(type == "clicklead_stat"){
				$.get('/cgi-bin/ajax_functions.pl', {action: "agentSubscriptionClickLeadXml", camp_id : id, from_date : $("input[name='from_date']").val(), to_date : $("input[name='to_date']").val(), agents:agents}, function(data) {
					window.open(data, "Download XML");
					$("#ajax_loader").css("display", "none");
				});
			} else if(type == "clicksale_stat"){
				$.get('/cgi-bin/ajax_functions.pl', {action: "agentSubscriptionClickSaleXml", camp_id : id, from_date : $("input[name='from_date']").val(), to_date : $("input[name='to_date']").val(), agents:agents}, function(data) {
					window.open(data, "Download XML");
					$("#ajax_loader").css("display", "none");
				});
			}
		} else {
			alert("Vælg de agenter du vil se statistik for");
		}
	}	
}

function view_warnings() {
	if($("#warning_div").css("display") == "block"){
		$("#warning_div").slideUp();
	} else {
		$("#warning_div").slideDown();
	}
}

