/*
JavaScript functions File
*/

var jsajaxpath="includes/ajax/";
var jsprefixout="";

var mouseX=0; var mouseY=0, enabletip=false;
var ie=document.all
var ns6=document.getElementById && !document.all
function getmousepos(e)
{
   mouseX=(ns6)?e.pageX : event.x+ietruebody().scrollLeft;
   mouseY=(ns6)?e.pageY : event.y+ietruebody().scrollTop;
   if(enabletip) {positiontip(e); }
}

function ietruebody() { return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body }
document.onmousemove=getmousepos;



//function to put focus on a window.
function trywindowfocus(windowref)
  {
    try {windowref.focus();}
    catch(e) {setTimeout("trywindowfocus(" + windowref + ")",500);} //on error, wait a bit and try again.
  }

//----------------------------------------------------------------------------
//new window function pop-ups a new rowser window with the specified URL.
//An optional popup version can be specified for a particular popup behaviour
//----------------------------------------------------------------------------
function newwindow(url,version,name,width,height,norandom)
{
   if(!url)    { return false; }
   if(!version){ version = "standard"; }
   if(!width)  { width   = 760; }
   if(!height) { height  = 500; }
   if(!name)   { name    = "newwindow"; }
   var existingwindow = eval("window."+name);
   var size = "width="+width+",height="+height;
   switch(version)
   {
      case "standard": { var properties=size+",toolbar=yes,resizable=yes,scrollbars=yes"; break; }
      case "stripped": { var properties=size+",toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=yes,scrollbars=auto,copyhistory=no"; break; }
      case "semistripped": { var properties=size+",toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=yes,scrollbars=yes,copyhistory=no"; break; }
      case "nolocation": { var properties=size+",location=no"; break; }
      default: { properties=size+",toolbar=yes,scrollbars=yes"; }
   }
   var rndseed = ""+Math.random();
	if (norandom) { existingwindow = window.open(url,name,properties); }
  else { existingwindow = window.open(url+"&rnd="+rndseed,name,properties); }
   //existingwindow.focus();
  trywindowfocus(existingwindow)
}

//----------------------------------------------------------------------------
//togglelayer shows or hides an object based on the display property (not visibility)
//it will detect the kind element it is and therefore the cross-browser method of hiding or showing the object.
//Garry Harstad: Oct 20006: enhanced to allow passing of an SPF flag (assumes the divid+"_spf" iframe exists) and will toggle accordingly.
//               Also, added 4th parameter to position the elements in the middle of the screen (flag).
//----------------------------------------------------------------------------
function togglelayer(divid,override,spf,positionmiddle)
{ //alert(divid+"\n"+override)
   //alert("Display: "+divid+"\noverride:"+override);
   var myobj = document.getElementById(divid);
   if(!myobj) { window.status="[fn togglelayer] Problem! Could find element with ID '"+divid+"' to toggle."; return; }
   var mytagname = myobj.tagName
   //get the proper display style value, depending on element and browser
   //use self.innerHeight, which IE doesn't support, to determine block or table style
   switch (mytagname)
   {
      case 'DIV': { var  showstyle = "block"; break; }
      case 'SPAN': { var  showstyle = "inline"; break; }
      case 'I': { var  showstyle = "inline"; break; }
      case 'P': { var  showstyle = "block"; break; }
      case 'TABLE': { var  showstyle = self.innerHeight ? "table" : "block"; break; }
      case 'TR': { var  showstyle = self.innerHeight ? "table-row" : "block"; break; }
      case 'TD': { var  showstyle = self.innerHeight ? "table-cell" : "block"; break; }
      default: { var  showstyle = "block"; break; }
   }
   if(override == 1) { myobj.style.display = showstyle; if(spf==1) { blockburnthru(divid+"_spf",divid); } return;}
   if(override == 0) { myobj.style.display = "none"; if(spf==1) { blockburnthru(divid+"_spf",divid,true); } return;}
   if ((myobj.style.display == showstyle)||(myobj.style.display=="")||(myobj.style.display==null)) { myobj.style.display = "none"; if(spf==1) { blockburnthru(divid+"_spf",divid,true); } }
   else { myobj.style.display = showstyle; if(spf==1) { blockburnthru(divid+"_spf",divid); } }
}

