function getPageSizeWithScroll(){
  if (window.innerHeight && window.scrollMaxY) {// Firefox
    yWithScroll = window.innerHeight + window.scrollMaxY;
    xWithScroll = window.innerWidth + window.scrollMaxX;
	//yWithScroll = document.body.clientHeight;
	//xWithScroll = document.body.clientWidth;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    yWithScroll = document.body.scrollHeight;
    xWithScroll = document.body.scrollWidth;
  } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
    yWithScroll = document.body.offsetHeight;
    xWithScroll = document.body.offsetWidth;
    }
	
  if((window.innerHeight)  > yWithScroll) yWithScroll = window.innerHeight - window.scrollMaxY;
	
  arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
  //alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
  //alert(window.innerHeight);
  return {width: xWithScroll, height: yWithScroll };//arrayPageSizeWithScroll;
}

/*
  method : POST/GET
  url    : Call url
  func   : custom function which is used to process returned data,
           takes one parameter: the data returned by a call to url.
*/
function downloadUrl(method, url, data, func) {
  var httpObj;
  var browser = navigator.appName;
  if(browser.indexOf("Microsoft") > -1)
    httpObj = new ActiveXObject("Microsoft.XMLHTTP");
  else
    httpObj = new XMLHttpRequest();
  httpObj.open(method, url, true);
  httpObj.onreadystatechange = function() {
    if(httpObj.readyState == 4){
       if (httpObj.status == 200 || httpObj.status == 0) {
          var contenttype = httpObj.getResponseHeader('Content-Type');
          if (contenttype.indexOf('xml') > -1) {
             func(httpObj.responseXML);
          } else {
             func(httpObj.responseText);
          }
       } else {
          func('Error: '+httpObj.status);
       }
    }
  };
  if (data.length > 0) {
    httpObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  }
  httpObj.send(data);
}

function createDiv(id, top, left, width, height, classname) {
  var divNew = document.createElement('div');
  divNew.setAttribute('id', id);
  divNew.className        = classname;
  divNew.style.position   = "absolute";
  divNew.style.width      = width;
  divNew.style.height     = height;
  divNew.style.left       = left;
  divNew.style.top        = top;
  divNew.style.visibility = 'hidden';
 	return divNew;
}


function disablePage() {
  // Create a new div tag to grey out the page 
  var divDisable = createDiv('divDisable', 0, 0, getPageSizeWithScroll().width, getPageSizeWithScroll().height, 'pageDisable');
  divDisable.style.visibility = 'visible';
  
  divDisable.style.zIndex = 450;
  	if(!(divDisable.style.width))
		{
			divDisable.style.width=getPageSizeWithScroll().width+"px";
			divDisable.style.height=getPageSizeWithScroll().height+"px";
		}
  document.body.appendChild(divDisable);
  // Returns a function that, when called, 'reenables' the page
  return function () {
           divDisable.style.visibility = 'hidden';
           document.body.removeChild(divDisable);
         };
}

