if (console==undefined) {
  var console={};
  console.debug=function (txt) {alert(txt);};
  console.dir=function () {};
  console.log=function () {};
}

function sortlist(id) {
  var lb = document.getElementById(id)||id;
  arrTexts = new Array();
  arrValues = new Array();
  for(i=0; i<lb.length; i++)  {
    arrTexts[i] = lb.options[i].text;
    arrValues[i] = lb.options[i].value;
  }
  arrTexts.sort();
  for(i=0; i<lb.length; i++)  {
    lb.options[i].text = arrTexts[i];
    lb.options[i].value = arrValues[i];
  }
}
function filterlist(id, filter, list_id, hidden_list_id) {
  var lb = document.getElementById(lb)||id;
  arrTexts = new Array();
  arrValues = new Array();
  for(i=0; i<lb.length; i++)  {
    arrTexts[i] = lb.options[i].text;
    arrValues[i] = lb.options[i].value;
  }
  arrTexts.sort();
  for(i=0; i<lb.length; i++)  {
    lb.options[i].text = arrTexts[i];
    lb.options[i].value = arrValues[i];
  }
}

function move_option(from, to) {
  from=document.getElementById(from);
  to=document.getElementById(to);
  while (from.selectedIndex != -1) { 
    //arSelected.push(from.options[from.selectedIndex].value); 
    to.options.add(from[from.selectedIndex]);      
  }
  sortlist(from);
  sortlist(to);
}

function save_to_hidden(source_field, hidden_field) {
    current=document.getElementById(source_field);
    hidden_field=document.getElementById(hidden_field);
    if (hidden_field!=null&&hidden_field.value!=null&&current!=null&&current.options!=null) {
      data="";
      for (i=0;i<current.options.length;i++) {
        data=data+","+current.options[i].value;
      }
      hidden_field.value=data.slice(1);
    }
}

function stopEvent(e) {  
    if (!e) e = window.event;  
    if (e.stopPropagation) {  
        e.stopPropagation();  
    } else {  
        e.cancelBubble = true;  
    }
}  
function cancelEvent(e) {  
    if (!e) e = window.event;  
      if (e.preventDefault) {  
          e.preventDefault();  
      } else {  
          e.returnValue = false;  
      }  
 }  
function halt(e) {  
  stopEvent(e);
  cancelEvent(e);
}

function toggleSelect(select_button, selected_ids, button_name, more_options, more_options2, option_test, option_reset) {
  var objectItems=Form.getElements(selected_ids) ;
  var option2=true;
  if (option_reset!=undefined&&option_reset!=null) {
     option_reset();
  }
  if (option_test!=undefined&&option_test!=null) {
     option2=option_test();
  }
  if (objectItems != undefined) {
    if (select_button.value == 'Välj alla...') {
      if (objectItems.length!=undefined) {
        for (i = 0; i < objectItems.length; i++) {
          objectItems[i].checked = true ;
        }
      } else {
        objectItems.checked = true ;
      }
      select_button_top=$(button_name+'_top');
      if (select_button_top) { select_button_top.value = "Rensa"; }
      select_button_bottom=$(button_name+'_bottom');
      if (select_button_bottom) { select_button_bottom.value = "Rensa"; }
      if (more_options!=undefined) {
          if (option2) {
              $(more_options).show();
          } else {
              $(more_options).hide();
          }
      }
      if (more_options2!=undefined) {
          if (!option2) {
              $(more_options2).show();
          } else {
              $(more_options2).hide();
          }
      }
      return true;
    }
    else {
      if (objectItems.length!=undefined) {
        for (i = 0; i < objectItems.length; i++) {
          objectItems[i].checked = false ;
        }
      } else {
        objectItems.checked = false ;
      }              
      select_button_top=$(button_name+'_top');
      if (select_button_top) { select_button_top.value = "Välj alla..."; }
      select_button_bottom=$(button_name+'_bottom');
      if (select_button_bottom) { select_button_bottom.value = "Välj alla..."; }
      if (more_options!=undefined) {
          $(more_options).hide();
      }
      if (more_options2!=undefined) {
          $(more_options2).hide();
      }
      return false;       
    }
  }
}
   
function toggleSelectLink(element_id, selected_ids, on_code, off_code) {
  var objectItems=$$('input[id^="'+selected_ids+'"]'); //'#'+selected_ids
  if (objectItems != undefined) {
    cmdObj=$(element_id);
    if (cmdObj.innerHTML == on_code) {
      
      if (objectItems.length!=undefined) {
        for (i = 0; i < objectItems.length; i++) {
          objectItems[i].checked = true ;
        }
      } else {
        objectItems.checked = true ;
      }
      cmdObj.innerHTML = off_code;
      return true;
    }
    else {
      if (objectItems.length!=undefined) {
        for (i = 0; i < objectItems.length; i++) {
          objectItems[i].checked = false ;
        }
      } else {
        objectItems.checked = false ;
      }              
      cmdObj.innerHTML = on_code;
      return false;       
    }
  }
}