//Pagination function
function paginate(formname,pagenum)
{
  var formobj = document.forms[formname];
  formobj.pagenum.value=""+pagenum;
  formobj.submit();
}

//Submits search form (form Object passed in, after attempting to set it's formfield [pagenum] to a value of "1" (new search means new page start)
function searchready(informobj)
{
  try
  {
    informobj.pagenum.value=1;
  }
  catch(e) {  /* do nothing different. */ }
  return true;
}

function evalJSON(responseText)
  {
    return eval('(' + responseText + ')');//convert JSON string to object
  }

  
//G.Harstad: make an XMLHttpRequest and return the string OR XML back to a defined function.
// Requires the URL, and the STRING name of the function that will handle the response text or XML.
//The return_xml paramtere is optional and if TRU will return the responseXML rather than the responseText of the http reponse.
function makeRequestcallback(url, callback_function, return_xml) 
{ 
   
   var http_request = false; 
   if (window.XMLHttpRequest)
   { // Mozilla, Safari,... 
      http_request = new XMLHttpRequest(); 
      if (http_request.overrideMimeType && return_xml) { http_request.overrideMimeType('text/xml'); } 
   }
   else if (window.ActiveXObject)
   { // IE 
      try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e)
      { 
         try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("No HTTP Request capability.");} 
      } 
   }
   if (!http_request)
   { 
      alert('Unfortunately you browser doesn\'t support this feature.'); 
      return false; 
   } 
   http_request.onreadystatechange = function()
   { 
       if (http_request.readyState == 4)
       { 
         if (http_request.status == 200)
         {
            if (typeof(callback_function) == 'string')
              {
                if (return_xml) { eval(callback_function + '(http_request.responseXML)'); }
                else { eval(callback_function + "(http_request.responseText)"); } 
              }
            else
              {
                if (return_xml) {callback_function(http_request.responseXML)}
                else {callback_function(http_request.responseText) }
              }
            
         }
         else
         {
            alert('There was a problem with the request.(Code: ' + http_request.status + ')');
            debugResponse(http_request.responseText);/*debug - open a window and dump response text into it */ 
         } 
       } 
   } 
   http_request.open('GET', url, true); 
   http_request.send(null); 
}
  
/*function to make an XMLHttpRequest. Must supply a URL (with variables, if necessary, and a reference to a function to handle the response.*/
function makeRequest(url,fnReference)
{
    http_request = false;
    if (window.XMLHttpRequest)
    { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();           
    }
    else if (window.ActiveXObject)
    { // IE
      try
      { http_request = new ActiveXObject("Msxml2.XMLHTTP"); }
       catch (e)
       {
         try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
         catch (e) {}
        }
    }
    if (!http_request)
    {
      //alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
    }
    http_request.onreadystatechange = fnReference;        
    http_request.open('GET', url, true);
    http_request.send(null);
}

 function debugResponse(responseText)
{
   try
   {
   var tmp = window.open();
   tmp.document.open();
   tmp.document.write(responseText);
   tmp.document.close();
   }
   catch(e)
   {
      document.write(responseText);
   }
}
  
//----------------------------------------------------------------------------
//objectMaker creates cross-browser/DOM reference to an object and returns the object as a handle to manipulating it.
//----------------------------------------------------------------------------
function objectMaker(textstring)
{
   var thisobj=false;
   try
   {
      if (document.getElementById) { var thisobj = document.getElementById(textstring); }
      else if (document.all) { var thisobj = document.all[textstring]; }
   }
   catch(e) { thisobj=false; }
   return thisobj;
}


//----------------------------------------------------------------------------
// Add and Remove Events
//----------------------------------------------------------------------------
function addEvent(elm, evType, fn, useCapture)
{
  if(typeof fn=="string") { fn=new Function(fn); }
  if (elm.addEventListener)
  {
    elm.addEventListener(evType,fn,useCapture);
    return true;
  }
  else if (elm.attachEvent)
  {
    var r = elm.attachEvent("on"+evType,fn);
    
    return r;
  }
  else { alert("[fn addEvent]\naddEventListener AND attachEvent not supported by Object Element.\nElement type="+elm); }
}