function makeDialog(variables, template, blockid) {
  var closeHTML    = "<div class='dialog_close'><a href='javascript:document.getElementById(\"divDialog\").close();'>close [x]</a></div>";
  var structureXML = "<structure><column width=\"\" id=\"1\"><cell id=\"1\"><block id=\""+blockid+"\" open=\"\" close=\"\" assign=\"\" btype=\"\"><template>"+template+"</template></block></cell></column></structure>";
  var requestData  = variables+"&structure=" + structureXML + "\r\n";
  var enablePage = disablePage();
  downloadUrl('POST', "/ajax.php", requestData, function (content) {
    // Use a throw away div to get the sizes for the dialog box 
    // based on the returned content. Using the displayed div 
    // gives wierd resizing problems in IE
    var divSizer = createDiv('divSizer', '', '', '580px', '', '');
	divSizer.innerHTML = content;
	document.body.appendChild(divSizer);
	// Calculate the top and left positions for the dialog based on 
    // the size of the window and the dialog.
	var offset = window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop;
    var width  = window.innerWidth ||document.documentElement.clientWidth; //document.body.clientWidth
    var height = window.innerHeight||document.documentElement.clientHeight; //document.body.clientHeight
    var top    = offset + (height/2) - (divSizer.clientHeight / 2);
    var left   = (width/2) - (divSizer.clientWidth / 2);
    top  = top  > 0 ? top  : 0;
    left = left > 0 ? left : 0;
	// Create the div that acts as the dialog box and set various propertiesdivSizer.clientWidth
  	var divDialog = createDiv('divDialog', top, left, divSizer.clientWidth + 'px', '', 'dialogBox');
    document.body.removeChild(divSizer);
    document.body.appendChild(divDialog);
    divDialog.innerHTML = closeHTML + content;
    divDialog.align = 'center';
    divDialog.style.visibility = 'visible';
    divDialog.style.zIndex = 500;
	if(!(divDialog.style.top))
		{
			divDialog.style.top=top+'px';
			divDialog.style.left=left+'px';
		}
    divDialog.close = function () {
      divDialog.style.visibility = 'hidden';
      document.body.removeChild(divDialog);
      enablePage();
    };
    // If the content has any forms, change them so that they
    // submit quietly. Then, reload the current page.
    var aForms = divDialog.getElementsByTagName("FORM");
    for (i = 0; i < aForms.length; i++) {
      var form = aForms[i];
      form.onsubmit = function () {
        var submission_string = "";
        for (var elems=0; elems<form.length;elems++) {
          var elem = form.elements[elems];
          if (elem.name != "" && ((elem.type != 'radio' && elem.type != 'checkbox') || elem.checked)) {
            submission_string += elem.name + "=" + elem.value + "&";
          }
        }
        submission_string += "\r\n";
        downloadUrl('POST', '/index.php', submission_string, function () {
            // Obey ret_link element in ajax forms if it exists. 
            // Transfer to that page after executing the form post.
            // Also, if a _ajax_ret_vars element exists, add its value
            // to the end of the current location and reload.
            var next_page = "";
            if (form.ret_link != null) {
              next_page += form.ret_link.value;
            } else {
              next_page += window.location;
            }
            if (form._ajax_ret_vars != null) {
              if (next_page.indexOf('?') != -1)
                next_page += "&" + form._ajax_ret_vars.value;
              else
                next_page += "?" + form._ajax_ret_vars.value;
            }
            window.location = next_page;
            divDialog.close(); 
        });
        return false;  
      };
    }
  } );
}


/*
    AJAX REQUEST
    The return of this Object is just a plain text.
*/

