var the_table = '';
var the_action = '';
var the_id = '';

var busy = false;

function do_action(passedArgsObj)
{
  if (busy) return;

  the_table = escape(do_action.arguments[0]);
  the_action = escape(do_action.arguments[1]);
  the_id = escape(do_action.arguments[2]);
  var _surl = do_action.arguments[3] ? do_action.arguments[3] : false;

  switch (the_table)
  {
    case 'categories':
      var toggler = 'toggle_w_children.php';
      break;
    case 'content':
      var toggler = 'toggle_top_bottom_w_children.php';
      break;
    default:
      var toggler = 'toggle.php';
      break;
  }
  switch (the_action)
  {
    case "del":
    case "del_w_children":
      sURL = "_xml/"+the_action+".php?id="+the_id+"&t="+the_table;
      break;

    case "active":
    case "banner":
    case "to_show_content":
    case "recommended":
    case "top":
    case "moderated":
    case "visible":
    case "visible_top":
    case "visible_bottom":
    case "check_before":
    case "sold":
    case "closed":																										// NM adding, what am i missing?
	case "accepted":
    
      sURL = "_xml/"+toggler+"?what="+the_action+"&id="+the_id+"&t="+the_table;
      break;

    case "down":
    case "up":
      sURL = "_xml/change_order.php?id="+the_id+"&dir="+the_action+"&t="+the_table;
      break;

    case "status":
      break;

    default:
      alert("unknown action to do: '"+the_action+"'");
      return;
  }

  if (_surl) sURL = _surl+"?id="+the_id+"&t="+the_table+"&dir="+the_action+"&what="+the_action;

  status_line.innerHTML = "requesting..";

  busy = true;
  diXMLRequest.get(sURL, do_action_response);
}

function do_action_response(xmlDoc)
{
  busy = false;

  var result_rs = xmlDoc.documentElement.getElementsByTagName("result");
  if (result_rs.length == 0) return null;

  if (the_action=="up" || the_action=="down")
  {
    var idz1 = [];
    var idz2 = [];
  }

  for (var i = 0; i < result_rs.length; i++)
  {
    var id = parseInt(result_rs[i].getAttribute("id"));
    var name = result_rs[i].getAttribute("name");
    var value = parseInt(result_rs[i].getAttribute("value"));
    var ok = parseInt(result_rs[i].getAttribute("ok"));
    var text = result_rs[i].getAttribute("text");

    if (ok == 1)
    {
      switch (name) //the_action
      {
        case "del":
        case "del_w_children":
          var _s = _ge("section"+id);
          var _s2 = _s.nextSibling;
          var _p = _s.parentNode;
          _p.removeChild(_s);
          _p.removeChild(_s2);

          status_line.innerHTML = "deleted";
          break;

        case "active":
        case "banner":
        case "recommended":
        case "to_show_content":
        case "top":
        case "moderated":
        case "visible":
        case "visible_top":
        case "visible_bottom":
        case "check_before":
        case "sold":
	    case "closed":
		case "accepted":        
		
          var btn_href = _ge("btn_href_"+name+id); //the_action
          var btn_img = _ge("btn_img_"+name+id); //the_action

          if (btn_href!=null && btn_img!=null)
          {
            if (value != 1) value = 0;

            btn_href.title = btn_ar["alt"][name][value];
            btn_img.src = btn_ar["src"][name][value];
          }
          else
          {
            //status_line.innerHTML = "error! no such elements '"+"btn_href_"+name+id+"' and '"+"btn_img_"+name+id+"'";
            //return;
          }

          status_line.innerHTML = "done";
          break;

        case "down":
        case "up":
          if (value != 0) eval("if (_ge('section"+id+"')) idz"+value+"[idz"+value+".length]=id;");
          break;

        case "status":
          break;

        default:
          alert("unknown action done: '"+name+"'");
          return;
      }
    }
    else
    {
      status_line.innerHTML = text ? text : "error";
      if (text) alert(text);
      return;
    }
  }

  if (name=="up" || name=="down")
  {
    change_rows_order(idz1, idz2);
  }
}

