
// AJAX request timeout = 10 seconds
var req_timeout = 10000;

// pop open a new window containing url
function popup(url, width, height) {
  if (width == 0)
    width = 640;
  if (height == 0)
    height = 480;
  if (width > 760)
    width = 760;
  if (height > 550)
    height = 550;

  var parms = "fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=" + width + ",height=" + height;
  window.open(url, 'crm_popup', parms);
}

function submitForm_v1(theform, target) {
  var targetid = $(target).parentNode.id;
  pars = Form.serialize(theform);
  url = $(theform).action + '?' + pars;
  var form_dynConObj = new DHTMLSuite.dynamicContent();  // Creating object of class DHTMLSuite.dynamicContent
  form_dynConObj.enableCache = false;
  form_dynConObj.loadContent(targetid, url);
  paneSplitter.showContent(targetid);
  return false;
}

function submitForm(theform, target, action, newTab, title, label) {
  if (action == 'undefined') {
    return submitForm_v1(theform, target);
  }

  if (newTab) {
    var targetid = target;
    newCenterTab(targetid, title, label);
  }
  else {
    var targetid = $(target).parentNode.id;
  }
  var formObj = new DHTMLSuite.form({
    formRef: theform,
    action: action,
    responseEl: targetid }
  );

  formObj.submit();
  if (newTab)
    paneSplitter.showContent(targetid);
  return false;
}

function processResponse(target, value) {
  if(target == 'alert') {
    if(value!='ok')
      alert(value);
  }
  else {
    $(target).innerHTML = value;
  }
}

function updateField(url, field)
{
  value = $F(field);
  url += '&value=' + value;
  var staus = new Ajax.Request( url,
  {
    'timeout':req_timeout,
    'onTimeout':function(req){ alert('Error: The request to the server timed out!'); },
    'onSuccess':function(req) { processResponse('alert',req.responseText); }
  }
  );
}


function toggleVisibility(div) {
  if ($(div).style.display == 'none' || $(div).style.display == '')
    $(div).style.display = 'block';
  else
    $(div).style.display = 'none';
}

// This function creates a new empty tab as a form target
// not refreshable because the contents come from a post
// that we may not want submitted a second time
// Also not sure refresh would work for a form post.
function newCenterTab(id,title,tabTitle)
{
  var position = 'center';
  var inputArray = new Array();
  inputArray['id'] = id;
  inputArray['position'] = position;
  inputArray['contentUrl'] = '/idx/rs.php?rs_method=blank';
//  inputArray['contentUrl'] = false;
  inputArray['title'] = title;
  inputArray['tabTitle'] = tabTitle;
  inputArray['closable'] = true;
  inputArray['displayRefreshButton'] = false;

  paneSplitter.addContent(position, new DHTMLSuite.paneSplitterContentModel( inputArray ) );
}

// This function opens a new tab - called by the menu items
function openPage(position,id,contentUrl,title,tabTitle,closable)
{
  var inputArray = new Array();
  inputArray['id'] = id;
  inputArray['position'] = position;
  inputArray['contentUrl'] = contentUrl;
  inputArray['title'] = title;
  inputArray['tabTitle'] = tabTitle;
  inputArray['closable'] = closable;
  inputArray['displayRefreshButton'] = true;

  paneSplitter.addContent(position, new DHTMLSuite.paneSplitterContentModel( inputArray ));
  paneSplitter.showContent(id);
}

function openPage_nocache(position,id,contentUrl,title,tabTitle,closable)
{
  if ($(id))
  {
    var opnc_dynConObj = new DHTMLSuite.dynamicContent();  // Creating object of class DHTMLSuite.dynamicContent
    opnc_dynConObj.enableCache = false;
    opnc_dynConObj.loadContent(id, contentUrl);
    paneSplitter.showContent(id);
  }
  else
  {
    openPage(position,id,contentUrl,title,tabTitle,closable)
  }
}

function notImplemented() { alert('This function is not yet implemented.'); }

function updateWiki(wiki, what) {
  var wiki_dynConObj = new DHTMLSuite.dynamicContent();  // Creating object of class DHTMLSuite.dynamicContent
  wiki_dynConObj.enableCache = false;
  wiki_dynConObj.loadContent(what, '/wiki/rs.php?rs_method=page&p='+what+'&wiki_uid='+wiki);
  paneSplitter.showContent(what);
}

function showWikiPage(wiki, what, topic) {
  if ($(what))
    updateWiki(wiki, what);
  else
    openPage('center',what,'/wiki/rs.php?rs_method=page&p='+what+'&wiki_uid='+wiki,topic,topic);
}

function vtoggle(i, j)
{
  if ($(i).style.display == 'none'){
    $(i).style.display = ''
  }
  else{
    $(i).style.display = 'none'
  }

  if ($(j).style.display == 'none'){
    $(j).style.display = ''
  }
  else{
    $(j).style.display = 'none'
  }
}

// addLoadEvent() by Simon Wilson
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    var oldonload = window.onload;
    window.onload = function() {
      oldonload();
      func();
    };
  }
}

/**
Usage
  addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);

or

addLoadEvent(function() {
  // more code to run on page load
});
*/

// Schedule a function to execute as soon as an object is loaded
function schedule(objectID, functionCall)
{
  if ($(objectID)) {
    eval(functionCall);
  }
  else {
    setTimeout("schedule('" + objectID + "', '" + functionCall + "')", 10);
  }
  return true;
}

// ************** Application specific functions - these are specific to the idx application only
function show_idx_detail(mlsnumber)
{
  openPage_nocache('center','search_result_detail','/idx/rs.php?rs_method=detail&mlsnumber='+mlsnumber+'&PHPSESSID='+sessionid,'Listing Details #'+mlsnumber,'Details #'+mlsnumber,true)
}

function map_listing(mlsnumber)
{
  openPage_nocache('center','map_listing','/idx/rs.php?rs_method=map_listing&mlsnumber='+mlsnumber+'&PHPSESSID='+sessionid,'Listing Map: '+mlsnumber,'Map: '+mlsnumber,true)
}
