﻿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();
        }
    });
}

// check the dropdowns
function chkI(i_input, i_defaultValue) {
    if (i_defaultValue != null && i_defaultValue != "" && i_defaultValue.length > 0 && i_input.value == i_defaultValue) {
        $(i_input).addClass("error2");
        return false;
    }
    else if (i_input.value.length > 0) {
        $(i_input).removeClass("error2");
        return true;
    }
    else {
        $(i_input).addClass("error2");
        return false;
    }
}

// check the dropdowns
function chkM(i_input, i_defaultValue) {
    if (chkI(i_input, i_defaultValue)) {
        if (!i_input.value.match(/^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/i))
            $(i_input).addClass("error2");
        else
            return true;
    }
    return false;
}