var Familylink = {
    index:"",
    form:"",
    URI:"",
    defaultURI:"/ajax.php",
    post : function(form,func){
        var serialized_form = Form.serialize(form,true);
        var blockid = serialized_form.handler;
        var template = "pages/ajax_bare.tpl";
        serialized_form.structure = "<structure><column width=\"\" id=\"1\"><cell id=\"1\"><block id=\""+blockid+"\" open=\"\" close=\"\" assign=\"\" btype=\"\"><template>"+template+"</template></block></cell></column></structure>";
        var onComplete = this.onComplete.bind(this,func);
        var URI = this.URI == "" ? this.defaultURI : this.URI;
        new Ajax.Request(URI,{
            url:URI,
            method:'post',
            parameters:serialized_form,
            onComplete: onComplete,
            onLoading:this.showAjaxLoading.bind(this)
            });
    },
    onComplete: function(){
        this.hideAjaxLoading();
        arguments[0](arguments[1]);
    },
    showAjaxLoading: function(){
        var err_div = document.getElementById("clubs_research_links_error");
        err_div.innerHTML = "<img src=\"/themes/css/gfx/ajax-loader.gif\" />";
    },
    hideAjaxLoading: function(){
        var err_div = $("clubs_research_links_error");
        err_div.innerHTML = "";
        Element.hide(err_div);
    }
}
// Modification 8.2.2007 
// for familyTree
function makeDialogEx(url, data) {
  var closeHTML    = "<div class='dialog_close'><a href='javascript:top.location.href = document.location.href;javascript:document.getElementById(\"divDialog\").close();'>close [x]</a></div>";
  //var structureXML = "<structure><column width=\"\" id=\"1\"><cell id=\"1\"><block id=\""+blockid+"\" open=\"\" close=\"\" assign=\"\" btype=\"\"><template>"+template+"</template></block></cell></column></structure>";
  //var requestData  = variables+"&structure=" + structureXML + "\r\n";
  var url2 = 'http://'+window.location.hostname+'/index.php?'+url+data;
	if (document.getElementById("divDisable"))	document.body.removeChild(document.getElementById("divDisable"));
	  var enablePage = disablePage();
  doAsyncRequest('POST', url2, data, function (content) {
    // Use a throw away div to get the sizes for the dialog box 
    // based on the returned content. Using the displayed div 
    // gives wierd resizing problems in IE
    var divSizer = createDiv('divSizer', '', '', '580px', '', '');
    divSizer.innerHTML = content;
    document.body.appendChild(divSizer);
	// Calculate the top and left positions for the dialog based on 
    // the size of the window and the dialog.
    var offset = window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop;
    var width  = window.innerWidth ||document.documentElement.clientWidth; //document.body.clientWidth
    var height = window.innerHeight||document.documentElement.clientHeight; //document.body.clientHeight
    var top    = offset + (height/2) - (divSizer.clientHeight / 2);
    var left   = (width/2) - (divSizer.clientWidth / 2);
    top  = top  > 0 ? top  : 0;
    left = left > 0 ? left : 0;
	if (document.getElementById("divDialog")) document.body.removeChild(document.getElementById("divDialog"));	
		var divDialog = createDiv('divDialog', top, left, divSizer.clientWidth + 'px', '', 'dialogBox');
    document.body.removeChild(divSizer);
    document.body.appendChild(divDialog);
    divDialog.innerHTML = closeHTML + content;
    divDialog.align = 'center';
    divDialog.style.visibility = 'visible';
    divDialog.style.zIndex = 500;
	if(!(divDialog.style.top))	{ divDialog.style.top=top+'px'; }
	if(!(divDialog.style.left))	{ divDialog.style.left=left+'px';} //if

    divDialog.close = function () {
      divDialog.style.visibility = 'hidden';
      document.body.removeChild(divDialog);
      enablePage();
    }; //function

    // If the content has any forms, change them so that they
    // submit quietly. Then, reload the current page.
    var aForms = divDialog.getElementsByTagName("FORM");
    for (i = 0; i < aForms.length; i++) {
      var form = aForms[i];
      form.onsubmit = function () {
        var submission_string = "";
        for (var elems=0; elems<form.length;elems++) {
          var elem = form.elements[elems];
          if (elem.name != "" && ((elem.type != 'radio' && elem.type != 'checkbox') || elem.checked)) {
            submission_string += elem.name + "=" + elem.value + "&";
          } //if
        } //for
        submission_string += "\r\n";
        downloadUrl('POST', '/index.php', submission_string, function () {
            // Obey ret_link element in ajax forms if it exists. 
            // Transfer to that page after executing the form post.
            // Also, if a _ajax_ret_vars element exists, add its value
            // to the end of the current location and reload.
            var next_page = "";
            if (form.ret_link != null) {
              next_page += form.ret_link.value;
            } //if
			else {
              next_page += window.location;
            } //else
            if (form._ajax_ret_vars != null) {
              if (next_page.indexOf('?') != -1)
                next_page += "&" + form._ajax_ret_vars.value;
              else
                next_page += "?" + form._ajax_ret_vars.value;
            } //if
            window.location = next_page;
            divDialog.close(); 
        }); //function
        return false;  
      };
    }

  } );
}

function doAsyncRequest(method, url, data, func) {
  var httpObj;
  var browser = navigator.appName;
  if(browser.indexOf("Microsoft") > -1)
    httpObj = new ActiveXObject("Microsoft.XMLHTTP");
  else
    httpObj = new XMLHttpRequest();
  httpObj.open(method, url, true);
  httpObj.onreadystatechange = function() {
    if(httpObj.readyState == 4){
       if (httpObj.status == 200 || httpObj.status == 0) {
          var contenttype = httpObj.getResponseHeader('Content-Type');
          if (contenttype.indexOf('xml') > -1) {
             func(httpObj.responseXML);
          } else {
             func(httpObj.responseText);
          }
       } else {
          func('Error: '+httpObj.status);
       }
    }
  };
  if (data.length > 0) {
   httpObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  }
 httpObj.send(data);
}

