var my_window;
var xmlHttp = new Array();
var globId = new Array();

/** TOOLTIP INITIALISATION **/
var DH = 0;
var an = 0;
var al = 0;
var ai = 0;
var browserVersion;

if (document.getElementById) 
{
 //alert("IE 5+");
 //IE 5+
 ai = 1; DH = 1;
}
else 
{
 if (document.all) 
 {
  //alert("IE 4+");
  //IE 4+
  al = 1; DH = 1;
 } 
 else 
 {
  browserVersion = parseInt(navigator.appVersion); 
  if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) 
  {
    //alert("NS");
    //NS
    an = 1; DH = 1;
  }
 }
}


function callPerl(val,url,id)
{
  xmlHttp[val] = null;
   globId[val] = id;
  try { /*Firefox, Opera 8.0+, Safari*/ xmlHttp[val] = new XMLHttpRequest(); } catch (e) { 
    try { /*Internet Explorer*/ xmlHttp[val] = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {  
      try { xmlHttp[val] = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { xmlHttp[val] = null; }
    }
  }

  if (xmlHttp[val] != null)
  {
    xmlHttp[val].onreadystatechange = function () { state_Change(val); };
    xmlHttp[val].open("GET",url,true); //Using "POST" seems to cause errors, so used "GET" instead 25-SEP-09
    xmlHttp[val].send(null);
  }
}

function state_Change(val)
{
//  alert("state_Change["+val+"]\nxmlHttp["+val+"].readyState="+xmlHttp[val].readyState+"\nxmlHttp["+val+"].status="+xmlHttp[val].status);
  if (xmlHttp[val].readyState==4) 
  {
    if (xmlHttp[val].status==200) 
    {                                                                                                    
      document.getElementById(globId[val]).innerHTML=xmlHttp[val].responseText;
    }
    else                                                                             
    {
//      document.getElementById(globId[val]).innerHTML = 'state_Change['+val+'] err: xmlHttp['+val+'].status = '+xmlHttp[val].status;
      document.getElementById(globId[val]).innerHTML = 'Loading...';
    }
  }
  else
  {
//    document.getElementById(globId[val]).innerHTML = 'state_Change['+val+'] err: xmlHttp['+val+'].readyState = '+xmlHttp[val].readyState;
    document.getElementById(globId[val]).innerHTML = 'Loading...';
  }
}


function popWin(wintitle,txt)
{
//  alert("hello");
  if (my_window) {my_window.close();}
 my_window = window.open("","mywindow1","width=350,height=350,resizable=1"); 
 my_window.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
 my_window.document.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
 my_window.document.write("<head><script type=\"text/javascript\" src=\"./churchScript.js\"></script>\n");
 my_window.document.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso\-8859\-1\"/>\n");
 my_window.document.write("<title>"+wintitle+"</title>\n");
 my_window.document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://127.0.0.1/style.css\" />\n");
 my_window.document.write("</head>\n");
 my_window.document.write("<body>\n");
 my_window.document.write("<br><a href=\"#\" onClick=\"javascript:self.close();\">Close Window</a><br><br>\n");
 my_window.document.write("<div id=\"wrapper\">\n");
 my_window.document.write("<div id=\"twocolumn\">\n");
 my_window.document.write(txt);  
 my_window.document.write("</div>\n");
 my_window.document.write("</div>\n");
 my_window.document.write("</body>\n");
 my_window.document.write("</html>\n");
}

/** TOOLTIP CODE **/

function fd(oi, wS) 
{
 if (ai) return wS ? document.getElementById(oi).style : document.getElementById(oi); 
 if (al) return wS ? document.all[oi].style: document.all[oi]; 
 if (an) return document.layers[oi];
}

function pw() 
{
 return window.innerWidth != null ? window.innerWidth : document.body.clientWidth != null ? document.body.clientWidth : null;
}

function mouseX(evt) 
{
 if (evt.pageX) return evt.pageX; 
 else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); 
 else return null;
}

function mouseY(evt) 
{
 if (evt.pageY) return evt.pageY; 
 else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); 
 else return null;
}