function removeEvent(elm, evType, fn,useCapture)
{
  if(typeof fn=="string") { fn=new Function(fn); }
  if (elm.addEventListener)
  {
    elm.removeEventListener(evType,fn,useCapture)
    return true;
  }
  else if (elm.attachEvent)
  {
    var r = elm.detachEvent("on" + evType, fn);
    return r;
  }
  else { alert("[fn removeEvent]\nremoveEventListener AND detachEvent not supported by Object Element.\nElement type="+elm); }
}



  
//------------------------------------------------------
//      Ajax: Content Zooming functions
//------------------------------------------------------
var globalpopulationcontainer="gen_contentzoompanel";
//Used to store default Mouse Offset values
var globalpopcontainer_xoffset=-20;
var globalpopcontainer_yoffset=10;
var gloabalcloselinkstyle="display:block; margin:5px; background:#EEE;";
//Used to store current Mouse Offset values
var thispopcontainer=globalpopulationcontainer;
var thispopcontainer_xoffset=globalpopcontainer_xoffset;
var thispopcontainer_yoffset=globalpopcontainer_yoffset;
var thismousex=0;
var thismousey=0;
var donotoffset_thispopcontainer=false;
var requesttimoutid="";
var globalmakerequestdelay=1000;
var makerequestdelay=1000; //millisecond delay before requesting data.
var thisshowfetcheddata=true;
var thiscloselinkstyle=gloabalcloselinkstyle;
//-------------------------------------------------------------
//Function to call a file with parameters from the Ajax folder
//and optionally specify the containing div that the HTML appears in and the X,Y offset pixels
//values from the current mouse position.
//You can also tell the function NOT to offset by passing true as the final parameter (useful if something else is positioning).
//requirements: fileqrystring (as it exists in the ajax folder.)
//-------------------------------------------------------------
function fetchinfo(fileqrystring,popcontainer,xoffset,yoffset,donotoffset,requestdelay,showfetcheddata,closelinkstyle)
{
  clearTimeout(requesttimoutid); //if entering this function, abandon any existing waiting HTTP request
  thismousex=mouseX; thismousey=mouseY;
  if(closelinkstyle==undefined) { thiscloselinkstyle=gloabalcloselinkstyle; } else { thiscloselinkstyle=closelinkstyle; }
  if(showfetcheddata==undefined) { thisshowfetcheddata=true; } else { thisshowfetcheddata=showfetcheddata; }
  if(popcontainer==undefined) { popcontainer=globalpopulationcontainer }
  popcontainerobj=objectMaker(popcontainer);
  if(popcontainerobj) { thispopcontainer=popcontainerobj; }
  if(requestdelay==undefined) { makerequestdelay=globalmakerequestdelay; } else { makerequestdelay=parseFloat(requestdelay); }
  if(donotoffset==undefined) { donotoffset_thispopcontainer=false; } else { donotoffset_thispopcontainer=donotoffset; }
  if(xoffset==undefined) { thispopcontainer_xoffset=globalpopcontainer_xoffset; } else { thispopcontainer_xoffset=parseInt(xoffset); }
  if(yoffset==undefined) { thispopcontainer_yoffset=globalpopcontainer_yoffset; } else { thispopcontainer_yoffset=parseInt(yoffset); }
  if(!popcontainerobj) { return; alert("[fn fetchinfo]\nCould not locate container to populate ["+popcontainer+"]\n in the Document Object Model.");  }
  var qryappendoperator="&";
  if(fileqrystring.indexOf("?")==-1) { qryappendoperator="?"; }
  //if the fileqrystring has a forward slash then treat it as a complete relative path otherwise assume the file is in the system's ajax folder
  var requeststring=jsajaxpath+fileqrystring+qryappendoperator+"rndseed="+Math.random();
  var filenamepart=fileqrystring.split(".")[0];
  var foundslash=filenamepart.indexOf("/");
  //alert("foundslash="+foundslash+"\nfilenamepart="+filenamepart)
  if(foundslash!=-1) { requeststring=jsprefixout+fileqrystring+qryappendoperator+"rndseed="+Math.random(); }
  var tempfn = function(){fetchrequest(requeststring)}
  requesttimoutid=setTimeout(tempfn,makerequestdelay);
}

function fetchrequest(requeststring)
{
   makeRequest(requeststring,popinfo);
   return;
}

