function editProperty(property){

       var win = new Ext.Window({
          layout:'fit',
          width:700,
          height:470,
          closeAction: 'close',
          plain: true,
          id: 'my-id-'+ 56,
          title:'EMI.com - Edit Property ( same database )',
          html: "<iframe id='ifr' style='width:100%;height:100%;border:none;background-color:white' src='http://admin.elitemeetings.com/properties/propEdit.php?prop_id=" + property.id +"'></iframe>"
      });

    win.show(this);
}


function saveFlop(o)
{
      Ext.MessageBox.show({
         msg: 'Submitting your request, please wait...',
         progressText: 'Saving...',
         width:300,
         wait:true,
         waitConfig: {interval:600},
         icon:'ext-mb-download', //custom class in msg-box.html
         animEl: o
     });
}

function searchUsers(is_supplier){
  is_supplier = (is_supplier) ? 1 : 0 ;
  w=window.open("/user/search?is_supplier="+is_supplier,"userSearch","status=yes,height=600,width=700,resizable=0,toolbar=no,scrollbars=yes");
  w.focus();
}

function addUser(is_supplier){
  is_supplier = (is_supplier) ? 1 : 0 ; 
  w=window.open("/user/adduser?is_supplier="+is_supplier,"addUsers","status=yes,height=600,width=700,resizable=0,toolbar=no,scrollbars=yes");
  w.focus();
}

function removeUser(user_id){
  w=window.open("/planner/remove/user_id/"+user_id,"userSearch","status=yes,height=600,width=700,resizable=0,toolbar=no,scrollbars=yes");
  w.focus();
}

function removeSupplier(supplier_id){
  w=window.open("/supplier/remove/supplier_id/"+supplier_id,"userSearch","status=yes,height=600,width=700,resizable=0,toolbar=no,scrollbars=yes");
  w.focus();
}

function editUser(user){

       var win = new Ext.Window({
          layout:'fit',
          width:700,
          height:470,
          closeAction: 'close',
          plain: true,
          id: 'my-id-'+ 56,
          title:'EMI.com - Edit User ( same database )',
          html: "<iframe id='ifr' style='width:100%;height:100%;border:none;background-color:white' src='http://admin.elitemeetings.com/members/accountEdit.php?account_id=" + user.id +"'></iframe>"
      });

    win.show(this);
}


function addRow(new_user,table,show_id,aps_id,module)
{

  if(table=="buyer_table"){
    var user_type = 'buyer';
  } else {
    var user_type = 'supplier';
  }

  if(aps_id){
    var append = "/aps_id/"+aps_id ;
  } else {
    var append = "/show_id/"+show_id ;
  }

  var user_array = new Array();
  user_array = new_user.split('-');
  user_name = user_array[1];
  user_id = user_array[0];

  if(user_id == "alert")
  {
    alert(user_name);
    return false;
  }

  var newRow = document.getElementById(table).insertRow(-1);
  newRow.id = user_type+"_"+user_id;

  oCell = newRow.insertCell(0);
  oCell.style.padding = '1px';
  var onclick="if (confirm('Are you sure you want to remove "+user_type+"?')) { new Ajax.Request('/"+module+"/remove"+user_type+"/"+user_type+"_id/"+user_id+append+"', {asynchronous:true, evalScripts:true, onComplete:function(request, json){removeRow('"+user_type+"_',"+user_id+")}}); };; return false;"
  oCell.innerHTML = "[<a href=\"#\" onclick=\""+onclick+"\">X</a>]";

  var oCell = newRow.insertCell(1);
  oCell.innerHTML = user_name;
  oCell.style.padding = '1px';
  oCell.align = 'left';
}

function add1on1Row(new_user,table,module)
{

  if(table=="buyer_table"){
    var user_type = 'buyer';
  } else {
    var user_type = 'supplier';
  }

  var user_array = new Array();  user_array = new_user.split('-');
  user_name = user_array[1];
  user_id = user_array[0];

  if(user_id == "alert")
  {
    alert(user_name);
    return false;
  }

  var newRow = document.getElementById(table).insertRow(-1);
  newRow.id = user_type+"_"+user_id;

  oCell = newRow.insertCell(0);
  oCell.style.padding = '1px';
  var onclick="if (confirm('Are you sure you want to remove "+user_type+"?')) { new Ajax.Request('/"+module+"/remove1on1"+user_type+"/"+user_type+"_id/"+user_id+"', {asynchronous:true, evalScripts:true, onComplete:function(request, json){removeRow('"+user_type+"_',"+user_id+")}}); };; return false;"
  oCell.innerHTML = "[<a href=\"#\" onclick=\""+onclick+"\">X</a>]";

  var oCell = newRow.insertCell(1);
  oCell.innerHTML = user_name;
  oCell.style.padding = '1px';
  oCell.align = 'left';
  
  // Do ajax call to refresh the slots
  refreshSlots();
    
}