function add_fm_data()
	{
		var gender = document.add_fm.gender.value;
		var tree = document.add_fm.tree.value;
		var personID = document.add_fm.id.value;
		var famc = document.add_fm.famc.value;
		var fm = document.add_fm.fm.value;

		var fname = document.add_fm.firstname.value;
		var lname = document.add_fm.lastname.value;
		var bdate = document.add_fm.birthdate.value;
		var bplace = document.add_fm.birthplace.value;
		var ddate = document.add_fm.deathdate.value;
		var dplace = document.add_fm.deathplace.value;
		var email = document.add_fm.email.value;
	   	for (i=0; i<document.add_fm.stat.length; i++) 
	   	      {
	         if (document.add_fm.stat[i].checked==true)
    			 {     stat =document.add_fm.stat[i].value   } //if
		      } //for
		if(stat == 'L') {ddate = ""; dplace = "";stat=1;} //living
		else if(stat == 'D') {email = "";stat=0;} //deceased

		
		if(fname.length == 0) 
				{
				if(lname.length == 0)
					{
						alert("Please enter name.");	
						return false;
					}
				}

		var linkstr  = "&gender="+gender; 
		    linkstr += "&tree="+tree; 
			linkstr += "&id="+personID;
			linkstr += "&famc="+famc;
			linkstr += "&firstname="+fname;
			linkstr += "&lastname="+lname;
			linkstr += "&birthdate="+bdate;
			linkstr += "&birthplace="+bplace;
			linkstr += "&living="+stat;
			linkstr += "&deathdate="+ddate;
			linkstr += "&deathplace="+dplace;
			linkstr += "&email="+email;
			linkstr += "&fm="+fm;
		//alert(linkstr);
		makeDialogEx('page=tree&tree_page=admin/addmother_father', linkstr);
	}

	function add_fm_data_thisperson()
	{
		var gender = document.add_fm.gender.value;
		var tree = document.add_fm.tree.value;
		var personID = document.add_fm.id.value;
		var famc = document.add_fm.famc.value;
		var fm = document.add_fm.fm.value;

		var fname = document.add_fm.firstname.value;
		var lname = document.add_fm.lastname.value;
		var bdate = document.add_fm.birthdate.value;
		var bplace = document.add_fm.birthplace.value;
		var ddate = document.add_fm.deathdate.value;
		var dplace = document.add_fm.deathplace.value;
		var email = document.add_fm.email.value;
	   	for (i=0; i<document.add_fm.stat.length; i++) 
	   	      {
	         if (document.add_fm.stat[i].checked==true)
    			 {     stat =document.add_fm.stat[i].value   } //if
		      } //for
		if(stat == 'L') {ddate = ""; dplace = "";stat=1;} //living
		else if(stat == 'D') {email = "";stat=0;} //deceased

		
		if(fname.length == 0) 
				{
				if(lname.length == 0)
					{
						alert("Please enter name.");	
						return false;
					}
				}

		var linkstr  = "&gender="+gender; 
		    linkstr += "&tree="+tree; 
			linkstr += "&id="+personID;
			linkstr += "&famc="+famc;
			linkstr += "&firstname="+fname;
			linkstr += "&lastname="+lname;
			linkstr += "&birthdate="+bdate;
			linkstr += "&birthplace="+bplace;
			linkstr += "&living="+stat;
			linkstr += "&deathdate="+ddate;
			linkstr += "&deathplace="+dplace;
			linkstr += "&email="+email;
			linkstr += "&fm="+fm;
		//alert(linkstr);
		makeDialogEx('page=tree&tree_page=admin/addthisperson', linkstr);
	}