function change_rows_order(idz1, idz2)
{
  if (idz1.length != 0 && idz2.length != 0)
  {
    var old_idz = idz1.concat(idz2);
    var new_idz = idz2.concat(idz1);

    var top_elem = _ge("section"+idz1[0]);

    var p = top_elem.parentNode;

    for (var i = 0; i < new_idz.length; i++)
    {
      var victim = _ge("section"+new_idz[i]);
      var shim = victim.nextSibling;

      var tmp = victim.cloneNode(true);
      tmp.id += "_";
      p.insertBefore(tmp, top_elem);

      var tmp = shim.cloneNode(true);
      p.insertBefore(tmp, top_elem);
    }

    for (var i = 0; i < old_idz.length; i++)
    {
      var victim = _ge("section"+old_idz[i]);
      var shim = victim.nextSibling;

      p.removeChild(victim);
      p.removeChild(shim);

      var new_born = _ge("section"+old_idz[i]+"_");
      new_born.id = new_born.id.substr(0, new_born.id.length - 1);
    }

    blink_tr(old_idz);

    status_line.innerHTML = "order changed";
  }
  else
  {
    window.location.reload();

    status_line.innerHTML = "error! contact administator";
  }
}

var blink_timer;
var blink_ar = false;

function blink_tr(id_or_ar, timeout)
{
  if (blink_ar !== false) return false;

  if (typeof timeout == 'undefined' || !timeout)
    timeout = 400;

  if (typeof id_or_ar != 'object')
    id_or_ar = [id_or_ar];

  blink_ar = id_or_ar;

  for (var i = 0; i < id_or_ar.length; i++)
  {
    highlight_tr('section'+id_or_ar[i], highlight_moved_color);

    blink_timer = setTimeout('blink_tr_exe()', timeout);
  }
}

function blink_tr_exe()
{
  for (var i = 0; i < blink_ar.length; i++)
  {
    unhighlight_tr('section'+blink_ar[i]);
  }

  blink_ar = false;

  clearTimeout(blink_timer);
}

// highlight before delete
var old_colors_ar;
var highlighted_flags_ar = [];

function highlight_tr(id, color)
{
  if (highlighted_flags_ar[id]) unhighlight_tr(id);

  var _r = _ge(id);
  if (!_r) return;

  var _d = _r.firstChild;
  var i = 0;
  old_colors_ar = new Array();

  if (!_d.style) _d = _d.nextSibling;
  if (!_d.style) return;

  highlighted_flags_ar[id] = 1;

  do
  {
    if (_d.nodeName == '#text') _d = _d.nextSibling;
    if (!_d) break;

    old_colors_ar[i++] = _d.style.backgroundColor;
    _d.style.backgroundColor = color;
  } while (_d = _d.nextSibling);
}

function unhighlight_tr(id)
{
  var _r = _ge(id);
  if (!_r) return;

  var _d = _r.firstChild;
  var i = 0;

  if (!_d.style) _d = _d.nextSibling;
  if (!_d.style) return;

  highlighted_flags_ar[id] = 0;

  do
  {
    if (_d.nodeName == '#text') _d = _d.nextSibling;
    if (!_d) break;

    _d.style.backgroundColor = old_colors_ar[i++];
  } while (_d = _d.nextSibling);
}
//

function save_order_status(the_id)
{
  status_line.innerHTML = "requesting..";

  var st = _ge('status_'+the_id);
  if (!st) { alert('error!'); return false; }

  var sURL = "_xml/set_order_status.php?id="+the_id+"&status="+st.value;

  busy = true;
  diXMLRequest.get(sURL, save_order_status_response);
}

