﻿
function openWeather()
{
    tmSizedPopup('/addons/weather/WeatherReport.aspx','weather',400,300);
}
function fileBrowserCallBack(i_fieldName) {
    imageBrowserWin = window.open('/admin/FileManager/','imageBrowser','width=760,height=420,status=yes,toolbar=no,scrollbars=yes,resizable=yes');
    document.imageWinFieldname = i_fieldName;
    imageBrowserWin.focus();
}

function fileBrowserCallBack_return(fileRef)
{
    
    if(document.imageWinRef != null)
        document.imageWinRef.document.forms[0][document.imageWinFieldname].value=fileRef;
    else
        document.forms[0][document.imageWinFieldname].value=fileRef;
    if (document.imageWinFieldname.indexOf("imgLink")>-1)
    {
        var splitName = document.imageWinFieldname.substring("imgLink".length);
        updateImage(splitName, fileRef);
    }
    else if (document.imageWinFieldname.indexOf("swfLink")>-1)
    {
        var splitName = document.imageWinFieldname.substring("swfLink".length);
        updateSWF(splitName, fileRef);
    }
}


// WTB OPTIMERING...
function updateSWF(i_key, i_url)
{
    var w = document.getElementById('swfWidth' + i_key).value;
    var h = document.getElementById('swfHeight' + i_key).value;
    $.post("/admin/SWFArea_update.aspx",{key: i_key,url: i_url, width: w, height: h}, function(i_val) {
        if (i_val == "width")
        {
            if (confirm("Vald flashs bredd är större än ytan specificerade bredd, vill du ändå fortsätta?"))
            {
                $.post("/admin/SWFArea_update.aspx",{key: i_key,url: i_url,width: w, height: h, confirmed: "true"}, function(i_val2){
                    if (i_val2 == "not_found")
                        alert("Flashen blev borttagen någon gång under processen, kontrollera att den finns!");
                    else
                    {
                        window.location = window.location;
                        window.focus();
                    }
                });
            }
        }
        else if (i_val == "height")
        {
            if (confirm("Vald flashs höjd är större än ytan specificerade höjd, vill du ändå fortsätta?"))
            {
                alert("postar igen");
                $.post("/admin/SWFArea_update.aspx",{key: i_key,url: i_url,width: w, height: h, confirmed: "true"}, function(i_val2){
                    if (i_val2 == "not_found")
                        alert("Flashen blev borttagen någon gång under processen, kontrollera att den finns!");
                    else
                    {
                        window.location = window.location;
                        window.focus();
                    }
                });
                
            }
        }
        else if (i_val == "widthheight")
        {
            if (confirm("Flashens storlek är både bredare och högre än ytans storlek, vill du ändå fortsätta?"))
            {
                $.post("/admin/SWFArea_update.aspx",{key: i_key,url: i_url,width: w, height: h, confirmed: "true"}, function(i_val2){
                    if (i_val2 == "not_found")
                        alert("Flashen blev borttagen någon gång under processen, kontrollera att den finns!");
                    else
                    {
                        window.location = window.location;
                        window.focus();
                    }
                });
            }
        }
        else if (i_val == "not_found")
            alert("Flashen blev borttagen någon gång under processen, kontrollera att den finns!");
        else
        {
            window.location = window.location;
            window.focus();
        }
    }); 
}

