function popup(url, target, width, height, features)
{
    var features1 = 'width=' + width + ',height=' + height;
    if (screen) {
        features1 += ',left=' + (screen.width - width) / 2 +
                ',top=' + (screen.height - height) /2;
    }
    if (features) {
        features1 += ',' + features;
    }
    var wnd = window.open(url, target, features1);
    wnd.focus();

    return wnd;
}


function openURLInPopup(url, width, height)
{
    if (typeof(width) == "undefined") {
        width = 800;
        height = 600;
    }

    if (typeof(height) == "undefined") {
        height = 600;
    }

    popup(url, 'slideShow', width, height, 'menubar=0,location=0,toolbar=0,status=0,scrollbars=1');
}

function trailer(filename, w, h)
{
    url = "/flvplayer.php?filename=" + filename;
    wnd = popup(url, "Trailer", w + 30, h + 100, "menubar=0,location=0,toolbar=0,status=0,scrollbars=0,resizable=0");
    wnd.focus();
}

function validate(action, formName)
{
    if (formName == undefined) {
        formName = "form1";        
        
    }
     
    document.forms[formName].elements["action"].value = action;
    document.forms[formName].submit();
}

function add_save_campaign(c_id)
{
    validate('commit','edit_campaign');
    return true;
}

function save_campaign()
{
    validate('edit','edit_campaign');
    return true;
}

function save_publish_campaign()
{
    validate('save_publish','edit_campaign');
    return true;
}




function clear_clicks()
{
    document.getElementById("estimated_value").innerHTML = "";
    document.getElementById("estimate").style.display = "inline";
}

function hide_estimate()
{
    document.getElementById("estimate").style.display = "none";
}

function modal_open (url, width, height) {
    cvr_edit = new CoverBody("#648AAA", url, width, height);
    cvr_edit.Create();
}

function add_site (url, width, height) {
    cvr_edit = new CoverBody("#648AAA", url, width, height);
    cvr_edit.Create();
}



function redirect(url)
{
    parent.window.reload();
}

function set_campaign_status(id, action)
{
    if (action == 'no_action') {
        alert('Aceasta campanie a expirat!');
        return false;
    }
    
    $.get('add_campaign.php', 
        { id : id ,action : action}, 
        function(html) { 
            var res = $.evalJSON(html);
            $("#campaign_status").removeClass();
            $("#campaign_status").addClass(res.spanclass);
            $("#campaign_status").html(res.status);
            $("#status_icon").attr("src", function() { 
                return res.play_img_src;
            });
        }
    );  
}

function remov(rel)
{
    
    try{
        cvr_edit.Remove(rel);
    } catch(e){
         try {
            parent.cvr_edit.Remove(rel);
        } catch(e) {
        
        }
    }
}

function initHttpRequest()
{
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}


function htmlspecialchars(p_string) {
    p_string = p_string.replace(/&/g, '&amp;');
    p_string = p_string.replace(/</g, '&lt;');
    p_string = p_string.replace(/>/g, '&gt;');
    p_string = p_string.replace(/"/g, '&quot;');
    return p_string;
};

function keycode(e)
{
    var key = (e)? e.which: event.keyCode;

    alert(key);
}

// secific functions

function getByAjax(href, container)
{
    return '$.get("'+href+'",{}, function(html){ $("#'+_container+'").html(html);}); return false;';
}

function x_close(id)
{
    $(id).removeClass('reports_product_show');
    $(id).addClass('reports_product_hide');
 
}


function toggle_modals()
{
    var modals = new Array();
    modals.push({
    'action': 'get_product',
    'container': 'p_container',
    'el':'li_sel_product'
    });
    
    modals.push({
    'action': 'get_campaign',
    'container': 'c_container',
    'el':'li_sel_campaign'
    });
    
    modals.push({
    'action': 'get_site',
    'container': 's_container',
    'el':'li_sel_site'
    });
     modals.push({
    'action': 'get_demographics',
    'container': 'd_container',
    'el':'li_sel_demographics'
    });
 
    
    modals.push({
    'action': 'get_advertiser',
    'container': 'a_container',
    'el':'li_sel_advertiser'
    });
 
    
    for (i = 0; i<modals.length; i++) {
        //alert(modals[i].el);
        $("#"+ modals[i].el).click(function(event){
            //alert(this.id);
           for (j = 0; j<modals.length; j++) {
                if (modals[j].el != this.id) {
                    $('#'+modals[j].container).removeClass('reports_product_show');
                    $('#'+modals[j].container).addClass('reports_product_hide');
                } else {
                    var target_el = '#'+modals[j].container;
                    $(target_el).removeClass('reports_product_hide');
                    $(target_el).addClass('reports_product_show');
                    $.ajax({
                        url : '/rapoarte/?action='+modals[j].action,
                        type : 'GET',
                        success: function(res){$(target_el).html(res);}
                    
                    });        
                }
            }
         
        });
    }
     
}


function toggle(e,c,a)
{
    $(e).click( function(){
        
        switch (a) {
        case 'get_product':
            $('#c_container').removeClass('reports_product_show');
            $('#c_container').addClass('reports_product_hide');
            $('#s_container').removeClass('reports_product_show');
            $('#s_container').addClass('reports_product_hide');
 
            break;
        case 'get_campaign':
            $('#p_container').removeClass('reports_product_show');
            $('#p_container').addClass('reports_product_hide');
            $('#s_container').removeClass('reports_product_show');
            $('#s_container').addClass('reports_product_hide');
            break;
        case 'get_site':
            $('#p_container').removeClass('reports_product_show');
            $('#p_container').addClass('reports_product_hide');
            $('#c_container').removeClass('reports_product_show');
            $('#c_container').addClass('reports_product_hide');
            break;
          
        }
       var class_name = $(c).attr("class");
        switch(class_name) {
        case "reports_product_show":
            $(c).removeClass('reports_product_show');
            $(c).addClass('reports_product_hide');
            return false;
            break;
        default :
            $(c).removeClass('reports_product_hide');
            $(c).addClass('reports_product_show');


            break;
       }
    $.ajax({
        url : '/rapoarte/?action='+a,
        type : 'GET',
        success: function(res){$(c).html(res);}
        
        });        
    });
   return false; 
}


function ajax_on_load(el_container, response_container)
{
    var ch = $(el_container).children();
    for (var i=0; i<ch.length ; i++ ) {
        if (ch[i].tagName == 'A') {
            var href= $(ch[i]).attr('href');
            $(ch[i]).click(function(){
                $.get(this.href, {}, function(html){ $(response_container).html(html);}); return false;
            });
            jQuery.url.setUrl(href);
        }
    }
}

function demographics_details()
{
    window.location.href = '/demografice/?grouping='+$("#id_grouping_options").val();
}

function report_details()
{
    window.location.href = '/rapoarte/?report_details='+$("#id_report_options").val();
}

function report_details_v2()
{
    window.location.href = '/rapoarte/?report_details='+$("#id_report_options").val();
}

function show_edit_icon(a_id)
{
    $('#a_'+a_id).removeClass('hidden'); 
    $('#a_'+a_id).addClass('visible');
}

function hide_edit_icon(a_id)
{
    $('#a_'+a_id).removeClass('visible'); 
    $('#a_'+a_id).addClass('hidden');
}

function nonperformed_action()
{
    
    if ($('#id_show_nonperformed').is(':checked')) {
        return "nonperformed";
    }
    return "only_from_campaign";
}