function justfetchinfo(fileqrystring,returnfunctionstring)
{
   var filenamepart=fileqrystring.split(".")[0];
   var foundslash=filenamepart.indexOf("/");
   //alert("foundslash="+foundslash+"\nfilenamepart="+filenamepart)
   var requeststring=fileqrystring;
   if(foundslash==-1) { requeststring=jsajaxpath+fileqrystring; }
   //alert("requeststring="+requeststring);
   makeRequestcallback(requeststring,returnfunctionstring);
}

//Function that creates an SPF iframe for an element/layer to be displayed if it doesn't have one.
//Also calls the togglelayer function to turn on or off the div/layer and SPF frame.
//Garry Harstad: Oct 2006
function togglelayer2(containerid,onoroff,positioninthemiddle)
{
   var cobj=objectMaker(containerid); if(!cobj) { return; }
   var spfobj=objectMaker(containerid+"_spf");
   if(spfobj==null && document.all)
   {
      spfobj=document.createElement("iframe");      
      spfobj.name=containerid+"_spf";      
      spfobj.src=jsprefixout+"blank.html";
      spfobj.className = "spf_iframe"
      spfobj.id=containerid+"_spf";
      document.body.appendChild(spfobj);
   }
   togglelayer(containerid,onoroff,1);
   //alert(parseInt(onoroff))
   //if(parseInt(onoroff)==1) { blockburnthru(spfobj.id,containerid) }
   //else { blockburnthru(spfobj.id,containerid,1); }
   if (document.all){if(positioninthemiddle==true) { positionmiddle(cobj,onoroff,spfobj.id); }}
   else{if(positioninthemiddle==true) { positionmiddle(cobj,onoroff); }}
   
}


//----------------------------------------------------------------------------
// shadows a div invisibly with an IFRAME to prevent IE select box burn thru
//----------------------------------------------------------------------------
function blockburnthru(iframeid,divid,hide)
{
  
	//If this is not IE then abandon
	if (!document.all) { return; }
	var iframeobj = objectMaker(iframeid);
   //alert("[fn blockburnthru]\n"+iframeid+"\n"+divid+"\nhide="+hide+"\niframeobj="+iframeobj);
   if (iframeobj == null) {return} //return out of function if iframe screen doesn't exist (for example, on an inner iframe)
   if(hide != undefined) { iframeobj.style.left='-2000px';iframeobj.style.top='-2000px'; return; }
	var divobj = document.getElementById(divid);
  
   //If either	 the iframe or div are not objects then abandon
   if(!iframeobj || !divobj) { alert("SPF object or container object are missing."); return;  }
	//duplicate divobj's position and proportions to the iframe
  
  iframeobj.style.width = divobj.offsetWidth 
  iframeobj.style.height = divobj.offsetHeight 
  iframeobj.style.top = divobj.style.top
  iframeobj.style.left = divobj.style.left
  
  iframeobj.style.zIndex = divobj.style.zIndex -1;
  iframeobj.style.marginLeft=divobj.style.marginLeft;
  iframeobj.style.marginTop=divobj.style.marginTop;
  iframeobj.style.padding=divobj.style.padding;
  iframeobj.style.display = "block";
  /* Debug:
  var stylestring = ''; var styleObj = iframeobj.style
  for (var pr in styleObj)
    {
      if (styleObj[pr]) {stylestring = stylestring + pr + ': ' + styleObj[pr] + '\n';}
    }
  alert(stylestring)*/
}

//----------------------------------------------------------------------------
// Returns the width of an object
//----------------------------------------------------------------------------
function getobjectwidth(objid) { return document.getElementById(objName).offsetWidth; }

//----------------------------------------------------------------------------
// Returns the X (left) position of an object
//----------------------------------------------------------------------------
function getxpos(objid)
{
  if (typeof objid == "string"){myobj=document.getElementById(objid)}
  else{myobj = objid}
	var posleft = 0;
	if (myobj.offsetParent)
   {
	   while (myobj.offsetParent)
      {
		   posleft += myobj.offsetLeft;
			myobj = myobj.offsetParent;
		}
	}
	else if (myobj.x) { posleft += myobj.x; }
	return posleft;
}