// WTB OPTIMERING...
function updateImage(i_key, i_url)
{
    
    var w = document.getElementById('width' + i_key).value;
    var h = document.getElementById('height' + i_key).value;
    $.post("/admin/ImageArea_update.aspx",{key: i_key,url: i_url,width: w, height: h}, function(i_val) {
        if (i_val == "width")
        {
            if (confirm("Vald bilds bredd är större än ytan specificerade bredd, vill du ändå fortsätta?"))
            {
                $.post("/admin/ImageArea_update.aspx",{key: i_key,url: i_url,width: w, height: h, confirmed: "true"}, function(i_val2){
                    if (i_val2 == "not_found")
                        alert("Bilden blev borttagen någon gång under processen, kontrollera att den finns!");
                    else
                    {
                        window.location = window.location;
                        window.focus();
                    }
                });
            }
        }
        else if (i_val == "height")
        {
            if (confirm("Vald bilds höjd är större än ytan specificerade höjd, vill du ändå fortsätta?"))
            {
                $.post("/admin/ImageArea_update.aspx",{key: i_key,url: i_url,width: w, height: h, confirmed: "true"}, function(i_val2){
                    if (i_val2 == "not_found")
                        alert("Bilden blev borttagen någon gång under processen, kontrollera att den finns!");
                    else
                    {
                        window.location = window.location;
                        window.focus();
                    }
                });
                
            }
        }
        else if (i_val == "widthheight")
        {
            if (confirm("Bildens storlek är både bredare och högre än ytans storlek, vill du ändå fortsätta?"))
            {
                $.post("/admin/ImageArea_update.aspx",{key: i_key,url: i_url,width: w, height: h, confirmed: "true"}, function(i_val2){
                    if (i_val2 == "not_found")
                        alert("Bilden blev borttagen någon gång under processen, kontrollera att den finns!");
                    else
                    {
                        window.location = window.location;
                        window.focus();
                    }
                });
            }
        }
        else if (i_val == "not_found")
            alert("Bilden blev borttagen någon gång under processen, kontrollera att den finns!");
        else
        {
            window.location = window.location;
            window.focus();
        }
    }); 
}

/* ASk for removal */
function Ask(i_question, i_redirUrl)
{
    if (confirm(i_question))
    {
        tmSizedPopup(i_redirUrl, 'title', 100,100);
    }
}

function CheckDD(i_field)
{
    
    if (i_field.selectedIndex > 0)
        $(i_field).removeClass("error2");
    else
        $(i_field).addClass("error2");
}

function CheckEmail(i_field)
{
    CheckText(i_field);
    if (i_field.className != 'error2')
    {
        if (!i_field.value.match(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i))
            $(i_field).addClass("error2");
    }
}

function CheckText(i_field)
{
    if (i_field.value.length > 0)
        $(i_field).removeClass("error2");
    else
        $(i_field).addClass("error2");
}