function toogleImage(img, first, next) {  
  if (img.src.endsWith(first)) {
    img.src=next;
  } else {
    img.src=first;
  }
}

function toogle_url_view(self, clip_id, url_row, url_row_table) {
    if (url_row==undefined) {
      var url_row='url_row_';
    }
    if (url_row_table==undefined) {
      var url_row_table='url_row_table';
    }
    var view=$(url_row+clip_id);
    if (view.visible()) {
      self.src='/themes/emunityedge/images/arrow-right.png';
      view.hide();
    }else {
      self.src='/themes/emunityedge/images/arrow-down.png';
      view.show();
      new Ajax.Updater(url_row_table+clip_id, '/products/mstreaming/view_ctrl.cfm', {parameters:{command:'load', clip_id:clip_id}});
    }
}


function check_mobile(self, land_code) {
  if (land_code==undefined) land_code='46';
  var value=self.value;
  if (value.substr(0,1)=='+' ) { 
      if (value.substr(1,2)!=land_code) {
          value=land_code+value.slice(1); 
      } else {
          value=value.slice(1); 
      }
  }
  if (value.substr(0,2)=='00') { value=land_code+value.slice(2); }
  if (value.substr(0,2)=='07') { value=land_code+value.slice(1); }
  if (value.substr(2,2)=='07') { value=value.slice(0,2)+value.slice(3); }
  if (value.length>1 && value.substr(0,2)!=land_code) { value=land_code+value; }
  self.value=value;
}

function charCounter(field, label, limit) {  
  if (limit==undefined) limit=480;
  removed="";
  field=$(field);
  label=$(label);
  data=field.value;
  data_len=data.length;
  if (data_len>limit) {
    field.value = field.value.substring(0, limit);
    removed=" (tecken har tagits bort i slutet av texten)";
  }
  label.innerHTML="Tecken kvar: "+(limit-field.value.length)+removed; 
}


var Stash = {controllers:[]}