//----------------------------------------------------------------------------
// Returns the Y (top) position of an object
//----------------------------------------------------------------------------
function getypos(objid)
{
  if (typeof objid == "string"){myobj=document.getElementById(objid)}
  else{myobj = objid}
	var postop = 0;
	if (myobj.offsetParent)
   {
	   while (myobj.offsetParent)
      {
         //alert(myobj.offsetTop)
		   postop += myobj.offsetTop;
			myobj = myobj.offsetParent;
		}
	}
  else if (myobj.y) { postop += myobj.y; } 
	return postop;
}


//returns inner dimensional width of a window frame or iframe
function getinnerwidth(objid)
{
   var obj=document.getElementById(objid);
   innerwidth=(document.documentElement) ? document.documentElement.clientWidth : window.innerWidth;
   return innerwidth;
}

//returns inner dimensional height of a window frame or iframe
function getinnerheight(objid)
{
   var obj=document.getElementById(objid);
   innerheight = (document.documentElement) ? document.documentElement.clientHeight : window.innerHeight;
   return innerheight;
}

var ifheight=(document.documentElement) ? document.documentElement.clientHeight : window.innerHeight;

//positions an element in the middle (vertical and horizontal) of the page
function positionmiddle(obj,onoroff,spfid)
{
   if(typeof onoroff=="undefined") { onoroff==1; }
   if(typeof obj=="string") { var obj=document.getElementById(obj); }
   if(typeof spfid!="string") { var spfid="spf_general"; }
   obj.style.visibility="hidden";
   obj.style.left="50%";
   obj.style.top="50%";
   
   objwidth=obj.offsetWidth;
   objheight=obj.offsetHeight;
   //obj.style.top=mouseY-(objheight/2);
   //if(!parseInt(objwidth) || objwidth<2) { objwidth=2; }
   var scrolltopoffset=getscrolloffset("y");
   var scrollleftoffset=getscrolloffset("x");
   //alert("Top Scroll Offset = "+scrolltopoffset+"\nLeft Scroll Offset = "+scrollleftoffset);
   obj.style.marginLeft=parseInt(scrollleftoffset)-(objwidth/2)+"px";
   obj.style.marginTop=parseInt(scrolltopoffset)-(objheight/2)+"px";
   /*var debuginfo="objwidth="+objwidth+"     objheight="+objheight;
   debuginfo+="\nmarginLeft="+obj.style.marginLeft+"   marginTop="+obj.style.marginTop;
   alert(debuginfo)*/
   obj.style.visibility="visible";
   if(onoroff==1) { blockburnthru(spfid,obj.id); } else { blockburnthru(spfid,obj.id,true); }
}

//Two function that set the height / width of a passed in object to the amount needed to display its contents wihtout scroll bars (eliminates scroll bars)
function setfullheight(obj,inc,minimum,maximum)
{
   //alert(obj.id + '\n' + inc  + '\n' + minimum  + '\n' + maximum)
   var currentval=parseInt(getcontentsize("y"));
   if(typeof inc == "undefined") { inc=0; }
   if(typeof minimum == "undefined") { minimum=0; }
   var targetHeight = currentval+inc;
   if(targetHeight < minimum) { targetHeight = minimum; }
   if(typeof maximum == "undefined") { maximum = targetHeight; }
   if (targetHeight > maximum){ targetHeight = maximum; }
   obj.style.height = targetHeight + "px";
}
function setfullwidth(obj,inc,minimum,maximum)
{
   //alert(obj.id + '\n' + inc  + '\n' + minimum  + '\n' + maximum)
   var currentval=parseInt(getcontentsize("x"));
   if(typeof inc == "undefined") { inc=0; }
   if(typeof minimum == "undefined") { minimum=0; }
   var targetWidth = currentval+inc;
   if(targetWidth < minimum) { targetWidth = minimum; }
   if(typeof maximum == "undefined") { maximum = targetWidth; }
   if (targetWidth > maximum){ targetWidth = maximum; }
   obj.style.width = targetWidth + "px";
}

//detects how far down you have scrolled within a viewport.
function getscrolloffset(xory)
{
   if(typeof xory=="undefined") { var xory="y"; }
   var x=0
   var y=0;
   if (self.pageYOffset)//NOT IE
   {
   	x = self.pageXOffset;
   	y = self.pageYOffset;
   }
   else if (document.documentElement && document.documentElement.scrollTop != "undefined")//IE 6
   {
   	x = document.documentElement.scrollLeft;
   	y = document.documentElement.scrollTop;
   }
   else if (document.body)//Older IE versions
   {
   	x = document.body.scrollLeft;
   	y = document.body.scrollTop;
   }
   if (xory.toLowerCase()=="x") { return x; }
   else { return y; }
}

