function getInternetExplorerVersion() {

    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer') {

        var ua = navigator.userAgent;

        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");

        if (re.exec(ua) != null)

            rv = parseFloat(RegExp.$1);

    }

    return rv;

}

function HandleResponse(xml,json) {
//  alert(xml.responseText);
  if (author_img = $('author_image_img')) {
    author_img.show();
  }
  if (json) {
    $H(json).each(function(v){
        var key = v[0];
        var value = v[1];
//        alert(key+' '+value);
        switch (key) {
          case 'location':
            if (value) {
              location.href = value;
            }
            break;
          case 'update-element':
            if (xml.reponseText) {
              var ele = $(value);
              ele.innerHTML = xml.responseText;
            }
            break;
          case 'update-select':
            var ele = $(value);
            var content = xml.responseText.evalJSON();
            var selected = ele.options[ele.selectedIndex].value;
            var select = ele.cloneNode(true);
            select.update();
            var i = 0;
//            ele.parentNode.appendChild(select);
            ele.parentNode.replaceChild(select,ele);
            content.each(function(option){
                if (option.value == selected) {
                  selIndex = i;
                }
                select[i] = new Option(option.text,option.value);
                i++;
              }
            )
            select.selectedIndex = selIndex;
            break;
          case 'update-element-json':
            var content = xml.responseText.evalJSON();
            if (content[value]) {
              $(value).update(content[value]);
            }
            break;
          case 'update-text':
            var content = xml.responseText.evalJSON();
            if (content[value]) {
              $(value).value = content[value];
            }
            break;
          case 'alert':
            alert(value);
            break;
        }
      }
    )
  }
}

function HandleUploadedImage(obj) {
  if (obj['error']) {
    alert(obj['error']);
    return false;
  } else {
    var thumb = obj['image']['thumb'];
    thumb = '<img src="/images/nordiska/tmp/thumb/'+thumb+'">';
    var thubm_h = $('tmp_image_holder');
    thubm_h.update(thumb);
    ImageUpdateDescription();
    $('tmp_image_big').value = obj['image']['big'];
  }
}

function ImageUpdateDescription() {
    $('tmp_image_description').value = $F('image_description');
}

function FormSave(frm) {
  var parms = $(frm).serialize(true);
  var ax = new Ajax.Request('/ajax.php?c='+CATEGORY_ID,{
      method:'post',
      parameters:parms,
      onComplete:HandleResponse
    }
  )
}

var AUTHOR_COUNT = null;
function PlayAddAuthor(play_id) {
  if (AUTHOR_COUNT == null) {
    AUTHOR_COUNT = CURRENT_AUTHOR_COUNT ? CURRENT_AUTHOR_COUNT : 0;
  }
  var role = $('play_add_role');
  var author = $('play_add_author');
  var output = $('playauthorlist');
  var holder = new Element('div');
  var input = new Element('input',
    {
      'type':'text',
      'readonly':'true',
      'value':author.options[author.selectedIndex].text
    }
  )
  var hidden = new Element('input',
    {
      'type':'hidden',
      'value':author.value
    }
  )
  var buttonImg = new Element('img',
    {
      'src':'/img/icons/delete.png'
    }
  )
  var button = new Element('button',
    {
      'class':'btn',
      'type':'button'
    }
  )
  button.appendChild(buttonImg);
  button.update(button.innerHTML+'&nbsp;');
  var new_role = holder.appendChild(role.cloneNode(true));
  new_role.name = 'play['+play_id+'][authors]['+AUTHOR_COUNT+'][author_role]';
  hidden.name = 'play['+play_id+'][authors]['+AUTHOR_COUNT+'][author]';
//  new_role.selectedIndex = role.selectedIndex;
  $(new_role).show();
  holder.appendChild(input);
  holder.appendChild(hidden);
  holder.appendChild(button);
  button.onclick = function() {
    PlayRemoveInputLine(this);
  }
  output.appendChild(holder);
  AUTHOR_COUNT++;
}

function PlayRemoveInputLine(ele) {
  if (confirm(LANG_CONFIRM_REMOVE)) {
    var ele = $(ele);
    var div = ele.up('div');
    div.parentNode.removeChild(div);
  }
}

function WindowBoxEdit(type,origSelect) {
  var origSelect = $(origSelect);
  var ax = new Ajax.Request('/ajax.php?c='+CATEGORY_ID,
    {
      method:'post',
      parameters:'&'+type+'=true&origvalue='+$F(origSelect),
      onComplete:function(xml,json) {
        WindowBox(xml.responseText);
      }
    }
  );
}

var openWindows = 0;
function WindowBox() {
  var html = arguments[0];
  pos = Prototype.Browser.IE ? 'absolute' : 'fixed';
  var box = new Element('div',
    {
      style:'position:'+pos+';width:400px;height:400px;background:white;border:1px solid black;top:50%;left:50%;margin-left:-200px;margin-top:-200px;padding:2px;',
      'class':'box'
    }
  )
  var header = new Element('div',
    {
      style:'height:16px;background:#f1f1f1;text-align:right;margin-bottom:2px;'
    }
  )
  var close = new Element('img',
    {
      src:'/img/icons/cross.png',
      style:'float:right;clear:right;cursor:pointer;'
    }
  )
  var content = new Element('div',
    {
      style:'overflow:auto;height:378px;'
    }
  );
  header.appendChild(close);
  close.onclick = function() {
    WindowBoxClose(box);
  }
  box.appendChild(header);
  box.appendChild(content);
  document.body.appendChild(box);
  content.update(html);
  openWindows++;
  WindowHideSelects();
}

function WindowBoxClose(box) {
  var box = $(box).hasClassName('box') ? $(box) : $(box).up('.box');
  box.parentNode.removeChild(box);
  openWindows--;
  WindowHideSelects()
}

function WindowHideSelects() {
  if (!Prototype.Browser.IE) return;
  if (openWindows > 0) {
    $('editSet').select('select').each(function(select){
        select.style.visibility = 'hidden';
      }
    );
  } else {
    $('editSet').select('select').each(function(select){
        select.style.visibility = 'visible';
      }
    );
  }
}

function CategoriesAdd(fname) {
  var ele = $(fname+'_edit_add');
  var holder = new Element('div');
  var output = $(fname+'_holder');
  var id = new Element('input',
    {
      type:'hidden',
      name:fname+'[id][]',
      value:'null'
    }
  )
  var voided = new Element('input',
    {
      type:'hidden',
      name:fname+'[voided][]',
      value:'null'
    }
  )
  var buttonImg = new Element('img',
    {
      'src':'/img/icons/delete.png'
    }
  )
  var button = new Element('button',
    {
      'class':'btn',
      'type':'button'
    }
  )
  button.appendChild(buttonImg);
  button.update(button.innerHTML+'&nbsp;');
  var name = ele.cloneNode(true);
  name.id = '';
  name.name = fname+'[name][]';
  holder.appendChild(id);
  voided = holder.appendChild(voided);
  holder.appendChild(name);
  holder.appendChild(button);
  button.onclick = function() {
    PlayDisableInputLine(this,voided);
  }
  ele.value = '';
  output.appendChild(holder);
}



function PlayDisableInputLine(ele,voided) {
  if (confirm(LANG_CONFIRM_REMOVE)) {
    var ele = $(ele);
    var voided = $(voided);
    var div = ele.up('div');
    div.hide();
    voided.value = '1';
  }
}

function AuthorRemoveImage() {
  if (confirm(LANG_CONFIRM_REMOVE)) {
    var ele = $('author_image');
    var h = $('author_image_img');
    ele.value = 0;
    h.hide();
  }
}