Stash.MyStash = Class.create({
  initialize: function(id, options) {
    this.id=id;
    this.options=options || {in_stash:0,note:'',in_wishlist:0}
    this.save_timer=null;
    this.mystash_button=$('mystash_button'+id);
    this.mystash_button.onclick= this.toggleStash.bind(this);
    this.create_stash(id);
    Stash.controllers.push(this);
  },
  create_stash: function(id) {
    var mystash_form=document.createElement('div');
    document.body.appendChild(mystash_form);
    Element.extend(mystash_form);
    mystash_form.id='mystash_form'+id;
    //setStyle
    mystash_form.style.display='none';
    mystash_form.style.position='absolute';
    mystash_form.style.width='400px';
    mystash_form.style.height='160px';
    mystash_form.style.padding='10px';
    mystash_form.style.WebkitBoxSizing='border-box';
    mystash_form.style.WebkitBorderBottomRightRadius='5px';
    mystash_form.style.WebkitBorderBottomLeftRadius='5px';
    mystash_form.style.WebkitBorderTopLeftRadius='5px';
    mystash_form.style.WebkitBoxShadow='5px 5px 5px rgba(0, 0, 0, 0.5)';
    mystash_form.style.MozBoxSizing='border-box';
    mystash_form.style.MozBorderBottomRightRadius='5px';
    mystash_form.style.MozBorderBottomLeftRadius='5px';
    mystash_form.style.MozBorderTopLeftRadius='5px';
    mystash_form.style.MozBoxShadow='5px 5px 5px rgba(0, 0, 0, 0.5)';
    mystash_form.style.backgroundColor='#7ab21f';
    mystash_form.style.boxSizing='border-box';
    mystash_form.style.borderBottomRightRadius='5px';
    mystash_form.style.borderBottomLeftRadius='5px';
    mystash_form.style.borderTopLeftRadius='5px';
    mystash_form.style.boxShadow='5px 5px 5px rgba(0, 0, 0, 0.5)';
    mystash_form.style.backgroundImage="-webkit-gradient(linear, left top, left bottom, color-stop(0, #7ab21f), color-stop(1, #438832))";
    mystash_form.style.backgroundImage="-moz-linear-gradient(top, bottom, #7ab21f, #438832)";
    mystash_form.makePositioned();
    this.mystash_form=mystash_form;

    var label1=document.createElement('label');
    this.mystash_form.appendChild(label1);
    Element.extend(label1);
    label1.style.width="370px";
    label1.style.color="#FFFFFF";
    label1.style.padding="0px 10px 0px 2px";
    label1.innerHTML="Note:";

    var textarea=document.createElement('textarea');
    this.mystash_form.appendChild(textarea);
    Element.extend(textarea);
    textarea.style.width="378px";
    textarea.style.height="100px";
    textarea.value=this.options['note'];
    this.note=textarea;
    Event.observe(this.note, 'keyup', this.onDataChanged.bind(this)); 
    
    var label2=document.createElement('label');
    this.mystash_form.appendChild(label2);
    Element.extend(label2);
    label2.style.width="100px";
    label2.style.color="#FFFFFF";
    label2.style.padding="0px 10px 0px 2px";
    label2.innerHTML="In my stash:";

    var input=document.createElement('input');
    this.mystash_form.appendChild(input);
    Element.extend(input);
    input.style.width="30px";
    input.value=this.options['in_stash'];
    this.in_stash=input;
    Event.observe(this.in_stash, 'keyup', this.onDataChanged.bind(this)); 

    var add=document.createElement('input');
    this.mystash_form.appendChild(add);
    Element.extend(add);
//    add.style.width="18px";
    add.type="image";
    add.src="/img/33-circle-plus-18.png";
    add.width=18;
    add.height=18;
    add.align="top";
    add.style.margin="0px 0px 0px 5px";
    Event.observe(add, 'click', this.onAddOne.bind(this)); 

    //Event.observe(this.in_stash, 'keyup', this.onDataChanged.bind(this)); 
    var remove=document.createElement('input');
    this.mystash_form.appendChild(remove);
    Element.extend(remove);
//    remove.style.width="30px";
    remove.type="image";
    remove.src="/img/34-circle-minus-18.png";
    remove.width=18;
    remove.height=18;
    remove.align="top";
    remove.style.margin="0px 5px";
    Event.observe(remove, 'click', this.onRemoveOne.bind(this)); 

    var label3=document.createElement('span');
    this.mystash_form.appendChild(label3);
    Element.extend(label3);
    label3.style.color="#FFFFFF";
    label3.style.padding="0px 10px";
    label3.innerHTML="";
    this.flash=label3
  },  
  toggleStash: function () { 
    if (this.mystash_form.style.display=='none') {
        Stash.controllers.each(function (x) {x.close();}) 
        this.open();
    } else {
        this.close();
    }
  },
  close: function () { 
    this.mystash_form.hide();
    this.mystash_button.style.background='';
    this.mystash_button.style.WebkitBoxShadow='';
    this.mystash_button.style.MozBoxShadow='';
    this.mystash_button.style.boxShadow='';
  },
  open: function () { 
    this.mystash_form.show();
    this.mystash_button.style.background='#7ab21f url(/img/close-grey.png) no-repeat right top';
    this.mystash_button.style.WebkitBorderTopLeftRadius='5px';
    this.mystash_button.style.WebkitBorderTopRightRadius='5px';
    this.mystash_button.style.WebkitBoxShadow='5px 5px 5px rgba(0, 0, 0, 0.5)';
    this.mystash_button.style.MozBorderTopLeftRadius='5px';
    this.mystash_button.style.MozBorderTopRightRadius='5px';
    this.mystash_button.style.MozBoxShadow='5px 5px 5px rgba(0, 0, 0, 0.5)';
    this.mystash_button.style.borderTopLeftRadius='5px';
    this.mystash_button.style.borderTopRightRadius='5px';
    this.mystash_button.style.boxShadow='5px 5px 5px rgba(0, 0, 0, 0.5)';
    var layout_form = new Element.Layout(this.mystash_form);
    var layout_button = new Element.Layout(this.mystash_button);
    this.mystash_form.style.top=layout_button.get('top')+layout_button.get('height')+"px";
    this.mystash_form.style.left=layout_button.get('left')+layout_button.get('border-box-width')-layout_form.get('border-box-width')+"px";    
  },
  onDataChanged: function (event) { 
    this.flash.innerHTML='';
    this.in_stash.value = parseInt(this.in_stash.value);
    if (this.in_stash.value=='NaN') {
      this.in_stash.value=this.options['in_stash'];
    }
    var a=document.createElement('a');
    this.flash.appendChild(a);
    Element.extend(a);
    a.style.color="#FFFFFF";
    a.innerHTML="[Save Changes]";
    a.onclick= function () { this.save(); }.bind(this);
    if (this.save_timer!=null) {
        this.save_timer.stop();
        this.save_timer=null;
    }
    this.save_timer = new PeriodicalExecuter(this.save.bind(this), 5);
  },
  save: function (pe) { 
    if (this.save_timer!=null) {
        this.save_timer.stop();
        this.save_timer=null;
    }
    new Ajax.Updater('mystash_button'+this.id, '/mystash/savestash/'+this.id, {
      parameters: { note: this.note.value, in_stash:this.in_stash.value }
    });
    this.options['note']=this.note.value;
    this.options['in_stash']=this.in_stash.value;
    this.flash.innerText='Saved';
    new PeriodicalExecuter(function (pe) {pe.stop();this.flash.innerText='';}.bind(this), 5);
  },
  onAddOne: function (pe) { 
    value = parseInt(this.in_stash.value);
    if (value=='NaN') {
      value=this.options['in_stash'];
    }
    value+=1
    this.in_stash.value=value;
    this.save("onAddOne: "+value);
  },
  onRemoveOne: function (pe) { 
    value = parseInt(this.in_stash.value);
    if (value=='NaN') {
      value=this.options['in_stash'];
    }
    value=Math.max(value-1,0)
    this.in_stash.value=value;
    this.save("onRemoveOne");
  }  
});