function SelectedCalendarium(i_which)
{
    
    theOne = $(i_which);
    second = $("#ctl00_ContentPlaceHolder1_ctl00_ddWhen");
    third = $("#ctl00_ContentPlaceHolder1_ctl00_ddWhat");
    fourth = $("#ctl00_ContentPlaceHolder1_ctl00_ddTheme");
    
    if (theOne.attr("id") == "ctl00_ContentPlaceHolder1_ctl00_ddWhen")
        second = $("#ctl00_ContentPlaceHolder1_ctl00_ddWhere");
        
    if (theOne.attr("id") == "ctl00_ContentPlaceHolder1_ctl00_ddWhat")
        third = $("#ctl00_ContentPlaceHolder1_ctl00_ddWhere");
        
    if (theOne.attr("id") == "ctl00_ContentPlaceHolder1_ctl00_ddTheme")
        fourth = $("#ctl00_ContentPlaceHolder1_ctl00_ddWhere");
    
    if (theOne.attr("selectedIndex") > 1)
    {
        tmpWhat = $("#ctl00_ContentPlaceHolder1_ctl00_ddWhat").attr("value");
        tmpWhen = $("#ctl00_ContentPlaceHolder1_ctl00_ddWhen").attr("value");
        tmpWhere = $("#ctl00_ContentPlaceHolder1_ctl00_ddWhere").attr("value");
        tmpTheme = $("#ctl00_ContentPlaceHolder1_ctl00_ddTheme").attr("value");
        second.html("<option value=''>Laddar... Vänta</option>");
        third.html("<option value=''>Laddar... Vänta</option>");
        $.post("/ajax/fetch" + second.attr("id").substring("ctl00_ContentPlaceHolder1_ctl00_dd".length) + ".aspx",
            { what: tmpWhat, when: tmpWhen, where: tmpWhere, theme: tmpTheme }, function (data) {
                second.html(data);
                $.post("/ajax/fetch" + third.attr("id").substring("ctl00_ContentPlaceHolder1_ctl00_dd".length) + ".aspx",
                { what: tmpWhat, when: tmpWhen, where: tmpWhere, theme: tmpTheme }, function (data) {
                    third.html(data);
                    $.post("/ajax/fetch" + fourth.attr("id").substring("ctl00_ContentPlaceHolder1_ctl00_dd".length) + ".aspx",
                    { what: tmpWhat, when: tmpWhen, where: tmpWhere, theme: tmpTheme }, function (data) {
                        fourth.html(data);
                        $(function () {
                            Retranslate("#leftColumn");
                        });
                    });
                });
            });
        
        
    }
}
function UpdateList(i_val, i_showall)
{
    tmpWhat = $("#ctl00_ContentPlaceHolder1_ctl00_ddWhat").attr("value");
    tmpWhen = $("#ctl00_ContentPlaceHolder1_ctl00_ddWhen").attr("value");
    tmpWhere = $("#ctl00_ContentPlaceHolder1_ctl00_ddWhere").attr("value");
    tmpTheme = $("#ctl00_ContentPlaceHolder1_ctl00_ddTheme").attr("value");
    if (i_showall == true)
        tmpWhat = tmpWhen = tmpWhere = tmpTheme = "";
    else
        i_showall = false;
    i_page = 1;
    if (i_val > 0)
    {
        if (i_val >= 1)
            i_page = i_val;
    }
    $("#activityList").html("<ul>Laddar...</ul>");
    $.post("/ajax/fetchActivities.aspx", { what: tmpWhat, when: tmpWhen, where: tmpWhere, theme: tmpTheme, page: i_page, showall: i_showall }, function (data) {
        $("#activityList").html(data);
        Retranslate("#rightColumn");
    });
}
function ShowArticle(i_act)
{
    $(".searchResults").html("Laddar... Var god vänta.");
    $.post("/ajax/fetchActivity.aspx", { activityId: i_act }, function (data) {
        $(".searchResults").html(data);
        var addthis_config = {};
        var addthis_share = { url: "http://www.orebro2010.se/content/calendarium.aspx?show=" + i_act };
        addthis.button("#shareButton", addthis_config, addthis_share);
        ShowEmail("#activityMail", $("#mBeforeAt").val(), $("#mAfterAt").val(), $("#mAfterDot").val(), "<br />E-post ");
        Retranslate("#middleColumn");
        location.hash = "#" + i_act;
    });
}

function ActivityRemove(i_activity)
{
    if (confirm("Vill du verkligen ta bort aktiviteten?"))
    {
        $.post("/admin/Activity_edit.aspx", {action:"remove", activityId:i_activity}, function() {
            // refresh acitivites
            $("#activityList").html("Laddar... Var god vänta.");
            $.post("/ajax/fetchActivities.aspx", function(data) {
                $("#activityList").html(data);
            });
        });
    }
}

function CorrectEmailaddress(i_email)
{
    i_email = $("#" + i_email);
    if (i_email.attr("value").length > 255)
    {
        alert("Ange som mest 255 tecken i \"E-postadress\" Fält.");
        i_email.focus();
        return (false);
    }

    if (i_email.attr("value") == "")
    {
        alert("Ange värdet för \"E-postadress\" Fält.");
        i_email.focus();
        return (false);
    }

    if (i_email.attr("value").length < 7)
    {
        alert("Ange minst 7 tecken i \"E-postadress\"Fält.");
        i_email.focus();
        return (false);
    }
    val = i_email.attr("value");
    at = val.indexOf("@");
    lastat = val.lastIndexOf("@");
    dot = val.lastIndexOf(".");
    if (at<1 || at!=lastat || dot<at)
    {
        alert("Din e-postadress är inte rätt. Försök igen.");
        return false;
    }
}


function SendNewsLetter(i_name, i_email)
{
    CorrectEmailaddress(i_email);
    
    //submitNL
    obj = $("#SubscriberForm");
    str = '';
    str += '';
    str += '';
    obj.append(str);
    $("form").submit();
}

function ShowEmail(i_where, i_beforeAt, i_afterAt, i_afterDot, i_additional)
{
    if (i_additional == null)
        i_additional = "";
    builtStr = i_beforeAt + "@" + i_afterAt + "." + i_afterDot;
    $(i_where).append(i_additional + '<a href="mailto:' + builtStr + '">' + builtStr + '</a>');
}