function popUp(evt,oi) 
{
 if (DH) 
 {
  var wp = pw(); 
  ds = fd(oi,1); 
  dm = fd(oi,0); 
  st = ds.visibility; 
  if (dm.offsetWidth) ew = dm.offsetWidth; 
  else if (dm.clip.width) ew = dm.clip.width; 
  
  if (st == "visible" || st == "show") 
  { 
   ds.visibility = "hidden"; 
  } 
  else 
  {
   tv = mouseY(evt) + 20; 
   lv = mouseX(evt);//(ew/4); 
   if (lv < 2) lv = 2; 
   else if (lv + ew > wp) lv -= ew/2; 
   
   if (!an) 
   {
    lv += 'px';tv += 'px';
   }
   ds.left = lv;
   ds.top = tv;
   ds.visibility = "visible";
  }
 }
}

/*** POPUP WINDOW CODE ***/

function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height=blanket_height/2-150;//150 is half popup's height
	popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-150;//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}
function WinPopup(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);		
}

function changeFontSize(inc) {
  var arr = new Array();
  arr[0] = document.getElementsByTagName('p');
  for(lp=0; lp<arr.length; lp++) 
  {
    p = arr[lp];
    for(n=0; n<p.length; n++) 
    {
      if(p[n].style.fontSize) 
      {
        var size = parseInt(p[n].style.fontSize.replace("px", ""));
      }
      else 
      {
        var size = 12;
      }
      p[n].style.fontSize = size+inc + 'px';   
    }
  }
}

function openFile(file) {
//  if (my_window) 
//  {
//    alert("my_window is open"); 
//    my_window.focus();
//    my_window.opener = my_window;
//    alert("my_window.opener = >"+my_window.opener+"<"); 
//    my_window.close();
//  }
  my_window = window.open(file,"my_window","left=20,top=20,width=800,height=500,toolbar=0,resizable=1,location=0,status=0");
}

//-----------------------------------------------------------------------------
// function insEventRow
//-----------------------------------------------------------------------------
function insEventRow(location) {

 var tbody = document.getElementById("events").getElementsByTagName("TBODY")[0];
 var currRows = document.getElementById("events").getElementsByTagName("TR").length;

 var inputParamCount = currRows;

 var newRow = document.createElement("TR");
 var newRowId = currRows + 1;

 var Column1 = document.createElement("TD");
 Column1.setAttribute("valign","top");
 Column1.setAttribute("nowrap",true);

 var Column2 = document.createElement("TD");
 Column2.setAttribute("valign","top");
 Column2.setAttribute("nowrap",true);

 var Column3 = document.createElement("TD");
 Column3.setAttribute("valign","top");
 Column3.setAttribute("nowrap",true);

 var Column4 = document.createElement("TD");
 Column4.setAttribute("valign","top");
 Column4.setAttribute("nowrap",true);
 
 var input1 = document.createElement("INPUT");
 input1.setAttribute("type", "text");
 input1.setAttribute("size", "6");
 input1.setAttribute("maxlength", "6");
 input1.setAttribute("name", "in_date");
 input1.setAttribute("id", "in_date_"+newRowId);

 var input2 = document.createElement("INPUT");
 input2.setAttribute("type", "text");
 input2.setAttribute("size", "15");
 input2.setAttribute("name", "in_time");
 input2.setAttribute("id", "in_time_"+newRowId);
 
 var input3 = document.createElement("INPUT");
 input3.setAttribute("type", "text");
 input3.setAttribute("size", "25");
 input3.setAttribute("name", "in_item");
 input3.setAttribute("id", "in_item_"+newRowId);

 var input4 = document.createElement("INPUT");
 input4.setAttribute("type", "text");
 input4.setAttribute("size", "80");
 input4.setAttribute("name", "in_details");
 input4.setAttribute("id", "in_details_"+newRowId);

 Column1.appendChild(input1);
 Column2.appendChild(input2);
 Column3.appendChild(input3);
 Column4.appendChild(input4);

 newRow.setAttribute("id","events" + newRowId);         
 newRow.appendChild(Column1);
 newRow.appendChild(Column2);
 newRow.appendChild(Column3);
 newRow.appendChild(Column4);

 var refObj = document.getElementById(location);
 if (refObj != null) 
 {
  refObj.parentNode.insertBefore(newRow, refObj);
 }

 inputParamCount++;
}