Stash.Wishlist = Class.create({
  initialize: function(id, options) {
    this.id=id;
    this.options=options || {in_stash:0,note:'',in_wishlist:0}
    this.in_wishlist=this.options['in_wishlist'] || 0;
    this.wishlist_button=$('wishlist_button'+this.id);
    this.wishlist_button.onclick= this.toggleWishlist.bind(this);
  },
  toggleWishlist: function () { 
    if (this.in_wishlist==0) {
        new Ajax.Updater('wishlist_button'+this.id, '/mystash/addtowishlist/'+this.id, {
          parameters: { in_wishlist:this.in_wishlist.value }
        });
        this.in_wishlist=1;
    } else {
        new Ajax.Updater('wishlist_button'+this.id, '/mystash/removefromwishlist/'+this.id, {
          parameters: { in_wishlist:this.in_wishlist.value }
        });
        this.in_wishlist=0;
    }
  }
});

function get_selection(the_id)
{
    var e = document.getElementById(the_id);

    //Mozilla and DOM 3.0
    if('selectionStart' in e)
    {
        var l = e.selectionEnd - e.selectionStart;
        return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
    }
    //IE
    else if(document.selection)
    {
        e.focus();
        var r = document.selection.createRange();
        var tr = e.createTextRange();
        var tr2 = tr.duplicate();
        tr2.moveToBookmark(r.getBookmark());
        tr.setEndPoint('EndToStart',tr2);
        if (r == null || tr == null) return { start: e.value.length, end: e.value.length, length: 0, text: '' };
        var text_part = r.text.replace(/[\r\n]/g,'.'); //for some reason IE doesn't always count the \n and \r in the length
        var text_whole = e.value.replace(/[\r\n]/g,'.');
        var the_start = text_whole.indexOf(text_part,tr.text.length);
        return { start: the_start, end: the_start + text_part.length, length: text_part.length, text: r.text };
    }
    //Browser not supported
    else return { start: e.value.length, end: e.value.length, length: 0, text: '' };
}

function replace_selection(the_id,replace_str)
{
    var e = document.getElementById(the_id);
    selection = get_selection(the_id);
    var start_pos = selection.start;
    var end_pos = start_pos + replace_str.length;
    e.value = e.value.substr(0, start_pos) + replace_str + e.value.substr(selection.end, e.value.length);
    set_selection(the_id,start_pos,end_pos);
    return {start: start_pos, end: end_pos, length: replace_str.length, text: replace_str};
}

function set_selection(the_id,start_pos,end_pos)
{
    var e = document.getElementById(the_id);

    //Mozilla and DOM 3.0
    if('selectionStart' in e)
    {
        e.focus();
        e.selectionStart = start_pos;
        e.selectionEnd = end_pos;
    }
    //IE
    else if(document.selection)
    {
        e.focus();
        var tr = e.createTextRange();

        //Fix IE from counting the newline characters as two seperate characters
        var stop_it = start_pos;
        for (i=0; i < stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) start_pos = start_pos - .5;
        stop_it = end_pos;
        for (i=0; i < stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) end_pos = end_pos - .5;

        tr.moveEnd('textedit',-1);
        tr.moveStart('character',start_pos);
        tr.moveEnd('character',end_pos - start_pos);
        tr.select();
    }
    return get_selection(the_id);
}

function wrap_selection(the_id, left_str, right_str, sel_offset, sel_length)
{
    var the_sel_text = get_selection(the_id).text;
    var selection =  replace_selection(the_id, left_str + the_sel_text + right_str );
    if(sel_offset !== undefined && sel_length !== undefined) selection = set_selection(the_id, selection.start +  sel_offset, selection.start +  sel_offset + sel_length);
    else if(the_sel_text == '') selection = set_selection(the_id, selection.start + left_str.length, selection.start + left_str.length);
    return selection;
}