function add_name_data()
	{
		var fname = document.add_name.firstname.value;
		var lname = document.add_name.lastname.value;

			if(fname.length == 0) 
				{
				if(lname.length == 0)
					{
						alert("Please enter name for First Generation.");	
						return false;
					}
				}
		var bdate = document.add_name.birthdate.value;
		var bplace = document.add_name.birthplace.value;
		var ddate = document.add_name.deathdate.value;
		var dplace = document.add_name.deathplace.value;
		var email = document.add_name.email.value;
		

		   for (i=0; i<document.add_name.gender.length; i++) 
		   	      {
		         if (document.add_name.gender[i].checked==true)
        			 {
		            gender =document.add_name.gender[i].value
         			} //if
			      } //for
		   for (i=0; i<document.add_name.stat.length; i++) 
	   	      {
	         if (document.add_name.stat[i].checked==true)
    			 {     stat =document.add_name.stat[i].value   } //if
		      } //for
		if(stat == 'L') {ddate = ""; dplace = "";stat=1;} //living
		else if(stat == 'D') {email = "";stat=0;} //deceased
				  
		var input = new Array(fname,lname,bdate,bplace,ddate,dplace,gender,true,stat,email);
		var input_name = new Array('fname','lname','bdate','bplace','ddate','dplace','gender','submit','stat','email');

		   for (i=0; i<input.length; i++) 
		   	{
				add_name_element(input_name[i], input[i]);
			}	
	 document.form1.submit();
	 document.getElementById('divDialog').close(); 
	}//function
function add_name_element(id, value)
	{
		var frm = document.form1;
		 if(document.getElementById("firstslot_"+id)==null){
			var firstslot=document.createElement('input');
				firstslot.setAttribute('id','firstslot_'+id);
				firstslot.setAttribute('type','hidden');
				firstslot.setAttribute('name','firstslot_'+id);
				firstslot.setAttribute('value',value);
				frm.appendChild(firstslot);
		} //if
	}
function add_child_data()
	{
		var tree = document.add_fm.tree.value;
		var personID = document.add_fm.id.value;
		var famc = document.add_fm.famc.value;
		var fname = document.add_fm.firstname.value;
		var lname = document.add_fm.lastname.value;
		var bdate = document.add_fm.birthdate.value;
		var bplace = document.add_fm.birthplace.value;
		var ddate = document.add_fm.deathdate.value;
		var dplace = document.add_fm.deathplace.value;
		var email = document.add_fm.email.value;
		
		   for (i=0; i<document.add_fm.gender.length; i++) 
	   	      {
	         if (document.add_fm.gender[i].checked==true)
    			 {     gender =document.add_fm.gender[i].value   } //if
		      } //for
		   for (i=0; i<document.add_fm.stat.length; i++) 
	   	      {
	         if (document.add_fm.stat[i].checked==true)
    			 {     stat =document.add_fm.stat[i].value   } //if
		      } //for
		if(stat == 'L') {ddate = ""; dplace = "";stat=1;} //living
		else if(stat == 'D') {email = "";stat=0;} //deceased
		if(fname.length == 0) 
				{
				if(lname.length == 0)
					{
						alert("Please enter name.");	
						return false;
					}
				}

		var linkstr  = "&gender="+gender; 
		    linkstr += "&tree="+tree; 
			linkstr += "&id="+personID;
			linkstr += "&famc="+famc;
			linkstr += "&firstname="+fname;
			linkstr += "&lastname="+lname;
			linkstr += "&birthdate="+bdate;
			linkstr += "&birthplace="+bplace;
			linkstr += "&living="+stat;
			linkstr += "&deathdate="+ddate;
			linkstr += "&deathplace="+dplace;
			linkstr += "&email="+email;
		//alert(linkstr);
		makeDialogEx('page=tree&tree_page=admin/addchild', linkstr);
	}
function show_email(){
	//show_email
	document.getElementById('email_str').style.display='block';	
	document.getElementById('email_add').style.display='block';	
	//hide death
	document.getElementById('ddate').style.display='none';	
	document.getElementById('ddate_str').style.display='none';	
	document.getElementById('dplace').style.display='none';	
	document.getElementById('dplace_str').style.display='none';	
}//show_email
function show_death(){
	//hide email
	document.getElementById('email_str').style.display='none';	
	document.getElementById('email_add').style.display='none';	
	//show death
	document.getElementById('ddate').style.display='block';	
	document.getElementById('ddate_str').style.display='block';	
	document.getElementById('dplace').style.display='block';	
	document.getElementById('dplace_str').style.display='block';	
}//show_death