//detects how tall the content within a viewport would be without scrollbars.
function getcontentsize(xory)
{
   if(typeof xory=="undefined") { var xory="y"; }
   var x=0
   var y=0;
   if (self.pageYOffset)//IE 
   { 
      x = document.body.scrollWidth;
      y = document.body.scrollHeight;
      //alert('Self = ' + self.name + '\nUsing document.body.scrollHeight/width\nx = ' + x + '\ny = ' + y)
   }
   else
   {
      //IE 6 Strict, Mozilla and Safari
      x = document.body.offsetWidth;
    	y = document.body.offsetHeight;
      if (!document.all){ y = y + 20;}//Mozilla shim
      //alert('Self = ' + self.name + '\nUsing document.body.offsetHeight/width\nx = ' + x + '\ny = ' + y)
   }
   //Add a little size to each
   x+=10;
   y+=10;
   if (xory.toLowerCase()=="x") { return x; }
   else { return y; }
}


function printpage()
{
  try
  {
    window.print();
  }
  catch(e)
  {
    alert("Sorry, your browser doesn't support this print button. Choose Print from your browser's File menu.");
  }
}

//-------------------------------------------------------------
//Function to populate the currently set ajax content div,
//and position and display it
//-------------------------------------------------------------
var globalreturndata="";
function popinfo()
{
   if (http_request.readyState == 4)
    {
      if (http_request.status == 200)
      {
        //debugResponse(http_request.responseText)/*debug - open a window and dump response text into it */         
        var returnstring = http_request.responseText;
        returnstring+="<a href='Javascript:/**/' onclick=\"togglelayer2(this.parentNode.id,0)\" style=\""+thiscloselinkstyle+"\"><b>x close</b></a>";
        thispopcontainer.innerHTML=returnstring;
        if(thisshowfetcheddata==true || thisshowfetcheddata==1)
        {
           if(!donotoffset_thispopcontainer)
           {
             thispopcontainer.style.top=(thismousey+thispopcontainer_yoffset)+"px";
             thispopcontainer.style.left=(thismousex+thispopcontainer_xoffset)+"px";
             thispopcontainer.style.margin="0";
             togglelayer2(thispopcontainer.id,1);
           }
           else
           { //for now assume to position in the middle of the page if [donotoffset_thispopcontainer] is true.
              togglelayer2(thispopcontainer.id,1,true);
           }
        }
        else
        {
           //maybe do something with a globalreturndata variable?
        }
      }
      else
      { //non 200 code
         alert('There was a problem with the request. Http Code:'+http_request.status);
         debugResponse(http_request.responseText);/*debug - open a window and dump response text into it */ 
      }
    }
}


function viewinterred(iid)
{
  //fetchinfo("includes/ajax/interred_view_ajx.cfm?isajax=1&interredid="+iid,"gen_contentzoompanel",-570,-100,false,0,1,"display:none");
  fetchinfo("includes/ajax/interred_view_ajx.cfm?isajax=1&interredid="+iid,"gen_contentzoompanel",-570,-100,true,0,1,"display:none");
  //positionmiddle("gen_contentzoompanel");
}
//fetchinfo(fileqrystring,popcontainer,xoffset,yoffset,donotoffset,requestdelay,showfetcheddata,closelinkstyle)
  
  
function debugResponse(responseText)
{
   try
   {
   var tmp = window.open();
   tmp.document.open();
   tmp.document.write(responseText);
   tmp.document.close();
   }
   catch(e)
   {
      document.write(responseText);
   }
}


function evalJSON(responseText)
{
  return eval('(' + responseText + ')');//convert JSON string to object
}



function randomnumber(maxnum)
{
  if(!parseInt(maxnum)) { maxnum=6; } //default to a total of 6
  maxnum--; //we are going to add 1 after the random result because JS counts from zero. So the minimum is always 1.
  var randomnumresult=Math.round(Math.random()*maxnum)+1;
  return randomnumresult;
}



