/*
 * Copyright 2006 Servelots Infotech Pvt. Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at:
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

/**
   This little Javascript reads the RSS feeds and displays them in a
   reasonably stylish form.
   Author : Srinivasa Raghavan & Surekha Sastry
 **/

/* The Symbolic Constants that represent the xmlHttpResponse state */
var READY_STATE_UNINITIALIZED = 0;
var READY_STATE_LOADING = 1;
var READY_STATE_LOADED = 2;
var READY_STATE_INTERACTIVE = 3;
var READY_STATE_COMPLETE = 4;

/* The xmlHttpRequest object/variable */
var xmlHttpRequest = null;


/** Returns the xmlHttpRequest object **/
function getXmlHttpRequest()
{
  var req = null;
  if (window.XMLHttpRequest)
    {
      req = new XMLHttpRequest();
    }
  else if (typeof ActiveXObject != "undefined")
    {
      req = new ActiveXObject("Microsoft.XMLHTTP");
    }
  return req;
}


/** This function calls the 'url' using the method passed and calls
    'readyStateChangeCallback' in response **/
function readContent(url, params, httpMethod)
{
  if (httpMethod == null)
  {
    httpMethod = "GET";
  }
  xmlHttpRequest = getXmlHttpRequest();
  if (xmlHttpRequest != null)
    {
      xmlHttpRequest.onreadystatechange = populateContent;
      xmlHttpRequest.open(httpMethod, url, true);
      xmlHttpRequest.setRequestHeader("Content-Type", "text/html;charset=UTF-8;");
      xmlHttpRequest.send(params);
    }
}


/** This function is called in response to the xmlHttpRequest called
    in readContent. This function checks if the RSS Feeds is
    configured and acts accordingly **/
function populateContent()
{
  var readyState = xmlHttpRequest.readyState;
  var data = null;
   if (readyState == READY_STATE_COMPLETE)
    {
      data = xmlHttpRequest.responseText;
      if (data != null)
      {
	tinyMCE.setContent(data);
      }
    }
 }

/** Returns the URL with which the current community is being accessed */
function getCustomPanelURL()
{
  var url = new String(document.location);
  var customPanelFile = document.CustomPanel.panel.value + ".wm";
  var currentDomainId = document.CustomPanel.urlid.value ; 
  url = url.substring(0, url.indexOf("servlet"));
  url += "domain/" + currentDomainId + "/" + customPanelFile;
  return url;
}

function hideEditor()
{
    document.getElementById("editorPanel").style.display = "none";
}


function toggleEditor(panel)
{

  document.CustomPanel.panel.value = panel;

  //  alert("inside toggleEditor");

  if (document.getElementById("edp").style.display == "inline")
  {
    document.getElementById("edp").style.display = "none";
  }
  else if (document.getElementById("edp").style.display == "none")
  {
    var panelURL = getCustomPanelURL();
    readContent(panelURL);
    document.getElementById("edp").style.display = "inline";
  }
  
  tinyMCE.execCommand('mceResetDesignMode');

}

function submit(formname)
{
  formname.submit();
}

function submitClose(formname)
{
  formname.submit();
  window.opener.parent.location.reload();
  window.close();
}


function closeChildWindow()
{
  window.close();
}

function show_login()
{
  var obj = document.getElementById("login");
  obj.style.display="block";
}

function hide_login()
{
  var obj = document.getElementById("login");
  obj.style.display="none";
}

function openInMain(o)
{
  document.getElementById("mainContent").innerHTML = "<Object data=" + o.href + " width='600px' height='2000px' type='text/html'>";
  return false;
}										