function save_order_status_response(xmlDoc)
{
  busy = false;

  var result_rs = xmlDoc.documentElement.getElementsByTagName("result");
  if (result_rs.length == 0) return null;

  var id = parseInt(result_rs[0].getAttribute("id"));
  var value = parseInt(result_rs[0].getAttribute("value"));
  var ok = parseInt(result_rs[0].getAttribute("ok"));

  if (ok == 1)
  {
    status_line.innerHTML = "order status successfully set";
  }
  else
  {
    status_line.innerHTML = "error";
  }

  do_action_response(xmlDoc);
}

function toggle_all_booking_details(flag)
{
  for (var i = 0; i < global_outer_ids_ar.length; i++)
  {
    var e = _ge('booking_plus_'+global_outer_ids_ar[i]);

    if (!e) continue;

    e.innerHTML = typeof flag != 'undefined' && flag ? '&ndash;' : '+';

    /*
    if (e.innerHTML == '+' || true)
    {
      e.innerHTML = '&ndash;';
    }
    else
    {
      e.innerHTML = '+';
    }
    */
  }

  for (var i = 0; i < global_inner_ids_ar.length; i++)
  {
    var e2 = _ge('section'+global_inner_ids_ar[i]);

    if (e2)
//      e2.style.display = e && e.innerHTML != '+' ? '' : 'none';
//      e2.style.display = '';
      e2.style.display = typeof flag != 'undefined' && flag ? '' : 'none';
  }
}

function toggle_booking_details(id, ids_ar) //, prefix
{
  var e = _ge('booking_plus_'+id);
  //if (typeof prefix == 'undefined' || !prefix || true) prefix = 'section';
  var prefix = 'section';

  if (e)
  {
    for (var i = 0; i < ids_ar.length; i++)
    {
      var e2 = _ge(prefix+ids_ar[i]);

      if (e2)
        e2.style.display = e.innerHTML == '+' ? '' : 'none';
    }

    if (e.innerHTML == '+')
    {
      e.innerHTML = '&ndash;';
    }
    else
    {
      e.innerHTML = '+';
    }
  }
}

function admin_form_cancel(table)
{
  if (confirm('All unsaved data will be lost. Are you sure?'))
    window.location.href = 'index.php?path='+table;
}

function comma_str_to_ar(s)
{
  var d = ',';

  if (s)
  {
    while (s.substr(0,1) == d)
      s = s.substr(1);
    while (s.substr(s.length - 1, 1) == d)
      s = s.substr(0,s.length - 1);
  }

  return s ? s.split(d) : [];
}

function lm_click(id)
{
  var e = _ge('lm_submenu['+id+']');
  id = id+''; // int2string

  if (typeof window.admin_visible_left_menu_ids == 'undefined')
    window.admin_visible_left_menu_ids = getCookie('admin_visible_left_menu_ids');

  var ar = comma_str_to_ar(window.admin_visible_left_menu_ids);

  if (e)
  {
    var flag = in_array(id, ar);

    e.style.display = !flag ? 'block' : 'none';

    var cookie_date = new Date();
    cookie_date.setFullYear(cookie_date.getFullYear() + 1);

    if (flag)
    {
      var x = ar.indexOf(id);
      if (x != -1) array_splice(ar, x, 1);
    }
    else
    {
      ar.push(id);
    }

    setCookie('admin_visible_left_menu_ids', ','+ar.join(',')+',', cookie_date, '/');

    window.admin_visible_left_menu_ids = ar.join(',');
  }
}

function mp_onchange(s)
{
  if (s.value != '0')
  {
    _ge('s_mp').style.display = '';
    _ge('a_mp').href = 'index.php?path=gallery&group_id='+s.value;
  }
  else
  {
    _ge('s_mp').style.display = 'none';
  }
}

function show_full_content(id, state)
{
  var e1 = _ge('cut_content['+id+']');
  var e2 = _ge('full_content['+id+']');

  e1.style.display = state ? 'none' : 'block';
  e2.style.display = state ? 'block' : 'none';

  return false;
}

function admin_onbeforeunload()
{
  if (typeof admin_form != 'undefined' && !admin_form.is_able_to_leave_page())
    return admin_form.get_cancel_message();

  return is_ie4up ? "" : null;
}