var slidepositionlist="";
var slidearea="slideshow";
var slidepos="0";
var slideshowon=1;
var slidedelay=20000;
function initslideshow(positionlist)
{
  slidepositionlist=positionlist.split("|");
  //alert(slidepositionlist)
  var area="slideshow";
  var position="1";
  getrandomslide();
  //fetchinfo("content_ajx.cfm?area="+area+"&position="+position,"slideshowdiv",0,0,0,0,1,"display:none;");
  //fetchinfo(fileqrystring,popcontainer,xoffset,yoffset,donotoffset,requestdelay,showfetcheddata,closelinkstyle)
}

function turnoffslideshow()
{
  try
  {
    document.getElementById("playslideshow").checked=false;
  }
  catch(e) { /*do nothing*/ }
  stoprandomslideshow();
}

function getrandomslide()
{
  var slidelen=slidepositionlist.length;
  //alert("slidelen="+slidelen)
  var rndnum=randomnumber(slidelen);
  //alert("rndnum="+rndnum)
  var thispositionname=slidepositionlist[rndnum-1];
  //alert("thispositionname="+thispositionname)
  //fetchinfo("content_ajx.cfm?area="+slidearea+"&position="+thispositionname,"slideshowdiv",0,0,0,0,1,"display:none;");
  //justfetchinfo("content_ajx.cfm?area="+slidearea+"&position="+thispositionname,"setslide");
  getslide(thispositionname)
}

var rndslide=null;
var rndslidecountdown=null;
var slidecounter=null;

function randomslideshow()
{
  getrandomslide(); slidecounter=countdown(true)
  rndslide=setInterval("getrandomslide(); slidecounter=countdown(true)",slidedelay);
  rndslidecountdown=setInterval("countdown()",1000);
  setTimeout("turnoffslideshow()",300000);
}

function countdown(start)
{
  var pixelsecondmultiplier=5;
  var secsdelay=parseInt(slidedelay)/1000;
  var cdobj=document.getElementById("slidecountdown");
  //var currentval=parseInt(cdobj.innerHTML);
  //alert(1)
  var currentval=parseInt(cdobj.style.width);
  //alert(2)
  if(start==true) { clearTimeout(slidecounter); currentval=secsdelay * pixelsecondmultiplier; }
  else
  {
    if(currentval>=pixelsecondmultiplier) { currentval-=pixelsecondmultiplier;}
  }
  //cdobj.innerHTML=currentval;
  //cdobj.style.width=(currentval*pixelsecondmultiplier)+"px";
  //alert("cdobj.style.width="+cdobj.style.width+"\ncurrentval="+currentval)
  cdobj.style.width=currentval+"px";
  //alert("currentval="+currentval)
}

function stoprandomslideshow()
{
  clearInterval(rndslide);
  clearInterval(rndslidecountdown);
  clearInterval(slidecounter);
  rndslide=null;
  rndslidecountdown=null;
  slidecounter=null;
}

function getslide(thispositionname)
{
    slidepos=thispositionname;
    justfetchinfo("content_ajx.cfm?area="+slidearea+"&position="+thispositionname,"setslide");
}

