// toggle the display state of the child menu group.
function menuclick(func,divno,url,rightcolumn)
{
     // update required blocks per menu click.
     quote();

     // if the right column flag is set show the column. if not hide it.
     var rc=document.getElementById('rightcolumn');
     if (rightcolumn)
          rc.style.display='inline';
     else
          rc.style.display='none';

     // if the item clicked is a menu check for collpase or expand.
     if (func=='m')
          {
          var object=document.getElementById('menuitems'+divno);
     
          // also need to call the server and tell it to set the state of the
          // display style in the session for this child group for when the
          // page is reloaded.
          if (object.style.display=='inline')
               {
               object.style.display='none';
               ret=serverFunction('modules.php?mod=ajax&req=setdisplay&val=none&div='+divno);
               }
          else
               {
               object.style.display='inline';
               ret=serverFunction('modules.php?mod=ajax&req=setdisplay&val=inline&div='+divno);     
               }
          }
     // check for content to be loaded
     if (func=='c')
          {
          content(url);
          }
     // check for URL, i.e. index.php or a module, to be called.
     if (func=='u')
          {
          if (url=='homepage')
               {
               displayText('maincontent','modules.php?mod=ajax&req=homepage');
               }
          else
               {
               // domScriptLoad(url);
               displayText('maincontent','modules.php?mod='+url);
               } 
          }
}

function domScriptLoad(nodename)
{
     URL='ajax/'+nodename+'.js';
     var d=document.getElementsByTagName("HEAD")[0];
     servercall=URL;
     var node = document.getElementById(nodename);
     if (node && navigator.userAgent.toLowerCase().indexOf("msie")<0) 
          {
          document.getElementsByTagName("HEAD")[0].removeChild(node);
          node=null;
          }
     if (node) 
          {
          node.setAttribute("src",URL);
          } 
     else 
          {
          node=document.createElement("script");
          node.setAttribute("id",nodename);
          node.setAttribute("type","text/javascript");
          node.setAttribute("src",URL);
          document.getElementsByTagName("HEAD")[0].appendChild(node);
          }
}

// *************************** BLOCK FUNCTIONS ***************************
// these functions display the blocks in their respective theme blocks.
// ***********************************************************************

// display the main menu in the menu span area.
function mainmenu()
{
     var url='modules.php?mod=ajax&req=menu';
     displayText('mainmenu',url);
}

// display the calendar in the calendar area.
function calendar(mm,yy)
{
     var url='modules.php?mod=ajax&req=calendar&mm='+mm+'&yy='+yy;
     displayText('calendar',url);
}

function quote()
{
     var url='modules.php?mod=ajax&req=quote';
     displayText('quoteblock',url);
}

// ************************ end block functions **************************


// puts page from templates/content/{page} into main content.
function content(page)
{
     var url='modules.php?mod=ajax&req=content&page='+page;
     displayText('maincontent',url);
}

// puts page from templates/{page} into main content.
function displaypage(page)
{
     var url='modules.php?mod=ajax&req=anypage&page='+page;
     displayText('maincontent',url);
}

// puts page into any div or span section.
function displaydiv(div,page)
{
     var url='modules.php?mod=ajax&req=anypage&page='+page;
     displayText(div,url);
}

function news(newsid)
{
     var url='modules.php?mod=ajax&req=news&newsid='+newsid;
     displayText('maincontent',url);
}

function download(docid)
{
     ret=serverFunction('modules.php?mod=ajax&req=download&doc='+docid);
     window.location.href=ret;
}


function displayText(id, url) {
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
 var element = document.getElementById(id);
 if (!element) {
  alert("Bad id " + id +
   "passed to clientSideInclude." +
   "You need a div or span element " +
   "with this id in your page.");
  return;
 }
  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    element.innerHTML = req.responseText;
  } else {
    element.innerHTML =
   "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }
}

function isEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}

function serverFunction(url)
{
	 var req = false;
	 // For Safari, Firefox, and other non-MS browsers
	 if (window.XMLHttpRequest) {
		  try {
			   req = new XMLHttpRequest();
		  } catch (e) {
			   req = false;
		  }
	 } else if (window.ActiveXObject) {
		  // For Internet Explorer on Windows
		  try {
			   req = new ActiveXObject("Msxml2.XMLHTTP");
		  } catch (e) {
			   try {
			   req = new ActiveXObject("Microsoft.XMLHTTP");
			   } catch (e) {
			   req = false;
			   }
		  }
	 }

	 if (req) {
	 // Synchronous request, wait till we have it all

	 req.open('GET', url, false);
	 req.send(null);
	 return(req.responseText);
	 } else {
	 alert("Sorry, your browser does not support " +
		  "XMLHTTPRequest objects. This page requires " +
		  "Internet Explorer 5 or better for Windows, " +
		  "or Firefox for any system, or Safari. Other " +
		  "compatible browsers may also exist.");
	 }
}

// set the number of days for a dropdown based on the days in the month field.
function setDays(field)
{
     var object=document.getElementById(field);
     var form=document.getElementById('eventform');
     
     // clear out the current options.
     object.options.length = 0;
     
     // get year and month from the fields.
     if (field=='startday')
          {
          var year=form.startyear.value;
          var month=form.startmonth.value;
          }
     else
          {
          var year=form.endyear.value;
          var month=form.endmonth.value;
          }

     var days=daysInMonth(month,year);
    
     for (x=1; x<days+1; x++)
          object.options[x-1] = new Option(x,x);
}

function daysInMonth(month,year) 
{
     return 32 - new Date(year, month, 32).getDate();
}

function logout()
{
     var url='modules.php?mod=user&req=logout';
     var ret=serverFunction(url);
     mainmenu();
     displaypage('homepage/welcome_guest.html');
     displaydiv('headerlinks','theme/logged_out_links.html');
     var element=document.getElementById('leftheaderlinks');
     element.innerHTML='';     
}	

function createCookie(name, value, days)
{
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}

function eraseCookie(name)
{
  createCookie(name, "", -1);
}
