
function dispatch(qs) {
    var href = location.href;
    var args = qs.split('&');
    for (i = 0; i < args.length; i++) {
        var cp = args[i].split('=');

        var hrefparts = href.split('?');
        var hrefDest = hrefparts[0];
        if (hrefparts.length > 1) {
            var found = false;
            var parameters = hrefparts[1].split('&');
            for (j = 0; j < parameters.length; j++) {
                sepChar = "&";
                if (j == 0) sepChar = '?';
                if (parameters[j].indexOf(cp[0] + '=') == 0) {
                    hrefDest += sepChar + args[i];
                    found = true;
                } else {
                    hrefDest += sepChar + parameters[j];
                }
            } 
            
            if(! found) {
                hrefDest += "&" + args[i];
            }
        } else {
            hrefDest += '?' + args[i];
        } 
        href = hrefDest;
    }
    
    location.href = href;
}

function tbl_sort(order, columnNum) {
    dispatch("order=" + order + "&sort=" + columnNum);
}

function tbl_changePage(page) {
    if (page < 0) {
        dispatch("pagesize=0");
    } else {
        dispatch("page=" + page);
    }
}

var currentRow = null;
function tbl_over(rowId) {
    if (currentRow != null) currentRow.style.backgroundColor = '';
    currentRow = document.getElementById(rowId);
    currentRow.style.backgroundColor = '#eeeeee';
}

function tbl_out(rowId) {
    currentRow.style.backgroundColor = '';
}

function tbl_click(id) {
}

function open_window(url, w, h) {
   doOpenWindow(url, -1, w, h);
}
function openWindowSized(url, w, h) {
   doOpenWindow(url, -1, w, h);
}
function openWindow(url, tp) {
   doOpenWindow(url, tp, -1, -1);
}

function doOpenWindow(url, tp, w, h) {

   // LearningResourceType.value
   // tp = 4, Experiment 
   // tp = 5, Testuppgift
   // tp = 6, Avslutande fråga
   // tp = 7, Typuppgift
   // tp = 9, Diagnostiskt prov
   // tp = 11, Inlämningsuppgift
   // tp = 12, Avslutande frågor
   // tp = 13, Seminarie
   // tp = 109, Interaktiv teori
   tp = Number(tp);
   if (isNaN(tp) || (tp <= 0)) { tp = -1; }

   // Bredd
   w = Number(w);
   if (isNaN(w) || (w <= 0) || (w >= screen.availWidth)) { w = 680; }

   // Höjd
   h = Number(h);
   if (isNaN(h) || (h <= 0) || (h >= screen.availHeight)) { h = 550; }
   
   // Standad x- och y-koordinater (är beroende av w och h)
   var xLeft = 0;
   var xCenter = (screen.availWidth - w)/2;
   var xRight = screen.availWidth - w;
   var yTop = 0;
   var yMiddle = (screen.availHeight - h)/2;
   var yBottom = screen.availHeight - h;

   // x- och y-koordinater
   var x = xCenter;
   var y = yMiddle;
   if ( ((4 <= tp) && (tp <= 7)) || (tp == 12) || (tp == 109) ) {
      x = xLeft;
      y = yMiddle;
   }
   else if (tp == 50) {
      x = xRight;
   }

   // Felhantering
   if ((x < 0) || (x >= screen.availWidth)) { x = xLeft; }
   if ((y < 0) || (y >= screen.availHeight)) { y = yTop; }
   if (w >= screen.availWidth) { w = (screen.availWidth*0.75); }
   if (h >= screen.availHeight) { h = (screen.availHeight*0.75); }

   // Target
   var target = "";
   if ((tp == 6) || (tp == 12)) { target = "tp6"; }   
   else if (tp == 9) { target = "tp9"; }   

   // windowFeatures
   windowFeatures = "directories=no";
   windowFeatures += ",location=no";
   windowFeatures += ",menubar=yes";
   windowFeatures += ",resizable=yes";
   windowFeatures += ",scrollbars=yes";
   windowFeatures += ",status=yes";
   windowFeatures += ",toolbar=no";
   windowFeatures += ",dependent=no";

   if ((tp == 6) || (tp == 12)) {
      windowFeatures = "directories=no";
      windowFeatures += ",location=no";
      windowFeatures += ",menubar=no";
      windowFeatures += ",resizable=yes";
      windowFeatures += ",scrollbars=yes";
      windowFeatures += ",status=yes";
      windowFeatures += ",toolbar=no";
      windowFeatures += ",dependent=no";
   }   
   else if (tp == 50) {
      windowFeatures = "directories=no";
      windowFeatures += ",location=yes";
      windowFeatures += ",menubar=yes";
      windowFeatures += ",resizable=yes";
      windowFeatures += ",scrollbars=yes";
      windowFeatures += ",status=yes";
      windowFeatures += ",toolbar=yes";
      windowFeatures += ",dependent=no";
   }   

   windowFeatures += ",height=" + String(h);
   windowFeatures += ",width=" + String(w);
   windowFeatures += ",left=" + String(x); 
   windowFeatures += ",top=" + String(y);
   windowFeatures += ",outerHeight=" + String(h);
   windowFeatures += ",outerWidth=" + String(w);
   windowFeatures += ",screenX=" + String(x); 
   windowFeatures += ",screenY=" + String(y);

   var popupWindow = window.open(url, target, windowFeatures);
   popupWindow.focus();

   return;
}

function insertLargeImage(id, imgWidth, imgHeight, popupWindowWidth, popupWindowHeight) {
   var sq = "\'";
   var dq = "\"";
   var strOutput = '';
   strOutput += "<P";
   strOutput += " align=" + dq + "left" + dq;
   strOutput += ">";
   strOutput += "<A href=" + dq + "javascript:openWindowSized(" + sq + "/assetDownload?id=" + id + sq + ", " + popupWindowWidth + ", " + popupWindowHeight + ")" + dq + ">";
   strOutput += "<IMG ";
   strOutput += " src=" + dq + "/assetDownload?id=" + id + dq;
   strOutput += " width=" + dq + imgWidth + dq;
   strOutput += " height=" + dq + imgHeight + dq;
   strOutput += " border=" + dq + "0" + dq;
   strOutput += ">";
   strOutput += "</A>";
   strOutput += "<BR>";
   strOutput += "<i>Klicka på bilden för att se förstoring.</i>";
   strOutput += "</P>";

   document.write(strOutput);
}

function insertThumbnail(assetIdThumbnail, assetIdLarge, popupWindowWidth, popupWindowHeight) {
   var sq = "\'";
   var dq = "\"";
   var strOutput = '';
   strOutput += "<P";
   strOutput += " align=" + dq + "left" + dq;
   strOutput += ">";
   strOutput += "<A href=" + dq + "javascript:openWindowSized(" + sq + "/assetDownload?id=" + assetIdLarge + sq + ", " + popupWindowWidth + ", " + popupWindowHeight + ")" + dq + ">";
   strOutput += "<IMG ";
   strOutput += " src=" + dq + "/assetDownload?id=" + assetIdThumbnail + dq;
   strOutput += " border=" + dq + "0" + dq;
   strOutput += ">";
   strOutput += "</A>";
   strOutput += "<BR>";
   strOutput += "<i>Klicka på bilden för att se förstoring.</i>";
   strOutput += "</P>";

   document.write(strOutput);
}