function refreshSlots() {

  if (document.getElementById('planner_id').value && document.getElementById('supplier_id').value)
   new Ajax.Updater('slots', '/aps/ajaxShowSlots', {asynchronous:true, evalScripts:false, parameters:Form.serialize(document.getElementById('sf_admin_edit_form'))});
}


function removeRow(prepend,id)
{
  var tr = document.getElementById(prepend + id);
  tr.parentNode.removeChild(tr);

  // Do ajax call to refresh the slots
  refreshSlots();
}

function drawPrimarySelect(obj,primary_user_id){

  document.getElementById('supplier_primary_user_id').options.length=0;
  var i = 0;

    for (key in obj) {
      var is_selected = (key==primary_user_id) ?true:false;
      document.getElementById('supplier_primary_user_id').options[i]=new Option(obj[key], key, false, is_selected);
      i++;
    }
}

function resetPrimaryUsers(){

  new Ajax.Request('/hotel/primaryusers?property_id='+document.getElementById('supplier_property_id').value,
    {
      method:'get',
      onSuccess: function(ajax){
        if (ajax.responseText!='no bueno'){
          drawPrimarySelect(ajax.responseJSON,document.getElementById('supplier_primary_user_id').value);
        } else {
          document.getElementById('supplier_primary_user_id').options.length=0;
          if(document.getElementById('supplier_property_id').value > 0)
            alert('Primary User Issue: No Users are associated to this property.');
        }
      },
      onFailure: function(){ alert('Primary User AJAX request encountered a Server Error...') }
    });

}


function addTag(tag,is_planner)
{

  if(tag=="dupe"){
    alert('This tag is already associated to the show. Please enter another.');
    return false;
  }
  
  if(is_planner)
  {
    table = 'planner';
  } else {
    table = 'supplier';
  }

  tag_table = table+'_tag_table';

  var tag_array = new Array();
  tag_array = tag.split('@-@');
  tag_name = tag_array[0];
  tag_show_id = tag_array[1];

  var newRow = document.getElementById(tag_table).insertRow(-1);
  newRow.id = "tag_"+tag_show_id;

  oCell = newRow.insertCell(0);
  oCell.style.padding = '1px';
  var onclick="if (confirm('Are you sure you want to remove tag?')) { new Ajax.Request('/show/removetag/tag_show_id/"+tag_show_id+"', {asynchronous:true, evalScripts:true, onComplete:function(request, json){removeRow('tag_',"+tag_show_id+")}}); };; return false;"
  oCell.innerHTML = "[<a href=\"#\" onclick=\""+onclick+"\">X</a>]";

  onclick="if (!(tag = prompt('New name', $$('#tag_" + tag_show_id + " span').first().textContent))) return false; new Ajax.Request(\'/show/edittag/tag_show_id/" + tag_show_id + "/is_planner/" + (is_planner ? '1' : '0') + "\', {asynchronous:true, evalScripts:true, parameters:\'new_tag=\' + tag, onComplete:function(req, data) { updateTag(req.responseText); }});; return false;";
  inner='<span>' + tag_name + '</span> (<a href="#" onclick="' + onclick + '">edit</a>)';

  var oCell = newRow.insertCell(1);
  oCell.innerHTML = inner;
  oCell.style.padding = '1px';
  oCell.align = 'left';
}

function updateTag(tag)
{
  if (tag == 'dupe') {
    alert('This tag already exists');
    return;
  }

  tag_array = tag.split('@-@');
  tag_name = tag_array[0];
  tag_show_id = tag_array[1];

  $$('#tag_' + tag_show_id + ' span').first().update(tag_name);
}

function setUserPopup(json)
{
  user = JSON.parse(json);
  setUser(user.user_id, user.first_name + ' ' + user.last_name + '<br/>' + user.email + '<br/>' + user.company);
}

