var public = false;
var keepshowingagent = false;
var noclear = false;
var start_frame = "";
var show_agent_stuff = false;
var adjust_main_window = false;

function InitScheduleVar(_pub,_keep_showing_agent,_noclear,_start_frame,_show_agent_stuff)
{
  public = _pub;
  keepshowingagent = _keep_showing_agent;
  noclear = _noclear,
  start_frame = _start_frame;
  show_agent_stuff = _show_agent_stuff;
}

function ResizeIFrame()
{
  var cell = document.getElementById('rcell');

  // TODO:  Determine slack space based on browser
  var slack = 2;

  if (cell)
  {
    var rcell_height = cell.offsetHeight - slack;

    var iZone = document.getElementById('izone');
    if (iZone)
    {
      var content_height=
        document.getElementById('izone').contentWindow.
          document.body.scrollHeight;

      if (content_height < 400 && content_height > rcell_height)
      {
        //iZone.height = content_height;
      }
      else
      {
        iZone.height = rcell_height;
      }
    }
  }
}

function ClearForm(frmname,keepshowingagent,clearlisting)
{
  var description;
  var agentType;
  
  if ((document.getElementById) && (document.body.innerHTML))
  {
    frm = document.getElementById(frmname);
    if (clearlisting)
    {
      description = document.getElementById('listdesc');
      description.innerHTML = "Click 'Find Listing' button to find a listing."
      listingfound = 0;
      frm.listnum = "";
      frm.mls = "";
    }
    
    if (frm.displayrequester.value == true)
    {
      description = document.getElementById('userdesc');
      description.innerHTML = "Click 'Find User' button to find Showing User."
    }
    
    if (frm.displaybuyer.value == true)
    {
      description = document.getElementById('buyerdesc');
      description.innerHTML = "Click 'Find User' button to find Showing User."
    }
    frm.buyer_user_id.value = "";

    if (isContractor || keepshowingagent == false)
    {
      description = document.getElementById('showagentdesc');
      agentType = document.getElementById('agentType');
      try
      {
        description.innerHTML = "Click 'Find User' button to find Showing User."
        agentType.innerHTML = "Showing Agent";
      }
      catch (e)
      {
      }
      frm.showagent_user_id.value = "";
    }

    description = document.getElementById('timedesc');
    description.innerHTML = "Click 'Set Time' button to set showing date/time."

    frm.date.value = "";
    frm.start_time.value = "";
    frm.end_time.value = "";
    frm.comments.value = "";

    userfound = 0;
    timefound = 0;
    
    //if (start_frame == "")
    //  start_frame = "/showing/schedule/findlisting.asw"
    
    parent.izone.location = start_frame;
  }
 
}

function SelectThisUser(userid,name,username,email,phone,company,photo,agentcode,officecode,groupname,mlsdb,stayput)
{
  var url = "/showing/schedule/quickacct.asw?name=" + name;
  url += "&email=" + email;
  url += "&phone1=" + phone;
  url += "&company=" + escape(company);
  url += "&photo=" + photo;
  url += "&agent_code=" + agentcode;
  url += "&office_code=" + officecode;
  url += "&mlsdb=" + mlsdb;

  if (userid)
    url += "&uid=" + userid;
  if (userid == '')
   location = url;

  // we've only got the Requested By block
  // we've got Buyer and Agent blocks
  //   we just filled out the Buyer block
  //   we just filled out the Agent block
  var user_type = "";

  if (userid && (document.getElementById) && (document.body.innerHTML))
  {
    var description;

    // need to see if we're only using the showing_user_id
    // or the buyer_user_id && agent_user_id
    if (parent.document.showfrm.displayrequester.value == 1)
    {
      user_type = "requester"
      description = parent.document.getElementById('userdesc');
      parent.document.showfrm.showing_user_id.value = userid;
      parent.document.showfrm.showagent_user_id.value = "";
      parent.document.showfrm.buyer_user_id.value = "";
    }
    else
    {
      // anyone w/agent code
      // Contractors
      if (! description)
      {
        if (agentcode || groupname == "contractor")
        {
          user_type = "agent";
          parent.document.showfrm.showagent_user_id.value = userid;
          description = parent.document.getElementById('showagentdesc');
      
          if (groupname == "contractor")
          {
              groupname = "Contractor";
          }
          else
          {
            groupname = "Showing Agent";
          }
          agentType = parent.document.getElementById('agentType');
          agentType.innerHTML = groupname;
        }
        else
        {
          user_type = "buyer";
          parent.document.showfrm.buyer_user_id.value = userid;
          description = parent.document.getElementById('buyerdesc');
          groupname = "Buyer";
        }
      }
    }
    
    description.innerHTML = "";
    if (photo != "")
      description.innerHTML += "<img src='" + photo + "' align='left' hspace='3' width='70'><BR>";
    description.innerHTML += "<b>Name<\/b>: " + name + "<BR>";
    description.innerHTML += "<b>Username<\/b>: " + username + "<BR>";
    if (company != "")
      description.innerHTML += "<b>Company<\/b>: " + company + "<BR>";
    description.innerHTML += "<b>E-mail<\/b>: " + email + "<BR>";
    description.innerHTML += "<b>Phone<\/b>: " + phone + "<BR>";
    
    description.innerHTML += "<BR><input type='button' value='Account Change' onClick='GoTo(\""+ url +"\");'>";

    var urlAgentInfo = "/showing/schedule/findshowinguser.asw?check=" + userid;

    var urlTime = "/showing/schedule/setshowingtime.asw";
    urlTime += "?listnum=" + parent.document.showfrm.listnum.value;
    urlTime += "&mls=" + parent.document.showfrm.mls.value;

    if (stayput != 1)
    {
      if (user_type == "buyer")
      {
        location = urlAgentInfo;
      }
      else
      {
        location = urlTime;
      }
    }
  }
}

function HighLightSection(id)
{
  if ((document.getElementById) && (document.body.innerHTML))
  {
    var elm = new Array("listinginfo", "agentInfo", "buyerInfo", "showdate", "requesterInfo");
    
    for (i = 0; i < elm.length; i++)
    {
      var description;
      
      if (elm[i] == id)
      {
        description = parent.document.getElementById(elm[i]);
        description.style.background = maincolor3;
        description.style.color = "white"
      }
      else
      {
        check = 0
        description = parent.document.getElementById(elm[i]);
        if (description)
        {
          description.style.background = maincolor6;
          description.style.color = "black";
        }
      }
    }
    
    if (document.all)
    {
      var iTab = parent.document.getElementById('ifrm');
      if (iTab)
      {
        var iZone = parent.document.getElementById('izone');
        window.alert(iZone.id);
        iZone.height = iTab.offsetHeight + "px";
        iZone.style.height = iTab.offsetHeight + "px";
      }
    }
  }
}

function CheckValues()
{
  var msg = "";
  var frm = document.showfrm;

  if (frm.listnum.value == "" || frm.mls.value == "")
  {
    msg += "You can not leave the 'Listing Information' blank";
  }
  
  if (frm.buyer_user_id.value == "" && frm.showagent_user_id.value == ""
    && frm.showing_user_id.value == "")
  {
    if (msg)
      msg +="\n";
    msg += "You must select a Buyer and/or Showing Agent";
  }

  if (frm.start_time.value == "" || frm.end_time.value == "")
  {
    if (msg)
      msg +="\n";
    msg += "You can not leave the 'Showing Date/Time' blank";
  }

  if (msg != "")
  {
    window.alert(msg);
    return('false');
  }
  else
    document.showfrm.submit();
}

