﻿// JScript File
   // thanks http://www.ruzee.com/blog/2006/07/\
   // retrieving-css-styles-via-javascript/

function rzCC(s){
   for(var exp=/-([a-z])/; 
       exp.test(s); 
       s=s.replace(exp,RegExp.$1.toUpperCase()));
   return s;
}

 function _setStyle(element, declaration) {
   if (declaration.charAt(declaration.length-1)==';')
     declaration = declaration.slice(0, -1);
   var k, v;
   var splitted = declaration.split(';');
   for (var i=0, len=splitted.length; i<len; i++) {
      k = rzCC(splitted[i].split(':')[0]);
      v = splitted[i].split(':')[1];
      //eval("element.style."+k+"='"+v+"'");
      eval("element.style."+k+'="'+v+'"'); 
   }
}
 
function exclude_spaces(str){
   //Tar bort ev. space från en "style-sträng" som den nedan t.ex.
   //str="width:"+cell_width+"px;background-color:"+header_bgcolor[currenttemaid]+";color:"+header_fgcolor[currenttemaid]+";font-weight:bold;"
   var new_str=""
   var FromIndex=0
   var LastIndex=str.lastIndexOf(" ")
   if (LastIndex>-1)
   {
      while (FromIndex<str.length)
      {
         start=FromIndex
         stop=str.indexOf(" ",FromIndex)
         if (str.charAt((stop-1))==":" || str.charAt((stop-1))==";")
         {
            new_str+=str.substring(start,stop)
         }
         else
         {
            new_str+=str.substring(start,(stop+1))
         }
         
         FromIndex = stop+1
         if (FromIndex>=LastIndex)
         {
            if (FromIndex<str.length)
            {
               new_str+=str.substring(FromIndex,str.length)
            }
            FromIndex =str.length
         }
      }
   } else {
      new_str=str
   }   
   return new_str
}

function fixastylen(id,thisstyle){
    thisstyle=thisstyle.toLowerCase()
    thisstyle=exclude_spaces(thisstyle)
    var o=document.getElementById(id)
    _setStyle(o, thisstyle)
}     

function getstyle(id){
    o=document.getElementById(id).getAttribute("style")
    var thisstyle=o
    if (typeof o=='object') {
        thisstyle=o.cssText
    }
    thisstyle=thisstyle.toLowerCase()
    thisstyle=exclude_spaces(thisstyle)

    //Kollar om färger är angivna i rgb-format
    start=thisstyle.indexOf('rgb(')
    while (start>-1) {
        stop=thisstyle.indexOf(';',start)
        
        rgbcolor=thisstyle.substring(start+4,(stop-1))
        hexcolor=gethexcolor(rgbcolor)
        
        newstyle=thisstyle.substring(0,start)//del 1
        newstyle+=hexcolor//hexadecimal color
        newstyle+=thisstyle.substring(stop)//del 2
        
        thisstyle=newstyle //Viktig tilldelning
        start=thisstyle.indexOf('rgb(')
    }   
    return thisstyle
}   

function gethexcolor(rgbcolor){
    komma1=rgbcolor.indexOf(',')
    komma2=rgbcolor.lastIndexOf(',')
    
    redcolor=rgbcolor.substring(0,komma1)
    greencolor=rgbcolor.substring(komma1+1,komma2)
    bluecolor=rgbcolor.substring(komma2+1)
    
    hexredcolor=gethexadecimal(redcolor)
    hexgreencolor=gethexadecimal(greencolor)
    hexbluecolor=gethexadecimal(bluecolor)
    
    hexcolor='#'+hexredcolor+hexgreencolor+hexbluecolor

    return hexcolor
} 

function gethexadecimal(decimalnumber){
    var heltalsdelen=Math.floor(decimalnumber/16)
    var part1=gethex(heltalsdelen)
    
    var resten=decimalnumber-(heltalsdelen*16)
    var part2=gethex(resten)
    
    var svar=part1+part2
    return svar
}

function gethex(tal) {
    switch(tal) {
        case 0:
            return '0'
            break;    
        case 1:
            return '1'
            break;
        case 2:
            return '2'
            break;
        case 3:
            return '3'
            break;
        case 4:
            return '4'
            break;
        case 5:
            return '5'
            break;
        case 6:
            return '6'
            break;
        case 7:
            return '7'
            break;
        case 8:
            return '8'
            break;
        case 9:
            return '9'
            break;
        case 10:
            return 'a'
            break;
        case 11:
            return 'b'
            break;
        case 12:
            return 'c'
            break;
        case 13:
            return 'd'
            break;
        case 14:
            return 'e'
            break;
        case 15:
            return 'f'
            break;
    }    
}   