function setslide(txt)
{
  var linkhtml="";
  var activelinkstyle="";
  if(slidepositionlist.length > 1)
  {
    
    for(var i=0; i<slidepositionlist.length; i++)
    {
      activelinkstyle="";
      if(slidepositionlist[i]==slidepos) { activelinkstyle="background-color:white;  border-style:inset; color:black;"; }
      linkhtml+='<a href="JavaScript:getslide(\''+ slidepositionlist[i] +'\')" style="padding:1px; width:22px;'+activelinkstyle+'">' + (i+1) + '</a> '
    }
  }
  document.getElementById("slideshowlinks").innerHTML=linkhtml;
  document.getElementById("slideshowdiv").innerHTML=txt;
}


 
  
  //----------------- IMAGE MAP FUNCTIONS ---------------------
   function addplotevents()
   {
      var areaelements=document.getElementsByTagName("area");
      var areaelementsLen=areaelements.length;
      var i=0;
      var thisareaObj=""; //current area element reference.
      var tmp=""; // temporary variable
      var plotnum="";
      var idprefix="";
      
      //alert(areaelementsLen)
      for(i=0; i<areaelementsLen; i++)
      {
         thisareaObj=areaelements[i];
         if(!thisareaObj) { return; }
         temp=thisareaObj.id.split("_");
         idprefix=temp[0];
         if(idprefix=="plot")
         {
            plotnum=temp[3];
            thisareaObj.title="Plot "+plotnum;
            thisareaObj.onclick=function() { getplotinterred(this.id.split("_")[1],this.id.split("_")[2],this.id.split("_")[3]); return false; }
            thisareaObj.onmouseover=function() { getplotimages(this.id.split("_")[1],this.id.split("_")[2],this.id.split("_")[3]); return false; }
            thisareaObj.onmouseout=function() { togglelayer2('plotimg_contentzoompanel',0); }
         }
      }
   }

   function showintdiv(intid)
   {
      var tablecollect=document.getElementsByTagName("table");
      var tblcollLen=tablecollect.length;
      var thistblObj="";
      var thistblid="";
      for(var i=0;i<tblcollLen; i++)
      {
         thistblObj=tablecollect[i];
         thistblid=thistblObj.id;
         if(thistblid.split("_")[0]=="interredviewtable")
         {
            if(thistblid != "interredviewtable_"+intid)
            {
               togglelayer(thistblid,0);
            }
            else
            {
               togglelayer(thistblid,1);
            }
         }
      }
   }
   
   
   function toggleinterredimages()
   {
      var thisimgObj="";
      var thisimgid="";
      var togglestatebit="";
      //NOTE: we want to toggle ALL to one state (on or off) so use the state of the toggled FIRST image.
      for(var i=1;i<=4; i++)
      {
         thisimgid="previewimg"+i;
         thisimgObj=document.getElementById(thisimgid);
         if(thisimgObj)
         {
            if(togglestatebit=="")
            {
               togglestatebit = thisimgObj.style.display=="none" ? 1 : 0;
            }
            togglelayer(thisimgid,togglestatebit);
         }
      }
   }
   
   
   
   
   var cemeteryid=0; //should be set on-Page-Load with actual cemetery id.
   //function getplotinterred(plotnum)
   //{
   //   alert("Plot Number:. "+plotnum)
   //}
   
   function getplotinterred(cemid,cemsection,plot)
   {
      //alert("cemid="+cemid+"\ncemsection="+cemsection+"\nplot="+plot)
      var cemeteryidtouse=cemeteryid;
      if(cemid == undefined) {  cemeteryidtouse=cemid; }
      if(cemsection == undefined) {  cemsection="SecA"; }
      //var clickObj=document.getElementById("plot_"+plot);
      //clickObj.style.border="1px solid red";
      //clickObj.style.borderColor="red"
      //alert("clickObj="+clickObj)
      fetchinfo("includes/ajax/interredplot_view_ajx.cfm?closelinkcolor=brown&popup=1&cemeteryid="+cemeteryidtouse+"&cemeterysection="+cemsection+"&plot="+plot,"gen_contentzoompanel",-200,-320,false,0,1,"display:none");
   }
   
   
   function getplotimages(cemid,cemsection,plot)
   {
      if(cemsection == undefined) {  cemsection="SecA"; }
      var cemeteryidtouse=cemeteryid;
      if(cemid != undefined) {  cemeteryidtouse=cemid; }
      var newurl="includes/ajax/plotimage_view_ajx.cfm?closelinkcolor=brown&popup=1&cemeteryid="+cemeteryidtouse+"&plot="+plot+"&cemeterysection="+cemsection+"&rndid="+Math.random();
      //alert(newurl)
      //window.open(newurl);
      fetchinfo(newurl,"plotimg_contentzoompanel",30,-180,false,200,1,"display:none");

   }
   
   
function toggleilist(lettr)
{
   var azcollection=document.getElementById('azcontentcontainer').getElementsByTagName("div");
   var thisDiv="";
   var thismatchname="ilist_"+lettr;
   var i=0;
   var azlen=azcollection.length; 
   for(i=0; i<azlen; i++)
   {
      thisDiv=azcollection[i];
      if(thisDiv.id==thismatchname) { alert("matched "+thismatchname); togglelayer(thismatchname,1); }
      else { alert("NOT matched "+thismatchname); togglelayer(thismatchname,0); }
   }
}